Skip to content

Commit 962a7e8

Browse files
westonruterclaude
andcommitted
HTML API: Remove the outer loop the adoption agency algorithm never repeats.
`WP_HTML_Processor::run_adoption_agency_algorithm()` wrapped its steps in a loop with a budget and an outer loop counter, mirroring the spec. But only the first pass of the algorithm is implemented, and every path through it either returns or calls `bail()`, which never returns. The loop has never run a second time since it was introduced, so its counter checks were constant and the `bail()` after it was unreachable. PHPStan 2.3 now reports both comparisons as constant, which had been baselined as `greater.alwaysTrue` and `greaterOrEqual.alwaysFalse`. The loop, its budget, and the unreachable `bail()` are removed, and the body runs once as it always has. A comment marks where the spec's outer loop belongs, to be restored along with the steps that would start another pass. Both baselines are deleted, and the `deadCode.unreachable` count for the file drops by one for the removed `bail()`. The html-api and html-api-web-platform-tests groups pass, with the same 1725 tests and 449 skips as before the change. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
1 parent ec8bf88 commit 962a7e8

5 files changed

Lines changed: 67 additions & 120 deletions

File tree

‎phpstan.neon.dist‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@ includes:
4242
- tests/phpstan/baselines/function.alreadyNarrowedType.neon
4343
- tests/phpstan/baselines/function.impossibleType.neon
4444
- tests/phpstan/baselines/function.resultUnused.neon
45-
- tests/phpstan/baselines/greater.alwaysTrue.neon
46-
- tests/phpstan/baselines/greaterOrEqual.alwaysFalse.neon
4745
- tests/phpstan/baselines/greaterOrEqual.alwaysTrue.neon
4846
- tests/phpstan/baselines/identical.alwaysFalse.neon
4947
- tests/phpstan/baselines/identical.alwaysTrue.neon

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

Lines changed: 66 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -6096,7 +6096,6 @@ private function reset_insertion_mode_appropriately(): void {
60966096
* @see https://html.spec.whatwg.org/#adoption-agency-algorithm
60976097
*/
60986098
private function run_adoption_agency_algorithm(): void {
6099-
$budget = 1000;
61006099
$subject = $this->get_tag();
61016100
$current_node = $this->state->stack_of_open_elements->current_node();
61026101

@@ -6110,88 +6109,88 @@ private function run_adoption_agency_algorithm(): void {
61106109
return;
61116110
}
61126111

6113-
$outer_loop_counter = 0;
6114-
while ( $budget-- > 0 ) {
6115-
if ( $outer_loop_counter++ >= 8 ) {
6116-
return;
6117-
}
6118-
6119-
/*
6120-
* > Let formatting element be the last element in the list of active formatting elements that:
6121-
* > - is between the end of the list and the last marker in the list,
6122-
* > if any, or the start of the list otherwise,
6123-
* > - and has the tag name subject.
6124-
*/
6125-
$formatting_element = null;
6126-
foreach ( $this->state->active_formatting_elements->walk_up() as $item ) {
6127-
if ( 'marker' === $item->node_name ) {
6128-
break;
6129-
}
6112+
/*
6113+
* > Let outer loop counter be 0.
6114+
* > Outer loop: If outer loop counter is greater than or equal to 8, then return.
6115+
*
6116+
* Only the first pass through the outer loop is supported. Every path through it
6117+
* either returns or bails before reaching the steps that would start another pass,
6118+
* so the loop and its counter are left out until those steps are implemented.
6119+
*/
61306120

6131-
if ( $subject === $item->node_name ) {
6132-
$formatting_element = $item;
6133-
break;
6134-
}
6121+
/*
6122+
* > Let formatting element be the last element in the list of active formatting elements that:
6123+
* > - is between the end of the list and the last marker in the list,
6124+
* > if any, or the start of the list otherwise,
6125+
* > - and has the tag name subject.
6126+
*/
6127+
$formatting_element = null;
6128+
foreach ( $this->state->active_formatting_elements->walk_up() as $item ) {
6129+
if ( 'marker' === $item->node_name ) {
6130+
break;
61356131
}
61366132

6137-
// > If there is no such element, then return and instead act as described in the "any other end tag" entry above.
6138-
if ( null === $formatting_element ) {
6139-
$this->bail( 'Cannot run adoption agency when "any other end tag" is required.' );
6133+
if ( $subject === $item->node_name ) {
6134+
$formatting_element = $item;
6135+
break;
61406136
}
6137+
}
61416138

6142-
// > If formatting element is not in the stack of open elements, then this is a parse error; remove the element from the list, and return.
6143-
if ( ! $this->state->stack_of_open_elements->contains_node( $formatting_element ) ) {
6144-
$this->state->active_formatting_elements->remove_node( $formatting_element );
6145-
return;
6146-
}
6139+
// > If there is no such element, then return and instead act as described in the "any other end tag" entry above.
6140+
if ( null === $formatting_element ) {
6141+
$this->bail( 'Cannot run adoption agency when "any other end tag" is required.' );
6142+
}
61476143

6148-
// > If formatting element is in the stack of open elements, but the element is not in scope, then this is a parse error; return.
6149-
if ( ! $this->state->stack_of_open_elements->has_element_in_scope( $formatting_element->node_name ) ) {
6150-
return;
6151-
}
6144+
// > If formatting element is not in the stack of open elements, then this is a parse error; remove the element from the list, and return.
6145+
if ( ! $this->state->stack_of_open_elements->contains_node( $formatting_element ) ) {
6146+
$this->state->active_formatting_elements->remove_node( $formatting_element );
6147+
return;
6148+
}
61526149

6153-
/*
6154-
* > Let furthest block be the topmost node in the stack of open elements that is lower in the stack
6155-
* > than formatting element, and is an element in the special category. There might not be one.
6156-
*/
6157-
$is_above_formatting_element = true;
6158-
$furthest_block = null;
6159-
foreach ( $this->state->stack_of_open_elements->walk_down() as $item ) {
6160-
if ( $is_above_formatting_element && $formatting_element->bookmark_name !== $item->bookmark_name ) {
6161-
continue;
6162-
}
6150+
// > If formatting element is in the stack of open elements, but the element is not in scope, then this is a parse error; return.
6151+
if ( ! $this->state->stack_of_open_elements->has_element_in_scope( $formatting_element->node_name ) ) {
6152+
return;
6153+
}
61636154

6164-
if ( $is_above_formatting_element ) {
6165-
$is_above_formatting_element = false;
6166-
continue;
6167-
}
6155+
/*
6156+
* > Let furthest block be the topmost node in the stack of open elements that is lower in the stack
6157+
* > than formatting element, and is an element in the special category. There might not be one.
6158+
*/
6159+
$is_above_formatting_element = true;
6160+
$furthest_block = null;
6161+
foreach ( $this->state->stack_of_open_elements->walk_down() as $item ) {
6162+
if ( $is_above_formatting_element && $formatting_element->bookmark_name !== $item->bookmark_name ) {
6163+
continue;
6164+
}
61686165

6169-
if ( self::is_special( $item ) ) {
6170-
$furthest_block = $item;
6171-
break;
6172-
}
6166+
if ( $is_above_formatting_element ) {
6167+
$is_above_formatting_element = false;
6168+
continue;
61736169
}
61746170

6175-
/*
6176-
* > If there is no furthest block, then the UA must first pop all the nodes from the bottom of the
6177-
* > stack of open elements, from the current node up to and including formatting element, then
6178-
* > remove formatting element from the list of active formatting elements, and finally return.
6179-
*/
6180-
if ( null === $furthest_block ) {
6181-
foreach ( $this->state->stack_of_open_elements->walk_up() as $item ) {
6182-
$this->state->stack_of_open_elements->pop();
6171+
if ( self::is_special( $item ) ) {
6172+
$furthest_block = $item;
6173+
break;
6174+
}
6175+
}
61836176

6184-
if ( $formatting_element->bookmark_name === $item->bookmark_name ) {
6185-
$this->state->active_formatting_elements->remove_node( $formatting_element );
6186-
return;
6187-
}
6177+
/*
6178+
* > If there is no furthest block, then the UA must first pop all the nodes from the bottom of the
6179+
* > stack of open elements, from the current node up to and including formatting element, then
6180+
* > remove formatting element from the list of active formatting elements, and finally return.
6181+
*/
6182+
if ( null === $furthest_block ) {
6183+
foreach ( $this->state->stack_of_open_elements->walk_up() as $item ) {
6184+
$this->state->stack_of_open_elements->pop();
6185+
6186+
if ( $formatting_element->bookmark_name === $item->bookmark_name ) {
6187+
$this->state->active_formatting_elements->remove_node( $formatting_element );
6188+
return;
61886189
}
61896190
}
6190-
6191-
$this->bail( 'Cannot extract common ancestor in adoption agency algorithm.' );
61926191
}
61936192

6194-
$this->bail( 'Cannot run adoption agency when looping required.' );
6193+
$this->bail( 'Cannot extract common ancestor in adoption agency algorithm.' );
61956194
}
61966195

61976196
/**

‎tests/phpstan/baselines/deadCode.unreachable.neon‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ parameters:
5656
-
5757
message: '#^Unreachable statement \- code above always terminates\.$#'
5858
identifier: deadCode.unreachable
59-
count: 31
59+
count: 30
6060
path: ../../../src/wp-includes/html-api/class-wp-html-processor.php
6161
-
6262
message: '#^Unreachable statement \- code above always terminates\.$#'

‎tests/phpstan/baselines/greater.alwaysTrue.neon‎

Lines changed: 0 additions & 25 deletions
This file was deleted.

‎tests/phpstan/baselines/greaterOrEqual.alwaysFalse.neon‎

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)