Skip to content
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Move script parsing adjacent to other script parsing test
  • Loading branch information
sirreal committed Aug 7, 2025
commit f7d4b8e1c41f09dca74a1349602d25f0e3eaf723
86 changes: 43 additions & 43 deletions tests/phpunit/tests/html-api/wpHtmlTagProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -1981,6 +1981,49 @@ public static function data_next_tag_ignores_script_tag_contents() {
);
}

/**
* Test that script tags are parsed correctly.
*
* Script tag parsing is very complicated, see the following resources for more details:
*
* - https://html.spec.whatwg.org/multipage/parsing.html#script-data-state
* - https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
*
* @ticket 63738
*
* @dataProvider data_script_tag
*/
public function test_script_tag_parsing( string $input, bool $closes ) {
$processor = new WP_HTML_Tag_Processor( $input );

if ( $closes ) {
$this->assertTrue( $processor->next_token(), 'Expected to find complete script tag.' );
$this->assertSame( 'SCRIPT', $processor->get_tag() );
return;
}

$this->assertFalse( $processor->next_token(), 'Expected to fail next_token().' );
$this->assertTrue( $processor->paused_at_incomplete_token(), 'Expected an incomplete SCRIPT tag token.' );
}

/**
* Data provider.
*/
public static function data_script_tag(): array {
return array(
'Basic script tag' => array( '<script></script>', true ),
'Script with type attribute' => array( '<script type="text/javascript"></script>', true ),
'Script data escaped' => array( '<script><!--</script>', true ),
'Script data double-escaped exit (comment)' => array( '<script><!--<script>--></script>', true ),
'Script data double-escaped exit (closed)' => array( '<script><!--<script></script></script>', true ),
'Script data double-escaped exit (closed/truncated)' => array( '<script><!--<script></script </script>', true ),
'Script data no double-escape' => array( '<script><!-- --><script></script>', true ),

'Script tag with self-close flag (ignored)' => array( '<script />', false ),
'Script data double-escaped' => array( '<script><!--<script></script>', false ),
);
}

/**
* Invalid tag names are comments on tag closers.
*
Expand Down Expand Up @@ -3046,47 +3089,4 @@ public static function data_alphabet_by_characters_uppercase() {
yield strtoupper( $data[0] ) => array( strtoupper( $data[0] ) );
}
}

/**
* Test that script tags are parsed correctly.
*
* Script tag parsing is very complicated, see the following resources for more details:
*
* - https://html.spec.whatwg.org/multipage/parsing.html#script-data-state
* - https://html.spec.whatwg.org/multipage/scripting.html#restrictions-for-contents-of-script-elements
*
* @ticket 63738
*
* @dataProvider data_script_tag
*/
public function test_script_tag_parsing( string $input, bool $closes ) {
$processor = new WP_HTML_Tag_Processor( $input );

if ( $closes ) {
$this->assertTrue( $processor->next_token(), 'Expected to find complete script tag.' );
$this->assertSame( 'SCRIPT', $processor->get_tag() );
return;
}

$this->assertFalse( $processor->next_token(), 'Expected to fail next_token().' );
$this->assertTrue( $processor->paused_at_incomplete_token(), 'Expected an incomplete SCRIPT tag token.' );
}

/**
* Data provider.
*/
public static function data_script_tag(): array {
return array(
'Basic script tag' => array( '<script></script>', true ),
'Script with type attribute' => array( '<script type="text/javascript"></script>', true ),
'Script data escaped' => array( '<script><!--</script>', true ),
'Script data double-escaped exit (comment)' => array( '<script><!--<script>--></script>', true ),
'Script data double-escaped exit (closed)' => array( '<script><!--<script></script></script>', true ),
'Script data double-escaped exit (closed/truncated)' => array( '<script><!--<script></script </script>', true ),
'Script data no double-escape' => array( '<script><!-- --><script></script>', true ),

'Script tag with self-close flag (ignored)' => array( '<script />', false ),
'Script data double-escaped' => array( '<script><!--<script></script>', false ),
);
}
}
Loading