Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Products.PageTemplates (with chameleon): bad comment handling #716

Closed
d-maurer opened this issue Oct 24, 2019 · 6 comments · Fixed by zopefoundation/z3c.pt#18
Closed

Products.PageTemplates (with chameleon): bad comment handling #716

d-maurer opened this issue Oct 24, 2019 · 6 comments · Fixed by zopefoundation/z3c.pt#18
Labels

Comments

@d-maurer
Copy link
Contributor

Some comments are badly handled.

This works

>>> from Products.PageTemplates.PageTemplate import PageTemplate
>>> pt = PageTemplate()
>>> pt.pt_edit(r'''
... <html xmlns:tal="http://xml.zope.org/namespaces/tal"><body>
... <div tal:content="string:1" />
... <!--
...         <div tal:repeat="si stock_items">
...           <a tal:attributes="href si"</a>
...       </div>
... --> 
... </body></html>''',
... "text/html"
... )
>>> pt()
'\n<html><body>\n<div>1</div>\n<!--\n        <div tal:repeat="si stock_items">\n          <a tal:attributes="href si"</a>\n      </div>\n--> \n</body></html>'

This does not:

>>> from Products.PageTemplates.PageTemplate import PageTemplate
>>> pt = PageTemplate()
>>> pt.pt_edit(r'''
... <html xmlns:tal="http://xml.zope.org/namespaces/tal"><body>
... <div tal:content="string:1" />
... <!--
...         <div tal:repeat="si stock_items">
...           <a tal:attributes="href string:${si/mid}"</a>
...       </div>
... --> 
... </body></html>''',
... "text/html"
... )
>>> pt()
Traceback (most recent call last):
  File "/home/dieter/tmp/xmlsec3/python/lib/python3.6/site-packages/chameleon/utils.py", line 405, in __getitem__
    return dict.__getitem__(self, key)
KeyError: 'si'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/dieter/tmp/xmlsec3/python/lib/python3.6/site-packages/chameleon/template.py", line 183, in render
    self._render(stream, econtext, rcontext)
  File "4ed89ab573418b650d2a190af61afb86.py", line 139, in render
  File "/home/dieter/tmp/xmlsec3/python/lib/python3.6/site-packages/chameleon/utils.py", line 407, in __getitem__
    raise NameError(key)
NameError: si

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/dieter/tmp/Zope/src/Products/PageTemplates/PageTemplate.py", line 103, in __call__
    return self.pt_render(extra_context={'options': kwargs})
  File "/home/dieter/tmp/Zope/src/Products/PageTemplates/PageTemplate.py", line 85, in pt_render
    showtal=showtal)
  File "/home/dieter/tmp/xmlsec3/python/lib/python3.6/site-packages/zope/pagetemplate/pagetemplate.py", line 135, in pt_render
    strictinsert=0, sourceAnnotations=sourceAnnotations
  File "/home/dieter/tmp/Zope/src/Products/PageTemplates/engine.py", line 88, in __call__
    return self.template.render(**kwargs)
  File "/home/dieter/tmp/xmlsec3/python/lib/python3.6/site-packages/z3c/pt/pagetemplate.py", line 158, in render
    return base_renderer(**context)
  File "/home/dieter/tmp/xmlsec3/python/lib/python3.6/site-packages/chameleon/zpt/template.py", line 305, in render
    return super(PageTemplate, self).render(**_kw)
  File "/home/dieter/tmp/xmlsec3/python/lib/python3.6/site-packages/chameleon/template.py", line 203, in render
    raise_with_traceback(exc, tb)
  File "/home/dieter/tmp/xmlsec3/python/lib/python3.6/site-packages/chameleon/utils.py", line 75, in raise_with_traceback
    raise exc
  File "/home/dieter/tmp/xmlsec3/python/lib/python3.6/site-packages/chameleon/template.py", line 183, in render
    self._render(stream, econtext, rcontext)
  File "4ed89ab573418b650d2a190af61afb86.py", line 139, in render
  File "/home/dieter/tmp/xmlsec3/python/lib/python3.6/site-packages/chameleon/utils.py", line 407, in __getitem__
    raise NameError(key)
NameError: si

 - Expression: "si/mid"
 - Filename:   <string>
 - Location:   (line 6: col 43)
 - Arguments:  template: <PageTemplate - at 0xb59fa78c>
               options: {...} (1)
               nothing: <NoneType - at 0x84687d0>
               request: <NoneType - at 0x84687d0>
               modules: <SimpleModuleImporter - at 0xb59fa92c>
               default: <object - at 0xb6bea8e0>
               repeat: {...} (0)
               loop: {...} (0)
               wrapped_repeat: <SafeMapping - at 0xb59fae8c>
               target_language: <NoneType - at 0x84687d0>
               translate: <function translate at 0xb5999974>

Apparently, sometimes, expressions in comments are (wrongly) evaluated.

This is almost surely a chameleon bug.

@malthe
Copy link
Contributor

malthe commented Oct 24, 2019

I'm pretty sure it's intentional: https://github.com/malthe/chameleon/blob/master/src/chameleon/zpt/program.py#L657

But if this is not Zope default behavior (and I'm guessing it's not), then it should be behind a feature gate. Then normally, that feature gate is disabled in the Zope integration (z3c.pt).

@d-maurer
Copy link
Contributor Author

d-maurer commented Oct 24, 2019 via email

@malthe
Copy link
Contributor

malthe commented Oct 24, 2019

Agreed. But the solution in this case is to comment using <!--?.

I agree that it might not be the best thought-through feature. But then again, commenting out TAL should be done using tal:condition="False". Or use the dedicated syntax: <!--?.

@d-maurer
Copy link
Contributor Author

The current Zope switches from its own page template implementation to chameleon implicitly -- without being (explicitly) instructed to do so. I noticed the problem while migrating an old application to Zope 4. The affected template was never designed to be used with chameleon - thus it used standard HTML/XML commenting, not one of the special chameleon commenting forms. In this context, it seemed bizarre that (apparently) a tal:content was evaluated and caused a NameError while the enclosing tal:repeat defining this name was not.
I could well imagine that chameleon wants to be able to generate HTML/XML comments and thus processes the material inside the comment, but then it should execute both the tal:repeat as well as the tal:content. Thanks to your explanation, I now see that inside a (normal HTML/XML) comment, $<expression> is evaluated wherever it occurs - and those are the only evaluations inside the comment. Thus, chameleon did not evaluate the tal_content but only the contained $<expression>.

@malthe
Copy link
Contributor

malthe commented Oct 25, 2019

Yeah that's exactly it. And as I mentioned, that "feature" should probably be put behind a configuration flag which is pretty easy to do. I'll see if I can find the time to set it up.

@malthe
Copy link
Contributor

malthe commented Mar 24, 2020

This kind now be disabled using enable_comment_interpolation=False. When a new release of Chameleon is out, this can added to https://github.com/zopefoundation/z3c.pt/blob/master/src/z3c/pt/pagetemplate.py#L82.

d-maurer added a commit to zopefoundation/z3c.pt that referenced this issue Mar 31, 2020
dataflake pushed a commit to zopefoundation/z3c.pt that referenced this issue Mar 31, 2020
dataflake added a commit that referenced this issue Mar 31, 2020
dataflake added a commit that referenced this issue Mar 31, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants