CharacterReference.php
6 months ago
DOMTreeBuilder.php
6 months ago
EventHandler.php
6 months ago
FileInputStream.php
6 months ago
InputStream.php
6 months ago
ParseError.php
6 months ago
README.md
1 year ago
Scanner.php
6 months ago
StringInputStream.php
6 months ago
Tokenizer.php
6 months ago
TreeBuildingRules.php
6 months ago
UTF8Utils.php
6 months ago
TreeBuildingRules.php
127 lines
| 1 | <?php |
| 2 | |
| 3 | namespace AmeliaVendor\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 | return $this->closeIfCurrentMatches($new, $current, array( |
| 84 | 'thead', |
| 85 | 'tfoot', |
| 86 | 'tbody', |
| 87 | )); |
| 88 | } |
| 89 | |
| 90 | return $current; |
| 91 | } |
| 92 | |
| 93 | protected function handleLI($ele, $current) |
| 94 | { |
| 95 | return $this->closeIfCurrentMatches($ele, $current, array( |
| 96 | 'li', |
| 97 | )); |
| 98 | } |
| 99 | |
| 100 | protected function handleDT($ele, $current) |
| 101 | { |
| 102 | return $this->closeIfCurrentMatches($ele, $current, array( |
| 103 | 'dt', |
| 104 | 'dd', |
| 105 | )); |
| 106 | } |
| 107 | |
| 108 | protected function handleRT($ele, $current) |
| 109 | { |
| 110 | return $this->closeIfCurrentMatches($ele, $current, array( |
| 111 | 'rt', |
| 112 | 'rp', |
| 113 | )); |
| 114 | } |
| 115 | |
| 116 | protected function closeIfCurrentMatches($ele, $current, $match) |
| 117 | { |
| 118 | if (in_array($current->tagName, $match, true)) { |
| 119 | $current->parentNode->appendChild($ele); |
| 120 | } else { |
| 121 | $current->appendChild($ele); |
| 122 | } |
| 123 | |
| 124 | return $ele; |
| 125 | } |
| 126 | } |
| 127 |