File tree Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Expand file tree Collapse file tree 2 files changed +19
-3
lines changed Original file line number Diff line number Diff line change @@ -32,9 +32,10 @@ var eventRE = func() *regexp.Regexp {
32
32
33
33
// StripJS removes non-AMP javascript from the DOM.
34
34
// - 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).
36
36
// - It has no src attribute and no type attribute (case-insensitive match).
37
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.
38
39
//
39
40
// - For all other elements, remove any event attribute that matches "on[A-Za-z].*".
40
41
func StripJS (e * Context ) error {
@@ -63,10 +64,15 @@ func StripJS(e *Context) error {
63
64
case "application/json" , "application/ld+json" :
64
65
// ok to keep
65
66
case "text/javascript" :
66
- // ok to keep only for AMP Cache scripts.
67
+ // ok to keep only for AMP Cache scripts
67
68
if ! isCacheSrc {
68
69
htmlnode .RemoveNode (& n )
69
70
}
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
+ }
70
76
default :
71
77
htmlnode .RemoveNode (& n )
72
78
}
Original file line number Diff line number Diff line change @@ -57,10 +57,20 @@ func TestStripJS(t *testing.T) {
57
57
Expected : "<head></head><body></body>" ,
58
58
},
59
59
{
60
- Desc : "keeps script corect type" ,
60
+ Desc : "keeps script correct type" ,
61
61
Input : "<script type=application/json>foo</script>" ,
62
62
Expected : "<head><script type=application/json>foo</script></head><body></body>" ,
63
63
},
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
+ },
64
74
{
65
75
Desc : "strip tag attr ona" ,
66
76
Input : "<body><select ona=\" myFunction()\" ></body>" ,
You can’t perform that action at this time.
0 commit comments