Skip to content

HTML API: Deduplicate class names with a keyed set - #15223

Draft
sirreal wants to merge 1 commit into
WordPress:trunkfrom
sirreal:html-api/class-list-keyed-set
Draft

sirreal wants to merge 1 commit into
WordPress:trunkfrom
sirreal:html-api/class-list-keyed-set

Conversation

@sirreal

@sirreal sirreal commented Oct 30, 2026 •

Copy link
Copy Markdown
Member

Replace in_array() scans with keyed isset() lookups in WP_HTML_Tag_Processor::class_list() and class attribute updates. Class order, quirks-mode case handling and whitespace preservation are unchanged.

This also fixes add_class() and remove_class() for numeric class names in no-quirks mode: PHP converts integer-string keys to integers, which the previous strict comparisons did not match. Distinct numeric strings remain distinct.

Tests cover numeric class names, duplicate removal and class updates. The new add/remove test fails on trunk.

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

Use of AI Tools

AI assistance: Yes
Tool(s): Claude Code, Codex
Model(s): Claude Opus 5.5, GPT-6
Used for: Implementation, tests, drafting and description editing.


This Pull Request is for code review only. Please keep all other discussion in the Trac ticket. Do not merge this Pull Request. See GitHub Pull Requests for Code Review in the Core Handbook for more details.

`WP_HTML_Tag_Processor::class_list()` deduplicates class names by scanning the list of names it has already yielded with `in_array()`. That list grows with each distinct name, so listing n distinct names takes O(n²) time. `has_class()` and `next_tag()` with a `class_name` query iterate `class_list()`. `class_name_updates_to_attributes_updates()` scans its `$to_remove` and `$seen` lists the same way for every name in the attribute and for every added name.

Key the `$seen` and `$to_remove` arrays by class name and test membership with `isset()`, which takes constant time. PHP stores a canonical integer string such as "123" as the integer key 123, and no other string maps to that key, so distinct names stay distinct. Yield order, quirks-mode lowercasing and the whitespace-preserving attribute rebuild are unchanged.

This also fixes `add_class()` and `remove_class()` in no-quirks mode for class names which are canonical integer strings. `$classname_updates` stores such a name under an integer key, and the strict `in_array()` comparison of that integer with the string names from the attribute never matched. On `<div class="123 abc">`, `remove_class( '123' )` produced `<div class="123 abc">` and now produces `<div class="abc">`; `add_class( '123' )` produced `<div class="123 abc 123">` and now produces `<div class="123 abc">`.

The `class_list()` data provider gains numeric-looking class names, and a new test covers `add_class()` and `remove_class()` with numeric class names.
@github-actions

Copy link
Copy Markdown

Test using WordPress Playground

The changes in this pull request can previewed and tested using a WordPress Playground instance.

WordPress Playground is an experimental project that creates a full WordPress instance entirely within the browser.

Some things to be aware of

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

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

Test this pull request with WordPress Playground.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant