Formatting: make get_tag_regex() match tags non-greedily (#26674) - #13037
Formatting: make get_tag_regex() match tags non-greedily (#26674)#13037gunjanjaswal wants to merge 3 commits into
Conversation
get_tag_regex() built a pattern with a greedy body match, so content with more than one tag of the same type matched from the first opening tag through the last closing tag, merging every tag in between (and the text between them) into a single match. Make the attribute and body matches lazy, aligning get_tag_regex() with the pattern get_media_embedded_in_content() already uses. Adds unit tests for get_tag_regex(), which previously had none, and refreshes the stale patch. Props kopepasah, afercia. Fixes #26674.
|
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 Core Committers: Use this line as a base for the props when committing in SVN: To understand the WordPress project's expectations around crediting contributors, please review the Contributor Attribution page in the Core Handbook. |
Test using WordPress PlaygroundThe changes in this pull request can previewed and tested using a WordPress Playground instance. WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser. Some things to be aware of
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
…space. Per review on #26674: an iframe cannot self-close, so exercise the self-closing branch with an input, both '<input />' and '<input/>'.
Trac ticket: https://core.trac.wordpress.org/ticket/26674
What
get_tag_regex()builds a pattern with a greedy body match:Because
[\s\S]*is greedy, content with more than one tag of the same type matches from the first opening tag all the way to the last closing tag, so both tags — and everything between them — collapse into a single match:Fix
Make the attribute and body matches lazy:
This is the same pattern core already uses inline in
get_media_embedded_in_content()([^<]*?…[\s\S]*?), so this really just brings the standaloneget_tag_regex()back in line with it. With the change, the two iframes above match as two separate entries, and single-tag, multiline-body, and self-closing cases keep matching as before.get_tag_regex()has no callers in core today (get_media_embedded_in_content()carries its own copy of the pattern), but it's a public function since 3.6.0, so the greedy behavior affects anything outside core that relies on it.Tests
get_tag_regex()had no test coverage, so this addstests/phpunit/tests/functions/getTagRegex.phpcovering a single tag with a body, two adjacent tags matched separately (the regression), a self-closing tag, a multiline body, and the no-match and empty-tag cases. The two-adjacent-tags case fails against the current greedy pattern and passes with the fix.Notes
This refreshes the long-stale patch on the ticket (it no longer applied against trunk). I kept the self-closing branch as
\s*\/>to match the pattern inget_media_embedded_in_content(); @afercia's earlier suggestion to also make the slash optional (\s*?\/?>) would broaden what counts as self-closing, so that feels like a separate decision from the greedy fix and I left it out here.Props @kopepasah for the report and original patch, and @afercia for the regex review.