49 * @since 6.4.0 |
49 * @since 6.4.0 |
50 * |
50 * |
51 * @param WP_HTML_Token $token Look for this node in the stack. |
51 * @param WP_HTML_Token $token Look for this node in the stack. |
52 * @return bool Whether the referenced node is in the stack of active formatting elements. |
52 * @return bool Whether the referenced node is in the stack of active formatting elements. |
53 */ |
53 */ |
54 public function contains_node( $token ) { |
54 public function contains_node( WP_HTML_Token $token ) { |
55 foreach ( $this->walk_up() as $item ) { |
55 foreach ( $this->walk_up() as $item ) { |
56 if ( $token->bookmark_name === $item->bookmark_name ) { |
56 if ( $token->bookmark_name === $item->bookmark_name ) { |
57 return true; |
57 return true; |
58 } |
58 } |
59 } |
59 } |
85 |
85 |
86 return $current_node ? $current_node : null; |
86 return $current_node ? $current_node : null; |
87 } |
87 } |
88 |
88 |
89 /** |
89 /** |
|
90 * Inserts a "marker" at the end of the list of active formatting elements. |
|
91 * |
|
92 * > The markers are inserted when entering applet, object, marquee, |
|
93 * > template, td, th, and caption elements, and are used to prevent |
|
94 * > formatting from "leaking" into applet, object, marquee, template, |
|
95 * > td, th, and caption elements. |
|
96 * |
|
97 * @see https://html.spec.whatwg.org/#concept-parser-marker |
|
98 * |
|
99 * @since 6.7.0 |
|
100 */ |
|
101 public function insert_marker(): void { |
|
102 $this->push( new WP_HTML_Token( null, 'marker', false ) ); |
|
103 } |
|
104 |
|
105 /** |
90 * Pushes a node onto the stack of active formatting elements. |
106 * Pushes a node onto the stack of active formatting elements. |
91 * |
107 * |
92 * @since 6.4.0 |
108 * @since 6.4.0 |
93 * |
109 * |
94 * @see https://html.spec.whatwg.org/#push-onto-the-list-of-active-formatting-elements |
110 * @see https://html.spec.whatwg.org/#push-onto-the-list-of-active-formatting-elements |
95 * |
111 * |
96 * @param WP_HTML_Token $token Push this node onto the stack. |
112 * @param WP_HTML_Token $token Push this node onto the stack. |
97 */ |
113 */ |
98 public function push( $token ) { |
114 public function push( WP_HTML_Token $token ) { |
99 /* |
115 /* |
100 * > If there are already three elements in the list of active formatting elements after the last marker, |
116 * > If there are already three elements in the list of active formatting elements after the last marker, |
101 * > if any, or anywhere in the list if there are no markers, that have the same tag name, namespace, and |
117 * > if any, or anywhere in the list if there are no markers, that have the same tag name, namespace, and |
102 * > attributes as element, then remove the earliest such element from the list of active formatting |
118 * > attributes as element, then remove the earliest such element from the list of active formatting |
103 * > elements. For these purposes, the attributes must be compared as they were when the elements were |
119 * > elements. For these purposes, the attributes must be compared as they were when the elements were |
117 * @since 6.4.0 |
133 * @since 6.4.0 |
118 * |
134 * |
119 * @param WP_HTML_Token $token Remove this node from the stack, if it's there already. |
135 * @param WP_HTML_Token $token Remove this node from the stack, if it's there already. |
120 * @return bool Whether the node was found and removed from the stack of active formatting elements. |
136 * @return bool Whether the node was found and removed from the stack of active formatting elements. |
121 */ |
137 */ |
122 public function remove_node( $token ) { |
138 public function remove_node( WP_HTML_Token $token ) { |
123 foreach ( $this->walk_up() as $position_from_end => $item ) { |
139 foreach ( $this->walk_up() as $position_from_end => $item ) { |
124 if ( $token->bookmark_name !== $item->bookmark_name ) { |
140 if ( $token->bookmark_name !== $item->bookmark_name ) { |
125 continue; |
141 continue; |
126 } |
142 } |
127 |
143 |
182 public function walk_up() { |
198 public function walk_up() { |
183 for ( $i = count( $this->stack ) - 1; $i >= 0; $i-- ) { |
199 for ( $i = count( $this->stack ) - 1; $i >= 0; $i-- ) { |
184 yield $this->stack[ $i ]; |
200 yield $this->stack[ $i ]; |
185 } |
201 } |
186 } |
202 } |
|
203 |
|
204 /** |
|
205 * Clears the list of active formatting elements up to the last marker. |
|
206 * |
|
207 * > When the steps below require the UA to clear the list of active formatting elements up to |
|
208 * > the last marker, the UA must perform the following steps: |
|
209 * > |
|
210 * > 1. Let entry be the last (most recently added) entry in the list of active |
|
211 * > formatting elements. |
|
212 * > 2. Remove entry from the list of active formatting elements. |
|
213 * > 3. If entry was a marker, then stop the algorithm at this point. |
|
214 * > The list has been cleared up to the last marker. |
|
215 * > 4. Go to step 1. |
|
216 * |
|
217 * @see https://html.spec.whatwg.org/multipage/parsing.html#clear-the-list-of-active-formatting-elements-up-to-the-last-marker |
|
218 * |
|
219 * @since 6.7.0 |
|
220 */ |
|
221 public function clear_up_to_last_marker(): void { |
|
222 foreach ( $this->walk_up() as $item ) { |
|
223 array_pop( $this->stack ); |
|
224 if ( 'marker' === $item->node_name ) { |
|
225 break; |
|
226 } |
|
227 } |
|
228 } |
187 } |
229 } |