Debian Codegraph @

vendor/wikimedia/parsoid/src/Ext/DOMUtils.php

raw
1 <?php
2 declare( strict_types = 1 );
3
4 namespace Wikimedia\Parsoid\Ext;
5
6 use Wikimedia\Parsoid\DOM\Element;
7 use Wikimedia\Parsoid\DOM\Node;
8 use Wikimedia\Parsoid\Utils\DOMUtils as DU;
9
10 /**
11 * This class provides DOM helpers useful for extensions.
12 */
13 class DOMUtils {
14 /**
15 * Test if a node matches a given typeof.
16 * @param Node $node node
17 * @param string $type type
18 * @return bool
19 */
20 public static function hasTypeOf( Node $node, string $type ): bool {
21 return DU::hasTypeOf( $node, $type );
22 }
23
24 /**
25 * @param Element $element
26 * @param string $regex Partial regular expression, e.g. "foo|bar"
27 * @return bool
28 */
29 public static function hasClass( Element $element, string $regex ): bool {
30 return DU::hasClass( $element, $regex );
31 }
32
33 /**
34 * Determine whether the node matches the given `typeof` attribute value.
35 *
36 * @param Node $n The node to test
37 * @param string $typeRe Regular expression matching the expected value of
38 * the `typeof` attribute.
39 * @return ?string The matching `typeof` value, or `null` if there is
40 * no match.
41 */
42 public static function matchTypeOf( Node $n, string $typeRe ): ?string {
43 return DU::matchTypeOf( $n, $typeRe );
44 }
45
46 /**
47 * Add a type to the typeof attribute. If the elt already has an existing typeof,
48 * it makes that attribute a string of space separated types.
49 * @param Element $elt
50 * @param string $type type
51 */
52 public static function addTypeOf( Element $elt, string $type ): void {
53 DU::addTypeOf( $elt, $type );
54 }
55
56 /**
57 * Remove a type from the typeof attribute.
58 * @param Element $elt
59 * @param string $type type
60 */
61 public static function removeTypeOf( Element $elt, string $type ): void {
62 DU::removeTypeOf( $elt, $type );
63 }
64
65 /**
66 * Determine whether the node matches the given rel attribute value.
67 *
68 * @param Node $n
69 * @param string $rel Expected value of "rel" attribute, as a literal string.
70 * @return ?string The match if there is one, null otherwise
71 */
72 public static function matchRel( Node $n, string $rel ): ?string {
73 return DU::matchRel( $n, $rel );
74 }
75
76 /**
77 * Add a type to the rel attribute. This method should almost always
78 * be used instead of `setAttribute`, to ensure we don't overwrite existing
79 * rel information.
80 *
81 * @param Element $node node
82 * @param string $rel type
83 */
84 public static function addRel( Element $node, string $rel ): void {
85 DU::addRel( $node, $rel );
86 }
87
88 /**
89 * Assert that this is a DOM element node.
90 * This is primarily to help phan analyze variable types.
91 * @phan-assert Element $node
92 * @param ?Node $node
93 * @return bool Always returns true
94 */
95 public static function assertElt( ?Node $node ): bool {
96 return DU::assertElt( $node );
97 }
98
99 /**
100 * Move 'from'.childNodes to 'to' adding them before 'beforeNode'
101 * If 'beforeNode' is null, the nodes are appended at the end.
102 * @param Node $from Source node. Children will be removed.
103 * @param Node $to Destination node. Children of $from will be added here
104 * @param ?Node $beforeNode Add the children before this node.
105 */
106 public static function migrateChildren(
107 Node $from, Node $to, ?Node $beforeNode = null
108 ): void {
109 DU::migrateChildren( $from, $to, $beforeNode );
110 }
111
112 /**
113 * Add attributes to a node element.
114 *
115 * @param Element $elt element
116 * @param array $attrs attributes
117 */
118 public static function addAttributes( Element $elt, array $attrs ): void {
119 DU::addAttributes( $elt, $attrs );
120 }
121
122 /**
123 * Find an ancestor of $node with nodeName $name.
124 *
125 * @param Node $node
126 * @param string $name
127 * @return ?Element
128 */
129 public static function findAncestorOfName( Node $node, string $name ): ?Element {
130 return DU::findAncestorOfName( $node, $name );
131 }
132 }