[FIX] qweb: fix t-if and t-else and add test

Can not use remove with the loop for in (in _compile_directive_content)
This commit is contained in:
Christophe Matthieu
2016-06-27 08:08:00 +02:00
parent 28656a1a3c
commit 15e1f88a73
3 changed files with 27 additions and 4 deletions
@@ -15,4 +15,18 @@
<t t-if="condition">fail</t>
</t>
<result id="boolean-value-condition-missing"/>
<t t-name="boolean-value-condition-else">
<div><span>begin</span><t t-if="condition">ok</t>
<t t-else="t-else">ok-else</t><span>end</span></div>
</t>
<params id="boolean-value-condition-else">{"condition": true}</params>
<result id="boolean-value-condition-else"><div><span>begin</span>ok<span>end</span></div></result>
<t t-name="boolean-value-condition-false-else">
<div><span>begin</span><t t-if="condition">fail</t>
<t t-else="t-else">fail-else</t><span>end</span></div>
</t>
<params id="boolean-value-condition-false-else">{"condition": false}</params>
<result id="boolean-value-condition-false-else"><div><span>begin</span>fail-else<span>end</span></div></result>
</templates>
+5 -3
View File
@@ -977,10 +977,13 @@ class QWeb(object):
return body
def _compile_directive_else(self, el, options):
if el.attrib.pop('t-else') == '_t_skip_else_':
return []
if not options.pop('t_if', None):
raise ValueError("t-else directive must be preceded by t-if directive")
el.attrib.pop('t-else')
return self._compile_directives(el, options)
compiled = self._compile_directives(el, options)
el.attrib['t-else'] = '_t_skip_else_'
return compiled
def _compile_directive_if(self, el, options):
orelse = []
@@ -990,7 +993,6 @@ class QWeb(object):
raise ValueError("Unexpected non-whitespace characters between t-if and t-else directives")
el.tail = None
orelse = self._compile_node(next_el, dict(options, t_if=True))
next_el.getparent().remove(next_el)
return [
# if $t-if:
# next tag directive
+8 -1
View File
@@ -8,6 +8,7 @@ import os.path
import re
from lxml import etree
from itertools import chain
from odoo.modules import get_module_resource
from odoo.tests.common import TransactionCase
@@ -85,6 +86,12 @@ class FileSystemLoader(object):
arch = etree.tostring(root, encoding='utf-8', xml_declaration=True)
return arch
def stringify_children(node):
parts = ([node.text] +
list(chain(*([c.text, etree.tostring(c), c.tail] for c in node.getchildren() if not isinstance(c, etree._Comment)))) +
[node.tail])
# filter removes possible Nones in texts and tails
return ''.join(filter(None, parts))
class TestQWeb(TransactionCase):
matcher = re.compile(r'^qweb-test-(.*)\.xml$')
@@ -125,7 +132,7 @@ class TestQWeb(TransactionCase):
# so output is predictable & repeatable
params = {} if param is None else json.loads(param.text, object_pairs_hook=collections.OrderedDict)
result = doc.find('result[@id="{}"]'.format(template)).text
result = stringify_children(doc.find('result[@id="{}"]'.format(template)))
self.assertEqual(
qweb.render(template, values=params, load=loader).strip().encode('utf-8'),
(result or u'').strip().encode('utf-8'),