Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store realNode reference on proxy node #75

Merged
merged 5 commits into from
Mar 3, 2018
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ Nanocomponent.prototype._createProxy = function () {
proxy.isSameNode = function (el) {
return (el && el.dataset.nanocomponent === self._ncID)
}
proxy.realNode = this.element
return proxy
}

Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"test:deps": "dependency-check .",
"test:node": "NODE_ENV=test node test/node.js | tap-format-spec",
"test:browser": "browserify test/browser/index.js | tape-run | tap-format-spec",
"test:proxy": "browserify test/browser/proxy-leak.js | tape-run | tap-format-spec",
"test:lint": "standard *.js",
"start": "bankai start example"
},
Expand Down Expand Up @@ -42,7 +43,7 @@
"dependencies": {
"global": "^4.3.1",
"nanoassert": "^1.1.0",
"nanomorph": "^5.1.2",
"nanomorph": "^6.0.0-beta1",
"nanotiming": "^7.2.0",
"on-load": "^3.3.4"
},
Expand Down
63 changes: 63 additions & 0 deletions test/browser/proxy-leak.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
var test = require('tape')
var morph = require('nanomorph')
var Nanocomponent = require('../../')
var html = require('bel')

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

class Component extends Nanocomponent {
createElement () {
return html`<div>I'm a component</div>`
}

update () {
return false
}
}

function makeID () {
return 'testid-' + Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1)
}

function createTestElement () {
var testRoot = document.createElement('div')
testRoot.id = makeID()
document.body.appendChild(testRoot)
return testRoot
}

test('should not leak proxy nodes', t => {
var testRoot = createTestElement()
var component = new Component()
var view = viewA()
var newView

function viewA () {
return html`
<body>
<a href="/">beep</a>
<a href="/boop">boop</a>
<div>
${component.render()}
</div>
</body>
`
}

function viewB () {
return html`
<body>
<a href="/">beep</a>
<a href="/boop">boop</a>
${component.render()}
</body>
`
}

testRoot.appendChild(view)
newView = morph(view, viewB())
t.notOk(isProxy(newView.children[2]), 'proxy node has not been leaked')
t.end()
})