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 5 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/>'.
| return array( | ||
| 'a single tag with a body' => array( | ||
| 'iframe', | ||
| '<iframe src="https://example.com/a"></iframe>', |
There was a problem hiding this comment.
Would the test be more meaningful if the passed $content contains also other HTML? To my understanding, the original usage of this function was inside get_media_embedded_in_content() which receives $content A string of HTML which might contain media elements.
I would consider to wrap the current$content passed by the data provider within other content, it could be just a simple div with some text. I'd do it for all the cases in the data provider. Thoughts?
There was a problem hiding this comment.
Good point — done. Every case now embeds the element(s) inside a containing <div> with surrounding text, so the data reflects what get_media_embedded_in_content() actually receives rather than a bare element. The expected matches are unchanged, since the regex still pulls out just the media element from the larger string. Pushed in ccb22e6.
Wrap each data provider case in a containing element with text, mirroring get_media_embedded_in_content()'s real input (a string of HTML that may contain media elements) rather than a bare element. Expected matches are unchanged.
Fixes a bug where `get_tag_regex()` may match multiple tags of the same type in the content. Also, adds tests. Developed in #13037 Props pbearne, kopepasah, gunjanjaswal, debarghyabanerjee, mukesh27, desrosj, ocean90, nacin, chriscct7, r1k0, afercia. Fixes 26674. Fixes 59791. git-svn-id: https://develop.svn.wordpress.org/trunk@63331 602fd350-edb4-49c9-b593-d223f7449a82
|
Fixed in https://core.trac.wordpress.org/changeset/63331, where the commit message missed the |
Fixes a bug where `get_tag_regex()` may match multiple tags of the same type in the content. Also, adds tests. Developed in WordPress/wordpress-develop#13037 Props pbearne, kopepasah, gunjanjaswal, debarghyabanerjee, mukesh27, desrosj, ocean90, nacin, chriscct7, r1k0, afercia. Fixes 26674. Fixes 59791. Built from https://develop.svn.wordpress.org/trunk@63331 git-svn-id: http://core.svn.wordpress.org/trunk@62524 1a063a9b-81f0-0310-95a4-ce76da25c4cd
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.