Skip to content

HTML API: Refactor wp_strip_all_tags() - #6196

Open
dmsnell wants to merge 2 commits into
WordPress:trunkfrom
dmsnell:html-api/refactor-wp-strip-all-tags
Open

HTML API: Refactor wp_strip_all_tags()#6196
dmsnell wants to merge 2 commits into
WordPress:trunkfrom
dmsnell:html-api/refactor-wp-strip-all-tags

Conversation

@dmsnell

@dmsnell dmsnell commented Feb 27, 2024

Copy link
Copy Markdown
Member

Trac ticket: Core-65984.

Rewrites the logic of wp_strip_all_tags() to rely on the HTML API rather than on strip_tags(), due to the challenges strip_tags() has with properly parsing HTML. The new version of the function returns renderable text nodes, normalized and re-escaped for more predictable inputs into downstream parsers.

Notable changes

  • The contents of SCRIPT and STYLE tags used to be lifted out of the elements, which led to data corruption, as these contents are not intended to be renderable with the rest of the text nodes. These are no longer included in the function outputs; the same is true for the “special atomic elements” defined by the HTML API (IFRAME, TEXTAREA, TITLE, etc…)
  • Text contents are decoded and then re-encoded to normalize the use of character references.
  • wp_html_excerpt() previously truncated early when the number of characters requested fell inside an HTML character reference, but since character references are now decoded before truncating, the function should more reliably return a string of the requested count of code points.
  • When generating the CSS for font faces, instead of running the entire generated CSS snippet through wp_strip_all_tags(), each declaration runs its property and values separately through wp_strip_all_tags(). This removes some corruption that occurred when there were spans of text resembling incomplete HTML syntax tokens.

@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

  • The Plugin and Theme Directories cannot be accessed within Playground.
  • 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.

@dmsnell
dmsnell force-pushed the html-api/refactor-wp-strip-all-tags branch from df2ca53 to 5837af5 Compare August 28, 2026 03:10
@dmsnell
dmsnell force-pushed the html-api/refactor-wp-strip-all-tags branch 14 times, most recently from f46f459 to 9fdccc4 Compare August 29, 2026 01:21
@dmsnell
dmsnell marked this pull request as ready for review August 29, 2026 01:37
@github-actions

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 dmsnell.

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

@dmsnell
dmsnell requested a review from sirreal August 29, 2026 01:51
Trac ticket: Core-65984

Replaces the internal logic of `wp_strip_all_tags()` away from
`strip_tags()` and relies on the HTML API instead. The contract
of the function changes subtly with this: instead of returning
the raw non-tag content of an HTML document, this returns the
normalized text nodes that are part of the renderable page, if
CSS properties are ignored (e.g. those which change visibility).
@dmsnell
dmsnell force-pushed the html-api/refactor-wp-strip-all-tags branch from 9fdccc4 to 866fc80 Compare August 29, 2026 19:03
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.

1 participant