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

Merged
merged 2 commits into from Jul 29, 2016

Conversation

Projects
None yet
2 participants
@dholth
Contributor

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:

This comment has been minimized.

@scoder

scoder Jul 29, 2016

Member

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.

@scoder

scoder Jul 29, 2016

Member

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.

This comment has been minimized.

@dholth

dholth Jul 29, 2016

Contributor

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

@dholth

dholth Jul 29, 2016

Contributor

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

This comment has been minimized.

@scoder

scoder Jul 29, 2016

Member

Yes. See right above. :)

@scoder

scoder Jul 29, 2016

Member

Yes. See right above. :)

+ form.set('novalidate', None)
+ self.assertEqual(html.tostring(form),
+ _bytes('<form novalidate></form>'))
+

This comment has been minimized.

@scoder

scoder Jul 29, 2016

Member

I think there should also be a test for the HTML parser behaviour here.

@scoder

scoder Jul 29, 2016

Member

I think there should also be a test for the HTML parser behaviour here.

This comment has been minimized.

@dholth

dholth Jul 29, 2016

Contributor

Maybe, although I didn't change it at all.

@dholth

dholth Jul 29, 2016

Contributor

Maybe, although I didn't change it at all.

This comment has been minimized.

@scoder

scoder Jul 29, 2016

Member

I know, but you are now making this an official feature. We should better make sure that it actually works, and keeps working.

@scoder

scoder Jul 29, 2016

Member

I know, but you are now making this an official feature. We should better make sure that it actually works, and keeps working.

@scoder

This comment has been minimized.

Show comment
Hide comment
@scoder

scoder Jul 29, 2016

Member

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

Member

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

This comment has been minimized.

Show comment
Hide comment
@scoder

scoder Jul 29, 2016

Member

Thanks!

Member

scoder commented Jul 29, 2016

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment