Skip to content

Commit

Permalink
Merge pull request #589 from antogyn/master
Browse files Browse the repository at this point in the history
Add striptags filter
  • Loading branch information
carljm committed Nov 19, 2015
2 parents 7342af4 + 81b1667 commit b7bdd7a
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 0 deletions.
1 change: 1 addition & 0 deletions docs/cn/templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,7 @@ Nunjucks 已经支持了大部分 jinja 的过滤器 (点击查看文档)。
* [slice](http://jinja.pocoo.org/docs/templates/#slice)
* [sort](http://jinja.pocoo.org/docs/templates/#sort)
* [string](http://jinja.pocoo.org/docs/templates/#string)
* [striptags](http://jinja.pocoo.org/docs/templates/#striptags)
* [title](http://jinja.pocoo.org/docs/templates/#title)
* [trim](http://jinja.pocoo.org/docs/templates/#trim)
* [truncate](http://jinja.pocoo.org/docs/templates/#truncate)
Expand Down
1 change: 1 addition & 0 deletions docs/fr/templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ passé, cela permettra de comparer `attr` à chaque élément.
* [selectattr](http://jinja.pocoo.org/docs/templates/#selectattr) (seulement pour le formulaire avec un unique argument)
* [slice](http://jinja.pocoo.org/docs/templates/#slice)
* [string](http://jinja.pocoo.org/docs/templates/#string)
* [striptags](http://jinja.pocoo.org/docs/templates/#striptags)
* [title](http://jinja.pocoo.org/docs/templates/#title)
* [trim](http://jinja.pocoo.org/docs/templates/#trim)
* [truncate](http://jinja.pocoo.org/docs/templates/#truncate)
Expand Down
1 change: 1 addition & 0 deletions docs/templating.md
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,7 @@ passed, will compare `attr` from each item.
* [selectattr](http://jinja.pocoo.org/docs/templates/#selectattr) (only the single-argument form)
* [slice](http://jinja.pocoo.org/docs/templates/#slice)
* [string](http://jinja.pocoo.org/docs/templates/#string)
* [striptags](http://jinja.pocoo.org/docs/templates/#striptags)
* [title](http://jinja.pocoo.org/docs/templates/#title)
* [trim](http://jinja.pocoo.org/docs/templates/#trim)
* [truncate](http://jinja.pocoo.org/docs/templates/#truncate)
Expand Down
6 changes: 6 additions & 0 deletions src/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,12 @@ var filters = {
return r.copySafeness(obj, obj);
},

striptags: function(input) {
input = normalize(input, '');
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>|<!--[\s\S]*?-->/gi;
return r.copySafeness(input, filters.trim(input.replace(tags, '')).replace(/\s+/gi, ' '));
},

title: function(str) {
str = normalize(str, '');
var words = str.split(' ');
Expand Down
14 changes: 14 additions & 0 deletions tests/filters.js
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,20 @@
finish(done);
});

it('striptags', function(done) {
equal('{{ html | striptags }}', {html: '<foo>bar'}, 'bar');
equal('{{ html | striptags }}',
{
html: ' <p>an \n <a href="#">example</a> link</p>\n<p>to a webpage</p> ' +
'<!-- <p>and some comments</p> -->'
},
'an example link to a webpage');
equal('{{ undefined | striptags }}', '');
equal('{{ null | striptags }}', '');
equal('{{ nothing | striptags }}', '');
finish(done);
});

it('title', function(done) {
equal('{{ "foo bar baz" | title }}', 'Foo Bar Baz');
equal('{{ str | title }}', {str: r.markSafe('foo bar baz')}, 'Foo Bar Baz');
Expand Down

0 comments on commit b7bdd7a

Please sign in to comment.