Skip to content

Commit f5c5c98

Browse files
committed
Don't strip <script type=text/plain>. The original purpose of this transformer
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
1 parent 51f5167 commit f5c5c98

File tree

2 files changed

+5
-19
lines changed

2 files changed

+5
-19
lines changed

transformer/transformers/stripjs.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ var eventRE = func() *regexp.Regexp {
3434
// - For <script> elements, remove where any of the following is true:
3535
// - It has a src attribute whose value is not prefixed by https://cdn.ampproject.org/ (case-insensitive match).
3636
// - It has no src attribute and no type attribute (case-insensitive match).
37-
// - It has a type attribute whose value is neither application/json nor application/ld+json (case-insensitive match on both name and value).
38-
// - Unless it is a child of template (amp-mustache) and has type=text/plain.
37+
// - 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).
3938
//
4039
// - For all other elements, remove any event attribute that matches "on[A-Za-z].*".
4140
func StripJS(e *Context) error {
@@ -61,21 +60,13 @@ func StripJS(e *Context) error {
6160
}
6261
if typeOk {
6362
switch strings.ToLower(typeVal) {
64-
case "application/json", "application/ld+json":
63+
case "application/json", "application/ld+json", "text/plain":
6564
// ok to keep
6665
case "text/javascript":
6766
// ok to keep only for AMP Cache scripts
6867
if !isCacheSrc {
6968
htmlnode.RemoveNode(&n)
7069
}
71-
case "text/plain":
72-
// ok to keep only when attribute template=amp-mustache is present
73-
if t, ok := htmlnode.GetAttributeVal(n, "", "template"); ok {
74-
if t == "amp-mustache" {
75-
continue
76-
}
77-
}
78-
htmlnode.RemoveNode(&n)
7970
default:
8071
htmlnode.RemoveNode(&n)
8172
}

transformer/transformers/stripjs_test.go

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,9 @@ func TestStripJS(t *testing.T) {
6262
Expected: "<head><script type=application/json>foo</script></head><body></body>",
6363
},
6464
{
65-
Desc: "keep type=text/plain when attribute template=amp-mustache",
66-
Input: "<body><script template=amp-mustache type=text/plain></script></template></body>",
67-
Expected: "<body><script template=amp-mustache type=text/plain></script></template></body>",
68-
},
69-
{
70-
Desc: "strips type=text/plain when attribute template=amp-mustache is not present",
71-
Input: "<body><script type=text/plain></script></body>",
72-
Expected: "<body></body>",
65+
Desc: "keep type=text/plain",
66+
Input: "<body><script type=text/plain></script></template></body>",
67+
Expected: "<body><script type=text/plain></script></template></body>",
7368
},
7469
{
7570
Desc: "strip tag attr ona",

0 commit comments

Comments
 (0)