Skip to content
This repository has been archived by the owner on Sep 7, 2020. It is now read-only.

Commit

Permalink
Merge pull request #8 from Olical/prevent-mutation
Browse files Browse the repository at this point in the history
Prevent mutation
  • Loading branch information
Olical committed Oct 3, 2015
2 parents 0d0f7b5 + d6b5afb commit 6869e59
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/Element.js
Expand Up @@ -203,21 +203,21 @@ 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()

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)
Expand Down
8 changes: 8 additions & 0 deletions test/react.js
Expand Up @@ -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')
})
11 changes: 11 additions & 0 deletions test/style.js
Expand Up @@ -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')
})

0 comments on commit 6869e59

Please sign in to comment.