Skip to content

Fix normalize_content() dropping text between shortcodes and entity c… - #1077

Merged
dkotter merged 14 commits into
WordPress:developfrom
sonali195:fix/normalize-content-data-loss
Oct 5, 2026
Merged

dkotter merged 14 commits into
WordPress:developfrom
sonali195:fix/normalize-content-data-loss

Conversation

@sonali195

@sonali195 sonali195 commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

What?

Fixes normalize_content() deleting parts of post content before it is sent to AI features.

Why?

get_post_context() passes post content through normalize_content(), which had two bugs:

  1. The shortcode regex #\[.+\](.+)\[/.+\]# was greedy and ran after newlines were removed, so it matched from the first [ to the last ] in the whole post.
    Intro [note]First[/note] middle text [tip]Second[/tip] outro became Intro Second outro. Plain brackets like [1] also triggered it.
  2. HTML entities were deleted instead of decoded. the_content texturizes quotes into ’ and similar, so don't became dont and R&D became RD.

How?

  • Unwrap only a shortcode whose opening tag matches its own closing tag ([name ...]…[/name]), non-greedily, repeating so nested shortcodes unwrap from the inside out.
  • Decode entities with html_entity_decode() after wp_strip_all_tags(), so escaped markup like <div> stays as literal text; convert non-breaking spaces to regular spaces.
  • Make the <br> replacement case-insensitive and accept <br />.

The function signature and both filters are unchanged.

Use of AI Tools

AI assistance: Yes
Tool(s): Claude
Used for: Finding the bug, drafting the fix and regression tests. I reviewed the changes and take responsibility for them.

Testing Instructions

  1. npm run wp-env:test start
  2. npm run test:php -- --filter normalize_content
  3. Or call WordPress\AI\normalize_content( 'Intro [note]First[/note] middle [tip]Second[/tip] outro' ) and confirm all text is kept.

Changelog Entry

Fixed - Preserve HTML entities and text between shortcodes when normalizing content, while still encoding entity-encoded tags.

Open WordPress Playground Preview

…haracters

The shortcode regex was greedy and matched from the first '[' to the last ']' in the post, deleting everything in between. HTML entities were deleted instead of decoded, so texturized apostrophes/quotes and '&' were lost.

- Unwrap only matching [name]...[/name] pairs, non-greedily, nested from the inside out.
- Decode entities after tag stripping; turn non-breaking spaces into spaces.
- Make the <br> replacement case-insensitive.
- Add regression tests.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Claude-Session: https://claude.ai/code/session_01Hm5dZXVYv7JGgiEFAFGNyi
@sonali195
sonali195 requested a review from a team September 30, 2026 02:46
@github-actions

github-actions Bot commented Sep 30, 2026 •

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: sonali195 <[email protected]>
Co-authored-by: dkotter <[email protected]>

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

sonali195 and others added 3 commits September 30, 2026 08:23
…haracters

Fix normalize_content() dropping text between shortcodes and entity characters

The shortcode regex was greedy and matched from the first '[' to the last ']'
in the post, deleting everything in between. HTML entities were deleted
instead of decoded, so apostrophes, quotes and '&' were lost.

- Unwrap only matching [name]...[/name] pairs, non-greedily, nested from the inside out.
- Decode entities after tag stripping; convert non-breaking spaces to regular spaces.
- Make the <br> replacement case-insensitive.
- Add regression tests.
@codecov

codecov Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 81.59%. Comparing base (f3f2bd2) to head (ef5206e).

Additional details and impacted files
@@            Coverage Diff             @@
##             develop    #1077   +/-   ##
==========================================
  Coverage      81.58%   81.59%           
  Complexity      3081     3081           
==========================================
  Files            129      129           
  Lines          12275    12281    +6     
==========================================
+ Hits           10015    10021    +6     
  Misses          2260     2260           
Flag Coverage Δ
unit 81.59% <100.00%> (+<0.01%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@dkotter dkotter left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few comments that need action.

In addition, for future reference, would be great to open an Issue first, documenting the actual problem, and then proceeding to the PR stage. While I overall understand the bug, there's really no context in the PR around the actual scenario you're running into where the bug crops up or real-life reproduction steps, making it harder to assess the severity here.

Comment thread includes/helpers.php
Comment thread includes/helpers.php Outdated
Comment thread includes/helpers.php Outdated
Comment thread includes/helpers.php Outdated
@dkotter dkotter added this to the 1.5.0 milestone Sep 30, 2026
sonali195 and others added 6 commits September 30, 2026 18:34
added changes regarding shortcode
Remove unrendered shortcode tags while keeping their inner content
resloved phpcs issue sloved from last commit
- Decode HTML entities before stripping tags so encoded markup is removed
- Simplify inline comments
- Update and add tests for entity-encoded tags
- Add changelog entry
@sonali195

Copy link
Copy Markdown
Contributor Author

Good catch, thanks. Moved html_entity_decode() before wp_strip_all_tags(), so entity-encoded markup like &lt;/block-content&gt; is decoded and then stripped instead of coming back as a real tag. Added test_normalize_content_strips_entity_encoded_tags to cover this, and updated the existing entity test since &lt;test&gt; is now stripped.

@dkotter dkotter modified the milestones: 1.5.0, 1.4.0 Oct 5, 2026
@dkotter
dkotter merged commit aae6de2 into WordPress:develop Oct 5, 2026
36 of 37 checks passed
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