1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @package s9e\TextFormatter |
5
|
|
|
* @copyright Copyright (c) 2010-2017 The s9e Authors |
6
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php The MIT License |
7
|
|
|
*/ |
8
|
|
|
namespace s9e\TextFormatter\Configurator\Helpers; |
9
|
|
|
|
10
|
|
|
use DOMDocument; |
11
|
|
|
use DOMElement; |
12
|
|
|
use DOMXPath; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* This class helps the RulesGenerator by analyzing a given template in order to answer questions |
16
|
|
|
* such as "can this tag be a child/descendant of that other tag?" and others related to the HTML5 |
17
|
|
|
* content model. |
18
|
|
|
* |
19
|
|
|
* We use the HTML5 specs to determine which children or descendants should be allowed or denied |
20
|
|
|
* based on HTML5 content models. While it does not exactly match HTML5 content models, it gets |
21
|
|
|
* pretty close. We also use HTML5 "optional end tag" rules to create closeParent rules. |
22
|
|
|
* |
23
|
|
|
* Currently, this method does not evaluate elements created with <xsl:element> correctly, or |
24
|
|
|
* attributes created with <xsl:attribute> and may never will due to the increased complexity it |
25
|
|
|
* would entail. Additionally, it does not evaluate the scope of <xsl:apply-templates/>. For |
26
|
|
|
* instance, it will treat <xsl:apply-templates select="LI"/> as if it was <xsl:apply-templates/> |
27
|
|
|
* |
28
|
|
|
* @link http://dev.w3.org/html5/spec/content-models.html#content-models |
29
|
|
|
* @link http://dev.w3.org/html5/spec/syntax.html#optional-tags |
30
|
|
|
* @see /scripts/patchTemplateInspector.php |
31
|
|
|
*/ |
32
|
|
|
class TemplateInspector |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var string allowChild bitfield (all branches) |
36
|
|
|
*/ |
37
|
|
|
protected $allowChildBitfield = "\0"; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var bool Whether elements are allowed as children |
41
|
|
|
*/ |
42
|
|
|
protected $allowsChildElements = true; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var bool Whether text nodes are allowed as children |
46
|
|
|
*/ |
47
|
|
|
protected $allowsText = true; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var string OR-ed bitfield representing all of the categories used by this template |
51
|
|
|
*/ |
52
|
|
|
protected $contentBitfield = "\0"; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var string denyDescendant bitfield |
56
|
|
|
*/ |
57
|
|
|
protected $denyDescendantBitfield = "\0"; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var DOMDocument Document containing the template |
61
|
|
|
*/ |
62
|
|
|
protected $dom; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var bool Whether this template contains any HTML elements |
66
|
|
|
*/ |
67
|
|
|
protected $hasElements = false; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var bool Whether this template renders non-whitespace text nodes at its root |
71
|
|
|
*/ |
72
|
|
|
protected $hasRootText = false; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var bool Whether this template should be considered a block-level element |
76
|
|
|
*/ |
77
|
|
|
protected $isBlock = false; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var bool Whether the template uses the "empty" content model |
81
|
|
|
*/ |
82
|
|
|
protected $isEmpty = true; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @var bool Whether this template adds to the list of active formatting elements |
86
|
|
|
*/ |
87
|
|
|
protected $isFormattingElement = false; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @var bool Whether this template lets content through via an xsl:apply-templates element |
91
|
|
|
*/ |
92
|
|
|
protected $isPassthrough = false; |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @var bool Whether all branches use the transparent content model |
96
|
|
|
*/ |
97
|
|
|
protected $isTransparent = false; |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @var bool Whether all branches have an ancestor that is a void element |
101
|
|
|
*/ |
102
|
|
|
protected $isVoid = true; |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @var array Names of every last HTML element that precedes an <xsl:apply-templates/> node |
106
|
|
|
*/ |
107
|
|
|
protected $leafNodes = []; |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @var bool Whether any branch has an element that preserves new lines by default (e.g. <pre>) |
111
|
|
|
*/ |
112
|
|
|
protected $preservesNewLines = false; |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @var array Bitfield of the first HTML element of every branch |
116
|
|
|
*/ |
117
|
|
|
protected $rootBitfields = []; |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* @var array Names of every HTML element that have no HTML parent |
121
|
|
|
*/ |
122
|
|
|
protected $rootNodes = []; |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @var DOMXPath XPath engine associated with $this->dom |
126
|
|
|
*/ |
127
|
|
|
protected $xpath; |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Constructor |
131
|
|
|
* |
132
|
|
|
* @param string $template Template content |
133
|
|
|
*/ |
134
|
|
|
public function __construct($template) |
135
|
|
|
{ |
136
|
|
|
$this->dom = TemplateHelper::loadTemplate($template); |
137
|
|
|
$this->xpath = new DOMXPath($this->dom); |
138
|
|
|
|
139
|
|
|
$this->analyseRootNodes(); |
140
|
|
|
$this->analyseBranches(); |
141
|
|
|
$this->analyseContent(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return whether this template allows a given child |
146
|
|
|
* |
147
|
|
|
* @param TemplateInspector $child |
148
|
|
|
* @return bool |
149
|
|
|
*/ |
150
|
|
|
public function allowsChild(TemplateInspector $child) |
151
|
|
|
{ |
152
|
|
|
// Sometimes, a template can technically be allowed as a child but denied as a descendant |
153
|
|
|
if (!$this->allowsDescendant($child)) |
154
|
|
|
{ |
155
|
|
|
return false; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
foreach ($child->rootBitfields as $rootBitfield) |
159
|
|
|
{ |
160
|
|
|
if (!self::match($rootBitfield, $this->allowChildBitfield)) |
161
|
|
|
{ |
162
|
|
|
return false; |
163
|
|
|
} |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
if (!$this->allowsText && $child->hasRootText) |
167
|
|
|
{ |
168
|
|
|
return false; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
return true; |
172
|
|
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* Return whether this template allows a given descendant |
176
|
|
|
* |
177
|
|
|
* @param TemplateInspector $descendant |
178
|
|
|
* @return bool |
179
|
|
|
*/ |
180
|
|
|
public function allowsDescendant(TemplateInspector $descendant) |
181
|
|
|
{ |
182
|
|
|
// Test whether the descendant is explicitly disallowed |
183
|
|
|
if (self::match($descendant->contentBitfield, $this->denyDescendantBitfield)) |
184
|
|
|
{ |
185
|
|
|
return false; |
186
|
|
|
} |
187
|
|
|
|
188
|
|
|
// Test whether the descendant contains any elements and we disallow elements |
189
|
|
|
if (!$this->allowsChildElements && $descendant->hasElements) |
190
|
|
|
{ |
191
|
|
|
return false; |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
return true; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
/** |
198
|
|
|
* Return whether this template allows elements as children |
199
|
|
|
* |
200
|
|
|
* @return bool |
201
|
|
|
*/ |
202
|
|
|
public function allowsChildElements() |
203
|
|
|
{ |
204
|
|
|
return $this->allowsChildElements; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
/** |
208
|
|
|
* Return whether this template allows text nodes as children |
209
|
|
|
* |
210
|
|
|
* @return bool |
211
|
|
|
*/ |
212
|
|
|
public function allowsText() |
213
|
|
|
{ |
214
|
|
|
return $this->allowsText; |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
/** |
218
|
|
|
* Return whether this template automatically closes given parent template |
219
|
|
|
* |
220
|
|
|
* @param TemplateInspector $parent |
221
|
|
|
* @return bool |
222
|
|
|
*/ |
223
|
|
|
public function closesParent(TemplateInspector $parent) |
224
|
|
|
{ |
225
|
|
|
foreach ($this->rootNodes as $rootName) |
226
|
|
|
{ |
227
|
|
|
if (empty(self::$htmlElements[$rootName]['cp'])) |
228
|
|
|
{ |
229
|
|
|
continue; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
foreach ($parent->leafNodes as $leafName) |
233
|
|
|
{ |
234
|
|
|
if (in_array($leafName, self::$htmlElements[$rootName]['cp'], true)) |
235
|
|
|
{ |
236
|
|
|
// If any of this template's root node closes one of the parent's leaf node, we |
237
|
|
|
// consider that this template closes the other one |
238
|
|
|
return true; |
239
|
|
|
} |
240
|
|
|
} |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
return false; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Return the source template as a DOMDocument |
248
|
|
|
* |
249
|
|
|
* NOTE: the document should not be modified |
250
|
|
|
* |
251
|
|
|
* @return DOMDocument |
252
|
|
|
*/ |
253
|
|
|
public function getDOM() |
254
|
|
|
{ |
255
|
|
|
return $this->dom; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Return whether this template should be considered a block-level element |
260
|
|
|
* |
261
|
|
|
* @return bool |
262
|
|
|
*/ |
263
|
|
|
public function isBlock() |
264
|
|
|
{ |
265
|
|
|
return $this->isBlock; |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Return whether this template adds to the list of active formatting elements |
270
|
|
|
* |
271
|
|
|
* @return bool |
272
|
|
|
*/ |
273
|
|
|
public function isFormattingElement() |
274
|
|
|
{ |
275
|
|
|
return $this->isFormattingElement; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* Return whether this template uses the "empty" content model |
280
|
|
|
* |
281
|
|
|
* @return bool |
282
|
|
|
*/ |
283
|
|
|
public function isEmpty() |
284
|
|
|
{ |
285
|
|
|
return $this->isEmpty; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* Return whether this template represents a single iframe |
290
|
|
|
* |
291
|
|
|
* @return bool |
292
|
|
|
*/ |
293
|
|
|
public function isIframe() |
294
|
|
|
{ |
295
|
|
|
return ($this->rootNodes === ['iframe']); |
296
|
|
|
} |
297
|
|
|
|
298
|
|
|
/** |
299
|
|
|
* Return whether this template lets content through via an xsl:apply-templates element |
300
|
|
|
* |
301
|
|
|
* @return bool |
302
|
|
|
*/ |
303
|
|
|
public function isPassthrough() |
304
|
|
|
{ |
305
|
|
|
return $this->isPassthrough; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
/** |
309
|
|
|
* Return whether this template uses the "transparent" content model |
310
|
|
|
* |
311
|
|
|
* @return bool |
312
|
|
|
*/ |
313
|
|
|
public function isTransparent() |
314
|
|
|
{ |
315
|
|
|
return $this->isTransparent; |
316
|
|
|
} |
317
|
|
|
|
318
|
|
|
/** |
319
|
|
|
* Return whether all branches have an ancestor that is a void element |
320
|
|
|
* |
321
|
|
|
* @return bool |
322
|
|
|
*/ |
323
|
|
|
public function isVoid() |
324
|
|
|
{ |
325
|
|
|
return $this->isVoid; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
/** |
329
|
|
|
* Return whether this template preserves the whitespace in its descendants |
330
|
|
|
* |
331
|
|
|
* @return bool |
332
|
|
|
*/ |
333
|
|
|
public function preservesNewLines() |
334
|
|
|
{ |
335
|
|
|
return $this->preservesNewLines; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
/** |
339
|
|
|
* Analyses the content of the whole template and set $this->contentBitfield accordingly |
340
|
|
|
*/ |
341
|
|
|
protected function analyseContent() |
342
|
|
|
{ |
343
|
|
|
// Get all non-XSL elements |
344
|
|
|
$query = '//*[namespace-uri() != "http://www.w3.org/1999/XSL/Transform"]'; |
345
|
|
|
|
346
|
|
|
foreach ($this->xpath->query($query) as $node) |
347
|
|
|
{ |
348
|
|
|
$this->contentBitfield |= $this->getBitfield($node->localName, 'c', $node); |
349
|
|
|
$this->hasElements = true; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
// Test whether this template is passthrough |
353
|
|
|
$this->isPassthrough = (bool) $this->xpath->evaluate('count(//xsl:apply-templates)'); |
354
|
|
|
} |
355
|
|
|
|
356
|
|
|
/** |
357
|
|
|
* Records the HTML elements (and their bitfield) rendered at the root of the template |
358
|
|
|
*/ |
359
|
|
|
protected function analyseRootNodes() |
360
|
|
|
{ |
361
|
|
|
// Get every non-XSL element with no non-XSL ancestor. This should return us the first |
362
|
|
|
// HTML element of every branch |
363
|
|
|
$query = '//*[namespace-uri() != "http://www.w3.org/1999/XSL/Transform"]' |
364
|
|
|
. '[not(ancestor::*[namespace-uri() != "http://www.w3.org/1999/XSL/Transform"])]'; |
365
|
|
|
|
366
|
|
|
foreach ($this->xpath->query($query) as $node) |
367
|
|
|
{ |
368
|
|
|
$elName = $node->localName; |
369
|
|
|
|
370
|
|
|
// Save the actual name of the root node |
371
|
|
|
$this->rootNodes[] = $elName; |
372
|
|
|
|
373
|
|
|
if (!isset(self::$htmlElements[$elName])) |
374
|
|
|
{ |
375
|
|
|
// Unknown elements are treated as if they were a <span> element |
376
|
|
|
$elName = 'span'; |
377
|
|
|
} |
378
|
|
|
|
379
|
|
|
// If any root node is a block-level element, we'll mark the template as such |
380
|
|
|
if ($this->elementIsBlock($elName, $node)) |
381
|
|
|
{ |
382
|
|
|
$this->isBlock = true; |
383
|
|
|
} |
384
|
|
|
|
385
|
|
|
$this->rootBitfields[] = $this->getBitfield($elName, 'c', $node); |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
// Test for non-whitespace text nodes at the root. For that we need a predicate that filters |
389
|
|
|
// out: nodes with a non-XSL ancestor, |
390
|
|
|
$predicate = '[not(ancestor::*[namespace-uri() != "http://www.w3.org/1999/XSL/Transform"])]'; |
391
|
|
|
|
392
|
|
|
// ..and nodes with an <xsl:attribute/>, <xsl:comment/> or <xsl:variable/> ancestor |
393
|
|
|
$predicate .= '[not(ancestor::xsl:attribute | ancestor::xsl:comment | ancestor::xsl:variable)]'; |
394
|
|
|
|
395
|
|
|
$query = '//text()[normalize-space() != ""]' . $predicate |
396
|
|
|
. '|' |
397
|
|
|
. '//xsl:text[normalize-space() != ""]' . $predicate |
398
|
|
|
. '|' |
399
|
|
|
. '//xsl:value-of' . $predicate; |
400
|
|
|
|
401
|
|
|
if ($this->evaluate($query, $this->dom->documentElement)) |
402
|
|
|
{ |
403
|
|
|
$this->hasRootText = true; |
404
|
|
|
} |
405
|
|
|
} |
406
|
|
|
|
407
|
|
|
/** |
408
|
|
|
* Analyses each branch that leads to an <xsl:apply-templates/> tag |
409
|
|
|
*/ |
410
|
|
|
protected function analyseBranches() |
411
|
|
|
{ |
412
|
|
|
/** |
413
|
|
|
* @var array allowChild bitfield for each branch |
414
|
|
|
*/ |
415
|
|
|
$branchBitfields = []; |
416
|
|
|
|
417
|
|
|
/** |
418
|
|
|
* @var bool Whether this template should be considered a formatting element |
419
|
|
|
*/ |
420
|
|
|
$isFormattingElement = true; |
421
|
|
|
|
422
|
|
|
// Consider this template transparent unless we find out there are no branches or that one |
423
|
|
|
// of the branches is not transparent |
424
|
|
|
$this->isTransparent = true; |
425
|
|
|
|
426
|
|
|
// For each <xsl:apply-templates/> element... |
427
|
|
|
foreach ($this->getXSLElements('apply-templates') as $applyTemplates) |
428
|
|
|
{ |
429
|
|
|
// ...we retrieve all non-XSL ancestors |
430
|
|
|
$nodes = $this->xpath->query( |
431
|
|
|
'ancestor::*[namespace-uri() != "http://www.w3.org/1999/XSL/Transform"]', |
432
|
|
|
$applyTemplates |
433
|
|
|
); |
434
|
|
|
|
435
|
|
|
/** |
436
|
|
|
* @var bool Whether this branch allows elements |
437
|
|
|
*/ |
438
|
|
|
$allowsChildElements = true; |
439
|
|
|
|
440
|
|
|
/** |
441
|
|
|
* @var bool Whether this branch allows text nodes |
442
|
|
|
*/ |
443
|
|
|
$allowsText = true; |
444
|
|
|
|
445
|
|
|
/** |
446
|
|
|
* @var string allowChild bitfield for current branch. Starts with the value associated |
447
|
|
|
* with <div> in order to approximate a value if the whole branch uses the |
448
|
|
|
* transparent content model |
449
|
|
|
*/ |
450
|
|
|
$branchBitfield = self::$htmlElements['div']['ac']; |
451
|
|
|
|
452
|
|
|
/** |
453
|
|
|
* @var bool Whether this branch denies all non-text descendants |
454
|
|
|
*/ |
455
|
|
|
$isEmpty = false; |
456
|
|
|
|
457
|
|
|
/** |
458
|
|
|
* @var bool Whether this branch contains a void element |
459
|
|
|
*/ |
460
|
|
|
$isVoid = false; |
461
|
|
|
|
462
|
|
|
/** |
463
|
|
|
* @var string Name of the last node of this branch |
464
|
|
|
*/ |
465
|
|
|
$leafNode = null; |
466
|
|
|
|
467
|
|
|
/** |
468
|
|
|
* @var boolean Whether this branch preserves new lines |
469
|
|
|
*/ |
470
|
|
|
$preservesNewLines = false; |
471
|
|
|
|
472
|
|
|
foreach ($nodes as $node) |
473
|
|
|
{ |
474
|
|
|
$elName = $leafNode = $node->localName; |
475
|
|
|
|
476
|
|
|
if (!isset(self::$htmlElements[$elName])) |
477
|
|
|
{ |
478
|
|
|
// Unknown elements are treated as if they were a <span> element |
479
|
|
|
$elName = 'span'; |
480
|
|
|
} |
481
|
|
|
|
482
|
|
|
// Test whether the element is void |
483
|
|
|
if ($this->hasProperty($elName, 'v', $node)) |
484
|
|
|
{ |
485
|
|
|
$isVoid = true; |
486
|
|
|
} |
487
|
|
|
|
488
|
|
|
// Test whether the element uses the "empty" content model |
489
|
|
|
if ($this->hasProperty($elName, 'e', $node)) |
490
|
|
|
{ |
491
|
|
|
$isEmpty = true; |
492
|
|
|
} |
493
|
|
|
|
494
|
|
|
if (!$this->hasProperty($elName, 't', $node)) |
495
|
|
|
{ |
496
|
|
|
// If the element isn't transparent, we reset its bitfield |
497
|
|
|
$branchBitfield = "\0"; |
498
|
|
|
|
499
|
|
|
// Also, it means that the template itself isn't transparent |
500
|
|
|
$this->isTransparent = false; |
501
|
|
|
} |
502
|
|
|
|
503
|
|
|
// Test whether this element is a formatting element |
504
|
|
|
if (!$this->hasProperty($elName, 'fe', $node) |
505
|
|
|
&& !$this->isFormattingSpan($node)) |
506
|
|
|
{ |
507
|
|
|
$isFormattingElement = false; |
508
|
|
|
} |
509
|
|
|
|
510
|
|
|
// Test whether this branch allows elements |
511
|
|
|
$allowsChildElements = !$this->hasProperty($elName, 'to', $node); |
512
|
|
|
|
513
|
|
|
// Test whether this branch allows text nodes |
514
|
|
|
$allowsText = !$this->hasProperty($elName, 'nt', $node); |
515
|
|
|
|
516
|
|
|
// allowChild rules are cumulative if transparent, and reset above otherwise |
517
|
|
|
$branchBitfield |= $this->getBitfield($elName, 'ac', $node); |
518
|
|
|
|
519
|
|
|
// denyDescendant rules are cumulative |
520
|
|
|
$this->denyDescendantBitfield |= $this->getBitfield($elName, 'dd', $node); |
521
|
|
|
|
522
|
|
|
// Test whether this branch preserves whitespace by inspecting the current element |
523
|
|
|
// and the value of its style attribute. Technically, this block of code also tests |
524
|
|
|
// this element's descendants' style attributes but the result is the same as we |
525
|
|
|
// need to check every element of this branch in order |
526
|
|
|
$style = ''; |
527
|
|
|
|
528
|
|
|
if ($this->hasProperty($elName, 'pre', $node)) |
529
|
|
|
{ |
530
|
|
|
$style .= 'white-space:pre;'; |
531
|
|
|
} |
532
|
|
|
|
533
|
|
|
if ($node->hasAttribute('style')) |
534
|
|
|
{ |
535
|
|
|
$style .= $node->getAttribute('style') . ';'; |
536
|
|
|
} |
537
|
|
|
|
538
|
|
|
$attributes = $this->xpath->query('.//xsl:attribute[@name="style"]', $node); |
539
|
|
|
foreach ($attributes as $attribute) |
540
|
|
|
{ |
541
|
|
|
$style .= $attribute->textContent; |
542
|
|
|
} |
543
|
|
|
|
544
|
|
|
preg_match_all( |
545
|
|
|
'/white-space\\s*:\\s*(no|pre)/i', |
546
|
|
|
strtolower($style), |
547
|
|
|
$matches |
548
|
|
|
); |
549
|
|
|
foreach ($matches[1] as $match) |
550
|
|
|
{ |
551
|
|
|
// TRUE: "pre", "pre-line" and "pre-wrap" |
552
|
|
|
// FALSE: "normal", "nowrap" |
553
|
|
|
$preservesNewLines = ($match === 'pre'); |
554
|
|
|
} |
555
|
|
|
} |
556
|
|
|
|
557
|
|
|
// Add this branch's bitfield to the list |
558
|
|
|
$branchBitfields[] = $branchBitfield; |
559
|
|
|
|
560
|
|
|
// Save the name of the last node processed |
561
|
|
|
if (isset($leafNode)) |
562
|
|
|
{ |
563
|
|
|
$this->leafNodes[] = $leafNode; |
564
|
|
|
} |
565
|
|
|
|
566
|
|
|
// If any branch disallows elements, the template disallows elements |
567
|
|
|
if (!$allowsChildElements) |
568
|
|
|
{ |
569
|
|
|
$this->allowsChildElements = false; |
570
|
|
|
} |
571
|
|
|
|
572
|
|
|
// If any branch disallows text, the template disallows text |
573
|
|
|
if (!$allowsText) |
574
|
|
|
{ |
575
|
|
|
$this->allowsText = false; |
576
|
|
|
} |
577
|
|
|
|
578
|
|
|
// If any branch is not empty, the template is not empty |
579
|
|
|
if (!$isEmpty) |
580
|
|
|
{ |
581
|
|
|
$this->isEmpty = false; |
582
|
|
|
} |
583
|
|
|
|
584
|
|
|
// If any branch is not void, the template is not void |
585
|
|
|
if (!$isVoid) |
586
|
|
|
{ |
587
|
|
|
$this->isVoid = false; |
588
|
|
|
} |
589
|
|
|
|
590
|
|
|
// If any branch preserves new lines, the template preserves new lines |
591
|
|
|
if ($preservesNewLines) |
592
|
|
|
{ |
593
|
|
|
$this->preservesNewLines = true; |
594
|
|
|
} |
595
|
|
|
} |
596
|
|
|
|
597
|
|
|
if (empty($branchBitfields)) |
598
|
|
|
{ |
599
|
|
|
// No branches => not transparent and no child elements |
600
|
|
|
$this->allowsChildElements = false; |
601
|
|
|
$this->isTransparent = false; |
602
|
|
|
} |
603
|
|
|
else |
604
|
|
|
{ |
605
|
|
|
// Take the bitfield of each branch and reduce them to a single ANDed bitfield |
606
|
|
|
$this->allowChildBitfield = $branchBitfields[0]; |
607
|
|
|
foreach ($branchBitfields as $branchBitfield) |
608
|
|
|
{ |
609
|
|
|
$this->allowChildBitfield &= $branchBitfield; |
610
|
|
|
} |
611
|
|
|
|
612
|
|
|
// Set the isFormattingElement property to our final value, but only if this template |
613
|
|
|
// had any branches |
614
|
|
|
if (!empty($this->leafNodes)) |
615
|
|
|
{ |
616
|
|
|
$this->isFormattingElement = $isFormattingElement; |
617
|
|
|
} |
618
|
|
|
} |
619
|
|
|
} |
620
|
|
|
|
621
|
|
|
/** |
622
|
|
|
* Test whether given element is a block-level element |
623
|
|
|
* |
624
|
|
|
* @param string $elName Element name |
625
|
|
|
* @param DOMElement $node Context node |
626
|
|
|
* @return bool |
627
|
|
|
*/ |
628
|
|
|
protected function elementIsBlock($elName, DOMElement $node) |
629
|
|
|
{ |
630
|
|
|
$style = $this->getStyle($node); |
631
|
|
|
if (preg_match('(\\bdisplay\\s*:\\s*block)i', $style)) |
632
|
|
|
{ |
633
|
|
|
return true; |
634
|
|
|
} |
635
|
|
|
if (preg_match('(\\bdisplay\\s*:\\s*inline)i', $style)) |
636
|
|
|
{ |
637
|
|
|
return false; |
638
|
|
|
} |
639
|
|
|
|
640
|
|
|
return $this->hasProperty($elName, 'b', $node); |
641
|
|
|
} |
642
|
|
|
|
643
|
|
|
/** |
644
|
|
|
* Evaluate a boolean XPath query |
645
|
|
|
* |
646
|
|
|
* @param string $query XPath query |
647
|
|
|
* @param DOMElement $node Context node |
648
|
|
|
* @return boolean |
649
|
|
|
*/ |
650
|
|
|
protected function evaluate($query, DOMElement $node) |
651
|
|
|
{ |
652
|
|
|
return $this->xpath->evaluate('boolean(' . $query . ')', $node); |
653
|
|
|
} |
654
|
|
|
|
655
|
|
|
/** |
656
|
|
|
* Retrieve and return the inline style assigned to given element |
657
|
|
|
* |
658
|
|
|
* @param DOMElement $node Context node |
659
|
|
|
* @return string |
660
|
|
|
*/ |
661
|
|
|
protected function getStyle(DOMElement $node) |
662
|
|
|
{ |
663
|
|
|
// Start with the inline attribute |
664
|
|
|
$style = $node->getAttribute('style'); |
665
|
|
|
|
666
|
|
|
// Add the content of any xsl:attribute named "style". This will miss optional attributes |
667
|
|
|
$xpath = new DOMXPath($node->ownerDocument); |
668
|
|
|
$query = 'xsl:attribute[@name="style"]'; |
669
|
|
|
foreach ($xpath->query($query, $node) as $attribute) |
670
|
|
|
{ |
671
|
|
|
$style .= ';' . $attribute->textContent; |
672
|
|
|
} |
673
|
|
|
|
674
|
|
|
return $style; |
675
|
|
|
} |
676
|
|
|
|
677
|
|
|
/** |
678
|
|
|
* Get all XSL elements of given name |
679
|
|
|
* |
680
|
|
|
* @param string $elName XSL element's name, e.g. "apply-templates" |
681
|
|
|
* @return \DOMNodeList |
682
|
|
|
*/ |
683
|
|
|
protected function getXSLElements($elName) |
684
|
|
|
{ |
685
|
|
|
return $this->dom->getElementsByTagNameNS('http://www.w3.org/1999/XSL/Transform', $elName); |
686
|
|
|
} |
687
|
|
|
|
688
|
|
|
/** |
689
|
|
|
* Test whether given node is a span element used for formatting |
690
|
|
|
* |
691
|
|
|
* Will return TRUE if the node is a span element with a class attribute and/or a style attribute |
692
|
|
|
* and no other attributes |
693
|
|
|
* |
694
|
|
|
* @param DOMElement $node |
695
|
|
|
* @return boolean |
696
|
|
|
*/ |
697
|
|
|
protected function isFormattingSpan(DOMElement $node) |
698
|
|
|
{ |
699
|
|
|
if ($node->nodeName !== 'span') |
700
|
|
|
{ |
701
|
|
|
return false; |
702
|
|
|
} |
703
|
|
|
|
704
|
|
|
if ($node->getAttribute('class') === '' |
705
|
|
|
&& $node->getAttribute('style') === '') |
706
|
|
|
{ |
707
|
|
|
return false; |
708
|
|
|
} |
709
|
|
|
|
710
|
|
|
foreach ($node->attributes as $attrName => $attribute) |
711
|
|
|
{ |
712
|
|
|
if ($attrName !== 'class' && $attrName !== 'style') |
713
|
|
|
{ |
714
|
|
|
return false; |
715
|
|
|
} |
716
|
|
|
} |
717
|
|
|
|
718
|
|
|
return true; |
719
|
|
|
} |
720
|
|
|
|
721
|
|
|
/** |
722
|
|
|
* "What is this?" you might ask. This is basically a compressed version of the HTML5 content |
723
|
|
|
* models and rules, with some liberties taken. |
724
|
|
|
* |
725
|
|
|
* For each element, up to three bitfields are defined: "c", "ac" and "dd". Bitfields are stored |
726
|
|
|
* as raw bytes, formatted using the octal notation to keep the sources ASCII. |
727
|
|
|
* |
728
|
|
|
* "c" represents the categories the element belongs to. The categories are comprised of HTML5 |
729
|
|
|
* content models (such as "phrasing content" or "interactive content") plus a few special |
730
|
|
|
* categories created to cover the parts of the specs that refer to "a group of X and Y |
731
|
|
|
* elements" rather than a specific content model. |
732
|
|
|
* |
733
|
|
|
* "ac" represents the categories that are allowed as children of given element. |
734
|
|
|
* |
735
|
|
|
* "dd" represents the categories that must not appear as a descendant of given element. |
736
|
|
|
* |
737
|
|
|
* Sometimes, HTML5 specifies some restrictions on when an element can accept certain children, |
738
|
|
|
* or what categories the element belongs to. For example, an <img> element is only part of the |
739
|
|
|
* "interactive content" category if it has a "usemap" attribute. Those restrictions are |
740
|
|
|
* expressed as an XPath expression and stored using the concatenation of the key of the bitfield |
741
|
|
|
* plus the bit number of the category. For instance, if "interactive content" got assigned to |
742
|
|
|
* bit 2, the definition of the <img> element will contain a key "c2" with value "@usemap". |
743
|
|
|
* |
744
|
|
|
* Additionally, other flags are set: |
745
|
|
|
* |
746
|
|
|
* "t" indicates that the element uses the "transparent" content model. |
747
|
|
|
* "e" indicates that the element uses the "empty" content model. |
748
|
|
|
* "v" indicates that the element is a void element. |
749
|
|
|
* "nt" indicates that the element does not accept text nodes. (no text) |
750
|
|
|
* "to" indicates that the element should only contain text. (text-only) |
751
|
|
|
* "fe" indicates that the element is a formatting element. It will automatically be reopened |
752
|
|
|
* when closed by an end tag of a different name. |
753
|
|
|
* "b" indicates that the element is not phrasing content, which makes it likely to act like |
754
|
|
|
* a block element. |
755
|
|
|
* |
756
|
|
|
* Finally, HTML5 defines "optional end tag" rules, where one element automatically closes its |
757
|
|
|
* predecessor. Those are used to generate closeParent rules and are stored in the "cp" key. |
758
|
|
|
* |
759
|
|
|
* @var array |
760
|
|
|
* @see /scripts/patchTemplateInspector.php |
761
|
|
|
*/ |
762
|
|
|
protected static $htmlElements = [ |
763
|
|
|
'a'=>['c'=>"\17\0\0\0\0\1",'c3'=>'@href','ac'=>"\0",'dd'=>"\10\0\0\0\0\1",'t'=>1,'fe'=>1], |
764
|
|
|
'abbr'=>['c'=>"\7",'ac'=>"\4"], |
765
|
|
|
'address'=>['c'=>"\3\40",'ac'=>"\1",'dd'=>"\0\45",'b'=>1,'cp'=>['p']], |
766
|
|
|
'article'=>['c'=>"\3\4",'ac'=>"\1",'b'=>1,'cp'=>['p']], |
767
|
|
|
'aside'=>['c'=>"\3\4",'ac'=>"\1",'dd'=>"\0\0\0\0\10",'b'=>1,'cp'=>['p']], |
768
|
|
|
'audio'=>['c'=>"\57",'c3'=>'@controls','c1'=>'@controls','ac'=>"\0\0\0\104",'ac26'=>'not(@src)','dd'=>"\0\0\0\0\0\2",'dd41'=>'@src','t'=>1], |
769
|
|
|
'b'=>['c'=>"\7",'ac'=>"\4",'fe'=>1], |
770
|
|
|
'base'=>['c'=>"\20",'nt'=>1,'e'=>1,'v'=>1,'b'=>1], |
771
|
|
|
'bdi'=>['c'=>"\7",'ac'=>"\4"], |
772
|
|
|
'bdo'=>['c'=>"\7",'ac'=>"\4"], |
773
|
|
|
'blockquote'=>['c'=>"\203",'ac'=>"\1",'b'=>1,'cp'=>['p']], |
774
|
|
|
'body'=>['c'=>"\200\0\4",'ac'=>"\1",'b'=>1], |
775
|
|
|
'br'=>['c'=>"\5",'nt'=>1,'e'=>1,'v'=>1], |
776
|
|
|
'button'=>['c'=>"\117",'ac'=>"\4",'dd'=>"\10"], |
777
|
|
|
'canvas'=>['c'=>"\47",'ac'=>"\0",'t'=>1], |
778
|
|
|
'caption'=>['c'=>"\0\2",'ac'=>"\1",'dd'=>"\0\0\0\200",'b'=>1], |
779
|
|
|
'cite'=>['c'=>"\7",'ac'=>"\4"], |
780
|
|
|
'code'=>['c'=>"\7",'ac'=>"\4",'fe'=>1], |
781
|
|
|
'col'=>['c'=>"\0\0\20",'nt'=>1,'e'=>1,'v'=>1,'b'=>1], |
782
|
|
|
'colgroup'=>['c'=>"\0\2",'ac'=>"\0\0\20",'ac20'=>'not(@span)','nt'=>1,'e'=>1,'e0'=>'@span','b'=>1], |
783
|
|
|
'data'=>['c'=>"\7",'ac'=>"\4"], |
784
|
|
|
'datalist'=>['c'=>"\5",'ac'=>"\4\200\0\10"], |
785
|
|
|
'dd'=>['c'=>"\0\0\200",'ac'=>"\1",'b'=>1,'cp'=>['dd','dt']], |
786
|
|
|
'del'=>['c'=>"\5",'ac'=>"\0",'t'=>1], |
787
|
|
|
'details'=>['c'=>"\213",'ac'=>"\1\0\0\2",'b'=>1,'cp'=>['p']], |
788
|
|
|
'dfn'=>['c'=>"\7\0\0\0\40",'ac'=>"\4",'dd'=>"\0\0\0\0\40"], |
789
|
|
|
'div'=>['c'=>"\3",'ac'=>"\1",'b'=>1,'cp'=>['p']], |
790
|
|
|
'dl'=>['c'=>"\3",'c1'=>'dt and dd','ac'=>"\0\200\200",'nt'=>1,'b'=>1,'cp'=>['p']], |
791
|
|
|
'dt'=>['c'=>"\0\0\200",'ac'=>"\1",'dd'=>"\0\5\0\40",'b'=>1,'cp'=>['dd','dt']], |
792
|
|
|
'em'=>['c'=>"\7",'ac'=>"\4",'fe'=>1], |
793
|
|
|
'embed'=>['c'=>"\57",'nt'=>1,'e'=>1,'v'=>1], |
794
|
|
|
'fieldset'=>['c'=>"\303",'ac'=>"\1\0\0\20",'b'=>1,'cp'=>['p']], |
795
|
|
|
'figcaption'=>['c'=>"\0\0\0\0\0\4",'ac'=>"\1",'b'=>1,'cp'=>['p']], |
796
|
|
|
'figure'=>['c'=>"\203",'ac'=>"\1\0\0\0\0\4",'b'=>1,'cp'=>['p']], |
797
|
|
|
'footer'=>['c'=>"\3\40",'ac'=>"\1",'dd'=>"\0\0\0\0\10",'b'=>1,'cp'=>['p']], |
798
|
|
|
'form'=>['c'=>"\3\0\0\0\20",'ac'=>"\1",'dd'=>"\0\0\0\0\20",'b'=>1,'cp'=>['p']], |
799
|
|
|
'h1'=>['c'=>"\3\1",'ac'=>"\4",'b'=>1,'cp'=>['p']], |
800
|
|
|
'h2'=>['c'=>"\3\1",'ac'=>"\4",'b'=>1,'cp'=>['p']], |
801
|
|
|
'h3'=>['c'=>"\3\1",'ac'=>"\4",'b'=>1,'cp'=>['p']], |
802
|
|
|
'h4'=>['c'=>"\3\1",'ac'=>"\4",'b'=>1,'cp'=>['p']], |
803
|
|
|
'h5'=>['c'=>"\3\1",'ac'=>"\4",'b'=>1,'cp'=>['p']], |
804
|
|
|
'h6'=>['c'=>"\3\1",'ac'=>"\4",'b'=>1,'cp'=>['p']], |
805
|
|
|
'head'=>['c'=>"\0\0\4",'ac'=>"\20",'nt'=>1,'b'=>1], |
806
|
|
|
'header'=>['c'=>"\3\40\0\40",'ac'=>"\1",'dd'=>"\0\0\0\0\10",'b'=>1,'cp'=>['p']], |
807
|
|
|
'hr'=>['c'=>"\1\100",'nt'=>1,'e'=>1,'v'=>1,'b'=>1,'cp'=>['p']], |
808
|
|
|
'html'=>['c'=>"\0",'ac'=>"\0\0\4",'nt'=>1,'b'=>1], |
809
|
|
|
'i'=>['c'=>"\7",'ac'=>"\4",'fe'=>1], |
810
|
|
|
'iframe'=>['c'=>"\57",'ac'=>"\4"], |
811
|
|
|
'img'=>['c'=>"\57\20\10",'c3'=>'@usemap','nt'=>1,'e'=>1,'v'=>1], |
812
|
|
|
'input'=>['c'=>"\17\20",'c3'=>'@type!="hidden"','c12'=>'@type!="hidden" or @type="hidden"','c1'=>'@type!="hidden"','nt'=>1,'e'=>1,'v'=>1], |
813
|
|
|
'ins'=>['c'=>"\7",'ac'=>"\0",'t'=>1], |
814
|
|
|
'kbd'=>['c'=>"\7",'ac'=>"\4"], |
815
|
|
|
'keygen'=>['c'=>"\117",'nt'=>1,'e'=>1,'v'=>1], |
816
|
|
|
'label'=>['c'=>"\17\20\0\0\4",'ac'=>"\4",'dd'=>"\0\0\1\0\4"], |
817
|
|
|
'legend'=>['c'=>"\0\0\0\20",'ac'=>"\4",'b'=>1], |
818
|
|
|
'li'=>['c'=>"\0\0\0\0\200",'ac'=>"\1",'b'=>1,'cp'=>['li']], |
819
|
|
|
'link'=>['c'=>"\20",'nt'=>1,'e'=>1,'v'=>1,'b'=>1], |
820
|
|
|
'main'=>['c'=>"\3\0\0\0\10",'ac'=>"\1",'b'=>1,'cp'=>['p']], |
821
|
|
|
'mark'=>['c'=>"\7",'ac'=>"\4"], |
822
|
|
|
'media element'=>['c'=>"\0\0\0\0\0\2",'nt'=>1,'b'=>1], |
823
|
|
|
'menu'=>['c'=>"\1\100",'ac'=>"\0\300",'nt'=>1,'b'=>1,'cp'=>['p']], |
824
|
|
|
'menuitem'=>['c'=>"\0\100",'nt'=>1,'e'=>1,'v'=>1,'b'=>1], |
825
|
|
|
'meta'=>['c'=>"\20",'nt'=>1,'e'=>1,'v'=>1,'b'=>1], |
826
|
|
|
'meter'=>['c'=>"\7\0\1\0\2",'ac'=>"\4",'dd'=>"\0\0\0\0\2"], |
827
|
|
|
'nav'=>['c'=>"\3\4",'ac'=>"\1",'dd'=>"\0\0\0\0\10",'b'=>1,'cp'=>['p']], |
828
|
|
|
'noscript'=>['c'=>"\25",'nt'=>1], |
829
|
|
|
'object'=>['c'=>"\147",'ac'=>"\0\0\0\0\1",'t'=>1], |
830
|
|
|
'ol'=>['c'=>"\3",'c1'=>'li','ac'=>"\0\200\0\0\200",'nt'=>1,'b'=>1,'cp'=>['p']], |
831
|
|
|
'optgroup'=>['c'=>"\0\0\2",'ac'=>"\0\200\0\10",'nt'=>1,'b'=>1,'cp'=>['optgroup','option']], |
832
|
|
|
'option'=>['c'=>"\0\0\2\10",'b'=>1,'cp'=>['option']], |
833
|
|
|
'output'=>['c'=>"\107",'ac'=>"\4"], |
834
|
|
|
'p'=>['c'=>"\3",'ac'=>"\4",'b'=>1,'cp'=>['p']], |
835
|
|
|
'param'=>['c'=>"\0\0\0\0\1",'nt'=>1,'e'=>1,'v'=>1,'b'=>1], |
836
|
|
|
'picture'=>['c'=>"\45",'ac'=>"\0\200\10",'nt'=>1], |
837
|
|
|
'pre'=>['c'=>"\3",'ac'=>"\4",'pre'=>1,'b'=>1,'cp'=>['p']], |
838
|
|
|
'progress'=>['c'=>"\7\0\1\1",'ac'=>"\4",'dd'=>"\0\0\0\1"], |
839
|
|
|
'q'=>['c'=>"\7",'ac'=>"\4"], |
840
|
|
|
'rb'=>['c'=>"\0\10",'ac'=>"\4",'b'=>1], |
841
|
|
|
'rp'=>['c'=>"\0\10\100",'ac'=>"\4",'b'=>1,'cp'=>['rp','rt']], |
842
|
|
|
'rt'=>['c'=>"\0\10\100",'ac'=>"\4",'b'=>1,'cp'=>['rp','rt']], |
843
|
|
|
'rtc'=>['c'=>"\0\10",'ac'=>"\4\0\100",'b'=>1], |
844
|
|
|
'ruby'=>['c'=>"\7",'ac'=>"\4\10"], |
845
|
|
|
's'=>['c'=>"\7",'ac'=>"\4",'fe'=>1], |
846
|
|
|
'samp'=>['c'=>"\7",'ac'=>"\4"], |
847
|
|
|
'script'=>['c'=>"\25\200",'to'=>1], |
848
|
|
|
'section'=>['c'=>"\3\4",'ac'=>"\1",'b'=>1,'cp'=>['p']], |
849
|
|
|
'select'=>['c'=>"\117",'ac'=>"\0\200\2",'nt'=>1], |
850
|
|
|
'small'=>['c'=>"\7",'ac'=>"\4",'fe'=>1], |
851
|
|
|
'source'=>['c'=>"\0\0\10\4",'nt'=>1,'e'=>1,'v'=>1,'b'=>1], |
852
|
|
|
'span'=>['c'=>"\7",'ac'=>"\4"], |
853
|
|
|
'strong'=>['c'=>"\7",'ac'=>"\4",'fe'=>1], |
854
|
|
|
'style'=>['c'=>"\20",'to'=>1,'b'=>1], |
855
|
|
|
'sub'=>['c'=>"\7",'ac'=>"\4"], |
856
|
|
|
'summary'=>['c'=>"\0\0\0\2",'ac'=>"\4\1",'b'=>1], |
857
|
|
|
'sup'=>['c'=>"\7",'ac'=>"\4"], |
858
|
|
|
'table'=>['c'=>"\3\0\0\200",'ac'=>"\0\202",'nt'=>1,'b'=>1,'cp'=>['p']], |
859
|
|
|
'tbody'=>['c'=>"\0\2",'ac'=>"\0\200\0\0\100",'nt'=>1,'b'=>1,'cp'=>['tbody','td','tfoot','th','thead','tr']], |
860
|
|
|
'td'=>['c'=>"\200\0\40",'ac'=>"\1",'b'=>1,'cp'=>['td','th']], |
861
|
|
|
'template'=>['c'=>"\25\200\20",'nt'=>1], |
862
|
|
|
'textarea'=>['c'=>"\117",'pre'=>1,'to'=>1], |
863
|
|
|
'tfoot'=>['c'=>"\0\2",'ac'=>"\0\200\0\0\100",'nt'=>1,'b'=>1,'cp'=>['tbody','td','th','thead','tr']], |
864
|
|
|
'th'=>['c'=>"\0\0\40",'ac'=>"\1",'dd'=>"\0\5\0\40",'b'=>1,'cp'=>['td','th']], |
865
|
|
|
'thead'=>['c'=>"\0\2",'ac'=>"\0\200\0\0\100",'nt'=>1,'b'=>1], |
866
|
|
|
'time'=>['c'=>"\7",'ac'=>"\4",'ac2'=>'@datetime'], |
867
|
|
|
'title'=>['c'=>"\20",'to'=>1,'b'=>1], |
868
|
|
|
'tr'=>['c'=>"\0\2\0\0\100",'ac'=>"\0\200\40",'nt'=>1,'b'=>1,'cp'=>['td','th','tr']], |
869
|
|
|
'track'=>['c'=>"\0\0\0\100",'nt'=>1,'e'=>1,'v'=>1,'b'=>1], |
870
|
|
|
'u'=>['c'=>"\7",'ac'=>"\4",'fe'=>1], |
871
|
|
|
'ul'=>['c'=>"\3",'c1'=>'li','ac'=>"\0\200\0\0\200",'nt'=>1,'b'=>1,'cp'=>['p']], |
872
|
|
|
'var'=>['c'=>"\7",'ac'=>"\4"], |
873
|
|
|
'video'=>['c'=>"\57",'c3'=>'@controls','ac'=>"\0\0\0\104",'ac26'=>'not(@src)','dd'=>"\0\0\0\0\0\2",'dd41'=>'@src','t'=>1], |
874
|
|
|
'wbr'=>['c'=>"\5",'nt'=>1,'e'=>1,'v'=>1] |
875
|
|
|
]; |
876
|
|
|
|
877
|
|
|
/** |
878
|
|
|
* Get the bitfield value for a given element name in a given context |
879
|
|
|
* |
880
|
|
|
* @param string $elName Name of the HTML element |
881
|
|
|
* @param string $k Bitfield name: either 'c', 'ac' or 'dd' |
882
|
|
|
* @param DOMElement $node Context node (not necessarily the same as $elName) |
883
|
|
|
* @return string |
884
|
|
|
*/ |
885
|
|
|
protected function getBitfield($elName, $k, DOMElement $node) |
886
|
|
|
{ |
887
|
|
|
if (!isset(self::$htmlElements[$elName][$k])) |
888
|
|
|
{ |
889
|
|
|
return "\0"; |
890
|
|
|
} |
891
|
|
|
|
892
|
|
|
$bitfield = self::$htmlElements[$elName][$k]; |
893
|
|
|
foreach (str_split($bitfield, 1) as $byteNumber => $char) |
894
|
|
|
{ |
895
|
|
|
$byteValue = ord($char); |
896
|
|
|
for ($bitNumber = 0; $bitNumber < 8; ++$bitNumber) |
897
|
|
|
{ |
898
|
|
|
$bitValue = 1 << $bitNumber; |
899
|
|
|
if (!($byteValue & $bitValue)) |
900
|
|
|
{ |
901
|
|
|
// The bit is not set |
902
|
|
|
continue; |
903
|
|
|
} |
904
|
|
|
|
905
|
|
|
$n = $byteNumber * 8 + $bitNumber; |
906
|
|
|
|
907
|
|
|
// Test for an XPath condition for that category |
908
|
|
|
if (isset(self::$htmlElements[$elName][$k . $n])) |
909
|
|
|
{ |
910
|
|
|
$xpath = 'boolean(' . self::$htmlElements[$elName][$k . $n] . ')'; |
911
|
|
|
|
912
|
|
|
// If the XPath condition is not fulfilled... |
913
|
|
|
if (!$this->evaluate($xpath, $node)) |
914
|
|
|
{ |
915
|
|
|
// ...turn off the corresponding bit |
916
|
|
|
$byteValue ^= $bitValue; |
917
|
|
|
|
918
|
|
|
// Update the original bitfield |
919
|
|
|
$bitfield[$byteNumber] = chr($byteValue); |
920
|
|
|
} |
921
|
|
|
} |
922
|
|
|
} |
923
|
|
|
} |
924
|
|
|
|
925
|
|
|
return $bitfield; |
926
|
|
|
} |
927
|
|
|
|
928
|
|
|
/** |
929
|
|
|
* Test whether given element has given property in context |
930
|
|
|
* |
931
|
|
|
* @param string $elName Element name |
932
|
|
|
* @param string $propName Property name, see self::$htmlElements |
933
|
|
|
* @param DOMElement $node Context node |
934
|
|
|
* @return bool |
935
|
|
|
*/ |
936
|
|
|
protected function hasProperty($elName, $propName, DOMElement $node) |
937
|
|
|
{ |
938
|
|
|
if (!empty(self::$htmlElements[$elName][$propName])) |
939
|
|
|
{ |
940
|
|
|
// Test the XPath condition |
941
|
|
|
if (!isset(self::$htmlElements[$elName][$propName . '0']) |
942
|
|
|
|| $this->evaluate(self::$htmlElements[$elName][$propName . '0'], $node)) |
943
|
|
|
{ |
944
|
|
|
return true; |
945
|
|
|
} |
946
|
|
|
} |
947
|
|
|
|
948
|
|
|
return false; |
949
|
|
|
} |
950
|
|
|
|
951
|
|
|
/** |
952
|
|
|
* Test whether two bitfields have any bits in common |
953
|
|
|
* |
954
|
|
|
* @param string $bitfield1 |
955
|
|
|
* @param string $bitfield2 |
956
|
|
|
* @return bool |
957
|
|
|
*/ |
958
|
|
|
protected static function match($bitfield1, $bitfield2) |
959
|
|
|
{ |
960
|
|
|
return (trim($bitfield1 & $bitfield2, "\0") !== ''); |
961
|
|
|
} |
962
|
|
|
} |