Skip to content
This repository was archived by the owner on Dec 8, 2022. It is now read-only.

Updates the stripTags option to take an Array #5

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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('<p>Stuff and <i>Nonsense</i></p>', {
Expand All @@ -44,6 +45,14 @@ If `stripTags` is truthy all html tags will be stipped, leaving only the text.
'Stuff and No…'
```

```javascript
> jQuery.truncate('<p><strong>Stuff and <i>Nonsense</i></strong></p>', {
length: 13,
stripTags: ['strong']
});
'<p>Stuff and <i>No</i></p>…'
```

### words

*Default: false*
Expand Down
11 changes: 10 additions & 1 deletion jquery.truncate.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
2 changes: 2 additions & 0 deletions test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
test('stripTags', function() {
strictEqual($.truncate('foo <em>bar baz</em>', {stripTags: true}), 'foo bar baz');
strictEqual($.truncate('foo <em>bar baz</em>', {length: 8, stripTags: true}), 'foo bar…');
strictEqual($.truncate('foo <em><strong>bar</strong> baz</em>', {length: 8, stripTags: true}), 'foo bar…');
strictEqual($.truncate('foo <em><strong>bar</strong> baz</em>', {length: 8, stripTags: ['em']}), 'foo <strong>bar</strong>…');
});

test('entities', function() {
Expand Down