Skip to content

Optimization Detective: Handle stray closing P tags when P is not open - #2653

Closed
wprashed wants to merge 2 commits into
WordPress:trunkfrom
wprashed:fix/2650-od-stray-closing-p-tag
Closed

Optimization Detective: Handle stray closing P tags when P is not open#2653
wprashed wants to merge 2 commits into
WordPress:trunkfrom
wprashed:fix/2650-od-stray-closing-p-tag

Conversation

@wprashed

Copy link
Copy Markdown

Fixes #2650

Description

When wpautop() wraps multiple shortcodes (e.g. <p><figure>...</figure><figure>...</figure></p>), encountering the first <figure> causes OD_HTML_Tag_Processor to implicitly pop P off the open tag stack (since FIGURE is in P_CLOSING_TAGS).

When the trailing </p> tag is later encountered, P is no longer at the top of ->open_stack_tags. Previously, OD_HTML_Tag_Processor called array_pop( $this->open_stack_tags ), popping a parent element (such as <div class="entry-content">) off the stack. This corrupted the open tag stack for the rest of the document, emitting warnings and producing incorrect data-od-xpath attributes on subsequent elements.

This PR updates OD_HTML_Tag_Processor to:

  1. Detect when a </p> tag is encountered while P is not at the top of ->open_stack_tags.
  2. Increment $this->open_stack_indices[ $level ] to match browser DOM behavior (where an implied empty <p> element occupies a sibling slot).
  3. Return true early without popping parent tags or emitting false warnings.

Testing Instructions

  1. Review plugins/optimization-detective/class-od-html-tag-processor.php and plugins/optimization-detective/tests/test-class-od-html-tag-processor.php.
  2. Confirm that HTML containing <p><figure>...</figure><figure>...</figure></p> parses without warnings and computes accurate XPath breadcrumbs for subsequent elements.

@wprashed
wprashed requested a review from westonruter as a code owner August 30, 2026 06:54
@github-actions

Copy link
Copy Markdown

The following accounts have interacted with this PR and/or linked issues. I will continue to update these lists as activity occurs. You can also manually ask me to refresh this list by adding the props-bot label.

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: wprashed <wprashed@git.wordpress.org>
Co-authored-by: westonruter <westonruter@git.wordpress.org>

To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook.

@github-actions github-actions Bot added the [Plugin] Optimization Detective Issues for the Optimization Detective plugin label Aug 30, 2026
@westonruter

Copy link
Copy Markdown
Member

There is already an open PR for this: #2651

Please carefully review an issue and any associated PRs before starting your own.

@wprashed
wprashed deleted the fix/2650-od-stray-closing-p-tag branch August 30, 2026 06:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Plugin] Optimization Detective Issues for the Optimization Detective plugin

Projects

None yet

Development

Successfully merging this pull request may close these issues.

HTML Tag Processor trips over wpautop and two caption shortcodes in the same “paragraph”

2 participants