Skip to content

Commit

Permalink
Add option to disable the automatic CDATA behaviour
Browse files Browse the repository at this point in the history
  • Loading branch information
amol- committed Jul 24, 2015
1 parent 79c7b8d commit d3a69c1
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
10 changes: 8 additions & 2 deletions kajiki/tests/test_xml.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,9 @@ def test_expand(self):


def perform(source, expected_output, context=dict(name='Rick'),
mode='xml', is_fragment=True):
tpl = XMLTemplate(source, mode=mode, is_fragment=is_fragment)
mode='xml', is_fragment=True, cdata_scripts=True):
tpl = XMLTemplate(source, mode=mode, is_fragment=is_fragment,
cdata_scripts=cdata_scripts)
try:
rsp = tpl(context).render()
assert isinstance(rsp, str), 'render() must return a unicode string.'
Expand Down Expand Up @@ -127,6 +128,11 @@ def test_script_variable(self):
perform(src, '<script>/*<![CDATA[*/ Rick /*]]>*/</script>', mode='xml')
perform(src, '<script> Rick </script>', mode='html')

def test_CDATA_disabled(self):
src = '<script> $name </script>'
perform(src, '<script> Rick </script>', mode='xml', cdata_scripts=False)
perform(src, '<script> Rick </script>', mode='html', cdata_scripts=False)

def test_CDATA_escaping(self):
src = '''<myxml><data><![CDATA[&gt;&#240; $name]]></data></myxml>'''
perform(src, '<myxml><data><![CDATA[&gt;&#240; Rick]]></data></myxml>', mode='xml')
Expand Down
9 changes: 5 additions & 4 deletions kajiki/xml_template.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@


def XMLTemplate(source=None, filename=None, mode=None, is_fragment=False,
encoding='utf-8', autoblocks=None):
encoding='utf-8', autoblocks=None, cdata_scripts=True):
if source is None:
with open(filename, encoding=encoding) as f:
source = f.read() # source is a unicode string
Expand All @@ -35,7 +35,7 @@ def XMLTemplate(source=None, filename=None, mode=None, is_fragment=False,
doc = _Parser(filename, source).parse()
expand(doc)
compiler = _Compiler(filename, doc, mode=mode, is_fragment=is_fragment,
autoblocks=autoblocks)
autoblocks=autoblocks, cdata_scripts=cdata_scripts)
ir_ = compiler.compile()
return template.from_ir(ir_)

Expand All @@ -50,7 +50,7 @@ def inner(self, node, *args, **kwargs):

class _Compiler(object):
def __init__(self, filename, doc, mode=None, is_fragment=False,
autoblocks=None):
autoblocks=None, cdata_scripts=True):
self.filename = filename
self.doc = doc
self.is_fragment = is_fragment
Expand All @@ -59,6 +59,7 @@ def __init__(self, filename, doc, mode=None, is_fragment=False,
self.function_lnos = {}
self.mod_py = []
self.autoblocks = autoblocks or []
self.cdata_scripts = cdata_scripts
self.in_def = False
self.is_child = False
# The rendering mode is either specified in the *mode* argument,
Expand Down Expand Up @@ -170,7 +171,7 @@ def _compile_xml(self, node):
else:
if node.childNodes:
yield ir.TextNode('>', guard)
if node.tagName in HTML_CDATA_TAGS:
if self.cdata_scripts and node.tagName in HTML_CDATA_TAGS:
# Special behaviour for <script>, <style> tags:
if self.mode == 'xml': # Start escaping
yield ir.TextNode('/*<![CDATA[*/')
Expand Down

0 comments on commit d3a69c1

Please sign in to comment.