Skip to content

Commit 374cba3

Browse files
committed
Continue iterating
1 parent ad82d3a commit 374cba3

2 files changed

Lines changed: 53 additions & 33 deletions

File tree

src/wp-includes/html-api/class-wp-html-active-formatting-elements.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class WP_HTML_Active_Formatting_Elements {
4444
private $stack = array();
4545

4646
/**
47-
* Returns the node at the given index in the list of active formatting elements.
47+
* Returns the node at the given 1-offset index in the list of active formatting elements.
4848
*
4949
* Do not use this method; it is meant to be used only by the HTML Processor.
5050
*
@@ -55,8 +55,8 @@ class WP_HTML_Active_Formatting_Elements {
5555
* @param int $index Number of nodes from the top node to return.
5656
* @return WP_HTML_Token|null Node at the given index in the stack, if one exists, otherwise null.
5757
*/
58-
public function at( $index ) {
59-
return $this->stack[ $index ];
58+
public function at( $nth ) {
59+
return $this->stack[ $nth - 1 ];
6060
}
6161

6262
/**

src/wp-includes/html-api/class-wp-html-processor.php

Lines changed: 50 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5093,63 +5093,83 @@ private function get_adjusted_current_node(): ?WP_HTML_Token {
50935093
private function reconstruct_active_formatting_elements(): bool {
50945094
$count = $this->state->active_formatting_elements->count();
50955095
/*
5096-
* > If there are no entries in the list of active formatting elements, then there is nothing
5097-
* > to reconstruct; stop this algorithm.
5096+
* > 1. If there are no entries in the list of active formatting elements,
5097+
* > then there is nothing to reconstruct; stop this algorithm.
50985098
*/
50995099
if ( 0 === $count ) {
51005100
return false;
51015101
}
51025102

5103-
// Start at the last node in the list of active formatting elements.
5104-
$currently_at = $count - 1;
5103+
$currently_at = $count;
51055104
$last_entry = $this->state->active_formatting_elements->at( $currently_at );
5105+
/*
5106+
* > 2. If the last (most recently added) entry in the list of active formatting
5107+
* > elements is a marker, or if it is an element that is in the stack of open
5108+
* > elements, then there is nothing to reconstruct; stop this algorithm.
5109+
*/
51065110
if (
5107-
5108-
/*
5109-
* > If the last (most recently added) entry in the list of active formatting elements is a marker;
5110-
* > stop this algorithm.
5111-
*/
51125111
'marker' === $last_entry->node_name ||
5113-
5114-
/*
5115-
* > If the last (most recently added) entry in the list of active formatting elements is an
5116-
* > element that is in the stack of open elements, then there is nothing to reconstruct;
5117-
* > stop this algorithm.
5118-
*/
51195112
$this->state->stack_of_open_elements->contains_node( $last_entry )
51205113
) {
51215114
return false;
51225115
}
51235116

5117+
/*
5118+
* > 3. Let entry be the last (most recently added) element
5119+
* > in the list of active formatting elements.
5120+
*/
51245121
$entry = $last_entry;
51255122

5126-
while ( $currently_at >= 0 ) {
5127-
if ( 0 === $currently_at ) {
5128-
goto create;
5129-
}
5130-
$entry = $this->state->active_formatting_elements->at( --$currently_at );
5123+
/*
5124+
* > 4. Rewind: If there are no entries before entry in the list of active
5125+
* > formatting elements, then jump to the step labeled create.
5126+
*/
5127+
rewind:
5128+
if ( 1 === $currently_at ) {
5129+
goto create;
5130+
}
51315131

5132-
/*
5133-
* > If entry is neither a marker nor an element that is also in the stack of open elements,
5134-
* > go to the step labeled rewind.
5135-
*/
5136-
if ( 'marker' === $entry->node_name || $this->state->stack_of_open_elements->contains_node( $entry ) ) {
5137-
break;
5138-
}
5132+
/*
5133+
* > 5. Let entry be the entry one earlier than entry
5134+
* > in the list of active formatting elements.
5135+
*/
5136+
$entry = $this->state->active_formatting_elements->at( --$currently_at );
5137+
5138+
/*
5139+
* > 6. If entry is neither a marker nor an element that is also in
5140+
* > the stack of open elements, go to the step labeled rewind.
5141+
*/
5142+
if (
5143+
'marker' !== $entry->node_name &&
5144+
! $this->state->stack_of_open_elements->contains_node( $entry )
5145+
) {
5146+
goto rewind;
51395147
}
51405148

5149+
/*
5150+
* > 7. Advance: Let entry be the element one later than entry
5151+
* > in the list of active formatting elements.
5152+
*/
51415153
advance:
51425154
$entry = $this->state->active_formatting_elements->at( ++$currently_at );
51435155

5156+
/*
5157+
* > 8. Create: Insert an HTML element for the token for which the
5158+
* > element entry was created, to obtain new element.
5159+
*/
51445160
create:
51455161
$this->insert_html_element( $entry );
51465162

51475163
/*
5148-
* > Replace the entry for entry in the list with an entry for new element.
5149-
* This doesn't need to happen here since no DOM is being created.
5164+
* > 9. Replace the entry for entry in the list with an entry for new element.
5165+
* > This doesn't need to happen here since no DOM is being created.
51505166
*/
51515167

5152-
if ( $count - 1 !== $currently_at ) {
5168+
/*
5169+
* > 10. If the entry for new element in the list of active formatting elements
5170+
* > is not the last entry in the list, return to the step labeled advance.
5171+
*/
5172+
if ( $count !== $currently_at ) {
51535173
goto advance;
51545174
}
51555175

0 commit comments

Comments
 (0)