Parsoid
A bidirectional parser between wikitext and HTML5
Wikimedia\Parsoid\Utils\DOMCompat Class Reference

Helper class that provides missing DOM level 3 methods for the PHP DOM classes. More...

Static Public Member Functions

static newDocument (bool $isHtml)
 Create a new empty document. More...
 
static nodeName (Node $node)
 Return the lower-case version of the node name (HTML says this should be capitalized). More...
 
static getTitle ( $document)
 Get document title. More...
 
static setTitle ( $document, string $title)
 Set document title. More...
 
static getElementById ( $node, string $id)
 Return the descendant with the specified ID. More...
 
static setIdAttribute ( $element, string $id)
 Workaround bug in PHP's Document::getElementById() which doesn't actually index the 'id' attribute unless you use the non-standard Element::setIdAttribute method after the attribute is set; see https://www.php.net/manual/en/domdocument.getelementbyid.php for more details. More...
 
static attributes ( $element)
 Workaround bug in PHP's Element::$attributes that fails to enumerate attributes named xmlns. More...
 
static hasAttributes ( $element)
 Workaround bug in PHP's Element::hasAttributes() that fails to enumerate attributes named xmlns. More...
 
static getElementsByTagName ( $node, string $tagName)
 Return all descendants with the specified tag name. More...
 
static getLastElementChild ( $node)
 Return the last child of the node that is an Element, or null otherwise. More...
 
static querySelector ( $node, string $selector)
 
static querySelectorAll ( $node, string $selector)
 
static getPreviousElementSibling ( $node)
 Return the last preceding sibling of the node that is an element, or null otherwise. More...
 
static getNextElementSibling ( $node)
 Return the first following sibling of the node that is an element, or null otherwise. More...
 
static remove ( $node)
 Removes the node from the document. More...
 
static getInnerHTML ( $element)
 Get innerHTML. More...
 
static setInnerHTML ( $element, string $html)
 Set innerHTML. More...
 
static getOuterHTML ( $element)
 Get outerHTML. More...
 
static getClassList ( $node)
 Return the class list of this element. More...
 
static normalize ( $elt)
 
static replaceChildren ( $parentNode,... $nodes)
 ParentNode.replaceChildren() https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/replaceChildren. More...
 

Detailed Description

Helper class that provides missing DOM level 3 methods for the PHP DOM classes.

For a DOM method $node->foo( $bar) the equivalent helper is DOMCompat::foo( $node, $bar ). For a DOM property $node->foo there is a DOMCompat::getFoo( $node ) and DOMCompat::setFoo( $node, $value ).

Only implements the methods that are actually used by Parsoid.

Because this class may be used by code outside Parsoid it tries to be relatively tolerant of object types: you can call it either with PHP's DOM* types or with a "proper" DOM implementation, and it will attempt to Do The Right Thing regardless. As a result there are generally not parameter type hints for DOM object types, and the return types will be broad enough to accomodate the value a "real" DOM implementation would return, as well as the values our thunk will return. (For instance, we can't create a "real" NodeList in our compatibility thunk.)

Member Function Documentation

◆ attributes()

static Wikimedia\Parsoid\Utils\DOMCompat::attributes (   $element)
static

Workaround bug in PHP's Element::$attributes that fails to enumerate attributes named xmlns.

Parameters
Element$element
Returns
Attr[]
See also
https://phabricator.wikimedia.org/T235295

◆ getClassList()

static Wikimedia\Parsoid\Utils\DOMCompat::getClassList (   $node)
static

Return the class list of this element.

Parameters
Element$node
Returns
TokenList
See also
https://dom.spec.whatwg.org/#dom-element-classlist

◆ getElementById()

static Wikimedia\Parsoid\Utils\DOMCompat::getElementById (   $node,
string  $id 
)
static

Return the descendant with the specified ID.

Workaround for https://bugs.php.net/bug.php?id=77686 and other issues related to inconsistent indexing behavior.

Parameters
Document | DocumentFragment$node
string$id
Returns
Element|null
See also
https://dom.spec.whatwg.org/#dom-nonelementparentnode-getelementbyid

◆ getElementsByTagName()

static Wikimedia\Parsoid\Utils\DOMCompat::getElementsByTagName (   $node,
string  $tagName 
)
static

Return all descendants with the specified tag name.

Workaround for PHP's getElementsByTagName being inexplicably slow in some situations and the lack of Element::getElementsByTagName().

Parameters
Document | Element$node
string$tagName
Returns
iterable<Element> Either an array or an HTMLCollection object
See also
https://dom.spec.whatwg.org/#dom-document-getelementsbytagname
https://dom.spec.whatwg.org/#dom-element-getelementsbytagname
Note
Note that unlike the spec this method is not guaranteed to return a NodeList (which cannot be freely constructed in PHP), just a traversable containing Elements.

◆ getInnerHTML()

static Wikimedia\Parsoid\Utils\DOMCompat::getInnerHTML (   $element)
static

Get innerHTML.

Parameters
Element$element
Returns
string
See also
https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml

◆ getLastElementChild()

static Wikimedia\Parsoid\Utils\DOMCompat::getLastElementChild (   $node)
static

Return the last child of the node that is an Element, or null otherwise.

Parameters
Document | DocumentFragment | Element$node
Returns
Element|null
See also
https://dom.spec.whatwg.org/#dom-parentnode-lastelementchild

◆ getNextElementSibling()

static Wikimedia\Parsoid\Utils\DOMCompat::getNextElementSibling (   $node)
static

Return the first following sibling of the node that is an element, or null otherwise.

Parameters
Node$node
Returns
Element|null
See also
https://dom.spec.whatwg.org/#dom-nondocumenttypechildnode-nextelementsibling

◆ getOuterHTML()

static Wikimedia\Parsoid\Utils\DOMCompat::getOuterHTML (   $element)
static

Get outerHTML.

Parameters
Element$element
Returns
string
See also
https://w3c.github.io/DOM-Parsing/#dom-element-outerhtml

◆ getPreviousElementSibling()

static Wikimedia\Parsoid\Utils\DOMCompat::getPreviousElementSibling (   $node)
static

Return the last preceding sibling of the node that is an element, or null otherwise.

Parameters
Node$node
Returns
Element|null
See also
https://dom.spec.whatwg.org/#dom-nondocumenttypechildnode-previouselementsibling

◆ getTitle()

static Wikimedia\Parsoid\Utils\DOMCompat::getTitle (   $document)
static

Get document title.

Parameters
Document$document
Returns
string
See also
https://html.spec.whatwg.org/multipage/dom.html#document.title

◆ hasAttributes()

static Wikimedia\Parsoid\Utils\DOMCompat::hasAttributes (   $element)
static

Workaround bug in PHP's Element::hasAttributes() that fails to enumerate attributes named xmlns.

Parameters
Element$element
Returns
bool True if the element has any attributes
See also
https://phabricator.wikimedia.org/T235295

◆ newDocument()

static Wikimedia\Parsoid\Utils\DOMCompat::newDocument ( bool  $isHtml)
static

Create a new empty document.

This is abstracted because the process is a little different depending on whether we're using Dodo or DOMDocument, and phan gets a little confused by this.

Parameters
bool$isHtml
Returns
Document

◆ nodeName()

static Wikimedia\Parsoid\Utils\DOMCompat::nodeName ( Node  $node)
static

Return the lower-case version of the node name (HTML says this should be capitalized).

Parameters
Node$node
Returns
string

◆ normalize()

static Wikimedia\Parsoid\Utils\DOMCompat::normalize (   $elt)
static
Parameters
Element | DocumentFragment$eltroot of the DOM tree that needs to be normalized

◆ querySelector()

static Wikimedia\Parsoid\Utils\DOMCompat::querySelector (   $node,
string  $selector 
)
static
Parameters
Document | DocumentFragment | Element$node
string$selector
Returns
Element|null
See also
https://dom.spec.whatwg.org/#dom-parentnode-queryselector

◆ querySelectorAll()

static Wikimedia\Parsoid\Utils\DOMCompat::querySelectorAll (   $node,
string  $selector 
)
static
Parameters
Document | DocumentFragment | Element$node
string$selector
Returns
iterable<Element> Either a NodeList or an array
See also
https://dom.spec.whatwg.org/#dom-parentnode-queryselectorall
Note
Note that unlike the spec this method is not guaranteed to return a NodeList (which cannot be freely constructed in PHP), just a traversable containing Elements.

◆ remove()

static Wikimedia\Parsoid\Utils\DOMCompat::remove (   $node)
static

Removes the node from the document.

Parameters
Element | CharacterData$node
See also
https://dom.spec.whatwg.org/#dom-childnode-remove

◆ replaceChildren()

static Wikimedia\Parsoid\Utils\DOMCompat::replaceChildren (   $parentNode,
  $nodes 
)
static

ParentNode.replaceChildren() https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/replaceChildren.

Parameters
Document | DocumentFragment | Element$parentNode
array<string|Node>...$nodes

◆ setIdAttribute()

static Wikimedia\Parsoid\Utils\DOMCompat::setIdAttribute (   $element,
string  $id 
)
static

Workaround bug in PHP's Document::getElementById() which doesn't actually index the 'id' attribute unless you use the non-standard Element::setIdAttribute method after the attribute is set; see https://www.php.net/manual/en/domdocument.getelementbyid.php for more details.

Parameters
Element$element
string$idThe desired value for the id attribute on $element.
See also
https://phabricator.wikimedia.org/T232390

◆ setInnerHTML()

static Wikimedia\Parsoid\Utils\DOMCompat::setInnerHTML (   $element,
string  $html 
)
static

Set innerHTML.

See also
https://w3c.github.io/DOM-Parsing/#dom-innerhtml-innerhtml
Parameters
Element$element
string$html

◆ setTitle()

static Wikimedia\Parsoid\Utils\DOMCompat::setTitle (   $document,
string  $title 
)
static

Set document title.

Parameters
Document$document
string$title
See also
https://html.spec.whatwg.org/multipage/dom.html#document.title

The documentation for this class was generated from the following file: