Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 16
CRAP
0.00% covered (danger)
0.00%
0 / 1
DOMDataUtils
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 16
272
0.00% covered (danger)
0.00%
0 / 1
 getAttributeObject
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getAttributeObjectDefault
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setAttributeObject
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 removeAttributeObject
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDataParsoid
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setDataParsoid
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDataMw
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDataMwIfExists
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 dataMwExists
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setDataMw
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getDataParsoidDiff
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 setDataParsoidDiff
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 noAttrs
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 cloneNode
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 cloneElement
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 cloneDocumentFragment
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\JsonCodec\Hint;
7use Wikimedia\Parsoid\DOM\DocumentFragment;
8use Wikimedia\Parsoid\DOM\Element;
9use Wikimedia\Parsoid\DOM\Node;
10use Wikimedia\Parsoid\NodeData\DataMw;
11use Wikimedia\Parsoid\NodeData\DataParsoid;
12use Wikimedia\Parsoid\NodeData\DataParsoidDiff;
13use Wikimedia\Parsoid\Utils\DOMDataUtils as DDU;
14
15/**
16 * This class provides DOM data helpers needed by extensions.
17 * These helpers support fetching / updating attributes of DOM nodes.
18 */
19class DOMDataUtils {
20    /**
21     * Return the value of a rich attribute as a live (by-reference) object.
22     * This also serves as an assertion that there are not conflicting types.
23     *
24     * @template T
25     * @param Element $node The node on which the attribute is to be found.
26     * @param string $name The name of the attribute.
27     * @param class-string<T>|Hint<T>|null $classHint
28     *   If the hint is null, will use the registered abbreviation.
29     * @return T|null The attribute value, or null if not present.
30     */
31    public static function getAttributeObject(
32        Element $node, string $name, $classHint = null
33    ): ?object {
34        return DDU::getAttributeObject( $node, $name, $classHint );
35    }
36
37    /**
38     * Return the value of a rich attribute as a live (by-reference)
39     * object.  This also serves as an assertion that there are not
40     * conflicting types.  If the value is not present, a default value
41     * will be created using `$codec->defaultValue()` falling back to
42     * `$className::defaultValue()` and stored as the value of the
43     * attribute.
44     *
45     * @note The $className should have be JsonCodecable (either directly
46     *  or via a custom JsonClassCodec).
47     *
48     * @template T
49     * @param Element $node The node on which the attribute is to be found.
50     * @param string $name The name of the attribute.
51     * @param class-string<T>|Hint<T>|null $classHint
52     *   If the hint is null, will use the registered abbreviation.
53     * @return T The attribute value, or a default value if not present.
54     */
55    public static function getAttributeObjectDefault(
56        Element $node, string $name, $classHint = null
57    ): object {
58        return DDU::getAttributeObjectDefault( $node, $name, $classHint );
59    }
60
61    /**
62     * Set the value of a rich attribute, overwriting any previous
63     * value.  Generally mutating the result returned by the
64     * `::getAttribute*Default()` methods should be done instead of
65     * using this method, since the objects returned are live.
66     *
67     * @param Element $node The node on which the attribute is to be found.
68     * @param string $name The name of the attribute.
69     * @param object $value The new (object) value for the attribute
70     * @param class-string|Hint|null $classHint Optional serialization hint
71     */
72    public static function setAttributeObject(
73        Element $node, string $name, object $value, $classHint = null
74    ): void {
75        DDU::setAttributeObject( $node, $name, $value, $classHint );
76    }
77
78    /**
79     * Remove a rich attribute.
80     *
81     * @param Element $node The node on which the attribute is to be found.
82     * @param string $name The name of the attribute.
83     */
84    public static function removeAttributeObject(
85        Element $node, string $name
86    ): void {
87        DDU::removeAttributeObject( $node, $name );
88    }
89
90    /**
91     * Get data parsoid info from DOM element
92     * @param Element $elt
93     * @return DataParsoid ( this is mostly used for type hinting )
94     */
95    public static function getDataParsoid( Element $elt ): DataParsoid {
96        return DDU::getDataParsoid( $elt );
97    }
98
99    /**
100     * Set data parsoid info on a DOM element
101     */
102    public static function setDataParsoid( Element $elt, ?DataParsoid $dp ): void {
103        DDU::setDataParsoid( $elt, $dp );
104    }
105
106    /**
107     * Get data meta wiki info from a DOM element
108     */
109    public static function getDataMw( Element $elt ): DataMw {
110        return DDU::getDataMw( $elt );
111    }
112
113    /**
114     * Get data meta wiki info, but don't create it if it doesn't exist.
115     */
116    public static function getDataMwIfExists( Element $node ): ?DataMw {
117        return DDU::getDataMwIfExists( $node );
118    }
119
120    /**
121     * Check if there is meta wiki info on a DOM element
122     * @param Element $elt
123     * @return bool
124     */
125    public static function dataMwExists( Element $elt ): bool {
126        return !DDU::getDataMw( $elt )->isEmpty();
127    }
128
129    /**
130     * Set data meta wiki info from a DOM element
131     * @param Element $elt
132     * @param ?DataMw $dmw data-mw
133     */
134    public static function setDataMw( Element $elt, ?DataMw $dmw ): void {
135        DDU::setDataMw( $elt, $dmw );
136    }
137
138    /**
139     * Get data diff info from a DOM element.
140     * @param Element $elt
141     * @return ?DataParsoidDiff
142     */
143    public static function getDataParsoidDiff( Element $elt ): ?DataParsoidDiff {
144        return DDU::getDataParsoidDiff( $elt );
145    }
146
147    /**
148     * Set data diff info on a DOM element.
149     * @param Element $elt
150     * @param ?DataParsoidDiff $diffObj data-parsoid-diff object
151     */
152    public static function setDataParsoidDiff( Element $elt, ?DataParsoidDiff $diffObj ): void {
153        DDU::setDataParsoidDiff( $elt, $diffObj );
154    }
155
156    /**
157     * Does this node have any attributes? This method is the preferred way of
158     * interrogating this property since Parsoid DOMs might have Parsoid-internal
159     * attributes added.
160     * @param Element $elt
161     * @return bool
162     */
163    public static function noAttrs( Element $elt ): bool {
164        return DDU::noAttrs( $elt );
165    }
166
167    /**
168     * Clones a node and its data bag
169     * @param Node $node
170     * @param bool $deep
171     * @return Node
172     */
173    public static function cloneNode( Node $node, bool $deep ): Node {
174        return DDU::cloneNode( $node, $deep );
175    }
176
177    /**
178     * Clones an element and its data bag
179     * @param Element $elt
180     * @param bool $deep
181     * @return Element
182     */
183    public static function cloneElement( Element $elt, bool $deep ): Element {
184        return DDU::cloneElement( $elt, $deep );
185    }
186
187    /**
188     * Clones a document fragment and its data bag
189     * @param DocumentFragment $df
190     * @return DocumentFragment
191     */
192    public static function cloneDocumentFragment( DocumentFragment $df ): DocumentFragment {
193        return DDU::cloneDocumentFragment( $df );
194    }
195}