Ticket #3532: spaceless.diff

File spaceless.diff, 5.0 KB (added by Rob Hudson <treborhudson@…>, 17 years ago)

Spaceless with optional numeric argument patch

  • django/utils/html.py

     
    3838    "Returns the given HTML with all tags stripped"
    3939    return re.sub(r'<[^>]*?>', '', value)
    4040
    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)
     41def 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)
    4444
    4545def strip_entities(value):
    4646    "Returns the given HTML with all entities (&something;) stripped"
  • django/template/defaulttags.py

     
    291291        return df.format(self.format_string)
    292292
    293293class SpacelessNode(Node):
    294     def __init__(self, nodelist):
     294    def __init__(self, nodelist, num_spaces):
    295295        self.nodelist = nodelist
     296        self.num_spaces = num_spaces
    296297
    297298    def render(self, context):
    298299        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)
    300301
    301302class TemplateTagNode(Node):
    302303    mapping = {'openblock': BLOCK_TAG_START,
     
    825826
    826827def spaceless(parser, token):
    827828    """
    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.
    830832
    831833    Example usage::
    832834
     
    840842
    841843        <p> <a href="foo/">Foo</a> </p>
    842844
     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
    843857    Only space between *tags* is normalized -- not space between tags and text. In
    844858    this example, the space around ``Hello`` won't be stripped::
    845859
     
    849863            </strong>
    850864        {% endspaceless %}
    851865    """
     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])
    852871    nodelist = parser.parse(('endspaceless',))
    853872    parser.delete_first_token()
    854     return SpacelessNode(nodelist)
     873    return SpacelessNode(nodelist, num_spaces)
    855874spaceless = register.tag(spaceless)
    856875
    857876#@register.tag
  • tests/regressiontests/templates/tests.py

     
    488488            'spaceless01': ("{% spaceless %} <b>    <i> text </i>    </b> {% endspaceless %}", {}, "<b> <i> text </i> </b>"),
    489489            'spaceless02': ("{% spaceless %} <b> \n <i> text </i> \n </b> {% endspaceless %}", {}, "<b> <i> text </i> </b>"),
    490490            '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>"),
    491495
    492496            # simple translation of a string delimited by '
    493497            'i18n01': ("{% load i18n %}{% trans 'xxxyyyxxx' %}", {}, "xxxyyyxxx"),
  • docs/templates.txt

     
    757757spaceless
    758758~~~~~~~~~
    759759
    760 Normalizes whitespace between HTML tags to a single space. This includes tab
    761 characters and newlines.
     760Adjusts whitespace between HTML tags to a given number of spaces, defaulting
     761to a single space if no numeric value given. This includes tab characters
     762and newlines.
    762763
    763764Example usage::
    764765
     
    772773
    773774    <p> <a href="foo/">Foo</a> </p>
    774775
     776Alternatively, 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
     784This example would return this HTML::
     785
     786    <p><a href="foo/">Foo</a></p>
     787
    775788Only space between *tags* is normalized -- not space between tags and text. In
    776789this example, the space around ``Hello`` won't be stripped::
    777790
Back to Top