Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 17
CRAP
0.00% covered (danger)
0.00%
0 / 1
DOMUtils
0.00% covered (danger)
0.00%
0 / 17
0.00% covered (danger)
0.00%
0 / 17
306
0.00% covered (danger)
0.00%
0 / 1
 parseHTML
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getFragmentInnerHTML
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setFragmentInnerHTML
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 parseHTMLToFragment
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasTypeOf
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasClass
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 matchTypeOf
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addTypeOf
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 removeTypeOf
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 matchRel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addRel
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 assertElt
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 childNodes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 migrateChildren
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 addAttributes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 findAncestorOfName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 nodeName
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Ext;
5
6use Wikimedia\Parsoid\DOM\Document;
7use Wikimedia\Parsoid\DOM\DocumentFragment;
8use Wikimedia\Parsoid\DOM\Element;
9use Wikimedia\Parsoid\DOM\Node;
10use Wikimedia\Parsoid\Utils\DOMUtils as DU;
11
12/**
13 * This class provides DOM helpers useful for extensions.
14 */
15class DOMUtils {
16    /**
17     * Parse HTML, return the DOM.
18     *
19     * @note The resulting document is not "prepared and loaded" into Parsoid's
20     * internal processed DOM format. Use ContentUtils::createAndLoadDocument()
21     * instead if that's what you need.
22     */
23    public static function parseHTML(
24        string $html, bool $validateXMLNames = false
25    ): Document {
26        return DU::parseHTML( $html, $validateXMLNames );
27    }
28
29    /**
30     * innerHTML and outerHTML are not defined on DocumentFragment.
31     *
32     * Defined similarly to DOMCompat::getInnerHTML()
33     */
34    public static function getFragmentInnerHTML( DocumentFragment $frag ): string {
35        return DU::getFragmentInnerHTML( $frag );
36    }
37
38    /**
39     * innerHTML and outerHTML are not defined on DocumentFragment.
40     * @see DOMCompat::setInnerHTML() for the Element version
41     */
42    public static function setFragmentInnerHTML( DocumentFragment $frag, string $html ): void {
43        DU::setFragmentInnerHTML( $frag, $html );
44    }
45
46    public static function parseHTMLToFragment( Document $doc, string $html ): DocumentFragment {
47        return DU::parseHTMLToFragment( $doc, $html );
48    }
49
50    /**
51     * Test if a node matches a given typeof.
52     * @param Node $node node
53     * @param string $type type
54     * @return bool
55     */
56    public static function hasTypeOf( Node $node, string $type ): bool {
57        return DU::hasTypeOf( $node, $type );
58    }
59
60    /**
61     * @param Element $element
62     * @param string $regex Partial regular expression, e.g. "foo|bar"
63     * @return bool
64     */
65    public static function hasClass( Element $element, string $regex ): bool {
66        return DU::hasClass( $element, $regex );
67    }
68
69    /**
70     * Determine whether the node matches the given `typeof` attribute value.
71     *
72     * @param Node $n The node to test
73     * @param string $typeRe Regular expression matching the expected value of
74     *   the `typeof` attribute.
75     * @return ?string The matching `typeof` value, or `null` if there is
76     *   no match.
77     */
78    public static function matchTypeOf( Node $n, string $typeRe ): ?string {
79        return DU::matchTypeOf( $n, $typeRe );
80    }
81
82    /**
83     * Add a type to the typeof attribute. If the elt already has an existing typeof,
84     * it makes that attribute a string of space separated types.
85     * @param Element $elt
86     * @param string $type type
87     */
88    public static function addTypeOf( Element $elt, string $type ): void {
89        DU::addTypeOf( $elt, $type );
90    }
91
92    /**
93     * Remove a type from the typeof attribute.
94     * @param Element $elt
95     * @param string $type type
96     */
97    public static function removeTypeOf( Element $elt, string $type ): void {
98        DU::removeTypeOf( $elt, $type );
99    }
100
101    /**
102     * Determine whether the node matches the given rel attribute value.
103     *
104     * @param Node $n
105     * @param string $rel Expected value of "rel" attribute, as a literal string.
106     * @return ?string The match if there is one, null otherwise
107     */
108    public static function matchRel( Node $n, string $rel ): ?string {
109        return DU::matchRel( $n, $rel );
110    }
111
112    /**
113     * Add a type to the rel attribute.  This method should almost always
114     * be used instead of `setAttribute`, to ensure we don't overwrite existing
115     * rel information.
116     *
117     * @param Element $node node
118     * @param string $rel type
119     */
120    public static function addRel( Element $node, string $rel ): void {
121        DU::addRel( $node, $rel );
122    }
123
124    /**
125     * Assert that this is a DOM element node.
126     * This is primarily to help phan analyze variable types.
127     * @phan-assert Element $node
128     * @param ?Node $node
129     * @return bool Always returns true
130     */
131    public static function assertElt( ?Node $node ): bool {
132        return DU::assertElt( $node );
133    }
134
135    /**
136     * Many DOM implementations will de-optimize the representation of a
137     * Node if `$node->childNodes` is accessed, converting the linked list
138     * of node children to an array which is then expensive to mutate.
139     *
140     * This method returns an array of child nodes, but uses the
141     * `->firstChild`/`->nextSibling` accessors to obtain it, avoiding
142     * deoptimization.  This is also robust against concurrent mutation.
143     *
144     * @param Node $n
145     * @return list<Node> the child nodes
146     */
147    public static function childNodes( Node $n ): array {
148        return DU::childNodes( $n );
149    }
150
151    /**
152     * Move 'from'.childNodes to 'to' adding them before 'beforeNode'
153     * If 'beforeNode' is null, the nodes are appended at the end.
154     * @param Node $from Source node. Children will be removed.
155     * @param Node $to Destination node. Children of $from will be added here
156     * @param ?Node $beforeNode Add the children before this node.
157     */
158    public static function migrateChildren(
159        Node $from, Node $to, ?Node $beforeNode = null
160    ): void {
161        DU::migrateChildren( $from, $to, $beforeNode );
162    }
163
164    /**
165     * Add attributes to a node element.
166     *
167     * @param Element $elt element
168     * @param array $attrs attributes
169     */
170    public static function addAttributes( Element $elt, array $attrs ): void {
171        DU::addAttributes( $elt, $attrs );
172    }
173
174    /**
175     * Find an ancestor of $node with nodeName $name.
176     *
177     * @param Node $node
178     * @param string $name
179     * @return ?Element
180     */
181    public static function findAncestorOfName( Node $node, string $name ): ?Element {
182        return DU::findAncestorOfName( $node, $name );
183    }
184
185    /**
186     * Return the lower-case version of the node name.
187     * FIXME: HTML says this should be capitalized, but we are tailoring
188     * this to the PHP7.x DOM libraries that return lower-case names.
189     * @see DOMCompat::nodeName()
190     */
191    public static function nodeName( Node $node ): string {
192        return DU::nodeName( $node );
193    }
194}