From bad4e8fded86c3ecd839ecf38f19be6a6535e52e Mon Sep 17 00:00:00 2001 From: Oliver Caldwell Date: Sat, 3 Oct 2015 09:06:54 +0100 Subject: [PATCH 1/2] Clear style functions even with a key --- src/Element.js | 8 ++++---- test/style.js | 11 +++++++++++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/Element.js b/src/Element.js index 9d345b6..b14d685 100644 --- a/src/Element.js +++ b/src/Element.js @@ -212,12 +212,12 @@ Element.prototype.toReact = function (index) { if (typeof props.key === 'undefined') { props = clone(props) props.key = uniqueKey() - - delete props.style.setProperty - delete props.style.getProperty - delete props.style.removeProperty } + delete props.style.setProperty + delete props.style.getProperty + delete props.style.removeProperty + return React.createElement(this.nodeName, props, this.text || this.children.map(function (el, i) { if (el instanceof Element) { return el.toReact(i) diff --git a/test/style.js b/test/style.js index 38a904c..167fd3f 100644 --- a/test/style.js +++ b/test/style.js @@ -35,3 +35,14 @@ test('style object methods do not leak through', function (t) { t.equal(typeof r.props.style.getProperty, 'undefined') t.equal(typeof r.props.style.removeProperty, 'undefined') }) + +test('when using a key the style object is still cleaned', function (t) { + var el = mk().node() + el.setAttribute('key', 'test') + el.style.backgroundColor = 'red' + var r = el.toReact() + t.plan(3) + t.equal(typeof r.props.style.setProperty, 'undefined') + t.equal(typeof r.props.style.getProperty, 'undefined') + t.equal(typeof r.props.style.removeProperty, 'undefined') +}) From d6b5afb623f244dfcfb45be4e1725ab97acea6df Mon Sep 17 00:00:00 2001 From: Oliver Caldwell Date: Sat, 3 Oct 2015 09:11:23 +0100 Subject: [PATCH 2/2] Fix mutation on toReact --- src/Element.js | 4 ++-- test/react.js | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/Element.js b/src/Element.js index b14d685..4023bba 100644 --- a/src/Element.js +++ b/src/Element.js @@ -203,14 +203,14 @@ Element.prototype.getElementById = function (id) { Element.prototype.toReact = function (index) { index = index || 0 - var props = this.props + var props = clone(this.props) + props.style = clone(props.style) function uniqueKey () { return 'faux-dom-' + index } if (typeof props.key === 'undefined') { - props = clone(props) props.key = uniqueKey() } diff --git a/test/react.js b/test/react.js index 3ac4063..3649869 100644 --- a/test/react.js +++ b/test/react.js @@ -53,3 +53,11 @@ test('pre-built React elements are rendered into the tree', function (t) { t.plan(1) t.equal(tree.props.children[0].props.foo, 'bar') }) + +test('toReact does not mutate the state', function (t) { + var el = mk().node() + t.plan(2) + t.equal(typeof el.props.style.setProperty, 'function') + el.toReact() + t.equal(typeof el.props.style.setProperty, 'function') +})