1 | <?php |
---|
2 | /** |
---|
3 | * DOMPDF - PHP5 HTML to PDF renderer |
---|
4 | * |
---|
5 | * File: $RCSfile: stylesheet.cls.php,v $ |
---|
6 | * Created on: 2004-06-01 |
---|
7 | * |
---|
8 | * Copyright (c) 2004 - Benj Carson <benjcarson@digitaljunkies.ca> |
---|
9 | * |
---|
10 | * This library is free software; you can redistribute it and/or |
---|
11 | * modify it under the terms of the GNU Lesser General Public |
---|
12 | * License as published by the Free Software Foundation; either |
---|
13 | * version 2.1 of the License, or (at your option) any later version. |
---|
14 | * |
---|
15 | * This library is distributed in the hope that it will be useful, |
---|
16 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
18 | * Lesser General Public License for more details. |
---|
19 | * |
---|
20 | * You should have received a copy of the GNU Lesser General Public License |
---|
21 | * along with this library in the file LICENSE.LGPL; if not, write to the |
---|
22 | * Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA |
---|
23 | * 02111-1307 USA |
---|
24 | * |
---|
25 | * Alternatively, you may distribute this software under the terms of the |
---|
26 | * PHP License, version 3.0 or later. A copy of this license should have |
---|
27 | * been distributed with this file in the file LICENSE.PHP . If this is not |
---|
28 | * the case, you can obtain a copy at http://www.php.net/license/3_0.txt. |
---|
29 | * |
---|
30 | * The latest version of DOMPDF might be available at: |
---|
31 | * http://www.digitaljunkies.ca/dompdf |
---|
32 | * |
---|
33 | * @link http://www.digitaljunkies.ca/dompdf |
---|
34 | * @copyright 2004 Benj Carson |
---|
35 | * @author Benj Carson <benjcarson@digitaljunkies.ca> |
---|
36 | * @contributor Helmut Tischer <htischer@weihenstephan.org> |
---|
37 | * @package dompdf |
---|
38 | * @version 0.5.1 |
---|
39 | * |
---|
40 | * Changes |
---|
41 | * @contributor Helmut Tischer <htischer@weihenstephan.org> |
---|
42 | * @version 0.5.1.htischer.20090507 |
---|
43 | * - Specifity of css selector chain was too small because leading whitespace |
---|
44 | * to be counted as number of elements was removed |
---|
45 | * - On parsing css properties accept and register !important attribute |
---|
46 | * - Add optional debug output |
---|
47 | * @version 20090610 |
---|
48 | * - _parse_properties on style property name and value remove augmenting superflous |
---|
49 | * space for consistent parsing, in particular combined values like background |
---|
50 | */ |
---|
51 | |
---|
52 | /* $Id: stylesheet.cls.php 186 2009-10-19 22:42:06Z eclecticgeek@gmail.com $ */ |
---|
53 | |
---|
54 | /** |
---|
55 | * The location of the default built-in CSS file. |
---|
56 | * {@link Stylesheet::DEFAULT_STYLESHEET} |
---|
57 | */ |
---|
58 | define('__DEFAULT_STYLESHEET', DOMPDF_LIB_DIR . DIRECTORY_SEPARATOR . "res" . DIRECTORY_SEPARATOR . "html.css"); |
---|
59 | |
---|
60 | /** |
---|
61 | * The master stylesheet class |
---|
62 | * |
---|
63 | * The Stylesheet class is responsible for parsing stylesheets and style |
---|
64 | * tags/attributes. It also acts as a registry of the individual Style |
---|
65 | * objects generated by the current set of loaded CSS files and style |
---|
66 | * elements. |
---|
67 | * |
---|
68 | * @see Style |
---|
69 | * @package dompdf |
---|
70 | */ |
---|
71 | class Stylesheet { |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | /** |
---|
76 | * the location of the default built-in CSS file. |
---|
77 | * |
---|
78 | */ |
---|
79 | const DEFAULT_STYLESHEET = __DEFAULT_STYLESHEET; // Hack: can't |
---|
80 | // concatenate stuff in |
---|
81 | // const declarations, |
---|
82 | // but I can do this? |
---|
83 | // protected members |
---|
84 | |
---|
85 | /** |
---|
86 | * array of currently defined styles |
---|
87 | * @var array |
---|
88 | */ |
---|
89 | private $_styles; |
---|
90 | |
---|
91 | /** |
---|
92 | * base protocol of the document being parsed |
---|
93 | * |
---|
94 | * Used to handle relative urls. |
---|
95 | * |
---|
96 | * @var string |
---|
97 | */ |
---|
98 | private $_protocol; |
---|
99 | |
---|
100 | /** |
---|
101 | * base hostname of the document being parsed |
---|
102 | * |
---|
103 | * Used to handle relative urls. |
---|
104 | * @var string |
---|
105 | */ |
---|
106 | private $_base_host; |
---|
107 | |
---|
108 | /** |
---|
109 | * base path of the document being parsed |
---|
110 | * |
---|
111 | * Used to handle relative urls. |
---|
112 | * @var string |
---|
113 | */ |
---|
114 | private $_base_path; |
---|
115 | |
---|
116 | |
---|
117 | /** |
---|
118 | * the style defined by @page rules |
---|
119 | * |
---|
120 | * @var Style |
---|
121 | */ |
---|
122 | private $_page_style; |
---|
123 | |
---|
124 | |
---|
125 | /** |
---|
126 | * list of loaded files, used to prevent recursion |
---|
127 | * |
---|
128 | * @var array |
---|
129 | */ |
---|
130 | private $_loaded_files; |
---|
131 | |
---|
132 | /** |
---|
133 | * accepted CSS media types |
---|
134 | * List of types and parsing rules for future extensions: |
---|
135 | * http://www.w3.org/TR/REC-html40/types.html |
---|
136 | * screen, tty, tv, projection, handheld, print, braille, aural, all |
---|
137 | * The following are non standard extensions for undocumented specific environments. |
---|
138 | * static, visual, bitmap, paged |
---|
139 | * Note, even though the generated pdf file is intended for print output, |
---|
140 | * the desired content might be different (e.g. screen or projection view of html file). |
---|
141 | * Therefore allow specification of content by dompdf setting DOMPDF_DEFAULT_MEDIA_TYPE. |
---|
142 | * If given, replace media "print" by DOMPDF_DEFAULT_MEDIA_TYPE. |
---|
143 | * (Previous version $ACCEPTED_MEDIA_TYPES = $ACCEPTED_GENERIC_MEDIA_TYPES + $ACCEPTED_DEFAULT_MEDIA_TYPE) |
---|
144 | */ |
---|
145 | static $ACCEPTED_DEFAULT_MEDIA_TYPE = "print"; |
---|
146 | static $ACCEPTED_GENERIC_MEDIA_TYPES = array("all", "static", "visual", "bitmap", "paged"); |
---|
147 | |
---|
148 | /** |
---|
149 | * The class constructor. |
---|
150 | * |
---|
151 | * The base protocol, host & path are initialized to those of |
---|
152 | * the current script. |
---|
153 | */ |
---|
154 | function __construct() { |
---|
155 | $this->_styles = array(); |
---|
156 | $this->_loaded_files = array(); |
---|
157 | list($this->_protocol, $this->_base_host, $this->_base_path) = explode_url($_SERVER["SCRIPT_FILENAME"]); |
---|
158 | $this->_page_style = null; |
---|
159 | } |
---|
160 | |
---|
161 | /** |
---|
162 | * Set the base protocol |
---|
163 | * |
---|
164 | * @param string $proto |
---|
165 | */ |
---|
166 | function set_protocol($proto) { $this->_protocol = $proto; } |
---|
167 | |
---|
168 | /** |
---|
169 | * Set the base host |
---|
170 | * |
---|
171 | * @param string $host |
---|
172 | */ |
---|
173 | function set_host($host) { $this->_base_host = $host; } |
---|
174 | |
---|
175 | /** |
---|
176 | * Set the base path |
---|
177 | * |
---|
178 | * @param string $path |
---|
179 | */ |
---|
180 | function set_base_path($path) { $this->_base_path = $path; } |
---|
181 | |
---|
182 | |
---|
183 | /** |
---|
184 | * Return the base protocol for this stylesheet |
---|
185 | * |
---|
186 | * @return string |
---|
187 | */ |
---|
188 | function get_protocol() { return $this->_protocol; } |
---|
189 | |
---|
190 | /** |
---|
191 | * Return the base host for this stylesheet |
---|
192 | * |
---|
193 | * @return string |
---|
194 | */ |
---|
195 | function get_host() { return $this->_base_host; } |
---|
196 | |
---|
197 | /** |
---|
198 | * Return the base path for this stylesheet |
---|
199 | * |
---|
200 | * @return string |
---|
201 | */ |
---|
202 | function get_base_path() { return $this->_base_path; } |
---|
203 | |
---|
204 | /** |
---|
205 | * add a new Style object to the stylesheet |
---|
206 | * |
---|
207 | * add_style() adds a new Style object to the current stylesheet, or |
---|
208 | * merges a new Style with an existing one. |
---|
209 | * |
---|
210 | * @param string $key the Style's selector |
---|
211 | * @param Style $style the Style to be added |
---|
212 | */ |
---|
213 | function add_style($key, Style $style) { |
---|
214 | if (!is_string($key)) |
---|
215 | throw new DOMPDF_Exception("CSS rule must be keyed by a string."); |
---|
216 | |
---|
217 | if ( isset($this->_styles[$key]) ) |
---|
218 | $this->_styles[$key]->merge($style); |
---|
219 | else |
---|
220 | $this->_styles[$key] = clone $style; |
---|
221 | } |
---|
222 | |
---|
223 | |
---|
224 | /** |
---|
225 | * lookup a specifc Style object |
---|
226 | * |
---|
227 | * lookup() returns the Style specified by $key, or null if the Style is |
---|
228 | * not found. |
---|
229 | * |
---|
230 | * @param string $key the selector of the requested Style |
---|
231 | * @return Style |
---|
232 | */ |
---|
233 | function lookup($key) { |
---|
234 | if ( !isset($this->_styles[$key]) ) |
---|
235 | return null; |
---|
236 | |
---|
237 | return $this->_styles[$key]; |
---|
238 | } |
---|
239 | |
---|
240 | /** |
---|
241 | * create a new Style object associated with this stylesheet |
---|
242 | * |
---|
243 | * @param Style $parent The style of this style's parent in the DOM tree |
---|
244 | * @return Style |
---|
245 | */ |
---|
246 | function create_style($parent = null) { |
---|
247 | return new Style($this, $parent); |
---|
248 | } |
---|
249 | |
---|
250 | |
---|
251 | /** |
---|
252 | * load and parse a CSS string |
---|
253 | * |
---|
254 | * @param string $css |
---|
255 | */ |
---|
256 | function load_css(&$css) { $this->_parse_css($css); } |
---|
257 | |
---|
258 | |
---|
259 | /** |
---|
260 | * load and parse a CSS file |
---|
261 | * |
---|
262 | * @param string $file |
---|
263 | */ |
---|
264 | function load_css_file($file) { |
---|
265 | global $_dompdf_warnings; |
---|
266 | |
---|
267 | // Prevent circular references |
---|
268 | if ( isset($this->_loaded_files[$file]) ) |
---|
269 | return; |
---|
270 | |
---|
271 | $this->_loaded_files[$file] = true; |
---|
272 | $parsed_url = explode_url($file); |
---|
273 | |
---|
274 | list($this->_protocol, $this->_base_host, $this->_base_path, $filename) = $parsed_url; |
---|
275 | |
---|
276 | if ( !DOMPDF_ENABLE_REMOTE && |
---|
277 | ($this->_protocol != "" && $this->_protocol != "file://") ) { |
---|
278 | record_warnings(E_USER_WARNING, "Remote CSS file '$file' requested, but DOMPDF_ENABLE_REMOTE is false.", __FILE__, __LINE__); |
---|
279 | return; |
---|
280 | } |
---|
281 | |
---|
282 | // Fix submitted by Nick Oostveen for aliased directory support: |
---|
283 | if ( $this->_protocol == "" ) |
---|
284 | $file = $this->_base_path . $filename; |
---|
285 | else |
---|
286 | $file = build_url($this->_protocol, $this->_base_host, $this->_base_path, $filename); |
---|
287 | |
---|
288 | set_error_handler("record_warnings"); |
---|
289 | $css = file_get_contents($file); |
---|
290 | restore_error_handler(); |
---|
291 | |
---|
292 | if ( $css == "" ) { |
---|
293 | record_warnings(E_USER_WARNING, "Unable to load css file $file", __FILE__, __LINE__);; |
---|
294 | return; |
---|
295 | } |
---|
296 | |
---|
297 | $this->_parse_css($css); |
---|
298 | |
---|
299 | } |
---|
300 | |
---|
301 | /** |
---|
302 | * @link http://www.w3.org/TR/CSS21/cascade.html#specificity} |
---|
303 | * |
---|
304 | * @param string $selector |
---|
305 | * @return int |
---|
306 | */ |
---|
307 | private function _specificity($selector) { |
---|
308 | // http://www.w3.org/TR/CSS21/cascade.html#specificity |
---|
309 | // ignoring the ":" pseudoclass modifyers |
---|
310 | // also ignored in _css_selector_to_xpath |
---|
311 | |
---|
312 | $a = ($selector === "!style attribute") ? 1 : 0; |
---|
313 | |
---|
314 | $b = min(mb_substr_count($selector, "#"), 255); |
---|
315 | |
---|
316 | $c = min(mb_substr_count($selector, ".") + |
---|
317 | mb_substr_count($selector, ">") + |
---|
318 | mb_substr_count($selector, "+"), 255); |
---|
319 | |
---|
320 | $d = min(mb_substr_count($selector, " "), 255); |
---|
321 | |
---|
322 | //If a normal element name is at the begining of the string, |
---|
323 | //a leading whitespace might have been removed on whitespace collapsing and removal |
---|
324 | //therefore there might be one whitespace less as selected element names |
---|
325 | //this can lead to a too small specificity |
---|
326 | //see _css_selector_to_xpath |
---|
327 | |
---|
328 | if ( !in_array($selector{0}, array(" ", ">", ".", "#", "+", ":", "[")) ) { |
---|
329 | $d++; |
---|
330 | } |
---|
331 | |
---|
332 | if (DEBUGCSS) { |
---|
333 | /*DEBUGCSS*/ print "<pre>\n"; |
---|
334 | /*DEBUGCSS*/ printf("_specificity(): 0x%08x \"%s\"\n", ($a << 24) | ($b << 16) | ($c << 8) | ($d), $selector); |
---|
335 | /*DEBUGCSS*/ print "</pre>"; |
---|
336 | } |
---|
337 | |
---|
338 | return ($a << 24) | ($b << 16) | ($c << 8) | ($d); |
---|
339 | } |
---|
340 | |
---|
341 | |
---|
342 | /** |
---|
343 | * converts a CSS selector to an XPath query. |
---|
344 | * |
---|
345 | * @param string $selector |
---|
346 | * @return string |
---|
347 | */ |
---|
348 | private function _css_selector_to_xpath($selector) { |
---|
349 | |
---|
350 | // Collapse white space and strip whitespace around delimiters |
---|
351 | // $search = array("/\\s+/", "/\\s+([.>#+:])\\s+/"); |
---|
352 | // $replace = array(" ", "\\1"); |
---|
353 | // $selector = preg_replace($search, $replace, trim($selector)); |
---|
354 | |
---|
355 | // Initial query (non-absolute) |
---|
356 | $query = "//"; |
---|
357 | |
---|
358 | // Parse the selector |
---|
359 | //$s = preg_split('/([ :>.#+])/', $selector, -1, PREG_SPLIT_DELIM_CAPTURE); |
---|
360 | |
---|
361 | $delimiters = array(" ", ">", ".", "#", "+", ":", "["); |
---|
362 | |
---|
363 | // Add an implicit space at the beginning of the selector if there is no |
---|
364 | // delimiter there already. |
---|
365 | if ( !in_array($selector{0}, $delimiters) ) |
---|
366 | $selector = " $selector"; |
---|
367 | |
---|
368 | $tok = ""; |
---|
369 | $len = mb_strlen($selector); |
---|
370 | $i = 0; |
---|
371 | |
---|
372 | while ( $i < $len ) { |
---|
373 | |
---|
374 | $s = $selector{$i}; |
---|
375 | $i++; |
---|
376 | |
---|
377 | // Eat characters up to the next delimiter |
---|
378 | $tok = ""; |
---|
379 | |
---|
380 | while ($i < $len) { |
---|
381 | if ( in_array($selector{$i}, $delimiters) ) |
---|
382 | break; |
---|
383 | $tok .= $selector{$i++}; |
---|
384 | } |
---|
385 | |
---|
386 | switch ($s) { |
---|
387 | |
---|
388 | case " ": |
---|
389 | case ">": |
---|
390 | // All elements matching the next token that are direct children of |
---|
391 | // the current token |
---|
392 | $expr = $s == " " ? "descendant" : "child"; |
---|
393 | |
---|
394 | if ( mb_substr($query, -1, 1) != "/" ) |
---|
395 | $query .= "/"; |
---|
396 | |
---|
397 | if ( !$tok ) |
---|
398 | $tok = "*"; |
---|
399 | |
---|
400 | $query .= "$expr::$tok"; |
---|
401 | $tok = ""; |
---|
402 | break; |
---|
403 | |
---|
404 | case ".": |
---|
405 | case "#": |
---|
406 | // All elements matching the current token with a class/id equal to |
---|
407 | // the _next_ token. |
---|
408 | |
---|
409 | $attr = $s == "." ? "class" : "id"; |
---|
410 | |
---|
411 | // empty class/id == * |
---|
412 | if ( mb_substr($query, -1, 1) == "/" ) |
---|
413 | $query .= "*"; |
---|
414 | |
---|
415 | // Match multiple classes: $tok contains the current selected |
---|
416 | // class. Search for class attributes with class="$tok", |
---|
417 | // class=".* $tok .*" and class=".* $tok" |
---|
418 | |
---|
419 | // This doesn't work because libxml only supports XPath 1.0... |
---|
420 | //$query .= "[matches(@$attr,\"^${tok}\$|^${tok}[ ]+|[ ]+${tok}\$|[ ]+${tok}[ ]+\")]"; |
---|
421 | |
---|
422 | // Query improvement by Michael Sheakoski <michael@mjsdigital.com>: |
---|
423 | $query .= "[contains(concat(' ', @$attr, ' '), concat(' ', '$tok', ' '))]"; |
---|
424 | $tok = ""; |
---|
425 | break; |
---|
426 | |
---|
427 | case "+": |
---|
428 | // All sibling elements that folow the current token |
---|
429 | if ( mb_substr($query, -1, 1) != "/" ) |
---|
430 | $query .= "/"; |
---|
431 | |
---|
432 | $query .= "following-sibling::$tok"; |
---|
433 | $tok = ""; |
---|
434 | break; |
---|
435 | |
---|
436 | case ":": |
---|
437 | // Pseudo-classes |
---|
438 | switch ($tok) { |
---|
439 | |
---|
440 | case "first-child": |
---|
441 | $query .= "[1]"; |
---|
442 | $tok = ""; |
---|
443 | break; |
---|
444 | |
---|
445 | case "link": |
---|
446 | $query .= "[@href]"; |
---|
447 | $tok = ""; |
---|
448 | break; |
---|
449 | |
---|
450 | case "first-line": |
---|
451 | break; |
---|
452 | |
---|
453 | case "first-letter": |
---|
454 | break; |
---|
455 | |
---|
456 | case "before": |
---|
457 | break; |
---|
458 | |
---|
459 | case "after": |
---|
460 | break; |
---|
461 | |
---|
462 | } |
---|
463 | |
---|
464 | break; |
---|
465 | |
---|
466 | case "[": |
---|
467 | // Attribute selectors. All with an attribute matching the following token(s) |
---|
468 | $attr_delimiters = array("=", "]", "~", "|"); |
---|
469 | $tok_len = mb_strlen($tok); |
---|
470 | $j = 0; |
---|
471 | |
---|
472 | $attr = ""; |
---|
473 | $op = ""; |
---|
474 | $value = ""; |
---|
475 | |
---|
476 | while ( $j < $tok_len ) { |
---|
477 | if ( in_array($tok{$j}, $attr_delimiters) ) |
---|
478 | break; |
---|
479 | $attr .= $tok{$j++}; |
---|
480 | } |
---|
481 | |
---|
482 | switch ( $tok{$j} ) { |
---|
483 | |
---|
484 | case "~": |
---|
485 | case "|": |
---|
486 | $op .= $tok{$j++}; |
---|
487 | |
---|
488 | if ( $tok{$j} != "=" ) |
---|
489 | throw new DOMPDF_Exception("Invalid CSS selector syntax: invalid attribute selector: $selector"); |
---|
490 | |
---|
491 | $op .= $tok{$j}; |
---|
492 | break; |
---|
493 | |
---|
494 | case "=": |
---|
495 | $op = "="; |
---|
496 | break; |
---|
497 | |
---|
498 | } |
---|
499 | |
---|
500 | // Read the attribute value, if required |
---|
501 | if ( $op != "" ) { |
---|
502 | $j++; |
---|
503 | while ( $j < $tok_len ) { |
---|
504 | if ( $tok{$j} == "]" ) |
---|
505 | break; |
---|
506 | $value .= $tok{$j++}; |
---|
507 | } |
---|
508 | } |
---|
509 | |
---|
510 | if ( $attr == "" ) |
---|
511 | throw new DOMPDF_Exception("Invalid CSS selector syntax: missing attribute name"); |
---|
512 | |
---|
513 | switch ( $op ) { |
---|
514 | |
---|
515 | case "": |
---|
516 | $query .= "[@$attr]"; |
---|
517 | break; |
---|
518 | |
---|
519 | case "=": |
---|
520 | $query .= "[@$attr$op\"$value\"]"; |
---|
521 | break; |
---|
522 | |
---|
523 | case "~=": |
---|
524 | // FIXME: this will break if $value contains quoted strings |
---|
525 | // (e.g. [type~="a b c" "d e f"]) |
---|
526 | $values = explode(" ", $value); |
---|
527 | $query .= "["; |
---|
528 | |
---|
529 | foreach ( $values as $val ) |
---|
530 | $query .= "@$attr=\"$val\" or "; |
---|
531 | |
---|
532 | $query = rtrim($query, " or ") . "]"; |
---|
533 | break; |
---|
534 | |
---|
535 | case "|=": |
---|
536 | $values = explode("-", $value); |
---|
537 | $query .= "["; |
---|
538 | |
---|
539 | foreach ($values as $val) |
---|
540 | $query .= "starts-with(@$attr, \"$val\") or "; |
---|
541 | |
---|
542 | $query = rtrim($query, " or ") . "]"; |
---|
543 | break; |
---|
544 | |
---|
545 | } |
---|
546 | |
---|
547 | break; |
---|
548 | } |
---|
549 | } |
---|
550 | $i++; |
---|
551 | |
---|
552 | // case ":": |
---|
553 | // // Pseudo selectors: ignore for now. Partially handled directly |
---|
554 | // // below. |
---|
555 | |
---|
556 | // // Skip until the next special character, leaving the token as-is |
---|
557 | // while ( $i < $len ) { |
---|
558 | // if ( in_array($selector{$i}, $delimiters) ) |
---|
559 | // break; |
---|
560 | // $i++; |
---|
561 | // } |
---|
562 | // break; |
---|
563 | |
---|
564 | // default: |
---|
565 | // // Add the character to the token |
---|
566 | // $tok .= $selector{$i++}; |
---|
567 | // break; |
---|
568 | // } |
---|
569 | |
---|
570 | // } |
---|
571 | |
---|
572 | |
---|
573 | // Trim the trailing '/' from the query |
---|
574 | if ( mb_strlen($query) > 2 ) |
---|
575 | $query = rtrim($query, "/"); |
---|
576 | |
---|
577 | return $query; |
---|
578 | } |
---|
579 | |
---|
580 | /** |
---|
581 | * applies all current styles to a particular document tree |
---|
582 | * |
---|
583 | * apply_styles() applies all currently loaded styles to the provided |
---|
584 | * {@link Frame_Tree}. Aside from parsing CSS, this is the main purpose |
---|
585 | * of this class. |
---|
586 | * |
---|
587 | * @param Frame_Tree $tree |
---|
588 | */ |
---|
589 | function apply_styles(Frame_Tree $tree) { |
---|
590 | |
---|
591 | // Use XPath to select nodes. This would be easier if we could attach |
---|
592 | // Frame objects directly to DOMNodes using the setUserData() method, but |
---|
593 | // we can't do that just yet. Instead, we set a _node attribute_ in |
---|
594 | // Frame->set_id() and use that as a handle on the Frame object via |
---|
595 | // Frame_Tree::$_registry. |
---|
596 | |
---|
597 | // We create a scratch array of styles indexed by frame id. Once all |
---|
598 | // styles have been assigned, we order the cached styles by specificity |
---|
599 | // and create a final style object to assign to the frame. |
---|
600 | |
---|
601 | // FIXME: this is not particularly robust... |
---|
602 | |
---|
603 | $styles = array(); |
---|
604 | $xp = new DOMXPath($tree->get_dom()); |
---|
605 | |
---|
606 | // Apply all styles in stylesheet |
---|
607 | foreach ($this->_styles as $selector => $style) { |
---|
608 | |
---|
609 | $query = $this->_css_selector_to_xpath($selector); |
---|
610 | // pre_var_dump($selector); |
---|
611 | // pre_var_dump($query); |
---|
612 | // echo ($style); |
---|
613 | |
---|
614 | // Retrieve the nodes |
---|
615 | $nodes = $xp->query($query); |
---|
616 | |
---|
617 | foreach ($nodes as $node) { |
---|
618 | //echo $node->nodeName . "\n"; |
---|
619 | // Retrieve the node id |
---|
620 | if ( $node->nodeType != 1 ) // Only DOMElements get styles |
---|
621 | continue; |
---|
622 | |
---|
623 | $id = $node->getAttribute("frame_id"); |
---|
624 | |
---|
625 | // Assign the current style to the scratch array |
---|
626 | $spec = $this->_specificity($selector); |
---|
627 | $styles[$id][$spec][] = $style; |
---|
628 | } |
---|
629 | } |
---|
630 | |
---|
631 | // Now create the styles and assign them to the appropriate frames. (We |
---|
632 | // iterate over the tree using an implicit Frame_Tree iterator.) |
---|
633 | $root_flg = false; |
---|
634 | foreach ($tree->get_frames() as $frame) { |
---|
635 | // pre_r($frame->get_node()->nodeName . ":"); |
---|
636 | if ( !$root_flg && $this->_page_style ) { |
---|
637 | $style = $this->_page_style; |
---|
638 | $root_flg = true; |
---|
639 | } else |
---|
640 | $style = $this->create_style(); |
---|
641 | |
---|
642 | // Find nearest DOMElement parent |
---|
643 | $p = $frame; |
---|
644 | while ( $p = $p->get_parent() ) |
---|
645 | if ($p->get_node()->nodeType == 1 ) |
---|
646 | break; |
---|
647 | |
---|
648 | // Styles can only be applied directly to DOMElements; anonymous |
---|
649 | // frames inherit from their parent |
---|
650 | if ( $frame->get_node()->nodeType != 1 ) { |
---|
651 | if ( $p ) |
---|
652 | $style->inherit($p->get_style()); |
---|
653 | $frame->set_style($style); |
---|
654 | continue; |
---|
655 | } |
---|
656 | |
---|
657 | $id = $frame->get_id(); |
---|
658 | |
---|
659 | // Handle HTML 4.0 attributes |
---|
660 | Attribute_Translator::translate_attributes($frame); |
---|
661 | |
---|
662 | // Locate any additional style attributes |
---|
663 | if ( ($str = $frame->get_node()->getAttribute("style")) !== "" ) { |
---|
664 | $spec = $this->_specificity("!style attribute"); |
---|
665 | $styles[$id][$spec][] = $this->_parse_properties($str); |
---|
666 | } |
---|
667 | |
---|
668 | // Grab the applicable styles |
---|
669 | if ( isset($styles[$id]) ) { |
---|
670 | |
---|
671 | $applied_styles = $styles[ $frame->get_id() ]; |
---|
672 | |
---|
673 | // Sort by specificity |
---|
674 | ksort($applied_styles); |
---|
675 | |
---|
676 | if (DEBUGCSS) { |
---|
677 | $debug_nodename = $frame->get_node()->nodeName; |
---|
678 | print "<pre>\n[$debug_nodename\n"; |
---|
679 | foreach ($applied_styles as $spec => $arr) { |
---|
680 | printf("specificity: 0x%08x\n",$spec); |
---|
681 | foreach ($arr as $s) { |
---|
682 | print "[\n"; |
---|
683 | $s->debug_print(); |
---|
684 | print "]\n"; |
---|
685 | } |
---|
686 | } |
---|
687 | } |
---|
688 | |
---|
689 | // Merge the new styles with the inherited styles |
---|
690 | foreach ($applied_styles as $arr) { |
---|
691 | foreach ($arr as $s) |
---|
692 | $style->merge($s); |
---|
693 | } |
---|
694 | } |
---|
695 | |
---|
696 | // Inherit parent's styles if required |
---|
697 | if ( $p ) { |
---|
698 | |
---|
699 | if (DEBUGCSS) { |
---|
700 | print "inherit:\n"; |
---|
701 | print "[\n"; |
---|
702 | $p->get_style()->debug_print(); |
---|
703 | print "]\n"; |
---|
704 | } |
---|
705 | |
---|
706 | $style->inherit( $p->get_style() ); |
---|
707 | } |
---|
708 | |
---|
709 | if (DEBUGCSS) { |
---|
710 | print "DomElementStyle:\n"; |
---|
711 | print "[\n"; |
---|
712 | $style->debug_print(); |
---|
713 | print "]\n"; |
---|
714 | print "/$debug_nodename]\n</pre>"; |
---|
715 | } |
---|
716 | |
---|
717 | /*DEBUGCSS print: see below different print debugging method |
---|
718 | pre_r($frame->get_node()->nodeName . ":"); |
---|
719 | echo "<pre>"; |
---|
720 | echo $style; |
---|
721 | echo "</pre>";*/ |
---|
722 | $frame->set_style($style); |
---|
723 | |
---|
724 | } |
---|
725 | |
---|
726 | // We're done! Clean out the registry of all styles since we |
---|
727 | // won't be needing this later. |
---|
728 | foreach ( array_keys($this->_styles) as $key ) { |
---|
729 | unset($this->_styles[$key]); |
---|
730 | } |
---|
731 | |
---|
732 | } |
---|
733 | |
---|
734 | |
---|
735 | /** |
---|
736 | * parse a CSS string using a regex parser |
---|
737 | * |
---|
738 | * Called by {@link Stylesheet::parse_css()} |
---|
739 | * |
---|
740 | * @param string $str |
---|
741 | */ |
---|
742 | private function _parse_css($str) { |
---|
743 | |
---|
744 | // Destroy comments |
---|
745 | $css = preg_replace("'/\*.*?\*/'si", "", $str); |
---|
746 | |
---|
747 | // FIXME: handle '{' within strings, e.g. [attr="string {}"] |
---|
748 | |
---|
749 | // Something more legible: |
---|
750 | $re = |
---|
751 | "/\s* # Skip leading whitespace \n". |
---|
752 | "( @([^\s]+)\s+([^{;]*) (?:;|({)) )? # Match @rules followed by ';' or '{' \n". |
---|
753 | "(?(1) # Only parse sub-sections if we're in an @rule... \n". |
---|
754 | " (?(4) # ...and if there was a leading '{' \n". |
---|
755 | " \s*( (?:(?>[^{}]+) ({)? # Parse rulesets and individual @page rules \n". |
---|
756 | " (?(6) (?>[^}]*) }) \s*)+? \n". |
---|
757 | " ) \n". |
---|
758 | " }) # Balancing '}' \n". |
---|
759 | "| # Branch to match regular rules (not preceeded by '@')\n". |
---|
760 | "([^{]*{[^}]*})) # Parse normal rulesets\n". |
---|
761 | "/xs"; |
---|
762 | |
---|
763 | if ( preg_match_all($re, $css, $matches, PREG_SET_ORDER) === false ) |
---|
764 | // An error occured |
---|
765 | throw new DOMPDF_Exception("Error parsing css file: preg_match_all() failed."); |
---|
766 | |
---|
767 | // After matching, the array indicies are set as follows: |
---|
768 | // |
---|
769 | // [0] => complete text of match |
---|
770 | // [1] => contains '@import ...;' or '@media {' if applicable |
---|
771 | // [2] => text following @ for cases where [1] is set |
---|
772 | // [3] => media types or full text following '@import ...;' |
---|
773 | // [4] => '{', if present |
---|
774 | // [5] => rulesets within media rules |
---|
775 | // [6] => '{', within media rules |
---|
776 | // [7] => individual rules, outside of media rules |
---|
777 | // |
---|
778 | //pre_r($matches); |
---|
779 | foreach ( $matches as $match ) { |
---|
780 | $match[2] = trim($match[2]); |
---|
781 | |
---|
782 | if ( $match[2] !== "" ) { |
---|
783 | // Handle @rules |
---|
784 | switch ($match[2]) { |
---|
785 | |
---|
786 | case "import": |
---|
787 | $this->_parse_import($match[3]); |
---|
788 | break; |
---|
789 | |
---|
790 | case "media": |
---|
791 | $acceptedmedia = self::$ACCEPTED_GENERIC_MEDIA_TYPES; |
---|
792 | if ( defined("DOMPDF_DEFAULT_MEDIA_TYPE") ) { |
---|
793 | $acceptedmedia[] = DOMPDF_DEFAULT_MEDIA_TYPE; |
---|
794 | } else { |
---|
795 | $acceptedmedia[] = self::$ACCEPTED_DEFAULT_MEDIA_TYPE; |
---|
796 | } |
---|
797 | if ( in_array(mb_strtolower(trim($match[3])), $acceptedmedia ) ) { |
---|
798 | $this->_parse_sections($match[5]); |
---|
799 | } |
---|
800 | break; |
---|
801 | |
---|
802 | case "page": |
---|
803 | //This handles @page to be applied to page oriented media |
---|
804 | //Note: This has a reduced syntax: |
---|
805 | //@page { margin:1cm; color:blue; } |
---|
806 | //Not a sequence of styles like a full.css, but only the properties |
---|
807 | //of a single style, which is applied to the very first "root" frame before |
---|
808 | //processing other styles of the frame. |
---|
809 | //Working properties: |
---|
810 | // margin (for margin around edge of paper) |
---|
811 | // font-family (default font of pages) |
---|
812 | // color (default text color of pages) |
---|
813 | //Non working properties: |
---|
814 | // border |
---|
815 | // padding |
---|
816 | // background-color |
---|
817 | //Todo:Reason is unknown |
---|
818 | //Other properties (like further font or border attributes) not tested. |
---|
819 | //If a border or background color around each paper sheet is desired, |
---|
820 | //assign it to the <body> tag, possibly only for the css of the correct media type. |
---|
821 | |
---|
822 | // Store the style for later... |
---|
823 | if ( is_null($this->_page_style) ) |
---|
824 | $this->_page_style = $this->_parse_properties($match[5]); |
---|
825 | else |
---|
826 | $this->_page_style->merge($this->_parse_properties($match[5])); |
---|
827 | break; |
---|
828 | |
---|
829 | default: |
---|
830 | // ignore everything else |
---|
831 | break; |
---|
832 | } |
---|
833 | |
---|
834 | continue; |
---|
835 | } |
---|
836 | |
---|
837 | if ( $match[7] !== "" ) |
---|
838 | $this->_parse_sections($match[7]); |
---|
839 | |
---|
840 | } |
---|
841 | } |
---|
842 | |
---|
843 | /* See also style.cls Style::_image(), refactoring?, works also for imported css files */ |
---|
844 | protected function _image($val) { |
---|
845 | $DEBUGCSS=DEBUGCSS; |
---|
846 | |
---|
847 | if ( mb_strpos($val, "url") === false ) { |
---|
848 | $path = "none"; //Don't resolve no image -> otherwise would prefix path and no longer recognize as none |
---|
849 | } |
---|
850 | else { |
---|
851 | $val = preg_replace("/url\(['\"]?([^'\")]+)['\"]?\)/","\\1", trim($val)); |
---|
852 | |
---|
853 | // Resolve the url now in the context of the current stylesheet |
---|
854 | $parsed_url = explode_url($val); |
---|
855 | if ( $parsed_url["protocol"] == "" && $this->get_protocol() == "" ) { |
---|
856 | if ($parsed_url["path"]{0} == '/' || $parsed_url["path"]{0} == '\\' ) { |
---|
857 | $path = $_SERVER["DOCUMENT_ROOT"].'/'; |
---|
858 | } else { |
---|
859 | $path = $this->get_base_path(); |
---|
860 | } |
---|
861 | $path .= $parsed_url["path"] . $parsed_url["file"]; |
---|
862 | $path = dompdf_realpath($path); |
---|
863 | } else { |
---|
864 | $path = build_url($this->get_protocol(), |
---|
865 | $this->get_host(), |
---|
866 | $this->get_base_path(), |
---|
867 | $val); |
---|
868 | } |
---|
869 | } |
---|
870 | if ($DEBUGCSS) { |
---|
871 | print "<pre>[_image\n"; |
---|
872 | print_r($parsed_url); |
---|
873 | print $this->get_protocol()."\n".$this->get_base_path()."\n".$path."\n"; |
---|
874 | print "_image]</pre>";; |
---|
875 | } |
---|
876 | return $path; |
---|
877 | } |
---|
878 | |
---|
879 | /** |
---|
880 | * parse @import{} sections |
---|
881 | * |
---|
882 | * @param string $url the url of the imported CSS file |
---|
883 | */ |
---|
884 | private function _parse_import($url) { |
---|
885 | $arr = preg_split('/[\s\n,]/', $url,-1, PREG_SPLIT_NO_EMPTY); |
---|
886 | $url = array_shift($arr); |
---|
887 | $accept = false; |
---|
888 | |
---|
889 | if ( count($arr) > 0 ) { |
---|
890 | |
---|
891 | $acceptedmedia = self::$ACCEPTED_GENERIC_MEDIA_TYPES; |
---|
892 | if ( defined("DOMPDF_DEFAULT_MEDIA_TYPE") ) { |
---|
893 | $acceptedmedia[] = DOMPDF_DEFAULT_MEDIA_TYPE; |
---|
894 | } else { |
---|
895 | $acceptedmedia[] = self::$ACCEPTED_DEFAULT_MEDIA_TYPE; |
---|
896 | } |
---|
897 | |
---|
898 | // @import url media_type [media_type...] |
---|
899 | foreach ( $arr as $type ) { |
---|
900 | if ( in_array(mb_strtolower(trim($type)), $acceptedmedia) ) { |
---|
901 | $accept = true; |
---|
902 | break; |
---|
903 | } |
---|
904 | } |
---|
905 | |
---|
906 | } else { |
---|
907 | // unconditional import |
---|
908 | $accept = true; |
---|
909 | } |
---|
910 | |
---|
911 | if ( $accept ) { |
---|
912 | // Store our current base url properties in case the new url is elsewhere |
---|
913 | $protocol = $this->_protocol; |
---|
914 | $host = $this->_base_host; |
---|
915 | $path = $this->_base_path; |
---|
916 | |
---|
917 | // $url = str_replace(array('"',"url", "(", ")"), "", $url); |
---|
918 | // If the protocol is php, assume that we will import using file:// |
---|
919 | // $url = build_url($protocol == "php://" ? "file://" : $protocol, $host, $path, $url); |
---|
920 | // Above does not work for subfolders and absolute urls. |
---|
921 | // Todo: As above, do we need to replace php or file to an empty protocol for local files? |
---|
922 | |
---|
923 | $url = $this->_image($url); |
---|
924 | |
---|
925 | $this->load_css_file($url); |
---|
926 | |
---|
927 | // Restore the current base url |
---|
928 | $this->_protocol = $protocol; |
---|
929 | $this->_base_host = $host; |
---|
930 | $this->_base_path = $path; |
---|
931 | } |
---|
932 | |
---|
933 | } |
---|
934 | |
---|
935 | /** |
---|
936 | * parse regular CSS blocks |
---|
937 | * |
---|
938 | * _parse_properties() creates a new Style object based on the provided |
---|
939 | * CSS rules. |
---|
940 | * |
---|
941 | * @param string $str CSS rules |
---|
942 | * @return Style |
---|
943 | */ |
---|
944 | private function _parse_properties($str) { |
---|
945 | $properties = explode(";", $str); |
---|
946 | |
---|
947 | if (DEBUGCSS) print '[_parse_properties'; |
---|
948 | |
---|
949 | // Create the style |
---|
950 | $style = new Style($this); |
---|
951 | foreach ($properties as $prop) { |
---|
952 | //A css property can have " ! important" appended (whitespace optional) |
---|
953 | //strip this off to decode core of the property correctly. |
---|
954 | //Pass on in the style to allow proper handling: |
---|
955 | //!important properties can only be overridden by other !important ones. |
---|
956 | //$style->$prop_name = is a shortcut of $style->__set($prop_name,$value);. |
---|
957 | //If no specific set function available, set _props["prop_name"] |
---|
958 | //style is always copied completely, or $_props handled separately |
---|
959 | //Therefore set a _important_props["prop_name"]=true to indicate the modifier |
---|
960 | |
---|
961 | /* Instead of short code, prefer the typical case with fast code |
---|
962 | $important = preg_match("/(.*?)!\s*important/",$prop,$match); |
---|
963 | if ( $important ) { |
---|
964 | $prop = $match[1]; |
---|
965 | } |
---|
966 | $prop = trim($prop); |
---|
967 | */ |
---|
968 | if (DEBUGCSS) print '('; |
---|
969 | $important = false; |
---|
970 | $prop = trim($prop); |
---|
971 | if (substr($prop,-9) == 'important') { |
---|
972 | $prop_tmp = rtrim(substr($prop,0,-9)); |
---|
973 | if (substr($prop_tmp,-1) == '!') { |
---|
974 | $prop = rtrim(substr($prop_tmp,0,-1)); |
---|
975 | $important = true; |
---|
976 | } |
---|
977 | } |
---|
978 | |
---|
979 | if ($prop == "") { |
---|
980 | if (DEBUGCSS) print 'empty)'; |
---|
981 | continue; |
---|
982 | } |
---|
983 | |
---|
984 | $i = mb_strpos($prop, ":"); |
---|
985 | if ( $i === false ) { |
---|
986 | if (DEBUGCSS) print 'novalue'.$prop.')'; |
---|
987 | continue; |
---|
988 | } |
---|
989 | |
---|
990 | $prop_name = rtrim(mb_strtolower(mb_substr($prop, 0, $i))); |
---|
991 | $value = ltrim(mb_substr($prop, $i+1)); |
---|
992 | if (DEBUGCSS) print $prop_name.':='.$value.($important?'!IMPORTANT':'').')'; |
---|
993 | //New style, anyway empty |
---|
994 | //if ($important || !$style->important_get($prop_name) ) { |
---|
995 | //$style->$prop_name = array($value,$important); |
---|
996 | //assignment might be replaced by overloading through __set, |
---|
997 | //and overloaded functions might check _important_props, |
---|
998 | //therefore set _important_props first. |
---|
999 | if ($important) { |
---|
1000 | $style->important_set($prop_name); |
---|
1001 | } |
---|
1002 | //For easier debugging, don't use overloading of assignments with __set |
---|
1003 | $style->$prop_name = $value; |
---|
1004 | //$style->props_set($prop_name, $value); |
---|
1005 | } |
---|
1006 | if (DEBUGCSS) print '_parse_properties]'; |
---|
1007 | |
---|
1008 | return $style; |
---|
1009 | } |
---|
1010 | |
---|
1011 | /** |
---|
1012 | * parse selector + rulesets |
---|
1013 | * |
---|
1014 | * @param string $str CSS selectors and rulesets |
---|
1015 | */ |
---|
1016 | private function _parse_sections($str) { |
---|
1017 | // Pre-process: collapse all whitespace and strip whitespace around '>', |
---|
1018 | // '.', ':', '+', '#' |
---|
1019 | |
---|
1020 | $patterns = array("/[\\s\n]+/", "/\\s+([>.:+#])\\s+/"); |
---|
1021 | $replacements = array(" ", "\\1"); |
---|
1022 | $str = preg_replace($patterns, $replacements, $str); |
---|
1023 | |
---|
1024 | $sections = explode("}", $str); |
---|
1025 | if (DEBUGCSS) print '[_parse_sections'; |
---|
1026 | foreach ($sections as $sect) { |
---|
1027 | $i = mb_strpos($sect, "{"); |
---|
1028 | |
---|
1029 | $selectors = explode(",", mb_substr($sect, 0, $i)); |
---|
1030 | if (DEBUGCSS) print '[section'; |
---|
1031 | $style = $this->_parse_properties(trim(mb_substr($sect, $i+1))); |
---|
1032 | |
---|
1033 | // Assign it to the selected elements |
---|
1034 | foreach ($selectors as $selector) { |
---|
1035 | $selector = trim($selector); |
---|
1036 | |
---|
1037 | if ($selector == "") { |
---|
1038 | if (DEBUGCSS) print '#empty#'; |
---|
1039 | continue; |
---|
1040 | } |
---|
1041 | if (DEBUGCSS) print '#'.$selector.'#'; |
---|
1042 | //if (DEBUGCSS) { if (strpos($selector,'p') !== false) print '!!!p!!!#'; } |
---|
1043 | |
---|
1044 | $this->add_style($selector, $style); |
---|
1045 | } |
---|
1046 | if (DEBUGCSS) print 'section]'; |
---|
1047 | } |
---|
1048 | if (DEBUGCSS) print '_parse_sections]'; |
---|
1049 | } |
---|
1050 | |
---|
1051 | /** |
---|
1052 | * dumps the entire stylesheet as a string |
---|
1053 | * |
---|
1054 | * Generates a string of each selector and associated style in the |
---|
1055 | * Stylesheet. Useful for debugging. |
---|
1056 | * |
---|
1057 | * @return string |
---|
1058 | */ |
---|
1059 | function __toString() { |
---|
1060 | $str = ""; |
---|
1061 | foreach ($this->_styles as $selector => $style) |
---|
1062 | $str .= "$selector => " . $style->__toString() . "\n"; |
---|
1063 | |
---|
1064 | return $str; |
---|
1065 | } |
---|
1066 | } |
---|
1067 | ?> |
---|