Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 70 |
|
0.00% |
0 / 5 |
CRAP | |
0.00% |
0 / 1 |
| InForeignContent | |
0.00% |
0 / 70 |
|
0.00% |
0 / 5 |
650 | |
0.00% |
0 / 1 |
| characters | |
0.00% |
0 / 27 |
|
0.00% |
0 / 1 |
30 | |||
| isIntegrationPoint | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
12 | |||
| startTag | |
0.00% |
0 / 25 |
|
0.00% |
0 / 1 |
132 | |||
| endTag | |
0.00% |
0 / 14 |
|
0.00% |
0 / 1 |
30 | |||
| endDocument | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace Wikimedia\RemexHtml\TreeBuilder; |
| 5 | |
| 6 | use Wikimedia\RemexHtml\HTMLData; |
| 7 | use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 8 | |
| 9 | /** |
| 10 | * The rules for parsing tokens in foreign content. |
| 11 | * |
| 12 | * This is not referred to as an insertion mode in the spec, but is |
| 13 | * sufficiently similar to one that we can inherit from InsertionMode here. |
| 14 | */ |
| 15 | class InForeignContent extends InsertionMode { |
| 16 | /** |
| 17 | * The list of tag names which unconditionally generate a parse error when |
| 18 | * seen in foreign content. |
| 19 | */ |
| 20 | private const NOT_ALLOWED = [ |
| 21 | 'b' => true, |
| 22 | 'big' => true, |
| 23 | 'blockquote' => true, |
| 24 | 'body' => true, |
| 25 | 'br' => true, |
| 26 | 'center' => true, |
| 27 | 'code' => true, |
| 28 | 'dd' => true, |
| 29 | 'div' => true, |
| 30 | 'dl' => true, |
| 31 | 'dt' => true, |
| 32 | 'em' => true, |
| 33 | 'embed' => true, |
| 34 | 'h1' => true, |
| 35 | 'h2' => true, |
| 36 | 'h3' => true, |
| 37 | 'h4' => true, |
| 38 | 'h5' => true, |
| 39 | 'h6' => true, |
| 40 | 'head' => true, |
| 41 | 'hr' => true, |
| 42 | 'i' => true, |
| 43 | 'img' => true, |
| 44 | 'li' => true, |
| 45 | 'listing' => true, |
| 46 | 'menu' => true, |
| 47 | 'meta' => true, |
| 48 | 'nobr' => true, |
| 49 | 'ol' => true, |
| 50 | 'p' => true, |
| 51 | 'pre' => true, |
| 52 | 'ruby' => true, |
| 53 | 's' => true, |
| 54 | 'small' => true, |
| 55 | 'span' => true, |
| 56 | 'strong' => true, |
| 57 | 'strike' => true, |
| 58 | 'sub' => true, |
| 59 | 'sup' => true, |
| 60 | 'table' => true, |
| 61 | 'tt' => true, |
| 62 | 'u' => true, |
| 63 | 'ul' => true, |
| 64 | 'var' => true, |
| 65 | ]; |
| 66 | |
| 67 | /** |
| 68 | * The table for correcting the tag names of SVG elements, given in the |
| 69 | * "Any other start tag" section of the spec. |
| 70 | */ |
| 71 | private const SVG_ELEMENT_CASE = [ |
| 72 | 'altglyph' => 'altGlyph', |
| 73 | 'altglyphdef' => 'altGlyphDef', |
| 74 | 'altglyphitem' => 'altGlyphItem', |
| 75 | 'animatecolor' => 'animateColor', |
| 76 | 'animatemotion' => 'animateMotion', |
| 77 | 'animatetransform' => 'animateTransform', |
| 78 | 'clippath' => 'clipPath', |
| 79 | 'feblend' => 'feBlend', |
| 80 | 'fecolormatrix' => 'feColorMatrix', |
| 81 | 'fecomponenttransfer' => 'feComponentTransfer', |
| 82 | 'fecomposite' => 'feComposite', |
| 83 | 'feconvolvematrix' => 'feConvolveMatrix', |
| 84 | 'fediffuselighting' => 'feDiffuseLighting', |
| 85 | 'fedisplacementmap' => 'feDisplacementMap', |
| 86 | 'fedistantlight' => 'feDistantLight', |
| 87 | 'fedropshadow' => 'feDropShadow', |
| 88 | 'feflood' => 'feFlood', |
| 89 | 'fefunca' => 'feFuncA', |
| 90 | 'fefuncb' => 'feFuncB', |
| 91 | 'fefuncg' => 'feFuncG', |
| 92 | 'fefuncr' => 'feFuncR', |
| 93 | 'fegaussianblur' => 'feGaussianBlur', |
| 94 | 'feimage' => 'feImage', |
| 95 | 'femerge' => 'feMerge', |
| 96 | 'femergenode' => 'feMergeNode', |
| 97 | 'femorphology' => 'feMorphology', |
| 98 | 'feoffset' => 'feOffset', |
| 99 | 'fepointlight' => 'fePointLight', |
| 100 | 'fespecularlighting' => 'feSpecularLighting', |
| 101 | 'fespotlight' => 'feSpotLight', |
| 102 | 'fetile' => 'feTile', |
| 103 | 'feturbulence' => 'feTurbulence', |
| 104 | 'foreignobject' => 'foreignObject', |
| 105 | 'glyphref' => 'glyphRef', |
| 106 | 'lineargradient' => 'linearGradient', |
| 107 | 'radialgradient' => 'radialGradient', |
| 108 | 'textpath' => 'textPath', |
| 109 | ]; |
| 110 | |
| 111 | /** @inheritDoc */ |
| 112 | public function characters( $text, $start, $length, $sourceStart, $sourceLength ) { |
| 113 | $builder = $this->builder; |
| 114 | |
| 115 | while ( $length ) { |
| 116 | $normalLength = strcspn( $text, "\0\t\n\f\r ", $start, $length ); |
| 117 | if ( $normalLength ) { |
| 118 | $builder->framesetOK = false; |
| 119 | $builder->insertCharacters( $text, $start, $normalLength, |
| 120 | $sourceStart, $sourceLength ); |
| 121 | } |
| 122 | $start += $normalLength; |
| 123 | $length -= $normalLength; |
| 124 | $sourceStart += $normalLength; |
| 125 | $sourceLength -= $normalLength; |
| 126 | if ( !$length ) { |
| 127 | break; |
| 128 | } |
| 129 | |
| 130 | $char = $text[$start]; |
| 131 | if ( $char === "\0" ) { |
| 132 | $builder->error( "replaced null character", $sourceStart ); |
| 133 | $builder->insertCharacters( "\xef\xbf\xbd", 0, 3, $sourceStart, $sourceLength ); |
| 134 | $start++; |
| 135 | $length--; |
| 136 | $sourceStart++; |
| 137 | $sourceLength--; |
| 138 | } else { |
| 139 | // Whitespace |
| 140 | $wsLength = strspn( $text, "\t\n\f\r ", $start, $length ); |
| 141 | $builder->insertCharacters( $text, $start, $wsLength, $sourceStart, $wsLength ); |
| 142 | $start += $wsLength; |
| 143 | $length -= $wsLength; |
| 144 | $sourceStart += $wsLength; |
| 145 | $sourceLength -= $wsLength; |
| 146 | } |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | private function isIntegrationPoint( Element $element ): bool { |
| 151 | return $element->namespace === HTMLData::NS_HTML |
| 152 | || $element->isMathmlTextIntegration() |
| 153 | || $element->isHtmlIntegration(); |
| 154 | } |
| 155 | |
| 156 | /** @inheritDoc */ |
| 157 | public function startTag( $name, Attributes $attrs, $selfClose, $sourceStart, $sourceLength ) { |
| 158 | $builder = $this->builder; |
| 159 | $stack = $builder->stack; |
| 160 | $dispatcher = $this->dispatcher; |
| 161 | |
| 162 | if ( isset( self::NOT_ALLOWED[$name] ) ) { |
| 163 | $allowed = false; |
| 164 | } elseif ( $name === 'font' && ( |
| 165 | isset( $attrs['color'] ) || isset( $attrs['face'] ) || isset( $attrs['size'] ) ) |
| 166 | ) { |
| 167 | $allowed = false; |
| 168 | } else { |
| 169 | $allowed = true; |
| 170 | } |
| 171 | |
| 172 | if ( !$allowed ) { |
| 173 | $builder->error( "unexpected <$name> tag in foreign content", $sourceStart ); |
| 174 | if ( !$builder->isFragment ) { |
| 175 | do { |
| 176 | $builder->pop( $sourceStart, 0 ); |
| 177 | } while ( $stack->current && !$this->isIntegrationPoint( $stack->current ) ); |
| 178 | $dispatcher->startTag( $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 179 | return; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | $acnNs = $builder->adjustedCurrentNode()->namespace; |
| 184 | if ( $acnNs === HTMLData::NS_MATHML ) { |
| 185 | $attrs = new ForeignAttributes( $attrs, 'math' ); |
| 186 | } elseif ( $acnNs === HTMLData::NS_SVG ) { |
| 187 | $attrs = new ForeignAttributes( $attrs, 'svg' ); |
| 188 | $name = self::SVG_ELEMENT_CASE[$name] ?? $name; |
| 189 | } else { |
| 190 | $attrs = new ForeignAttributes( $attrs, 'other' ); |
| 191 | } |
| 192 | $dispatcher->ack = true; |
| 193 | $builder->insertForeign( $acnNs, $name, $attrs, $selfClose, $sourceStart, $sourceLength ); |
| 194 | } |
| 195 | |
| 196 | /** @inheritDoc */ |
| 197 | public function endTag( $name, $sourceStart, $sourceLength ) { |
| 198 | $builder = $this->builder; |
| 199 | $stack = $builder->stack; |
| 200 | $dispatcher = $this->dispatcher; |
| 201 | |
| 202 | $node = $stack->current; |
| 203 | if ( strcasecmp( $node->name, $name ) !== 0 ) { |
| 204 | $builder->error( "mismatched end tag in foreign content", $sourceStart ); |
| 205 | } |
| 206 | for ( $idx = $stack->length() - 1; $idx > 0; $idx-- ) { |
| 207 | if ( strcasecmp( $node->name, $name ) === 0 ) { |
| 208 | $builder->popAllUpToElement( $node, $sourceStart, $sourceLength ); |
| 209 | break; |
| 210 | } |
| 211 | $node = $stack->item( $idx - 1 ); |
| 212 | if ( $node->namespace === HTMLData::NS_HTML ) { |
| 213 | $dispatcher->getHandler()->endTag( $name, $sourceStart, $sourceLength ); |
| 214 | break; |
| 215 | } |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /** @inheritDoc */ |
| 220 | public function endDocument( $pos ) { |
| 221 | // @phan-suppress-previous-line PhanPluginNeverReturnMethod |
| 222 | throw new TreeBuilderError( "unspecified, presumed unreachable" ); |
| 223 | } |
| 224 | } |