Prevent removal of closing table and td tags in script[template="amp-mustache"]#4333
Prevent removal of closing table and td tags in script[template="amp-mustache"]#4333westonruter merged 13 commits intodevelopfrom
table and td tags in script[template="amp-mustache"]#4333Conversation
…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.
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.
Sure, I'll add handling for cases like template='amp-mustache' or template=amp-mustache
| ], | ||
| '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.
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.
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.
I'd recommend the following changes to the regex to make it less brittle:
- Make it case-insensitive by adding the
iflag - 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[^>]*?)
|
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.
westonruter
left a comment
There was a problem hiding this comment.
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