Repository navigation
Conversation
`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.
Test using WordPress PlaygroundThe 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
For more details about these limitations and more, check out the Limitations page in the WordPress Playground documentation. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Replace
in_array()scans with keyedisset()lookups inWP_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()andremove_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.