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

allow None as an attribute value, for HTML boolean attributes #201

Merged
merged 2 commits into from Jul 29, 2016

Conversation

@dholth
Copy link
Contributor

@dholth dholth commented Jul 29, 2016

I was writing a script to convert some tags in Angular templates to a new API, and noticed that curiously lxml could parse and re-serialize boolean tags that have no value, but could not create new ones. It turns out libxml2 supports this by accepting NULL as the value for xmlSetNsProp(), creating an attribute node with NULL children instead of a text node.

This change allows None as a value for Element.set() so that those who value attributes with no value can create the same.

Note that lxml.etree.tostring() serializes these as <form novalidate=''></form> while lxml.html.tostring() produces the value in the test.

@@ -564,11 +564,14 @@ cdef int _setAttributeValue(_Element element, key, value) except -1:
if not element._doc._parser._for_html:
_attributeValidOrRaise(tag)
c_tag = _xcstr(tag)
if isinstance(value, QName):
value = _resolveQNameText(element, value)
if value is None:
Copy link
Member

@scoder scoder Jul 29, 2016

Shouldn't it reject None for XML trees and only allow it for HTML trees? The latter is the only use case I can see. XML always requires at least an empty string as value.

Copy link
Contributor Author

@dholth dholth Jul 29, 2016

XML serialization produces the empty string as value, even if it is None. Is there a way to tell which tree you are in?

Copy link
Member

@scoder scoder Jul 29, 2016

Yes. See right above. :)

@scoder
Copy link
Member

@scoder scoder commented Jul 29, 2016

Looks like a nice feature. A comment in the (lxml.html?) docs would be perfect.

@scoder scoder merged commit 04ceed4 into lxml:master Jul 29, 2016
@scoder
Copy link
Member

@scoder scoder commented Jul 29, 2016

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants