-
Notifications
You must be signed in to change notification settings - Fork 384
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
Prevent styles from being removed when in Customizer preview with Standard mode #4553
Merged
Merged
Changes from 4 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
9d0e2ad
Prevent styles from being removed when in Customizer preview with Sta…
pierlon f4c635e
Merge branch 'develop' of github.com:ampproject/amp-wp into fix/stand…
westonruter b7d273a
Revert "Prevent styles from being removed when in Customizer preview …
westonruter 00dc3ce
Re-work AmpBoilerplate to only remove boilerplate-specific styles
westonruter b3fb078
Remove now-unused AmpProject\DevMode
westonruter d8fc048
Improve phpdoc explaining refined behavior
westonruter ba90f19
Remove trailing comma from function args
westonruter 4f81f56
Simplify logic in removeStyleAndNoscriptTags
westonruter 6762054
Remove all boilerplates not just the identified one
westonruter 89105c8
Rename method to isBoilerplateStyle and improve docs
westonruter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,27 +26,16 @@ final class AmpBoilerplateTest extends TestCase | |
*/ | ||
public function dataTransform() | ||
{ | ||
$inputWithBoilerplate = static function ($html, $boilerplate) { | ||
$htmlDocument = static function ($html, $headEnd = '') { | ||
return TestMarkup::DOCTYPE . $html . '<head>' . | ||
TestMarkup::META_CHARSET . TestMarkup::META_VIEWPORT . TestMarkup::SCRIPT_AMPRUNTIME . | ||
TestMarkup::LINK_FAVICON . TestMarkup::LINK_CANONICAL . | ||
$boilerplate . | ||
$headEnd . | ||
'</head><body></body></html>'; | ||
}; | ||
|
||
$inputWithoutBoilerplate = static function ($html) { | ||
return TestMarkup::DOCTYPE . $html . '<head>' . | ||
TestMarkup::META_CHARSET . TestMarkup::META_VIEWPORT . TestMarkup::SCRIPT_AMPRUNTIME . | ||
TestMarkup::LINK_FAVICON . TestMarkup::LINK_CANONICAL . | ||
'</head><body></body></html>'; | ||
}; | ||
|
||
$expected = static function ($html, $boilerplate) { | ||
return TestMarkup::DOCTYPE . $html . '<head>' . | ||
TestMarkup::META_CHARSET . TestMarkup::META_VIEWPORT . TestMarkup::SCRIPT_AMPRUNTIME . | ||
TestMarkup::LINK_FAVICON . TestMarkup::LINK_CANONICAL . | ||
$boilerplate . | ||
'</head><body></body></html>'; | ||
$repeatTwice = static function ($value) { | ||
return array_fill(0, 2, $value); | ||
}; | ||
|
||
$ampBoilerplate = '<style ' . Attribute::AMP_BOILERPLATE . '>' . Amp::BOILERPLATE_CSS . '</style>' . | ||
|
@@ -57,65 +46,76 @@ public function dataTransform() | |
$amp4EmailBoilerplate = '<style ' . Attribute::AMP4EMAIL_BOILERPLATE . '>' . Amp::AMP4ADS_AND_AMP4EMAIL_BOILERPLATE_CSS . '</style>'; | ||
|
||
return [ | ||
'keeps boilerplate' => [ | ||
$inputWithBoilerplate('<html ⚡>', $ampBoilerplate), | ||
$expected('<html ⚡>', $ampBoilerplate), | ||
], | ||
'keeps boilerplate' => $repeatTwice( | ||
$htmlDocument('<html ⚡>', $ampBoilerplate), | ||
), | ||
|
||
'keeps boilerplate for amp4ads' => [ | ||
$inputWithBoilerplate('<html amp4ads>', $amp4AdsBoilerplate), | ||
$expected('<html amp4ads>', $amp4AdsBoilerplate), | ||
], | ||
'keeps boilerplate again' => $repeatTwice( | ||
$htmlDocument('<html amp>', $ampBoilerplate), | ||
), | ||
|
||
'keeps boilerplate for ⚡4ads' => [ | ||
$inputWithBoilerplate('<html ⚡4ads>', $amp4AdsBoilerplate), | ||
$expected('<html ⚡4ads>', $amp4AdsBoilerplate), | ||
], | ||
'leaves out boilerplate' => $repeatTwice( | ||
$htmlDocument('<html amp i-amphtml-no-boilerplate>'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I noticed there weren't tests for the |
||
), | ||
|
||
'keeps boilerplate for amp4email' => [ | ||
$inputWithBoilerplate('<html amp4email>', $amp4EmailBoilerplate), | ||
$expected('<html amp4email>', $amp4EmailBoilerplate), | ||
'removes boilerplate' => [ | ||
$htmlDocument('<html amp i-amphtml-no-boilerplate>', $ampBoilerplate), | ||
$htmlDocument('<html amp i-amphtml-no-boilerplate>'), | ||
], | ||
|
||
'keeps boilerplate for ⚡4email' => [ | ||
$inputWithBoilerplate('<html ⚡4email>', $amp4EmailBoilerplate), | ||
$expected('<html ⚡4email>', $amp4EmailBoilerplate), | ||
], | ||
'keeps boilerplate for amp4ads' => $repeatTwice( | ||
$htmlDocument('<html amp4ads>', $amp4AdsBoilerplate) | ||
), | ||
|
||
'keeps boilerplate for ⚡4ads' => $repeatTwice( | ||
$htmlDocument('<html ⚡4ads>', $amp4AdsBoilerplate) | ||
), | ||
|
||
'keeps boilerplate for amp4email' => $repeatTwice( | ||
$htmlDocument('<html amp4email>', $amp4EmailBoilerplate) | ||
), | ||
|
||
'keeps boilerplate for ⚡4email' => $repeatTwice( | ||
$htmlDocument('<html ⚡4email>', $amp4EmailBoilerplate) | ||
), | ||
|
||
'adds boilerplate if missing' => [ | ||
$inputWithoutBoilerplate('<html ⚡>'), | ||
$expected('<html ⚡>', $ampBoilerplate), | ||
$htmlDocument('<html ⚡>'), | ||
$htmlDocument('<html ⚡>', $ampBoilerplate), | ||
], | ||
|
||
'adds boilerplate if missing for amp4ads' => [ | ||
$inputWithoutBoilerplate('<html amp4ads>'), | ||
$expected('<html amp4ads>', $amp4AdsBoilerplate), | ||
$htmlDocument('<html amp4ads>'), | ||
$htmlDocument('<html amp4ads>', $amp4AdsBoilerplate), | ||
], | ||
|
||
'adds boilerplate if missing for ⚡4ads' => [ | ||
$inputWithoutBoilerplate('<html ⚡4ads>'), | ||
$expected('<html ⚡4ads>', $amp4AdsBoilerplate), | ||
$htmlDocument('<html ⚡4ads>'), | ||
$htmlDocument('<html ⚡4ads>', $amp4AdsBoilerplate), | ||
], | ||
|
||
'adds boilerplate if missing for amp4email' => [ | ||
$inputWithoutBoilerplate('<html amp4email>'), | ||
$expected('<html amp4email>', $amp4EmailBoilerplate), | ||
$htmlDocument('<html amp4email>'), | ||
$htmlDocument('<html amp4email>', $amp4EmailBoilerplate), | ||
], | ||
|
||
'adds boilerplate if missing for ⚡4email' => [ | ||
$inputWithoutBoilerplate('<html ⚡4email>'), | ||
$expected('<html ⚡4email>', $amp4EmailBoilerplate), | ||
$htmlDocument('<html ⚡4email>'), | ||
$htmlDocument('<html ⚡4email>', $amp4EmailBoilerplate), | ||
], | ||
|
||
'keeps devmode nodes when in devmode' => [ | ||
$inputWithBoilerplate('<html ⚡ data-ampdevmode>', '<style data-ampdevmode>h1: red;</style>' . $ampBoilerplate), | ||
$expected('<html ⚡ data-ampdevmode>', '<style data-ampdevmode>h1: red;</style>' . $ampBoilerplate), | ||
], | ||
'leaves styles that lack boilerplate attribute' => $repeatTwice( | ||
$htmlDocument('<html ⚡>', '<style>h1{color:red}</style><noscript><style>h2{color:blue}</style></noscript>' . $ampBoilerplate), | ||
), | ||
|
||
'removes devmode nodes when not in devmode' => [ | ||
$inputWithBoilerplate('<html ⚡>', '<style data-ampdevmode>h1: red;</style>' . $ampBoilerplate), | ||
$expected('<html ⚡>', $ampBoilerplate), | ||
'leaves styles that lack boilerplate attribute and adds boilerplate' => [ | ||
$htmlDocument('<html ⚡>', '<style>h1{color:red}</style><noscript><style>h2{color:blue}</style></noscript>'), | ||
$htmlDocument('<html ⚡>', '<style>h1{color:red}</style><noscript><style>h2{color:blue}</style></noscript>' . $ampBoilerplate), | ||
], | ||
|
||
'leaves styles that lack boilerplate attribute and leaves out boilerplate' => $repeatTwice( | ||
$htmlDocument('<html amp i-amphtml-no-boilerplate>', '<style>h1{color:red}</style><noscript><style>h2{color:blue}</style></noscript>'), | ||
), | ||
]; | ||
} | ||
|
||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@schlessera this fixes something I noticed before when first reviewing the Optimizer code. I thought at some point the logic of removing all styles other than the
amp-custom
could cause problems, and it seems my suspicion was correct. So now this logic removes exclusveily the AMP boilerplate styles only, leaving other styles intact.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't too happy with this logic either, but it is what the canonical Go optimizer package uses as well:
https://github.com/ampproject/amppackager/blob/releases/transformer/transformers/ampboilerplate.go#L24-L37
Maybe an issue and/or PR for the Go package would make sense as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes. But otherwise, you see the changes in this PR as correct and approved?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(Our needs are a bit different in that we often need to serve invalid AMP, whereas the Go version probably only ever needs to serve valid AMP. So that's perhaps why it is fine for Go use the logic it does now, but otherwise it seems more robust to do what this PR does now.)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One problem that I see is that if there's a mismatch between the type of AMP that the
<html>
tag states, and the boilerplate that is already included, the wrong one will stay in place.So, if the document states it is AMP4EMAIL, but the AMP4ADS boilerplate was added (for whatever reason), the optimizer will only try to remove the AMP4EMAIL boilerplate (and fail), and then re-insert it, with the end result being that two boilerplates are in the document.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was originally looping over all the possible boilerplates but then paired it down to just the identified one. You think it's best to check all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I think that'd be safer. We should always keep in mind that the Optimizer is not at all coupled to the plugin, so we cannot make any assumptions about the actual HTML that gets served into the optimizer. For all we know, it could be a buggy Drupal extension or a Symfony middleware that hands it over to the Optimizer. So, while it's important to keep the needs of the plugin in mind, we also need to keep the Optimizer in a state where it will just work regardless of input.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How do you think of 6762054?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approach looks good, just not happy with the naming (see code review comments).