Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
NodeFilter
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 1
 acceptNode
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2
3namespace MediaWiki\Extension\DiscussionTools;
4
5use Exception;
6use 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 */
16class 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    public $filter;
39
40    private $active = false;
41
42    /**
43     * See https://dom.spec.whatwg.org/#dom-nodefilter-acceptnode
44     *
45     * @param Node $node
46     * @return int Constant NodeFilter::FILTER_ACCEPT,
47     *  NodeFilter::FILTER_REJECT or NodeFilter::FILTER_SKIP.
48     */
49    public function acceptNode( $node ) {
50        if ( $this->active ) {
51            throw new Exception( 'DOMException: INVALID_STATE_ERR' );
52        }
53
54        $this->active = true;
55        $result = call_user_func( $this->filter, $node );
56        $this->active = false;
57
58        return $result;
59    }
60}