Ticket #3532: spaceless.diff
File spaceless.diff, 5.0 KB (added by , 18 years ago) |
---|
-
django/utils/html.py
38 38 "Returns the given HTML with all tags stripped" 39 39 return re.sub(r'<[^>]*?>', '', value) 40 40 41 def strip_spaces_between_tags(value ):42 "Returns the given HTML with spaces between tags normalized to a single space"43 return re.sub(r'>\s+<', '> <', value)41 def strip_spaces_between_tags(value, num_spaces=1): 42 "Returns the given HTML with the given number spaces between tags" 43 return re.sub(r'>\s+<', '>%s<' % (' ' * num_spaces), value) 44 44 45 45 def strip_entities(value): 46 46 "Returns the given HTML with all entities (&something;) stripped" -
django/template/defaulttags.py
291 291 return df.format(self.format_string) 292 292 293 293 class SpacelessNode(Node): 294 def __init__(self, nodelist ):294 def __init__(self, nodelist, num_spaces): 295 295 self.nodelist = nodelist 296 self.num_spaces = num_spaces 296 297 297 298 def render(self, context): 298 299 from django.utils.html import strip_spaces_between_tags 299 return strip_spaces_between_tags(self.nodelist.render(context).strip() )300 return strip_spaces_between_tags(self.nodelist.render(context).strip(), self.num_spaces) 300 301 301 302 class TemplateTagNode(Node): 302 303 mapping = {'openblock': BLOCK_TAG_START, … … 825 826 826 827 def spaceless(parser, token): 827 828 """ 828 Normalize whitespace between HTML tags to a single space. This includes tab 829 characters and newlines. 829 Adjusts whitespace between HTML tags to a given number of spaces, defaulting 830 to a single space if no numeric value given. This includes tab characters 831 and newlines. 830 832 831 833 Example usage:: 832 834 … … 840 842 841 843 <p> <a href="foo/">Foo</a> </p> 842 844 845 Alternatively, providing a numeric value works as well as in this example:: 846 847 {% spaceless 0 %} 848 <p> 849 <a href="foo/">Foo</a> 850 </p> 851 {% endspaceless %} 852 853 This example would return this HTML:: 854 855 <p><a href="foo/">Foo</a></p> 856 843 857 Only space between *tags* is normalized -- not space between tags and text. In 844 858 this example, the space around ``Hello`` won't be stripped:: 845 859 … … 849 863 </strong> 850 864 {% endspaceless %} 851 865 """ 866 num_spaces = 1 # default to 1 space between tags 867 bits = token.contents.split() 868 if len(bits) > 1: 869 # User specified number of spaces between tags 870 num_spaces = int(bits[1]) 852 871 nodelist = parser.parse(('endspaceless',)) 853 872 parser.delete_first_token() 854 return SpacelessNode(nodelist )873 return SpacelessNode(nodelist, num_spaces) 855 874 spaceless = register.tag(spaceless) 856 875 857 876 #@register.tag -
tests/regressiontests/templates/tests.py
488 488 'spaceless01': ("{% spaceless %} <b> <i> text </i> </b> {% endspaceless %}", {}, "<b> <i> text </i> </b>"), 489 489 'spaceless02': ("{% spaceless %} <b> \n <i> text </i> \n </b> {% endspaceless %}", {}, "<b> <i> text </i> </b>"), 490 490 'spaceless03': ("{% spaceless %}<b><i>text</i></b>{% endspaceless %}", {}, "<b><i>text</i></b>"), 491 # {% spaceless %} with optional numeric argument 492 'spaceless04': ("{% spaceless 0 %} <b> <i> text </i> </b> {% endspaceless %}", {}, "<b><i> text </i></b>"), 493 'spaceless05': ("{% spaceless 0 %} <b> \n <i> text </i> \n </b> {% endspaceless %}", {}, "<b><i> text </i></b>"), 494 'spaceless06': ("{% spaceless 3 %}<b> <i>text</i> </b>{% endspaceless %}", {}, "<b> <i>text</i> </b>"), 491 495 492 496 # simple translation of a string delimited by ' 493 497 'i18n01': ("{% load i18n %}{% trans 'xxxyyyxxx' %}", {}, "xxxyyyxxx"), -
docs/templates.txt
757 757 spaceless 758 758 ~~~~~~~~~ 759 759 760 Normalizes whitespace between HTML tags to a single space. This includes tab 761 characters and newlines. 760 Adjusts whitespace between HTML tags to a given number of spaces, defaulting 761 to a single space if no numeric value given. This includes tab characters 762 and newlines. 762 763 763 764 Example usage:: 764 765 … … 772 773 773 774 <p> <a href="foo/">Foo</a> </p> 774 775 776 Alternatively, providing a numeric value works as well as in this example:: 777 778 {% spaceless 0 %} 779 <p> 780 <a href="foo/">Foo</a> 781 </p> 782 {% endspaceless %} 783 784 This example would return this HTML:: 785 786 <p><a href="foo/">Foo</a></p> 787 775 788 Only space between *tags* is normalized -- not space between tags and text. In 776 789 this example, the space around ``Hello`` won't be stripped:: 777 790