Skip to content

A11y: Keep text that only looks like an HTML tag in speak() - #87916

Open
ciampo wants to merge 6 commits into
trunkfrom
t3/speak-html-tag-filter
Open

ciampo wants to merge 6 commits into
trunkfrom
t3/speak-html-tag-filter

Conversation

@ciampo

@ciampo ciampo commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

What?

Follow-up to #86732.

speak() now removes text in angle brackets only when the message shows that it is an HTML tag. Other text in angle brackets is announced, so report <draft>.pdf is no longer read as "report .pdf".

Why?

speak() removed everything between < and >. A filename such as report <draft>.pdf lost <draft>, and 1 < 2 and 3 > 2 lost "2 and 3". #86732 found this while building the Selecting Local Files patterns, and the page had to warn about it.

How?

Comments and doctypes are removed whole first. After that, a tag is removed in three cases:

  • It is self-closing, such as <br/>.
  • The message has both its opening and its closing tag, such as <strong> and </strong>.
  • It is a void element, such as <br> or <img>. A void element never has a closing tag.

Everything else in angle brackets stays. That includes an HTML element name on its own, so notes <b>.txt keeps <b>, and a message can name <search> or <form>.

Existing callers that pass markup, such as <p>…</p>, <a href="…">…</a>, <br>, or <br/>, get the same result as before.

The speak() docs now state this rule, and they say how to name an HTML element in a message.

Why not check the tag name against the browser's elements?

An earlier revision of this PR looked up each name in an inert document and removed the tag when the browser knew the element. That removed every HTML element name, so it could not keep notes <b>.txt or the <search> that the Search block names in #78485. It also kept SVG tags such as <path> as text, and the result depended on the browser's list of elements. The closing-tag rule came from review feedback. It needs no DOM, so the filter test runs without jsdom again.

Limits

  • HTML that omits a closing tag, such as <p>One<p>Two or an <li> without </li>, is now announced with its tags. On trunk those tags are removed.
  • A void element name in angle brackets is still removed. photo <img>.png is announced as "photo .png". The Selecting Local Files page states this limit.
  • Text is still removed when the message has both tags with the same name. In <b>Added</b> notes <b>.txt, the <b> of the filename is removed.

Testing Instructions

  1. Open a post in the editor and open the browser console.
  2. Run:
    wp.a11y.speak( 'Added report <draft>.pdf' );
    document.getElementById( 'a11y-speak-polite' ).textContent;
    On trunk, the result is "Added report .pdf". With this PR, it is "Added report <draft>.pdf".
  3. Run the same with an HTML element name:
    wp.a11y.speak( 'Added notes <b>.txt' );
    document.getElementById( 'a11y-speak-polite' ).textContent;
    On trunk, the result is "Added notes .txt". With this PR, it is "Added notes <b>.txt".
  4. Run:
    wp.a11y.speak( '<strong>Saved</strong> as <em>draft</em>' );
    document.getElementById( 'a11y-speak-polite' ).textContent;
    The tags are still removed: " Saved as draft ", the same as on trunk.
  5. Optional, with a screen reader: in Storybook, open Design System → Patterns → Selecting Local Files and add a file named report <draft>.pdf (macOS or Linux allow < and > in file names). The announcement includes <draft>.

Testing Instructions for Keyboard

No UI change.

Use of AI Tools

Claude Code wrote the fix, tests, and description from the follow-up listed in #86732. Review rounds with GPT-6.2 Astra shaped the earlier revision that checked tag names. Claude Code then replaced that check with the closing-tag rule and added the speak() docs, both in response to review feedback on this PR.

🤖 Generated with Claude Code

ciampo and others added 2 commits December 3, 2026 17:58
`speak()` removed anything between angle brackets, so a filename such as `report <draft>.pdf` was announced without `<draft>`, and `1 < 2 and 3 > 2` lost its middle. It now removes only HTML elements the browser knows and comments.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@github-actions github-actions Bot added the [Package] A11y /packages/a11y label Dec 3, 2026
@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

🤖 PR meta 🤖

Preview in WordPress Playground Preview in Storybook

🎉 Props

If you're merging code through a pull request on GitHub, copy and paste the following into the bottom of the merge commit message.

Co-authored-by: ciampo <mciampini@git.wordpress.org>
Co-authored-by: aduth <aduth@git.wordpress.org>
Co-authored-by: joedolson <joedolson@git.wordpress.org>

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


Props Bot commands

You can trigger Props Bot actions by commenting on this PR:

  • @propsbot add <username> to give props to a GitHub or WordPress.org user. Requires write access.
  • @propsbot remove <username> to leave a user out of the props. Requires write access.
  • @propsbot refresh to refresh this list. Adding the props-bot label works too.

Updated as activity occurs, without notifying anyone named here. Add the props-bot label to refresh.

📦 Bundle size

Size Change: +484 B (+0.01%)

Total Size: 8.86 MB

📦 View Changed
Filename Size Change
build/modules/a11y/index.min.js 598 B +244 B (+68.93%) 🆘
build/scripts/a11y/index.min.js 1.19 kB +240 B (+25.16%) 🚨

be4bc8c Run

⚡ Performance

Show the results

Client side metrics exclude the server response time.

front-end-block-theme

Metric fe02e82 trunk % Change
timeToFirstByte 58.65 ms +6.65% -6.82% 56.6 ms +11.04% -4.86% 3.62%
largestContentfulPaint 98 ms +4.08% -8.16% 92 ms +4.35% -8.7% 6.52%
lcpMinusTtfb 37.5 ms +7.47% -18.67% 33.1 ms +15.11% -17.07% 13.29%
wpBeforeTemplate 31.3 ms +3.29% -8.82% 29.24 ms +13.27% -3.35% 7.05%
wpTemplate 23.29 ms +8.03% -6.05% 22.59 ms +3.94% -4.47% 3.1%
wpTotal 54.52 ms +5.8% -7.34% 52.62 ms +11.29% -5.11% 3.61%
wpMemoryUsage 7.76 MB +0% -0% 7.72 MB +0% -0% 0.45%
wpDbQueries 16 +0% -0% 16 +0% -0% 0%

front-end-classic-theme

Metric fe02e82 trunk % Change
timeToFirstByte 49.3 ms +5.27% -6.09% 45.75 ms +8.52% -1.97% 7.76%
largestContentfulPaint 84 ms +4.76% -4.76% 80 ms +10% -0% 5%
lcpMinusTtfb 35.7 ms +5.6% -2.94% 35.3 ms +2.69% -2.12% 1.13%
wpBeforeTemplate 26.66 ms +10.02% -5.48% 24.9 ms +11.93% -0.52% 7.07%
wpTemplate 17.51 ms +4.97% -4.34% 17.09 ms +2.34% -5.32% 2.46%
wpTotal 44.96 ms +5.58% -4.98% 42.31 ms +8.11% -1.99% 6.26%
wpMemoryUsage 6.47 MB +0% -0% 6.43 MB +0% -0% 0.55%
wpDbQueries 13 +0% -0% 13 +0% -0% 0%

media-processing

Metric fe02e82 trunk % Change
mediaProcessingJpeg 416.8 ms +1.08% -1.02% 418.29 ms +1.51% -0.5% -0.36%
mediaProcessingAvif 6231.76 ms +0.46% -0.17% 6208.5 ms +0.08% -0.05% 0.37%
mediaProcessingJpegToAvif 4390.75 ms +0.02% -0.15% 4360.38 ms +0.1% -0.42% 0.7%

media-upload

Metric fe02e82 trunk % Change
jpegUploadProcessing 1959.38 ms +5.23% -21.81% 1503.95 ms +1.67% -0.81% 30.28%
pngUploadProcessing 268.01 ms +3.82% -1.11% 273.36 ms +2.04% -1.74% -1.96%
largeJpegUploadProcessing 1514.24 ms +2.57% -1.21% 1501.02 ms +0.3% -0.57% 0.88%
multipleImageUploadProcessing 2103.52 ms +0.93% -2.29% 2116.44 ms +0.31% -19.15% -0.61%

post-editor

Metric fe02e82 trunk % Change
serverResponse 492.92 ms +1.75% -0.68% 485.85 ms +0.53% -10.02% 1.46%
firstPaint 447.66 ms +4.45% -2.42% 384.93 ms +7.29% -2.41% 16.3%
domContentLoaded 830.16 ms +1.5% -2.76% 829.29 ms +2.39% -1.78% 0.1%
loaded 831.51 ms +1.53% -2.75% 830.53 ms +2.41% -1.75% 0.12%
firstContentfulPaint 472.64 ms +3.34% -5.09% 455.1 ms +4.98% -2.1% 3.85%
firstBlock 2248.14 ms +4.1% -1.87% 2226.6 ms +2.58% -0.35% 0.97%
type 18.49 ms +9.3% -14.12% 18.52 ms +4.1% -7.51% -0.16%
typeWithoutInspector 17.67 ms +10.75% -11.6% 17.71 ms +3.22% -9.82% -0.23%
typeWithTopToolbar 24.32 ms +3.17% -13.03% 23.45 ms +7.25% -6.74% 3.71%
typeContainer 8.43 ms +10.32% -4.98% 8.25 ms +11.88% -11.64% 2.18%
focus 101.45 ms +11.3% -2.21% 103.14 ms +4.68% -6.27% -1.64%
firstFocus 269.03 ms +0% -0% 259.23 ms +0% -0% 3.78%
selectAll 388.28 ms +6.02% -0.32% 389.3 ms +0.49% -1.82% -0.26%
undo 29.74 ms +10.02% -5.21% 29.72 ms +6.26% -3.36% 0.07%
listViewOpen 38.34 ms +8.35% -1.88% 38.42 ms +12.6% -2.71% -0.21%
inserterOpen 41.54 ms +5.1% -9.56% 40.75 ms +4.02% -16.02% 1.94%
inserterHover 10.91 ms +6.97% -6.87% 11.12 ms +6.29% -2.07% -1.89%
inserterSearch 8.16 ms +4.66% -5.76% 8.08 ms +10.4% -8.42% 0.99%
loadPatterns 653.48 ms +7.26% -2.75% 678.8 ms +3.83% -7.52% -3.73%
wpTotal 483.22 ms +1.56% -0.69% 476.2 ms +0.5% -10.26% 1.47%
wpMemoryUsage 14.86 MB +0% -0% 14.82 MB +0% -0% 0.25%
wpDbQueries 55 +0% -0% 55 +0% -0% 0%

site-editor

Metric fe02e82 trunk % Change
serverResponse 472.31 ms +0.84% -7.31% 474.97 ms +1.3% -1.08% -0.56%
firstPaint 408.62 ms +13.06% -10.9% 460.17 ms +1.48% -3.9% -11.2%
domContentLoaded 827 ms +2.52% -0.99% 834.01 ms +1.25% -1.03% -0.84%
loaded 828.33 ms +2.49% -1.01% 835.14 ms +1.29% -1.03% -0.82%
firstContentfulPaint 460.83 ms +1.13% -2.75% 462.36 ms +2.76% -0.5% -0.33%
firstBlock 2607.84 ms +1.76% -1.08% 2604.46 ms +1.49% -1.55% 0.13%
type 17.06 ms +9.61% -6.62% 17.75 ms +3.27% -10.7% -3.89%
navigate 113.79 ms +6.18% -2.33% 106.08 ms +3.24% -3.63% 7.27%
loadPatterns 1655.76 ms +6.09% -5.49% 1558.89 ms +2.72% -15.29% 6.21%
loadPages 959.46 ms +1.92% -2.17% 959.81 ms +1.08% -1.25% -0.04%
wpTotal 462.61 ms +0.93% -7.42% 465.35 ms +1.36% -1.09% -0.59%
wpMemoryUsage 12.84 MB +0% -0% 12.80 MB +0% -0% 0.28%
wpDbQueries 45 +0% -0% 45 +0% -0% 0%

be4bc8c Run

🏁 Flaky tests

Some tests passed with failed attempts. The failures may not be related to this commit but are still reported for visibility. See the documentation for more information.

Navigates the items list via UP/DOWN arrow keys in /test/e2e/specs/site-editor/dataviews-list-layout-keyboard.spec.js, passed after 1 failed attempt.
Error: expect(locator).toBeFocused() failed

Locator:  getByLabel('Page Two')
Expected: focused
Received: inactive
Timeout:  5000ms

Call log:
  - Expect "toBeFocused" getByLabel('Page Two') with timeout 5000ms
  - waiting for getByLabel('Page Two')
    14 × locator resolved to <button type="button" tabindex="-1" aria-pressed="false" id="view-list-0-212-item-wrapper" class="dataviews-view-list__item" aria-labelledby="view-list-0-212-label" aria-describedby="view-list-0-212-description"></button>
       - unexpected value "inactive"

    at /home/runner/work/gutenberg/gutenberg/test/e2e/specs/site-editor/dataviews-list-layout-keyboard.spec.js:146:49

be4bc8c Run

Checking a tag name with `document.createElement()` ran the constructor of a registered custom element. Names are now checked in an inert document. `<!…>` is stripped only for comments and doctypes, and the changelog and Storybook note state what is still removed.
A tag name now ends at ASCII whitespace or `/`, so a valid custom element name such as `wp-notice.v2` is stripped, and a name the browser rejects is kept as text. Comments are removed whole first, so angle brackets inside them aren't announced. Also restores the escaped no-break space.
@ciampo ciampo self-assigned this Dec 3, 2026
@ciampo ciampo added [Type] Enhancement A suggestion for improvement. [Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). labels Dec 3, 2026
@ciampo
ciampo marked this pull request as ready for review December 3, 2026 17:26
@aduth

aduth commented Dec 3, 2026 •

Copy link
Copy Markdown
Member

Wondering if an alternative to creating a whole DOM would be to only strip things that look like HTML tags if there's a matching closing tag (or self-closing), e.g. using a regular expression capture group plus a \1 backreference (I have a similar pattern in another project of mine).

@joedolson

Copy link
Copy Markdown
Contributor

There's a related case in #78485 where the message strips out angle brackets before sending to speak(). That's not a case that this would solve, because the issue there was HTML elements as values in the content.

I think that, in principle, this PR is an improvement - it does a better job of avoiding stripping unneeded things; I'm just wondering if it wouldn't be helpful to document what people need to do if they need to communicate an HTML element as a value within the spoken message.

A tag is now removed only when the message shows that it is markup: it is self-closing, its name has both an opening and a closing tag in the message, or it is a void element. This replaces the lookup of the name in an inert document, so an HTML element name on its own, such as the `<b>` in `notes <b>.txt` or a `<search>` that a message names, is announced. Paired SVG and MathML tags are removed again. The filter no longer needs the DOM, so its test runs without jsdom.

The `speak()` docs now say which tags are removed and how to name an HTML element in a message.
@ciampo

ciampo commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks both, I changed the approach in be4bc8c.

@aduth I went with your idea and dropped the DOM lookup. speak() now strips a tag only if it's self-closing, if the message also has its matching closing tag, or if it's a void element like <br>. I didn't use a single backreference regex because nested tags with the same name (<div><div>…</div></div>) trip it up. The filter collects the opened and closed names first, then strips the ones that have both.

@joedolson a nice side effect is that a lone <search> or <form> no longer gets dropped, so the case from #78485 would be announced with the names in it. Void elements like <img> are still stripped though, so I added a note to the speak() docs that suggests naming an element without the angle brackets, like "the search element".

The trade-off is that HTML with missing closing tags, like <p>One<p>Two, is now announced with its tags. I think that's rare for speak() messages, but tell me if you know of callers that do this.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

[Focus] Accessibility (a11y) Changes that impact accessibility and need corresponding review (e.g. markup changes). [Package] A11y /packages/a11y [Type] Enhancement A suggestion for improvement.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants