Skip to content

Commit

Permalink
Assert key is not in Dynamic Attributes
Browse files Browse the repository at this point in the history
A changing key attribute (but not passed as the `key` parameter) could
cause havoc. Force `key` to always be static.

This is of particular interest to compile to iDOM build steps, since a
runtime `attr(name, value)` would be impossible to catch:

```js
var attrs = { key: 'key' };

elementOpenStart('div');
for (var name in attrs) {
    attr(name, attrs[name]);
}
elementOpenEnd('div');
elementClose('div');
```
  • Loading branch information
jridgewell committed Oct 8, 2015
1 parent 44098f0 commit e8a7632
Showing 1 changed file with 17 additions and 1 deletion.
18 changes: 17 additions & 1 deletion src/virtual_elements.js
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,19 @@ if (process.env.NODE_ENV !== 'production') {

if (tag !== data.nodeName) {
throw new Error('Received a call to close ' + tag + ' but ' +
data.nodeName + ' was open.');
data.nodeName + ' was open.');
}
};


/**
* Makes sure that the key attribute does not appear in dynamic attributes.
* @param {!Object<string, *>} attrs
*/
var assertNoKeyInDynamicAttributes = function(attrs) {
if ('key' in attrs) {
throw new Error('The `key` attribute may not appear in the dynamic' +
'attributes');
}
};

Expand Down Expand Up @@ -177,6 +189,10 @@ var elementOpen = function(tag, key, statics, var_args) {
newAttrs[arguments[i]] = arguments[i + 1];
}

if (process.env.NODE_ENV !== 'production') {
assertNoKeyInDynamicAttributes(newAttrs);
}

for (attr in newAttrs) {
updateAttribute(node, attr, newAttrs[attr]);
}
Expand Down

0 comments on commit e8a7632

Please sign in to comment.