Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 67
0.00% covered (danger)
0.00%
0 / 15
CRAP
0.00% covered (danger)
0.00%
0 / 1
SimpleStack
0.00% covered (danger)
0.00%
0 / 67
0.00% covered (danger)
0.00%
0 / 15
1560
0.00% covered (danger)
0.00%
0 / 1
 push
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 pop
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 replace
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
6
 remove
0.00% covered (danger)
0.00%
0 / 5
0.00% covered (danger)
0.00%
0 / 1
6
 isInScope
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isElementInScope
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
20
 isOneOfSetInScope
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 isInListScope
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
6
 isInButtonScope
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 isInTableScope
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 isInSelectScope
0.00% covered (danger)
0.00%
0 / 9
0.00% covered (danger)
0.00%
0 / 1
56
 isInSpecificScope
0.00% covered (danger)
0.00%
0 / 7
0.00% covered (danger)
0.00%
0 / 1
30
 item
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 length
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 hasTemplate
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\RemexHtml\TreeBuilder;
5
6use Wikimedia\RemexHtml\HTMLData;
7
8/**
9 * An implementation of the "stack of open elements" which, unlike CachingStack,
10 * iterates through the stack in order to answer queries about which elements
11 * are in scope. This is presumably faster for best case input.
12 */
13class SimpleStack extends Stack {
14    /** @var array<int,Element> */
15    private $elements;
16
17    /**
18     * A 2-d array giving the element types which break a scope region for the
19     * default scope, i.e. the one for phrases of the form "has an X element
20     * in scope".
21     */
22    private const DEFAULT_SCOPE = [
23        HTMLData::NS_HTML => [
24            'applet' => true,
25            'caption' => true,
26            'html' => true,
27            'table' => true,
28            'td' => true,
29            'th' => true,
30            'marquee' => true,
31            'object' => true,
32            'template' => true,
33        ],
34        HTMLData::NS_MATHML => [
35            'mi' => true,
36            'mo' => true,
37            'mn' => true,
38            'ms' => true,
39            'mtext' => true,
40            'annotation-xml' => true,
41        ],
42        HTMLData::NS_SVG => [
43            'foreignObject' => true,
44            'desc' => true,
45            'title' => true,
46        ],
47    ];
48
49    /**
50     * The element types which break the table scope.
51     */
52    private const TABLE_SCOPE = [
53        HTMLData::NS_HTML => [
54            'html' => true,
55            'table' => true,
56            'template' => true,
57        ]
58    ];
59
60    /**
61     * The element types which break the list scope. This is lazy-initialised.
62     * @var array<string,array<string,bool>>
63     */
64    private static $listScope;
65
66    /**
67     * The element types which break the button scope. This is lazy-initialised.
68     * @var array<string,array<string,bool>>
69     */
70    private static $buttonScope;
71
72    /** @inheritDoc */
73    public function push( Element $elt ) {
74        $this->elements[] = $elt;
75        $this->current = $elt;
76        $elt->stackIndex = array_key_last( $this->elements );
77    }
78
79    /** @inheritDoc */
80    public function pop() {
81        $elt = array_pop( $this->elements );
82        $elt->stackIndex = null;
83        $this->current = array_last( $this->elements );
84        return $elt;
85    }
86
87    /** @inheritDoc */
88    public function replace( Element $oldElt, Element $elt ) {
89        $idx = $oldElt->stackIndex;
90        $this->elements[$idx] = $elt;
91        $oldElt->stackIndex = null;
92        $elt->stackIndex = $idx;
93        if ( $idx === array_key_last( $this->elements ) ) {
94            $this->current = $elt;
95        }
96    }
97
98    /** @inheritDoc */
99    public function remove( Element $elt ) {
100        $eltIndex = $elt->stackIndex;
101        $n = count( $this->elements );
102        for ( $i = $eltIndex + 1; $i < $n; $i++ ) {
103            $this->elements[$i]->stackIndex--;
104        }
105        $elt->stackIndex = null;
106    }
107
108    /** @inheritDoc */
109    public function isInScope( $name ) {
110        return $this->isInSpecificScope( $name, self::DEFAULT_SCOPE );
111    }
112
113    /** @inheritDoc */
114    public function isElementInScope( Element $elt ) {
115        for ( $i = count( $this->elements ) - 1; $i >= 0; $i-- ) {
116            $node = $this->elements[$i];
117            if ( $node === $elt ) {
118                return true;
119            }
120            if ( isset( self::DEFAULT_SCOPE[$node->namespace][$node->name] ) ) {
121                return false;
122            }
123        }
124        return false;
125    }
126
127    /** @inheritDoc */
128    public function isOneOfSetInScope( $names ) {
129        for ( $i = count( $this->elements ) - 1; $i >= 0; $i-- ) {
130            $node = $this->elements[$i];
131            if ( $node->namespace === HTMLData::NS_HTML && isset( $names[$node->name] ) ) {
132                return true;
133            }
134            if ( isset( self::DEFAULT_SCOPE[$node->namespace][$node->name] ) ) {
135                return false;
136            }
137        }
138        return false;
139    }
140
141    /** @inheritDoc */
142    public function isInListScope( $name ) {
143        if ( self::$listScope === null ) {
144            self::$listScope = self::DEFAULT_SCOPE;
145            self::$listScope[HTMLData::NS_HTML] += [
146                'ol' => true,
147                'li' => true
148            ];
149        }
150        return $this->isInSpecificScope( $name, self::$listScope );
151    }
152
153    /** @inheritDoc */
154    public function isInButtonScope( $name ) {
155        if ( self::$buttonScope === null ) {
156            self::$buttonScope = self::DEFAULT_SCOPE;
157            self::$buttonScope[HTMLData::NS_HTML]['button'] = true;
158        }
159        return $this->isInSpecificScope( $name, self::$buttonScope );
160    }
161
162    /** @inheritDoc */
163    public function isInTableScope( $name ) {
164        return $this->isInSpecificScope( $name, self::TABLE_SCOPE );
165    }
166
167    /** @inheritDoc */
168    public function isInSelectScope( $name ) {
169        for ( $i = count( $this->elements ) - 1; $i >= 0; $i-- ) {
170            $node = $this->elements[$i];
171            if ( $node->namespace === HTMLData::NS_HTML && $node->name === $name ) {
172                return true;
173            }
174            if ( $node->namespace !== HTMLData::NS_HTML ) {
175                return false;
176            }
177            if ( $node->name !== 'optgroup' && $node->name !== 'option' ) {
178                return false;
179            }
180        }
181        return false;
182    }
183
184    private function isInSpecificScope( string $name, array $set ): bool {
185        for ( $i = count( $this->elements ) - 1; $i >= 0; $i-- ) {
186            $node = $this->elements[$i];
187            if ( $node->namespace === HTMLData::NS_HTML && $node->name === $name ) {
188                return true;
189            }
190            if ( isset( $set[$node->namespace][$node->name] ) ) {
191                return false;
192            }
193        }
194        return false;
195    }
196
197    /** @inheritDoc */
198    public function item( $idx ) {
199        return $this->elements[$idx];
200    }
201
202    /** @inheritDoc */
203    public function length() {
204        return count( $this->elements );
205    }
206
207    /** @inheritDoc */
208    public function hasTemplate() {
209        foreach ( $this->elements as $elt ) {
210            if ( $elt->namespace === HTMLData::NS_HTML && $elt->name === 'template' ) {
211                return true;
212            }
213        }
214        return false;
215    }
216}