Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
CRAP | |
0.00% |
0 / 1 |
NodeFilter | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |
0.00% |
0 / 1 |
acceptNode | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\DiscussionTools; |
4 | |
5 | use DOMException; |
6 | use Wikimedia\Parsoid\DOM\Node; |
7 | |
8 | /** |
9 | * Partial implementation of W3 DOM4 NodeFilter interface. |
10 | * |
11 | * See also: |
12 | * - https://dom.spec.whatwg.org/#interface-nodefilter |
13 | * |
14 | * Adapted from https://github.com/Krinkle/dom-TreeWalker-polyfill/blob/master/src/TreeWalker-polyfill.js |
15 | */ |
16 | class NodeFilter { |
17 | |
18 | // Constants for acceptNode() |
19 | public const FILTER_ACCEPT = 1; |
20 | public const FILTER_REJECT = 2; |
21 | public const FILTER_SKIP = 3; |
22 | |
23 | // Constants for whatToShow |
24 | public const SHOW_ALL = 0xFFFFFFFF; |
25 | public const SHOW_ELEMENT = 0x1; |
26 | public const SHOW_ATTRIBUTE = 0x2; |
27 | public const SHOW_TEXT = 0x4; |
28 | public const SHOW_CDATA_SECTION = 0x8; |
29 | public const SHOW_ENTITY_REFERENCE = 0x10; |
30 | public const SHOW_ENTITY = 0x20; |
31 | public const SHOW_PROCESSING_INSTRUCTION = 0x40; |
32 | public const SHOW_COMMENT = 0x80; |
33 | public const SHOW_DOCUMENT = 0x100; |
34 | public const SHOW_DOCUMENT_TYPE = 0x200; |
35 | public const SHOW_DOCUMENT_FRAGMENT = 0x400; |
36 | public const SHOW_NOTATION = 0x800; |
37 | |
38 | /** @var callable */ |
39 | public $filter; |
40 | |
41 | private bool $active = false; |
42 | |
43 | /** |
44 | * See https://dom.spec.whatwg.org/#dom-nodefilter-acceptnode |
45 | * |
46 | * @param Node $node |
47 | * @return int Constant NodeFilter::FILTER_ACCEPT, |
48 | * NodeFilter::FILTER_REJECT or NodeFilter::FILTER_SKIP. |
49 | */ |
50 | public function acceptNode( $node ) { |
51 | if ( $this->active ) { |
52 | throw new DOMException( 'INVALID_STATE_ERR' ); |
53 | } |
54 | |
55 | $this->active = true; |
56 | $result = call_user_func( $this->filter, $node ); |
57 | $this->active = false; |
58 | |
59 | return $result; |
60 | } |
61 | } |