Skip to content

Commit

Permalink
Don't strip <script type=text/plain>. The original purpose of this tr…
Browse files Browse the repository at this point in the history
…ansformer

was #148, and text/plain scripts should not be relevant to that aim. Note that
the text/plain scripts used by the amp-script component are further guarded by
computeMaxAgeSeconds in transformer.go.

PiperOrigin-RevId: 286250297
  • Loading branch information
twifkak committed Dec 18, 2019
1 parent 51f5167 commit dc71bfb
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 19 deletions.
13 changes: 2 additions & 11 deletions transformer/transformers/stripjs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,7 @@ var eventRE = func() *regexp.Regexp {
// - For <script> elements, remove where any of the following is true:
// - 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.
// - It has a type attribute whose value is not application/json, application/ld+json, or text/plain (case-insensitive match on both name and value).
//
// - For all other elements, remove any event attribute that matches "on[A-Za-z].*".
func StripJS(e *Context) error {
Expand All @@ -61,21 +60,13 @@ func StripJS(e *Context) error {
}
if typeOk {
switch strings.ToLower(typeVal) {
case "application/json", "application/ld+json":
case "application/json", "application/ld+json", "text/plain":
// ok to keep
case "text/javascript":
// ok to keep only for AMP Cache scripts
if !isCacheSrc {
htmlnode.RemoveNode(&n)
}
case "text/plain":
// ok to keep only when attribute template=amp-mustache is present
if t, ok := htmlnode.GetAttributeVal(n, "", "template"); ok {
if t == "amp-mustache" {
continue
}
}
htmlnode.RemoveNode(&n)
default:
htmlnode.RemoveNode(&n)
}
Expand Down
11 changes: 3 additions & 8 deletions transformer/transformers/stripjs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,9 @@ func TestStripJS(t *testing.T) {
Expected: "<head><script type=application/json>foo</script></head><body></body>",
},
{
Desc: "keep type=text/plain when attribute template=amp-mustache",
Input: "<body><script template=amp-mustache type=text/plain></script></template></body>",
Expected: "<body><script template=amp-mustache type=text/plain></script></template></body>",
},
{
Desc: "strips type=text/plain when attribute template=amp-mustache is not present",
Input: "<body><script type=text/plain></script></body>",
Expected: "<body></body>",
Desc: "keep type=text/plain",
Input: "<body><script type=text/plain></script></template></body>",
Expected: "<body><script type=text/plain></script></template></body>",
},
{
Desc: "strip tag attr ona",
Expand Down

0 comments on commit dc71bfb

Please sign in to comment.