Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fixed #16677 -- Fixed the future version of the ssi template tag to w…

…ork with template file names that contain spaces. Note that this fix cannot be applied to the currently deprecated version of the ssi tag since it requires an unquoted literal as parameter. Many thanks to Aymeric Augustin for the report and patch.

git-svn-id: http://code.djangoproject.com/svn/django/trunk@16687 bcc190cf-cafb-0310-a4f2-bffc1f526a37
  • Loading branch information...
commit bf9ba45f6b5da97154f317ba0b8349b2822a00ba 1 parent bb99fe4
Julien Phalip authored
2  django/templatetags/future.py
@@ -19,7 +19,7 @@ def ssi(parser, token):
19 19
 
20 20
         {% ssi "/home/html/ljworld.com/includes/right_generic.html" parsed %}
21 21
     """
22  
-    bits = token.contents.split()
  22
+    bits = token.split_contents()
23 23
     parsed = False
24 24
     if len(bits) not in (2, 3):
25 25
         raise TemplateSyntaxError("'ssi' tag takes one argument: the path to"
1  tests/regressiontests/templates/templates/ssi include with spaces.html
... ...
@@ -0,0 +1 @@
  1
+This is for testing an ssi include with spaces in its name. {{ test }}
40  tests/regressiontests/templates/tests.py
@@ -508,7 +508,8 @@ def render(self, test_template, vals):
508 508
     def get_template_tests(self):
509 509
         # SYNTAX --
510 510
         # 'template_name': ('template contents', 'context dict', 'expected string output' or Exception class)
511  
-        return {
  511
+        basedir = os.path.dirname(os.path.abspath(__file__))
  512
+        tests = {
512 513
             ### BASIC SYNTAX ################################################
513 514
 
514 515
             # Plain text should go through the template parser untouched
@@ -1350,27 +1351,34 @@ def get_template_tests(self):
1350 1351
             ### SSI TAG ########################################################
1351 1352
 
1352 1353
             # Test normal behavior
1353  
-            'old-ssi01': ('{%% ssi %s %%}' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates', 'ssi_include.html'), {}, 'This is for testing an ssi include. {{ test }}\n'),
1354  
-            'old-ssi02': ('{%% ssi %s %%}' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'not_here'), {}, ''),
  1354
+            'old-ssi01': ('{%% ssi %s %%}' % os.path.join(basedir, 'templates', 'ssi_include.html'), {}, 'This is for testing an ssi include. {{ test }}\n'),
  1355
+            'old-ssi02': ('{%% ssi %s %%}' % os.path.join(basedir, 'not_here'), {}, ''),
1355 1356
 
1356 1357
             # Test parsed output
1357  
-            'old-ssi06': ('{%% ssi %s parsed %%}' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates', 'ssi_include.html'), {'test': 'Look ma! It parsed!'}, 'This is for testing an ssi include. Look ma! It parsed!\n'),
1358  
-            'old-ssi07': ('{%% ssi %s parsed %%}' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'not_here'), {'test': 'Look ma! It parsed!'}, ''),
  1358
+            'old-ssi06': ('{%% ssi %s parsed %%}' % os.path.join(basedir, 'templates', 'ssi_include.html'), {'test': 'Look ma! It parsed!'}, 'This is for testing an ssi include. Look ma! It parsed!\n'),
  1359
+            'old-ssi07': ('{%% ssi %s parsed %%}' % os.path.join(basedir, 'not_here'), {'test': 'Look ma! It parsed!'}, ''),
  1360
+
  1361
+            # Test space in file name
  1362
+            'old-ssi08': ('{%% ssi %s %%}' % os.path.join(basedir, 'templates', 'ssi include with spaces.html'), {}, template.TemplateSyntaxError),
  1363
+            'old-ssi09': ('{%% ssi %s parsed %%}' % os.path.join(basedir, 'templates', 'ssi include with spaces.html'), {'test': 'Look ma! It parsed!'}, template.TemplateSyntaxError),
1359 1364
 
1360 1365
             # Future compatibility
1361 1366
             # Test normal behavior
1362  
-            'ssi01': ('{%% load ssi from future %%}{%% ssi "%s" %%}' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates', 'ssi_include.html'), {}, 'This is for testing an ssi include. {{ test }}\n'),
1363  
-            'ssi02': ('{%% load ssi from future %%}{%% ssi "%s" %%}' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'not_here'), {}, ''),
1364  
-            'ssi03': ("{%% load ssi from future %%}{%% ssi '%s' %%}" % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'not_here'), {}, ''),
  1367
+            'ssi01': ('{%% load ssi from future %%}{%% ssi "%s" %%}' % os.path.join(basedir, 'templates', 'ssi_include.html'), {}, 'This is for testing an ssi include. {{ test }}\n'),
  1368
+            'ssi02': ('{%% load ssi from future %%}{%% ssi "%s" %%}' % os.path.join(basedir, 'not_here'), {}, ''),
  1369
+            'ssi03': ("{%% load ssi from future %%}{%% ssi '%s' %%}" % os.path.join(basedir, 'not_here'), {}, ''),
1365 1370
 
1366 1371
             # Test passing as a variable
1367  
-            'ssi04': ('{% load ssi from future %}{% ssi ssi_file %}', {'ssi_file': os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates', 'ssi_include.html')}, 'This is for testing an ssi include. {{ test }}\n'),
  1372
+            'ssi04': ('{% load ssi from future %}{% ssi ssi_file %}', {'ssi_file': os.path.join(basedir, 'templates', 'ssi_include.html')}, 'This is for testing an ssi include. {{ test }}\n'),
1368 1373
             'ssi05': ('{% load ssi from future %}{% ssi ssi_file %}', {'ssi_file': 'no_file'}, ''),
1369 1374
 
1370 1375
             # Test parsed output
1371  
-            'ssi06': ('{%% load ssi from future %%}{%% ssi "%s" parsed %%}' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'templates', 'ssi_include.html'), {'test': 'Look ma! It parsed!'}, 'This is for testing an ssi include. Look ma! It parsed!\n'),
1372  
-            'ssi07': ('{%% load ssi from future %%}{%% ssi "%s" parsed %%}' % os.path.join(os.path.dirname(os.path.abspath(__file__)), 'not_here'), {'test': 'Look ma! It parsed!'}, ''),
  1376
+            'ssi06': ('{%% load ssi from future %%}{%% ssi "%s" parsed %%}' % os.path.join(basedir, 'templates', 'ssi_include.html'), {'test': 'Look ma! It parsed!'}, 'This is for testing an ssi include. Look ma! It parsed!\n'),
  1377
+            'ssi07': ('{%% load ssi from future %%}{%% ssi "%s" parsed %%}' % os.path.join(basedir, 'not_here'), {'test': 'Look ma! It parsed!'}, ''),
1373 1378
 
  1379
+            # Test space in file name
  1380
+            'ssi08': ('{%% load ssi from future %%}{%% ssi "%s" %%}' % os.path.join(basedir, 'templates', 'ssi include with spaces.html'), {}, 'This is for testing an ssi include with spaces in its name. {{ test }}\n'),
  1381
+            'ssi09': ('{%% load ssi from future %%}{%% ssi "%s" parsed %%}' % os.path.join(basedir, 'templates', 'ssi include with spaces.html'), {'test': 'Look ma! It parsed!'}, 'This is for testing an ssi include with spaces in its name. Look ma! It parsed!\n'),
1374 1382
 
1375 1383
             ### TEMPLATETAG TAG #######################################################
1376 1384
             'templatetag01': ('{% templatetag openblock %}', {}, '{%'),
@@ -1612,6 +1620,16 @@ def get_template_tests(self):
1612 1620
             'static-statictag01': ('{% load static %}{% static "admin/base.css" %}', {}, urljoin(settings.STATIC_URL, 'admin/base.css')),
1613 1621
             'static-statictag02': ('{% load static %}{% static base_css %}', {'base_css': 'admin/base.css'}, urljoin(settings.STATIC_URL, 'admin/base.css')),
1614 1622
         }
  1623
+        # Until Django 1.5, the ssi tag takes an unquoted constant in argument,
  1624
+        # and there's no way to escape spaces. As a consequence it's impossible
  1625
+        # to include a file if its absolute path contains a space, as
  1626
+        # demonstrated by tests old-ssi08 and old-ssi09.
  1627
+        # If the patch to the Django chekout contains a space, the following
  1628
+        # tests will raise an exception too.
  1629
+        if ' ' in basedir:
  1630
+            for test_name in 'old-ssi01', 'old-ssi02', 'old-ssi06', 'old-ssi07':
  1631
+                tests[test_name] = tests[test_name][:-1] + (template.TemplateSyntaxError,)
  1632
+        return tests
1615 1633
 
1616 1634
 class TemplateTagLoading(unittest.TestCase):
1617 1635
 

0 notes on commit bf9ba45

Please sign in to comment.
Something went wrong with that request. Please try again.