Skip to content

Conversation

@hassox
Copy link

@hassox hassox commented Jan 3, 2016

When trying to serialize a DOMElement that has an attribute set to a boolean or is blank, the current behaviour doesn't handle this well.

@mwiencek
Copy link
Collaborator

mwiencek commented Jan 5, 2016

The existing behavior seems more in line with how browsers generally behave—attribute values are coerced into strings, and you need removeAttribute to make them disappear. Outputting the attribute name alone for true also seems wrong for things that aren't actual boolean attribute.

@hassox
Copy link
Author

hassox commented Jan 5, 2016

Unfortunately that isn't how the spec says they should behave (or any browser I've used).

Here's the relevant info:
http://www.w3.org/TR/2008/WD-html5-20080610/semantics.html#boolean

@mwiencek
Copy link
Collaborator

mwiencek commented Jan 5, 2016

I think we're confusing boolean properties with boolean attributes, since this code applies to both. Here's an example of where your patch behaves incorrectly:

var document = require('min-document');

var div = document.createElement('div');
div.setAttribute('data-foo', true);
div.toString();
// WRONG: got '<div data-foo></div>', expected '<div data-foo="true"></div>'

var input = document.createElement('input');
// The spec says if a boolean attribute is present, it should be set to either "" or the name of the attribute ("checked"), but browsers will let you set it to anything.
input.setAttribute('checked', true);
input.toString();
// WRONG: got '<input type="text" checked />', expected '<input type="text" checked="true" />'

Here's where your patch probably behaves correctly:

var document = require('min-document');

var input = document.createElement('input');
input.checked = true;
input.toString();
// OK?: got '<input type="text" checked />'

Note that browsers won't even serialize the checked property as an attribute like that, but it might make sense here.

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

Successfully merging this pull request may close these issues.

2 participants