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

Bug in rendering tal:attributes in case of boolean values? #318

Closed
gh-PonyM opened this issue Jun 26, 2020 · 15 comments
Closed

Bug in rendering tal:attributes in case of boolean values? #318

gh-PonyM opened this issue Jun 26, 2020 · 15 comments

Comments

@gh-PonyM
Copy link

We discovered a changement in rendering of boolean attributes in the option tag since release 3.8.0 (using python 3.8).
Example:
<select> <option value="" tal:attributes="selected True">Selected</option> <option value="a" tal:attributes="selected False">Not selected</option> <option value="ok" tal:attributes="selected None">OK</option> </select>
will render to:
<select> <option value="" selected="True">Selected</option> <option value="a" selected="False">Not selected</option> <option value="ok">OK</option> </select>
Can somebody confirm this?
The workaround for now would be to pin down the version to 3.7.4.

@malthe
Copy link
Owner

malthe commented Jun 26, 2020

You need to configure boolean_attributes. It's a template option that takes a set of attributes, for example "selected", "checked", etc.

For an example, see https://github.com/zopefoundation/z3c.pt/blob/master/src/z3c/pt/pagetemplate.py#L40.

dataflake added a commit to zopefoundation/Zope that referenced this issue Jun 26, 2020
dataflake added a commit to zopefoundation/Zope that referenced this issue Jun 26, 2020
* - 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>
dataflake added a commit to zopefoundation/Zope that referenced this issue Jun 26, 2020
* - 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>
@stevepiercy
Copy link
Contributor

@DadaDaMotha I confirm the same behavior. Chameleon 3.7.4 does not exhibit this behavior. I have pinned my dependencies to Chameleon < 3.8.0.

In Deform, all CheckboxChoiceWidgets (and probably other checkbox widgets) render a checked attribute regardless of its actual value. If the value is False, it should not render a checked attribute.

I'll follow up after I deal with the fire drill this change caused.

@malthe
Copy link
Owner

malthe commented Jun 27, 2020

@stevepiercy let me know how you fare – it should be relatively straight-forward to get those boolean_attributes in there, after which the behavior should essentially be back to normal.

@stevepiercy
Copy link
Contributor

Ha, you assume I know what I'm doing! 😬

I honestly don't know whether to look first in Deform or pyramid_chameleon. I spend more time staring blankly at the code and drooling than understanding it. Ah, well, no time like the present to start learning.

@stevepiercy
Copy link
Contributor

My first guess was to scan for any imports of chameleon in either library. I then tried to understand code for each result, but I'm failing to comprehend it.

@stevepiercy
Copy link
Contributor

Our issues were fixed by Pylons/deform#418

@malthe
Copy link
Owner

malthe commented Jun 28, 2020 via email

@malthe malthe closed this as completed Jun 28, 2020
@gh-PonyM
Copy link
Author

Dear maintainers,
By now, I don't think this issue is fixed. By using the BOOLEAN_HTML_ATTRIBUTES in https://github.com/zopefoundation/z3c.pt/blob/master/src/z3c/pt/pagetemplate.py#L40 like:

from chameleon.zpt.template import PageTemplate
ts = "<select> <option value="1" tal:attributes="selected True">Selected</option></select>"
page = PageTemplate(ts, boolean_attributes=BOOLEAN_HTML_ATTRS)()

page will yield <option value="1" selected="selected">Selected</option>
Our custom integration shows the same...

@malthe
Copy link
Owner

malthe commented Aug 18, 2020

I am not sure I understand. What is the expected output?

@gh-PonyM
Copy link
Author

Hi malthe, thx for your reply. I would expect <option value="1" selected">Selected</option>.

@mauritsvanrees
Copy link

mauritsvanrees commented Aug 19, 2020

[Edit: our replies crossed.]

The comment in z3c.pt says about BOOLEAN_HTML_ATTRS:

List of Boolean attributes in HTML that should be rendered in minimized form (e.g. <img ismap> rather than <img ismap="">)

So I think he expects this as output:

<option value="1" selected>Selected</option>

I think the generated html is fine, at least better than what it was according to the original issue comment, especially when it could be selected="False".
I wonder if the comment in z3c.pt is simply wrong.
Note: I have not tried any of this.

@stevepiercy
Copy link
Contributor

stevepiercy commented Aug 19, 2020

I think the comment is a misinterpretation of the W3 specification.

        # List of Boolean attributes in HTML that should be rendered in
        # minimized form (e.g. <img ismap> rather than <img ismap="">)
        # From http://www.w3.org/TR/xhtml1/#guidelines (C.10)
        # TODO: The problem with this is that this is not valid XML and
        # can't be parsed back!

The link should be https://www.w3.org/TR/xhtml1/#C_10, which is quoted below.

Some HTML user agents are unable to interpret boolean attributes when these appear in their full (non-minimized) form, as required by XML 1.0. Note this problem doesn't affect user agents compliant with HTML 4. [emphasis added] The following attributes are involved: compact, nowrap, ismap, declare, noshade, checked, disabled, readonly, multiple, selected, noresize, defer.

In other words, it is perfectly OK, and actually better to use, the full form, such as selected="selected". The full form is required by XML 1.0 and is understood by any user agent that supports HTML 4, which is over a decade old.

@gh-PonyM
Copy link
Author

gh-PonyM commented Aug 19, 2020

Okay, now I understand and true, I was mislead by that comment. Thanks very much for the clarifications.
However, I found something else that is really part of the bug. I still get <option selected=False></option> if used inside a macro. I'll try to figure out why.

@stevepiercy
Copy link
Contributor

@malthe @mauritsvanrees should I submit a PR to update that comment? I think its current state is a misinterpretation of the specification.

@malthe
Copy link
Owner

malthe commented Aug 20, 2020

@stevepiercy yeah I think that would be helpful. Thanks!

stevepiercy added a commit to zopefoundation/z3c.pt that referenced this issue Aug 20, 2020
This PR updates the comment on boolean HTML attributes to accurately reflect the intent of the XHTML 1.0 specification.

See malthe/chameleon#318 (comment)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants