2 namespace Masterminds\HTML5\Parser;
5 * Handles special-case rules for the DOM tree builder.
7 * Many tags have special rules that need to be accomodated on an
8 * individual basis. This class handles those rules.
10 * See section 8.1.2.4 of the spec.
12 * @todo - colgroup and col special behaviors
13 * - body and head special behaviors
15 class TreeBuildingRules
18 protected static $tags = array(
36 * Build a new rules engine.
38 * @param \DOMDocument $doc
39 * The DOM document to use for evaluation and modification.
41 public function __construct($doc)
47 * Returns true if the given tagname has special processing rules.
49 public function hasRules($tagname)
51 return isset(static::$tags[$tagname]);
55 * Evaluate the rule for the current tag name.
57 * This may modify the existing DOM.
59 * @return \DOMElement The new Current DOM element.
61 public function evaluate($new, $current)
63 switch ($new->tagName) {
65 return $this->handleLI($new, $current);
68 return $this->handleDT($new, $current);
71 return $this->handleRT($new, $current);
73 return $this->closeIfCurrentMatches($new, $current, array(
77 return $this->closeIfCurrentMatches($new, $current, array(
81 return $this->closeIfCurrentMatches($new, $current, array(
86 return $this->closeIfCurrentMatches($new, $current, array(
93 case 'table': // Spec isn't explicit about this, but it's necessary.
95 return $this->closeIfCurrentMatches($new, $current, array(
105 protected function handleLI($ele, $current)
107 return $this->closeIfCurrentMatches($ele, $current, array(
112 protected function handleDT($ele, $current)
114 return $this->closeIfCurrentMatches($ele, $current, array(
120 protected function handleRT($ele, $current)
122 return $this->closeIfCurrentMatches($ele, $current, array(
128 protected function closeIfCurrentMatches($ele, $current, $match)
130 $tname = $current->tagName;
131 if (in_array($current->tagName, $match)) {
132 $current->parentNode->appendChild($ele);
134 $current->appendChild($ele);