| 1 |
<?php |
| 2 |
|
| 3 |
namespace Wikimedia\RemexHtml\DOM; |
| 4 |
|
| 5 |
use Wikimedia\RemexHtml\HTMLData; |
| 6 |
use Wikimedia\RemexHtml\Tokenizer\Attributes; |
| 7 |
use Wikimedia\RemexHtml\TreeBuilder\Element; |
| 8 |
use Wikimedia\RemexHtml\TreeBuilder\TreeBuilder; |
| 9 |
use Wikimedia\RemexHtml\TreeBuilder\TreeHandler; |
| 10 |
|
| 11 |
/** |
| 12 |
* A TreeHandler which constructs a DOMDocument. |
| 13 |
* |
| 14 |
* Note that this class permits third-party `DOMImplementation`s |
| 15 |
* (documents other than `\DOMDocument`, nodes other than `\DOMNode`, |
| 16 |
* etc) and so no enforced PHP type hints are used which name these |
| 17 |
* classes directly. For the sake of static type checking, the |
| 18 |
* types *in comments* are given as if the standard PHP `\DOM*` |
| 19 |
* classes are being used but at runtime everything is duck-typed. |
| 20 |
*/ |
| 21 |
class DOMBuilder implements TreeHandler { |
| 22 |
|
| 23 |
/** @var string|null The name of the input document type */ |
| 24 |
public $doctypeName; |
| 25 |
|
| 26 |
/** @var string|null The public ID */ |
| 27 |
public $public; |
| 28 |
|
| 29 |
/** @var string|null The system ID */ |
| 30 |
public $system; |
| 31 |
|
| 32 |
/** |
| 33 |
* @var int The quirks mode. May be either TreeBuilder::NO_QUIRKS, |
| 34 |
* TreeBuilder::LIMITED_QUIRKS or TreeBuilder::QUIRKS to indicate |
| 35 |
* no-quirks mode, limited-quirks mode or quirks mode respectively. |
| 36 |
*/ |
| 37 |
public $quirks; |
| 38 |
|
| 39 |
/** @var \DOMDocument */ |
| 40 |
private $doc; |
| 41 |
|
| 42 |
/** @var callable|null */ |
| 43 |
private $errorCallback; |
| 44 |
|
| 45 |
/** @var bool */ |
| 46 |
private $suppressHtmlNamespace; |
| 47 |
|
| 48 |
/** @var bool */ |
| 49 |
private $suppressIdAttribute; |
| 50 |
|
| 51 |
/** @var \DOMImplementation */ |
| 52 |
private $domImplementation; |
| 53 |
|
| 54 |
/** @var class-string */ |
| 55 |
private $domExceptionClass; |
| 56 |
|
| 57 |
/** @var bool */ |
| 58 |
private $isFragment; |
| 59 |
|
| 60 |
/** @var bool */ |
| 61 |
private $coerced = false; |
| 62 |
|
| 63 |
/** |
| 64 |
* @param array $options An associative array of options: |
| 65 |
* - errorCallback : A function which is called on parse errors |
| 66 |
* - suppressHtmlNamespace : omit the namespace when creating HTML |
| 67 |
* elements. False by default. |
| 68 |
* - suppressIdAttribute : don't call the nonstandard |
| 69 |
* DOMElement::setIdAttribute() method while constructing elements. |
| 70 |
* False by default (this method is needed for efficient |
| 71 |
* DOMDocument::getElementById() calls). Set to true if you are |
| 72 |
* using a W3C spec-compliant DOMImplementation and wish to avoid |
| 73 |
* nonstandard calls. |
| 74 |
* - domImplementation: The DOMImplementation object to use. If this |
| 75 |
* parameter is missing or null, a new DOMImplementation object will |
| 76 |
* be constructed using the `domImplementationClass` option value. |
| 77 |
* You can use a third-party DOM implementation by passing in an |
| 78 |
* appropriately duck-typed object here. |
| 79 |
* - domImplementationClass: The string name of the DOMImplementation |
| 80 |
* class to use. Defaults to `\DOMImplementation::class` but |
| 81 |
* you can use a third-party DOM implementation by passing |
| 82 |
* an alternative class name here. |
| 83 |
* - domExceptionClass: The string name of the DOMException |
| 84 |
* class to use. Defaults to `\DOMException::class` but |
| 85 |
* you can use a third-party DOM implementation by passing |
| 86 |
* an alternative class name here. |
| 87 |
*/ |
| 88 |
public function __construct( $options = [] ) { |
| 89 |
$options += [ |
| 90 |
'suppressHtmlNamespace' => false, |
| 91 |
'suppressIdAttribute' => false, |
| 92 |
'errorCallback' => null, |
| 93 |
'domImplementation' => null, |
| 94 |
'domImplementationClass' => \DOMImplementation::class, |
| 95 |
'domExceptionClass' => \DOMException::class, |
| 96 |
]; |
| 97 |
$this->errorCallback = $options['errorCallback']; |
| 98 |
$this->suppressHtmlNamespace = $options['suppressHtmlNamespace']; |
| 99 |
$this->suppressIdAttribute = $options['suppressIdAttribute']; |
| 100 |
$this->domImplementation = $options['domImplementation'] ?? |
| 101 |
new $options['domImplementationClass']; |
| 102 |
$this->domExceptionClass = $options['domExceptionClass']; |
| 103 |
} |
| 104 |
|
| 105 |
private function rethrowIfNotDomException( \Throwable $t ) { |
| 106 |
if ( is_a( $t, $this->domExceptionClass, false ) ) { |
| 107 |
return; |
| 108 |
} |
| 109 |
throw $t; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Get the constructed document or document fragment. In the fragment case, |
| 114 |
* a DOMElement is returned, and the caller is expected to extract its |
| 115 |
* inner contents, ignoring the wrapping element. This convention is |
| 116 |
* convenient because the wrapping element gives libxml somewhere to put |
| 117 |
* its namespace declarations. If we copied the children into a |
| 118 |
* DOMDocumentFragment, libxml would invent new prefixes for the orphaned |
| 119 |
* namespaces. |
| 120 |
* |
| 121 |
* @return \DOMNode |
| 122 |
*/ |
| 123 |
public function getFragment() { |
| 124 |
if ( $this->isFragment ) { |
| 125 |
return $this->doc->documentElement; |
| 126 |
} else { |
| 127 |
return $this->doc; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
/** |
| 132 |
* Returns true if the document was coerced due to libxml limitations. We |
| 133 |
* follow HTML 5.1 ยง 8.2.7 "Coercing an HTML DOM into an infoset". |
| 134 |
* |
| 135 |
* @return bool |
| 136 |
*/ |
| 137 |
public function isCoerced() { |
| 138 |
return $this->coerced; |
| 139 |
} |
| 140 |
|
| 141 |
/** @inheritDoc */ |
| 142 |
public function startDocument( $fragmentNamespace, $fragmentName ) { |
| 143 |
$this->isFragment = $fragmentNamespace !== null; |
| 144 |
$this->doc = $this->createDocument(); |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* @param string|null $doctypeName |
| 149 |
* @param string|null $public |
| 150 |
* @param string|null $system |
| 151 |
* @return \DOMDocument |
| 152 |
* @suppress PhanTypeMismatchArgumentInternalReal |
| 153 |
* Null args to DOMImplementation::createDocument |
| 154 |
*/ |
| 155 |
protected function createDocument( |
| 156 |
?string $doctypeName = null, |
| 157 |
?string $public = null, |
| 158 |
?string $system = null |
| 159 |
) { |
| 160 |
$impl = $this->domImplementation; |
| 161 |
if ( $doctypeName === '' ) { |
| 162 |
$this->coerced = true; |
| 163 |
$doc = $impl->createDocument( null, '' ); |
| 164 |
} elseif ( $doctypeName === null ) { |
| 165 |
$doc = $impl->createDocument( null, '' ); |
| 166 |
} else { |
| 167 |
$doctype = $impl->createDocumentType( $doctypeName, $public, $system ); |
| 168 |
$doc = $impl->createDocument( null, '', $doctype ); |
| 169 |
} |
| 170 |
$doc->encoding = 'UTF-8'; |
| 171 |
return $doc; |
| 172 |
} |
| 173 |
|
| 174 |
/** @inheritDoc */ |
| 175 |
public function endDocument( $pos ) { |
| 176 |
} |
| 177 |
|
| 178 |
/** |
| 179 |
* @param int $preposition |
| 180 |
* @param Element $refElement |
| 181 |
* @param \DOMNode $node |
| 182 |
*/ |
| 183 |
protected function insertNode( $preposition, $refElement, $node ) { |
| 184 |
if ( $preposition === TreeBuilder::ROOT ) { |
| 185 |
$parent = $this->doc; |
| 186 |
$refNode = null; |
| 187 |
} elseif ( $preposition === TreeBuilder::BEFORE ) { |
| 188 |
$parent = $refElement->userData->parentNode; |
| 189 |
$refNode = $refElement->userData; |
| 190 |
} else { |
| 191 |
$parent = $refElement->userData; |
| 192 |
$refNode = null; |
| 193 |
} |
| 194 |
$parent->insertBefore( $node, $refNode ); |
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Replace unsupported characters with a code of the form U123456. |
| 199 |
* |
| 200 |
* @param string $name |
| 201 |
* @return string |
| 202 |
*/ |
| 203 |
private function coerceName( $name ) { |
| 204 |
$coercedName = DOMUtils::coerceName( $name ); |
| 205 |
if ( $name !== $coercedName ) { |
| 206 |
$this->coerced = true; |
| 207 |
} |
| 208 |
return $coercedName; |
| 209 |
} |
| 210 |
|
| 211 |
/** |
| 212 |
* @return \DOMNode |
| 213 |
*/ |
| 214 |
protected function createNode( Element $element ) { |
| 215 |
$noNS = $this->suppressHtmlNamespace && $element->namespace === HTMLData::NS_HTML; |
| 216 |
try { |
| 217 |
if ( $noNS ) { |
| 218 |
$node = $this->doc->createElement( $element->name ); |
| 219 |
} else { |
| 220 |
$node = $this->doc->createElementNS( |
| 221 |
$element->namespace, |
| 222 |
$element->name ); |
| 223 |
} |
| 224 |
} catch ( \Throwable $e ) { |
| 225 |
$this->rethrowIfNotDomException( $e ); |
| 226 |
'@phan-var \DOMException $e'; /** @var \DOMException $e */ |
| 227 |
// Attempt to escape the name so that it is more acceptable |
| 228 |
if ( $noNS ) { |
| 229 |
$node = $this->doc->createElement( |
| 230 |
$this->coerceName( $element->name ) |
| 231 |
); |
| 232 |
} else { |
| 233 |
$node = $this->doc->createElementNS( |
| 234 |
$element->namespace, |
| 235 |
$this->coerceName( $element->name ) ); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
foreach ( $element->attrs->getObjects() as $attr ) { |
| 240 |
if ( $attr->namespaceURI === null |
| 241 |
&& strpos( $attr->localName, ':' ) !== false |
| 242 |
) { |
| 243 |
// Create a DOMText explicitly instead of setting $attrNode->value, |
| 244 |
// to work around the DOMAttr entity expansion bug (T324408) |
| 245 |
$textNode = new \DOMText( $attr->value ); |
| 246 |
try { |
| 247 |
// FIXME: this apparently works to create a prefixed localName |
| 248 |
// in the null namespace, but this is probably taking advantage |
| 249 |
// of a bug in PHP's DOM library, and screws up in various |
| 250 |
// interesting ways. For example, attributes created in this |
| 251 |
// way can't be discovered via hasAttribute() or hasAttributeNS(). |
| 252 |
$attrNode = $this->doc->createAttribute( $attr->localName ); |
| 253 |
$attrNode->appendChild( $textNode ); |
| 254 |
$node->setAttributeNodeNS( $attrNode ); |
| 255 |
} catch ( \Throwable $e ) { |
| 256 |
$this->rethrowIfNotDomException( $e ); |
| 257 |
'@phan-var \DOMException $e'; /** @var \DOMException $e */ |
| 258 |
$attrNode = $this->doc->createAttribute( |
| 259 |
$this->coerceName( $attr->localName ) ); |
| 260 |
$attrNode->appendChild( $textNode ); |
| 261 |
$node->setAttributeNodeNS( $attrNode ); |
| 262 |
} |
| 263 |
} else { |
| 264 |
try { |
| 265 |
$node->setAttributeNS( |
| 266 |
$attr->namespaceURI, |
| 267 |
$attr->qualifiedName, |
| 268 |
$attr->value ); |
| 269 |
} catch ( \Throwable $e ) { |
| 270 |
$this->rethrowIfNotDomException( $e ); |
| 271 |
'@phan-var \DOMException $e'; /** @var \DOMException $e */ |
| 272 |
$node->setAttributeNS( |
| 273 |
$attr->namespaceURI, |
| 274 |
$this->coerceName( $attr->qualifiedName ), |
| 275 |
$attr->value ); |
| 276 |
} |
| 277 |
} |
| 278 |
} |
| 279 |
if ( ( !$this->suppressIdAttribute ) && $node->hasAttribute( 'id' ) ) { |
| 280 |
// This is a call to a non-standard DOM method required by PHP in |
| 281 |
// order to implement DOMDocument::getElementById() efficiently. |
| 282 |
$node->setIdAttribute( 'id', true ); |
| 283 |
} |
| 284 |
$element->userData = $node; |
| 285 |
return $node; |
| 286 |
} |
| 287 |
|
| 288 |
/** @inheritDoc */ |
| 289 |
public function characters( $preposition, $refElement, $text, $start, $length, |
| 290 |
$sourceStart, $sourceLength |
| 291 |
) { |
| 292 |
// Parse $preposition and $refElement as in self::insertNode() |
| 293 |
if ( $preposition === TreeBuilder::ROOT ) { |
| 294 |
$parent = $this->doc; |
| 295 |
$refNode = null; |
| 296 |
} elseif ( $preposition === TreeBuilder::BEFORE ) { |
| 297 |
$parent = $refElement->userData->parentNode; |
| 298 |
$refNode = $refElement->userData; |
| 299 |
} else { |
| 300 |
$parent = $refElement->userData; |
| 301 |
$refNode = null; |
| 302 |
} |
| 303 |
// https://html.spec.whatwg.org/#insert-a-character |
| 304 |
// If the adjusted insertion location is in a Document node, then |
| 305 |
// return. |
| 306 |
if ( $parent === $this->doc ) { |
| 307 |
return; |
| 308 |
} |
| 309 |
$data = substr( $text, $start, $length ); |
| 310 |
// If there is a Text node immediately before the adjusted insertion |
| 311 |
// location, then append data to that Text node's data. |
| 312 |
if ( $refNode === null ) { |
| 313 |
$prev = $parent->lastChild; |
| 314 |
} else { |
| 315 |
/** @var \DOMNode $refNode */ |
| 316 |
$prev = $refNode->previousSibling; |
| 317 |
} |
| 318 |
if ( $prev !== null && $prev->nodeType === XML_TEXT_NODE ) { |
| 319 |
'@phan-var \DOMCharacterData $prev'; /** @var \DOMCharacterData $prev */ |
| 320 |
$prev->appendData( $data ); |
| 321 |
} else { |
| 322 |
$node = $this->doc->createTextNode( $data ); |
| 323 |
$this->insertNode( $preposition, $refElement, $node ); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
/** @inheritDoc */ |
| 328 |
public function insertElement( $preposition, $refElement, Element $element, $void, |
| 329 |
$sourceStart, $sourceLength |
| 330 |
) { |
| 331 |
if ( $element->userData ) { |
| 332 |
$node = $element->userData; |
| 333 |
} else { |
| 334 |
$node = $this->createNode( $element ); |
| 335 |
} |
| 336 |
$this->insertNode( $preposition, $refElement, $node ); |
| 337 |
} |
| 338 |
|
| 339 |
/** @inheritDoc */ |
| 340 |
public function endTag( Element $element, $sourceStart, $sourceLength ) { |
| 341 |
} |
| 342 |
|
| 343 |
/** @inheritDoc */ |
| 344 |
public function doctype( $name, $public, $system, $quirks, $sourceStart, $sourceLength ) { |
| 345 |
if ( !$this->doc->firstChild ) { |
| 346 |
$this->doc = $this->createDocument( $name, $public, $system ); |
| 347 |
} |
| 348 |
$this->doctypeName = $name; |
| 349 |
$this->public = $public; |
| 350 |
$this->system = $system; |
| 351 |
$this->quirks = $quirks; |
| 352 |
} |
| 353 |
|
| 354 |
/** @inheritDoc */ |
| 355 |
public function comment( $preposition, $refElement, $text, $sourceStart, $sourceLength ) { |
| 356 |
$node = $this->doc->createComment( $text ); |
| 357 |
$this->insertNode( $preposition, $refElement, $node ); |
| 358 |
} |
| 359 |
|
| 360 |
/** @inheritDoc */ |
| 361 |
public function error( $text, $pos ) { |
| 362 |
if ( $this->errorCallback ) { |
| 363 |
( $this->errorCallback )( $text, $pos ); |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
/** @inheritDoc */ |
| 368 |
public function mergeAttributes( Element $element, Attributes $attrs, $sourceStart ) { |
| 369 |
$node = $element->userData; |
| 370 |
'@phan-var \DOMElement $node'; /** @var \DOMElement $node */ |
| 371 |
foreach ( $attrs->getObjects() as $name => $attr ) { |
| 372 |
if ( $attr->namespaceURI === null |
| 373 |
&& strpos( $attr->localName, ':' ) !== false |
| 374 |
) { |
| 375 |
try { |
| 376 |
// As noted in createNode(), we can't use hasAttribute() here. |
| 377 |
// However, we can use the return value of setAttributeNodeNS() |
| 378 |
// instead. |
| 379 |
$attrNode = $this->doc->createAttribute( $attr->localName ); |
| 380 |
$attrNode->value = $attr->value; |
| 381 |
$replaced = $node->setAttributeNodeNS( $attrNode ); |
| 382 |
} catch ( \Throwable $e ) { |
| 383 |
$this->rethrowIfNotDomException( $e ); |
| 384 |
'@phan-var \DOMException $e'; /** @var \DOMException $e */ |
| 385 |
$attrNode = $this->doc->createAttribute( |
| 386 |
$this->coerceName( $attr->localName ) ); |
| 387 |
$attrNode->value = $attr->value; |
| 388 |
$replaced = $node->setAttributeNodeNS( $attrNode ); |
| 389 |
} |
| 390 |
if ( $replaced ) { |
| 391 |
// Put it back how it was |
| 392 |
$node->setAttributeNodeNS( $replaced ); |
| 393 |
} |
| 394 |
} elseif ( $attr->namespaceURI === null ) { |
| 395 |
try { |
| 396 |
if ( !$node->hasAttribute( $attr->localName ) ) { |
| 397 |
$node->setAttribute( $attr->localName, $attr->value ); |
| 398 |
} |
| 399 |
} catch ( \Throwable $e ) { |
| 400 |
$this->rethrowIfNotDomException( $e ); |
| 401 |
'@phan-var \DOMException $e'; /** @var \DOMException $e */ |
| 402 |
$name = $this->coerceName( $attr->localName ); |
| 403 |
if ( !$node->hasAttribute( $name ) ) { |
| 404 |
$node->setAttribute( $name, $attr->value ); |
| 405 |
} |
| 406 |
} |
| 407 |
} else { |
| 408 |
try { |
| 409 |
if ( !$node->hasAttributeNS( $attr->namespaceURI, $attr->localName ) ) { |
| 410 |
$node->setAttributeNS( $attr->namespaceURI, |
| 411 |
$attr->localName, $attr->value ); |
| 412 |
} |
| 413 |
} catch ( \Throwable $e ) { |
| 414 |
$this->rethrowIfNotDomException( $e ); |
| 415 |
'@phan-var \DOMException $e'; /** @var \DOMException $e */ |
| 416 |
$name = $this->coerceName( $attr->localName ); |
| 417 |
if ( !$node->hasAttributeNS( $attr->namespaceURI, $name ) ) { |
| 418 |
$node->setAttributeNS( $attr->namespaceURI, $name, $attr->value ); |
| 419 |
} |
| 420 |
} |
| 421 |
} |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
/** @inheritDoc */ |
| 426 |
public function removeNode( Element $element, $sourceStart ) { |
| 427 |
$node = $element->userData; |
| 428 |
$node->parentNode->removeChild( $node ); |
| 429 |
} |
| 430 |
|
| 431 |
/** @inheritDoc */ |
| 432 |
public function reparentChildren( Element $element, Element $newParent, $sourceStart ) { |
| 433 |
$this->insertElement( TreeBuilder::UNDER, $element, $newParent, false, $sourceStart, 0 ); |
| 434 |
$node = $element->userData; |
| 435 |
/** @var \DOMElement $newParentNode */ |
| 436 |
$newParentNode = $newParent->userData; |
| 437 |
'@phan-var \DOMElement $newParentNode'; |
| 438 |
while ( $node->firstChild !== $newParentNode ) { |
| 439 |
$firstChild = $node->firstChild; |
| 440 |
'@phan-var \DOMNode $firstChild'; |
| 441 |
$newParentNode->appendChild( $firstChild ); |
| 442 |
} |
| 443 |
} |
| 444 |
} |