Ticket #2594: template_whitespace_ticket_2594_with_tests.diff
File template_whitespace_ticket_2594_with_tests.diff, 5.5 KB (added by , 15 years ago) |
---|
-
django/template/__init__.py
90 90 UNKNOWN_SOURCE="<unknown source>" 91 91 92 92 # 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))) 93 tag_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 99 block_command_re = re.compile('%s(?P<token_string>.*?)%s' % (re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END))) 96 100 97 101 # global dictionary of libraries that have been loaded using get_library 98 102 libraries = {} … … 207 211 self.contents[:20].replace('\n', '')) 208 212 209 213 def split_contents(self): 210 split = [] 214 split = [] 211 215 bits = iter(smart_split(self.contents)) 212 216 for bit in bits: 213 217 # Handle translation-marked template pieces … … 230 234 "Return a list of tokens from a given template_string." 231 235 in_tag = False 232 236 result = [] 233 for bit in tag_re.split(self.template_string): 237 for bit in tag_re.split(self.template_string): 234 238 if bit: 235 239 result.append(self.create_token(bit, in_tag)) 236 240 in_tag = not in_tag … … 241 245 Convert the given token string into a new Token object and return it. 242 246 If in_tag is True, we are processing something that matched a tag, 243 247 otherwise it should be treated as a literal string. 244 """ 248 """ 245 249 if in_tag: 246 250 if token_string.startswith(VARIABLE_TAG_START): 247 251 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()) 250 256 elif token_string.startswith(COMMENT_TAG_START): 251 257 token = Token(TOKEN_COMMENT, '') 252 258 else: … … 984 990 builtins.append(get_library(module_name)) 985 991 986 992 add_to_builtins('django.template.defaulttags') 987 add_to_builtins('django.template.defaultfilters') 993 add_to_builtins('django.template.defaultfilters') 994 No newline at end of file -
tests/regressiontests/templates/tests.py
965 965 'templatetag10': ('{% templatetag closebrace %}{% templatetag closebrace %}', {}, '}}'), 966 966 'templatetag11': ('{% templatetag opencomment %}', {}, '{#'), 967 967 '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 969 980 ### WIDTHRATIO TAG ######################################################## 970 981 'widthratio01': ('{% widthratio a b 0 %}', {'a':50,'b':100}, '0'), 971 982 'widthratio02': ('{% widthratio a b 100 %}', {'a':0,'b':0}, ''), … … 1082 1093 # implementation details (fortunately, the (no)autoescape block 1083 1094 # tags can be used in those cases) 1084 1095 'autoescape-filtertag01': ("{{ first }}{% filter safe %}{{ first }} x<y{% endfilter %}", {"first": "<a>"}, template.TemplateSyntaxError), 1096 1085 1097 } 1086 1098 1087 1099 if __name__ == "__main__":