Skip to content

Formatting: make get_tag_regex() match tags non-greedily (#26674) - #13037

Closed
gunjanjaswal wants to merge 5 commits into
WordPress:trunkfrom
gunjanjaswal:fix/26674-get-tag-regex-greedy
Closed

Formatting: make get_tag_regex() match tags non-greedily (#26674)#13037
gunjanjaswal wants to merge 5 commits into
WordPress:trunkfrom
gunjanjaswal:fix/26674-get-tag-regex-greedy

Conversation

@gunjanjaswal

Copy link
Copy Markdown

Trac ticket: https://core.trac.wordpress.org/ticket/26674

What

get_tag_regex() builds a pattern with a greedy body match:

<iframe[^<]*(?:>[\s\S]*<\/iframe>|\s*\/>)

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:

$content = '<iframe src="a"></iframe> text <iframe src="b"></iframe>';
preg_match_all( '#' . get_tag_regex( 'iframe' ) . '#', $content, $m );
// $m[0] currently has ONE entry containing both iframes and the text between.

Fix

Make the attribute and body matches lazy:

<iframe[^<]*?(?:>[\s\S]*?<\/iframe>|\s*\/>)

This is the same pattern core already uses inline in get_media_embedded_in_content() ([^<]*?[\s\S]*?), so this really just brings the standalone get_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 adds tests/phpunit/tests/functions/getTagRegex.php covering 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 in get_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.

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.
@github-actions

github-actions Bot commented Aug 13, 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.

Core Committers: Use this line as a base for the props when committing in SVN:

Props gunjanjaswal, afercia.

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

@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The 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

  • All changes will be lost when closing a tab with a Playground instance.
  • All changes will be lost when refreshing the page.
  • A fresh instance is created each time the link below is clicked.
  • Every time this pull request is updated, a new ZIP file containing all changes is created. If changes are not reflected in the Playground instance,
    it's possible that the most recent build failed, or has not completed. Check the list of workflow runs to be sure.

For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation.

Test this pull request with WordPress Playground.

…space.

Per review on #26674: an iframe cannot self-close, so exercise the
self-closing branch with an input, both '<input />' and '<input/>'.
@afercia afercia added the props-bot Adding this label triggers the Props Bot workflow for a PR. label Aug 21, 2026
@github-actions github-actions Bot removed the props-bot Adding this label triggers the Props Bot workflow for a PR. label Aug 21, 2026
return array(
'a single tag with a body' => array(
'iframe',
'<iframe src="https://example.com/a"></iframe>',

@afercia afercia Aug 21, 2026

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

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.

gunjanjaswal and others added 2 commits August 21, 2026 17:09
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.
pento pushed a commit that referenced this pull request Aug 21, 2026
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
@afercia

afercia commented Aug 21, 2026

Copy link
Copy Markdown
Member

Fixed in https://core.trac.wordpress.org/changeset/63331, where the commit message missed the # before the ticket number.

@afercia afercia closed this Aug 21, 2026
markjaquith pushed a commit to markjaquith/WordPress that referenced this pull request Aug 21, 2026
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
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