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

Prevent mutation #8

Merged
merged 2 commits into from Oct 3, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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')
})