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