Skip to content

Fix test broken by assertEqualHTML whitespace change in WP 7.0 (core #64531)#1

Merged
juanmaguitar merged 1 commit intomainfrom
fix/assert-equal-html-whitespace-wp70
Feb 23, 2026
Merged

Fix test broken by assertEqualHTML whitespace change in WP 7.0 (core #64531)#1
juanmaguitar merged 1 commit intomainfrom
fix/assert-equal-html-whitespace-wp70

Conversation

@juanmaguitar
Copy link
Copy Markdown
Contributor

What

Fixes test_card_block_renders_with_background_color which started failing on WordPress 7.0.

Why

WordPress core ticket #64531 corrected a bug in build_visual_html_tree() (the function powering assertEqualHTML). The function was incorrectly trimming leading whitespace from text nodes, including whitespace-only text nodes between elements. The fix shipped in WP 6.9.1 and is included in WP 7.0.

Before the fix, this $expected string passed silently:

$expected = '
    <div class="...">
        <p>Hello</p>
    </div>
';

Even though the actual output had no whitespace text nodes between elements, the leading newline and the tabs between <div> and <p> were being stripped from the tree, so the comparison appeared equal.

After the fix, those whitespace-only text nodes are preserved and compared — correctly causing the assertion to fail when the actual output has no such nodes.

The fix

  • Remove the leading newline before <div> in $expected.
  • Remove the whitespace between the closing > of the opening tag and <p>, and between </p> and </div>.
  • Attributes can still span multiple lines for readability — whitespace inside an opening tag is not a text node.

Updated the docblock to document this behaviour as a learning point for readers of these examples.

Also bumps .wp-env.json to wordpress-7.0-beta1 to match the version under test.

Key rule going forward

Whitespace between elements in $expected creates text nodes that must match the actual output. If the render function produces <div><p>...</p></div> (no gaps), the expected string must do the same. Attributes on separate lines are fine.

🤖 Generated with Claude Code

WordPress core ticket #64531 (fixed in 6.9.1, included in 7.0) corrected
build_visual_html_tree() to stop trimming leading whitespace from text nodes.
Whitespace-only text nodes between elements are now preserved and compared.

The test_card_block_renders_with_background_color test had a multi-line
$expected string that started with a newline before <div> and had newlines
and tabs between </div...> and <p>. These became text nodes in the parsed
tree that don't exist in the actual render output, causing the assertion
to fail on WP 7.0.

Fix: remove the leading newline before <div> and close the gap between
the opening tag and <p> so no whitespace text nodes are introduced.
Attributes can still span multiple lines for readability — whitespace
inside a tag is not a text node.

Also bump .wp-env.json to wordpress-7.0-beta1 to reflect the WP version
this fix targets.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@juanmaguitar juanmaguitar merged commit badabdf into main Feb 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants