Make WordPress Core


Ignore:
Timestamp:
09/04/2024 04:32:37 AM (15 months ago)
Author:
dmsnell
Message:

HTML API: Respect document compat mode when handling CSS class names.

The HTML API has been behaving as if CSS class name selectors matched class names in an ASCII case-insensitive manner. This is only true if the document in question is set to quirks mode. Unfortunately most documents processed will be set to no-quirks mode, meaning that some CSS behaviors have been matching incorrectly when provided with case variants of class names.

In this patch, the CSS methods have been audited and updated to adhere to the rules governing ASCII case sensitivity when matching classes. This includes add_class(), remove_class(), has_class(), and class_list(). Now, it is assumed that a document is in no-quirks mode unless a full HTML parser infers quirks mode, and these methods will treat class names in a byte-for-byte manner. Otherwise, when a document is in quirks mode, the methods will compare the provided class names against existing class names for the tag in an ASCII case insensitive way, while class_list() will return a lower-cased version of the existing class names.

The lower-casing in class_list() is performed for consistency, since it's possible that multiple case variants of the same comparable class name exists on a tag in the input HTML.

Developed in https://github.com/WordPress/wordpress-develop/pull/7169
Discussed in https://core.trac.wordpress.org/ticket/61531

Props dmsnell, jonsurrell.
See #61531.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/html-api/class-wp-html-processor-state.php

    r58867 r58985  
    301301
    302302    /**
    303      * No-quirks mode document compatability mode.
    304      *
    305      * > In no-quirks mode, the behavior is (hopefully) the desired behavior
    306      * > described by the modern HTML and CSS specifications.
    307      *
    308      * @since 6.7.0
    309      *
    310      * @var string
    311      */
    312     const NO_QUIRKS_MODE = 'no-quirks-mode';
    313 
    314     /**
    315      * Quirks mode document compatability mode.
    316      *
    317      * > In quirks mode, layout emulates behavior in Navigator 4 and Internet
    318      * > Explorer 5. This is essential in order to support websites that were
    319      * > built before the widespread adoption of web standards.
    320      *
    321      * @since 6.7.0
    322      *
    323      * @var string
    324      */
    325     const QUIRKS_MODE = 'quirks-mode';
    326 
    327     /**
    328303     * The stack of template insertion modes.
    329304     *
     
    381356     */
    382357    public $insertion_mode = self::INSERTION_MODE_INITIAL;
    383 
    384     /**
    385      * Indicates if the document is in quirks mode or no-quirks mode.
    386      *
    387      * Impact on HTML parsing:
    388      *
    389      *  - In `NO_QUIRKS_MODE` CSS class and ID selectors match in a byte-for-byte
    390      *    manner, otherwise for backwards compatability, class selectors are to
    391      *    match in an ASCII case-insensitive manner.
    392      *
    393      *  - When not in `QUIRKS_MODE`, a TABLE start tag implicitly closes an open P tag
    394      *    if one is in scope and open, otherwise the TABLE becomes a child of the P.
    395      *
    396      * `QUIRKS_MODE` impacts many styling-related aspects of an HTML document, but
    397      * none of the other changes modifies how the HTML is parsed or selected.
    398      *
    399      * @see self::QUIRKS_MODE
    400      * @see self::NO_QUIRKS_MODE
    401      *
    402      * @since 6.7.0
    403      *
    404      * @var string
    405      */
    406     public $document_mode = self::NO_QUIRKS_MODE;
    407358
    408359    /**
Note: See TracChangeset for help on using the changeset viewer.