Skip to content

Instantly share code, notes, and snippets.

@shashi
Last active March 29, 2016 18:29
Show Gist options
  • Save shashi/167eef348f268049460f to your computer and use it in GitHub Desktop.
Save shashi/167eef348f268049460f to your computer and use it in GitHub Desktop.
Sneaky, Shady DOM
(function() {
function Sneaky (node) {
var lightNode = Polymer.dom(node)
this.lightNode = lightNode
this.node = lightNode.node
}
Sneaky.prototype = Polymer.dom()
Object.defineProperties(Sneaky.prototype, {
childNodes: {
get: function() {
var nodes = this.lightNode.childNodes
return nodes.map(function (node) { return new Sneaky(node) })
},
configurable: true
},
children: {
get: function() {
var nodes = this.lightNode.children
return nodes.map(function (node) { return new Sneaky(node) })
},
configurable: true
},
parentNode: {
get: function() {
return this.lightNode ? new Sneaky(this.lightNode.parentNode) : null
},
configurable: true
}
});
function getNode(x) {
return x.node ? x.node : x
}
// forward mutations to light DOM
Sneaky.prototype.appendChild = function (node) {
return new Sneaky(this.lightNode.appendChild(getNode(node)))
}
Sneaky.prototype.removeChild = function (node) {
return new Sneaky(this.lightNode.removeChild(getNode(node)))
}
Sneaky.prototype.insertBefore = function (node, refNode) {
return new Sneaky(this.lightNode.insertBefore(getNode(node), refNode && getNode(refNode) || null))
}
window.Sneaky = Sneaky;
window.sneakyDoc = {
document: {
createElement: function (x, y) {
return new Sneaky(document.createElement(x, y))
},
},
extractNode: getNode
}
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment