Skip to content

Commit 83f5a46

Browse files
authored
Merge pull request #98 from choojs/proxynode
Adds half of fix for nanocomponent #65
2 parents ccbffcf + 985cdc3 commit 83f5a46

File tree

3 files changed

+34
-6
lines changed

3 files changed

+34
-6
lines changed

index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ var morph = require('./lib/morph')
44
var TEXT_NODE = 3
55
// var DEBUG = false
66

7+
function isProxy (node) {
8+
return node && node.dataset && node.dataset.proxy !== undefined
9+
}
10+
711
module.exports = nanomorph
812

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

134138
// Insert the node at the index if we couldn't morph or find a matching node
135139
} else {
136-
oldNode.insertBefore(newChild, oldChild)
140+
if (isProxy(newChild) && !newChild.isSameNode(oldChild) && newChild.realNode) {
141+
oldNode.insertBefore(newChild.realNode, oldChild)
142+
} else {
143+
oldNode.insertBefore(newChild, oldChild)
144+
}
137145
offset++
138146
}
139147
}

package.json

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
"deps": "dependency-check . && dependency-check . --extra --no-dev -i nanoassert",
88
"test": "standard && npm run deps && browserify test/index.js | tape-run",
99
"test:fast": "browserify test/diff.js | tape-run",
10+
"test:proxy": "browserify test/proxy.js | tape-run",
1011
"start": "bankai start --debug test/diff.js"
1112
},
1213
"repository": "yoshuawuyts/nanomorph",
@@ -27,13 +28,13 @@
2728
"assert": "nanoassert"
2829
},
2930
"devDependencies": {
30-
"bankai": "^7.6.2",
31-
"bel": "^5.1.1",
32-
"browserify": "^14.1.0",
31+
"bankai": "^9.8.0",
32+
"bel": "^5.1.7",
33+
"browserify": "^16.1.0",
3334
"dependency-check": "^2.5.1",
3435
"math-random-seed": "^1.0.0",
3536
"standard": "^10.0.3",
36-
"tape": "^4.6.0",
37-
"tape-run": "^3.0.0"
37+
"tape": "^4.9.0",
38+
"tape-run": "^3.0.4"
3839
}
3940
}

test/proxy.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var test = require('tape')
2+
var html = require('bel')
3+
var nanomorph = require('../')
4+
5+
// FIXME: Need a way to test this... any ideas?
6+
test('do not leak proxy nodes', t => {
7+
var a = html`<ul><li><div><span id="a">leaky?</span></div></li></ul>`
8+
var b = html`<ul><li><span id="a">leaky?</span></li></ul>`
9+
var realNode = html`<span id="a">leaky?</span>`
10+
var proxyA = html`<div id="a" data-proxy=''></div>`
11+
proxyA.realNode = realNode
12+
proxyA.isSameNode = function (el) {
13+
return el === realNode
14+
}
15+
var actual = nanomorph(a, b)
16+
console.log('ACTUAL', actual)
17+
t.ok(actual, actual.outerHTML)
18+
t.end()
19+
})

0 commit comments

Comments
 (0)