From cc0a75fe1976cd7f0ff97f77d962ba02935aa2be Mon Sep 17 00:00:00 2001 From: Andrew Dupont Date: Mon, 11 May 2009 23:29:26 -0500 Subject: [PATCH] Make regex used in `stripTags` stricter. (igor, kangax) [#674 state:resolved] --- src/lang/string.js | 4 +++- test/unit/string_test.js | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/lang/string.js b/src/lang/string.js index 2798bcc..b9c5f50 100644 --- a/src/lang/string.js +++ b/src/lang/string.js @@ -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, ''); } /** diff --git a/test/unit/string_test.js b/test/unit/string_test.js index 5d7ce33..93308a4 100644 --- a/test/unit/string_test.js +++ b/test/unit/string_test.js @@ -208,6 +208,7 @@ new Test.Unit.Runner({ this.assertEqual('hello world', 'hello world'.stripTags()); this.assertEqual('hello world', 'hello world'.stripTags()); this.assertEqual('1\n2', '1\n2'.stripTags()); + this.assertEqual('one < two blah baz', 'one < two blah baz'.stripTags()); }, testStripScripts: function() {