File tree Expand file tree Collapse file tree 3 files changed +48
-1
lines changed Expand file tree Collapse file tree 3 files changed +48
-1
lines changed Original file line number Diff line number Diff line change 2
2
3
3
## Unreleased
4
4
5
+ - HtmlFilter: <script > content should be filtered out.
6
+
7
+
5
8
## 1.7.1 - 2017-05-01
6
9
7
10
### Fixed
Original file line number Diff line number Diff line change 16
16
*/
17
17
class HtmlFilter implements Filter
18
18
{
19
+ /**
20
+ * Ignore content of these tags.
21
+ *
22
+ * @var string[]
23
+ */
24
+ static private $ ignoreTags = [
25
+ 'script '
26
+ ];
27
+
19
28
/**
20
29
* Attrs with text contents.
21
30
*
@@ -66,7 +75,9 @@ public function filter($string)
66
75
break ;
67
76
68
77
case '> ' === $ char :
69
- $ context = null ;
78
+ $ context = 'tag_name ' === $ context && $ this ->isIgnoredTag ($ tagName )
79
+ ? 'ignored_tag_content '
80
+ : null ;
70
81
$ expecting = null ;
71
82
$ char = ' ' ;
72
83
break ;
@@ -130,6 +141,10 @@ public function filter($string)
130
141
case 'attr_value ' :
131
142
$ char = ' ' ;
132
143
break ;
144
+
145
+ case 'ignored_tag_content ' :
146
+ $ char = ' ' ;
147
+ break ;
133
148
}
134
149
}
135
150
$ result .= $ char ;
@@ -173,4 +188,22 @@ function ($match) {
173
188
$ string
174
189
);
175
190
}
191
+
192
+ /**
193
+ * Return true if $name is in the list of ignored tags.
194
+ *
195
+ * @param string $name Tag name.
196
+ *
197
+ * @return bool
198
+ */
199
+ private function isIgnoredTag ($ name )
200
+ {
201
+ foreach (self ::$ ignoreTags as $ tag ) {
202
+ if (strcasecmp ($ tag , $ name ) === 0 ) {
203
+ return true ;
204
+ }
205
+ }
206
+
207
+ return false ;
208
+ }
176
209
}
Original file line number Diff line number Diff line change @@ -46,4 +46,15 @@ public function testMetaContent()
46
46
' Bar ' ;
47
47
static ::assertEquals ($ text , $ filter ->filter ($ html ));
48
48
}
49
+
50
+ /**
51
+ * <script> content should be filtered out.
52
+ */
53
+ public function testScript ()
54
+ {
55
+ $ filter = new HtmlFilter ();
56
+ $ html = "<p>Foo</p> \n<script>Bar Baz \nBuz</script> " ;
57
+ $ text = " Foo \n \n " ;
58
+ static ::assertEquals ($ text , $ filter ->filter ($ html ));
59
+ }
49
60
}
You can’t perform that action at this time.
0 commit comments