Make WordPress Core


Ignore:
Timestamp:
09/04/2024 04:32:37 AM (17 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.php

    r58977 r58985  
    10811081                $doctype = $this->get_doctype_info();
    10821082                if ( null !== $doctype && 'quirks' === $doctype->indicated_compatability_mode ) {
    1083                     $this->state->document_mode = WP_HTML_Processor_State::QUIRKS_MODE;
     1083                    $this->compat_mode = WP_HTML_Tag_Processor::QUIRKS_MODE;
    10841084                }
    10851085
     
    10961096         */
    10971097        initial_anything_else:
    1098         $this->state->document_mode  = WP_HTML_Processor_State::QUIRKS_MODE;
     1098        $this->compat_mode           = WP_HTML_Tag_Processor::QUIRKS_MODE;
    10991099        $this->state->insertion_mode = WP_HTML_Processor_State::INSERTION_MODE_BEFORE_HTML;
    11001100        return $this->step( self::REPROCESS_CURRENT_NODE );
     
    24492449                 */
    24502450                if (
    2451                     WP_HTML_Processor_State::QUIRKS_MODE !== $this->state->document_mode &&
     2451                    WP_HTML_Tag_Processor::QUIRKS_MODE !== $this->compat_mode &&
    24522452                    $this->state->stack_of_open_elements->has_p_in_button_scope()
    24532453                ) {
     
    49384938     *
    49394939     * @since 6.6.0 Subclassed for the HTML Processor.
     4940     *
     4941     * @todo When reconstructing active formatting elements with attributes, find a way
     4942     *       to indicate if the virtually-reconstructed formatting elements contain the
     4943     *       wanted class name.
    49404944     *
    49414945     * @param string $wanted_class Look for this CSS class name, ASCII case-insensitive.
Note: See TracChangeset for help on using the changeset viewer.