Make WordPress Core


Ignore:
Timestamp:
11/13/2024 04:13:52 PM (3 months ago)
Author:
cbravobernal
Message:

HTML API: Include doctype in full parser serialize.

Output DOCTYPE when calling WP_HTML_Processor::serialize on a full document that includes a DOCTYPE.

The DOCTYPE should be included in the serialized/normalized HTML output as it has an impact in how the document is handled, in particular whether the document should be handled in quirks or no-quirks mode.

This only affects the serialization of full parsers at this time because DOCTYPE tokens are currently ignored in all possible fragments. The omission of the DOCTYPE is subtle but can change the serialized document's quirks/no-quirks mode.

Reviewed by cbravobernal.
Merges [59399] to the 6.7 branch.

Props jonsurrell.
Fixes #62396.

Location:
branches/6.7
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/6.7

  • branches/6.7/tests/phpunit/tests/html-api/wpHtmlProcessor-serialize.php

    r59076 r59401  
    285285        );
    286286    }
     287
     288    /**
     289     * @ticket 62396
     290     *
     291     * @dataProvider data_provider_serialize_doctype
     292     */
     293    public function test_full_document_serialize_includes_doctype( string $doctype_input, string $doctype_output ) {
     294        $processor = WP_HTML_Processor::create_full_parser(
     295            "{$doctype_input}👌"
     296        );
     297        $this->assertSame(
     298            "{$doctype_output}<html><head></head><body>👌</body></html>",
     299            $processor->serialize()
     300        );
     301    }
     302
     303    /**
     304     * Data provider.
     305     *
     306     * @return array[]
     307     */
     308    public static function data_provider_serialize_doctype() {
     309        return array(
     310            'None'                   => array( '', '' ),
     311            'Empty'                  => array( '<!DOCTYPE>', '<!DOCTYPE>' ),
     312            'HTML5'                  => array( '<!DOCTYPE html>', '<!DOCTYPE html>' ),
     313            'Strange name'           => array( '<!DOCTYPE WordPress>', '<!DOCTYPE wordpress>' ),
     314            'With public'            => array( '<!DOCTYPE html PUBLIC "x">', '<!DOCTYPE html PUBLIC "x">' ),
     315            'With system'            => array( '<!DOCTYPE html SYSTEM "y">', '<!DOCTYPE html SYSTEM "y">' ),
     316            'With public and system' => array( '<!DOCTYPE html PUBLIC "x" "y">', '<!DOCTYPE html PUBLIC "x" "y">' ),
     317            'Weird casing'           => array( '<!docType HtmL pubLIc\'xxx\'"yyy" all this is ignored>', '<!DOCTYPE html PUBLIC "xxx" "yyy">' ),
     318        );
     319    }
    287320}
Note: See TracChangeset for help on using the changeset viewer.