Skip to content

Commit

Permalink
Making Sanitize::stripScripts() to remove multi-line script and style…
Browse files Browse the repository at this point in the history
… blocks. Fixes #657
  • Loading branch information
markstory committed May 4, 2010
1 parent 95dbae8 commit ce10c85
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
2 changes: 1 addition & 1 deletion cake/libs/sanitize.php
Expand Up @@ -156,7 +156,7 @@ function stripImages($str) {
* @static * @static
*/ */
function stripScripts($str) { function stripScripts($str) {
return preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/i', '', $str); return preg_replace('/(<link[^>]+rel="[^"]*stylesheet"[^>]*>|<img[^>]*>|style="[^"]*")|<script[^>]*>.*?<\/script>|<style[^>]*>.*?<\/style>|<!--.*?-->/is', '', $str);
} }


/** /**
Expand Down
26 changes: 26 additions & 0 deletions cake/tests/cases/libs/sanitize.test.php
Expand Up @@ -346,6 +346,32 @@ function testStripScripts() {
$expected = ''; $expected = '';
$result = Sanitize::stripScripts($string); $result = Sanitize::stripScripts($string);
$this->assertEqual($result, $expected); $this->assertEqual($result, $expected);

$string = <<<HTML
text
<style type="text/css">
<!--
#content { display:none; }
-->
</style>
text
HTML;
$expected = "text\n\ntext";
$result = Sanitize::stripScripts($string);
$this->assertEqual($result, $expected);

$string = <<<HTML
text
<script type="text/javascript">
<!--
alert('wooo');
-->
</script>
text
HTML;
$expected = "text\n\ntext";
$result = Sanitize::stripScripts($string);
$this->assertEqual($result, $expected);
} }


/** /**
Expand Down

0 comments on commit ce10c85

Please sign in to comment.