Skip to content

Commit

Permalink
Merge pull request #9156 from cakephp/strip-links
Browse files Browse the repository at this point in the history
Make stripLinks a bit more thorough
  • Loading branch information
markstory committed Jul 23, 2016
2 parents 5b0543e + 0bd9dc1 commit ac06672
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/Utility/Text.php
Original file line number Diff line number Diff line change
Expand Up @@ -506,12 +506,20 @@ public static function highlight($text, $phrase, array $options = [])
/**
* Strips given text of all links (<a href=....).
*
* *Warning* This method is not an robust solution in preventing XSS
* or malicious HTML.
*
* @param string $text Text
* @return string The text without links
* @deprecated 3.2.12 This method will be removed in 4.0.0
*/
public static function stripLinks($text)
{
return preg_replace('|<a\s+[^>]+>|im', '', preg_replace('|<\/a>|im', '', $text));
do {
$text = preg_replace('#</?a([/\s][^>]*)?(>|$)#i', '', $text, -1, $count);
} while ($count);

return $text;
}

/**
Expand Down
9 changes: 9 additions & 0 deletions tests/TestCase/Utility/TextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,15 @@ public function testStripLinks()
$expected = 'This <strong>is</strong> a test and <abbr>some</abbr> other text';
$result = $this->Text->stripLinks($text);
$this->assertEquals($expected, $result);

$text = '<a<a h> href=\'bla\'>test</a</a>>';
$this->assertEquals('test', $this->Text->stripLinks($text));

$text = '<a/href="#">test</a/>';
$this->assertEquals('test', $this->Text->stripLinks($text));

$text = '<a href="#"';
$this->assertEquals('', $this->Text->stripLinks($text));
}

/**
Expand Down

0 comments on commit ac06672

Please sign in to comment.