Code

Ticket #3670: numeric_with_negative.patch

File numeric_with_negative.patch, 3.1 KB (added by SmileyChris, 7 years ago)
  • django/template/__init__.py

     
    9191tag_re = re.compile('(%s.*?%s|%s.*?%s|%s.*?%s)' % (re.escape(BLOCK_TAG_START), re.escape(BLOCK_TAG_END), 
    9292                                          re.escape(VARIABLE_TAG_START), re.escape(VARIABLE_TAG_END), 
    9393                                          re.escape(COMMENT_TAG_START), re.escape(COMMENT_TAG_END))) 
     94# matches if the string is valid number 
     95number_re = re.compile(r'[-+]?(\d+|\d*\.\d+)$') 
    9496 
    9597# global dictionary of libraries that have been loaded using get_library 
    9698libraries = {} 
     
    632634 
    633635    (The example assumes VARIABLE_ATTRIBUTE_SEPARATOR is '.') 
    634636    """ 
    635     if path[0].isdigit(): 
     637    if number_re.match(path): 
    636638        number_type = '.' in path and float or int 
    637         try: 
    638             current = number_type(path) 
    639         except ValueError: 
    640             current = settings.TEMPLATE_STRING_IF_INVALID 
     639        current = number_type(path) 
    641640    elif path[0] in ('"', "'") and path[0] == path[-1]: 
    642641        current = path[1:-1] 
    643642    else: 
  • tests/regressiontests/templates/tests.py

     
    401401            'ifequal-split09': (r"{% ifequal a 'slash\man' %}yes{% else %}no{% endifequal %}", {'a': r"slash\man"}, "yes"), 
    402402            'ifequal-split10': (r"{% ifequal a 'slash\man' %}yes{% else %}no{% endifequal %}", {'a': r"slashman"}, "no"), 
    403403 
     404            # NUMERIC RESOLUTION 
     405            'ifequal-numeric01': ('{% ifequal x 5 %}yes{% endifequal %}', {'x': '5'}, ''), 
     406            'ifequal-numeric02': ('{% ifequal x 5 %}yes{% endifequal %}', {'x': 5}, 'yes'), 
     407            'ifequal-numeric03': ('{% ifequal x 5.2 %}yes{% endifequal %}', {'x': 5}, ''), 
     408            'ifequal-numeric04': ('{% ifequal x 5.2 %}yes{% endifequal %}', {'x': 5.2}, 'yes'), 
     409            'ifequal-numeric05': ('{% ifequal x 0.2 %}yes{% endifequal %}', {'x': .2}, 'yes'), 
     410            'ifequal-numeric06': ('{% ifequal x .2 %}yes{% endifequal %}', {'x': .2}, 'yes'), 
     411            'ifequal-numeric07': ('{% ifequal x 2. %}yes{% endifequal %}', {'x': 2}, ''), 
     412            'ifequal-numeric08': ('{% ifequal x "5" %}yes{% endifequal %}', {'x': 5}, ''), 
     413            'ifequal-numeric09': ('{% ifequal x "5" %}yes{% endifequal %}', {'x': '5'}, 'yes'), 
     414            'ifequal-numeric10': ('{% ifequal x -5 %}yes{% endifequal %}', {'x': -5}, 'yes'), 
     415            'ifequal-numeric11': ('{% ifequal x -5.2 %}yes{% endifequal %}', {'x': -5.2}, 'yes'), 
     416            'ifequal-numeric12': ('{% ifequal x +5 %}yes{% endifequal %}', {'x': 5}, 'yes'), 
     417 
    404418            ### IFNOTEQUAL TAG ######################################################## 
    405419            'ifnotequal01': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 2}, "yes"), 
    406420            'ifnotequal02': ("{% ifnotequal a b %}yes{% endifnotequal %}", {"a": 1, "b": 1}, ""),