Skip to content

Commit 76c4ab8

Browse files
committed
Refactor last fix to create post-hooks for element addition and removal from the stack of open elements.
1 parent 7482fe9 commit 76c4ab8

File tree

1 file changed

+57
-25
lines changed

1 file changed

+57
-25
lines changed

src/wp-includes/html-api/class-wp-html-open-elements.php

Lines changed: 57 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -187,16 +187,7 @@ public function pop() {
187187
return false;
188188
}
189189

190-
/*
191-
* When adding support for new elements, expand this switch to trap
192-
* cases where the precalculated value needs to change.
193-
*/
194-
switch ( $item->node_name ) {
195-
case 'P':
196-
$this->has_p_in_button_scope = $this->has_element_in_button_scope( 'P' );
197-
break;
198-
}
199-
190+
$this->after_element_pop( $item );
200191
return true;
201192
}
202193

@@ -233,16 +224,7 @@ public function pop_until( $tag_name ) {
233224
*/
234225
public function push( $stack_item ) {
235226
$this->stack[] = $stack_item;
236-
237-
/*
238-
* When adding support for new elements, expand this switch to trap
239-
* cases where the precalculated value needs to change.
240-
*/
241-
switch ( $stack_item->node_name ) {
242-
case 'P':
243-
$this->has_p_in_button_scope = true;
244-
break;
245-
}
227+
$this->after_element_push( $stack_item );
246228
}
247229

248230
/**
@@ -257,11 +239,7 @@ public function remove_node( $token ) {
257239
foreach ( $this->walk_up() as $position => $item ) {
258240
if ( $token->bookmark_name === $item->bookmark_name ) {
259241
array_splice( $this->stack, $this->count() - $position - 1, 1 );
260-
switch ( $item->node_name ) {
261-
case 'P':
262-
$this->has_p_in_button_scope = $this->has_element_in_button_scope( 'P' );
263-
break;
264-
}
242+
$this->after_element_pop( $item );
265243
return true;
266244
}
267245
}
@@ -321,4 +299,58 @@ public function walk_up() {
321299
yield $this->stack[ $i ];
322300
}
323301
}
302+
303+
/*
304+
* Internal helpers.
305+
*/
306+
307+
/**
308+
* Updates internal flags after adding an element.
309+
*
310+
* Certain conditions (such as "has_p_in_button_scope") are maintained here as
311+
* flags that are only modified when adding and removing elements. This allows
312+
* the HTML Processor to quickly check for these conditions instead of iterating
313+
* over the open stack elements upon each new tag it encounters. These flags,
314+
* however, need to be maintained as items are added and removed from the stack.
315+
*
316+
* @since 6.4.0
317+
*
318+
* @param WP_HTML_Token $item Element that was added to the stack of open elements.
319+
*/
320+
public function after_element_push( $item ) {
321+
/*
322+
* When adding support for new elements, expand this switch to trap
323+
* cases where the precalculated value needs to change.
324+
*/
325+
switch ( $item->node_name ) {
326+
case 'P':
327+
$this->has_p_in_button_scope = true;
328+
break;
329+
}
330+
}
331+
332+
/**
333+
* Updates internal flags after removing an element.
334+
*
335+
* Certain conditions (such as "has_p_in_button_scope") are maintained here as
336+
* flags that are only modified when adding and removing elements. This allows
337+
* the HTML Processor to quickly check for these conditions instead of iterating
338+
* over the open stack elements upon each new tag it encounters. These flags,
339+
* however, need to be maintained as items are added and removed from the stack.
340+
*
341+
* @since 6.4.0
342+
*
343+
* @param WP_HTML_Token $item Element that was removed to the stack of open elements.
344+
*/
345+
public function after_element_pop( $item ) {
346+
/*
347+
* When adding support for new elements, expand this switch to trap
348+
* cases where the precalculated value needs to change.
349+
*/
350+
switch ( $item->node_name ) {
351+
case 'P':
352+
$this->has_p_in_button_scope = $this->has_element_in_button_scope( 'P' );
353+
break;
354+
}
355+
}
324356
}

0 commit comments

Comments
 (0)