Fix normalize_content() dropping text between shortcodes and entity c… - #1077
Conversation
…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
|
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 If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message. To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
…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.
…sonali195/ai into fix/normalize-content-data-loss
Codecov Report✅ All modified and coverable lines are covered by tests. 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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
dkotter
left a comment
There was a problem hiding this comment.
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.
…sonali195/ai into fix/normalize-content-data-loss
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
|
Good catch, thanks. Moved |
…his ensures we don't remove valid content but we prevent prompt injection. Fix some shortcode mangling
What?
Fixes
normalize_content()deleting parts of post content before it is sent to AI features.Why?
get_post_context()passes post content throughnormalize_content(), which had two bugs:#\[.+\](.+)\[/.+\]#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] outrobecameIntro Second outro. Plain brackets like[1]also triggered it.the_contenttexturizes quotes into’and similar, sodon'tbecamedontandR&DbecameRD.How?
[name ...]…[/name]), non-greedily, repeating so nested shortcodes unwrap from the inside out.html_entity_decode()afterwp_strip_all_tags(), so escaped markup like<div>stays as literal text; convert non-breaking spaces to regular spaces.<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
npm run wp-env:test startnpm run test:php -- --filter normalize_contentWordPress\AI\normalize_content( 'Intro [note]First[/note] middle [tip]Second[/tip] outro' )and confirm all text is kept.Changelog Entry