| 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 |
|