Skip to content

Commit b41f760

Browse files
committed
Add utility for semantically comparing a SCRIPT tag within HTML
1 parent 44f1517 commit b41f760

File tree

1 file changed

+22
-0
lines changed

1 file changed

+22
-0
lines changed

tests/phpunit/tests/dependencies/scripts.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,28 @@ public function tear_down() {
6969
parent::tear_down();
7070
}
7171

72+
private function assertEqualHTMLScriptTagById( string $expected, string $html, ?string $message ) {
73+
$find_id_tag_processor = new WP_HTML_Tag_Processor( $expected );
74+
$find_id_tag_processor->next_token();
75+
$id = $find_id_tag_processor->get_attribute( 'id' );
76+
assert( is_string( $id ) );
77+
78+
$processor = ( new class('', WP_HTML_Processor::CONSTRUCTOR_UNLOCK_CODE ) extends WP_HTML_Processor {
79+
public function get_script_html() {
80+
assert( 'SCRIPT' === $this->get_tag() );
81+
$this->set_bookmark( 'here' );
82+
$span = $this->bookmarks['_here'];
83+
return substr( $this->html, $span->start, $span->length );
84+
}
85+
} )::create_fragment( $html );
86+
87+
while ( $processor->next_tag( 'SCRIPT' ) && $processor->get_attribute( 'id' ) !== $id ) {
88+
// Loop until we find the right script tag.
89+
}
90+
$this->assertSame( 'SCRIPT', $processor->get_tag(), "Matching tag `script#{$id}` could not be found." );
91+
$this->assertEqualHTML( $expected, $processor->get_script_html(), '<body>', $message );
92+
}
93+
7294
/**
7395
* Test versioning
7496
*

0 commit comments

Comments
 (0)