Fix the order in which ensure_required_markup() is called#1041
Conversation
|
This is a regression introduced by me in 0154620 |
includes/class-amp-theme-support.php
Outdated
| $scripts[] = $script; | ||
| } | ||
| foreach ( $scripts as $script ) { | ||
| $head->appendChild( $script ); |
There was a problem hiding this comment.
The $head is undefined. We should grab it with DOMXPath and only do the script gathering if it is defined.
|
It would be good to add a new test case for the bug this is fixed so that it doesn't re-surface. For example another test like |
| $original_html = trim( ob_get_clean() ); | ||
| $removed_nodes = array(); | ||
| $sanitized_html = AMP_Theme_Support::prepare_response( $original_html, array( | ||
| 'validation_error_callback' => function( $removed ) use ( &$removed_nodes ) { |
There was a problem hiding this comment.
I don't think we need validation_error_callback here since $removed_nodes is never checked. Alternative, you could $this->assertEmpty( $removed_nodes ) to verify that nothing got stripped out.
| }, | ||
| ) ); | ||
|
|
||
| $this->assertContains( '<meta charset="' . get_bloginfo( 'charset' ) . '">', $sanitized_html ); |
There was a problem hiding this comment.
Since AMP must be UTF-8, should this use not be a literal UTF-8?
There was a problem hiding this comment.
Agree; changed to $this->assertContains( '<meta charset="utf-8">', $sanitized_html );
| * | ||
| * @global WP_Widget_Factory $wp_widget_factory | ||
| * @global WP_Scripts $wp_scripts | ||
| * @covers AMP_Theme_Support::validate_non_amp_theme() |
There was a problem hiding this comment.
There is no such method as AMP_Theme_Support::validate_non_amp_theme(). It should be AMP_Theme_Support:: prepare_response().
| AMP_Theme_Support::init(); | ||
| AMP_Theme_Support::finish_init(); | ||
| $wp_widget_factory = new WP_Widget_Factory(); | ||
| wp_widgets_init(); |
There was a problem hiding this comment.
Since no widgets are being tested in this response, I think the references to $wp_widget_factory and wp_widgets_init() in this PR can be removed.
0c6c205 to
6ce2206
Compare
6ce2206 to
d61de17
Compare
…ed (and fail) for unknown reason See xwp/wp-dev-lib#270
e7f90a7 to
6cef7e6
Compare
| * @covers AMP_Theme_Support::prepare_response() | ||
| */ | ||
| public function test_validate_non_amp_theme() { | ||
| global $wp_widget_factory, $wp_scripts; |
There was a problem hiding this comment.
$wp_widget_factory doesn't seem to be used in this method and the $wp_scripts are already reset in the tearDown() method.
| </html> | ||
| <?php | ||
| $original_html = trim( ob_get_clean() ); | ||
| $removed_nodes = array(); |
There was a problem hiding this comment.
$removed_nodes doesn't seem to be used in this method.
includes/class-amp-theme-support.php
Outdated
|
|
||
| if ( isset( $head ) ) { | ||
| // Make sure scripts from the body get moved to the head. | ||
| $scripts = array(); |
There was a problem hiding this comment.
Any reason to store the scripts in a var and run two loops rather than doing the following:
foreach ( $xpath->query( '//body//script[ @custom-element or @custom-template ]' ) as $script ) {
$head->appendChild( $script );
}| */ | ||
| public function test_validate_non_amp_theme() { | ||
| global $wp_widget_factory, $wp_scripts; | ||
| $wp_scripts = null; |
There was a problem hiding this comment.
This line still needs to be removed and this PR will be good to go. Thanks @amedina, great stuff!
e4c7388 to
38edc74
Compare
The order in which
ensure_required_markupwas being called was causing the viewport meta tag to be stripped out by the sanitizer.