From 301b1cd59da667be255214f86d12701a71192e59 Mon Sep 17 00:00:00 2001 From: Addison Brickey Date: Mon, 27 Apr 2015 13:26:36 -0700 Subject: [PATCH 1/2] updates stripTags option to take an array so that you can remove specific tags --- jquery.truncate.js | 11 ++++++++++- test/index.js | 2 ++ 2 files changed, 12 insertions(+), 1 deletion(-) 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() { From 1aa3a0af5229b25117c2c838d1b09deceb0370dd Mon Sep 17 00:00:00 2001 From: Addison Brickey Date: Mon, 27 Apr 2015 13:50:40 -0700 Subject: [PATCH 2/2] updates readme with updated stripTags option --- README.md | 9 +++++++++ 1 file changed, 9 insertions(+) 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*