Make regex used in `stripTags` stricter. (igor, kangax) [#674 state:resolved]

This commit is contained in:
Andrew Dupont 2009-05-11 23:29:26 -05:00
parent cb729625ae
commit cc0a75fe19
2 changed files with 4 additions and 1 deletions

View File

@ -122,9 +122,11 @@ Object.extend(String.prototype, (function() {
* String#stripTags() -> String
*
* Strips a string of any HTML tag.
* Note that `stripTags` will only strip HTML4.01 tags (such as - div, span and abbr)
* It will not strip namespace-prefixed tags such as "h:table" or "xsl:template"
**/
function stripTags() {
return this.replace(/<\/?[^>]+>/gi, '');
return this.replace(/<\w+(\s+("[^"]*"|'[^']*'|[^>])+)?>|<\/\w+>/gi, '');
}
/**

View File

@ -208,6 +208,7 @@ new Test.Unit.Runner({
this.assertEqual('hello world', '<a href="#" onclick="moo!">hello</a> world'.stripTags());
this.assertEqual('hello world', 'h<b><em>e</em></b>l<i>l</i>o w<span class="moo" id="x"><b>o</b></span>rld'.stripTags());
this.assertEqual('1\n2', '1\n2'.stripTags());
this.assertEqual('one < two blah baz', 'one < two <a href="#" title="foo > bar">blah</a> <input disabled>baz'.stripTags());
},
testStripScripts: function() {