Ticket #2594: template_whitespace_ticket_2594_with_tests.diff

File template_whitespace_ticket_2594_with_tests.diff, 5.5 KB (added by jshedd, 5 years ago)

Everything in one diff.

  • django/template/__init__.py

     
    9090UNKNOWN_SOURCE="<unknown source>"
    9191
    9292# match a variable or block tag and capture the entire tag, including start/end delimiters
    93 tag_re = re.compile('(%s.*?%s|%s.*?%s|%s.*?%s)' % (re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END),
    94                                           re.escape(VARIABLE_TAG_START), re.escape(VARIABLE_TAG_END),
    95                                           re.escape(COMMENT_TAG_START), re.escape(COMMENT_TAG_END)))
     93tag_re = re.compile('([\n\r\v\f]+[ \t]*?%s[^\n\r\v\f%s%s]*?%s(?=[ \t]*?[\n\r\v\f]+)|%s.*?%s|%s.*?%s|%s.*?%s)' % (
     94                        re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END), re.escape(BLOCK_TAG_END),
     95                        re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END),                       
     96                        re.escape(VARIABLE_TAG_START), re.escape(VARIABLE_TAG_END),
     97                        re.escape(COMMENT_TAG_START), re.escape(COMMENT_TAG_END)))
     98# find the command within a block tag
     99block_command_re = re.compile('%s(?P<token_string>.*?)%s' % (re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END)))
    96100
    97101# global dictionary of libraries that have been loaded using get_library
    98102libraries = {}
     
    207211            self.contents[:20].replace('\n', ''))
    208212
    209213    def split_contents(self):
    210         split = []
     214        split = []             
    211215        bits = iter(smart_split(self.contents))
    212216        for bit in bits:
    213217            # Handle translation-marked template pieces
     
    230234        "Return a list of tokens from a given template_string."
    231235        in_tag = False
    232236        result = []
    233         for bit in tag_re.split(self.template_string):
     237        for bit in tag_re.split(self.template_string): 
    234238            if bit:
    235239                result.append(self.create_token(bit, in_tag))
    236240            in_tag = not in_tag
     
    241245        Convert the given token string into a new Token object and return it.
    242246        If in_tag is True, we are processing something that matched a tag,
    243247        otherwise it should be treated as a literal string.
    244         """
     248        """               
    245249        if in_tag:
    246250            if token_string.startswith(VARIABLE_TAG_START):
    247251                token = Token(TOKEN_VAR, token_string[len(VARIABLE_TAG_START):-len(VARIABLE_TAG_END)].strip())
    248             elif token_string.startswith(BLOCK_TAG_START):
    249                 token = Token(TOKEN_BLOCK, token_string[len(BLOCK_TAG_START):-len(BLOCK_TAG_END)].strip())
     252            elif token_string.lstrip().startswith(BLOCK_TAG_START):
     253                token_struct = block_command_re.search(token_string)
     254                token_string = token_struct.group('token_string')
     255                token = Token(TOKEN_BLOCK, token_string.strip())
    250256            elif token_string.startswith(COMMENT_TAG_START):
    251257                token = Token(TOKEN_COMMENT, '')
    252258        else:
     
    984990    builtins.append(get_library(module_name))
    985991
    986992add_to_builtins('django.template.defaulttags')
    987 add_to_builtins('django.template.defaultfilters')
     993add_to_builtins('django.template.defaultfilters')
     994 No newline at end of file
  • tests/regressiontests/templates/tests.py

     
    965965            'templatetag10': ('{% templatetag closebrace %}{% templatetag closebrace %}', {}, '}}'),
    966966            'templatetag11': ('{% templatetag opencomment %}', {}, '{#'),
    967967            'templatetag12': ('{% templatetag closecomment %}', {}, '#}'),
    968 
     968           
     969            # WHITESPACE TESTING
     970            # Tags on their own line should collapse the newline before them
     971            'templatetag-whitespace01': ('\n {% templatetag openblock %}\n', {}, '{%\n'),
     972            # Tags that start on a newline, but have content thereafter, should not collapse the newline before or after them.
     973            'templatetag-whitespace02': ('\n {% templatetag openblock %} String', {}, '\n {% String'),
     974            'templatetag-whitespace03': ('\n {% templatetag openblock %} String \n', {}, '\n {% String \n'),
     975            'templatetag-whitespace04': ('\n {% templatetag openbrace %}\nString\n{% templatetag closebrace %}\n', {}, '{\nString}\n'),
     976            'templatetag-whitespace05': ('\n {% templatetag openbrace %}String{% templatetag closebrace %}\n', {}, '\n {String}\n'),
     977            'templatetag-whitespace06': ('\n {% templatetag openbrace %}String{% templatetag closebrace %}', {}, '\n {String}'),
     978            'templatetag-whitespace07': (' {% templatetag openbrace %}String{% templatetag closebrace %}', {}, ' {String}'),
     979           
    969980            ### WIDTHRATIO TAG ########################################################
    970981            'widthratio01': ('{% widthratio a b 0 %}', {'a':50,'b':100}, '0'),
    971982            'widthratio02': ('{% widthratio a b 100 %}', {'a':0,'b':0}, ''),
     
    10821093            # implementation details (fortunately, the (no)autoescape block
    10831094            # tags can be used in those cases)
    10841095            'autoescape-filtertag01': ("{{ first }}{% filter safe %}{{ first }} x<y{% endfilter %}", {"first": "<a>"}, template.TemplateSyntaxError),
     1096           
    10851097        }
    10861098
    10871099if __name__ == "__main__":
Back to Top