Skip to content

Instantly share code, notes, and snippets.

@1N50MN14
Created February 23, 2013 22:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save 1N50MN14/5021563 to your computer and use it in GitHub Desktop.
Save 1N50MN14/5021563 to your computer and use it in GitHub Desktop.
document shim for hyperscript
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, '&lt;')
.replace(/>/g, '&gt;');
}
}
module.exports = Document;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment