-
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 removal of closing table
and td
tags in script[template="amp-mustache"]
#4333
Conversation
…tput Now, it doesn't have </table> stripped, or the closing </td>.
src/Dom/Document.php
Outdated
*/ | ||
private function replace_mustache_templates( $html ) { | ||
return preg_replace( | ||
'#<script(\s[^>]*template="amp-mustache"[^>]*)>(.*?)</script>#s', |
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.
This can be made a bit more robust. Consider attribute values using single quotes or not quotes at 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.
Sure, I'll add handling for cases like template='amp-mustache'
or template=amp-mustache
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.
What do you think about 83d3dd6 ?
], | ||
'amp_mustache_template_script_with_table' => [ | ||
'utf-8', | ||
'<!DOCTYPE html><html>' . $head . '<body><script id="baz" type="text/plain" template="amp-mustache"><table><tr>{{#example}}<td></td>{{/example}}</tr></table></script></body></html>', |
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.
Could you add a test for a script
that had more than one child node? This will help ensure no bugs get introduced in regards to the order at restoration.
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.
Sure, good idea
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.
Applied in 5ef39b7
As Weston suggested, to prevent regressions.
Weston mentioned that this could have single or no quotes. I hadn't thought of that.
|
src/Dom/Document.php
Outdated
*/ | ||
private function replace_mustache_templates( $html ) { | ||
return preg_replace( | ||
'#<script(\s[^>]*template=["\']?amp-mustache["\']?[^>]*)>(.*?)</script>#s', |
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'd recommend the following changes to the regex to make it less brittle:
- Make it case-insensitive by adding the
i
flag - Support whitespace at the end of the closing tag (
</script >
), which is valid HTML - Only match a pair of matching quotes, instead of a mismatch, which would be parsed differently
- Make the initial non-closing-bracket group non-greedy to avoid some backtracking (
<script(\s[^>]*?
)
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.
Sure, applied in 52d49e9
Updated description to note that this PR also fixes #4276. |
Allow spaces in the closing </script>, like </script > Also, make this case-insensitive and ensure the quotes types are the same.
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.
We'll merge this after the mammoth PR #4019 is merged (which will likely require merge conflict resolutions on this PR).
Merge conflicts need to be resolved here. Take special care that this code needs to be included/adapted into the new Lines 357 to 366 in 65a16ef
|
This PR is extra ready for merge conflict resolution now that #4297 is merged. |
The conflicts were deleted files: src/Dom/Document.php tests/php/test-class-amp-dom-document.php This simply moves the changes in those files to the relevant new files.
Hm, merging in |
This reverts commit a1d8469.
The conflicts were deleted files: src/Dom/Document.php tests/php/test-class-amp-dom-document.php This simply moves the changes in those files to the relevant new files.
Also, correct a function name in a DocBlock tag.
Maybe <script async shouldn't be changed to <script async="" But with how the workaround works, it sets the script attributes to their previous attributes. So <script async will eventually end up as <script async="".
Hi @westonruter,
Good point to look at But do you think amp-wp/lib/common/src/Dom/Document.php Lines 337 to 345 in 4ba19bf
So far, it looks like it does, but I definitely could be missing something. Thanks, Weston! |
@schlessera Please approve if the changes are 👍 from your end as well. |
Co-Authored-By: Alain Schlesser <alain.schlesser@gmail.com>
Summary
Before,
<script template="amp-mustache" ...>
had the closing tags of child<table>
and<td>
stripped.Now, given a Custom HTML block with the snippet from #4254 (comment):
...that snippet now appears the same on the front-end:
Fixes #4254
Fixes #4276
Checklist