Completed
Push — master ( a9ac01...2b3709 )
by Josh
19:10
created

TemplateForensics::allowsText()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/**
4
* @package   s9e\TextFormatter
5
* @copyright Copyright (c) 2010-2016 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/patchTemplateForensics.php
31
*/
32
class TemplateForensics
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  self $child
148
	* @return bool
149
	*/
150
	public function allowsChild(self $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  self $descendant
178
	* @return bool
179
	*/
180
	public function allowsDescendant(self $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  self $parent
221
	* @return bool
222
	*/
223
	public function closesParent(self $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 lets content through via an xsl:apply-templates element
290
	*
291
	* @return bool
292
	*/
293
	public function isPassthrough()
294
	{
295
		return $this->isPassthrough;
296
	}
297
298
	/**
299
	* Return whether this template uses the "transparent" content model
300
	*
301
	* @return bool
302
	*/
303
	public function isTransparent()
304
	{
305
		return $this->isTransparent;
306
	}
307
308
	/**
309
	* Return whether all branches have an ancestor that is a void element
310
	*
311
	* @return bool
312
	*/
313
	public function isVoid()
314
	{
315
		return $this->isVoid;
316
	}
317
318
	/**
319
	* Return whether this template preserves the whitespace in its descendants
320
	*
321
	* @return bool
322
	*/
323
	public function preservesNewLines()
324
	{
325
		return $this->preservesNewLines;
326
	}
327
328
	/**
329
	* Analyses the content of the whole template and set $this->contentBitfield accordingly
330
	*/
331
	protected function analyseContent()
332
	{
333
		// Get all non-XSL elements
334
		$query = '//*[namespace-uri() != "http://www.w3.org/1999/XSL/Transform"]';
335
336
		foreach ($this->xpath->query($query) as $node)
337
		{
338
			$this->contentBitfield |= $this->getBitfield($node->localName, 'c', $node);
339
			$this->hasElements = true;
340
		}
341
342
		// Test whether this template is passthrough
343
		$this->isPassthrough = (bool) $this->xpath->evaluate('count(//xsl:apply-templates)');
344
	}
345
346
	/**
347
	* Records the HTML elements (and their bitfield) rendered at the root of the template
348
	*/
349
	protected function analyseRootNodes()
350
	{
351
		// Get every non-XSL element with no non-XSL ancestor. This should return us the first
352
		// HTML element of every branch
353
		$query = '//*[namespace-uri() != "http://www.w3.org/1999/XSL/Transform"]'
354
		       . '[not(ancestor::*[namespace-uri() != "http://www.w3.org/1999/XSL/Transform"])]';
355
356
		foreach ($this->xpath->query($query) as $node)
357
		{
358
			$elName = $node->localName;
359
360
			// Save the actual name of the root node
361
			$this->rootNodes[] = $elName;
362
363
			if (!isset(self::$htmlElements[$elName]))
364
			{
365
				// Unknown elements are treated as if they were a <span> element
366
				$elName = 'span';
367
			}
368
369
			// If any root node is a block-level element, we'll mark the template as such
370
			if ($this->elementIsBlock($elName, $node))
371
			{
372
				$this->isBlock = true;
373
			}
374
375
			$this->rootBitfields[] = $this->getBitfield($elName, 'c', $node);
376
		}
377
378
		// Test for non-whitespace text nodes at the root. For that we need a predicate that filters
379
		// out: nodes with a non-XSL ancestor,
380
		$predicate = '[not(ancestor::*[namespace-uri() != "http://www.w3.org/1999/XSL/Transform"])]';
381
382
		// ..and nodes with an <xsl:attribute/>, <xsl:comment/> or <xsl:variable/> ancestor
383
		$predicate .= '[not(ancestor::xsl:attribute | ancestor::xsl:comment | ancestor::xsl:variable)]';
384
385
		$query = '//text()[normalize-space() != ""]' . $predicate
386
		       . '|'
387
		       . '//xsl:text[normalize-space() != ""]' . $predicate
388
		       . '|'
389
		       . '//xsl:value-of' . $predicate;
390
391
		if ($this->evaluate($query, $this->dom->documentElement))
392
		{
393
			$this->hasRootText = true;
394
		}
395
	}
396
397
	/**
398
	* Analyses each branch that leads to an <xsl:apply-templates/> tag
399
	*/
400
	protected function analyseBranches()
401
	{
402
		/**
403
		* @var array allowChild bitfield for each branch
404
		*/
405
		$branchBitfields = [];
406
407
		/**
408
		* @var bool Whether this template should be considered a formatting element
409
		*/
410
		$isFormattingElement = true;
411
412
		// Consider this template transparent unless we find out there are no branches or that one
413
		// of the branches is not transparent
414
		$this->isTransparent = true;
415
416
		// For each <xsl:apply-templates/> element...
417
		foreach ($this->getXSLElements('apply-templates') as $applyTemplates)
418
		{
419
			// ...we retrieve all non-XSL ancestors
420
			$nodes = $this->xpath->query(
421
				'ancestor::*[namespace-uri() != "http://www.w3.org/1999/XSL/Transform"]',
422
				$applyTemplates
423
			);
424
425
			/**
426
			* @var bool Whether this branch allows elements
427
			*/
428
			$allowsChildElements = true;
429
430
			/**
431
			* @var bool Whether this branch allows text nodes
432
			*/
433
			$allowsText = true;
434
435
			/**
436
			* @var string allowChild bitfield for current branch. Starts with the value associated
437
			*             with <div> in order to approximate a value if the whole branch uses the
438
			*             transparent content model
439
			*/
440
			$branchBitfield = self::$htmlElements['div']['ac'];
441
442
			/**
443
			* @var bool Whether this branch denies all non-text descendants
444
			*/
445
			$isEmpty = false;
446
447
			/**
448
			* @var bool Whether this branch contains a void element
449
			*/
450
			$isVoid = false;
451
452
			/**
453
			* @var string Name of the last node of this branch
454
			*/
455
			$leafNode = null;
456
457
			/**
458
			* @var boolean Whether this branch preserves new lines
459
			*/
460
			$preservesNewLines = false;
461
462
			foreach ($nodes as $node)
463
			{
464
				$elName = $leafNode = $node->localName;
465
466
				if (!isset(self::$htmlElements[$elName]))
467
				{
468
					// Unknown elements are treated as if they were a <span> element
469
					$elName = 'span';
470
				}
471
472
				// Test whether the element is void
473
				if ($this->hasProperty($elName, 'v', $node))
474
				{
475
					$isVoid = true;
476
				}
477
478
				// Test whether the element uses the "empty" content model
479
				if ($this->hasProperty($elName, 'e', $node))
480
				{
481
					$isEmpty = true;
482
				}
483
484
				if (!$this->hasProperty($elName, 't', $node))
485
				{
486
					// If the element isn't transparent, we reset its bitfield
487
					$branchBitfield = "\0";
488
489
					// Also, it means that the template itself isn't transparent
490
					$this->isTransparent = false;
491
				}
492
493
				// Test whether this element is a formatting element
494
				if (!$this->hasProperty($elName, 'fe', $node)
495
				 && !$this->isFormattingSpan($node))
496
				{
497
					$isFormattingElement = false;
498
				}
499
500
				// Test whether this branch allows elements
501
				$allowsChildElements = !$this->hasProperty($elName, 'to', $node);
502
503
				// Test whether this branch allows text nodes
504
				$allowsText = !$this->hasProperty($elName, 'nt', $node);
505
506
				// allowChild rules are cumulative if transparent, and reset above otherwise
507
				$branchBitfield |= $this->getBitfield($elName, 'ac', $node);
508
509
				// denyDescendant rules are cumulative
510
				$this->denyDescendantBitfield |= $this->getBitfield($elName, 'dd', $node);
511
512
				// Test whether this branch preserves whitespace by inspecting the current element
513
				// and the value of its style attribute. Technically, this block of code also tests
514
				// this element's descendants' style attributes but the result is the same as we
515
				// need to check every element of this branch in order
516
				$style = '';
517
518
				if ($this->hasProperty($elName, 'pre', $node))
519
				{
520
					$style .= 'white-space:pre;';
521
				}
522
523
				if ($node->hasAttribute('style'))
524
				{
525
					$style .= $node->getAttribute('style') . ';';
526
				}
527
528
				$attributes = $this->xpath->query('.//xsl:attribute[@name="style"]', $node);
529
				foreach ($attributes as $attribute)
530
				{
531
					$style .= $attribute->textContent;
532
				}
533
534
				preg_match_all(
535
					'/white-space\\s*:\\s*(no|pre)/i',
536
					strtolower($style),
537
					$matches
538
				);
539
				foreach ($matches[1] as $match)
540
				{
541
					// TRUE:  "pre", "pre-line" and "pre-wrap"
542
					// FALSE: "normal", "nowrap"
543
					$preservesNewLines = ($match === 'pre');
544
				}
545
			}
546
547
			// Add this branch's bitfield to the list
548
			$branchBitfields[] = $branchBitfield;
549
550
			// Save the name of the last node processed
551
			if (isset($leafNode))
552
			{
553
				$this->leafNodes[] = $leafNode;
554
			}
555
556
			// If any branch disallows elements, the template disallows elements
557
			if (!$allowsChildElements)
558
			{
559
				$this->allowsChildElements = false;
560
			}
561
562
			// If any branch disallows text, the template disallows text
563
			if (!$allowsText)
564
			{
565
				$this->allowsText = false;
566
			}
567
568
			// If any branch is not empty, the template is not empty
569
			if (!$isEmpty)
570
			{
571
				$this->isEmpty = false;
572
			}
573
574
			// If any branch is not void, the template is not void
575
			if (!$isVoid)
576
			{
577
				$this->isVoid = false;
578
			}
579
580
			// If any branch preserves new lines, the template preserves new lines
581
			if ($preservesNewLines)
582
			{
583
				$this->preservesNewLines = true;
584
			}
585
		}
586
587
		if (empty($branchBitfields))
588
		{
589
			// No branches => not transparent
590
			$this->isTransparent = false;
591
		}
592
		else
593
		{
594
			// Take the bitfield of each branch and reduce them to a single ANDed bitfield
595
			$this->allowChildBitfield = $branchBitfields[0];
596
597
			foreach ($branchBitfields as $branchBitfield)
598
			{
599
				$this->allowChildBitfield &= $branchBitfield;
600
			}
601
602
			// Set the isFormattingElement property to our final value, but only if this template
603
			// had any branches
604
			if (!empty($this->leafNodes))
605
			{
606
				$this->isFormattingElement = $isFormattingElement;
607
			}
608
		}
609
	}
610
611
	/**
612
	* Test whether given element is a block-level element
613
	*
614
	* @param  string     $elName Element name
615
	* @param  DOMElement $node   Context node
616
	* @return bool
617
	*/
618
	protected function elementIsBlock($elName, DOMElement $node)
619
	{
620
		return $this->hasProperty($elName, 'b', $node);
621
	}
622
623
	/**
624
	* Evaluate a boolean XPath query
625
	*
626
	* @param  string     $query XPath query
627
	* @param  DOMElement $node  Context node
628
	* @return boolean
629
	*/
630
	protected function evaluate($query, DOMElement $node)
631
	{
632
		return $this->xpath->evaluate('boolean(' . $query . ')', $node);
633
	}
634
635
	/**
636
	* Get all XSL elements of given name
637
	*
638
	* @param  string      $elName XSL element's name, e.g. "apply-templates"
639
	* @return \DOMNodeList
640
	*/
641
	protected function getXSLElements($elName)
642
	{
643
		return $this->dom->getElementsByTagNameNS('http://www.w3.org/1999/XSL/Transform', $elName);
644
	}
645
646
	/**
647
	* Test whether given node is a span element used for formatting
648
	*
649
	* Will return TRUE if the node is a span element with a class attribute and/or a style attribute
650
	* and no other attributes
651
	*
652
	* @param  DOMElement $node
653
	* @return boolean
654
	*/
655
	protected function isFormattingSpan(DOMElement $node)
656
	{
657
		if ($node->nodeName !== 'span')
658
		{
659
			return false;
660
		}
661
662
		if ($node->getAttribute('class') === ''
663
		 && $node->getAttribute('style') === '')
664
		{
665
			return false;
666
		}
667
668
		foreach ($node->attributes as $attrName => $attribute)
669
		{
670
			if ($attrName !== 'class' && $attrName !== 'style')
671
			{
672
				return false;
673
			}
674
		}
675
676
		return true;
677
	}
678
679
	/**
680
	* "What is this?" you might ask. This is basically a compressed version of the HTML5 content
681
	* models and rules, with some liberties taken.
682
	*
683
	* For each element, up to three bitfields are defined: "c", "ac" and "dd". Bitfields are stored
684
	* as raw bytes, formatted using the octal notation to keep the sources ASCII.
685
	*
686
	*   "c" represents the categories the element belongs to. The categories are comprised of HTML5
687
	*   content models (such as "phrasing content" or "interactive content") plus a few special
688
	*   categories created to cover the parts of the specs that refer to "a group of X and Y
689
	*   elements" rather than a specific content model.
690
	*
691
	*   "ac" represents the categories that are allowed as children of given element.
692
	*
693
	*   "dd" represents the categories that must not appear as a descendant of given element.
694
	*
695
	* Sometimes, HTML5 specifies some restrictions on when an element can accept certain children,
696
	* or what categories the element belongs to. For example, an <img> element is only part of the
697
	* "interactive content" category if it has a "usemap" attribute. Those restrictions are
698
	* expressed as an XPath expression and stored using the concatenation of the key of the bitfield
699
	* plus the bit number of the category. For instance, if "interactive content" got assigned to
700
	* bit 2, the definition of the <img> element will contain a key "c2" with value "@usemap".
701
	*
702
	* Additionally, other flags are set:
703
	*
704
	*   "t" indicates that the element uses the "transparent" content model.
705
	*   "e" indicates that the element uses the "empty" content model.
706
	*   "v" indicates that the element is a void element.
707
	*   "nt" indicates that the element does not accept text nodes. (no text)
708
	*   "to" indicates that the element should only contain text. (text-only)
709
	*   "fe" indicates that the element is a formatting element. It will automatically be reopened
710
	*   when closed by an end tag of a different name.
711
	*   "b" indicates that the element is not phrasing content, which makes it likely to act like
712
	*   a block element.
713
	*
714
	* Finally, HTML5 defines "optional end tag" rules, where one element automatically closes its
715
	* predecessor. Those are used to generate closeParent rules and are stored in the "cp" key.
716
	*
717
	* @var array
718
	* @see /scripts/patchTemplateForensics.php
719
	*/
720
	protected static $htmlElements = [
721
		'a'=>['c'=>"\17",'ac'=>"\0",'dd'=>"\10",'t'=>1,'fe'=>1],
722
		'abbr'=>['c'=>"\7",'ac'=>"\4"],
723
		'address'=>['c'=>"\3\10",'ac'=>"\1",'dd'=>"\100\12",'b'=>1,'cp'=>['p']],
724
		'area'=>['c'=>"\5",'nt'=>1,'e'=>1,'v'=>1],
725
		'article'=>['c'=>"\3\2",'ac'=>"\1",'b'=>1,'cp'=>['p']],
726
		'aside'=>['c'=>"\3\2",'ac'=>"\1",'dd'=>"\0\0\0\200",'b'=>1,'cp'=>['p']],
727
		'audio'=>['c'=>"\57",'c3'=>'@controls','c1'=>'@controls','ac'=>"\0\0\200\4",'ac23'=>'not(@src)','ac26'=>'@src','t'=>1],
728
		'b'=>['c'=>"\7",'ac'=>"\4",'fe'=>1],
729
		'base'=>['c'=>"\20",'nt'=>1,'e'=>1,'v'=>1,'b'=>1],
730
		'bdi'=>['c'=>"\7",'ac'=>"\4"],
731
		'bdo'=>['c'=>"\7",'ac'=>"\4"],
732
		'blockquote'=>['c'=>"\3\1",'ac'=>"\1",'b'=>1,'cp'=>['p']],
733
		'body'=>['c'=>"\0\1\2",'ac'=>"\1",'b'=>1],
734
		'br'=>['c'=>"\5",'nt'=>1,'e'=>1,'v'=>1],
735
		'button'=>['c'=>"\17",'ac'=>"\4",'dd'=>"\10"],
736
		'canvas'=>['c'=>"\47",'ac'=>"\0",'t'=>1],
737
		'caption'=>['c'=>"\200",'ac'=>"\1",'dd'=>"\0\0\0\10",'b'=>1],
738
		'cite'=>['c'=>"\7",'ac'=>"\4"],
739
		'code'=>['c'=>"\7",'ac'=>"\4",'fe'=>1],
740
		'col'=>['c'=>"\0\0\4",'nt'=>1,'e'=>1,'v'=>1,'b'=>1],
741
		'colgroup'=>['c'=>"\200",'ac'=>"\0\0\4",'ac18'=>'not(@span)','nt'=>1,'e'=>1,'e0'=>'@span','b'=>1],
742
		'data'=>['c'=>"\7",'ac'=>"\4"],
743
		'datalist'=>['c'=>"\5",'ac'=>"\4\0\0\1"],
744
		'dd'=>['c'=>"\0\0\20",'ac'=>"\1",'b'=>1,'cp'=>['dd','dt']],
745
		'del'=>['c'=>"\5",'ac'=>"\0",'t'=>1],
746
		'dfn'=>['c'=>"\7\0\0\0\2",'ac'=>"\4",'dd'=>"\0\0\0\0\2"],
747
		'div'=>['c'=>"\3",'ac'=>"\1",'b'=>1,'cp'=>['p']],
748
		'dl'=>['c'=>"\3",'ac'=>"\0\40\20",'nt'=>1,'b'=>1,'cp'=>['p']],
749
		'dt'=>['c'=>"\0\0\20",'ac'=>"\1",'dd'=>"\100\2\1",'b'=>1,'cp'=>['dd','dt']],
750
		'em'=>['c'=>"\7",'ac'=>"\4",'fe'=>1],
751
		'embed'=>['c'=>"\57",'nt'=>1,'e'=>1,'v'=>1],
752
		'fieldset'=>['c'=>"\3\1",'ac'=>"\1\0\0\2",'b'=>1,'cp'=>['p']],
753
		'figcaption'=>['c'=>"\0\0\0\0\40",'ac'=>"\1",'b'=>1],
754
		'figure'=>['c'=>"\3\1",'ac'=>"\1\0\0\0\40",'b'=>1],
755
		'footer'=>['c'=>"\3\30\1",'ac'=>"\1",'dd'=>"\0\20",'b'=>1,'cp'=>['p']],
756
		'form'=>['c'=>"\3\0\0\0\1",'ac'=>"\1",'dd'=>"\0\0\0\0\1",'b'=>1,'cp'=>['p']],
757
		'h1'=>['c'=>"\103",'ac'=>"\4",'b'=>1,'cp'=>['p']],
758
		'h2'=>['c'=>"\103",'ac'=>"\4",'b'=>1,'cp'=>['p']],
759
		'h3'=>['c'=>"\103",'ac'=>"\4",'b'=>1,'cp'=>['p']],
760
		'h4'=>['c'=>"\103",'ac'=>"\4",'b'=>1,'cp'=>['p']],
761
		'h5'=>['c'=>"\103",'ac'=>"\4",'b'=>1,'cp'=>['p']],
762
		'h6'=>['c'=>"\103",'ac'=>"\4",'b'=>1,'cp'=>['p']],
763
		'head'=>['c'=>"\0\0\2",'ac'=>"\20",'nt'=>1,'b'=>1],
764
		'header'=>['c'=>"\3\30\1",'ac'=>"\1",'dd'=>"\0\20",'b'=>1,'cp'=>['p']],
765
		'hr'=>['c'=>"\1",'nt'=>1,'e'=>1,'v'=>1,'b'=>1,'cp'=>['p']],
766
		'html'=>['c'=>"\0",'ac'=>"\0\0\2",'nt'=>1,'b'=>1],
767
		'i'=>['c'=>"\7",'ac'=>"\4",'fe'=>1],
768
		'iframe'=>['c'=>"\57",'nt'=>1,'e'=>1,'to'=>1],
769
		'img'=>['c'=>"\57",'c3'=>'@usemap','nt'=>1,'e'=>1,'v'=>1],
770
		'input'=>['c'=>"\17",'c3'=>'@type!="hidden"','c1'=>'@type!="hidden"','nt'=>1,'e'=>1,'v'=>1],
771
		'ins'=>['c'=>"\7",'ac'=>"\0",'t'=>1],
772
		'kbd'=>['c'=>"\7",'ac'=>"\4"],
773
		'keygen'=>['c'=>"\17",'nt'=>1,'e'=>1,'v'=>1],
774
		'label'=>['c'=>"\17\0\0\100",'ac'=>"\4",'dd'=>"\0\0\0\100"],
775
		'legend'=>['c'=>"\0\0\0\2",'ac'=>"\4",'b'=>1],
776
		'li'=>['c'=>"\0\0\0\0\20",'ac'=>"\1",'b'=>1,'cp'=>['li']],
777
		'link'=>['c'=>"\20",'nt'=>1,'e'=>1,'v'=>1,'b'=>1],
778
		'main'=>['c'=>"\3\20\0\200",'ac'=>"\1",'b'=>1,'cp'=>['p']],
779
		'map'=>['c'=>"\7",'ac'=>"\0",'t'=>1],
780
		'mark'=>['c'=>"\7",'ac'=>"\4"],
781
		'meta'=>['c'=>"\20",'nt'=>1,'e'=>1,'v'=>1,'b'=>1],
782
		'meter'=>['c'=>"\7\100\0\40",'ac'=>"\4",'dd'=>"\0\0\0\40"],
783
		'nav'=>['c'=>"\3\2",'ac'=>"\1",'dd'=>"\0\0\0\200",'b'=>1,'cp'=>['p']],
784
		'noscript'=>['c'=>"\25\0\100",'ac'=>"\0",'dd'=>"\0\0\100",'t'=>1],
785
		'object'=>['c'=>"\57",'c3'=>'@usemap','ac'=>"\0\0\0\20",'t'=>1],
786
		'ol'=>['c'=>"\3",'ac'=>"\0\40\0\0\20",'nt'=>1,'b'=>1,'cp'=>['p']],
787
		'optgroup'=>['c'=>"\0\200",'ac'=>"\0\40\0\1",'nt'=>1,'b'=>1,'cp'=>['optgroup','option']],
788
		'option'=>['c'=>"\0\200\0\1",'e'=>1,'e0'=>'@label and @value','to'=>1,'b'=>1,'cp'=>['option']],
789
		'output'=>['c'=>"\7",'ac'=>"\4"],
790
		'p'=>['c'=>"\3",'ac'=>"\4",'b'=>1,'cp'=>['p']],
791
		'param'=>['c'=>"\0\0\0\20",'nt'=>1,'e'=>1,'v'=>1,'b'=>1],
792
		'pre'=>['c'=>"\3",'ac'=>"\4",'pre'=>1,'b'=>1,'cp'=>['p']],
793
		'progress'=>['c'=>"\7\100\40",'ac'=>"\4",'dd'=>"\0\0\40"],
794
		'q'=>['c'=>"\7",'ac'=>"\4"],
795
		'rb'=>['c'=>"\0\4",'ac'=>"\4",'b'=>1,'cp'=>['rb','rp','rt','rtc']],
796
		'rp'=>['c'=>"\0\4",'ac'=>"\4",'b'=>1,'cp'=>['rb','rp','rtc']],
797
		'rt'=>['c'=>"\0\4\0\0\10",'ac'=>"\4",'b'=>1,'cp'=>['rb','rp','rt']],
798
		'rtc'=>['c'=>"\0\4",'ac'=>"\4\0\0\0\10",'b'=>1,'cp'=>['rb','rp','rt','rtc']],
799
		'ruby'=>['c'=>"\7",'ac'=>"\4\4"],
800
		's'=>['c'=>"\7",'ac'=>"\4",'fe'=>1],
801
		'samp'=>['c'=>"\7",'ac'=>"\4"],
802
		'script'=>['c'=>"\25\40",'e'=>1,'e0'=>'@src','to'=>1],
803
		'section'=>['c'=>"\3\2",'ac'=>"\1",'b'=>1,'cp'=>['p']],
804
		'select'=>['c'=>"\17",'ac'=>"\0\240",'nt'=>1],
805
		'small'=>['c'=>"\7",'ac'=>"\4",'fe'=>1],
806
		'source'=>['c'=>"\0\0\200",'nt'=>1,'e'=>1,'v'=>1,'b'=>1],
807
		'span'=>['c'=>"\7",'ac'=>"\4"],
808
		'strong'=>['c'=>"\7",'ac'=>"\4",'fe'=>1],
809
		'style'=>['c'=>"\20",'to'=>1,'b'=>1],
810
		'sub'=>['c'=>"\7",'ac'=>"\4"],
811
		'sup'=>['c'=>"\7",'ac'=>"\4"],
812
		'table'=>['c'=>"\3\0\0\10",'ac'=>"\200\40",'nt'=>1,'b'=>1,'cp'=>['p']],
813
		'tbody'=>['c'=>"\200",'ac'=>"\0\40\0\0\4",'nt'=>1,'b'=>1,'cp'=>['tbody','tfoot','thead']],
814
		'td'=>['c'=>"\0\1\10",'ac'=>"\1",'b'=>1,'cp'=>['td','th']],
815
		'template'=>['c'=>"\25\40\4",'ac'=>"\21"],
816
		'textarea'=>['c'=>"\17",'pre'=>1],
817
		'tfoot'=>['c'=>"\200",'ac'=>"\0\40\0\0\4",'nt'=>1,'b'=>1,'cp'=>['tbody','thead']],
818
		'th'=>['c'=>"\0\0\10",'ac'=>"\1",'dd'=>"\100\2\1",'b'=>1,'cp'=>['td','th']],
819
		'thead'=>['c'=>"\200",'ac'=>"\0\40\0\0\4",'nt'=>1,'b'=>1],
820
		'time'=>['c'=>"\7",'ac'=>"\4"],
821
		'title'=>['c'=>"\20",'to'=>1,'b'=>1],
822
		'tr'=>['c'=>"\200\0\0\0\4",'ac'=>"\0\40\10",'nt'=>1,'b'=>1,'cp'=>['tr']],
823
		'track'=>['c'=>"\0\0\0\4",'nt'=>1,'e'=>1,'v'=>1,'b'=>1],
824
		'u'=>['c'=>"\7",'ac'=>"\4",'fe'=>1],
825
		'ul'=>['c'=>"\3",'ac'=>"\0\40\0\0\20",'nt'=>1,'b'=>1,'cp'=>['p']],
826
		'var'=>['c'=>"\7",'ac'=>"\4"],
827
		'video'=>['c'=>"\57",'c3'=>'@controls','ac'=>"\0\0\200\4",'ac23'=>'not(@src)','ac26'=>'@src','t'=>1],
828
		'wbr'=>['c'=>"\5",'nt'=>1,'e'=>1,'v'=>1]
829
	];
830
831
	/**
832
	* Get the bitfield value for a given element name in a given context
833
	*
834
	* @param  string     $elName Name of the HTML element
835
	* @param  string     $k      Bitfield name: either 'c', 'ac' or 'dd'
836
	* @param  DOMElement $node   Context node (not necessarily the same as $elName)
837
	* @return string
838
	*/
839
	protected function getBitfield($elName, $k, DOMElement $node)
840
	{
841
		if (!isset(self::$htmlElements[$elName][$k]))
842
		{
843
			return "\0";
844
		}
845
846
		$bitfield = self::$htmlElements[$elName][$k];
847
848
		foreach (str_split($bitfield, 1) as $byteNumber => $char)
849
		{
850
			$byteValue = ord($char);
851
852
			for ($bitNumber = 0; $bitNumber < 8; ++$bitNumber)
853
			{
854
				$bitValue = 1 << $bitNumber;
855
856
				if (!($byteValue & $bitValue))
857
				{
858
					// The bit is not set
859
					continue;
860
				}
861
862
				$n = $byteNumber * 8 + $bitNumber;
863
864
				// Test for an XPath condition for that category
865
				if (isset(self::$htmlElements[$elName][$k . $n]))
866
				{
867
					$xpath = self::$htmlElements[$elName][$k . $n];
868
869
					// If the XPath condition is not fulfilled...
870
					if (!$this->evaluate($xpath, $node))
871
					{
872
						// ...turn off the corresponding bit
873
						$byteValue ^= $bitValue;
874
875
						// Update the original bitfield
876
						$bitfield[$byteNumber] = chr($byteValue);
877
					}
878
				}
879
			}
880
		}
881
882
		return $bitfield;
883
	}
884
885
	/**
886
	* Test whether given element has given property in context
887
	*
888
	* @param  string     $elName   Element name
889
	* @param  string     $propName Property name, see self::$htmlElements
890
	* @param  DOMElement $node     Context node
891
	* @return bool
892
	*/
893
	protected function hasProperty($elName, $propName, DOMElement $node)
894
	{
895
		if (!empty(self::$htmlElements[$elName][$propName]))
896
		{
897
			// Test the XPath condition
898
			if (!isset(self::$htmlElements[$elName][$propName . '0'])
899
			 || $this->evaluate(self::$htmlElements[$elName][$propName . '0'], $node))
900
			{
901
				return true;
902
			}
903
		}
904
905
		return false;
906
	}
907
908
	/**
909
	* Test whether two bitfields have any bits in common
910
	*
911
	* @param  string $bitfield1
912
	* @param  string $bitfield2
913
	* @return bool
914
	*/
915
	protected static function match($bitfield1, $bitfield2)
916
	{
917
		return (trim($bitfield1 & $bitfield2, "\0") !== '');
918
	}
919
}