Skip to content

Commit f033d27

Browse files
committed
Fix: stripTags strips things that aren't tags
Regex borrowed and modified from: https://github.com/kvz/phpjs/blob/master/functions/strings/strip_tags.js
1 parent c1f7b1f commit f033d27

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

stripTags.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
var makeString = require('./helper/makeString');
2+
var tagsAndComments = /<\/?([a-z][a-z0-9]*)\b[^>]*>|<!--[\s\S]*?-->/gi;
23

34
module.exports = function stripTags(str) {
4-
return makeString(str).replace(/<\/?[^>]+>/g, '');
5+
return makeString(str).replace(tagsAndComments, '');
56
};

tests/stripTags.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ test('#stripTags', function() {
66
equal(stripTags('a <a href="#">link</a>'), 'a link');
77
equal(stripTags('a <a href="#">link</a><script>alert("hello world!")</scr'+'ipt>'), 'a linkalert("hello world!")');
88
equal(stripTags('<html><body>hello world</body></html>'), 'hello world');
9+
equal(stripTags('<h1 id="foo" data-foo="bar">hello world</body></h1>'), 'hello world');
10+
equal(stripTags('<web-component>hello world</web-component>'), 'hello world');
11+
equal(stripTags('<ReactComponent.Title>hello world</ReactComponent.Title>'), 'hello world');
12+
equal(stripTags('I have < I want, but that is > nothing'), 'I have < I want, but that is > nothing');
13+
equal(stripTags('<!-- a html comment --->hello world<!-- a html comment --->'), 'hello world');
914
equal(stripTags(123), '123');
1015
equal(stripTags(''), '');
1116
equal(stripTags(null), '');

0 commit comments

Comments
 (0)