Created
February 23, 2013 22:05
-
-
Save 1N50MN14/5021563 to your computer and use it in GitHub Desktop.
document shim for hyperscript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function Document() {} | |
Document.prototype.createTextNode = function(v) { | |
var n = new Text(); | |
n.value = v; | |
return n; | |
} | |
Document.prototype.createElement = function(nodeName) { | |
var el = new Element(); | |
el.nodeName = nodeName; | |
return el; | |
} | |
function Element() { | |
this.childNodes = []; | |
} | |
Element.prototype.appendChild = function(child) { | |
child.parentElement = this; | |
this.childNodes.push(child); | |
} | |
Element.prototype.replaceChild = function(newChild, oldChild) { | |
var self = this; | |
this.childNodes.forEach(function(child, index){ | |
if (child === oldChild) | |
self.childNodes[index] = newChild; | |
}); | |
} | |
function Text(){ | |
return new Element(); | |
} | |
Text.prototype.toString = function() { | |
return escapeHTML(this.value); | |
function escapeHTML(s) { | |
return String(s) | |
.replace(/&/g, '&') | |
.replace(/"/g, ''') | |
.replace(/'/g, '"') | |
.replace(/</g, '<') | |
.replace(/>/g, '>'); | |
} | |
} | |
module.exports = Document; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment