Title: html-api | Make WordPress Core

---

#  Tag Archives: html-api

 [  ](https://profiles.wordpress.org/dmsnell/) [Dennis Snell](https://profiles.wordpress.org/dmsnell/)
7:57 pm _on_ October 7, 2026     
Tags: html-api, [html-api-progress-report ( 2 )](https://make.wordpress.org/core/tag/html-api-progress-report/),
progress-report   

# 󠀁[Progress Report: `wp_kses()`](https://make.wordpress.org/core/2026/10/07/progress-report-wp_kses/)󠁿

Since its inception, WordPress has relied on the KSES^([1](https://make.wordpress.org/core/tag/html-api/?output_format=md#fn-1e84fd31-badc-41d8-8b6e-2aae1aae060a))
subsystem to sanitize HTMLHTML HyperText Markup Language. The semantic scripting
language primarily used for outputting content in web browsers.. Its purpose has
grown and changed over time, but primarily serves two purposes: correct aesthetic
defects that are likely the result of typos or pasting errors; and remove security
risks that might appear in untrusted inputs. The original `kses` library was written
before HTML5 fully-formalized HTML parsing and at a time when it was common to “
hand type” HTML. Today, however, there is no such thing as “seriously malformed^(
[2](https://make.wordpress.org/core/tag/html-api/?output_format=md#fn-e7609e07-da47-43de-9f5a-b8815311246b))”
HTML and most people author content through rich editors, such as WordPress’ BlockBlock
Block is the abstract term used to describe units of markup that, composed together,
form the content or layout of a webpage using the WordPress editor. The idea combines
concepts of what in the past may have achieved with shortcodes, custom HTML, and
embed discovery into a single consistent API and user experience. editor. This means
that today, the primary purpose of `wp_kses()` is to prune out risks from untrusted
inputs, and it’s time for it to get an overhaul.

As part of [#66208](https://core.trac.wordpress.org/ticket/66208), [PR#13271](https://github.com/WordPress/wordpress-develop/pull/13271)
replaced the implementation of `wp_kses()` with a new one based on the [HTML API](https://make.wordpress.org/core/components/html-api/).
This update replaces a complicated chain of functions, string processing, PCRE regular
expressions, and outdated logic to ensure that WordPress can perform its sanitization
role reliably and comprehensibly.

 [Continue reading →](https://make.wordpress.org/core/2026/10/07/progress-report-wp_kses/#more-126106)

[#html-api](https://make.wordpress.org/core/tag/html-api/), [#html-api-progress-report](https://make.wordpress.org/core/tag/html-api-progress-report/),
[#progress-report](https://make.wordpress.org/core/tag/progress-report/)

 * [Login to Reply](https://login.wordpress.org/?redirect_to=https%3A%2F%2Fmake.wordpress.org%2Fcore%2F2026%2F10%2F07%2Fprogress-report-wp_kses%2F%23respond&locale=en_US)

 [  ](https://profiles.wordpress.org/dmsnell/) [Dennis Snell](https://profiles.wordpress.org/dmsnell/)
5:08 pm _on_ November 21, 2025     
Tags: [6-9 ( 87 )](https://make.wordpress.org/core/tag/6-9/),
[dev-notes ( 654 )](https://make.wordpress.org/core/tag/dev-notes/), [dev-notes-6-9 ( 25 )](https://make.wordpress.org/core/tag/dev-notes-6-9/),
html-api   

# 󠀁[Updates to the HTML API in 6.9](https://make.wordpress.org/core/2025/11/21/updates-to-the-html-api-in-6-9/)󠁿

WordPress 6.9 brings an abundance of quiet improvements to the HTMLHTML HyperText
Markup Language. The semantic scripting language primarily used for outputting content
in web browsers. APIAPI An API or Application Programming Interface is a software
intermediary that allows programs to interact with each other and share data in 
limited, clearly defined ways.. Updates in this release mostly represent applications
of the HTML API to existing code in CoreCore Core is the set of software required
to run WordPress. The Core Development Team builds WordPress.; these updates increase
WordPress’ reliability, improve its security hardening, and reduce maintenance burden
on the project.

## Major Updates

### `WP_HTML_Processor​::​serialize_token()` is now public.

The HTML Processor’s `serialize_token()` method returns a fully-normalized and well-
formed representation of the currently-matched token. It was introduced in [#62036](https://core.trac.wordpress.org/ticket/62036)
for WordPress 6.7 as a private method which performs the heavy-lifting for how the
HTML API turns “junk” inputs into equivalent well-formed outputs. For example:

    ```php
    $html = '5 < 8 & <tag a=v a="dup"id=di></3>bl&#97rg';
    echo WP_HTML_Processor::normalize( $html );
    // 5 &lt; 8 &amp; <tag a="v" id="di"><!--3-->blarg</tag>
    ```

Its value outside of `WP_HTML_Processor::normalize()` became evident, however, particularly
in the creation of “serialization builders^([1](https://make.wordpress.org/core/tag/html-api/?output_format=md#5810a71a-cd37-4821-86e7-4354e6977819))”
which make it possible to modify more of the HTML structure than the HTML Processor
itself does. In typical HTML API loops, this method can be used to partially extract
portions of the document safely:

    ```php
    // Extract the outerHTML of every paragraph element.
    $processor = WP_HTML_Processor::create_fragment( $html );
    $content   = '';
    while ( $processor->next_tag( 'P' ) ) {
        $content .= $processor->serialize_token();
        $depth    = $processor->get_current_depth();
        while (
            $processor->next_token() &&
            $processor->get_current_depth() > $depth
        ) {
            $content .= $processor->serialize_token();
        }
        $content .= $processor->serialize_token();
        $content .= "\n\n";
    }
    ```

### WordPress understands JavaScriptJavaScript JavaScript or JS is an object-oriented computer programming language commonly used to create interactive effects within web browsers. WordPress makes extensive use of JS for a better user experience. While PHP is executed on the server, JS executes within a user’s browser. 󠀁[https://www.javascript.com](https://www.javascript.com/)󠁿 `.dataset` properties.

HTML provides a convenient mechanism tying HTML and JavaScript together through 
the [custom data attributes](https://developer.mozilla.org/en-US/docs/Web/HTML/Reference/Global_attributes/data-*)
on a tagtag A directory in Subversion. WordPress uses tags to store a single snapshot
of a version (3.6, 3.6.1, etc.), the common convention of tags in version control
systems. (Not to be confused with post tags.). These are the attributes starting
with `data-` like `data‑wp‑interactive` or `data‑post‑id` and their values are [available on the corresponding Element](https://software.hixie.ch/utilities/js/live-dom-viewer/?%3Cspan%20data-order%3D%22Carrots%20please!%22%3EWhat%20should%20we%20order%3F%3C%2Fspan%3E%0A%3Cscript%3E%0Adocument.body.addEventListener(%20%27click%27%2C%20event%20%3D%3E%20alert(%20event.target.dataset.order%20)%20)%3B%0A%3C%2Fscript%3E)
object in JavaScript through the `.dataset` property:

    ```language-markup
    <span data-order="Carrots please!">
        What should we order?
    </span>
    <script>
    document.body.addEventListener(
        'click',
        event => alert( event.target.dataset.order )
    );
    </script>
    ```

There are endless ways this integration can be used to add a level of dynamism to
a site. Unfortunately, _how_ the name of these attributes is transformed looks simpler
than it is. For example, the `data‑wp‑bind‑‑class` HTML attribute corresponds to
the `wpBind‑Class` dataset property.

To prevent confusion, WordPress 6.9 includes two new functions to map between the
HTML and JavaScript names: `wp_js_dataset_name()` indicates what would appear on
the `.dataset` property in a browser while `wp_html_custom_data_attribute_name()`
indicates what name should be used in HTML to produce the `.dataset` property of
a given name. For example:

    ```php
    // What would this HTML attribute name correspond to in JavaScript?
    echo wp_js_dataset_name( 'data-one-two--three---four' );
    // oneTwo-Three--Four

    // What HTML attribute name is necessary to produce the given JavaScript name?
    echo wp_html_custom_data_attribute_name( 'postId.guid' );
    // data-post-id.guid
    ```

### No more hard-coding HTML string assertions in unit tests.

WordPress is full of unit tests asserting specific HTML transformations. The expected
outputs for these tests are usually hard-coded and sent to `$this->assertSame()`
to compare against the actual outputs from the code under test. Unfortunately this
tends to produce a high rate of false positives because of trivialities like adding
an attribute in a different order than was expected, using single-quotes around 
an attribute value rather than double-quotes, leaving extra whitespace or not enough,
or using the mistaken self-closer on an `<img>` or `<br>` tag.

When two HTML strings produce the same result in a browser they should pass regardless
of their insignificant differences. To ease the development of these kinds of tests
and to reduce their false-positive rates, WordPress 6.9 introduces a new method 
on the `WP_UnitTestClass` base class: `$this->assertEqualHTML()`.

This new test assertion verifies that two strings are equivalent representations
of the same normative HTML. They compare HTML strings semantically, provide more
useful output than string comparison when they fail to assert, and they’re even 
aware of blockBlock Block is the abstract term used to describe units of markup 
that, composed together, form the content or layout of a webpage using the WordPress
editor. The idea combines concepts of what in the past may have achieved with shortcodes,
custom HTML, and embed discovery into a single consistent API and user experience.
semantics.

    ```php
    $this->assertEqualHTML(
      "<img src='puppy&period;jpg'   loading=lazy>",
      '<img loading="l&#97zy"src="puppy.jpg"/>'
    );
    ```

    ```wp-block-code
     ✔︎ Is equivalent html

    Time: 00:00.038, Memory: 40.00 MB

    OK (1 test, 1 assertion)
    ```

This test case would pass since the arguments are two equivalent constructions of
the same IMG element. However, a few small changes and it succinctly highlights 
their differences. The addition of the block comment delimiter is for illustrative
purposes only.

    ```php
    $this->assertEqualHTML(
        "<!-- wp:image {\"id\":5} --><img src='puppy.jpg' loading=lazy>",
        '<!-- wp:img {"id":6} --><img loading="lazy" data-priority=5 src=puppy.jpg/>'
    );
    ```

    ```wp-block-code
     ✘ Is equivalent html
       ┐
       ├ HTML markup was not equivalent.
       ├ Failed asserting that two strings are identical.
       ┊ ---·Expected
       ┊ +++·Actual
       ┊ @@ @@
       ┊ -'BLOCK["core/image"]
       ┊ +'BLOCK["core/img"]
       ┊    {
       ┊ -····"id": 5
       ┊ +····"id": 6
       ┊    }
       ┊    <img>
       ┊ +····data-priority="5"
       ┊      loading="lazy"
       ┊ -····src="puppy.jpg"
       ┊ +····src="puppy.jpg/"
       ┊  '
       │
       ╵ /WordPress-develop/tests/phpunit/includes/abstract-testcase.php:1235
       ╵ /WordPress-develop/tests/phpunit/tests/html/equivalentHtmlTest.php:10
       ┴

    Time: 00:00.038, Memory: 40.00 MB
    ```

### The HTML API received minor updates.

 * The Tag Processor’s constructor will now cast `null` to an empty string. Similarly,
   the static creator methods on the HTML Processor will return `null` instead of
   an instance of the `WP_HTML_Processor` class. In each case a `_doing_it_wrong()`
   notice will alert developers that these classes expect a string input. This change
   prevents burying the type errors, which leads to unexpected crashes later on,
   such as when calling `get_updated_html()`.
 * When calling `set_modifiable_text()` on a SCRIPT element, updates are rejected
   if they contain `<script` or `</script` in them. This is a conservative measure
   to avoid entering the [script data double escaped state](https://sirre.al/2025/08/06/safe-json-in-script-tags-how-not-to-break-a-site/)(
   personal blogblog (versus network, site)) which is prone to misinterpretation.

## Full Changelog

### Enhancements

 * `wp_js_dataset_name()` and `wp_html_custom_data_attribute_name()` map between
   HTML attributes and the `.dataset` property in JavaScript. [[#61501](https://core.trac.wordpress.org/ticket/61501),
   [PR#9953](https://github.com/WordPress/wordpress-develop/pull/9953)]
 * The `WP_UnitTestClass` now contains an `assertEqualHTML()` method which determines
   if two strings represent the same normative HTML. [[#63527](https://core.trac.wordpress.org/ticket/63527),
   [PR#8882](https://github.com/WordPress/wordpress-develop/pull/8882)]
 * Multiple length checks are safely skipped when processing SCRIPT content due 
   to an early minimum-length check. [[#63738](https://core.trac.wordpress.org/ticket/63738),
   [PR#9230](https://github.com/WordPress/wordpress-develop/pull/9230)]
 * Encoding detection in METAMeta Meta is a term that refers to the inside workings
   of a group. For us, this is the team that works on internal WordPress sites like
   WordCamp Central and Make WordPress. tags is simplified, leading to a minor performance
   lift. [[#63738](https://core.trac.wordpress.org/ticket/63738), [PR#9231](https://github.com/WordPress/wordpress-develop/pull/9231)]
 * `WP_HTML_Processor::serialize_token()` is now _public_, making it easier to mix
   the full safety of the HTML API with outside code modifying and combining HTML.[
   [#63823](https://core.trac.wordpress.org/ticket/63823), [PR#9456](https://github.com/WordPress/wordpress-develop/pull/9456)]
 * The Tag Processor and HTML Processor handle invalidinvalid A resolution on the
   bug tracker (and generally common in software development, sometimes also _notabug_)
   that indicates the ticket is not a bug, is a support request, or is generally
   invalid. `null` inputs safely. [[#63854](https://core.trac.wordpress.org/ticket/63854),
   [PR#9545](https://github.com/WordPress/wordpress-develop/pull/9545)]
 * `set_modifiable_text()` rejects additional contents inside a SCRIPT element when
   the contents could disturb its normal closing. [[#63738](https://core.trac.wordpress.org/ticket/63738),
   [PR#9560](https://github.com/WordPress/wordpress-develop/pull/9560)]

### Bug Fixes

 * Attempting to avoid the [HTTP _Referer_ problem](https://en.wikipedia.org/wiki/HTTP_referer#Etymology),
   quirks mode is referred to as `indicated_compatibility_mode`. [[#63391](https://core.trac.wordpress.org/ticket/63391),
   [PR#9401](https://github.com/WordPress/wordpress-develop/pull/9401)]
 * `wp_kses()` no longer unescapes escaped numeric character references for users
   without `unfiltered_html`, preserving more of the actual entered content in a
   post or comment. [[#63630](https://core.trac.wordpress.org/ticket/63630), [PR#9099](https://github.com/WordPress/wordpress-develop/pull/9099)]
 * SCRIPT tags are properly closed in the presence of _abruptly-closed HTML comments_
   within the contents, and when the closing SCRIPT tag’s tag name is delimited 
   by a form-feed. [[#63738](https://core.trac.wordpress.org/ticket/63738), [PR#9397](https://github.com/WordPress/wordpress-develop/pull/9397)]
 * `wp_kses()` now allows some previously-missing HTML5 semantic tags and their 
   attributes. [[#63786](https://core.trac.wordpress.org/ticket/63786), [PR#9379](https://github.com/WordPress/wordpress-develop/pull/9379)]
 * `set_attribute()` directly escapes syntax characters into HTML character references
   to avoid problems with double-escaping logic. This ensures that all values are
   represented accurately in the resulting HTML. [[#64054](https://core.trac.wordpress.org/ticket/64054),
   [PR#10143](https://github.com/WordPress/wordpress-develop/pull/10143)]

### Core refactors

A number of places in Core were updated to benefit from the HTML API.

 * Several of the unit tests now rely on `assertEqualHTML()`, including for block
   supports, `wp_rel_nofollow()`, `wp_rel_ugc()`, `wp_kses`, post-filtering, media,
   oEmbed filtering. [[#59622](https://core.trac.wordpress.org/ticket/59622), [#63694](https://core.trac.wordpress.org/ticket/63694),
   [PR#5486](https://github.com/WordPress/wordpress-develop/pull/5486), [PR#9251](https://github.com/WordPress/wordpress-develop/pull/9251),
   [PR#9255](https://github.com/WordPress/wordpress-develop/pull/9255), [PR#9257](https://github.com/WordPress/wordpress-develop/pull/9257),
   [PR#9258](https://github.com/WordPress/wordpress-develop/pull/9258), [PR#9259](https://github.com/WordPress/wordpress-develop/pull/9259),
   [PR#9264](https://github.com/WordPress/wordpress-develop/pull/9264)]
 * `get_url_in_content()` relies on the Tag Processor to more reliably detect links.
   Besides improving general HTML parsing, this new version always returns the decoded`
   href` attribute, preventing confusion in downstream code. [[#63694](https://core.trac.wordpress.org/ticket/63694),
   [PR#9272](https://github.com/WordPress/wordpress-develop/pull/9272)]
 * Processing for image blocks in classic themes is now performed via the HTML API
   rather than with PCREs. [[#63694](https://core.trac.wordpress.org/ticket/63694),
   [PR#10218](https://github.com/WordPress/wordpress-develop/pull/10218)]

## Acknowledgements

Props to [@jonsurrell](https://profiles.wordpress.org/jonsurrell/) and [@westonruter](https://profiles.wordpress.org/westonruter/)
for reviewing this post.

 1. Methods to replace `innerHTML` and `outerHTML`, wrap an element, unwrap an element,
    insert elements, and more are possible by scanning through a document and conditionally
    copying the normalized tokens into an output string. [↩︎](https://make.wordpress.org/core/tag/html-api/?output_format=md#5810a71a-cd37-4821-86e7-4354e6977819-link)

[#6-9](https://make.wordpress.org/core/tag/6-9/), [#dev-notes](https://make.wordpress.org/core/tag/dev-notes/),
[#dev-notes-6-9](https://make.wordpress.org/core/tag/dev-notes-6-9/), [#html-api](https://make.wordpress.org/core/tag/html-api/)

 [  ](https://profiles.wordpress.org/dmsnell/) [Dennis Snell](https://profiles.wordpress.org/dmsnell/)
10:27 am _on_ October 17, 2024     
Tags: [dev-notes ( 654 )](https://make.wordpress.org/core/tag/dev-notes/),
[dev-notes-6-7 ( 15 )](https://make.wordpress.org/core/tag/dev-notes-6-7/), html-
api   

# 󠀁[Updates to the HTML API in 6.7](https://make.wordpress.org/core/2024/10/17/updates-to-the-html-api-in-6-7/)󠁿

After important internal [changes to the HTML API in 6.6](https://make.wordpress.org/core/2024/06/24/updates-to-the-html-api-in-6-6/),
WordPress 6.7 brings forward a major leap in support with the HTMLHTML HyperText
Markup Language. The semantic scripting language primarily used for outputting content
in web browsers. Processor.

 1. [Major Updates](https://make.wordpress.org/core/2024/10/17/updates-to-the-html-api-in-6-7/#major-updates)
 2.  a. [Full HTML Support](https://make.wordpress.org/core/2024/10/17/updates-to-the-html-api-in-6-7/#full-html-support)
     b. [Ability to modify most text in a document](https://make.wordpress.org/core/2024/10/17/updates-to-the-html-api-in-6-7/#ability-to-modify-most-text-in-a-document)
     c. [A full-parser mode in the HTML Processor](https://make.wordpress.org/core/2024/10/17/updates-to-the-html-api-in-6-7/#a-full-parser-mode-in-the-html-processor)
     d. [Normalization of input documents](https://make.wordpress.org/core/2024/10/17/updates-to-the-html-api-in-6-7/#normalization-of-input-documents)
     e. [Thorough review and alignment of CSS-related behaviors](https://make.wordpress.org/core/2024/10/17/updates-to-the-html-api-in-6-7/#thorough-review-and-alignment-of-css-related-behaviors)
 3. [Important changes for existing code.](https://make.wordpress.org/core/2024/10/17/updates-to-the-html-api-in-6-7/#important-changes-for-existing-code)
 4. [Enhancements](https://make.wordpress.org/core/2024/10/17/updates-to-the-html-api-in-6-7/#enhancements)
 5. [Bug Fixes](https://make.wordpress.org/core/2024/10/17/updates-to-the-html-api-in-6-7/#bug-fixes)

## Major Updates

### Full HTML Support

After a long time of only supporting a subset of HTML, the HTML Processor now supports
practically all HTML and all HTML tags^([1](https://make.wordpress.org/core/tag/html-api/?output_format=md#01b6c5ec-7321-4138-a6f4-5cf1bc9950bb)).
The internal work during the past few releases made it possible to add the remaining
support needed to tackle all the colorful varieties of HTML that exist. Only in 
the rarest situations will the HTML Processor now give up, and in none of these 
cases is it because the processor doesn’t know how to handle a given tagtag A directory
in Subversion. WordPress uses tags to store a single snapshot of a version (3.6,
3.6.1, etc.), the common convention of tags in version control systems. (Not to 
be confused with post tags.).

Effectively, the HTML APIAPI An API or Application Programming Interface is a software
intermediary that allows programs to interact with each other and share data in 
limited, clearly defined ways. is entering a new phase, where the rules for understanding
HTML are finally baked in, and the API can start to proliferate into new developer
APIs and replace older and buggier HTML processing code.

### Ability to modify most text in a document

The first of a new set of modification methods has been added to the HTML API: `
set_modifiable_text()`. When paused on a text node, a special self-contained element
like SCRIPT, STYLE, or TITLE, or on a comment, it’s possible to replace the text
content of that node.

    ```notranslate
    while ( $processor->next_tag( 'SCRIPT' ) ) {
        $existing_script = $processor->get_modifiable_text();
        $prefix          = "// Careful, this is executable!\n";
        $processor->set_modifiable_text( "{$prefix}{$existing_script}" );
    }
    ```

This method will handle the appropriate escaping for the kind of node it is. For
example, it will escape the text for a `SCRIPT` element in a different way than 
for a `STYLE` element or a raw text node. This is important, because it means you
don’t have to think about what kind of text it is, but it will remain reliable.

It’s important to understand here that this operates on the level of individual 
text nodes. `set_modifiable_text()` is different than `set_inner_text()`. A given
element may contain multiple text nodes which are separated by things like HTML 
comments, specific byte sequences, and invalidinvalid A resolution on the bug tracker(
and generally common in software development, sometimes also _notabug_) that indicates
the ticket is not a bug, is a support request, or is generally invalid. markup which
is interpreted as a comment. For `set_inner_text()` it will be best to wait until
the HTML API itself supports these operations.

### A _full-parser_ mode in the HTML Processor

Until now, the HTML Processor has provided a slightly unexpected interface. Creating
a processor involved a call to `create_fragment()`, which creates something called
a “fragment parser.” There was a good reason for this: the fragment parser is the
version that’s used in JavaScriptJavaScript JavaScript or JS is an object-oriented
computer programming language commonly used to create interactive effects within
web browsers. WordPress makes extensive use of JS for a better user experience. 
While PHP is executed on the server, JS executes within a user’s browser. [https://www.javascript.com](https://www.javascript.com/)
when assigning `node.innerHTML = newHtml`. Unfortunately, the fragment parser cannot
ingest an entire document from start to finish. If you worked with the HTML Processor
you would have noticed that it gave up as soon as it found the starting `<!DOCTYPE
html>` or `<html>` tokens.

In WordPress 6.7 the HTML Processor offers `create_full_parser()` to fill this need.
Thanks to completing support for all of the remaining contexts in a document, the
HTML Processor can now start with arbitrary HTML and understand how it turns into
a DOM tree.

While possibly unexpected, the default implementation with the fragment processor
is still the best option in most cases. If you’ve ever had to deal with HTML parsing
libraries adding `<!DOCTYPE html><html><head>` (and other tags) to the start of 
every snippet of HTML that they process, it’s because these tools are performing
full parses and full serialization. When working with anything other than a complete
HTML document, the fragment parser remains the most appropriate tool. Process _fragments_
of HTML with the fragment parser, and _full documents_ with the full parser.

### Normalization of input documents

With the HTML specification rules in place it’s possible to start building higher-
level tools and helpers without having to worry about the complexities of HTML. 
The first of these is `[WP_HTML_Processor::normalize()](https://github.com/WordPress/wordpress-develop/pull/7331)`,
which _gives us the HTML we always wanted_. Normalizing is the process of taking
any HTML input and returning a fully well-formed equivalent document. Since a picture
is worth a thousand words:

    ```notranslate
    $html = '</p><p class=wp class=duplicate><[GETTING[started]]><p><?not a tag?></p done=yet?><script><!--<script></script>';
    echo WP_HTML_Processor::normalize( $html );
    // <p></p><p class="wp">&lt;[GETTING[started]]></p><p><!--?not a tag?--></p>
    ```

This little function is packed with utility. It interprets the input HTML the way
a browser would, meaning that it applies _all_ of the complicated parsing rules,
and it prints out a new serialization of that parsed structure. Attribute quoting,
mismatched tags, missing tags, invalid syntax, unexpected whitespace, comments, 
and more – it prints out tags without duplicates, where each value is double-quoted,
where all relevant syntax characters are encoded, and it even removes partial tokens
at the end of the input. That’s right, `normalize()` won’t leave you hanging – literally!

    ```notranslate
    $html = 'Just a <em>sneaky but <!-- foiled attempt to hide the rest of the page';
    echo WP_HTML_Processor::normalize( $html );
    // Just a <em>sneaky but </em>
    ```

Significantly, there’s something extremely valuable in the output of this function:**
once HTML has been normalized, it’s considerably safer to process with naive parsing
tools**. There’s a guarantee on how the text it produces will appear, so string-
based and regular-expression-based tools will instantly become _more reliable_; 
the edge cases they fail to handle will never make it out of `normalize()`.

### Thorough review and alignment of CSSCSS Cascading Style Sheets.-related behaviors

CSS selectors and HTML interact via the _document mode_, which is either in standards
mode or quirks mode. In standards mode, CSS selectors match classes and IDs in a
byte-for-byte match. Otherwise they are matched in an ASCII case-insensitive manner.
Confusing!

Thankfully, [the entire HTML API was audited](https://core.trac.wordpress.org/ticket/61531)
to ensure compliance even in how CSS class selectors work, and the behavior of methods
like `has_class()`, `add_class()`, and `remove_class()` will now respect each document’s
mode. For almost every case this should be the “standards mode” and so both of the
HTML API processors default to it, matching class names in a byte-for-byte manner.
When creating an HTML Processor though with the _full parser_, since it’s able to
determine the document mode from the input HTML, it will make the same choice a 
browser makes, and when appropriate, will match case-insensitively.

## Important changes for existing code.

There should be no significant changes required to existing code written with previous
versions of the HTML API. While almost all HTML documents are now supported, it’s
still required to verify if the HTML Processor bailed while attempting to process
unsupported input.

There were several changes to the way null bytes and whitespace are handled in specific
circumstances. These cases represent invalid sections of documents and normal HTML
inputs aren’t affected by them.

Since changes were also made with the handling of ASCII casing in CSS class names,
there may be cases where a class name previously matched and now won’t. For example,
a browser will not match the CSS class name selector for `full-width` with an element
containing `FULL-WIDTH` in its `class` attribute _unless_ the document is parsed
in quirks-mode. The HTML API is now processing its inputs in standards mode, matching
the behavior of a browser, unless created with a full parser containing the appropriate
DOCTYPE to indicate quirks mode. Code that relied on `has_class()` need not change,
but the results in WordPress 6.7 may be different at times because it has become
more reliable to the proper handling of these class names.

## Enhancements

 * Low-level parsing updates improved the scanning speed of the Tag Processor by
   3.5-7.5%. [[Core-61545](https://core.trac.wordpress.org/ticket/61545)]
 * When removing class names, leading whitespace in front of the class name is now
   removed. [[Core-61531](https://core.trac.wordpress.org/ticket/61531)]
 * CSS class names and class selectors behave according to the document mode for
   the input document, or _standards mode_ if none can be directly inferred. [[Core-61531](https://github.com/WordPress/wordpress-develop/pull/7169)]
 * Improved spec-compliance by handling remaining HTML tags and insertion modes,
   including SVG, TABLE, TEMPLATE, and more. [[Core-61576](https://core.trac.wordpress.org/ticket/61576)]
 * Added `WP_HTML_Processor::create_full_parser()` to process entire HTML documents
   from start to finish. [[Core-61576](https://core.trac.wordpress.org/ticket/61576)]
 * Added `get_qualified_tag_name()` and `get_qualified_attribute_name()` to return
   case-variants for certain tags inside foreign content where the HTML rules specify.[
   [Core-61576](https://core.trac.wordpress.org/ticket/61576)]
 * It’s now possible to replace the value of modifiable text. [[Core-61617](https://core.trac.wordpress.org/ticket/61617)]
 * Added `get_unsupported_exception()` to provide more debugging information when
   the HTML Processor _bails_ on unsupported input. [[Core-61646](https://core.trac.wordpress.org/ticket/61646)]
 * `set_attribute()` returns `false` when WordPress rejects the update. [[Core-61719](https://core.trac.wordpress.org/ticket/61719)]
 * Added `subdivide_text_appropriately()` to aid in algorithms wanting to skip null
   byte or whitespace-only prefixes of text nodes. [[Core-61974](https://core.trac.wordpress.org/ticket/61974)]
 * Added `normalize()` and `serialize()` to return a normalized representation of
   parsed input HTML. [[Core-62036](https://core.trac.wordpress.org/ticket/62036)]

## Bug Fixes

 * `class_list()` now replaces null bytes with the Unicode replacement character.[
   [Core-61531](https://core.trac.wordpress.org/ticket/61531)]
 * The HTML Processor now properly generates implied end tags. Previously, in some
   cases, it would generate the end tag but leave the element open on the stack 
   of open elements, leading to mistakes in how it handled later tags in the document.[
   [Core-61576](https://core.trac.wordpress.org/ticket/61576)]
 * The HTML Processor now respects the array form of a `next_tag()` query when specifying
   the `tag_name`. [[Core-61581](https://core.trac.wordpress.org/ticket/61581)]
 * The HTML API no longer hangs when fed documents ending with open SCRIPT elements
   and which end in one of the `-` or `<` characters. [[Core-61810](https://core.trac.wordpress.org/ticket/61810)]
 * The Tag Processor now reports all un-closed funky comments as having paused at
   incomplete input. Previously, when the document ended immediately after opening
   the funky comment, e.g. `</#` with no more characters, it failed to report the
   incomplete token. [[Core-61831](https://core.trac.wordpress.org/ticket/61831)]

 1. There are some unusual situations in HTML which are not supported. They relate 
    to what HTML calls “fostering” and “adoption,” where a node’s placement in the 
    document tree does not correspond to where it appears in the HTML source. This 
    arises in roughly 0.5% of all websites on the web and isn’t currently supported
    in the HTML API. The deprecated `PLAINTEXT` element is also unsupported. [↩︎](https://make.wordpress.org/core/tag/html-api/?output_format=md#01b6c5ec-7321-4138-a6f4-5cf1bc9950bb-link)

Props [@jonsurrell](https://profiles.wordpress.org/jonsurrell/) and [@fabiankaegy](https://profiles.wordpress.org/fabiankaegy/)
for review.

[#dev-notes](https://make.wordpress.org/core/tag/dev-notes/), [#dev-notes-6-7](https://make.wordpress.org/core/tag/dev-notes-6-7/),
[#html-api](https://make.wordpress.org/core/tag/html-api/)

 [  ](https://profiles.wordpress.org/dmsnell/) [Dennis Snell](https://profiles.wordpress.org/dmsnell/)
12:30 am _on_ September 11, 2024     
Tags: html-api, [html-api-progress-report ( 2 )](https://make.wordpress.org/core/tag/html-api-progress-report/)

# 󠀁[Progress Report: HTML API](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/)󠁿

 1. [Quick review](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#quick-review)
 2. [What’s being worked on right now?](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#what-s-being-worked-on-right-now)
 3.  a. [Full HTML support](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#full-html-support)
     b.  a. [All supported tags, but with a caveat](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#all-supported-tags-but-with-a-caveat)
     c. [Reading and modifying inner HTML](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#reading-and-modifying-inner-html)
     d. [Review of all CSS semantics](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#review-of-all-css-semantics)
 4. [What’s coming after reaching full tag support?](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#what-s-coming-after-reaching-full-tag-support)
 5.  a. [Continuing towards 100%](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#continuing-towards-100)
     b. [Reading sourced block attributes on the server](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#reading-sourced-block-attributes-on-the-server)
     c. [WordPress Bits](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#wordpress-bits)
     d. [Native PHP support](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#native-php-support)
 6. [Additional notes](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#additional-notes)
 7.  a. [An HTML API debugger makes exploring HTML fun](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#an-html-api-debugger-makes-exploring-html-fun)
     b. [Following the progress](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#following-the-progress)
 8. [Acknowledgements](https://make.wordpress.org/core/2024/09/11/progress-report-html-api-3/#acknowledgements)

## Quick review

WordPress’s HTMLHTML HyperText Markup Language. The semantic scripting language 
primarily used for outputting content in web browsers. APIAPI An API or Application
Programming Interface is a software intermediary that allows programs to interact
with each other and share data in limited, clearly defined ways. is developing at
a rapid pace. [Introduced in WordPress 6.2](https://make.wordpress.org/core/2023/03/07/introducing-the-html-api-in-wordpress-6-2/),
and expanded in each release since, the HTML Processor is expected to support all
HTML inputs^([1](https://make.wordpress.org/core/tag/html-api/?output_format=md#86925f32-032a-4f74-8063-bc44beb31ea1))
in WordPress 6.7. What does this mean for you and where does development go once“
full support” has been reached?

## What’s being worked on right now?

### Full HTML support

The [major internal changes to the HTML Processor in WordPress 6.6](https://make.wordpress.org/core/2024/06/24/updates-to-the-html-api-in-6-6/#an-idealized-view-of-an-html-document)
opened the door to finish adding HTML support, meaning things like `TEMPLATE` tags,
tables, `SVG`, and forms. These are now all supported in the HTML API.

The HTML Processor also gained a new constructor method: `create_full_parser()`.
The full parser expects an HTML document in its entirety, including the doctype 
declaration, `<html>` tagtag A directory in Subversion. WordPress uses tags to store
a single snapshot of a version (3.6, 3.6.1, etc.), the common convention of tags
in version control systems. (Not to be confused with post tags.), and all. The existing
mechanism, `create_fragment()`, assumes that the given input exists within an existing
HTML context, such as inside a `<body>` tag. If you’ve ever wondered by `DOMDocument`
creates an HTML and HEAD element even when none exists, it’s because it’s performing
a full document parse. While both methods have their place, _**`create\_fragment()`
is probably still what you want to use**_ because WordPress rarely presents a complete
HTML document to its functions, plugins, and themes.

#### All supported tags, but with a caveat

There are a few situations that the HTML Processor still won’t support for WordPress
6.7. These are cases where nodes are supposed to be _re-parented_ and they have 
remained an obstacle for the past few releases. The problem isn’t knowing how to
parse these situations, but rather that they imply that parts of a document have
changed after the processor has already seen them. They occur most frequently when
formatting elements are implicitly closed and then need to be re-opened and when
content inside a `TABLE` element is not in a cell. Such [can be seen](https://playground.wordpress.net/?plugin=html-api-debugger&php-extension-bundle=light&url=%2Fwp-admin%2Fadmin.php%3Fpage%3Dhtml-api-debugger%26html%3D%253Ctable%253E%253Ctd%253E1%253C%252Ftd%253E2&wp=nightly)
with the following HTML:

    ```language-markup
    <table><td>1</td>2
    ```

This looks innocent enough, but the `2` belongs _in front of the `TABLE` element_.
When nodes are moved around _after the fact_ like this, it’s possible that the parser
might have seen hundreds of kilobytes of content in the meantime. This disconnect
between the order of the HTML text and the traversal order in the DOM tree raises
complicated questions, including for situations where inner markup is being modified.

This snippet is equivalent to the following HTML.

    ```language-markup
    2<table><tbody><tr><td>1</td></tr></tbody></table>
    ```

A cursory analysis of high-ranked domains on the broader internet suggests that 
around 1% of real web pages involve these and other unsupported situations. Luckily,
most content within WordPress and all content generated by frameworks like ReactReact
React is a JavaScript library that makes it easy to reason about, construct, and
maintain stateless and stateful user interfaces. [https://reactjs.org](https://reactjs.org/)
avoid these invalidinvalid A resolution on the bug tracker (and generally common
in software development, sometimes also _notabug_) that indicates the ticket is 
not a bug, is a support request, or is generally invalid. HTML constructions.

### Reading and modifying inner HTML

One question that has delayed the introduction of methods like `set_inner_html()`
is how to know whether content is already escaped or not. Can a system be built 
that allows “trusted” input without introducing opportunities for vulnerabilities?
With the expansion of support and the unlocking of new fragment parser contexts,
there’s finally a way forward to address this question: _do what a browser does_.

To set inner content, the HTML Processor will create a new fragment parser using
the current element as the context element. It will run the fragment parser to its
end and transfer the parsed nodes into the target, just like `node.innerHTML = '
string of <code>HTML</code>'` in the DOM does. This approach seems obvious from 
some angles, but implies that the HTML API will be parsing (and possibly re-parsing)
any contents that are being set into a target document – this is a slower approach
than naïvely slicing in new HTML.

This approach has a fascinating upside though: in addition to ensuring that content
is properly escaped, it will normalize the content and prevent the new HTML contents
from escaping its outer node. It properly isolates HTML updates. For example, suppose
one wants to set the inner HTML for a `DIV` element.

    ```php
    $processor = WP_HTML_Processor::create_fragment( '<main><div>Content here</div></main>' );
    if ( $processor->next_tag( 'DIV' ) ) {
        $processor->set_inner_html( 'What will </div> do?' );
    }
    echo $processor->get_updated_html();
    ```

What should happen with the `</div>`? If the HTML API were simply replacing text
or concatenating HTML, then the update would close the open `<div>` and the “` do?`”
content would now appear outside of the intended spot.

    ```language-markup
    // Broken by string concatenation unaware of HTML.
    <main><div>What will </div> do?</div></main>
    ```

Since the HTML API creates a fragment parser in the appropriate context, however,
when it finds the `</div>` token it will realize that there’s no open `<div>` and
ignore the token (note the extra space below, which in a browser will collapse into
single space). The content will remain inside the original `DIV`.

    ```language-markup
    <main><div>What will  do?</div></main>
    ```

Things can appear fairly wacky at times, but the normalization process will leave
the HTML _better than when it found it_.

    ```php
    if ( $processor->next_tag( 'DIV' ) ) {
        $processor->set_inner_html(
            '<em class=i class="dup"></p class="not-here"></div><li>1<li>2<!--'
        );
    }

    echo $processor->get_updated_html();
    ```

    ```language-markup
    <main><div><em class="i"><p></p><li>1</li><li>2</li></div></main>
    ```

Modifying HTML content mostly depends on being able to create a new fragment parser
in the context of the current element. Since this is an ongoing project it will 
not likely make it into WordPress 6.7, but should be available early in the 6.8 
development cycle.

### Review of all CSSCSS Cascading Style Sheets. semantics

The job of the HTML API is to know all of the minute details of HTML parsing so 
that you don’t have to. To that end, all of the CSS-related methods have been audited
to ensure that they reliably match the behavior in a browser. This mostly involved
examining the impact of “quirks mode,” which determines if class selectors match
names in a case sensitive manner or not.

So if you ask whether CSS class names are matched case-sensitively or not then the
HTML API will answer based on the HTML document it was provided. The Tag Processor
and fragment parser will assume no-quirks mode (standards mode) while the full parser
will choose based on the rules in the HTML specification.

## What’s coming after reaching full tag support?

### Continuing towards 100%

Parsing HTML involves no uncertainly: WordPress needs to understand all possible
HTML documents. In the coming releases work will continue on the tricker design 
details of supporting the final unsupported bits.

In some situations an element is found outside where it should be. For example, 
a `<p>` tag might appear after a `BODY` element is closed, but it belongs inside
the `BODY` element. The HTML Processor _could_ report this at the proper breadcrumbs(
because it knows it belongs at `HTML > BODY > P`), but that would appear as though
the parser jumped backwards in the document back into the `BODY`. A document should
only ever open and close the `BODY` once. Whether it’s an errant `<meta>` tag, a`
<script>` after the `BODY`, or an HTML comment after the `BODY`, these situations
account for about half of the situations that aren’t supported.

The other half of the unsupported situations are more complicated. They involve 
things which are easy in the DOM but hard in HTML. Specifically, there are times
where elements get moved up higher in the tree, or earlier, or are duplicated in
different parts of the tree. The HTML API knows where these elements should move,
but it doesn’t know until after it has parsed and moved on from those places. It
may be possible to look ahead in the document and rearrange the virtual sequence
in the HTML, but this is a complicated and delicate operation that requires lots
of care.

However these situations are handled, they will probably be done so in an _opt-in_
manner. Lookahead might resolve the problem of accurately representing the document,
but in some extreme cases, it may be required to fully parse _the entire document_
before returning control to the calling code. Potentially there could be a user-
selectable amount of lookahead before the processor gives up, and a small default
value could strike a good balance between fully-supported parsing and surprise performance
behaviors.

    ```php
    $processor->set_allowable_lookahead( 3 )
    ```

These unsupported situations present some design issues that also need to be solved
when discussing _modification_ of an HTML document. For example, in cases where 
formatting elements are reconstructed, if some code adds a new attribute to the 
real formatting token then the attribute value appears also on all of the recreated
versions of it. How should the HTML API know if someone wanted to set an attribute
only on the first element or if they wanted to set it also on all reconstructed 
elements?

In some cases this can all be avoided by using the HTML API to normalize a document
before processing, but in other cases these questions need to be explored before
making any hasty decisions. Supporting out-of-order elements makes reading and writing
inner HTML significantly more complicated because it can’t return a simple substring;
while auto-fixing could invoke catastrophic backtracking into the parser.

### Reading sourced blockBlock Block is the abstract term used to describe units of markup that, composed together, form the content or layout of a webpage using the WordPress editor. The idea combines concepts of what in the past may have achieved with shortcodes, custom HTML, and embed discovery into a single consistent API and user experience. attributes on the server

Although this feature has also been pushed back a couple of releases the work continues.
There wasn’t sufficient HTML support in WordPress 6.6 to allow reading block attributes
reliably enough to be usable. With the practically-complete support in WordPress
6.7 this will no longer be a problem. The primary challenge now is building an interface
to parse a CSS selector in a way that properly translates into HTML API code.

When this work was first explored it relied on the Tag Processor and started searching
in a top-down manner based on the selector. This isn’t the most efficient way to
search, however. In rebuilding the block attribute sourcer for the HTML Processor,
the parsing of the CSS selector is going to need to figure out how to search in 
a bottom-up manner. It’s also going to need to pay attention to things like class
names, attribute values, and `nth-child` counts for relevant tokens while parsing.

### WordPress Bits

WordPress 6.7 is not going to see any major work on [the Bits proposal](https://make.wordpress.org/core/2024/06/08/proposal-bits-as-dynamic-tokens/),
but the work is still being actively pursued. Other developments on templating, 
custom data types/fields, and block bindings continues to explore the space from
the UIUI User interface and UXUX User experience angle.

### Native PHPPHP The web scripting language in which WordPress is primarily architected. WordPress requires PHP 7.4 or higher support

The HTML API will be significantly faster when it’s available in PHP, written in
C instead of user-land PHP. The first proposal to move the HTML API into PHP itself
is [in the RFC](https://wiki.php.net/rfc/decode_html) process, adding a `decode_html()`
function corresponding to the recently-introduced `WP_HTML_Decoder` class.

## Additional notes

### An HTML API debugger makes exploring HTML fun

[@jonsurrell](https://profiles.wordpress.org/jonsurrell/) built [a WordPress plugin](https://wordpress.org/plugins/html-api-debugger/)
to add [a visual HTML API debugger](https://playground.wordpress.net/?plugin=html-api-debugger&php-extension-bundle=light&url=%2Fwp-admin%2Fadmin.php%3Fpage%3Dhtml-api-debugger%26html%3D%253C%253EHTML%253C%252Fp%253E%2Bcan%2Bbe%2Bweird%250Aand%2B%253Ctemplate%253E%250A%253Cli%253E%253Cp%253E%253Cem%253EWordPress%250A%253Ctable%253E%253Ctd%253EKnows%2BIt.&wp=nightly)
in `wp-admin` and it’s built with the [Interactivity API](https://developer.wordpress.org/block-editor/reference-guides/interactivity-api/).
This is a fun tool that shows how the HTML API parses a given HTML input. In addition,
it provide insight into the parsing mechanics that lead to the final structure. 
One can learn a lot about HTML parsing just by seeing how different inputs are processed.
For example, the “virtual” nodes in the output represent tags that weren’t in the
HTML text but were implicitly created based on HTML’s parsing rules.

[[

### Following the progress

For updates to the HTML API keep watch here for new posts.

If you want to follow the development work you can review [Trac tickets in the HTML API component](https://core.trac.wordpress.org/query?status=!closed&component=HTML+API)
or start watching new HTML API tickets from the [component overview page](https://make.wordpress.org/core/components/html-api/).
If you want to talk details or bugs or applications, check out the `#core-html-api`
channel in WordPress.orgWordPress.org The community site where WordPress code is
created and shared by the users. This is where you can download the source code 
for WordPress core, plugins and themes as well as the central location for community
conversations and organization. [https://wordpress.org/](https://wordpress.org/)
SlackSlack Slack is a Collaborative Group Chat Platform [https://slack.com/](https://slack.com/).
The WordPress community has its own Slack Channel at [https://make.wordpress.org/chat/](https://make.wordpress.org/chat/).

See also [the previous Progress Report](https://make.wordpress.org/core/2024/05/07/progress-report-html-api-2/)
and [Updates to the HTML API in 6.6](https://make.wordpress.org/core/2024/06/24/updates-to-the-html-api-in-6-6/)
for recently-completed developments.

## Acknowledgements

Thanks to [@annezazu](https://profiles.wordpress.org/annezazu/), [@gziolo](https://profiles.wordpress.org/gziolo/),
[@jonsurrell](https://profiles.wordpress.org/jonsurrell/), [@westonruter](https://profiles.wordpress.org/westonruter/)
for review and guidance when writing this post.

 1. “All” is mostly correct, because while all HTML tags are supported, there are still
    situations that account for around 1% of all HTML documents on the internet that
    get into edge cases that it can’t parse. These cases all deal with content that’s
    found outside of the place it should be, where the tree-order of the nodes in a
    DOM are rearranged from the order in which the HTML tags are found. The HTML Processor
    still aborts parsing when it encounters unsupported markup, so it should continue
    to be reliable for everything it supports. [↩︎](https://make.wordpress.org/core/tag/html-api/?output_format=md#86925f32-032a-4f74-8063-bc44beb31ea1-link)

[#html-api](https://make.wordpress.org/core/tag/html-api/), [#html-api-progress-report](https://make.wordpress.org/core/tag/html-api-progress-report/)

 [  ](https://profiles.wordpress.org/dmsnell/) [Dennis Snell](https://profiles.wordpress.org/dmsnell/)
9:48 pm _on_ June 24, 2024     
Tags: [6-6 ( 57 )](https://make.wordpress.org/core/tag/6-6/),
[dev-notes ( 654 )](https://make.wordpress.org/core/tag/dev-notes/), [dev-notes-6.6 ( 19 )](https://make.wordpress.org/core/tag/dev-notes-6-6/),
html-api   

# 󠀁[Updates to the HTML API in 6.6](https://make.wordpress.org/core/2024/06/24/updates-to-the-html-api-in-6-6/)󠁿

WordPress 6.6 includes a helpful maintenance release to the HTMLHTML HyperText Markup
Language. The semantic scripting language primarily used for outputting content 
in web browsers. APIAPI An API or Application Programming Interface is a software
intermediary that allows programs to interact with each other and share data in 
limited, clearly defined ways.. Included in this work are a few new features and
a major improvement to the usability of the HTML Processor. This continues paced
development since [WordPress 6.5](https://make.wordpress.org/core/2024/03/04/updates-to-the-html-api-in-6-5/).

## Table of Contents

 1. [A spec-compliant text decoder.](https://make.wordpress.org/core/tag/html-api/?output_format=md#a-spec-compliant-text-decoder)
 2.  a. [An idealized view of an HTML document.](https://make.wordpress.org/core/tag/html-api/?output_format=md#an-idealized-view-of-an-html-document)
     b. [An optimized class for looking up string tokens and their associated mappings.](https://make.wordpress.org/core/tag/html-api/?output_format=md#an-optimized-class-for-looking-up-string-tokens-and-their-associated-mappings)
 3. [Features](https://make.wordpress.org/core/tag/html-api/?output_format=md#features)
 4. [Bug Fixes](https://make.wordpress.org/core/tag/html-api/?output_format=md#bug-fixes)

## A spec-compliant text decoder.

This may be surprising, but PHPPHP The web scripting language in which WordPress
is primarily architected. WordPress requires PHP 7.4 or higher leaves us hanging
if we want to properly read the text content of an HTML document. The `html_entity_decode()`
and `htmlspecialchars_decode()` functions work somewhat well for pure XML documents,
but HTML contains more complicated rules for decoding, rules which change depending
on whether the text is found inside an attribute value or normal text. These functions
default to XML and HTML4 parsing rules and require manually setting the `ENT_HTML5`
flag on every invocation (for example, [HTML5 redefined two of HTML4’s character references](https://www.w3.org/TR/html5-diff/#syntax-misc)),
but _are still wrong_ in many cases.

Luckily you shouldn’t need to know about or call the new decoder, developed in [Core-61072](https://core.trac.wordpress.org/ticket/61072).
It fits into `get_modified_text()`, further improving the HTML API’s implementation
without requiring you to change any of your existing code. With WordPress 6.6 your
existing code becomes more reliable for free.

One part of this change you might want to know about is `WP_HTML_Decoder::attribute_starts_with()`.
This new method takes a plaintext prefix and a raw attribute value and indicates
if the decoded value starts with the given prefix. This can be invaluable for efficiently
detecting strings at the start of an attribute, as some attributes can be extremely
large, and if not careful, naive parsers can overlook content hidden behind long
slides of zeros.

    ```notranslate
    $html = 'bob&#x00000000000000000003a,';

    'bob&#x00000000000000000003a,' === html_entity_decode( $html, ENT_HTML5 );
    'bob:,' === WP_Text_Decoder::decode_attribute( $html );
    true    === WP_Text_Decoder::attribute_starts_with( $html, 'bob:' );
    ```

In the case of extremely long attribute values (for example, when pasting content
from cloud document editors which send images as data URIs), the `attribute_starts_with()`
can avoid megabytes of memory overhead and return much quicker than when calling
functions which entirely decode the attribute value.

The new text decoder will mostly help ensure that the HTML API remains safe and 
reliable. There are complicated rules in parsing HTML, so as always, it’s best to
leave the low-level work to the HTML API, preferring to call functions like `get_attribute()`
and `get_modified_text()` directly instead of parsing raw text segments.

#### An idealized view of an HTML document.

The Tag Processor was initially designed to jump from tagtag A directory in Subversion.
WordPress uses tags to store a single snapshot of a version (3.6, 3.6.1, etc.), 
the common convention of tags in version control systems. (Not to be confused with
post tags.) to tag, then it was refactored to allow scanning every kind of syntax
token in an HTML document. Likewise, the HTML Processor was initially designed to
jump from tag to tag, all the while also acknowledging the complex HTML parsing 
rules. These rules largely exist in the form of a stack machine that tracks which
elements are currently open. While the HTML Processor has always maintained this
stack, it has never exposed it to calling code.

In WordPress 6.6 the HTML Processor underwent a major internal refactor to report
those stack events (when an element opens and when an element closes) rather than
when it finds raw text that represents things like tag openers and tag closers. 
This is a really big change for calling code! Previously, the HTML Processor would
track all elements, but only return when a tag or token appeared in an HTML document.
For instance, it always knew that `<p><p>` represents two sibling P elements, but
it only presented each opening `P` tag to calling code. Now, the HTML processor 
is going to present not only the tags and tokens that exist in the raw HTML text,
but also the “virtual nodes” that are implied but not textually present.

    ```notranslate
    $processor = WP_HTML_Processor::create_fragment( '<h1>One</h3><h2>Two<p>Three<p>Four<h3>Five' );
    while ( $processor->next_token() ) {
    	$depth = $processor->get_current_depth();
        $slash = $processor->is_tag_closer() ? '/' : '';
    	echo "{$depth}: {$slash}{$processor->get_token_name()}: {$processor->get_modifiable_text()}n";
    }
    ```

Let’s compare the output in WordPress 6.5 against the output in WordPress 6.6.

HTML Processor in WordPress 6.5

    ```notranslate
    H1:
    #text: One
    /H3:
    H2:
    #text: Two
    P:
    #text: Three
    P:
    #text: Four
    H3:
    #text: Five
    ```

HTML Processor in WordPress 6.6

    ```notranslate
    3: H1:
    4: #text: One
    2: /H1:
    3: H2:
    4: #text: Two
    4: P:
    5: #text: Three
    4: /P:
    4: P:
    5: #text: Four
    3: /P:
    3: /H2:
    3: H3:
    4: #text: Five
    0: /H3:
    ```

With the HTML API in WordPress 6.6, it’s possible to treat an HTML document in the
idealized way we often think about it: where every tag has an appropriate corresponding
closing tag in the right place, and no tags overlap. In WordPress 6.5, only the 
opening tags which appeared in the document return from `next_tag()`, and the `</
h3>` closing tag appears _as_ an `H3` closing tag, even though the HTML specification
indicates that it closes the already-open `H1` element. In WordPress 6.6, every 
opening tag gets its closer, and the `</h3>` appears as if it were an `</h1>`. This
is because the HTML Processor is exposing the document structure instead of the 
raw text.

Two new methods make working with HTML even easier:

 * `WP_HTML_Processor->get_current_depth()` returns the depth into the HTML structure
   where the current node is found.
 * `WP_HTML_Processor->expects_closer()` indicates if the opened node expects a 
   closing tag or if it will close automatically when proceeding to the next token
   in the document. For example, text nodes and HTML comments and void elements 
   never expect a closer.

With the help of these methods it’s possible to trivially detect when an element
opens and closes, because the HTML Processor guarantees a “perfect” view of the 
structure.

    ```notranslate
    $processor = WP_HTML_Processor( $block_html );
    if ( ! $processor->next_tag( 'DIV' ) ) {
    	return $block_html;
    }

    $depth = $processor->get_current_depth();
    while ( $processor->get_current_depth() >= $depth && $processor->next_token() ) {
    	// Everything inside of here is inside the open DIV.
    }
    if ( ! isset( $processor->get_last_error() ) ) {
    	// This is where the DIV closed.
    }
    ```

#### An optimized class for looking up string tokens and their associated mappings.

As part of the text decoder work the `WP_Token_Map` was introduced. This is a handy
and efficient utility class for mapping between keys or tokens and their replacements.
It’s also handy for efficient set membership; for example, to determine if a given
username is found within a set of known usernames.

Read more in [the Token Map announcement](https://make.wordpress.org/core/?p=113042).

## Features

 * The HTML Processor will now return the depth of the current node in the stack
   of open elements with `get_current_depth()`. [[58191](https://core.trac.wordpress.org/changeset/58191)]
 * The HTML Processor now includes `expects_closer()` to indicate the currently-
   matched node expect a closing token. For example, no HTML void element expects
   a closer, no text node expects a closer, and none of the elements treated specially
   in the HTML API as atomic elements (such as `SCRIPT`, `STYLE`, `TITLE`, or `TEXTAREA`)
   expect a closer. [[58192](https://core.trac.wordpress.org/changeset/58192)]
 * The `WP_HTML_Decoder` class can take a raw HTML attribute or text value and decode
   it, assuming that the source and destination are UTF-8. The HTML API now uses
   this instead of `html_entity_decode()` for more reliable parsing of HTML text
   content. [[58281](https://core.trac.wordpress.org/changeset/58281)]
 * The HTML Processor now visits all real and virtual nodes, not only those which
   are also present in the text of the HTML, but those which are implied by what’s
   there or not there. [[58304](https://core.trac.wordpress.org/changeset/58304)]

## Bug Fixes

 * Funky-comments whose contents are only a single character are now properly recognized.
   Previously the parser would get off track in these situations, consuming text
   until the next `>` after the funky comment. [[58040](https://core.trac.wordpress.org/changeset/58040)]
 * The HTML Processor now respects the `class_name` argument if passed to `next_tag()`.
   Formerly it was overlooking this constraint. [[58190](https://core.trac.wordpress.org/changeset/58190)]
 * The Tag Processor was incorrectly tracking the position of the last character
   in some tokens, internally and when bookmarking. While this bugbug A bug is an
   error or unexpected result. Performance improvements, code optimization, and 
   are considered enhancements, not defects. After feature freeze, only bugs are
   dealt with, with regressions (adverse changes from the previous version) being
   the highest priority. did not affect the operation of the Tag Processor, it has
   been fixed so that future code which might rely upon it will work properly. [
   [58233](https://core.trac.wordpress.org/changeset/58233)]
 * When subclassing `WP_HTML_Processor` the `::create_fragment()` method will return
   the subclass instance instead of a `WP_HTML_Processor` instance. [[58365](https://core.trac.wordpress.org/changeset/58365)]

_Props to [@gziolo](https://profiles.wordpress.org/gziolo/), [@jonsurrell](https://profiles.wordpress.org/jonsurrell/),
[@juanmaguitar](https://profiles.wordpress.org/juanmaguitar/), and [@westonruter](https://profiles.wordpress.org/westonruter/)
for reviewing this post and providing helpful feedback._

[#6-6](https://make.wordpress.org/core/tag/6-6/), [#dev-notes](https://make.wordpress.org/core/tag/dev-notes/),
[#dev-notes-6-6](https://make.wordpress.org/core/tag/dev-notes-6-6/), [#html-api](https://make.wordpress.org/core/tag/html-api/)

 [  ](https://profiles.wordpress.org/dmsnell/) [Dennis Snell](https://profiles.wordpress.org/dmsnell/)
7:04 pm _on_ May 7, 2024     
Tags: html-api   

# 󠀁[Progress Report: HTML API](https://make.wordpress.org/core/2024/05/07/progress-report-html-api-2/)󠁿

The HTMLHTML HyperText Markup Language. The semantic scripting language primarily
used for outputting content in web browsers. APIAPI An API or Application Programming
Interface is a software intermediary that allows programs to interact with each 
other and share data in limited, clearly defined ways. continues to receive paced
and steady development. The Tag Processor was [introduced in WordPress 6.2](https://make.wordpress.org/core/2023/03/07/introducing-the-html-api-in-wordpress-6-2/),
the HTML Processor [in WordPress 6.4](https://make.wordpress.org/core/2023/10/21/updates-to-the-html-api-in-6-4/),
and the ability to traverse all syntax tokens in a document was [added in WordPress 6.5](https://make.wordpress.org/core/2024/03/04/updates-to-the-html-api-in-6-5/).
What’s been happening since then and what’s in store for the coming releases?

 1. [Quick Review](https://make.wordpress.org/core/2024/05/07/progress-report-html-api-2/#quick-review)
 2. [What’s being worked on right now?](https://make.wordpress.org/core/2024/05/07/progress-report-html-api-2/#what-s-being-worked-on-right-now)
 3.  a. [Finish the HTML Processor.](https://make.wordpress.org/core/2024/05/07/progress-report-html-api-2/#finish-the-html-processor)
     b. [Represent virtual nodes.](https://make.wordpress.org/core/2024/05/07/progress-report-html-api-2/#represent-virtual-nodes)
     c. [Eliminate text decoding problems.](https://make.wordpress.org/core/2024/05/07/progress-report-html-api-2/#eliminate-text-decoding-problems)
 4. [What’s coming after these internal improvements?](https://make.wordpress.org/core/2024/05/07/progress-report-html-api-2/#what-s-coming-after-these-internal-improvements)
 5.  a. [Reading and modifying sourced block attributes.](https://make.wordpress.org/core/2024/05/07/progress-report-html-api-2/#reading-and-modifying-sourced-block-attributes)
     b. [Full HTML support.](https://make.wordpress.org/core/2024/05/07/progress-report-html-api-2/#full-html-support)
     c. [Little bits of semantic meaning.](https://make.wordpress.org/core/2024/05/07/progress-report-html-api-2/#little-bits-of-semantic-meaning)
 6. [Following the progress](https://make.wordpress.org/core/2024/05/07/progress-report-html-api-2/#following-progress)
 7. [Acknowledgements](https://make.wordpress.org/core/2024/05/07/progress-report-html-api-2/#acknowledgements)

## Quick Review

The [last Progress Report](https://make.wordpress.org/core/2023/08/19/progress-report-html-api/)
explained what the HTML API provides and why it was introduced. The HTML API is 
being designed to provide a reliable, safe, convenient, and efficient means for 
understanding and modifying HTML from the server. The driving motivation is to support
all server-side needs when working with HTML, while the pace of development is governed
by moving as fast as possible without compromising safety and reliability.

While the eventual goal is to provide an assortment of DOM-like methods (for example,`
set_inner_html()`), work continues to ensure that the foundation on which these 
are built is steady, so that **you can trust it from day one**.

## What’s being worked on right now?

### Finish the HTML Processor.

The largest remaining goal is adding full support to the HTML Processor. While the
Tag Processor understands each tagtag A directory in Subversion. WordPress uses 
tags to store a single snapshot of a version (3.6, 3.6.1, etc.), the common convention
of tags in version control systems. (Not to be confused with post tags.), each comment,
each text node in isolation and can even change a tag’s attributes, it’s unaware
of where one _element_ begins and ends. HTML is, from a certain perspective, a shorthand
script for a full DOM document, and the tags which are or are not present may not
correspond to what a browser creates when parsing them. The HTML Processor takes
the stream of tokens produced by the Tag Processor and builds a map of how the tags
relate to elements.

    ```notranslate
    <p>This is <b>bold.<p>This is also bold.</p>
    ```

For example, a `<p>` tag can appear after another `<p>` tag but before a `</p>` 
has been found. This is often referred to as having missing or overlapping tags.
However, the HTML specification’s rules are clear in this situation: the second `
<p>` tag implies the closing of the first one as well as any other elements which
were already open, and it starts a new `P` elements as a sibling of the first. The
HTML Processor knows this so you don’t have to.

Unfortunately there are extensive rules and the HTML Processor doesn’t currently
understand them all. Thankfully, if it doesn’t understand a situation it finds itself
in, then it will “bail” by returning `false` and storing an error, retrievable with`
get_last_error()`.

### 󠀁[Represent virtual nodes](https://github.com/WordPress/wordpress-develop/pull/6348)󠁿.

The past couple releases saw major progression in supporting more HTML elements,
but one obstacle has stood in the way: so-called _virtual nodes_ that don’t exist
in the HTML text itself.

    ```notranslate
    <li><p>One</p></p><p>Two</p></li>
    ```

Given the snippet of HTML above, what would you guess is the `innerText` of the 
second `P` element (`p:nth-child(2)`)? You probably already know this is a trick
question: it’s empty!

The rules of HTML dictate that when encountering an unexpected closing `</p>` tag
for which there’s no corresponding opening tag, an empty `P` element without any
attributes is created. Most tags don’t do this, but `P` does. If someone is using
the HTML Processor to find the second `P` element in a document, it shouldn’t lead
them astray, but how should it represent tags which in a sense don’t actually exist?

This problem has held up a number of the remaining tags because many of them lead
to situations where not only one element appears, but possibly _many_ elements are
created between a very simple-looking boundary.

The HTML Processor is therefore undergoing an internal refactor to change the way
it represents movement through the document. It will pause at these virtual nodes
and represent them so that calling code can find them, though they will remain read-
only for now. There’s a spectacular side-effect to this change though: **the HTML
Processor will be presenting a view of HTML in an idealized form**. Because virtual
nodes appear not only on openings, but also on closings, _the processor will present
the document as if it were entirely normative_. There are no missing, unexpected,
or overlapping tags; every opening tag expecting a closer will find one in the right
place. It will be possible through the HTML Processor interface to reliably assume
basic structure of HTML: that’s right, all of those “what if” question about quirky
or mangled HTML are irrelevant, because the HTML Processor thinks in terms of HTML
and _not_ in terms of strings.

    ```notranslate
    <a>link<a>link</a><ul><li><p><b>One<li>Two</b><li><p>Three</li></ul>Four
    ```

This snippet above looks _broken_, and very few parsers will know how to handle 
it the way a browser does. Consider, however, the sequence of tags or tokens that
the HTML Processor will find (in the next snippet, imagine that the loopLoop The
Loop is PHP code used by WordPress to display posts. Using The Loop, WordPress processes
each post to be displayed on the current page, and formats it according to how it
matches specified criteria within The Loop tags. Any HTML or PHP code in the Loop
will be processed on each post. [https://codex.wordpress.org/The_Loop](https://codex.wordpress.org/The_Loop)
is printing a `+` for an opening tag and a `-` for a closing tag):

    ```notranslate
    while ( $html_processor->next_token() ) { … }

    [
      '+A', '#text', '-A',
      '+A', '#text', '-A',
      '+UL',
        '+LI', '+P', '+B', '#text', '-B', '-P', '-LI',
        '+LI', '+B', '#text', '-B', '-LI',
        '+LI', '+P', '#text', '-P', '-LI',
      '-UL',
      '#text'
    ]
    ```

While this may appear confusing, it’s worth spending some time pondering. The HTML
Processor found many more tags than actually exist in the text of the HTML, because
it knows it needs to create them as it steps through the document. There’s a really
incredible implication here: traversing inside an element is trivial. This means
that finding inner content and matching tags can be done the way we often expect
or want it to, and that it’s a reliable means to do so. Every `<a>` opening tag 
will be followed by a `</a>` no matter what else is in the HTML. Even when dealing
with nested tags and sibling tags, **finding the end of an element is as simple 
as looking for the first closing tag of the same name at the same depth** – it’s
guaranteed to exist!

    ```notranslate
    $div_depth = $processor->get_depth();
    // Find where the DIV closes.
    while (
        'DIV' !== $processor->get_tag() &&
        ! $processor->is_tag_closer() &&
        $div_depth !== $processor->get_depth() &&
        $processor->next_token()
    ) {
        // Whatever this is, it's inside the DIV.
    }
    ```

### Eliminate 󠀁[text decoding](https://github.com/WordPress/wordpress-develop/pull/6387)󠁿 problems.

Like most things in HTML, proper parsing is more complicated than it would appear
at first. When decoding _text_ content from the HTML, this is worse than it seems,
because PHPPHP The web scripting language in which WordPress is primarily architected.
WordPress requires PHP 7.4 or higher currently lacks the ability to properly decode
_character references_. Character references are what people often call “entities”(
a term borrowed from XML). They start with `&` and what follows is either a number(
in decimal or hexadecimal) that represents a Unicode code point, or a name found
in a lookup table to map to a specific code point or sequence of code points (for
example, `&NotEqualTilde;` maps to U+2242 U+0338 producing a not-approximately-equal
sign ≂̸).

HTML incorporates special rules when decoding these character references to maintain
backwards compatibility with common practices that predate HTML5. These mostly revolve
around what happens when the final semicolon `;` is missing. In many cases the `;`
isn’t necessary while in other cases it is. Notably, when the semicolon is optional,
there are additional restrictions inside attribute values when the reference name
could be _ambiguous_. This rule preserves cases when URLURL A specific web address
of a website or web page on the Internet, such as a website’s URL www.wordpress.
org query arguments aren’t properly encoded, as every `&` in a URL ought to be encoded
as `&amp;`, but often aren’t.

PHP’s `html_entity_decode()` currently lacks full support:

 * There are 1,730 named character references it’s unaware of.
 * `&lang;` and `&rang;` are improperly decoded as if decoding HTML4 [instead of HTML5](https://www.w3.org/TR/html5-diff/#syntax-misc).
 * It doesn’t provide a way to govern whether the _ambiguous ampersand_ rule should
   be applied.
 * It’s unable to decode named character references without a trailing `;`.
 * It doesn’t handle border cases where the character references end a string, such
   as at the end of an attribute value or right before another tag appears (at the
   end of a text node).

Further, HTML applies special rules to a range of code points _when decoded from
numeric character references_. Many of you have problem seen cases where “smart 
quotes” turn into junk when rendered. This is because HTML may store certain code
points as if they representing the Windows-1252 encoding, but only from numeric 
character references. This transformation is not applied in `html_entity_decode()`
and the references are left intact.

The HTML API needs a mechanism for properly decoding text content _by default_, 
and that will likely appear in the form of two new methods:

 * `WP_HTML_Decoder::decode_text_node()` for decoding text found in normal markup(`#
   text` nodes).
 * `WP_HTML_Decoder::decode_attribute()` for decoding text found inside attribute
   values.

There is nothing special about these methods other than they should be a reliable
mechanism for reading (in UTF-8) the actual text a browser would read for the given
HTML. _The HTML API knows the intricate details of HTML so you don’t have to_.

---

For WordPress 6.6 these are the two primary changes planned: ensure that the HTML
Processor visits all virtual nodes, and properly decode all text. For the most part
that means no major changes to the interface; everything is in a sense a bugbug 
A bug is an error or unexpected result. Performance improvements, code optimization,
and are considered enhancements, not defects. After feature freeze, only bugs are
dealt with, with regressions (adverse changes from the previous version) being the
highest priority.-fix to more closely conform the implementation to its design.

## What’s coming after these internal improvements?

### Reading and modifying sourced blockBlock Block is the abstract term used to describe units of markup that, composed together, form the content or layout of a webpage using the WordPress editor. The idea combines concepts of what in the past may have achieved with shortcodes, custom HTML, and embed discovery into a single consistent API and user experience. attributes.

There’s a very exciting development that is resurfacing that started over a year
ago: reading block attributes from the server, at least for blocks with a `block.
json` file. The ability to read the “sourced attributes” for a block was one of 
the driving reasons that work on the HTML API progressed beyond the Tag Processor.
It mainly comprises parsing a CSSCSS Cascading Style Sheets. selector, finding a
matching location with an HTML document, and then reading an attribute, inner text,
or inner HTML.

While the initial prototypes were encouraging, it was clear that it would be important
to _truly_ understand HTML structure in order to do this right. The concept of _balanced
tags_^([1](https://make.wordpress.org/core/tag/html-api/?output_format=md#d1b5679e-9d70-4c2f-9324-78649baa9308))
simply isn’t a very useful model for understanding HTML. Now that the HTML Processor
is so much further along, however, rebuilding the _attribute sourcer_ is turning
out to be not only more reliable, but considerably simpler too!

It’s our hope that we have a working system ready by the time that WordPress 6.6
is released so that we can test it for the whole 6.7 release cycle. This is useful
not only for individual render functions, but the Block Bindings project needs to
be able to read and modify these attributes as well. By ensuring that the system
is robust to handle whatever HTML comes its way, we can make Block Bindings work
for any block attribute.

### Full HTML support.

With the issue of virtual nodes taken care of it is possible to push forward on 
supporting even more HTML tags and situations. One issue will remain, which is a
tricky situation where something called fostering occurs. This can happen, for instance,
when tags are found where they shouldn’t be, such as a `<div>` tag inside a `TABLE`
element but not inside a `TD` or `TH`. Strangely enough, that `DIV` element will
be moved up _in front of _ the `TABLE` element. This implies that there’s a kind
of retroactive change in the document after we’ve visited a location in it. There
is currently no clear way to represent this situation or communicate it to calling
code, so the HTML Processor will continue to bail in these rare scenarios.

Apart from that the rest of the HTML support is large and tedious but straightforward.
Expect that in WordPress 6.7 you will be able to send it almost any HTML and it 
will be able to fully understand the document.

### Little bits of semantic meaning.

With the introduction of the block editor, WordPress largely lost Shortcodes, which
were the go-to way of incorporating small tidbits of external content or meaning
into a post. Shortcodes had their shortcomings, but they also had value. For more
than a couple years we’ve been discussing various approaches to _[bringing shortcodes back](https://github.com/WordPress/gutenberg/discussions/39831)_:
safely and without the most significant drawbacks (breaking HTML, taking over layout,
ambiguous nesting rules, introducing a full page of content, etc…). The [HTML API changed the game](https://make.wordpress.org/core/2023/08/19/progress-report-html-api/#templating)
for all of these explorations because it offers a way to build a _context-aware 
auto-escaping _templating engine that can power the next generation of Shortcodes,
what we have lately been calling “Bits” (Blocks are big, and Bits are small 😉).

While it’s currently possible to add a _post author_ block into a post template,
or use a block binding to replace a paragraph’s content with a custom fieldCustom
Field Custom Field, also referred to as post meta, is a feature in WordPress. It
allows users to add additional information when writing a post, eg contributors’
names, auth. WordPress stores this information as metadata. Users can display this
meta data by using template tags in their WordPress themes., Bits will open up new
opportunity to place these snippets of external content anywhere you want them, 
even inside the middle of a paragraph or image caption!

In WordPress 6.6 look for explorations in the editor for how to enter and configure
Bits. Work has already started in WordPress’s backend to ensure that the Bit syntax
is preserved through post saves and renders. This is going to be a large project
with many different systems working together, so it won’t likely be available anytime
soon, but many of the independent pieces will be appearing in the next couple releases
for those who want to explore the foundations of how they work.

## Following the progress

For updates to the HTML API keep watch here for new posts.

If you want to follow the development work you can review [Trac tickets in the HTML API component](https://core.trac.wordpress.org/query?status=!closed&component=HTML+API)
or start watching new HTML API tickets from the [component overview page](https://make.wordpress.org/core/components/html-api/).
If you want to talk details or bugs or applications, check out the `#core-html-api`
channel in WordPress.orgWordPress.org The community site where WordPress code is
created and shared by the users. This is where you can download the source code 
for WordPress core, plugins and themes as well as the central location for community
conversations and organization. [https://wordpress.org/](https://wordpress.org/)
SlackSlack Slack is a Collaborative Group Chat Platform [https://slack.com/](https://slack.com/).
The WordPress community has its own Slack Channel at [https://make.wordpress.org/chat/](https://make.wordpress.org/chat/).

## Acknowledgements

Thanks [@gziolo](https://profiles.wordpress.org/gziolo/), [@jonsurrell](https://profiles.wordpress.org/jonsurrell/),
and [@westonruter](https://profiles.wordpress.org/westonruter/) for helping create
and edit this post.

[#html-api](https://make.wordpress.org/core/tag/html-api/)

 1. “Balanced tags” is a best-effort guess at HTML structure based on scanning from
    an element’s opening tag until an appropriate closing tag is found. Opening tags
    along the way will increase a depth just as closing tags decrease the depth. In
    the idealized view of HTML that the HTML Processor provides this guess is reliable,
    but without that, it’s very difficult in practice with real HTML documents to reliably
    understand where an element opens and closes. [↩︎](https://make.wordpress.org/core/tag/html-api/?output_format=md#d1b5679e-9d70-4c2f-9324-78649baa9308-link)

 [  ](https://profiles.wordpress.org/bph/) [Birgit Pauli-Haack](https://profiles.wordpress.org/bph/)
3:17 pm _on_ March 7, 2023     
Tags: [6.2 ( 90 )](https://make.wordpress.org/core/tag/6-2/),
[dev-notes ( 654 )](https://make.wordpress.org/core/tag/dev-notes/), [dev-notes-6.2 ( 19 )](https://make.wordpress.org/core/tag/dev-notes-6-2/),
html-api   

# 󠀁[Introducing the HTML API in WordPress 6.2](https://make.wordpress.org/core/2023/03/07/introducing-the-html-api-in-wordpress-6-2/)󠁿

**This post was co-authored by Adam Zielinski [@zieladam](https://profiles.wordpress.org/zieladam/)
and Dennis Snell [@dmsnell](https://profiles.wordpress.org/dmsnell/) **

WordPress 6.2 introduces [WP_HTML_Tag_Processor](https://core.trac.wordpress.org/ticket/57575)–
a tool for blockBlock Block is the abstract term used to describe units of markup
that, composed together, form the content or layout of a webpage using the WordPress
editor. The idea combines concepts of what in the past may have achieved with shortcodes,
custom HTML, and embed discovery into a single consistent API and user experience.
authors to adjust HTMLHTML HyperText Markup Language. The semantic scripting language
primarily used for outputting content in web browsers. tagtag A directory in Subversion.
WordPress uses tags to store a single snapshot of a version (3.6, 3.6.1, etc.), 
the common convention of tags in version control systems. (Not to be confused with
post tags.) attributes in block markup within PHPPHP The web scripting language 
in which WordPress is primarily architected. WordPress requires PHP 7.4 or higher.
It’s the first component in a new HTML processing APIAPI An API or Application Programming
Interface is a software intermediary that allows programs to interact with each 
other and share data in limited, clearly defined ways..

Updating HTML in WordPress has always required using uncomfortable tools. Regular
expressions are difficult and prone to all kinds of errors. `DOMDocument` is resource-
heavy, fails to handle modern HTML correctly, and isn’t available on many hosting
platforms.

`WP_HTML_Tag_Processor` takes the first step towards bridging this gap.

## The Tag Processor can reliably update HTML attributes

The Tag Processor finds specific tags and can change its attributes. Here’s an example
setting an `alt` attribute on the first `img` tag within a block of HTML.

    ```notranslate
    $html = '<img src="/husky.jpg">';

    $p = new WP_HTML_Tag_Processor( $html );

    if ( $p->next_tag() ) {
        $p->set_attribute( 'alt', 'Husky in the snow' );
    }

    echo $p->get_updated_html();

    // Output:
    // <img alt="Husky in the snow" src="/husky.jpg">
    ```

The `next_tag()` method moves to the next available tag in the HTML, but also accepts
a tag name, a CSSCSS Cascading Style Sheets. class, or both in order to find specific
tags. According to the HTML specification, lookup of tag and attribute names aren’t
case-sensitive, but CSS class names are.

    ```notranslate
    if ( $p->next_tag( array( 'tag_name' => 'DIV', 'class_name' => 'block-GROUP' ) ) ) {
        $p->remove_class( 'block-group' );
        $p->add_class( 'wp-block-group' );
    }
    ```

Operations are safe by default:

 * remove an attribute without first checking if it exists,
 * add a CSS class which might already be there,
 * set an attribute value without ensuring that it’s not duplicating an existing
   one.

You no longer need to be concerned that your code mistakes for a real tag the content
inside a `<textarea>`, and attribute value, or even inside an HTML comment.

The Tag Processor conforms to the HTML5 specification, so you don’t have to. It 
automatically escapes and decodes values where necessary and knows how to handle
malformed markup.

    ```notranslate
    $ugly_html = <<<HTML
    <textarea title='<div> elements are semantically void'>
        <div><!--<div attr-->="</div>"></div>">
    </textarea>
    <div></div>
    HTML;

    $p = new WP_HTML_Tag_Processor( $ugly_html );
    if ( $p->next_tag( 'div' ) ) {
        $p->add_class( 'bold' );
    }

    echo $p->get_updated_html();
    // Output:
    // <textarea title='<div> elements are semantically void'>
    //     <div><!--<div attr-->="</div>"></div>">
    // </textarea>
    // <div class="bold"></div>
    ```

The Tag Processor operates fast enough to run in critical hot code paths and incurs
almost no memory overhead. In WordPress 6.2 it replaces bugbug A bug is an error
or unexpected result. Performance improvements, code optimization, and are considered
enhancements, not defects. After feature freeze, only bugs are dealt with, with 
regressions (adverse changes from the previous version) being the highest priority.-
prone code relying on regular expressions and string-searching to perform similar
updates.

For more advanced use of the Tag Processor, read through the extensive in-class 
documentation and learn how to…

 * …set bookmarks to re-visit parts of the document which have already been scanned
   and modified.
 * …visit closing tags like `</div>` in addition to the opening tags.
 * …run advanced and stateful queries by visiting every tag in a document.

## Further considerations

There are many things the Tag Processor doesn’t do: it doesn’t build a DOM document
tree, find nested tags, or update a tag’s inner HTML or inner text. Work on new 
HTML-related APIs continues, and a future WordPress release will build upon this
work to enable accessing all of a block’s attributes from within PHP (if the block
supplies a `block.json` file), finding tags using a CSS selector, and modifying 
the HTML structure with new tags, removed tags, and updated inner markup.

You can keep up with [further development via this overview issues](https://github.com/WordPress/gutenberg/issues/44410)
on the GutenbergGutenberg The Gutenberg project is the new Editor Interface for 
WordPress. The editor improves the process and experience of creating new content,
making writing rich content much simpler. It uses ‘blocks’ to add richness rather
than shortcodes, custom HTML etc. [https://wordpress.org/gutenberg/](https://wordpress.org/gutenberg/)
GitHubGitHub GitHub is a website that offers online implementation of git repositories
that can easily be shared, copied and modified by other developers. Public repositories
are free to host, private repositories require a paid subscription. GitHub introduced
the concept of the ‘pull request’ where code changes done in branches by contributors
can be reviewed and discussed before being merged by the repository owner. [https://github.com/](https://github.com/)
Repo.

[#6-2](https://make.wordpress.org/core/tag/6-2/), [#dev-notes](https://make.wordpress.org/core/tag/dev-notes/),
[#dev-notes-6-2](https://make.wordpress.org/core/tag/dev-notes-6-2/), [#html-api](https://make.wordpress.org/core/tag/html-api/)