Skip to content

Commit

Permalink
Adds second half of fix for choojs/nanocomponent #65
Browse files Browse the repository at this point in the history
  • Loading branch information
kristoferjoseph committed Mar 1, 2018
1 parent 29d6058 commit 985cdc3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 1 deletion.
10 changes: 9 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ var morph = require('./lib/morph')
var TEXT_NODE = 3
// var DEBUG = false

function isProxy (node) {
return node && node.dataset && node.dataset.proxy !== undefined
}

module.exports = nanomorph

// Morph one tree into another tree
Expand Down Expand Up @@ -133,7 +137,11 @@ function updateChildren (newNode, oldNode) {

// Insert the node at the index if we couldn't morph or find a matching node
} else {
oldNode.insertBefore(newChild, oldChild)
if (isProxy(newChild) && !newChild.isSameNode(oldChild) && newChild.realNode) {
oldNode.insertBefore(newChild.realNode, oldChild)
} else {
oldNode.insertBefore(newChild, oldChild)
}
offset++
}
}
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"deps": "dependency-check . && dependency-check . --extra --no-dev -i nanoassert",
"test": "standard && npm run deps && browserify test/index.js | tape-run",
"test:fast": "browserify test/diff.js | tape-run",
"test:proxy": "browserify test/proxy.js | tape-run",
"start": "bankai start --debug test/diff.js"
},
"repository": "yoshuawuyts/nanomorph",
Expand Down
19 changes: 19 additions & 0 deletions test/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
var test = require('tape')
var html = require('bel')
var nanomorph = require('../')

// FIXME: Need a way to test this... any ideas?
test('do not leak proxy nodes', t => {
var a = html`<ul><li><div><span id="a">leaky?</span></div></li></ul>`
var b = html`<ul><li><span id="a">leaky?</span></li></ul>`
var realNode = html`<span id="a">leaky?</span>`
var proxyA = html`<div id="a" data-proxy=''></div>`
proxyA.realNode = realNode
proxyA.isSameNode = function (el) {
return el === realNode
}
var actual = nanomorph(a, b)
console.log('ACTUAL', actual)
t.ok(actual, actual.outerHTML)
t.end()
})

0 comments on commit 985cdc3

Please sign in to comment.