Browse files

Fixed issue #76 (edge-case with special characters and attribute tran…

…slation).
  • Loading branch information...
1 parent dd6415f commit 3d143d89c21fae243a97fa5d84e944e1dbd161af @malthe malthe committed Dec 13, 2011
Showing with 24 additions and 2 deletions.
  1. +12 −0 CHANGES.rst
  2. +1 −1 src/chameleon/compiler.py
  3. +6 −0 src/chameleon/i18n.py
  4. +5 −1 src/chameleon/tests/test_templates.py
View
12 CHANGES.rst
@@ -1,6 +1,18 @@
Changes
=======
+In next releases ...
+
+Bugfixes:
+
+- If an attribute name for translation was not a valid Python
+ identifier, the compiler would generate invalid code. This has been
+ fixed, and the compiler now also throws an exception if an attribute
+ specification contains a comma. (Note that the only valid separator
+ character is the semicolon, when specifying attributes for
+ translation via the ``i18n:translate`` statement). This addresses
+ issue #76.
+
2.6.2 (2011-12-08)
------------------
View
2 src/chameleon/compiler.py
@@ -73,7 +73,7 @@
])
-RE_MANGLE = re.compile('[\-: ]')
+RE_MANGLE = re.compile('[^\w_]')
RE_NAME = re.compile('^%s$' % NAME)
if DEBUG_MODE:
View
6 src/chameleon/i18n.py
@@ -80,6 +80,11 @@ def parse_attributes(attrs, xml=True):
attrs = [spec for spec in attrs.split(";") if spec]
for spec in attrs:
+ if ',' in spec:
+ raise CompilationError(
+ "Attribute must not contain comma. Use semicolon to "
+ "list multiple attributes", spec
+ )
parts = spec.split()
if len(parts) == 2:
attr, msgid = parts
@@ -91,6 +96,7 @@ def parse_attributes(attrs, xml=True):
"Illegal i18n:attributes specification.", spec)
if not xml:
attr = attr.lower()
+ attr = attr.strip()
if attr in d:
raise CompilationError(
"Attribute may only be specified once in i18n:attributes", attr)
View
6 src/chameleon/tests/test_templates.py
@@ -168,7 +168,7 @@ def wrapper(self):
exc = sys.exc_info()[1]
return func(self, body, exc)
else:
- self.fail("Expected exception; got: %s." % result)
+ self.fail("Expected exception.")
return wrapper
return decorator
@@ -201,6 +201,10 @@ def test_syntax_error_in_non_strict_mode(self):
def test_attributes_on_tal_tag_fails(self, body, exc):
self.assertTrue(body[exc.offset:].startswith('dummy'))
+ @error("""<tal:dummy i18n:attributes=\"foo, bar\" />""")
+ def test_i18n_attributes_with_non_identifiers(self, body, exc):
+ self.assertTrue(body[exc.offset:].startswith('foo,'))
+
def test_encoded(self):
filename = '074-encoded-template.pt'
with open(os.path.join(self.root, 'inputs', filename), 'rb') as f:

0 comments on commit 3d143d8

Please sign in to comment.