2020-07-15 14:53:11 +00:00
|
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace MediaWiki\Extension\DiscussionTools;
|
|
|
|
|
|
2022-10-28 18:24:02 +00:00
|
|
|
|
use DOMException;
|
2020-11-16 00:13:55 +00:00
|
|
|
|
use Throwable;
|
2021-07-29 02:16:15 +00:00
|
|
|
|
use Wikimedia\Parsoid\DOM\Node;
|
2020-07-15 14:53:11 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Partial implementation of W3 DOM4 TreeWalker interface.
|
|
|
|
|
*
|
|
|
|
|
* See also:
|
|
|
|
|
* - https://dom.spec.whatwg.org/#interface-treewalker
|
|
|
|
|
*
|
2020-11-16 00:13:55 +00:00
|
|
|
|
* Ported from https://github.com/TRowbotham/PHPDOM (MIT)
|
2020-07-15 14:53:11 +00:00
|
|
|
|
*/
|
|
|
|
|
class TreeWalker {
|
|
|
|
|
|
2023-09-16 00:31:47 +00:00
|
|
|
|
public Node $root;
|
|
|
|
|
public int $whatToShow;
|
|
|
|
|
public Node $currentNode;
|
|
|
|
|
/** @var callable */
|
2020-07-15 14:53:11 +00:00
|
|
|
|
public $filter;
|
2023-09-16 00:31:47 +00:00
|
|
|
|
private bool $isActive = false;
|
2020-07-15 14:53:11 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* See https://dom.spec.whatwg.org/#interface-treewalker
|
|
|
|
|
*
|
2021-07-29 02:16:15 +00:00
|
|
|
|
* @param Node $root
|
2020-11-16 00:13:55 +00:00
|
|
|
|
* @param int $whatToShow
|
2020-07-15 14:53:11 +00:00
|
|
|
|
* @param callable|null $filter
|
|
|
|
|
*/
|
2020-11-16 00:13:55 +00:00
|
|
|
|
public function __construct(
|
2021-07-29 02:16:15 +00:00
|
|
|
|
Node $root,
|
2020-11-16 00:13:55 +00:00
|
|
|
|
int $whatToShow = NodeFilter::SHOW_ALL,
|
|
|
|
|
callable $filter = null
|
|
|
|
|
) {
|
2020-07-15 14:53:11 +00:00
|
|
|
|
$this->currentNode = $root;
|
2020-11-16 00:13:55 +00:00
|
|
|
|
$this->filter = $filter;
|
|
|
|
|
$this->root = $root;
|
|
|
|
|
$this->whatToShow = $whatToShow;
|
2020-07-15 14:53:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* See https://dom.spec.whatwg.org/#dom-treewalker-nextnode
|
|
|
|
|
*
|
2021-07-29 02:16:15 +00:00
|
|
|
|
* @return Node|null The current node
|
2020-07-15 14:53:11 +00:00
|
|
|
|
*/
|
2021-07-29 02:16:15 +00:00
|
|
|
|
public function nextNode(): ?Node {
|
2020-07-15 14:53:11 +00:00
|
|
|
|
$node = $this->currentNode;
|
|
|
|
|
$result = NodeFilter::FILTER_ACCEPT;
|
|
|
|
|
|
|
|
|
|
while ( true ) {
|
|
|
|
|
while ( $result !== NodeFilter::FILTER_REJECT && $node->firstChild !== null ) {
|
|
|
|
|
$node = $node->firstChild;
|
2020-11-16 00:13:55 +00:00
|
|
|
|
$result = $this->filterNode( $node );
|
2020-07-15 14:53:11 +00:00
|
|
|
|
if ( $result === NodeFilter::FILTER_ACCEPT ) {
|
|
|
|
|
$this->currentNode = $node;
|
|
|
|
|
return $node;
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-16 00:13:55 +00:00
|
|
|
|
|
|
|
|
|
$sibling = null;
|
|
|
|
|
$temp = $node;
|
|
|
|
|
while ( $temp !== null ) {
|
|
|
|
|
if ( $temp === $this->root ) {
|
|
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$sibling = $temp->nextSibling;
|
|
|
|
|
|
|
|
|
|
if ( $sibling !== null ) {
|
|
|
|
|
$node = $sibling;
|
|
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$temp = $temp->parentNode;
|
2020-07-15 14:53:11 +00:00
|
|
|
|
}
|
2020-11-16 00:13:55 +00:00
|
|
|
|
|
2021-07-29 02:16:15 +00:00
|
|
|
|
'@phan-var Node $node';
|
2020-11-16 00:13:55 +00:00
|
|
|
|
$result = $this->filterNode( $node );
|
|
|
|
|
|
2020-07-15 14:53:11 +00:00
|
|
|
|
if ( $result === NodeFilter::FILTER_ACCEPT ) {
|
|
|
|
|
$this->currentNode = $node;
|
2020-11-16 00:13:55 +00:00
|
|
|
|
|
2020-07-15 14:53:11 +00:00
|
|
|
|
return $node;
|
|
|
|
|
}
|
2020-11-16 00:13:55 +00:00
|
|
|
|
|
2020-07-15 14:53:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
2020-11-16 00:13:55 +00:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Filters a node.
|
|
|
|
|
*
|
|
|
|
|
* @internal
|
|
|
|
|
*
|
|
|
|
|
* @see https://dom.spec.whatwg.org/#concept-node-filter
|
|
|
|
|
*
|
2021-07-29 02:16:15 +00:00
|
|
|
|
* @param Node $node The node to check.
|
2020-11-16 00:13:55 +00:00
|
|
|
|
* @return int Returns one of NodeFilter's FILTER_* constants.
|
|
|
|
|
* - NodeFilter::FILTER_ACCEPT
|
|
|
|
|
* - NodeFilter::FILTER_REJECT
|
|
|
|
|
* - NodeFilter::FILTER_SKIP
|
|
|
|
|
*/
|
2021-07-29 02:16:15 +00:00
|
|
|
|
private function filterNode( Node $node ): int {
|
2020-11-16 00:13:55 +00:00
|
|
|
|
if ( $this->isActive ) {
|
2022-10-28 18:24:02 +00:00
|
|
|
|
throw new DOMException( 'InvalidStateError' );
|
2020-11-16 00:13:55 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Let n be node’s nodeType attribute value minus 1.
|
|
|
|
|
$n = $node->nodeType - 1;
|
|
|
|
|
|
|
|
|
|
// If the nth bit (where 0 is the least significant bit) of whatToShow
|
|
|
|
|
// is not set, return FILTER_SKIP.
|
|
|
|
|
if ( !( ( 1 << $n ) & $this->whatToShow ) ) {
|
|
|
|
|
return NodeFilter::FILTER_SKIP;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// If filter is null, return FILTER_ACCEPT.
|
|
|
|
|
if ( !$this->filter ) {
|
|
|
|
|
return NodeFilter::FILTER_ACCEPT;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->isActive = true;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
// Let $result be the return value of call a user object's operation
|
|
|
|
|
// with traverser's filter, "acceptNode", and Node. If this throws
|
|
|
|
|
// an exception, then unset traverser's active flag and rethrow the
|
|
|
|
|
// exception.
|
|
|
|
|
$result = $this->filter instanceof NodeFilter
|
|
|
|
|
? $this->filter->acceptNode( $node )
|
|
|
|
|
: ( $this->filter )( $node );
|
|
|
|
|
} catch ( Throwable $e ) {
|
|
|
|
|
$this->isActive = false;
|
|
|
|
|
|
|
|
|
|
throw $e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
$this->isActive = false;
|
|
|
|
|
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
2020-07-15 14:53:11 +00:00
|
|
|
|
}
|