Make WordPress Core


Ignore:
Timestamp:
11/27/2024 02:33:46 PM (16 months ago)
Author:
Bernhard Reiter
Message:

HTML API: Allow more contexts in create_fragment.

This changeset modifies WP_HTML_Processor::create_fragment( $html, $context ) to use a full processor and create_fragment_at_node instead of the other way around. This makes more sense and makes the main factory methods more clear, where the state required for fragments is set up in create_fragment_at_node instead of in both create_fragment and create_fragment_at_current_node.

This allows for more HTML contexts to be provided to the basic create_fragment where the provided context HTML is appended to <!DOCTYPE html>, a full processor is created, the last tag opener is found, and a fragment parser is created at that node via create_fragment_at_current_node.

The HTML5lib tests are updated accordingly to use this new method to create fragments.

Props jonsurrell, dmsnell, bernhard-reiter.
Fixes #62584.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/tests/phpunit/tests/html-api/wpHtmlProcessor.php

    r59450 r59467  
    10451045
    10461046    /**
    1047      * @ticket 62357
    1048      */
    1049     public function test_create_fragment_at_current_node_in_foreign_content() {
    1050         $processor = WP_HTML_Processor::create_full_parser( '<svg>' );
    1051         $this->assertTrue( $processor->next_tag( 'SVG' ) );
    1052 
    1053         $fragment = $processor->create_fragment_at_current_node( "\0preceded-by-nul-byte<rect /><circle></circle><foreignobject><div></div></foreignobject><g>" );
    1054 
    1055         $this->assertSame( 'svg', $fragment->get_namespace() );
    1056         $this->assertTrue( $fragment->next_token() );
    1057 
    1058         /*
    1059          * In HTML parsing, a nul byte would be ignored.
    1060          * In SVG it should be replaced with a replacement character.
    1061          */
    1062         $this->assertSame( '#text', $fragment->get_token_type() );
    1063         $this->assertSame( "\u{FFFD}", $fragment->get_modifiable_text() );
    1064 
    1065         $this->assertTrue( $fragment->next_tag( 'RECT' ) );
    1066         $this->assertSame( 'svg', $fragment->get_namespace() );
    1067 
    1068         $this->assertTrue( $fragment->next_tag( 'CIRCLE' ) );
    1069         $this->assertSame( array( 'HTML', 'SVG', 'CIRCLE' ), $fragment->get_breadcrumbs() );
    1070         $this->assertTrue( $fragment->next_tag( 'foreignObject' ) );
    1071         $this->assertSame( 'svg', $fragment->get_namespace() );
    1072     }
    1073 
    1074     /**
    1075      * @ticket 62357
    1076      */
    1077     public function test_create_fragment_at_current_node_in_foreign_content_integration_point() {
    1078         $processor = WP_HTML_Processor::create_full_parser( '<svg><foreignObject>' );
    1079         $this->assertTrue( $processor->next_tag( 'foreignObject' ) );
    1080 
    1081         $fragment = $processor->create_fragment_at_current_node( "<image>\0not-preceded-by-nul-byte<rect />" );
    1082 
    1083         // Nothing has been processed, the html namespace should be used for parsing as an integration point.
    1084         $this->assertSame( 'html', $fragment->get_namespace() );
    1085 
    1086         // HTML parsing transforms IMAGE into IMG.
    1087         $this->assertTrue( $fragment->next_tag( 'IMG' ) );
    1088 
    1089         $this->assertTrue( $fragment->next_token() );
    1090 
    1091         // In HTML parsing, the nul byte is ignored and the text is reached.
    1092         $this->assertSame( '#text', $fragment->get_token_type() );
    1093         $this->assertSame( 'not-preceded-by-nul-byte', $fragment->get_modifiable_text() );
    1094 
    1095         /*
    1096          * svg:foreignObject is an HTML integration point, so the processor should be in the HTML namespace.
    1097          * RECT is an HTML element here, meaning it may have the self-closing flag but does not self-close.
    1098          */
    1099         $this->assertTrue( $fragment->next_tag( 'RECT' ) );
    1100         $this->assertSame( array( 'HTML', 'FOREIGNOBJECT', 'RECT' ), $fragment->get_breadcrumbs() );
    1101         $this->assertSame( 'html', $fragment->get_namespace() );
    1102         $this->assertTrue( $fragment->has_self_closing_flag() );
    1103         $this->assertTrue( $fragment->expects_closer() );
    1104     }
    1105 
    1106     /**
    1107      * @ticket 62357
    1108      */
    1109     public function test_prevent_fragment_creation_on_closers() {
    1110         $processor = WP_HTML_Processor::create_full_parser( '<p></p>' );
    1111         $processor->next_tag( 'P' );
    1112         $processor->next_tag(
    1113             array(
    1114                 'tag_name'    => 'P',
    1115                 'tag_closers' => 'visit',
    1116             )
    1117         );
    1118         $this->assertSame( 'P', $processor->get_tag() );
    1119         $this->assertTrue( $processor->is_tag_closer() );
    1120         $this->assertNull( $processor->create_fragment_at_current_node( '<i>fragment HTML</i>' ) );
    1121     }
    1122 
    1123     /**
    11241047     * Ensure that lowercased tag_name query matches tags case-insensitively.
    11251048     *
Note: See TracChangeset for help on using the changeset viewer.