Skip to content

Commit

Permalink
New branch to fix TAL default handling (#853)
Browse files Browse the repository at this point in the history
* - new branch to fix TAL default handling

* adapt to changed default handling of `chameleon==3.7.4` (does not yet work

* Python 3 compatibility

* - use Chameleon 3.8

* Adaptations for `chameleon>=3.8`

* - fix lint issue

* - add test for straight boolean after seeing malthe/chameleon#318

* - comment change [ci skip]

Co-authored-by: dieter <dieter@handshake.de>
  • Loading branch information
dataflake and d-maurer committed Jun 26, 2020
1 parent 8171f85 commit c36defd
Show file tree
Hide file tree
Showing 9 changed files with 28 additions and 25 deletions.
2 changes: 1 addition & 1 deletion constraints.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ AccessControl==4.2
Acquisition==4.6
AuthEncoding==4.1
BTrees==4.6.1
Chameleon==3.7.2
Chameleon==3.8.0
DateTime==4.3
DocumentTemplate==3.2.3
ExtensionClass==4.4
Expand Down
2 changes: 1 addition & 1 deletion requirements-full.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ AccessControl==4.2
Acquisition==4.6
AuthEncoding==4.1
BTrees==4.6.1
Chameleon==3.7.2
Chameleon==3.8.0
DateTime==4.3
DocumentTemplate==3.2.3
ExtensionClass==4.4
Expand Down
24 changes: 2 additions & 22 deletions src/Products/PageTemplates/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
from chameleon.codegen import template
from chameleon.exc import ExpressionError
from chameleon.tal import RepeatDict
from chameleon.tales import DEFAULT_MARKER # only in chameleon 3.8.0 and up
from chameleon.zpt.template import Macros

from AccessControl.class_init import InitializeClass
Expand Down Expand Up @@ -288,19 +289,6 @@ class ZtPageTemplate(ChameleonPageTemplate):
but it does not hurt to use the fixed value to represent ``default``
rather than a template specific value.
"""
# override to get the proper ``zope.tales`` default marker
def _compile(self, body, builtins):
code = super(ZtPageTemplate, self)._compile(body, builtins)
# redefine ``__default`` as ``zope.tales.tales._default``
# Potentially this could be simpler
frags = code.split("\n", 5)
for i, frag in enumerate(frags[:-1]):
if frag.startswith("__default = "):
frags[i] = "from zope.tales.tales import _default as __default"
break
else:
raise RuntimeError("unexpected code prelude %s" % frags[:-1])
return "\n".join(frags)

# use `chameleon` configuration to obtain more
# informative error information
Expand Down Expand Up @@ -329,15 +317,7 @@ def __call__(self, context, macros, tal=True, **options):
kwargs["__zt_context__"] = context

template = self.template
# work around ``https://github.com/zopefoundation/Zope/issues/846``
template.cook_check() # ensure `_render` is computed
rf = getattr(template, "_render", None)
if rf is not None:
defs = [c for c in rf.__code__.co_consts if c == '__default']
if len(defs) == 1:
# use the template's private ``default`` representation
# as our TALES ``default`` value
kwargs["default"] = defs[0]
kwargs["default"] = DEFAULT_MARKER

return template.render(**kwargs)

Expand Down
7 changes: 7 additions & 0 deletions src/Products/PageTemplates/tests/input/BooleanAttributes.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<select>
<option value="" tal:attributes="selected python: True">Selected</option>
<option value="a" tal:attributes="selected python: False">Not selected</option>
<option value="ok" tal:attributes="selected python: None">Not selected</option>
<option value="b" tal:attributes="selected nothing">Not selected</option>
<option value="c" tal:attributes="selected string:">Not selected</option>
</select>
2 changes: 2 additions & 0 deletions src/Products/PageTemplates/tests/input/Default.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,7 @@
<p tal:define="foo string:"
tal:content="python: foo or default">Default in Python expression</p>
<p tal:content="context/I_Fail|default">Default in Path expression</p>
<p><span tal:replace="default">Default in replace</span></p>
<p attr="attribute default" tal:attributes="attr default"></p>
</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<select>
<option value="" selected="selected">Selected</option>
<option value="a">Not selected</option>
<option value="ok">Not selected</option>
<option value="b">Not selected</option>
<option value="c">Not selected</option>
</select>
2 changes: 2 additions & 0 deletions src/Products/PageTemplates/tests/output/Default.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,7 @@
<body>
<p>Default in Python expression</p>
<p>Default in Path expression</p>
<p><span>Default in replace</span></p>
<p attr="attribute default"></p>
</body>
</html>
5 changes: 5 additions & 0 deletions src/Products/PageTemplates/tests/testHTMLTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ def testImportOldStyleClass(self):
def testRepeatVariable(self):
self.assert_expected(self.folder.t, 'RepeatVariable.html')

def testBooleanAttributes(self):
# Test rendering an attribute that should be empty or left out
# if the value is non-True
self.assert_expected(self.folder.t, 'BooleanAttributes.html')

def testBooleanAttributesAndDefault(self):
# Zope 2.9 and below support the semantics that an HTML
# "boolean" attribute (e.g. 'selected', 'disabled', etc.) can
Expand Down
2 changes: 1 addition & 1 deletion versions-prod.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ AccessControl = 4.2
Acquisition = 4.6
AuthEncoding = 4.1
BTrees = 4.6.1
Chameleon = 3.7.2
Chameleon = 3.8.0
DateTime = 4.3
DocumentTemplate = 3.2.3
ExtensionClass = 4.4
Expand Down

0 comments on commit c36defd

Please sign in to comment.