Skip to content

Commit

Permalink
HtmlFilter: <script> content should be filtered out
Browse files Browse the repository at this point in the history
  • Loading branch information
mekras committed Apr 29, 2017
1 parent f9ac838 commit bebe5dc
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 1 deletion.
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

## Unreleased

- HtmlFilter: <script> content should be filtered out.


## 1.7.1 - 2017-05-01

### Fixed
Expand Down
35 changes: 34 additions & 1 deletion src/Source/Filter/HtmlFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,15 @@
*/
class HtmlFilter implements Filter
{
/**
* Ignore content of these tags.
*
* @var string[]
*/
static private $ignoreTags = [
'script'
];

/**
* Attrs with text contents.
*
Expand Down Expand Up @@ -66,7 +75,9 @@ public function filter($string)
break;

case '>' === $char:
$context = null;
$context = 'tag_name' === $context && $this->isIgnoredTag($tagName)
? 'ignored_tag_content'
: null;
$expecting = null;
$char = ' ';
break;
Expand Down Expand Up @@ -130,6 +141,10 @@ public function filter($string)
case 'attr_value':
$char = ' ';
break;

case 'ignored_tag_content':
$char = ' ';
break;
}
}
$result .= $char;
Expand Down Expand Up @@ -173,4 +188,22 @@ function ($match) {
$string
);
}

/**
* Return true if $name is in the list of ignored tags.
*
* @param string $name Tag name.
*
* @return bool
*/
private function isIgnoredTag($name)
{
foreach (self::$ignoreTags as $tag) {
if (strcasecmp($tag, $name) === 0) {
return true;
}
}

return false;
}
}
11 changes: 11 additions & 0 deletions tests/Source/Filter/HtmlFilterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,15 @@ public function testMetaContent()
' Bar ';
static::assertEquals($text, $filter->filter($html));
}

/**
* <script> content should be filtered out.
*/
public function testScript()
{
$filter = new HtmlFilter();
$html = "<p>Foo</p>\n<script>Bar Baz\nBuz</script>";
$text = " Foo \n \n ";
static::assertEquals($text, $filter->filter($html));
}
}

0 comments on commit bebe5dc

Please sign in to comment.