From 985cdc39e8b9ec4bccf8af180c6510b3b96e0243 Mon Sep 17 00:00:00 2001 From: kj Date: Thu, 1 Mar 2018 15:56:54 -0800 Subject: [PATCH] Adds second half of fix for choojs/nanocomponent #65 --- index.js | 10 +++++++++- package.json | 1 + test/proxy.js | 19 +++++++++++++++++++ 3 files changed, 29 insertions(+), 1 deletion(-) create mode 100644 test/proxy.js diff --git a/index.js b/index.js index efb0599..1fb19ab 100644 --- a/index.js +++ b/index.js @@ -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 @@ -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++ } } diff --git a/package.json b/package.json index 5531588..a62eac5 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/test/proxy.js b/test/proxy.js new file mode 100644 index 0000000..b551d15 --- /dev/null +++ b/test/proxy.js @@ -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`` + var b = html`` + var realNode = html`leaky?` + var proxyA = html`
` + 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() +})