[1575] | 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 | * @package dompdf |
---|
| 37 | * @version 0.5.1 |
---|
| 38 | */ |
---|
| 39 | |
---|
| 40 | /* $Id: stylesheet.cls.php,v 1.16 2006/07/07 21:31:04 benjcarson Exp $ */ |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * The location of the default built-in CSS file. |
---|
| 44 | * {@link Stylesheet::DEFAULT_STYLESHEET} |
---|
| 45 | */ |
---|
| 46 | define('__DEFAULT_STYLESHEET', DOMPDF_LIB_DIR . DIRECTORY_SEPARATOR . "res" . DIRECTORY_SEPARATOR . "html.css"); |
---|
| 47 | |
---|
| 48 | /** |
---|
| 49 | * The master stylesheet class |
---|
| 50 | * |
---|
| 51 | * The Stylesheet class is responsible for parsing stylesheets and style |
---|
| 52 | * tags/attributes. It also acts as a registry of the individual Style |
---|
| 53 | * objects generated by the current set of loaded CSS files and style |
---|
| 54 | * elements. |
---|
| 55 | * |
---|
| 56 | * @see Style |
---|
| 57 | * @package dompdf |
---|
| 58 | */ |
---|
| 59 | class Stylesheet { |
---|
| 60 | |
---|
| 61 | |
---|
| 62 | |
---|
| 63 | /** |
---|
| 64 | * the location of the default built-in CSS file. |
---|
| 65 | * |
---|
| 66 | */ |
---|
| 67 | const DEFAULT_STYLESHEET = __DEFAULT_STYLESHEET; // Hack: can't |
---|
| 68 | // concatenate stuff in |
---|
| 69 | // const declarations, |
---|
| 70 | // but I can do this? |
---|
| 71 | // protected members |
---|
| 72 | |
---|
| 73 | /** |
---|
| 74 | * array of currently defined styles |
---|
| 75 | * @var array |
---|
| 76 | */ |
---|
| 77 | private $_styles; |
---|
| 78 | |
---|
| 79 | /** |
---|
| 80 | * base protocol of the document being parsed |
---|
| 81 | * |
---|
| 82 | * Used to handle relative urls. |
---|
| 83 | * |
---|
| 84 | * @var string |
---|
| 85 | */ |
---|
| 86 | private $_protocol; |
---|
| 87 | |
---|
| 88 | /** |
---|
| 89 | * base hostname of the document being parsed |
---|
| 90 | * |
---|
| 91 | * Used to handle relative urls. |
---|
| 92 | * @var string |
---|
| 93 | */ |
---|
| 94 | private $_base_host; |
---|
| 95 | |
---|
| 96 | /** |
---|
| 97 | * base path of the document being parsed |
---|
| 98 | * |
---|
| 99 | * Used to handle relative urls. |
---|
| 100 | * @var string |
---|
| 101 | */ |
---|
| 102 | private $_base_path; |
---|
| 103 | |
---|
| 104 | |
---|
| 105 | /** |
---|
| 106 | * the style defined by @page rules |
---|
| 107 | * |
---|
| 108 | * @var Style |
---|
| 109 | */ |
---|
| 110 | private $_page_style; |
---|
| 111 | |
---|
| 112 | |
---|
| 113 | /** |
---|
| 114 | * list of loaded files, used to prevent recursion |
---|
| 115 | * |
---|
| 116 | * @var array |
---|
| 117 | */ |
---|
| 118 | private $_loaded_files; |
---|
| 119 | |
---|
| 120 | /** |
---|
| 121 | * accepted CSS media types |
---|
| 122 | */ |
---|
| 123 | static $ACCEPTED_MEDIA_TYPES = array("all", "static", "visual", |
---|
| 124 | "bitmap", "paged", "print"); |
---|
| 125 | |
---|
| 126 | /** |
---|
| 127 | * The class constructor. |
---|
| 128 | * |
---|
| 129 | * The base protocol, host & path are initialized to those of |
---|
| 130 | * the current script. |
---|
| 131 | */ |
---|
| 132 | function __construct() { |
---|
| 133 | $this->_styles = array(); |
---|
| 134 | $this->_loaded_files = array(); |
---|
| 135 | list($this->_protocol, $this->_base_host, $this->_base_path) = explode_url($_SERVER["SCRIPT_FILENAME"]); |
---|
| 136 | $this->_page_style = null; |
---|
| 137 | } |
---|
| 138 | |
---|
| 139 | /** |
---|
| 140 | * Set the base protocol |
---|
| 141 | * |
---|
| 142 | * @param string $proto |
---|
| 143 | */ |
---|
| 144 | function set_protocol($proto) { $this->_protocol = $proto; } |
---|
| 145 | |
---|
| 146 | /** |
---|
| 147 | * Set the base host |
---|
| 148 | * |
---|
| 149 | * @param string $host |
---|
| 150 | */ |
---|
| 151 | function set_host($host) { $this->_base_host = $host; } |
---|
| 152 | |
---|
| 153 | /** |
---|
| 154 | * Set the base path |
---|
| 155 | * |
---|
| 156 | * @param string $path |
---|
| 157 | */ |
---|
| 158 | function set_base_path($path) { $this->_base_path = $path; } |
---|
| 159 | |
---|
| 160 | |
---|
| 161 | /** |
---|
| 162 | * Return the base protocol for this stylesheet |
---|
| 163 | * |
---|
| 164 | * @return string |
---|
| 165 | */ |
---|
| 166 | function get_protocol() { return $this->_protocol; } |
---|
| 167 | |
---|
| 168 | /** |
---|
| 169 | * Return the base host for this stylesheet |
---|
| 170 | * |
---|
| 171 | * @return string |
---|
| 172 | */ |
---|
| 173 | function get_host() { return $this->_base_host; } |
---|
| 174 | |
---|
| 175 | /** |
---|
| 176 | * Return the base path for this stylesheet |
---|
| 177 | * |
---|
| 178 | * @return string |
---|
| 179 | */ |
---|
| 180 | function get_base_path() { return $this->_base_path; } |
---|
| 181 | |
---|
| 182 | /** |
---|
| 183 | * add a new Style object to the stylesheet |
---|
| 184 | * |
---|
| 185 | * add_style() adds a new Style object to the current stylesheet, or |
---|
| 186 | * merges a new Style with an existing one. |
---|
| 187 | * |
---|
| 188 | * @param string $key the Style's selector |
---|
| 189 | * @param Style $style the Style to be added |
---|
| 190 | */ |
---|
| 191 | function add_style($key, Style $style) { |
---|
| 192 | if (!is_string($key)) |
---|
| 193 | throw new DOMPDF_Exception("CSS rule must be keyed by a string."); |
---|
| 194 | |
---|
| 195 | if ( isset($this->_styles[$key]) ) |
---|
| 196 | $this->_styles[$key]->merge($style); |
---|
| 197 | else |
---|
| 198 | $this->_styles[$key] = clone $style; |
---|
| 199 | } |
---|
| 200 | |
---|
| 201 | |
---|
| 202 | /** |
---|
| 203 | * lookup a specifc Style object |
---|
| 204 | * |
---|
| 205 | * lookup() returns the Style specified by $key, or null if the Style is |
---|
| 206 | * not found. |
---|
| 207 | * |
---|
| 208 | * @param string $key the selector of the requested Style |
---|
| 209 | * @return Style |
---|
| 210 | */ |
---|
| 211 | function lookup($key) { |
---|
| 212 | if ( !isset($this->_styles[$key]) ) |
---|
| 213 | return null; |
---|
| 214 | |
---|
| 215 | return $this->_styles[$key]; |
---|
| 216 | } |
---|
| 217 | |
---|
| 218 | /** |
---|
| 219 | * create a new Style object associated with this stylesheet |
---|
| 220 | * |
---|
| 221 | * @param Style $parent The style of this style's parent in the DOM tree |
---|
| 222 | * @return Style |
---|
| 223 | */ |
---|
| 224 | function create_style($parent = null) { |
---|
| 225 | return new Style($this, $parent); |
---|
| 226 | } |
---|
| 227 | |
---|
| 228 | |
---|
| 229 | /** |
---|
| 230 | * load and parse a CSS string |
---|
| 231 | * |
---|
| 232 | * @param string $css |
---|
| 233 | */ |
---|
| 234 | function load_css(&$css) { $this->_parse_css($css); } |
---|
| 235 | |
---|
| 236 | |
---|
| 237 | /** |
---|
| 238 | * load and parse a CSS file |
---|
| 239 | * |
---|
| 240 | * @param string $file |
---|
| 241 | */ |
---|
| 242 | function load_css_file($file) { |
---|
| 243 | global $_dompdf_warnings; |
---|
| 244 | |
---|
| 245 | // Prevent circular references |
---|
| 246 | if ( isset($this->_loaded_files[$file]) ) |
---|
| 247 | return; |
---|
| 248 | |
---|
| 249 | $this->_loaded_files[$file] = true; |
---|
| 250 | $parsed_url = explode_url($file); |
---|
| 251 | |
---|
| 252 | list($this->_protocol, $this->_base_host, $this->_base_path, $filename) = $parsed_url; |
---|
| 253 | |
---|
| 254 | if ( !DOMPDF_ENABLE_REMOTE && |
---|
| 255 | ($this->_protocol != "" && $this->_protocol != "file://") ) { |
---|
| 256 | record_warnings(E_USER_WARNING, "Remote CSS file '$file' requested, but DOMPDF_ENABLE_REMOTE is false.", __FILE__, __LINE__); |
---|
| 257 | return; |
---|
| 258 | } |
---|
| 259 | |
---|
| 260 | // Fix submitted by Nick Oostveen for aliased directory support: |
---|
| 261 | if ( $this->_protocol == "" ) |
---|
| 262 | $file = $this->_base_path . $filename; |
---|
| 263 | else |
---|
| 264 | $file = build_url($this->_protocol, $this->_base_host, $this->_base_path, $filename); |
---|
| 265 | |
---|
| 266 | set_error_handler("record_warnings"); |
---|
| 267 | $css = file_get_contents($file); |
---|
| 268 | restore_error_handler(); |
---|
| 269 | |
---|
| 270 | if ( $css == "" ) { |
---|
| 271 | record_warnings(E_USER_WARNING, "Unable to load css file $file", __FILE__, __LINE__);; |
---|
| 272 | return; |
---|
| 273 | } |
---|
| 274 | |
---|
| 275 | $this->_parse_css($css); |
---|
| 276 | |
---|
| 277 | } |
---|
| 278 | |
---|
| 279 | /** |
---|
| 280 | * @link http://www.w3.org/TR/CSS21/cascade.html#specificity} |
---|
| 281 | * |
---|
| 282 | * @param string $selector |
---|
| 283 | * @return int |
---|
| 284 | */ |
---|
| 285 | private function _specificity($selector) { |
---|
| 286 | // http://www.w3.org/TR/CSS21/cascade.html#specificity |
---|
| 287 | |
---|
| 288 | $a = ($selector === "!style attribute") ? 1 : 0; |
---|
| 289 | |
---|
| 290 | $b = min(mb_substr_count($selector, "#"), 255); |
---|
| 291 | |
---|
| 292 | $c = min(mb_substr_count($selector, ".") + |
---|
| 293 | mb_substr_count($selector, ">") + |
---|
| 294 | mb_substr_count($selector, "+"), 255); |
---|
| 295 | |
---|
| 296 | $d = min(mb_substr_count($selector, " "), 255); |
---|
| 297 | |
---|
| 298 | return ($a << 24) | ($b << 16) | ($c << 8) | ($d); |
---|
| 299 | } |
---|
| 300 | |
---|
| 301 | |
---|
| 302 | /** |
---|
| 303 | * converts a CSS selector to an XPath query. |
---|
| 304 | * |
---|
| 305 | * @param string $selector |
---|
| 306 | * @return string |
---|
| 307 | */ |
---|
| 308 | private function _css_selector_to_xpath($selector) { |
---|
| 309 | |
---|
| 310 | // Collapse white space and strip whitespace around delimiters |
---|
| 311 | // $search = array("/\\s+/", "/\\s+([.>#+:])\\s+/"); |
---|
| 312 | // $replace = array(" ", "\\1"); |
---|
| 313 | // $selector = preg_replace($search, $replace, trim($selector)); |
---|
| 314 | |
---|
| 315 | // Initial query (non-absolute) |
---|
| 316 | $query = "//"; |
---|
| 317 | |
---|
| 318 | // Parse the selector |
---|
| 319 | //$s = preg_split("/([ :>.#+])/", $selector, -1, PREG_SPLIT_DELIM_CAPTURE); |
---|
| 320 | |
---|
| 321 | $delimiters = array(" ", ">", ".", "#", "+", ":", "["); |
---|
| 322 | |
---|
| 323 | // Add an implicit space at the beginning of the selector if there is no |
---|
| 324 | // delimiter there already. |
---|
| 325 | if ( !in_array($selector{0}, $delimiters) ) |
---|
| 326 | $selector = " $selector"; |
---|
| 327 | |
---|
| 328 | $tok = ""; |
---|
| 329 | $len = mb_strlen($selector); |
---|
| 330 | $i = 0; |
---|
| 331 | |
---|
| 332 | while ( $i < $len ) { |
---|
| 333 | |
---|
| 334 | $s = $selector{$i}; |
---|
| 335 | $i++; |
---|
| 336 | |
---|
| 337 | // Eat characters up to the next delimiter |
---|
| 338 | $tok = ""; |
---|
| 339 | |
---|
| 340 | while ($i < $len) { |
---|
| 341 | if ( in_array($selector{$i}, $delimiters) ) |
---|
| 342 | break; |
---|
| 343 | $tok .= $selector{$i++}; |
---|
| 344 | } |
---|
| 345 | |
---|
| 346 | switch ($s) { |
---|
| 347 | |
---|
| 348 | case " ": |
---|
| 349 | case ">": |
---|
| 350 | // All elements matching the next token that are direct children of |
---|
| 351 | // the current token |
---|
| 352 | $expr = $s == " " ? "descendant" : "child"; |
---|
| 353 | |
---|
| 354 | if ( mb_substr($query, -1, 1) != "/" ) |
---|
| 355 | $query .= "/"; |
---|
| 356 | |
---|
| 357 | if ( !$tok ) |
---|
| 358 | $tok = "*"; |
---|
| 359 | |
---|
| 360 | $query .= "$expr::$tok"; |
---|
| 361 | $tok = ""; |
---|
| 362 | break; |
---|
| 363 | |
---|
| 364 | case ".": |
---|
| 365 | case "#": |
---|
| 366 | // All elements matching the current token with a class/id equal to |
---|
| 367 | // the _next_ token. |
---|
| 368 | |
---|
| 369 | $attr = $s == "." ? "class" : "id"; |
---|
| 370 | |
---|
| 371 | // empty class/id == * |
---|
| 372 | if ( mb_substr($query, -1, 1) == "/" ) |
---|
| 373 | $query .= "*"; |
---|
| 374 | |
---|
| 375 | // Match multiple classes: $tok contains the current selected |
---|
| 376 | // class. Search for class attributes with class="$tok", |
---|
| 377 | // class=".* $tok .*" and class=".* $tok" |
---|
| 378 | |
---|
| 379 | // This doesn't work because libxml only supports XPath 1.0... |
---|
| 380 | //$query .= "[matches(@$attr,\"^${tok}\$|^${tok}[ ]+|[ ]+${tok}\$|[ ]+${tok}[ ]+\")]"; |
---|
| 381 | |
---|
| 382 | // Query improvement by Michael Sheakoski <michael@mjsdigital.com>: |
---|
| 383 | $query .= "[contains(concat(' ', @$attr, ' '), concat(' ', '$tok', ' '))]"; |
---|
| 384 | $tok = ""; |
---|
| 385 | break; |
---|
| 386 | |
---|
| 387 | case "+": |
---|
| 388 | // All sibling elements that folow the current token |
---|
| 389 | if ( mb_substr($query, -1, 1) != "/" ) |
---|
| 390 | $query .= "/"; |
---|
| 391 | |
---|
| 392 | $query .= "following-sibling::$tok"; |
---|
| 393 | $tok = ""; |
---|
| 394 | break; |
---|
| 395 | |
---|
| 396 | case ":": |
---|
| 397 | // Pseudo-classes |
---|
| 398 | switch ($tok) { |
---|
| 399 | |
---|
| 400 | case "first-child": |
---|
| 401 | break; |
---|
| 402 | |
---|
| 403 | case "link": |
---|
| 404 | $query .= "[@href]"; |
---|
| 405 | $tok = ""; |
---|
| 406 | break; |
---|
| 407 | |
---|
| 408 | case "first-line": |
---|
| 409 | break; |
---|
| 410 | |
---|
| 411 | case "first-letter": |
---|
| 412 | break; |
---|
| 413 | |
---|
| 414 | case "before": |
---|
| 415 | break; |
---|
| 416 | |
---|
| 417 | case "after": |
---|
| 418 | break; |
---|
| 419 | |
---|
| 420 | } |
---|
| 421 | |
---|
| 422 | break; |
---|
| 423 | |
---|
| 424 | case "[": |
---|
| 425 | // Attribute selectors. All with an attribute matching the following token(s) |
---|
| 426 | $attr_delimiters = array("=", "]", "~", "|"); |
---|
| 427 | $tok_len = mb_strlen($tok); |
---|
| 428 | $j = 0; |
---|
| 429 | |
---|
| 430 | $attr = ""; |
---|
| 431 | $op = ""; |
---|
| 432 | $value = ""; |
---|
| 433 | |
---|
| 434 | while ( $j < $tok_len ) { |
---|
| 435 | if ( in_array($tok{$j}, $attr_delimiters) ) |
---|
| 436 | break; |
---|
| 437 | $attr .= $tok{$j++}; |
---|
| 438 | } |
---|
| 439 | |
---|
| 440 | switch ( $tok{$j} ) { |
---|
| 441 | |
---|
| 442 | case "~": |
---|
| 443 | case "|": |
---|
| 444 | $op .= $tok{$j++}; |
---|
| 445 | |
---|
| 446 | if ( $tok{$j} != "=" ) |
---|
| 447 | throw new DOMPDF_Exception("Invalid CSS selector syntax: invalid attribute selector: $selector"); |
---|
| 448 | |
---|
| 449 | $op .= $tok{$j}; |
---|
| 450 | break; |
---|
| 451 | |
---|
| 452 | case "=": |
---|
| 453 | $op = "="; |
---|
| 454 | break; |
---|
| 455 | |
---|
| 456 | } |
---|
| 457 | |
---|
| 458 | // Read the attribute value, if required |
---|
| 459 | if ( $op != "" ) { |
---|
| 460 | $j++; |
---|
| 461 | while ( $j < $tok_len ) { |
---|
| 462 | if ( $tok{$j} == "]" ) |
---|
| 463 | break; |
---|
| 464 | $value .= $tok{$j++}; |
---|
| 465 | } |
---|
| 466 | } |
---|
| 467 | |
---|
| 468 | if ( $attr == "" ) |
---|
| 469 | throw new DOMPDF_Exception("Invalid CSS selector syntax: missing attribute name"); |
---|
| 470 | |
---|
| 471 | switch ( $op ) { |
---|
| 472 | |
---|
| 473 | case "": |
---|
| 474 | $query .= "[@$attr]"; |
---|
| 475 | break; |
---|
| 476 | |
---|
| 477 | case "=": |
---|
| 478 | $query .= "[@$attr$op\"$value\"]"; |
---|
| 479 | break; |
---|
| 480 | |
---|
| 481 | case "~=": |
---|
| 482 | // FIXME: this will break if $value contains quoted strings |
---|
| 483 | // (e.g. [type~="a b c" "d e f"]) |
---|
| 484 | $values = explode(" ", $value); |
---|
| 485 | $query .= "["; |
---|
| 486 | |
---|
| 487 | foreach ( $values as $val ) |
---|
| 488 | $query .= "@$attr=\"$val\" or "; |
---|
| 489 | |
---|
| 490 | $query = rtrim($query, " or ") . "]"; |
---|
| 491 | break; |
---|
| 492 | |
---|
| 493 | case "|=": |
---|
| 494 | $values = explode("-", $value); |
---|
| 495 | $query .= "["; |
---|
| 496 | |
---|
| 497 | foreach ($values as $val) |
---|
| 498 | $query .= "starts-with(@$attr, \"$val\") or "; |
---|
| 499 | |
---|
| 500 | $query = rtrim($query, " or ") . "]"; |
---|
| 501 | break; |
---|
| 502 | |
---|
| 503 | } |
---|
| 504 | |
---|
| 505 | break; |
---|
| 506 | } |
---|
| 507 | } |
---|
| 508 | $i++; |
---|
| 509 | |
---|
| 510 | // case ":": |
---|
| 511 | // // Pseudo selectors: ignore for now. Partially handled directly |
---|
| 512 | // // below. |
---|
| 513 | |
---|
| 514 | // // Skip until the next special character, leaving the token as-is |
---|
| 515 | // while ( $i < $len ) { |
---|
| 516 | // if ( in_array($selector{$i}, $delimiters) ) |
---|
| 517 | // break; |
---|
| 518 | // $i++; |
---|
| 519 | // } |
---|
| 520 | // break; |
---|
| 521 | |
---|
| 522 | // default: |
---|
| 523 | // // Add the character to the token |
---|
| 524 | // $tok .= $selector{$i++}; |
---|
| 525 | // break; |
---|
| 526 | // } |
---|
| 527 | |
---|
| 528 | // } |
---|
| 529 | |
---|
| 530 | |
---|
| 531 | // Trim the trailing '/' from the query |
---|
| 532 | if ( mb_strlen($query) > 2 ) |
---|
| 533 | $query = rtrim($query, "/"); |
---|
| 534 | |
---|
| 535 | return $query; |
---|
| 536 | } |
---|
| 537 | |
---|
| 538 | /** |
---|
| 539 | * applies all current styles to a particular document tree |
---|
| 540 | * |
---|
| 541 | * apply_styles() applies all currently loaded styles to the provided |
---|
| 542 | * {@link Frame_Tree}. Aside from parsing CSS, this is the main purpose |
---|
| 543 | * of this class. |
---|
| 544 | * |
---|
| 545 | * @param Frame_Tree $tree |
---|
| 546 | */ |
---|
| 547 | function apply_styles(Frame_Tree $tree) { |
---|
| 548 | |
---|
| 549 | // Use XPath to select nodes. This would be easier if we could attach |
---|
| 550 | // Frame objects directly to DOMNodes using the setUserData() method, but |
---|
| 551 | // we can't do that just yet. Instead, we set a _node attribute_ in |
---|
| 552 | // Frame->set_id() and use that as a handle on the Frame object via |
---|
| 553 | // Frame_Tree::$_registry. |
---|
| 554 | |
---|
| 555 | // We create a scratch array of styles indexed by frame id. Once all |
---|
| 556 | // styles have been assigned, we order the cached styles by specificity |
---|
| 557 | // and create a final style object to assign to the frame. |
---|
| 558 | |
---|
| 559 | // FIXME: this is not particularly robust... |
---|
| 560 | |
---|
| 561 | $styles = array(); |
---|
| 562 | $xp = new DOMXPath($tree->get_dom()); |
---|
| 563 | |
---|
| 564 | // Apply all styles in stylesheet |
---|
| 565 | foreach ($this->_styles as $selector => $style) { |
---|
| 566 | |
---|
| 567 | $query = $this->_css_selector_to_xpath($selector); |
---|
| 568 | // pre_var_dump($selector); |
---|
| 569 | // pre_var_dump($query); |
---|
| 570 | // echo ($style); |
---|
| 571 | |
---|
| 572 | // Retrieve the nodes |
---|
| 573 | $nodes = $xp->query($query); |
---|
| 574 | |
---|
| 575 | foreach ($nodes as $node) { |
---|
| 576 | //echo $node->nodeName . "\n"; |
---|
| 577 | // Retrieve the node id |
---|
| 578 | if ( $node->nodeType != 1 ) // Only DOMElements get styles |
---|
| 579 | continue; |
---|
| 580 | |
---|
| 581 | $id = $node->getAttribute("frame_id"); |
---|
| 582 | |
---|
| 583 | // Assign the current style to the scratch array |
---|
| 584 | $spec = $this->_specificity($selector); |
---|
| 585 | $styles[$id][$spec][] = $style; |
---|
| 586 | } |
---|
| 587 | } |
---|
| 588 | |
---|
| 589 | // Now create the styles and assign them to the appropriate frames. (We |
---|
| 590 | // iterate over the tree using an implicit Frame_Tree iterator.) |
---|
| 591 | $root_flg = false; |
---|
| 592 | foreach ($tree->get_frames() as $frame) { |
---|
| 593 | // pre_r($frame->get_node()->nodeName . ":"); |
---|
| 594 | |
---|
| 595 | if ( !$root_flg && $this->_page_style ) { |
---|
| 596 | $style = $this->_page_style; |
---|
| 597 | $root_flg = true; |
---|
| 598 | |
---|
| 599 | } else |
---|
| 600 | $style = $this->create_style(); |
---|
| 601 | |
---|
| 602 | // Find nearest DOMElement parent |
---|
| 603 | $p = $frame; |
---|
| 604 | while ( $p = $p->get_parent() ) |
---|
| 605 | if ($p->get_node()->nodeType == 1 ) |
---|
| 606 | break; |
---|
| 607 | |
---|
| 608 | // Styles can only be applied directly to DOMElements; anonymous |
---|
| 609 | // frames inherit from their parent |
---|
| 610 | if ( $frame->get_node()->nodeType != 1 ) { |
---|
| 611 | if ( $p ) |
---|
| 612 | $style->inherit($p->get_style()); |
---|
| 613 | $frame->set_style($style); |
---|
| 614 | continue; |
---|
| 615 | } |
---|
| 616 | |
---|
| 617 | $id = $frame->get_id(); |
---|
| 618 | |
---|
| 619 | // Handle HTML 4.0 attributes |
---|
| 620 | Attribute_Translator::translate_attributes($frame); |
---|
| 621 | |
---|
| 622 | // Locate any additional style attributes |
---|
| 623 | if ( ($str = $frame->get_node()->getAttribute("style")) !== "" ) { |
---|
| 624 | $spec = $this->_specificity("!style attribute"); |
---|
| 625 | $styles[$id][$spec][] = $this->_parse_properties($str); |
---|
| 626 | } |
---|
| 627 | |
---|
| 628 | // Grab the applicable styles |
---|
| 629 | if ( isset($styles[$id]) ) { |
---|
| 630 | |
---|
| 631 | $applied_styles = $styles[ $frame->get_id() ]; |
---|
| 632 | |
---|
| 633 | // Sort by specificity |
---|
| 634 | ksort($applied_styles); |
---|
| 635 | |
---|
| 636 | // Merge the new styles with the inherited styles |
---|
| 637 | foreach ($applied_styles as $arr) { |
---|
| 638 | foreach ($arr as $s) |
---|
| 639 | $style->merge($s); |
---|
| 640 | } |
---|
| 641 | } |
---|
| 642 | |
---|
| 643 | // Inherit parent's styles if required |
---|
| 644 | if ( $p ) { |
---|
| 645 | $style->inherit( $p->get_style() ); |
---|
| 646 | } |
---|
| 647 | |
---|
| 648 | // pre_r($frame->get_node()->nodeName . ":"); |
---|
| 649 | // echo "<pre>"; |
---|
| 650 | // echo $style; |
---|
| 651 | // echo "</pre>"; |
---|
| 652 | $frame->set_style($style); |
---|
| 653 | |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | // We're done! Clean out the registry of all styles since we |
---|
| 657 | // won't be needing this later. |
---|
| 658 | foreach ( array_keys($this->_styles) as $key ) { |
---|
| 659 | unset($this->_styles[$key]); |
---|
| 660 | } |
---|
| 661 | |
---|
| 662 | } |
---|
| 663 | |
---|
| 664 | |
---|
| 665 | /** |
---|
| 666 | * parse a CSS string using a regex parser |
---|
| 667 | * |
---|
| 668 | * Called by {@link Stylesheet::parse_css()} |
---|
| 669 | * |
---|
| 670 | * @param string $str |
---|
| 671 | */ |
---|
| 672 | private function _parse_css($str) { |
---|
| 673 | |
---|
| 674 | // Destroy comments |
---|
| 675 | $css = preg_replace("'/\*.*?\*/'si", "", $str); |
---|
| 676 | |
---|
| 677 | // FIXME: handle '{' within strings, e.g. [attr="string {}"] |
---|
| 678 | |
---|
| 679 | // Something more legible: |
---|
| 680 | $re = |
---|
| 681 | "/\s* # Skip leading whitespace \n". |
---|
| 682 | "( @([^\s]+)\s+([^{;]*) (?:;|({)) )? # Match @rules followed by ';' or '{' \n". |
---|
| 683 | "(?(1) # Only parse sub-sections if we're in an @rule... \n". |
---|
| 684 | " (?(4) # ...and if there was a leading '{' \n". |
---|
| 685 | " \s*( (?:(?>[^{}]+) ({)? # Parse rulesets and individual @page rules \n". |
---|
| 686 | " (?(6) (?>[^}]*) }) \s*)+? \n". |
---|
| 687 | " ) \n". |
---|
| 688 | " }) # Balancing '}' \n". |
---|
| 689 | "| # Branch to match regular rules (not preceeded by '@')\n". |
---|
| 690 | "([^{]*{[^}]*})) # Parse normal rulesets\n". |
---|
| 691 | "/xs"; |
---|
| 692 | |
---|
| 693 | if ( preg_match_all($re, $css, $matches, PREG_SET_ORDER) === false ) |
---|
| 694 | // An error occured |
---|
| 695 | throw new DOMPDF_Exception("Error parsing css file: preg_match_all() failed."); |
---|
| 696 | |
---|
| 697 | // After matching, the array indicies are set as follows: |
---|
| 698 | // |
---|
| 699 | // [0] => complete text of match |
---|
| 700 | // [1] => contains '@import ...;' or '@media {' if applicable |
---|
| 701 | // [2] => text following @ for cases where [1] is set |
---|
| 702 | // [3] => media types or full text following '@import ...;' |
---|
| 703 | // [4] => '{', if present |
---|
| 704 | // [5] => rulesets within media rules |
---|
| 705 | // [6] => '{', within media rules |
---|
| 706 | // [7] => individual rules, outside of media rules |
---|
| 707 | // |
---|
| 708 | //pre_r($matches); |
---|
| 709 | foreach ( $matches as $match ) { |
---|
| 710 | $match[2] = trim($match[2]); |
---|
| 711 | |
---|
| 712 | if ( $match[2] !== "" ) { |
---|
| 713 | // Handle @rules |
---|
| 714 | switch ($match[2]) { |
---|
| 715 | |
---|
| 716 | case "import": |
---|
| 717 | $this->_parse_import($match[3]); |
---|
| 718 | break; |
---|
| 719 | |
---|
| 720 | case "media": |
---|
| 721 | if ( in_array(mb_strtolower(trim($match[3])), self::$ACCEPTED_MEDIA_TYPES ) ) { |
---|
| 722 | $this->_parse_sections($match[5]); |
---|
| 723 | } |
---|
| 724 | break; |
---|
| 725 | |
---|
| 726 | case "page": |
---|
| 727 | // Store the style for later... |
---|
| 728 | if ( is_null($this->_page_style) ) |
---|
| 729 | $this->_page_style = $this->_parse_properties($match[5]); |
---|
| 730 | else |
---|
| 731 | $this->_page_style->merge($this->_parse_properties($match[5])); |
---|
| 732 | break; |
---|
| 733 | |
---|
| 734 | default: |
---|
| 735 | // ignore everything else |
---|
| 736 | break; |
---|
| 737 | } |
---|
| 738 | |
---|
| 739 | continue; |
---|
| 740 | } |
---|
| 741 | |
---|
| 742 | if ( $match[7] !== "" ) |
---|
| 743 | $this->_parse_sections($match[7]); |
---|
| 744 | |
---|
| 745 | } |
---|
| 746 | } |
---|
| 747 | |
---|
| 748 | |
---|
| 749 | /** |
---|
| 750 | * parse @import{} sections |
---|
| 751 | * |
---|
| 752 | * @param string $url the url of the imported CSS file |
---|
| 753 | */ |
---|
| 754 | private function _parse_import($url) { |
---|
| 755 | $arr = preg_split("/[\s\n]/", $url); |
---|
| 756 | $url = array_pop($arr); |
---|
| 757 | $accept = false; |
---|
| 758 | |
---|
| 759 | if ( count($arr) > 0 ) { |
---|
| 760 | |
---|
| 761 | // @import url media_type [media_type...] |
---|
| 762 | foreach ( $arr as $type ) { |
---|
| 763 | if ( in_array($type, self::$ACCEPTED_MEDIA_TYPES) ) { |
---|
| 764 | $accept = true; |
---|
| 765 | break; |
---|
| 766 | } |
---|
| 767 | } |
---|
| 768 | |
---|
| 769 | } else |
---|
| 770 | // unconditional import |
---|
| 771 | $accept = true; |
---|
| 772 | |
---|
| 773 | if ( $accept ) { |
---|
| 774 | $url = str_replace(array('"',"url", "(", ")"), "", $url); |
---|
| 775 | // Store our current base url properties in case the new url is elsewhere |
---|
| 776 | $protocol = $this->_protocol; |
---|
| 777 | $host = $this->_base_host; |
---|
| 778 | $path = $this->_base_path; |
---|
| 779 | |
---|
| 780 | // If the protocol is php, assume that we will import using file:// |
---|
| 781 | $url = build_url($protocol == "php://" ? "file://" : $protocol, $host, $path, $url); |
---|
| 782 | |
---|
| 783 | $this->load_css_file($url); |
---|
| 784 | |
---|
| 785 | // Restore the current base url |
---|
| 786 | $this->_protocol = $protocol; |
---|
| 787 | $this->_base_host = $host; |
---|
| 788 | $this->_base_path = $path; |
---|
| 789 | } |
---|
| 790 | |
---|
| 791 | } |
---|
| 792 | |
---|
| 793 | /** |
---|
| 794 | * parse regular CSS blocks |
---|
| 795 | * |
---|
| 796 | * _parse_properties() creates a new Style object based on the provided |
---|
| 797 | * CSS rules. |
---|
| 798 | * |
---|
| 799 | * @param string $str CSS rules |
---|
| 800 | * @return Style |
---|
| 801 | */ |
---|
| 802 | private function _parse_properties($str) { |
---|
| 803 | $properties = explode(";", $str); |
---|
| 804 | |
---|
| 805 | // Create the style |
---|
| 806 | $style = new Style($this); |
---|
| 807 | foreach ($properties as $prop) { |
---|
| 808 | |
---|
| 809 | $prop = trim($prop); |
---|
| 810 | |
---|
| 811 | if ($prop == "") |
---|
| 812 | continue; |
---|
| 813 | |
---|
| 814 | $i = mb_strpos($prop, ":"); |
---|
| 815 | if ( $i === false ) |
---|
| 816 | continue; |
---|
| 817 | |
---|
| 818 | $prop_name = mb_strtolower(mb_substr($prop, 0, $i)); |
---|
| 819 | $value = mb_substr($prop, $i+1); |
---|
| 820 | $style->$prop_name = $value; |
---|
| 821 | |
---|
| 822 | } |
---|
| 823 | |
---|
| 824 | return $style; |
---|
| 825 | } |
---|
| 826 | |
---|
| 827 | /** |
---|
| 828 | * parse selector + rulesets |
---|
| 829 | * |
---|
| 830 | * @param string $str CSS selectors and rulesets |
---|
| 831 | */ |
---|
| 832 | private function _parse_sections($str) { |
---|
| 833 | // Pre-process: collapse all whitespace and strip whitespace around '>', |
---|
| 834 | // '.', ':', '+', '#' |
---|
| 835 | |
---|
| 836 | $patterns = array("/[\\s\n]+/", "/\\s+([>.:+#])\\s+/"); |
---|
| 837 | $replacements = array(" ", "\\1"); |
---|
| 838 | $str = preg_replace($patterns, $replacements, $str); |
---|
| 839 | |
---|
| 840 | $sections = explode("}", $str); |
---|
| 841 | foreach ($sections as $sect) { |
---|
| 842 | $i = mb_strpos($sect, "{"); |
---|
| 843 | |
---|
| 844 | $selectors = explode(",", mb_substr($sect, 0, $i)); |
---|
| 845 | $style = $this->_parse_properties(trim(mb_substr($sect, $i+1))); |
---|
| 846 | |
---|
| 847 | // Assign it to the selected elements |
---|
| 848 | foreach ($selectors as $selector) { |
---|
| 849 | $selector = trim($selector); |
---|
| 850 | |
---|
| 851 | if ($selector == "") |
---|
| 852 | continue; |
---|
| 853 | |
---|
| 854 | $this->add_style($selector, $style); |
---|
| 855 | } |
---|
| 856 | } |
---|
| 857 | } |
---|
| 858 | |
---|
| 859 | /** |
---|
| 860 | * dumps the entire stylesheet as a string |
---|
| 861 | * |
---|
| 862 | * Generates a string of each selector and associated style in the |
---|
| 863 | * Stylesheet. Useful for debugging. |
---|
| 864 | * |
---|
| 865 | * @return string |
---|
| 866 | */ |
---|
| 867 | function __toString() { |
---|
| 868 | $str = ""; |
---|
| 869 | foreach ($this->_styles as $selector => $style) |
---|
| 870 | $str .= "$selector => " . $style->__toString() . "\n"; |
---|
| 871 | |
---|
| 872 | return $str; |
---|
| 873 | } |
---|
| 874 | } |
---|
| 875 | ?> |
---|