-
Notifications
You must be signed in to change notification settings - Fork 24
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 MinifyHtml transformer #251
Conversation
@ediamin Can you rebase this PR onto the latest |
@ediamin Regarding |
Do I understand correctly that the |
b7faec2
to
4e09b53
Compare
@westonruter Yes, not only is it overkill, but it is also hardly feasible in PHP. While there are libraries around, I didn't find any that supports full ES6 and minifies to the degree that |
@schlessera I've added the amp-script failing test in |
@ediamin For the secure doctype comment, I'd suggest adding a hard-coded check in the minifier to skip this particular comment. Also, please check whether there might be other comments we are using as placeholders (both in the library as well as in the plugin) that we might want to preserve. It probably makes sense to have a configuration argument that allows a consumer of the library to provide a list of patterns to preserve. |
We already have the |
@schlessera I tried to skip the secured doctype comment from removing with this in private function minifyCommentNode(DOMComment $node, $opts)
{
// ....
if (preg_match('/^amp-doctype html/i', $node->data)) {
return;
}
$this->nodesToRemove[] = $node;
} It prevents removing the comment node, but since there is no comment node before this doctype comment node, the
In order to fix this I need to change the the const HTML_RESTORE_DOCTYPE_PATTERN = '/(<!--amp-)(doctype)(\s+[^>]+?)(-->)/i'; and also need to change the replacement pattern in private function restoreDoctypeNode($html)
{
if (! $this->securedDoctype) {
return $html;
}
return preg_replace(self::HTML_RESTORE_DOCTYPE_PATTERN, '<!\2\3>', $html, 1);
} Having these changes in Document class, we can get the correct Let me know your thoughts on this. Should I implement this changes in Document class? |
Ah, I see, the doctype is only secured because there's an element in front of it, and that element is removed by the minify transformer. I think it is enough to make the element in front of the secured doctype optional by turning the - const HTML_RESTORE_DOCTYPE_PATTERN = '/(^[^<]*(?>\s*<!--[^>]*>\s*)+<)'
+ const HTML_RESTORE_DOCTYPE_PATTERN = '/(^[^<]*(?>\s*<!--[^>]*>\s*)*<)'
. '(!--amp-)(doctype)(\s+[^>]+?)(-->)/i'; No other changes should be needed then. |
@schlessera Thank you very much. I've updated the regex pattern and it's working. The PR is ready for your final review. |
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.
Great work so far, @ediamin!
There are a lot of minor nitpicks in the review, mostly about comments. I had hoped that the WPCS docs enforcing would highlight these, but it seems that's not (yet) the case.
The most important change I'd like you to work through is to separate configuration from internal state. The $canCollapseWhitespace
and $inBody
values are not configuration values, they manage the state to be kept across node iteration, therefore they should be managed as method arguments.
Another bigger change is to use the return value of the methods to pass around $nodesToRemove
, as there is no reason to store these as a class property.
Co-authored-by: Alain Schlesser <[email protected]>
Hi @schlessera, I've updated the code with following notable changes
Please review again and let me know your feedback. Thanks. |
I just finished another round of review.
|
@schlessera Updated the code according to your suggestions. |
Great job, @ediamin ! |
// In case the main $document has `securedDoctype`. | ||
if (preg_match('/^amp-doctype html/i', $node->data)) { | ||
return []; | ||
} |
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.
There's another case that I think may have been missed here, and that is the Mustache template workaround for HTML tables which is documented at https://amp.dev/documentation/components/amp-mustache/#tables
So if a comment node appears inside of a template[type=amp-mustache]
element, and the comment text includes {{
, then it should be ignored.
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.
@ediamin Maybe you could open a new issue or PR to address this point?
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.
Er, I just filed an issue for it: #285.
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.
@westonruter Yeah sure.
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 see you've already created the issue. Thank you very much @westonruter.
minifies inline amp-script using terser(see Missing JavaScript minifier #260)closes #14