diff --git a/README.md b/README.md index 2b7da91..e337282 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ The maximum length (including the ellipsis) of the truncated html. *Default: false* If `stripTags` is truthy all html tags will be stipped, leaving only the text. +If `stripTags` is an Array then only the tags specified will be stripped. ```javascript > jQuery.truncate('

Stuff and Nonsense

', { @@ -44,6 +45,14 @@ If `stripTags` is truthy all html tags will be stipped, leaving only the text. 'Stuff and No…' ``` +```javascript +> jQuery.truncate('

Stuff and Nonsense

', { + length: 13, + stripTags: ['strong'] +}); +'

Stuff and No

…' +``` + ### words *Default: false* diff --git a/jquery.truncate.js b/jquery.truncate.js index bf0b155..73704b0 100644 --- a/jquery.truncate.js +++ b/jquery.truncate.js @@ -21,7 +21,16 @@ var text = self.text(); var excess = text.length - o.length; - if (o.stripTags) self.text(text); + if (o.stripTags) { + // Strip Specific Tags + if ($.isArray(o.stripTags)) { + self.find(o.stripTags.join(', ')).replaceWith(function(){ return $(this).html() }); + + // Strip All Tags + } else { + self.text(text); + } + } // Chop off any partial words if appropriate. if (o.words && excess > 0) { diff --git a/test/index.js b/test/index.js index 5fb1096..e02f274 100644 --- a/test/index.js +++ b/test/index.js @@ -42,6 +42,8 @@ test('stripTags', function() { strictEqual($.truncate('foo bar baz', {stripTags: true}), 'foo bar baz'); strictEqual($.truncate('foo bar baz', {length: 8, stripTags: true}), 'foo bar…'); + strictEqual($.truncate('foo bar baz', {length: 8, stripTags: true}), 'foo bar…'); + strictEqual($.truncate('foo bar baz', {length: 8, stripTags: ['em']}), 'foo bar…'); }); test('entities', function() {