Skip to content

Commit 60ff779

Browse files
committed
Do not allow attribute names of '0'
While working with dynamically created elements and attributes, it's surprisingly easy to wind up with an attribute name of '0'. When this happens, nomplate throws an arcane, disconnnected, async and hard to debug exception because DOM elements do not allow attributes with this name. Could consider doing something more helpful too, but for now let's not blow up.
1 parent c719a7e commit 60ff779

File tree

3 files changed

+9
-1
lines changed

3 files changed

+9
-1
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "nomplate",
3-
"version": "1.3.33",
3+
"version": "1.3.34",
44
"description": "Nomplate: Node template engine",
55
"main": "index.js",
66
"engines": {

src/operations.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ function setId(value) {
2323
function setAttribute(name, value) {
2424
return function _setAttribute(domElement, stack, document) {
2525
const updatedName = name === 'key' ? constants.NOM_ATTR_KEY : name;
26+
if (updatedName === '0') {
27+
return domElement;
28+
}
2629

2730
if (typeof value === 'boolean' && !value) {
2831
domElement.removeAttribute(updatedName);

test/render_element_test.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ describe('renderElement', () => {
6262
assert.equal(domElement.outerHTML, '<option value="0"></option>');
6363
});
6464

65+
it('does not apply attribute values if the key is "0"', () => {
66+
const domElement = renderElement(dom.div({0: 'abcd', className: 'foo'}), doc);
67+
assert.equal(domElement.outerHTML, '<div class="foo"></div>');
68+
});
69+
6570
it('creates children', () => {
6671
const domElement = renderElement(dom.div(() => {
6772
dom.ul(() => {

0 commit comments

Comments
 (0)