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
|
103 |
|
public function hasRules($tagname) |
39
|
|
|
{ |
40
|
103 |
|
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
|
6 |
|
public function evaluate($new, $current) |
51
|
|
|
{ |
52
|
6 |
|
switch ($new->tagName) { |
53
|
6 |
|
case 'li': |
54
|
2 |
|
return $this->handleLI($new, $current); |
55
|
4 |
|
case 'dt': |
56
|
4 |
|
case 'dd': |
57
|
1 |
|
return $this->handleDT($new, $current); |
58
|
3 |
|
case 'rt': |
59
|
3 |
|
case 'rp': |
60
|
|
|
return $this->handleRT($new, $current); |
61
|
3 |
|
case 'optgroup': |
62
|
2 |
|
return $this->closeIfCurrentMatches($new, $current, array( |
63
|
2 |
|
'optgroup', |
64
|
2 |
|
)); |
65
|
3 |
|
case 'option': |
66
|
2 |
|
return $this->closeIfCurrentMatches($new, $current, array( |
67
|
2 |
|
'option', |
68
|
2 |
|
)); |
69
|
1 |
|
case 'tr': |
70
|
|
|
return $this->closeIfCurrentMatches($new, $current, array( |
71
|
|
|
'tr', |
72
|
|
|
)); |
73
|
1 |
|
case 'td': |
74
|
1 |
|
case 'th': |
75
|
1 |
|
return $this->closeIfCurrentMatches($new, $current, array( |
76
|
1 |
|
'th', |
77
|
1 |
|
'td', |
78
|
1 |
|
)); |
79
|
1 |
|
case 'tbody': |
80
|
1 |
|
case 'thead': |
81
|
1 |
|
case 'tfoot': |
82
|
1 |
|
case 'table': // Spec isn't explicit about this, but it's necessary. |
83
|
|
|
|
84
|
1 |
|
return $this->closeIfCurrentMatches($new, $current, array( |
85
|
1 |
|
'thead', |
86
|
1 |
|
'tfoot', |
87
|
1 |
|
'tbody', |
88
|
1 |
|
)); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
return $current; |
92
|
|
|
} |
93
|
|
|
|
94
|
2 |
|
protected function handleLI($ele, $current) |
95
|
|
|
{ |
96
|
2 |
|
return $this->closeIfCurrentMatches($ele, $current, array( |
97
|
2 |
|
'li', |
98
|
2 |
|
)); |
99
|
|
|
} |
100
|
|
|
|
101
|
1 |
|
protected function handleDT($ele, $current) |
102
|
|
|
{ |
103
|
1 |
|
return $this->closeIfCurrentMatches($ele, $current, array( |
104
|
1 |
|
'dt', |
105
|
1 |
|
'dd', |
106
|
1 |
|
)); |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
protected function handleRT($ele, $current) |
110
|
|
|
{ |
111
|
|
|
return $this->closeIfCurrentMatches($ele, $current, array( |
112
|
|
|
'rt', |
113
|
|
|
'rp', |
114
|
|
|
)); |
115
|
|
|
} |
116
|
|
|
|
117
|
6 |
|
protected function closeIfCurrentMatches($ele, $current, $match) |
118
|
|
|
{ |
119
|
6 |
|
if (in_array($current->tagName, $match, true)) { |
120
|
4 |
|
$current->parentNode->appendChild($ele); |
121
|
4 |
|
} else { |
122
|
6 |
|
$current->appendChild($ele); |
123
|
|
|
} |
124
|
|
|
|
125
|
6 |
|
return $ele; |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
|