Make WordPress Core


Ignore:
Timestamp:
08/23/2024 02:53:59 PM (2 years ago)
Author:
dmsnell
Message:

HTML API: Parse DOCTYPE tokens and set HTML parser mode accordingly.

This patch adds until-now missing code to parse the structure of HTML DOCTYPE declarations. The DOCTYPE is mostly unused but can dictate the document compatability mode, which governs whether CSS class names match in a ASCII-case-insensitive way or not, and whether TABLE elements close an open P element.

The DOCTYPE information is made available through a new method on the Tag Processor, get_doctype_info().

Developed in https://github.com/wordpress/wordpress-develop/pull/7195
Discussed in https://core.trac.wordpress.org/ticket/61576

Props dmsnell, jonsurrell.
See #61576.

File:
1 edited

Legend:

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

    r58897 r58925  
    40284028
    40294029        /**
     4030         * Gets DOCTYPE declaration info from a DOCTYPE token.
     4031         *
     4032         * DOCTYPE tokens may appear in many places in an HTML document. In most places, they are
     4033         * simply ignored. The main parsing functions find the basic shape of DOCTYPE tokens but
     4034         * do not perform detailed parsing.
     4035         *
     4036         * This method can be called to perform a full parse of the DOCTYPE token and retrieve
     4037         * its information.
     4038         *
     4039         * @return WP_HTML_Doctype_Info|null The DOCTYPE declaration information or `null` if not
     4040         *                                   currently at a DOCTYPE node.
     4041         */
     4042        public function get_doctype_info(): ?WP_HTML_Doctype_Info {
     4043                if ( self::STATE_DOCTYPE !== $this->parser_state ) {
     4044                        return null;
     4045                }
     4046
     4047                return WP_HTML_Doctype_Info::from_doctype_token( substr( $this->html, $this->token_starts_at, $this->token_length ) );
     4048        }
     4049
     4050        /**
    40304051         * Parser Ready State.
    40314052         *
     
    41184139        /**
    41194140         * Indicates that the parser has found a DOCTYPE node and it's
    4120          * possible to read and modify its modifiable text.
     4141         * possible to read its DOCTYPE information via `get_doctype_info()`.
    41214142         *
    41224143         * @since 6.5.0
Note: See TracChangeset for help on using the changeset viewer.