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