Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the order in which ensure_required_markup() is called #1041

Merged
merged 9 commits into from
Mar 28, 2018

Conversation

amedina
Copy link
Member

@amedina amedina commented Mar 23, 2018

The order in which ensure_required_markup was being called was causing the viewport meta tag to be stripped out by the sanitizer.

@amedina amedina requested a review from westonruter March 23, 2018 01:40
@westonruter
Copy link
Member

This is a regression introduced by me in 0154620

$scripts[] = $script;
}
foreach ( $scripts as $script ) {
$head->appendChild( $script );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The $head is undefined. We should grab it with DOMXPath and only do the script gathering if it is defined.

@westonruter westonruter added this to the v0.7 milestone Mar 23, 2018
@westonruter
Copy link
Member

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 \Test_AMP_Theme_Support::test_prepare_response() could be made which specifically tests the validation of a non-AMP theme like Twenty Fifteen. Take some of the actual output of Twenty Fifteen (in particular the AMP-invalid meta viewport) and running it through the sanitizer to ensure the expected viewport comes out the other end. Similarly, inside the body there could be a call to <?php wp_print_scripts( 'amp-mathml' ); ?> and then test to make sure that the script tag gets moved to the head properly.

$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 ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 );
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since AMP must be UTF-8, should this use not be a literal UTF-8?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@amedina amedina self-assigned this Mar 24, 2018
@amedina amedina force-pushed the amedina/fix-order-of-ensure-required-markup branch 3 times, most recently from 0c6c205 to 6ce2206 Compare March 24, 2018 23:09
@amedina amedina force-pushed the amedina/fix-order-of-ensure-required-markup branch from 6ce2206 to d61de17 Compare March 26, 2018 22:38
@amedina amedina force-pushed the amedina/fix-order-of-ensure-required-markup branch from e7f90a7 to 6cef7e6 Compare March 26, 2018 23:27
Copy link
Collaborator

@ThierryA ThierryA left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@amedina thanks for fixing the viewport bug 👍I left my CR below.

* @covers AMP_Theme_Support::prepare_response()
*/
public function test_validate_non_amp_theme() {
global $wp_widget_factory, $wp_scripts;
Copy link
Collaborator

@ThierryA ThierryA Mar 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$wp_widget_factory doesn't seem to be used in this method and the $wp_scripts are already reset in the tearDown() method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

</html>
<?php
$original_html = trim( ob_get_clean() );
$removed_nodes = array();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

$removed_nodes doesn't seem to be used in this method.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.


if ( isset( $head ) ) {
// Make sure scripts from the body get moved to the head.
$scripts = array();
Copy link
Collaborator

@ThierryA ThierryA Mar 27, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 );
}

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense :)

@@ -220,7 +220,6 @@ public function test_prepare_response() {
* @covers AMP_Theme_Support::prepare_response()
*/
public function test_validate_non_amp_theme() {
global $wp_widget_factory, $wp_scripts;
$wp_scripts = null;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line still needs to be removed and this PR will be good to go. Thanks @amedina, great stuff!

@amedina amedina force-pushed the amedina/fix-order-of-ensure-required-markup branch from e4c7388 to 38edc74 Compare March 28, 2018 16:48
@amedina amedina merged commit d53af32 into 0.7 Mar 28, 2018
@westonruter westonruter deleted the amedina/fix-order-of-ensure-required-markup branch March 30, 2018 04:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants