| 1 |
<?php |
| 2 |
|
| 3 |
namespace WindPressPackages\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('li' => 1, 'dd' => 1, 'dt' => 1, 'rt' => 1, 'rp' => 1, 'tr' => 1, 'th' => 1, 'td' => 1, 'thead' => 1, 'tfoot' => 1, 'tbody' => 1, 'table' => 1, 'optgroup' => 1, 'option' => 1); |
| 19 |
/** |
| 20 |
* Returns true if the given tagname has special processing rules. |
| 21 |
*/ |
| 22 |
public function hasRules($tagname) |
| 23 |
{ |
| 24 |
return isset(static::$tags[$tagname]); |
| 25 |
} |
| 26 |
/** |
| 27 |
* Evaluate the rule for the current tag name. |
| 28 |
* |
| 29 |
* This may modify the existing DOM. |
| 30 |
* |
| 31 |
* @return \DOMElement The new Current DOM element. |
| 32 |
*/ |
| 33 |
public function evaluate($new, $current) |
| 34 |
{ |
| 35 |
switch ($new->tagName) { |
| 36 |
case 'li': |
| 37 |
return $this->handleLI($new, $current); |
| 38 |
case 'dt': |
| 39 |
case 'dd': |
| 40 |
return $this->handleDT($new, $current); |
| 41 |
case 'rt': |
| 42 |
case 'rp': |
| 43 |
return $this->handleRT($new, $current); |
| 44 |
case 'optgroup': |
| 45 |
return $this->closeIfCurrentMatches($new, $current, array('optgroup')); |
| 46 |
case 'option': |
| 47 |
return $this->closeIfCurrentMatches($new, $current, array('option')); |
| 48 |
case 'tr': |
| 49 |
return $this->closeIfCurrentMatches($new, $current, array('tr')); |
| 50 |
case 'td': |
| 51 |
case 'th': |
| 52 |
return $this->closeIfCurrentMatches($new, $current, array('th', 'td')); |
| 53 |
case 'tbody': |
| 54 |
case 'thead': |
| 55 |
case 'tfoot': |
| 56 |
case 'table': |
| 57 |
// Spec isn't explicit about this, but it's necessary. |
| 58 |
return $this->closeIfCurrentMatches($new, $current, array('thead', 'tfoot', 'tbody')); |
| 59 |
} |
| 60 |
return $current; |
| 61 |
} |
| 62 |
protected function handleLI($ele, $current) |
| 63 |
{ |
| 64 |
return $this->closeIfCurrentMatches($ele, $current, array('li')); |
| 65 |
} |
| 66 |
protected function handleDT($ele, $current) |
| 67 |
{ |
| 68 |
return $this->closeIfCurrentMatches($ele, $current, array('dt', 'dd')); |
| 69 |
} |
| 70 |
protected function handleRT($ele, $current) |
| 71 |
{ |
| 72 |
return $this->closeIfCurrentMatches($ele, $current, array('rt', 'rp')); |
| 73 |
} |
| 74 |
protected function closeIfCurrentMatches($ele, $current, $match) |
| 75 |
{ |
| 76 |
if (\in_array($current->tagName, $match, \true)) { |
| 77 |
$current->parentNode->appendChild($ele); |
| 78 |
} else { |
| 79 |
$current->appendChild($ele); |
| 80 |
} |
| 81 |
return $ele; |
| 82 |
} |
| 83 |
} |
| 84 |
|