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:
committed by
Andrew Godwin
parent
58683e9c82
commit
212b9826bd
@@ -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))
|
||||
|
||||
Reference in New Issue
Block a user