| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Extension\DiscussionTools; |
| 4 |
|
| 5 |
use DOMException; |
| 6 |
use Throwable; |
| 7 |
use Wikimedia\Parsoid\DOM\Node; |
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
class TreeWalker { |
| 18 |
|
| 19 |
public Node $root; |
| 20 |
public int $whatToShow; |
| 21 |
public Node $currentNode; |
| 22 |
|
| 23 |
public $filter; |
| 24 |
private bool $isActive = false; |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public function __construct( |
| 30 |
Node $root, |
| 31 |
int $whatToShow = NodeFilter::SHOW_ALL, |
| 32 |
?callable $filter = null |
| 33 |
) { |
| 34 |
$this->currentNode = $root; |
| 35 |
$this->filter = $filter; |
| 36 |
$this->root = $root; |
| 37 |
$this->whatToShow = $whatToShow; |
| 38 |
} |
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
public function nextNode(): ?Node { |
| 46 |
$node = $this->currentNode; |
| 47 |
$result = NodeFilter::FILTER_ACCEPT; |
| 48 |
|
| 49 |
while ( true ) { |
| 50 |
while ( $result !== NodeFilter::FILTER_REJECT && $node->firstChild !== null ) { |
| 51 |
$node = $node->firstChild; |
| 52 |
$result = $this->filterNode( $node ); |
| 53 |
if ( $result === NodeFilter::FILTER_ACCEPT ) { |
| 54 |
$this->currentNode = $node; |
| 55 |
return $node; |
| 56 |
} |
| 57 |
} |
| 58 |
|
| 59 |
$sibling = null; |
| 60 |
$temp = $node; |
| 61 |
while ( $temp !== null ) { |
| 62 |
if ( $temp === $this->root ) { |
| 63 |
return null; |
| 64 |
} |
| 65 |
|
| 66 |
$sibling = $temp->nextSibling; |
| 67 |
|
| 68 |
if ( $sibling !== null ) { |
| 69 |
$node = $sibling; |
| 70 |
|
| 71 |
break; |
| 72 |
} |
| 73 |
|
| 74 |
$temp = $temp->parentNode; |
| 75 |
} |
| 76 |
|
| 77 |
'@phan-var Node $node'; |
| 78 |
$result = $this->filterNode( $node ); |
| 79 |
|
| 80 |
if ( $result === NodeFilter::FILTER_ACCEPT ) { |
| 81 |
$this->currentNode = $node; |
| 82 |
|
| 83 |
return $node; |
| 84 |
} |
| 85 |
|
| 86 |
} |
| 87 |
} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
private function filterNode( Node $node ): int { |
| 103 |
if ( $this->isActive ) { |
| 104 |
throw new DOMException( 'InvalidStateError' ); |
| 105 |
} |
| 106 |
|
| 107 |
1. |
| 108 |
$n = $node->nodeType - 1; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
if ( !( ( 1 << $n ) & $this->whatToShow ) ) { |
| 113 |
return NodeFilter::FILTER_SKIP; |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
if ( !$this->filter ) { |
| 118 |
return NodeFilter::FILTER_ACCEPT; |
| 119 |
} |
| 120 |
|
| 121 |
$this->isActive = true; |
| 122 |
|
| 123 |
try { |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
$result = $this->filter instanceof NodeFilter |
| 129 |
? $this->filter->acceptNode( $node ) |
| 130 |
: ( $this->filter )( $node ); |
| 131 |
} catch ( Throwable $e ) { |
| 132 |
$this->isActive = false; |
| 133 |
|
| 134 |
throw $e; |
| 135 |
} |
| 136 |
|
| 137 |
$this->isActive = false; |
| 138 |
|
| 139 |
return $result; |
| 140 |
} |
| 141 |
} |