Skip to content

Commit 670d87c

Browse files
honeybadgerdontcaretwifkak
authored andcommitted
Do not strip <script type=text/plain> when children of template (amp-mustache).
PiperOrigin-RevId: 275108293
1 parent e4eea0d commit 670d87c

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

transformer/transformers/stripjs.go

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,10 @@ var eventRE = func() *regexp.Regexp {
3232

3333
// StripJS removes non-AMP javascript from the DOM.
3434
// - For <script> elements, remove where any of the following is true:
35-
// - has a src attribute whose value is not prefixed by https://cdn.ampproject.org/ (case-insensitive match).
35+
// - 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).
3737
// - 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.
3839
//
3940
// - For all other elements, remove any event attribute that matches "on[A-Za-z].*".
4041
func StripJS(e *Context) error {
@@ -63,10 +64,15 @@ func StripJS(e *Context) error {
6364
case "application/json", "application/ld+json":
6465
// ok to keep
6566
case "text/javascript":
66-
// ok to keep only for AMP Cache scripts.
67+
// ok to keep only for AMP Cache scripts
6768
if !isCacheSrc {
6869
htmlnode.RemoveNode(&n)
6970
}
71+
case "text/plain":
72+
// ok to keep only for children of template (amp-mustache)
73+
if !htmlnode.IsDescendantOf(n, atom.Template) {
74+
htmlnode.RemoveNode(&n)
75+
}
7076
default:
7177
htmlnode.RemoveNode(&n)
7278
}

transformer/transformers/stripjs_test.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,20 @@ func TestStripJS(t *testing.T) {
5757
Expected: "<head></head><body></body>",
5858
},
5959
{
60-
Desc: "keeps script corect type",
60+
Desc: "keeps script correct type",
6161
Input: "<script type=application/json>foo</script>",
6262
Expected: "<head><script type=application/json>foo</script></head><body></body>",
6363
},
64+
{
65+
Desc: "keep type=text/plain when child of template (amp-mustache)",
66+
Input: "<body><template type=amp-mustache><script type=text/plain></script></template></body>",
67+
Expected: "<body><template type=amp-mustache><script type=text/plain></script></template></body>",
68+
},
69+
{
70+
Desc: "strips type=text/plain when not child of template (amp-mustache)",
71+
Input: "<body><script type=text/plain></script></body>",
72+
Expected: "<body></body>",
73+
},
6474
{
6575
Desc: "strip tag attr ona",
6676
Input: "<body><select ona=\"myFunction()\"></body>",

0 commit comments

Comments
 (0)