1
0
mirror of https://github.com/django/django.git synced 2025-10-26 07:06:08 +00:00

Fixed #14516 -- Extract methods from removetags and slugify template filters

Patch by @jphalip updated to apply, documentation and release notes
added.

I've documented strip_tags as well as remove_tags as the difference
between the two wouldn't be immediately obvious.
This commit is contained in:
Marc Tamlyn
2012-08-18 13:53:22 +01:00
committed by Andrew Godwin
parent 58683e9c82
commit 212b9826bd
7 changed files with 94 additions and 5 deletions

View File

@@ -123,6 +123,17 @@ def strip_tags(value):
return re.sub(r'<[^>]*?>', '', force_text(value))
strip_tags = allow_lazy(strip_tags)
def remove_tags(html, tags):
"""Returns the given HTML with given tags removed."""
tags = [re.escape(tag) for tag in tags.split()]
tags_re = u'(%s)' % u'|'.join(tags)
starttag_re = re.compile(ur'<%s(/?>|(\s+[^>]*>))' % tags_re, re.U)
endtag_re = re.compile(u'</%s>' % tags_re)
html = starttag_re.sub(u'', html)
html = endtag_re.sub(u'', html)
return html
remove_tags = allow_lazy(remove_tags, unicode)
def strip_spaces_between_tags(value):
"""Returns the given HTML with spaces between tags removed."""
return re.sub(r'>\s+<', '><', force_text(value))