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/tests/phpunit/tests/html-api/wpHtmlProcessorHtml5lib.php

    r58870 r58925  
    2828                'comments01/line0155'    => 'Unimplemented: Need to access raw comment text on non-normative comments.',
    2929                'comments01/line0169'    => 'Unimplemented: Need to access raw comment text on non-normative comments.',
     30                'doctype01/line0380'     => 'Bug: Mixed whitespace, non-whitespace text in head not split correctly',
    3031                'html5test-com/line0129' => 'Unimplemented: Need to access raw comment text on non-normative comments.',
    3132                'noscript01/line0014'    => 'Unimplemented: This parser does not add missing attributes to existing HTML or BODY tags.',
    … …  
    116117                                $test_context_element = $test[1];
    117118
    118                                 if ( self::should_skip_test( $test_context_element, $test_name, $test[3] ) ) {
     119                                if ( self::should_skip_test( $test_context_element, $test_name ) ) {
    119120                                        continue;
    120121                                }
    … …  
    134135         * @return bool True if the test case should be skipped. False otherwise.
    135136         */
    136         private static function should_skip_test( ?string $test_context_element, string $test_name, string $expected_tree ): bool {
     137        private static function should_skip_test( ?string $test_context_element, string $test_name ): bool {
    137138                if ( null !== $test_context_element && 'body' !== $test_context_element ) {
    138139                        return true;
    … …  
    190191
    191192                        switch ( $token_type ) {
     193                                case '#doctype':
     194                                        $doctype = $processor->get_doctype_info();
     195                                        $output .= "<!DOCTYPE {$doctype->name}";
     196                                        if ( null !== $doctype->public_identifier || null !== $doctype->system_identifier ) {
     197                                                $output .= " \"{$doctype->public_identifier}\" \"{$doctype->system_identifier}\"";
     198                                        }
     199                                        $output .= ">\n";
     200                                        break;
     201
    192202                                case '#tag':
    193203                                        $namespace = $processor->get_namespace();
    … …  
    451461                                case 'document':
    452462                                        if ( '|' === $line[0] ) {
    453                                                 /*
    454                                                  * The next_token() method these tests rely on do not stop
    455                                                  * at doctype nodes. Strip doctypes from output.
    456                                                  * @todo Restore this line if and when the processor
    457                                                  * exposes doctypes.
    458                                                  */
    459                                                 if ( '| <!DOCTYPE ' !== substr( $line, 0, 12 ) ) {
    460                                                         $test_dom .= substr( $line, 2 );
    461                                                 }
     463                                                $test_dom .= substr( $line, 2 );
    462464                                        } else {
    463465                                                // This is a text node that includes unescaped newlines.
Note: See TracChangeset for help on using the changeset viewer.