| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RemexHtml\TreeBuilder; |
| 4 |
|
| 5 |
use Wikimedia\RemexHtml\HTMLData; |
| 6 |
|
| 7 |
/** |
| 8 |
* An implementation of the "stack of open elements" which includes a cache of |
| 9 |
* elements currently in the various kinds of scope. It presumably has good |
| 10 |
* worst-case performance at the expense of somewhat slower updates. |
| 11 |
*/ |
| 12 |
class CachingStack extends Stack { |
| 13 |
private const SCOPE_DEFAULT = 0; |
| 14 |
private const SCOPE_LIST = 1; |
| 15 |
private const SCOPE_BUTTON = 2; |
| 16 |
private const SCOPE_TABLE = 3; |
| 17 |
private const SCOPE_SELECT = 4; |
| 18 |
|
| 19 |
private const ALL_SCOPES = [ self::SCOPE_DEFAULT, self::SCOPE_LIST, self::SCOPE_BUTTON, |
| 20 |
self::SCOPE_TABLE, self::SCOPE_SELECT ]; |
| 21 |
private const NON_TABLE_SCOPES = [ self::SCOPE_DEFAULT, self::SCOPE_LIST, self::SCOPE_BUTTON, |
| 22 |
self::SCOPE_SELECT ]; |
| 23 |
private const LIST_SCOPES = [ self::SCOPE_LIST, self::SCOPE_SELECT ]; |
| 24 |
private const BUTTON_SCOPES = [ self::SCOPE_BUTTON, self::SCOPE_SELECT ]; |
| 25 |
private const SELECT_ONLY = [ self::SCOPE_SELECT ]; |
| 26 |
|
| 27 |
private const MATH_BREAKERS = [ |
| 28 |
'mi' => true, |
| 29 |
'mo' => true, |
| 30 |
'mn' => true, |
| 31 |
'ms' => true, |
| 32 |
'mtext' => true, |
| 33 |
'annotation-xml' => true |
| 34 |
]; |
| 35 |
|
| 36 |
private const SVG_BREAKERS = [ |
| 37 |
'foreignObject' => true, |
| 38 |
'desc' => true, |
| 39 |
'title' => true |
| 40 |
]; |
| 41 |
|
| 42 |
/** |
| 43 |
* If you compile every predicate of the form "an X element in Y scope" in |
| 44 |
* the HTML 5 spec, you discover that every element name X corresponds to |
| 45 |
* at most one such scope Y. This is useful because it means when we see |
| 46 |
* a new element, we need to add it to at most one scope cache. |
| 47 |
* |
| 48 |
* This is a list of such statements in the spec, and the scope they relate |
| 49 |
* to. All formatting elements are included as SCOPE_DEFAULT since the AAA |
| 50 |
* involves pulling an item out of the AFE list and checking if it is in |
| 51 |
* scope. |
| 52 |
*/ |
| 53 |
private const PREDICATE_MAP = [ |
| 54 |
'a' => self::SCOPE_DEFAULT, |
| 55 |
'address' => self::SCOPE_DEFAULT, |
| 56 |
'applet' => self::SCOPE_DEFAULT, |
| 57 |
'article' => self::SCOPE_DEFAULT, |
| 58 |
'aside' => self::SCOPE_DEFAULT, |
| 59 |
'b' => self::SCOPE_DEFAULT, |
| 60 |
'big' => self::SCOPE_DEFAULT, |
| 61 |
'blockquote' => self::SCOPE_DEFAULT, |
| 62 |
'body' => self::SCOPE_DEFAULT, |
| 63 |
'button' => self::SCOPE_DEFAULT, |
| 64 |
'caption' => self::SCOPE_TABLE, |
| 65 |
'center' => self::SCOPE_DEFAULT, |
| 66 |
'code' => self::SCOPE_DEFAULT, |
| 67 |
'dd' => self::SCOPE_DEFAULT, |
| 68 |
'details' => self::SCOPE_DEFAULT, |
| 69 |
'dialog' => self::SCOPE_DEFAULT, |
| 70 |
'dir' => self::SCOPE_DEFAULT, |
| 71 |
'div' => self::SCOPE_DEFAULT, |
| 72 |
'dl' => self::SCOPE_DEFAULT, |
| 73 |
'dt' => self::SCOPE_DEFAULT, |
| 74 |
'em' => self::SCOPE_DEFAULT, |
| 75 |
'fieldset' => self::SCOPE_DEFAULT, |
| 76 |
'figcaption' => self::SCOPE_DEFAULT, |
| 77 |
'figure' => self::SCOPE_DEFAULT, |
| 78 |
'font' => self::SCOPE_DEFAULT, |
| 79 |
'footer' => self::SCOPE_DEFAULT, |
| 80 |
'form' => self::SCOPE_DEFAULT, |
| 81 |
'h1' => self::SCOPE_DEFAULT, |
| 82 |
'h2' => self::SCOPE_DEFAULT, |
| 83 |
'h3' => self::SCOPE_DEFAULT, |
| 84 |
'h4' => self::SCOPE_DEFAULT, |
| 85 |
'h5' => self::SCOPE_DEFAULT, |
| 86 |
'h6' => self::SCOPE_DEFAULT, |
| 87 |
'header' => self::SCOPE_DEFAULT, |
| 88 |
'hgroup' => self::SCOPE_DEFAULT, |
| 89 |
'i' => self::SCOPE_DEFAULT, |
| 90 |
'li' => self::SCOPE_LIST, |
| 91 |
'listing' => self::SCOPE_DEFAULT, |
| 92 |
'main' => self::SCOPE_DEFAULT, |
| 93 |
'marquee' => self::SCOPE_DEFAULT, |
| 94 |
'menu' => self::SCOPE_DEFAULT, |
| 95 |
'nav' => self::SCOPE_DEFAULT, |
| 96 |
'nobr' => self::SCOPE_DEFAULT, |
| 97 |
'object' => self::SCOPE_DEFAULT, |
| 98 |
'ol' => self::SCOPE_DEFAULT, |
| 99 |
'p' => self::SCOPE_BUTTON, |
| 100 |
'pre' => self::SCOPE_DEFAULT, |
| 101 |
'ruby' => self::SCOPE_DEFAULT, |
| 102 |
's' => self::SCOPE_DEFAULT, |
| 103 |
'section' => self::SCOPE_DEFAULT, |
| 104 |
'select' => self::SCOPE_SELECT, |
| 105 |
'small' => self::SCOPE_DEFAULT, |
| 106 |
'strike' => self::SCOPE_DEFAULT, |
| 107 |
'strong' => self::SCOPE_DEFAULT, |
| 108 |
'summary' => self::SCOPE_DEFAULT, |
| 109 |
'table' => self::SCOPE_TABLE, |
| 110 |
'tbody' => self::SCOPE_TABLE, |
| 111 |
'td' => self::SCOPE_TABLE, |
| 112 |
'tfoot' => self::SCOPE_TABLE, |
| 113 |
'th' => self::SCOPE_TABLE, |
| 114 |
'thead' => self::SCOPE_TABLE, |
| 115 |
'tr' => self::SCOPE_TABLE, |
| 116 |
'tt' => self::SCOPE_DEFAULT, |
| 117 |
'u' => self::SCOPE_DEFAULT, |
| 118 |
'ul' => self::SCOPE_DEFAULT, |
| 119 |
]; |
| 120 |
|
| 121 |
/** |
| 122 |
* The stack of open elements |
| 123 |
* |
| 124 |
* @var array |
| 125 |
*/ |
| 126 |
private $elements = []; |
| 127 |
|
| 128 |
/** |
| 129 |
* A cache of the elements which are currently in a given scope. |
| 130 |
* The first key is the scope ID, the second key is the element name, and the |
| 131 |
* value is the first Element in a singly-linked list of Element objects, |
| 132 |
* linked by $element->nextEltInScope. |
| 133 |
* |
| 134 |
* @todo Benchmark time and memory compared to an array stack instead of an |
| 135 |
* SLL. The SLL here is maybe not quite so well justified as some other |
| 136 |
* SLLs in RemexHtml. |
| 137 |
* |
| 138 |
* @var array<int,array<string,Element>> |
| 139 |
*/ |
| 140 |
private $scopes = [ |
| 141 |
self::SCOPE_DEFAULT => [], |
| 142 |
self::SCOPE_LIST => [], |
| 143 |
self::SCOPE_BUTTON => [], |
| 144 |
self::SCOPE_TABLE => [], |
| 145 |
self::SCOPE_SELECT => [] |
| 146 |
]; |
| 147 |
|
| 148 |
/** |
| 149 |
* This is the part of the scope cache which stores scope lists for objects |
| 150 |
* which are not currently in scope. The first key is the scope ID, the |
| 151 |
* second key is the stack index, the third key is the element name, |
| 152 |
* and the value is the Element object. |
| 153 |
* |
| 154 |
* @var array<int,array<int,array<string,Element>>> |
| 155 |
*/ |
| 156 |
private $scopeStacks = [ |
| 157 |
self::SCOPE_DEFAULT => [], |
| 158 |
self::SCOPE_LIST => [], |
| 159 |
self::SCOPE_BUTTON => [], |
| 160 |
self::SCOPE_TABLE => [], |
| 161 |
self::SCOPE_SELECT => [] |
| 162 |
]; |
| 163 |
|
| 164 |
/** |
| 165 |
* The number of <template> elements in the stack of open elements. This |
| 166 |
* speeds up the hot function hasTemplate(). |
| 167 |
* |
| 168 |
* @var int |
| 169 |
*/ |
| 170 |
private $templateCount; |
| 171 |
|
| 172 |
/** |
| 173 |
* For a given namespace and element name, get the list of scopes |
| 174 |
* for which a new scope should be created and the old one needs to |
| 175 |
* be pushed onto the scope stack. |
| 176 |
* |
| 177 |
* @param string $ns |
| 178 |
* @param string $name |
| 179 |
* @return int[] |
| 180 |
*/ |
| 181 |
private function getScopeTypesToStack( $ns, $name ) { |
| 182 |
if ( $ns === HTMLData::NS_HTML ) { |
| 183 |
switch ( $name ) { |
| 184 |
case 'html': |
| 185 |
case 'table': |
| 186 |
case 'template': |
| 187 |
return self::ALL_SCOPES; |
| 188 |
|
| 189 |
case 'applet': |
| 190 |
case 'caption': |
| 191 |
case 'td': |
| 192 |
case 'th': |
| 193 |
case 'marquee': |
| 194 |
case 'object': |
| 195 |
return self::NON_TABLE_SCOPES; |
| 196 |
|
| 197 |
case 'ol': |
| 198 |
case 'ul': |
| 199 |
return self::LIST_SCOPES; |
| 200 |
|
| 201 |
case 'button': |
| 202 |
return self::BUTTON_SCOPES; |
| 203 |
|
| 204 |
case 'option': |
| 205 |
case 'optgroup': |
| 206 |
return []; |
| 207 |
|
| 208 |
default: |
| 209 |
return self::SELECT_ONLY; |
| 210 |
} |
| 211 |
} elseif ( $ns === HTMLData::NS_MATHML ) { |
| 212 |
if ( isset( self::MATH_BREAKERS[$name] ) ) { |
| 213 |
return self::NON_TABLE_SCOPES; |
| 214 |
} else { |
| 215 |
return self::SELECT_ONLY; |
| 216 |
} |
| 217 |
} elseif ( $ns === HTMLData::NS_SVG ) { |
| 218 |
if ( isset( self::SVG_BREAKERS[$name] ) ) { |
| 219 |
return self::NON_TABLE_SCOPES; |
| 220 |
} else { |
| 221 |
return self::SELECT_ONLY; |
| 222 |
} |
| 223 |
} else { |
| 224 |
return self::SELECT_ONLY; |
| 225 |
} |
| 226 |
} |
| 227 |
|
| 228 |
/** @inheritDoc */ |
| 229 |
public function push( Element $elt ) { |
| 230 |
// Update the stack store |
| 231 |
$n = count( $this->elements ); |
| 232 |
$this->elements[$n] = $elt; |
| 233 |
// Update the current node and index cache |
| 234 |
$this->current = $elt; |
| 235 |
$elt->stackIndex = $n; |
| 236 |
// Update the scope cache |
| 237 |
$ns = $elt->namespace; |
| 238 |
$name = $elt->name; |
| 239 |
foreach ( $this->getScopeTypesToStack( $ns, $name ) as $type ) { |
| 240 |
$this->scopeStacks[$type][] = $this->scopes[$type]; |
| 241 |
$this->scopes[$type] = []; |
| 242 |
} |
| 243 |
if ( $ns === HTMLData::NS_HTML && isset( self::PREDICATE_MAP[$name] ) ) { |
| 244 |
$type = self::PREDICATE_MAP[$name]; |
| 245 |
$scope =& $this->scopes[$type]; |
| 246 |
$elt->nextEltInScope = $scope[$name] ?? null; |
| 247 |
$scope[$name] = $elt; |
| 248 |
unset( $scope ); |
| 249 |
} |
| 250 |
// Update the template count |
| 251 |
if ( $ns === HTMLData::NS_HTML && $name === 'template' ) { |
| 252 |
$this->templateCount++; |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
/** @inheritDoc */ |
| 257 |
public function pop() { |
| 258 |
$n = count( $this->elements ); |
| 259 |
if ( !$n ) { |
| 260 |
throw new TreeBuilderError( __METHOD__ . ': stack empty' ); |
| 261 |
} |
| 262 |
// Update the stack store, index cache and current node |
| 263 |
$elt = array_pop( $this->elements ); |
| 264 |
$n--; |
| 265 |
$elt->stackIndex = null; |
| 266 |
$ns = $elt->namespace; |
| 267 |
$name = $elt->name; |
| 268 |
$this->current = $n ? $this->elements[$n - 1] : null; |
| 269 |
// Update the scope cache |
| 270 |
if ( $ns === HTMLData::NS_HTML && isset( self::PREDICATE_MAP[$name] ) ) { |
| 271 |
$scope = self::PREDICATE_MAP[$name]; |
| 272 |
$this->scopes[$scope][$name] = $elt->nextEltInScope; |
| 273 |
$elt->nextEltInScope = null; |
| 274 |
} |
| 275 |
foreach ( $this->getScopeTypesToStack( $ns, $name ) as $scope ) { |
| 276 |
$this->scopes[$scope] = array_pop( $this->scopeStacks[$scope] ); |
| 277 |
} |
| 278 |
// Update the template count |
| 279 |
if ( $ns === HTMLData::NS_HTML && $name === 'template' ) { |
| 280 |
$this->templateCount--; |
| 281 |
} |
| 282 |
return $elt; |
| 283 |
} |
| 284 |
|
| 285 |
/** @inheritDoc */ |
| 286 |
public function replace( Element $oldElt, Element $elt ) { |
| 287 |
$idx = $oldElt->stackIndex; |
| 288 |
// AAA calls this function only for elements with the same name, which |
| 289 |
// simplifies the scope cache update, and eliminates the template count |
| 290 |
// update |
| 291 |
if ( $oldElt->name !== $elt->name || $oldElt->namespace !== $elt->namespace ) { |
| 292 |
throw new TreeBuilderError( __METHOD__ . ' can only be called for elements of the same name' ); |
| 293 |
} |
| 294 |
$ns = $elt->namespace; |
| 295 |
$name = $elt->name; |
| 296 |
// Find the old element in its scope list and replace it |
| 297 |
if ( $ns === HTMLData::NS_HTML && isset( self::PREDICATE_MAP[$name] ) ) { |
| 298 |
$type = self::PREDICATE_MAP[$name]; |
| 299 |
$scopeElt = $this->scopes[$type][$name]; |
| 300 |
if ( $scopeElt === $oldElt ) { |
| 301 |
$this->scopes[$type][$name] = $elt; |
| 302 |
$elt->nextEltInScope = $scopeElt->nextEltInScope; |
| 303 |
$scopeElt->nextEltInScope = null; |
| 304 |
} else { |
| 305 |
$nextElt = $scopeElt->nextEltInScope; |
| 306 |
while ( $nextElt ) { |
| 307 |
if ( $nextElt === $oldElt ) { |
| 308 |
$scopeElt->nextEltInScope = $elt; |
| 309 |
$elt->nextEltInScope = $nextElt->nextEltInScope; |
| 310 |
$nextElt->nextEltInScope = null; |
| 311 |
break; |
| 312 |
} |
| 313 |
$scopeElt = $scopeElt->nextEltInScope; |
| 314 |
$nextElt = $scopeElt->nextEltInScope; |
| 315 |
} |
| 316 |
if ( !$nextElt ) { |
| 317 |
throw new TreeBuilderError( __METHOD__ . ': cannot find old element in scope cache' ); |
| 318 |
} |
| 319 |
} |
| 320 |
} |
| 321 |
// Replace the stack element |
| 322 |
$this->elements[$idx] = $elt; |
| 323 |
if ( $idx === count( $this->elements ) - 1 ) { |
| 324 |
$this->current = $elt; |
| 325 |
} |
| 326 |
$oldElt->stackIndex = null; |
| 327 |
$elt->stackIndex = $idx; |
| 328 |
} |
| 329 |
|
| 330 |
/** @inheritDoc */ |
| 331 |
public function remove( Element $elt ) { |
| 332 |
$tempStack = []; |
| 333 |
$eltIndex = $elt->stackIndex; |
| 334 |
if ( $eltIndex === null ) { |
| 335 |
throw new TreeBuilderError( __METHOD__ . ': element not in stack' ); |
| 336 |
} |
| 337 |
$n = count( $this->elements ); |
| 338 |
for ( $i = $n - 1; $i > $eltIndex; $i-- ) { |
| 339 |
$tempStack[] = $this->pop(); |
| 340 |
} |
| 341 |
$this->pop(); |
| 342 |
foreach ( array_reverse( $tempStack ) as $temp ) { |
| 343 |
$this->push( $temp ); |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
/** @inheritDoc */ |
| 348 |
public function isInScope( $name ) { |
| 349 |
if ( self::PREDICATE_MAP[$name] !== self::SCOPE_DEFAULT ) { |
| 350 |
throw new TreeBuilderError( "Unexpected predicate: \"$name is in scope\"" ); |
| 351 |
} |
| 352 |
return !empty( $this->scopes[self::SCOPE_DEFAULT][$name] ); |
| 353 |
} |
| 354 |
|
| 355 |
/** @inheritDoc */ |
| 356 |
public function isElementInScope( Element $elt ) { |
| 357 |
$name = $elt->name; |
| 358 |
if ( self::PREDICATE_MAP[$name] !== self::SCOPE_DEFAULT ) { |
| 359 |
throw new TreeBuilderError( "Unexpected predicate: \"$name is in scope\"" ); |
| 360 |
} |
| 361 |
if ( !empty( $this->scopes[self::SCOPE_DEFAULT][$name] ) ) { |
| 362 |
$scopeMember = $this->scopes[self::SCOPE_DEFAULT][$name]; |
| 363 |
while ( $scopeMember ) { |
| 364 |
if ( $scopeMember === $elt ) { |
| 365 |
return true; |
| 366 |
} |
| 367 |
$scopeMember = $scopeMember->nextEltInScope; |
| 368 |
} |
| 369 |
} |
| 370 |
return false; |
| 371 |
} |
| 372 |
|
| 373 |
/** @inheritDoc */ |
| 374 |
public function isOneOfSetInScope( $names ) { |
| 375 |
foreach ( $names as $name => $unused ) { |
| 376 |
if ( $this->isInScope( $name ) ) { |
| 377 |
return true; |
| 378 |
} |
| 379 |
} |
| 380 |
return false; |
| 381 |
} |
| 382 |
|
| 383 |
/** @inheritDoc */ |
| 384 |
public function isInListScope( $name ) { |
| 385 |
if ( self::PREDICATE_MAP[$name] !== self::SCOPE_LIST ) { |
| 386 |
throw new TreeBuilderError( "Unexpected predicate: \"$name is in list scope\"" ); |
| 387 |
} |
| 388 |
return !empty( $this->scopes[self::SCOPE_LIST][$name] ); |
| 389 |
} |
| 390 |
|
| 391 |
/** @inheritDoc */ |
| 392 |
public function isInButtonScope( $name ) { |
| 393 |
if ( self::PREDICATE_MAP[$name] !== self::SCOPE_BUTTON ) { |
| 394 |
throw new TreeBuilderError( "Unexpected predicate: \"$name is in button scope\"" ); |
| 395 |
} |
| 396 |
return !empty( $this->scopes[self::SCOPE_BUTTON][$name] ); |
| 397 |
} |
| 398 |
|
| 399 |
/** @inheritDoc */ |
| 400 |
public function isInTableScope( $name ) { |
| 401 |
if ( self::PREDICATE_MAP[$name] !== self::SCOPE_TABLE ) { |
| 402 |
throw new TreeBuilderError( "Unexpected predicate: \"$name is in table scope\"" ); |
| 403 |
} |
| 404 |
return !empty( $this->scopes[self::SCOPE_TABLE][$name] ); |
| 405 |
} |
| 406 |
|
| 407 |
/** @inheritDoc */ |
| 408 |
public function isInSelectScope( $name ) { |
| 409 |
if ( self::PREDICATE_MAP[$name] !== self::SCOPE_SELECT ) { |
| 410 |
throw new TreeBuilderError( "Unexpected predicate: \"$name is in select scope\"" ); |
| 411 |
} |
| 412 |
return !empty( $this->scopes[self::SCOPE_SELECT][$name] ); |
| 413 |
} |
| 414 |
|
| 415 |
/** @inheritDoc */ |
| 416 |
public function item( $idx ) { |
| 417 |
return $this->elements[$idx]; |
| 418 |
} |
| 419 |
|
| 420 |
/** @inheritDoc */ |
| 421 |
public function length() { |
| 422 |
return count( $this->elements ); |
| 423 |
} |
| 424 |
|
| 425 |
/** @inheritDoc */ |
| 426 |
public function hasTemplate() { |
| 427 |
return (bool)$this->templateCount; |
| 428 |
} |
| 429 |
|
| 430 |
/** @inheritDoc */ |
| 431 |
public function dump() { |
| 432 |
return parent::dump() . |
| 433 |
$this->scopeDump( self::SCOPE_DEFAULT, 'In scope' ) . |
| 434 |
$this->scopeDump( self::SCOPE_LIST, 'In list scope' ) . |
| 435 |
$this->scopeDump( self::SCOPE_BUTTON, 'In button scope' ) . |
| 436 |
$this->scopeDump( self::SCOPE_TABLE, 'In table scope' ) . |
| 437 |
$this->scopeDump( self::SCOPE_SELECT, 'In select scope' ) . "\n"; |
| 438 |
} |
| 439 |
|
| 440 |
private function scopeDump( int $type, string $scopeName ): string { |
| 441 |
if ( count( $this->scopes[$type] ) ) { |
| 442 |
return "$scopeName: " . implode( ', ', array_keys( $this->scopes[$type] ) ) . "\n"; |
| 443 |
} else { |
| 444 |
return ''; |
| 445 |
} |
| 446 |
} |
| 447 |
} |