Skip to content

Commit

Permalink
Merge pull request #73 from 10up/fix/assert-equal-markup
Browse files Browse the repository at this point in the history
Eliminate `WP_HTML_Tag_Processor` in favor of `DOMDocument` for `assertMarkupEquals`
  • Loading branch information
westonruter authored Jun 8, 2023
2 parents 3061a8f + 6d2ff36 commit 5ae3a5b
Showing 1 changed file with 11 additions and 45 deletions.
56 changes: 11 additions & 45 deletions tests/phpunit/tests/dependencies/scripts.php
Original file line number Diff line number Diff line change
Expand Up @@ -1791,10 +1791,7 @@ public function test_wp_add_inline_script_before_after_concat_with_core_dependen
$print_scripts // Printed scripts.
);

$this->assertSameIgnoreEOL(
$this->normalize_markup( $expected ),
$this->normalize_markup( $print_scripts )
);
$this->assertEqualMarkup( $expected, $print_scripts );
}

/**
Expand Down Expand Up @@ -2660,48 +2657,17 @@ protected function get_delayed_inline_script_loader_script_tag() {
}

/**
* Normalize markup using WP_HTML_Tag_Processor.
*
* Attributes are sorted alphabetically and values are made to use double quotes instead of single quotes.
* Parse an HTML markup fragment.
*
* @param string $markup Markup.
* @return string Normalized markup.
* @return DOMElement Body element wrapping supplied markup fragment.
*/
protected function normalize_markup( $markup ) {
$p = new WP_HTML_Tag_Processor( $markup );
while ( $p->next_tag() ) {
if ( $p->is_tag_closer() ) {
continue;
}
$attribute_names = $p->get_attribute_names_with_prefix( '' );
sort( $attribute_names );
$attributes = array();
foreach ( $attribute_names as $attribute_name ) {
// For some reason these are considered attributes.
if ( '<' === $attribute_name || strtoupper( $attribute_name ) === $p->get_tag() ) {
continue;
}
$attributes[ $attribute_name ] = $p->get_attribute( $attribute_name );
$p->remove_attribute( $attribute_name );
}
$p->get_updated_html(); // This seems to be required to "commit" the changes, otherwise re-adding them below will result in no change.
foreach ( $attributes as $attribute_name => $attribute_value ) {
$p->set_attribute( $attribute_name, $attribute_value );
}
}

$normalized = $p->get_updated_html();

// Normalize inside of IE conditional comments which the HTML tag processor rightfully skips over.
$normalized = preg_replace_callback(
'#(<!--\[[^\]]+?\]>)(.+?)(<!\[endif\]-->)#s',
function ( $matches ) {
return $matches[1] . $this->normalize_markup( $matches[2] ) . $matches[3];
},
$normalized
protected function parse_markup_fragment( $markup ) {
$dom = new DOMDocument();
@$dom->loadHTML(
"<!DOCTYPE html><html><head><meta charset=utf8></head><body>{$markup}</body></html>"
);

return $normalized;
return $dom->getElementsByTagName( 'body' )->item( 0 );
}

/**
Expand All @@ -2712,9 +2678,9 @@ function ( $matches ) {
* @param string $message Message.
*/
protected function assertEqualMarkup( $expected, $actual, $message = '' ) {
$this->assertSame(
$this->normalize_markup( $expected ),
$this->normalize_markup( $actual ),
$this->assertEquals(
$this->parse_markup_fragment( $expected ),
$this->parse_markup_fragment( $actual ),
$message
);
}
Expand Down

0 comments on commit 5ae3a5b

Please sign in to comment.