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

2 participants
@westonruter
Member

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 the beta label Jun 25, 2018

@westonruter westonruter added this to the v1.0 milestone Jun 25, 2018

@westonruter westonruter requested review from amedina and kienstra Jun 25, 2018

@westonruter westonruter added this to Ready for review in v1.0 Jun 25, 2018

@kienstra

kienstra approved these changes Jun 26, 2018 edited

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

This comment has been minimized.

@kienstra

kienstra Jun 26, 2018

Collaborator

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() {

This comment has been minimized.

@kienstra

kienstra Jun 26, 2018

Collaborator

Nice thorough testing of the boilerplate not being removed.

@westonruter

This comment has been minimized.

Member

westonruter commented Jun 26, 2018

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

@kienstra

This comment has been minimized.

Collaborator

kienstra commented Jun 26, 2018

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

@westonruter westonruter force-pushed the add/noscript-sanitizer branch from cf4751d to 22a1457 Jun 26, 2018

@westonruter

This comment has been minimized.

Member

westonruter commented Jun 26, 2018

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 westonruter force-pushed the add/noscript-sanitizer branch from 22a1457 to b0a08e1 Jun 26, 2018

@westonruter

This comment has been minimized.

Member

westonruter commented Jun 26, 2018

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 force-pushed the add/noscript-sanitizer branch from 66e04d1 to c44b954 Jun 26, 2018

@westonruter westonruter force-pushed the add/noscript-sanitizer branch from c44b954 to fb55f08 Jun 26, 2018

@westonruter westonruter merged commit a0eaa53 into develop Jun 26, 2018

2 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@westonruter westonruter deleted the add/noscript-sanitizer branch Jun 26, 2018

@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