PluginProbe
Parse.ly / 3.19.1
Parse.ly v3.19.1
3.24.1 3.24.0 3.23.7 3.23.6 3.23.5 3.23.4 3.23.3 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.17.0 3.18.0 3.18.1 3.19.0 3.19.1 3.19.2 3.19.3 3.2.0 3.2.1 3.20.0 3.20.1 3.20.2 3.20.3 All 105 releases
wp-parsely / vendor / masterminds / html5 / src / HTML5 / Parser / TreeBuildingRules.php

TreeBuildingRules.php in Parse.ly 3.19.1, at vendor/masterminds/html5/src/HTML5/Parser/TreeBuildingRules.php

128 lines 3.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Masterminds\HTML5\Parser;
4
5 /**
6 * Handles special-case rules for the DOM tree builder.
7 *
8 * Many tags have special rules that need to be accomodated on an
9 * individual basis. This class handles those rules.
10 *
11 * See section 8.1.2.4 of the spec.
12 *
13 * @todo - colgroup and col special behaviors
14 * - body and head special behaviors
15 */
16 class TreeBuildingRules
17 {
18 protected static $tags = array(
19 'li' => 1,
20 'dd' => 1,
21 'dt' => 1,
22 'rt' => 1,
23 'rp' => 1,
24 'tr' => 1,
25 'th' => 1,
26 'td' => 1,
27 'thead' => 1,
28 'tfoot' => 1,
29 'tbody' => 1,
30 'table' => 1,
31 'optgroup' => 1,
32 'option' => 1,
33 );
34
35 /**
36 * Returns true if the given tagname has special processing rules.
37 */
38 public function hasRules($tagname)
39 {
40 return isset(static::$tags[$tagname]);
41 }
42
43 /**
44 * Evaluate the rule for the current tag name.
45 *
46 * This may modify the existing DOM.
47 *
48 * @return \DOMElement The new Current DOM element.
49 */
50 public function evaluate($new, $current)
51 {
52 switch ($new->tagName) {
53 case 'li':
54 return $this->handleLI($new, $current);
55 case 'dt':
56 case 'dd':
57 return $this->handleDT($new, $current);
58 case 'rt':
59 case 'rp':
60 return $this->handleRT($new, $current);
61 case 'optgroup':
62 return $this->closeIfCurrentMatches($new, $current, array(
63 'optgroup',
64 ));
65 case 'option':
66 return $this->closeIfCurrentMatches($new, $current, array(
67 'option',
68 ));
69 case 'tr':
70 return $this->closeIfCurrentMatches($new, $current, array(
71 'tr',
72 ));
73 case 'td':
74 case 'th':
75 return $this->closeIfCurrentMatches($new, $current, array(
76 'th',
77 'td',
78 ));
79 case 'tbody':
80 case 'thead':
81 case 'tfoot':
82 case 'table': // Spec isn't explicit about this, but it's necessary.
83
84 return $this->closeIfCurrentMatches($new, $current, array(
85 'thead',
86 'tfoot',
87 'tbody',
88 ));
89 }
90
91 return $current;
92 }
93
94 protected function handleLI($ele, $current)
95 {
96 return $this->closeIfCurrentMatches($ele, $current, array(
97 'li',
98 ));
99 }
100
101 protected function handleDT($ele, $current)
102 {
103 return $this->closeIfCurrentMatches($ele, $current, array(
104 'dt',
105 'dd',
106 ));
107 }
108
109 protected function handleRT($ele, $current)
110 {
111 return $this->closeIfCurrentMatches($ele, $current, array(
112 'rt',
113 'rp',
114 ));
115 }
116
117 protected function closeIfCurrentMatches($ele, $current, $match)
118 {
119 if (in_array($current->tagName, $match, true)) {
120 $current->parentNode->appendChild($ele);
121 } else {
122 $current->appendChild($ele);
123 }
124
125 return $ele;
126 }
127 }
128