commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / vendor / dompdf / dompdf / src / Css / Stylesheet.php
1 <?php
2 /**
3 * @package dompdf
4 * @link http://dompdf.github.com/
5 * @author Benj Carson <benjcarson@digitaljunkies.ca>
6 * @author Helmut Tischer <htischer@weihenstephan.org>
7 * @author Fabien Ménager <fabien.menager@gmail.com>
8 * @license http://www.gnu.org/copyleft/lesser.html GNU Lesser General Public License
9 */
10 namespace Dompdf\Css;
11
12 use DOMXPath;
13 use Dompdf\Dompdf;
14 use Dompdf\Helpers;
15 use Dompdf\Exception;
16 use Dompdf\FontMetrics;
17 use Dompdf\Frame\FrameTree;
18
19 /**
20 * The master stylesheet class
21 *
22 * The Stylesheet class is responsible for parsing stylesheets and style
23 * tags/attributes. It also acts as a registry of the individual Style
24 * objects generated by the current set of loaded CSS files and style
25 * elements.
26 *
27 * @see Style
28 * @package dompdf
29 */
30 class Stylesheet
31 {
32 /**
33 * The location of the default built-in CSS file.
34 */
35 const DEFAULT_STYLESHEET = "/lib/res/html.css";
36
37 /**
38 * User agent stylesheet origin
39 *
40 * @var int
41 */
42 const ORIG_UA = 1;
43
44 /**
45 * User normal stylesheet origin
46 *
47 * @var int
48 */
49 const ORIG_USER = 2;
50
51 /**
52 * Author normal stylesheet origin
53 *
54 * @var int
55 */
56 const ORIG_AUTHOR = 3;
57
58 private static $_stylesheet_origins = array(
59 self::ORIG_UA => -0x0FFFFFFF, // user agent style sheets
60 self::ORIG_USER => -0x0000FFFF, // user normal style sheets
61 self::ORIG_AUTHOR => 0x00000000, // author normal style sheets
62 );
63
64 /**
65 * Current dompdf instance
66 *
67 * @var Dompdf
68 */
69 private $_dompdf;
70
71 /**
72 * Array of currently defined styles
73 *
74 * @var Style[]
75 */
76 private $_styles;
77
78 /**
79 * Base protocol of the document being parsed
80 * Used to handle relative urls.
81 *
82 * @var string
83 */
84 private $_protocol;
85
86 /**
87 * Base hostname of the document being parsed
88 * Used to handle relative urls.
89 *
90 * @var string
91 */
92 private $_base_host;
93
94 /**
95 * Base path of the document being parsed
96 * Used to handle relative urls.
97 *
98 * @var string
99 */
100 private $_base_path;
101
102 /**
103 * The styles defined by @page rules
104 *
105 * @var array<Style>
106 */
107 private $_page_styles;
108
109 /**
110 * List of loaded files, used to prevent recursion
111 *
112 * @var array
113 */
114 private $_loaded_files;
115
116 /**
117 * Current stylesheet origin
118 *
119 * @var int
120 */
121 private $_current_origin = self::ORIG_UA;
122
123 /**
124 * Accepted CSS media types
125 * List of types and parsing rules for future extensions:
126 * http://www.w3.org/TR/REC-html40/types.html
127 * screen, tty, tv, projection, handheld, print, braille, aural, all
128 * The following are non standard extensions for undocumented specific environments.
129 * static, visual, bitmap, paged, dompdf
130 * Note, even though the generated pdf file is intended for print output,
131 * the desired content might be different (e.g. screen or projection view of html file).
132 * Therefore allow specification of content by dompdf setting Options::defaultMediaType.
133 * If given, replace media "print" by Options::defaultMediaType.
134 * (Previous version $ACCEPTED_MEDIA_TYPES = $ACCEPTED_GENERIC_MEDIA_TYPES + $ACCEPTED_DEFAULT_MEDIA_TYPE)
135 */
136 static $ACCEPTED_DEFAULT_MEDIA_TYPE = "print";
137 static $ACCEPTED_GENERIC_MEDIA_TYPES = array("all", "static", "visual", "bitmap", "paged", "dompdf");
138
139 /**
140 * @var FontMetrics
141 */
142 private $fontMetrics;
143
144 /**
145 * The class constructor.
146 *
147 * The base protocol, host & path are initialized to those of
148 * the current script.
149 */
150 function __construct(Dompdf $dompdf)
151 {
152 $this->_dompdf = $dompdf;
153 $this->setFontMetrics($dompdf->getFontMetrics());
154 $this->_styles = array();
155 $this->_loaded_files = array();
156 list($this->_protocol, $this->_base_host, $this->_base_path) = Helpers::explode_url($_SERVER["SCRIPT_FILENAME"]);
157 $this->_page_styles = array("base" => null);
158 }
159
160 /**
161 * Set the base protocol
162 *
163 * @param string $protocol
164 */
165 function set_protocol($protocol)
166 {
167 $this->_protocol = $protocol;
168 }
169
170 /**
171 * Set the base host
172 *
173 * @param string $host
174 */
175 function set_host($host)
176 {
177 $this->_base_host = $host;
178 }
179
180 /**
181 * Set the base path
182 *
183 * @param string $path
184 */
185 function set_base_path($path)
186 {
187 $this->_base_path = $path;
188 }
189
190 /**
191 * Return the Dompdf object
192 *
193 * @return Dompdf
194 */
195 function get_dompdf()
196 {
197 return $this->_dompdf;
198 }
199
200 /**
201 * Return the base protocol for this stylesheet
202 *
203 * @return string
204 */
205 function get_protocol()
206 {
207 return $this->_protocol;
208 }
209
210 /**
211 * Return the base host for this stylesheet
212 *
213 * @return string
214 */
215 function get_host()
216 {
217 return $this->_base_host;
218 }
219
220 /**
221 * Return the base path for this stylesheet
222 *
223 * @return string
224 */
225 function get_base_path()
226 {
227 return $this->_base_path;
228 }
229
230 /**
231 * Return the array of page styles
232 *
233 * @return Style[]
234 */
235 function get_page_styles()
236 {
237 return $this->_page_styles;
238 }
239
240 /**
241 * Add a new Style object to the stylesheet
242 * add_style() adds a new Style object to the current stylesheet, or
243 * merges a new Style with an existing one.
244 *
245 * @param string $key the Style's selector
246 * @param Style $style the Style to be added
247 *
248 * @throws \Dompdf\Exception
249 */
250 function add_style($key, Style $style)
251 {
252 if (!is_string($key)) {
253 throw new Exception("CSS rule must be keyed by a string.");
254 }
255
256 if (isset($this->_styles[$key])) {
257 $this->_styles[$key]->merge($style);
258 } else {
259 $this->_styles[$key] = clone $style;
260 }
261
262 $this->_styles[$key]->set_origin($this->_current_origin);
263 }
264
265 /**
266 * lookup a specifc Style object
267 *
268 * lookup() returns the Style specified by $key, or null if the Style is
269 * not found.
270 *
271 * @param string $key the selector of the requested Style
272 * @return Style
273 */
274 function lookup($key)
275 {
276 if (!isset($this->_styles[$key])) {
277 return null;
278 }
279
280 return $this->_styles[$key];
281 }
282
283 /**
284 * create a new Style object associated with this stylesheet
285 *
286 * @param Style $parent The style of this style's parent in the DOM tree
287 * @return Style
288 */
289 function create_style(Style $parent = null)
290 {
291 return new Style($this, $this->_current_origin);
292 }
293
294 /**
295 * load and parse a CSS string
296 *
297 * @param string $css
298 */
299 function load_css(&$css)
300 {
301 $this->_parse_css($css);
302 }
303
304
305 /**
306 * load and parse a CSS file
307 *
308 * @param string $file
309 * @param int $origin
310 */
311 function load_css_file($file, $origin = self::ORIG_AUTHOR)
312 {
313 if ($origin) {
314 $this->_current_origin = $origin;
315 }
316
317 // Prevent circular references
318 if (isset($this->_loaded_files[$file])) {
319 return;
320 }
321
322 $this->_loaded_files[$file] = true;
323
324 if (strpos($file, "data:") === 0) {
325 $parsed = Helpers::parse_data_uri($file);
326 $css = $parsed["data"];
327 } else {
328 $parsed_url = Helpers::explode_url($file);
329
330 list($this->_protocol, $this->_base_host, $this->_base_path, $filename) = $parsed_url;
331
332 // Fix submitted by Nick Oostveen for aliased directory support:
333 if ($this->_protocol == "") {
334 $file = $this->_base_path . $filename;
335 } else {
336 $file = Helpers::build_url($this->_protocol, $this->_base_host, $this->_base_path, $filename);
337 }
338
339 set_error_handler(array("\\Dompdf\\Helpers", "record_warnings"));
340 $css = file_get_contents($file, null, $this->_dompdf->get_http_context());
341 restore_error_handler();
342
343 $good_mime_type = true;
344
345 // See http://the-stickman.com/web-development/php/getting-http-response-headers-when-using-file_get_contents/
346 if (isset($http_response_header) && !$this->_dompdf->get_quirksmode()) {
347 foreach ($http_response_header as $_header) {
348 if (preg_match("@Content-Type:\s*([\w/]+)@i", $_header, $matches) &&
349 ($matches[1] !== "text/css")
350 ) {
351 $good_mime_type = false;
352 }
353 }
354 }
355
356 if (!$good_mime_type || $css == "") {
357 Helpers::record_warnings(E_USER_WARNING, "Unable to load css file $file", __FILE__, __LINE__);
358 return;
359 }
360 }
361
362 $this->_parse_css($css);
363 }
364
365 /**
366 * @link http://www.w3.org/TR/CSS21/cascade.html#specificity
367 *
368 * @param string $selector
369 * @param int $origin :
370 * - ua: user agent style sheets
371 * - un: user normal style sheets
372 * - an: author normal style sheets
373 * - ai: author important style sheets
374 * - ui: user important style sheets
375 *
376 * @return int
377 */
378 private function _specificity($selector, $origin = self::ORIG_AUTHOR)
379 {
380 // http://www.w3.org/TR/CSS21/cascade.html#specificity
381 // ignoring the ":" pseudoclass modifiers
382 // also ignored in _css_selector_to_xpath
383
384 $a = ($selector === "!attr") ? 1 : 0;
385
386 $b = min(mb_substr_count($selector, "#"), 255);
387
388 $c = min(mb_substr_count($selector, ".") +
389 mb_substr_count($selector, "["), 255);
390
391 $d = min(mb_substr_count($selector, " ") +
392 mb_substr_count($selector, ">") +
393 mb_substr_count($selector, "+"), 255);
394
395 //If a normal element name is at the beginning of the string,
396 //a leading whitespace might have been removed on whitespace collapsing and removal
397 //therefore there might be one whitespace less as selected element names
398 //this can lead to a too small specificity
399 //see _css_selector_to_xpath
400
401 if (!in_array($selector[0], array(" ", ">", ".", "#", "+", ":", "[")) /* && $selector !== "*"*/) {
402 $d++;
403 }
404
405 if ($this->_dompdf->get_option('debugCss')) {
406 /*DEBUGCSS*/
407 print "<pre>\n";
408 /*DEBUGCSS*/
409 printf("_specificity(): 0x%08x \"%s\"\n", ($a << 24) | ($b << 16) | ($c << 8) | ($d), $selector);
410 /*DEBUGCSS*/
411 print "</pre>";
412 }
413
414 return self::$_stylesheet_origins[$origin] + ($a << 24) | ($b << 16) | ($c << 8) | ($d);
415 }
416
417 /**
418 * Converts a CSS selector to an XPath query.
419 *
420 * @param string $selector
421 * @param bool $first_pass
422 *
423 * @throws Exception
424 * @return string
425 */
426 private function _css_selector_to_xpath($selector, $first_pass = false)
427 {
428
429 // Collapse white space and strip whitespace around delimiters
430 // $search = array("/\\s+/", "/\\s+([.>#+:])\\s+/");
431 // $replace = array(" ", "\\1");
432 // $selector = preg_replace($search, $replace, trim($selector));
433
434 // Initial query (non-absolute)
435 $query = "//";
436
437 // Will contain :before and :after if they must be created
438 $pseudo_elements = array();
439
440 // Parse the selector
441 //$s = preg_split("/([ :>.#+])/", $selector, -1, PREG_SPLIT_DELIM_CAPTURE);
442
443 $delimiters = array(" ", ">", ".", "#", "+", ":", "[", "(");
444
445 // Add an implicit * at the beginning of the selector
446 // if it begins with an attribute selector
447 if ($selector[0] === "[") {
448 $selector = "*$selector";
449 }
450
451 // Add an implicit space at the beginning of the selector if there is no
452 // delimiter there already.
453 if (!in_array($selector[0], $delimiters)) {
454 $selector = " $selector";
455 }
456
457 $tok = "";
458 $len = mb_strlen($selector);
459 $i = 0;
460
461 while ($i < $len) {
462
463 $s = $selector[$i];
464 $i++;
465
466 // Eat characters up to the next delimiter
467 $tok = "";
468 $in_attr = false;
469
470 while ($i < $len) {
471 $c = $selector[$i];
472 $c_prev = $selector[$i - 1];
473
474 if (!$in_attr && in_array($c, $delimiters)) {
475 break;
476 }
477
478 if ($c_prev === "[") {
479 $in_attr = true;
480 }
481
482 $tok .= $selector[$i++];
483
484 if ($in_attr && $c === "]") {
485 $in_attr = false;
486 break;
487 }
488 }
489
490 switch ($s) {
491
492 case " ":
493 case ">":
494 // All elements matching the next token that are direct children of
495 // the current token
496 $expr = $s === " " ? "descendant" : "child";
497
498 if (mb_substr($query, -1, 1) !== "/") {
499 $query .= "/";
500 }
501
502 // Tag names are case-insensitive
503 $tok = strtolower($tok);
504
505 if (!$tok) {
506 $tok = "*";
507 }
508
509 $query .= "$expr::$tok";
510 $tok = "";
511 break;
512
513 case ".":
514 case "#":
515 // All elements matching the current token with a class/id equal to
516 // the _next_ token.
517
518 $attr = $s === "." ? "class" : "id";
519
520 // empty class/id == *
521 if (mb_substr($query, -1, 1) === "/") {
522 $query .= "*";
523 }
524
525 // Match multiple classes: $tok contains the current selected
526 // class. Search for class attributes with class="$tok",
527 // class=".* $tok .*" and class=".* $tok"
528
529 // This doesn't work because libxml only supports XPath 1.0...
530 //$query .= "[matches(@$attr,\"^${tok}\$|^${tok}[ ]+|[ ]+${tok}\$|[ ]+${tok}[ ]+\")]";
531
532 // Query improvement by Michael Sheakoski <michael@mjsdigital.com>:
533 $query .= "[contains(concat(' ', @$attr, ' '), concat(' ', '$tok', ' '))]";
534 $tok = "";
535 break;
536
537 case "+":
538 // All sibling elements that folow the current token
539 if (mb_substr($query, -1, 1) !== "/") {
540 $query .= "/";
541 }
542
543 $query .= "following-sibling::$tok";
544 $tok = "";
545 break;
546
547 case ":":
548 $i2 = $i - strlen($tok) - 2; // the char before ":"
549 if (!isset($selector[$i2]) || in_array($selector[$i2], $delimiters)) {
550 $query .= "*";
551 }
552
553 $last = false;
554
555 // Pseudo-classes
556 switch ($tok) {
557
558 case "first-child":
559 $query .= "[1]";
560 $tok = "";
561 break;
562
563 case "last-child":
564 $query .= "[not(following-sibling::*)]";
565 $tok = "";
566 break;
567
568 case "first-of-type":
569 $query .= "[position() = 1]";
570 $tok = "";
571 break;
572
573 case "last-of-type":
574 $query .= "[position() = last()]";
575 $tok = "";
576 break;
577
578 // an+b, n, odd, and even
579 case "nth-last-of-type":
580 case "nth-last-child":
581 $last = true;
582
583 case "nth-of-type":
584 case "nth-child":
585 $p = $i + 1;
586 $nth = trim(mb_substr($selector, $p, strpos($selector, ")", $i) - $p));
587
588 // 1
589 if (preg_match("/^\d+$/", $nth)) {
590 $condition = "position() = $nth";
591 } // odd
592 elseif ($nth === "odd") {
593 $condition = "(position() mod 2) = 1";
594 } // even
595 elseif ($nth === "even") {
596 $condition = "(position() mod 2) = 0";
597 } // an+b
598 else {
599 $condition = $this->_selector_an_plus_b($nth, $last);
600 }
601
602 $query .= "[$condition]";
603 $tok = "";
604 break;
605
606 case "link":
607 $query .= "[@href]";
608 $tok = "";
609 break;
610
611 case "first-line": // TODO
612 case "first-letter": // TODO
613
614 // N/A
615 case "active":
616 case "hover":
617 case "visited":
618 $query .= "[false()]";
619 $tok = "";
620 break;
621
622 /* Pseudo-elements */
623 case "before":
624 case "after":
625 if ($first_pass) {
626 $pseudo_elements[$tok] = $tok;
627 } else {
628 $query .= "/*[@$tok]";
629 }
630
631 $tok = "";
632 break;
633
634 case "empty":
635 $query .= "[not(*) and not(normalize-space())]";
636 $tok = "";
637 break;
638
639 case "disabled":
640 case "checked":
641 $query .= "[@$tok]";
642 $tok = "";
643 break;
644
645 case "enabled":
646 $query .= "[not(@disabled)]";
647 $tok = "";
648 break;
649 }
650
651 break;
652
653 case "[":
654 // Attribute selectors. All with an attribute matching the following token(s)
655 $attr_delimiters = array("=", "]", "~", "|", "$", "^", "*");
656 $tok_len = mb_strlen($tok);
657 $j = 0;
658
659 $attr = "";
660 $op = "";
661 $value = "";
662
663 while ($j < $tok_len) {
664 if (in_array($tok[$j], $attr_delimiters)) {
665 break;
666 }
667 $attr .= $tok[$j++];
668 }
669
670 switch ($tok[$j]) {
671
672 case "~":
673 case "|":
674 case "$":
675 case "^":
676 case "*":
677 $op .= $tok[$j++];
678
679 if ($tok[$j] !== "=") {
680 throw new Exception("Invalid CSS selector syntax: invalid attribute selector: $selector");
681 }
682
683 $op .= $tok[$j];
684 break;
685
686 case "=":
687 $op = "=";
688 break;
689
690 }
691
692 // Read the attribute value, if required
693 if ($op != "") {
694 $j++;
695 while ($j < $tok_len) {
696 if ($tok[$j] === "]") {
697 break;
698 }
699 $value .= $tok[$j++];
700 }
701 }
702
703 if ($attr == "") {
704 throw new Exception("Invalid CSS selector syntax: missing attribute name");
705 }
706
707 $value = trim($value, "\"'");
708
709 switch ($op) {
710
711 case "":
712 $query .= "[@$attr]";
713 break;
714
715 case "=":
716 $query .= "[@$attr=\"$value\"]";
717 break;
718
719 case "~=":
720 // FIXME: this will break if $value contains quoted strings
721 // (e.g. [type~="a b c" "d e f"])
722 $values = explode(" ", $value);
723 $query .= "[";
724
725 foreach ($values as $val) {
726 $query .= "@$attr=\"$val\" or ";
727 }
728
729 $query = rtrim($query, " or ") . "]";
730 break;
731
732 case "|=":
733 $values = explode("-", $value);
734 $query .= "[";
735
736 foreach ($values as $val) {
737 $query .= "starts-with(@$attr, \"$val\") or ";
738 }
739
740 $query = rtrim($query, " or ") . "]";
741 break;
742
743 case "$=":
744 $query .= "[substring(@$attr, string-length(@$attr)-" . (strlen($value) - 1) . ")=\"$value\"]";
745 break;
746
747 case "^=":
748 $query .= "[starts-with(@$attr,\"$value\")]";
749 break;
750
751 case "*=":
752 $query .= "[contains(@$attr,\"$value\")]";
753 break;
754 }
755
756 break;
757 }
758 }
759 $i++;
760
761 // case ":":
762 // // Pseudo selectors: ignore for now. Partially handled directly
763 // // below.
764
765 // // Skip until the next special character, leaving the token as-is
766 // while ( $i < $len ) {
767 // if ( in_array($selector[$i], $delimiters) )
768 // break;
769 // $i++;
770 // }
771 // break;
772
773 // default:
774 // // Add the character to the token
775 // $tok .= $selector[$i++];
776 // break;
777 // }
778
779 // }
780
781
782 // Trim the trailing '/' from the query
783 if (mb_strlen($query) > 2) {
784 $query = rtrim($query, "/");
785 }
786
787 return array("query" => $query, "pseudo_elements" => $pseudo_elements);
788 }
789
790 // https://github.com/tenderlove/nokogiri/blob/master/lib/nokogiri/css/xpath_visitor.rb
791 protected function _selector_an_plus_b($expr, $last = false)
792 {
793 $expr = preg_replace("/\s/", "", $expr);
794 if (!preg_match("/^(?P<a>-?[0-9]*)?n(?P<b>[-+]?[0-9]+)?$/", $expr, $matches)) {
795 return "false()";
796 }
797
798 $a = ((isset($matches["a"]) && $matches["a"] !== "") ? intval($matches["a"]) : 1);
799 $b = ((isset($matches["b"]) && $matches["b"] !== "") ? intval($matches["b"]) : 0);
800
801 $position = ($last ? "(last()-position()+1)" : "position()");
802
803 if ($b == 0) {
804 return "($position mod $a) = 0";
805 } else {
806 $compare = (($a < 0) ? "<=" : ">=");
807 $b2 = -$b;
808 if ($b2 >= 0) {
809 $b2 = "+$b2";
810 }
811 return "($position $compare $b) and ((($position $b2) mod " . abs($a) . ") = 0)";
812 }
813 }
814
815 /**
816 * applies all current styles to a particular document tree
817 *
818 * apply_styles() applies all currently loaded styles to the provided
819 * {@link FrameTree}. Aside from parsing CSS, this is the main purpose
820 * of this class.
821 *
822 * @param \Dompdf\Frame\FrameTree $tree
823 */
824 function apply_styles(FrameTree $tree)
825 {
826 // Use XPath to select nodes. This would be easier if we could attach
827 // Frame objects directly to DOMNodes using the setUserData() method, but
828 // we can't do that just yet. Instead, we set a _node attribute_ in
829 // Frame->set_id() and use that as a handle on the Frame object via
830 // FrameTree::$_registry.
831
832 // We create a scratch array of styles indexed by frame id. Once all
833 // styles have been assigned, we order the cached styles by specificity
834 // and create a final style object to assign to the frame.
835
836 // FIXME: this is not particularly robust...
837
838 $styles = array();
839 $xp = new DOMXPath($tree->get_dom());
840
841 // Add generated content
842 foreach ($this->_styles as $selector => $style) {
843 if (strpos($selector, ":before") === false && strpos($selector, ":after") === false) {
844 continue;
845 }
846
847 $query = $this->_css_selector_to_xpath($selector, true);
848
849 // Retrieve the nodes, limit to body for generated content
850 $nodes = @$xp->query('.' . $query["query"]);
851 if ($nodes == null) {
852 Helpers::record_warnings(E_USER_WARNING, "The CSS selector '$selector' is not valid", __FILE__, __LINE__);
853 continue;
854 }
855
856 foreach ($nodes as $node) {
857 foreach ($query["pseudo_elements"] as $pos) {
858 // Do not add a new pseudo element if another one already matched
859 if ($node->hasAttribute("dompdf_{$pos}_frame_id")) {
860 continue;
861 }
862
863 if (($src = $this->_image($style->content)) !== "none") {
864 $new_node = $node->ownerDocument->createElement("img_generated");
865 $new_node->setAttribute("src", $src);
866 } else {
867 $new_node = $node->ownerDocument->createElement("dompdf_generated");
868 }
869
870 $new_node->setAttribute($pos, $pos);
871 $new_frame_id = $tree->insert_node($node, $new_node, $pos);
872 $node->setAttribute("dompdf_{$pos}_frame_id", $new_frame_id);
873 }
874 }
875 }
876
877 // Apply all styles in stylesheet
878 foreach ($this->_styles as $selector => $style) {
879 $query = $this->_css_selector_to_xpath($selector);
880
881 // Retrieve the nodes
882 $nodes = @$xp->query($query["query"]);
883 if ($nodes == null) {
884 Helpers::record_warnings(E_USER_WARNING, "The CSS selector '$selector' is not valid", __FILE__, __LINE__);
885 continue;
886 }
887
888 foreach ($nodes as $node) {
889 // Retrieve the node id
890 // Only DOMElements get styles
891 if ($node->nodeType != XML_ELEMENT_NODE) {
892 continue;
893 }
894
895 $id = $node->getAttribute("frame_id");
896
897 // Assign the current style to the scratch array
898 $spec = $this->_specificity($selector);
899 $styles[$id][$spec][] = $style;
900 }
901 }
902
903 // Now create the styles and assign them to the appropriate frames. (We
904 // iterate over the tree using an implicit FrameTree iterator.)
905 $root_flg = false;
906 foreach ($tree->get_frames() as $frame) {
907 // Helpers::pre_r($frame->get_node()->nodeName . ":");
908 if (!$root_flg && $this->_page_styles["base"]) {
909 $style = $this->_page_styles["base"];
910 $root_flg = true;
911 } else {
912 $style = $this->create_style();
913 }
914
915 // Find nearest DOMElement parent
916 $p = $frame;
917 while ($p = $p->get_parent()) {
918 if ($p->get_node()->nodeType == XML_ELEMENT_NODE) {
919 break;
920 }
921 }
922
923 // Styles can only be applied directly to DOMElements; anonymous
924 // frames inherit from their parent
925 if ($frame->get_node()->nodeType != XML_ELEMENT_NODE) {
926 if ($p) {
927 $style->inherit($p->get_style());
928 }
929
930 $frame->set_style($style);
931 continue;
932 }
933
934 $id = $frame->get_id();
935
936 // Handle HTML 4.0 attributes
937 AttributeTranslator::translate_attributes($frame);
938 if (($str = $frame->get_node()->getAttribute(AttributeTranslator::$_style_attr)) !== "") {
939 // Lowest specificity
940 $styles[$id][1][] = $this->_parse_properties($str);
941 }
942
943 // Locate any additional style attributes
944 if (($str = $frame->get_node()->getAttribute("style")) !== "") {
945 // Destroy CSS comments
946 $str = preg_replace("'/\*.*?\*/'si", "", $str);
947
948 $spec = $this->_specificity("!attr");
949 $styles[$id][$spec][] = $this->_parse_properties($str);
950 }
951
952 // Grab the applicable styles
953 if (isset($styles[$id])) {
954
955 $applied_styles = $styles[$frame->get_id()];
956
957 // Sort by specificity
958 ksort($applied_styles);
959
960 if ($this->_dompdf->get_option('debugCss')) {
961 $debug_nodename = $frame->get_node()->nodeName;
962 print "<pre>\n[$debug_nodename\n";
963 foreach ($applied_styles as $spec => $arr) {
964 printf("specificity: 0x%08x\n", $spec);
965 foreach ($arr as $s) {
966 print "[\n";
967 $s->debug_print();
968 print "]\n";
969 }
970 }
971 }
972
973 // Merge the new styles with the inherited styles
974 foreach ($applied_styles as $arr) {
975 foreach ($arr as $s) {
976 $style->merge($s);
977 }
978 }
979 }
980
981 // Inherit parent's styles if required
982 if ($p) {
983
984 if ($this->_dompdf->get_option('debugCss')) {
985 print "inherit:\n";
986 print "[\n";
987 $p->get_style()->debug_print();
988 print "]\n";
989 }
990
991 $style->inherit($p->get_style());
992 }
993
994 if ($this->_dompdf->get_option('debugCss')) {
995 print "DomElementStyle:\n";
996 print "[\n";
997 $style->debug_print();
998 print "]\n";
999 print "/$debug_nodename]\n</pre>";
1000 }
1001
1002 /*DEBUGCSS print: see below different print debugging method
1003 Helpers::pre_r($frame->get_node()->nodeName . ":");
1004 echo "<pre>";
1005 echo $style;
1006 echo "</pre>";*/
1007 $frame->set_style($style);
1008
1009 }
1010
1011 // We're done! Clean out the registry of all styles since we
1012 // won't be needing this later.
1013 foreach (array_keys($this->_styles) as $key) {
1014 $this->_styles[$key] = null;
1015 unset($this->_styles[$key]);
1016 }
1017
1018 }
1019
1020 /**
1021 * parse a CSS string using a regex parser
1022 * Called by {@link Stylesheet::parse_css()}
1023 *
1024 * @param string $str
1025 *
1026 * @throws Exception
1027 */
1028 private function _parse_css($str)
1029 {
1030
1031 $str = trim($str);
1032
1033 // Destroy comments and remove HTML comments
1034 $css = preg_replace(array(
1035 "'/\*.*?\*/'si",
1036 "/^<!--/",
1037 "/-->$/"
1038 ), "", $str);
1039
1040 // FIXME: handle '{' within strings, e.g. [attr="string {}"]
1041
1042 // Something more legible:
1043 $re =
1044 "/\s* # Skip leading whitespace \n" .
1045 "( @([^\s{]+)\s*([^{;]*) (?:;|({)) )? # Match @rules followed by ';' or '{' \n" .
1046 "(?(1) # Only parse sub-sections if we're in an @rule... \n" .
1047 " (?(4) # ...and if there was a leading '{' \n" .
1048 " \s*( (?:(?>[^{}]+) ({)? # Parse rulesets and individual @page rules \n" .
1049 " (?(6) (?>[^}]*) }) \s*)+? \n" .
1050 " ) \n" .
1051 " }) # Balancing '}' \n" .
1052 "| # Branch to match regular rules (not preceded by '@')\n" .
1053 "([^{]*{[^}]*})) # Parse normal rulesets\n" .
1054 "/xs";
1055
1056 if (preg_match_all($re, $css, $matches, PREG_SET_ORDER) === false) {
1057 // An error occurred
1058 throw new Exception("Error parsing css file: preg_match_all() failed.");
1059 }
1060
1061 // After matching, the array indicies are set as follows:
1062 //
1063 // [0] => complete text of match
1064 // [1] => contains '@import ...;' or '@media {' if applicable
1065 // [2] => text following @ for cases where [1] is set
1066 // [3] => media types or full text following '@import ...;'
1067 // [4] => '{', if present
1068 // [5] => rulesets within media rules
1069 // [6] => '{', within media rules
1070 // [7] => individual rules, outside of media rules
1071 //
1072 //Helpers::pre_r($matches);
1073 foreach ($matches as $match) {
1074 $match[2] = trim($match[2]);
1075
1076 if ($match[2] !== "") {
1077 // Handle @rules
1078 switch ($match[2]) {
1079
1080 case "import":
1081 $this->_parse_import($match[3]);
1082 break;
1083
1084 case "media":
1085 $acceptedmedia = self::$ACCEPTED_GENERIC_MEDIA_TYPES;
1086 $acceptedmedia[] = $this->_dompdf->get_option("default_media_type");
1087
1088 $media = preg_split("/\s*,\s*/", mb_strtolower(trim($match[3])));
1089
1090 if (count(array_intersect($acceptedmedia, $media))) {
1091 $this->_parse_sections($match[5]);
1092 }
1093 break;
1094
1095 case "page":
1096 //This handles @page to be applied to page oriented media
1097 //Note: This has a reduced syntax:
1098 //@page { margin:1cm; color:blue; }
1099 //Not a sequence of styles like a full.css, but only the properties
1100 //of a single style, which is applied to the very first "root" frame before
1101 //processing other styles of the frame.
1102 //Working properties:
1103 // margin (for margin around edge of paper)
1104 // font-family (default font of pages)
1105 // color (default text color of pages)
1106 //Non working properties:
1107 // border
1108 // padding
1109 // background-color
1110 //Todo:Reason is unknown
1111 //Other properties (like further font or border attributes) not tested.
1112 //If a border or background color around each paper sheet is desired,
1113 //assign it to the <body> tag, possibly only for the css of the correct media type.
1114
1115 // If the page has a name, skip the style.
1116 $page_selector = trim($match[3]);
1117
1118 $key = null;
1119 switch ($page_selector) {
1120 case "":
1121 $key = "base";
1122 break;
1123
1124 case ":left":
1125 case ":right":
1126 case ":odd":
1127 case ":even":
1128 case ":first":
1129 $key = $page_selector;
1130
1131 default:
1132 continue;
1133 }
1134
1135 // Store the style for later...
1136 if (empty($this->_page_styles[$key])) {
1137 $this->_page_styles[$key] = $this->_parse_properties($match[5]);
1138 } else {
1139 $this->_page_styles[$key]->merge($this->_parse_properties($match[5]));
1140 }
1141 break;
1142
1143 case "font-face":
1144 $this->_parse_font_face($match[5]);
1145 break;
1146
1147 default:
1148 // ignore everything else
1149 break;
1150 }
1151
1152 continue;
1153 }
1154
1155 if ($match[7] !== "") {
1156 $this->_parse_sections($match[7]);
1157 }
1158
1159 }
1160 }
1161
1162 /* See also style.cls Style::_image(), refactoring?, works also for imported css files */
1163 protected function _image($val)
1164 {
1165 $DEBUGCSS = $this->_dompdf->get_option('debugCss');
1166 $parsed_url = "none";
1167
1168 if (mb_strpos($val, "url") === false) {
1169 $path = "none"; //Don't resolve no image -> otherwise would prefix path and no longer recognize as none
1170 } else {
1171 $val = preg_replace("/url\(['\"]?([^'\")]+)['\"]?\)/", "\\1", trim($val));
1172
1173 // Resolve the url now in the context of the current stylesheet
1174 $parsed_url = Helpers::explode_url($val);
1175 if ($parsed_url["protocol"] == "" && $this->get_protocol() == "") {
1176 if ($parsed_url["path"][0] === '/' || $parsed_url["path"][0] === '\\') {
1177 $path = $_SERVER["DOCUMENT_ROOT"] . '/';
1178 } else {
1179 $path = $this->get_base_path();
1180 }
1181
1182 $path .= $parsed_url["path"] . $parsed_url["file"];
1183 $path = realpath($path);
1184 // If realpath returns FALSE then specifically state that there is no background image
1185 // FIXME: Is this causing problems for imported CSS files? There are some './none' references when running the test cases.
1186 if (!$path) {
1187 $path = 'none';
1188 }
1189 } else {
1190 $path = Helpers::build_url($this->get_protocol(),
1191 $this->get_host(),
1192 $this->get_base_path(),
1193 $val);
1194 }
1195 }
1196
1197 if ($DEBUGCSS) {
1198 print "<pre>[_image\n";
1199 print_r($parsed_url);
1200 print $this->get_protocol() . "\n" . $this->get_base_path() . "\n" . $path . "\n";
1201 print "_image]</pre>";;
1202 }
1203
1204 return $path;
1205 }
1206
1207 /**
1208 * parse @import{} sections
1209 *
1210 * @param string $url the url of the imported CSS file
1211 */
1212 private function _parse_import($url)
1213 {
1214 $arr = preg_split("/[\s\n,]/", $url, -1, PREG_SPLIT_NO_EMPTY);
1215 $url = array_shift($arr);
1216 $accept = false;
1217
1218 if (count($arr) > 0) {
1219 $acceptedmedia = self::$ACCEPTED_GENERIC_MEDIA_TYPES;
1220 $acceptedmedia[] = $this->_dompdf->get_option("default_media_type");
1221
1222 // @import url media_type [media_type...]
1223 foreach ($arr as $type) {
1224 if (in_array(mb_strtolower(trim($type)), $acceptedmedia)) {
1225 $accept = true;
1226 break;
1227 }
1228 }
1229
1230 } else {
1231 // unconditional import
1232 $accept = true;
1233 }
1234
1235 if ($accept) {
1236 // Store our current base url properties in case the new url is elsewhere
1237 $protocol = $this->_protocol;
1238 $host = $this->_base_host;
1239 $path = $this->_base_path;
1240
1241 // $url = str_replace(array('"',"url", "(", ")"), "", $url);
1242 // If the protocol is php, assume that we will import using file://
1243 // $url = Helpers::build_url($protocol == "php://" ? "file://" : $protocol, $host, $path, $url);
1244 // Above does not work for subfolders and absolute urls.
1245 // Todo: As above, do we need to replace php or file to an empty protocol for local files?
1246
1247 $url = $this->_image($url);
1248
1249 $this->load_css_file($url);
1250
1251 // Restore the current base url
1252 $this->_protocol = $protocol;
1253 $this->_base_host = $host;
1254 $this->_base_path = $path;
1255 }
1256
1257 }
1258
1259 /**
1260 * parse @font-face{} sections
1261 * http://www.w3.org/TR/css3-fonts/#the-font-face-rule
1262 *
1263 * @param string $str CSS @font-face rules
1264 * @return Style
1265 */
1266 private function _parse_font_face($str)
1267 {
1268 $descriptors = $this->_parse_properties($str);
1269
1270 preg_match_all("/(url|local)\s*\([\"\']?([^\"\'\)]+)[\"\']?\)\s*(format\s*\([\"\']?([^\"\'\)]+)[\"\']?\))?/i", $descriptors->src, $src);
1271
1272 $sources = array();
1273 $valid_sources = array();
1274
1275 foreach ($src[0] as $i => $value) {
1276 $source = array(
1277 "local" => strtolower($src[1][$i]) === "local",
1278 "uri" => $src[2][$i],
1279 "format" => $src[4][$i],
1280 "path" => Helpers::build_url($this->_protocol, $this->_base_host, $this->_base_path, $src[2][$i]),
1281 );
1282
1283 if (!$source["local"] && in_array($source["format"], array("", "truetype"))) {
1284 $valid_sources[] = $source;
1285 }
1286
1287 $sources[] = $source;
1288 }
1289
1290 // No valid sources
1291 if (empty($valid_sources)) {
1292 return;
1293 }
1294
1295 $style = array(
1296 "family" => $descriptors->get_font_family_raw(),
1297 "weight" => $descriptors->font_weight,
1298 "style" => $descriptors->font_style,
1299 );
1300
1301 $this->getFontMetrics()->registerFont($style, $valid_sources[0]["path"], $this->_dompdf->getHttpContext());
1302 }
1303
1304 /**
1305 * parse regular CSS blocks
1306 *
1307 * _parse_properties() creates a new Style object based on the provided
1308 * CSS rules.
1309 *
1310 * @param string $str CSS rules
1311 * @return Style
1312 */
1313 private function _parse_properties($str)
1314 {
1315 $properties = preg_split("/;(?=(?:[^\(]*\([^\)]*\))*(?![^\)]*\)))/", $str);
1316
1317 if ($this->_dompdf->get_option('debugCss')) print '[_parse_properties';
1318
1319 // Create the style
1320 $style = new Style($this, Stylesheet::ORIG_AUTHOR);
1321
1322 foreach ($properties as $prop) {
1323 // If the $prop contains an url, the regex may be wrong
1324 // @todo: fix the regex so that it works everytime
1325 /*if (strpos($prop, "url(") === false) {
1326 if (preg_match("/([a-z-]+)\s*:\s*[^:]+$/i", $prop, $m))
1327 $prop = $m[0];
1328 }*/
1329 //A css property can have " ! important" appended (whitespace optional)
1330 //strip this off to decode core of the property correctly.
1331 //Pass on in the style to allow proper handling:
1332 //!important properties can only be overridden by other !important ones.
1333 //$style->$prop_name = is a shortcut of $style->__set($prop_name,$value);.
1334 //If no specific set function available, set _props["prop_name"]
1335 //style is always copied completely, or $_props handled separately
1336 //Therefore set a _important_props["prop_name"]=true to indicate the modifier
1337
1338 /* Instead of short code, prefer the typical case with fast code
1339 $important = preg_match("/(.*?)!\s*important/",$prop,$match);
1340 if ( $important ) {
1341 $prop = $match[1];
1342 }
1343 $prop = trim($prop);
1344 */
1345 if ($this->_dompdf->get_option('debugCss')) print '(';
1346
1347 $important = false;
1348 $prop = trim($prop);
1349
1350 if (substr($prop, -9) === 'important') {
1351 $prop_tmp = rtrim(substr($prop, 0, -9));
1352
1353 if (substr($prop_tmp, -1) === '!') {
1354 $prop = rtrim(substr($prop_tmp, 0, -1));
1355 $important = true;
1356 }
1357 }
1358
1359 if ($prop === "") {
1360 if ($this->_dompdf->get_option('debugCss')) print 'empty)';
1361 continue;
1362 }
1363
1364 $i = mb_strpos($prop, ":");
1365 if ($i === false) {
1366 if ($this->_dompdf->get_option('debugCss')) print 'novalue' . $prop . ')';
1367 continue;
1368 }
1369
1370 $prop_name = rtrim(mb_strtolower(mb_substr($prop, 0, $i)));
1371 $value = ltrim(mb_substr($prop, $i + 1));
1372 if ($this->_dompdf->get_option('debugCss')) print $prop_name . ':=' . $value . ($important ? '!IMPORTANT' : '') . ')';
1373 //New style, anyway empty
1374 //if ($important || !$style->important_get($prop_name) ) {
1375 //$style->$prop_name = array($value,$important);
1376 //assignment might be replaced by overloading through __set,
1377 //and overloaded functions might check _important_props,
1378 //therefore set _important_props first.
1379 if ($important) {
1380 $style->important_set($prop_name);
1381 }
1382 //For easier debugging, don't use overloading of assignments with __set
1383 $style->$prop_name = $value;
1384 //$style->props_set($prop_name, $value);
1385 }
1386 if ($this->_dompdf->get_option('debugCss')) print '_parse_properties]';
1387
1388 return $style;
1389 }
1390
1391 /**
1392 * parse selector + rulesets
1393 *
1394 * @param string $str CSS selectors and rulesets
1395 */
1396 private function _parse_sections($str)
1397 {
1398 // Pre-process: collapse all whitespace and strip whitespace around '>',
1399 // '.', ':', '+', '#'
1400
1401 $patterns = array("/[\\s\n]+/", "/\\s+([>.:+#])\\s+/");
1402 $replacements = array(" ", "\\1");
1403 $str = preg_replace($patterns, $replacements, $str);
1404
1405 $sections = explode("}", $str);
1406 if ($this->_dompdf->get_option('debugCss')) print '[_parse_sections';
1407 foreach ($sections as $sect) {
1408 $i = mb_strpos($sect, "{");
1409
1410 $selectors = explode(",", mb_substr($sect, 0, $i));
1411 if ($this->_dompdf->get_option('debugCss')) print '[section';
1412 $style = $this->_parse_properties(trim(mb_substr($sect, $i + 1)));
1413
1414 // Assign it to the selected elements
1415 foreach ($selectors as $selector) {
1416 $selector = trim($selector);
1417
1418 if ($selector == "") {
1419 if ($this->_dompdf->get_option('debugCss')) print '#empty#';
1420 continue;
1421 }
1422 if ($this->_dompdf->get_option('debugCss')) print '#' . $selector . '#';
1423 //if ($this->_dompdf->get_option('debugCss')) { if (strpos($selector,'p') !== false) print '!!!p!!!#'; }
1424
1425 $this->add_style($selector, $style);
1426 }
1427
1428 if ($this->_dompdf->get_option('debugCss')) print 'section]';
1429 }
1430
1431 if ($this->_dompdf->get_option('debugCss')) print '_parse_sections]';
1432 }
1433
1434 public static function getDefaultStylesheet()
1435 {
1436 $dir = realpath(__DIR__ . "/../..");
1437 return $dir . self::DEFAULT_STYLESHEET;
1438 }
1439
1440 /**
1441 * @param FontMetrics $fontMetrics
1442 * @return $this
1443 */
1444 public function setFontMetrics(FontMetrics $fontMetrics)
1445 {
1446 $this->fontMetrics = $fontMetrics;
1447 return $this;
1448 }
1449
1450 /**
1451 * @return FontMetrics
1452 */
1453 public function getFontMetrics()
1454 {
1455 return $this->fontMetrics;
1456 }
1457
1458 /**
1459 * dumps the entire stylesheet as a string
1460 *
1461 * Generates a string of each selector and associated style in the
1462 * Stylesheet. Useful for debugging.
1463 *
1464 * @return string
1465 */
1466 function __toString()
1467 {
1468 $str = "";
1469 foreach ($this->_styles as $selector => $style) {
1470 $str .= "$selector => " . $style->__toString() . "\n";
1471 }
1472
1473 return $str;
1474 }
1475 }