Skip to content

Commit bebe5dc

Browse files
committed
HtmlFilter: <script> content should be filtered out
1 parent f9ac838 commit bebe5dc

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22

33
## Unreleased
44

5+
- HtmlFilter: <script> content should be filtered out.
6+
7+
58
## 1.7.1 - 2017-05-01
69

710
### Fixed

src/Source/Filter/HtmlFilter.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@
1616
*/
1717
class HtmlFilter implements Filter
1818
{
19+
/**
20+
* Ignore content of these tags.
21+
*
22+
* @var string[]
23+
*/
24+
static private $ignoreTags = [
25+
'script'
26+
];
27+
1928
/**
2029
* Attrs with text contents.
2130
*
@@ -66,7 +75,9 @@ public function filter($string)
6675
break;
6776

6877
case '>' === $char:
69-
$context = null;
78+
$context = 'tag_name' === $context && $this->isIgnoredTag($tagName)
79+
? 'ignored_tag_content'
80+
: null;
7081
$expecting = null;
7182
$char = ' ';
7283
break;
@@ -130,6 +141,10 @@ public function filter($string)
130141
case 'attr_value':
131142
$char = ' ';
132143
break;
144+
145+
case 'ignored_tag_content':
146+
$char = ' ';
147+
break;
133148
}
134149
}
135150
$result .= $char;
@@ -173,4 +188,22 @@ function ($match) {
173188
$string
174189
);
175190
}
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+
}
176209
}

tests/Source/Filter/HtmlFilterTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,15 @@ public function testMetaContent()
4646
' Bar ';
4747
static::assertEquals($text, $filter->filter($html));
4848
}
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+
}
4960
}

0 commit comments

Comments
 (0)