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

Add AMP_Script_Sanitizer to replace noscript elements with their contents #1226

Merged
merged 3 commits into from
Jun 26, 2018

Conversation

westonruter
Copy link
Member

@westonruter westonruter commented Jun 25, 2018

As noted in Implementing Interactivity:

the baseline experience after enabling AMP on an existing theme is that it largely behaves as if JavaScript is disabled in the user's browser. Many themes and plugins already have fallbacks in case JavaScript is not available so that will serve them well in AMP. Therefore, most of the work entailed is to progressively enhance a theme for AMP to restore features that were lost during this graceful no-JS degradation to AMP

Nevertheless, this is not totally true at present: the plugin's postprocessor does not currently do any transformation of noscript elements meaning that the no-JS fallbacks will be not be successfully used in AMP pages.

So this PR ensures that that a non-AMP document like:

<script>document.write('Scripts!')</script><noscript><em>No scripts!</em></noscript>

Will get served in AMP as:

<!--noscript--><em>No scripts!</em><!--/noscript-->

Thus it encourages the use of no-JS fallbacks for the baseline AMP experience as well as for visitors who have JS turned off in their browsers.

This will be further built upon in #1213 and #1032 with more intelligence about whether to promote the noscript element's contents or rather to convert a companion script to an AMP equivalent.

The base of this PR comes from #1127

@westonruter westonruter added this to the v1.0 milestone Jun 25, 2018
@westonruter westonruter added this to Ready for review in v1.0 Jun 25, 2018
Copy link
Contributor

@kienstra kienstra left a comment

Choose a reason for hiding this comment

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

Approved

Hi @westonruter and @amedina,
This PR looks really good, and has very thorough PHPUnit tests. As you mentioned, this should help the user experience when scripts are removed.

When you have a chance, it'd be good to address the Travis build issues. I could help with that if you'd like.

while ( $noscript->firstChild ) {
$fragment->appendChild( $noscript->firstChild );
}
$fragment->appendChild( $this->dom->createComment( '/noscript' ) );
Copy link
Contributor

Choose a reason for hiding this comment

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

It's nice how this outputs the comment <!--noscript-->. This should help with debugging if needed, like on WordPress.org support topics.

/**
* Test style[amp-boilerplate] preservation.
*/
public function test_boilerplate_preservation() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nice thorough testing of the boilerplate not being removed.

@westonruter
Copy link
Member Author

@kienstra thank you. I'm very confused about why the tests are failing. They all pass for me locally.

@kienstra
Copy link
Contributor

@westonruter, that's strange, the tests pass locally for me also.

@westonruter
Copy link
Member Author

Force-pushed a tweak. Maybe there was a caching problem. The test is failing as if these lines are not present:

--- a/tests/test-amp-script-sanitizer.php
+++ b/tests/test-amp-script-sanitizer.php
@@ -38,8 +38,6 @@ class AMP_Script_Sanitizer_Test extends WP_UnitTestCase {
 	 */
 	public function test_noscript_promotion( $source, $expected = null ) {
 		$dom       = AMP_DOM_Utils::get_dom_from_content( $source );
-		$sanitizer = new AMP_Script_Sanitizer( $dom );
-		$sanitizer->sanitize();
 		$whitelist_sanitizer = new AMP_Tag_And_Attribute_Sanitizer( $dom );
 		$whitelist_sanitizer->sanitize();
 		$content = AMP_DOM_Utils::get_content_from_dom( $dom );

@westonruter
Copy link
Member Author

I'm pretty sure the problem is due to an old version of libxml and this bit of logic:

https://github.com/Automattic/amp-wp/blob/70711f0cd647f233b02ffa1bbbd58496eeeea510/includes/utils/class-amp-dom-utils.php#L77-L110

@westonruter westonruter merged commit a0eaa53 into develop Jun 26, 2018
@westonruter westonruter deleted the add/noscript-sanitizer branch June 26, 2018 01:58
@westonruter westonruter moved this from Ready for review to Ready for QA in v1.0 Jul 2, 2018
@kienstra kienstra moved this from Ready for QA to Ready for Merging in v1.0 Aug 3, 2018
@kienstra kienstra moved this from Ready for Merging to In Production in v1.0 Dec 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
No open projects
v1.0
In Production
Development

Successfully merging this pull request may close these issues.

None yet

2 participants