Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add striptags filter #589

Merged
merged 1 commit into from
Nov 19, 2015
Merged
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
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