Skip to content

Commit

Permalink
Do not strip <script type=text/plain> when children of template (amp-…
Browse files Browse the repository at this point in the history
…mustache).

PiperOrigin-RevId: 275108293
  • Loading branch information
honeybadgerdontcare authored and banaag committed Oct 23, 2019
1 parent 9bb263f commit 8c67122
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 8 additions & 2 deletions transformer/transformers/stripjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,10 @@ var eventRE = func() *regexp.Regexp {

// StripJS removes non-AMP javascript from the DOM.
// - For <script> elements, remove where any of the following is true:
// - has a src attribute whose value is not prefixed by https://cdn.ampproject.org/ (case-insensitive match).
// - It has a src attribute whose value is not prefixed by https://cdn.ampproject.org/ (case-insensitive match).
// - It has no src attribute and no type attribute (case-insensitive match).
// - It has a type attribute whose value is neither application/json nor application/ld+json (case-insensitive match on both name and value).
// - Unless it is a child of template (amp-mustache) and has type=text/plain.
//
// - For all other elements, remove any event attribute that matches "on[A-Za-z].*".
func StripJS(e *Context) error {
Expand Down Expand Up @@ -63,10 +64,15 @@ func StripJS(e *Context) error {
case "application/json", "application/ld+json":
// ok to keep
case "text/javascript":
// ok to keep only for AMP Cache scripts.
// ok to keep only for AMP Cache scripts
if !isCacheSrc {
htmlnode.RemoveNode(&n)
}
case "text/plain":
// ok to keep only for children of template (amp-mustache)
if !htmlnode.IsDescendantOf(n, atom.Template) {
htmlnode.RemoveNode(&n)
}
default:
htmlnode.RemoveNode(&n)
}
Expand Down
12 changes: 11 additions & 1 deletion transformer/transformers/stripjs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,20 @@ func TestStripJS(t *testing.T) {
Expected: "<head></head><body></body>",
},
{
Desc: "keeps script corect type",
Desc: "keeps script correct type",
Input: "<script type=application/json>foo</script>",
Expected: "<head><script type=application/json>foo</script></head><body></body>",
},
{
Desc: "keep type=text/plain when child of template (amp-mustache)",
Input: "<body><template type=amp-mustache><script type=text/plain></script></template></body>",
Expected: "<body><template type=amp-mustache><script type=text/plain></script></template></body>",
},
{
Desc: "strips type=text/plain when not child of template (amp-mustache)",
Input: "<body><script type=text/plain></script></body>",
Expected: "<body></body>",
},
{
Desc: "strip tag attr ona",
Input: "<body><select ona=\"myFunction()\"></body>",
Expand Down

0 comments on commit 8c67122

Please sign in to comment.