From 81b166729acf8b1db46f1f69e823deb7b61bda5e Mon Sep 17 00:00:00 2001 From: antogyn Date: Wed, 18 Nov 2015 19:08:32 +0100 Subject: [PATCH] Add striptags filter --- docs/cn/templating.md | 1 + docs/fr/templating.md | 1 + docs/templating.md | 1 + src/filters.js | 6 ++++++ tests/filters.js | 14 ++++++++++++++ 5 files changed, 23 insertions(+) diff --git a/docs/cn/templating.md b/docs/cn/templating.md index c2c54cf0..5e7cb5d8 100644 --- a/docs/cn/templating.md +++ b/docs/cn/templating.md @@ -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) diff --git a/docs/fr/templating.md b/docs/fr/templating.md index 0660cfdf..49de9180 100644 --- a/docs/fr/templating.md +++ b/docs/fr/templating.md @@ -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) diff --git a/docs/templating.md b/docs/templating.md index f8aa68d1..021e4ae7 100644 --- a/docs/templating.md +++ b/docs/templating.md @@ -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) diff --git a/src/filters.js b/src/filters.js index 528faae8..b95b2b15 100644 --- a/src/filters.js +++ b/src/filters.js @@ -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[^>]*>|/gi; + return r.copySafeness(input, filters.trim(input.replace(tags, '')).replace(/\s+/gi, ' ')); + }, + title: function(str) { str = normalize(str, ''); var words = str.split(' '); diff --git a/tests/filters.js b/tests/filters.js index 5248ab7e..80fd2e47 100644 --- a/tests/filters.js +++ b/tests/filters.js @@ -394,6 +394,20 @@ finish(done); }); + it('striptags', function(done) { + equal('{{ html | striptags }}', {html: 'bar'}, 'bar'); + equal('{{ html | striptags }}', + { + html: '

an \n example link

\n

to a webpage

' + + '' + }, + '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');