Opened 6 weeks ago
Last modified 3 weeks ago
#61531 assigned enhancement
HTML API: Audit class name methods for consistency and correctness
Reported by: |
|
Owned by: | |
---|---|---|---|
Milestone: | Future Release | Priority: | normal |
Severity: | normal | Version: | 6.5 |
Component: | HTML API | Keywords: | has-patch has-unit-tests |
Focuses: | Cc: |
Description (last modified by )
The Tag Processor CSS class name methods class_list
, add_class
, remove_class
, and has_class
. These should behave consistently regarding case-sensistivity.
These methods are intended to align with CSS class selectors. CSS class selector matching behavior is complicated and may depend on the document. This makes it difficult to determine the correct behavior for these methods.
At the moment, class_list
yields ASCII lower-cased class names, but remove_class
and add_class
match case sensitive class names.
Attachments (2)
Change History (10)
This ticket was mentioned in PR #6931 on WordPress/wordpress-develop by @jonsurrell.
6 weeks ago
#1
- Keywords has-patch has-unit-tests added
#2
@
6 weeks ago
- Description modified (diff)
- Milestone changed from 6.7 to Future Release
- Owner jonsurrell deleted
- Summary changed from HTML API: Tag processor class name methods should behave consistently with case sensitivity to HTML API: Audit class name methods for consistency and correctness
- Type changed from defect (bug) to enhancement
After further review, it's difficult to determine exactly the correct behavior of these class methods should be. I've updated the summary to talk about auditing the behavior and I've changed the milestone to "future release."
@jonsurrell commented on PR #6931:
6 weeks ago
#3
This doesn't seem like the right approach, it may not align with browser behavior.
#4
@
6 weeks ago
After a lengthy and extremely valuable chat with @jonsurrell and some independent testing I think we can say comfortably that the behavior in the HTML API is wrong, but that making it right is a bit more complicated than a simple bug fix would be.
In the HTML API we have to make ourselves aware of the quirks mode of the document being parsed. For normative posts it's probably safe to assume no quirks mode, as most WordPress themes output an HTML doctype that enters no-quirks mode. But, we may encounter quirks-mode documents, and the HTML Processor will likely need to communicate eventually which one a given document is.
When matching against a document which is in quirks mode, class names must be matched ASCII case-insensitively; class selectors are otherwise case-sensitive, only matching class names they are identical to.
I created the following document to illustrate the relevant semantics: in no-quirks
mode, CSS class selectors match byte-for-byte with what's in the decoded class
attribute; in quirks
mode, they match in an ASCII-case-insensitive manner.
Remove the <!DOCTYPE html>
line to test in quirks mode.
<!DOCTYPE html> <style> .one { border-left: 3px solid red; } .ONE { border-top: 3px solid red; } .right { border-right: 3px solid blue; } .green { color: green; } .italic { font-style: italic; } .underline { text-decoration: underline; } </style> <div> <p class="one">One</p> <p class="ONE">ONE</p> <p class="one">&#x64;ne</p> <p class="ONE">&#79;NE</p> <p class="oNe one ONE oNe one oNe ONE">All of them</p> </div> <script> const by = s => document.querySelectorAll( s ); by( '.one' ).forEach( node => node.classList.add('grEen') ); by( '.ONE' ).forEach( node => node.classList.add('right') ); by( '[class~="one"]' ).forEach( node => node.classList.add( 'italic' ) ); by( '[class~="ONE"]' ).forEach( node => node.classList.add( 'underline' ) ); </script>
No-Quirks Mode | Quirks Mode |
---|---|
![]() | ![]() |
This ticket was mentioned in PR #6933 on WordPress/wordpress-develop by @jonsurrell.
3 weeks ago
#6
The HTML API remove_class
regarding white space opts to maintain "inter-class" whitespace.
This seems reasonable and justified, but normalization seems inconsistent. Leading space is maintained, but trailing space is not.
It seems more consistent to trim leading and trailing space, and actually maintain _only_ inter-class whitespace.
Trac ticket: https://core.trac.wordpress.org/ticket/61531 ~https://core.trac.wordpress.org/ticket/61655~
Trac ticket: https://core.trac.wordpress.org/ticket/61531
Related to #61520 which documents the lower-casing behavior of
class_list
.