Code Coverage |
||||||||||
Classes and Traits |
Functions and Methods |
Lines |
||||||||
Total | |
0.00% |
0 / 1 |
|
0.00% |
0 / 11 |
CRAP | |
0.00% |
0 / 15 |
DOMUtils | |
0.00% |
0 / 1 |
|
0.00% |
0 / 11 |
342 | |
0.00% |
0 / 15 |
hasTypeOf | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
matchTypeOf | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 1 |
|||
addTypeOf | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
removeTypeOf | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
assertElt | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 1 |
|||
isDiffMarker | |
0.00% |
0 / 1 |
12 | |
0.00% |
0 / 1 |
|||
hasNChildren | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 1 |
|||
migrateChildren | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 2 |
|||
addAttributes | |
0.00% |
0 / 1 |
2 | |
0.00% |
0 / 2 |
|||
firstNonSepChild | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 1 |
|||
findAncestorOfName | |
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 1 |
<?php | |
declare( strict_types = 1 ); | |
namespace Wikimedia\Parsoid\Ext; | |
use Wikimedia\Parsoid\DOM\Element; | |
use Wikimedia\Parsoid\DOM\Node; | |
use Wikimedia\Parsoid\Utils\DOMUtils as DU; | |
/** | |
* This class provides DOM helpers useful for extensions. | |
*/ | |
class DOMUtils { | |
/** | |
* Test if a node matches a given typeof. | |
* @param Node $node node | |
* @param string $type type | |
* @return bool | |
*/ | |
public static function hasTypeOf( Node $node, string $type ): bool { | |
return DU::hasTypeOf( $node, $type ); | |
} | |
/** | |
* Determine whether the node matches the given `typeof` attribute value. | |
* | |
* @param Node $n The node to test | |
* @param string $typeRe Regular expression matching the expected value of | |
* the `typeof` attribute. | |
* @return ?string The matching `typeof` value, or `null` if there is | |
* no match. | |
*/ | |
public static function matchTypeOf( Node $n, string $typeRe ): ?string { | |
return DU::matchTypeOf( $n, $typeRe ); | |
} | |
/** | |
* Add a type to the typeof attribute. If the elt already has an existing typeof, | |
* it makes that attribute a string of space separated types. | |
* @param Element $elt | |
* @param string $type type | |
*/ | |
public static function addTypeOf( Element $elt, string $type ): void { | |
DU::addTypeOf( $elt, $type ); | |
} | |
/** | |
* Remove a type from the typeof attribute. | |
* @param Element $elt | |
* @param string $type type | |
*/ | |
public static function removeTypeOf( Element $elt, string $type ): void { | |
DU::removeTypeOf( $elt, $type ); | |
} | |
/** | |
* Assert that this is a DOM element node. | |
* This is primarily to help phan analyze variable types. | |
* @phan-assert Element $node | |
* @param ?Node $node | |
* @return bool Always returns true | |
*/ | |
public static function assertElt( ?Node $node ): bool { | |
return DU::assertElt( $node ); | |
} | |
/** | |
* Check a node to see whether it's a diff marker. | |
* | |
* @param ?Node $node | |
* @param ?string $mark | |
* @return bool | |
*/ | |
public static function isDiffMarker( | |
?Node $node, ?string $mark = null | |
): bool { | |
return DU::isDiffMarker( $node, $mark ); | |
} | |
/** | |
* Test the number of children this node has without using | |
* `DOMNode::$childNodes->count()`. This walks the sibling list and so | |
* takes O(`nchildren`) time -- so `nchildren` is expected to be small | |
* (say: 0, 1, or 2). | |
* | |
* Skips all diff markers by default. | |
* @param Node $node | |
* @param int $nchildren | |
* @param bool $countDiffMarkers | |
* @return bool | |
*/ | |
public static function hasNChildren( | |
Node $node, int $nchildren, bool $countDiffMarkers = false | |
): bool { | |
return DU::hasNChildren( $node, $nchildren, $countDiffMarkers ); | |
} | |
/** | |
* Move 'from'.childNodes to 'to' adding them before 'beforeNode' | |
* If 'beforeNode' is null, the nodes are appended at the end. | |
* @param Node $from Source node. Children will be removed. | |
* @param Node $to Destination node. Children of $from will be added here | |
* @param ?Node $beforeNode Add the children before this node. | |
*/ | |
public static function migrateChildren( | |
Node $from, Node $to, ?Node $beforeNode = null | |
): void { | |
DU::migrateChildren( $from, $to, $beforeNode ); | |
} | |
/** | |
* Add attributes to a node element. | |
* | |
* @param Element $elt element | |
* @param array $attrs attributes | |
*/ | |
public static function addAttributes( Element $elt, array $attrs ): void { | |
DU::addAttributes( $elt, $attrs ); | |
} | |
/** | |
* Get the first child element or non-IEW text node, ignoring | |
* whitespace-only text nodes, comments, and deleted nodes. | |
* | |
* @param Node $node | |
* @return Node|null | |
*/ | |
public static function firstNonSepChild( Node $node ): ?Node { | |
return DU::firstNonSepChild( $node ); | |
} | |
/** | |
* Find an ancestor of $node with nodeName $name. | |
* | |
* @param Node $node | |
* @param string $name | |
* @return ?Element | |
*/ | |
public static function findAncestorOfName( Node $node, string $name ): ?Element { | |
return DU::findAncestorOfName( $node, $name ); | |
} | |
} |