Skip to content

Instantly share code, notes, and snippets.

@ringoluo
Created August 19, 2015 03:09
Show Gist options
  • Save ringoluo/2d0c1d0f15c1f942d08c to your computer and use it in GitHub Desktop.
Save ringoluo/2d0c1d0f15c1f942d08c to your computer and use it in GitHub Desktop.
DOM.js from DOM Enlightenment
/**
* Created by Chun on 8/18/2015.
*/
(function (win) {
var global = win;
var doc = this.document;
var dom = function(params, context) {
return new GetOrMakeDom(params, context);
};
var regXContainsTag = /^\s*<(\w+|!)[^>]*>/;
var GetOrMakeDom = function(params, context) {
var currentContext = doc;
if(context) {
if(context.nodeType) {
//it is either a document node or element node
currentContext = context;
} else {
//else it is a string selector, use it to select a node
currentContext = doc.querySelector(context);
}
}
//if no params, return empty dom() object
if(!params || params === '' || typeof params === 'string' && params.trim() === '') {
this.length = 0;
return this;
}
//if HTML string, construct domfragment, fill object, then return object
if(typeof params === 'string' && regXContainsTag.test(params)) {
/* create div and docfrag, append div to docfrag, then set
its div's inner HTML to the string, then get first child */
var divElm = currentContext.createElement('div');
divElm.className = 'hippo-doc-frag-wrapper';
var docFrag = currentContext.createDocumentFragment();
docFrag.appendChild(divElm);
var queryDiv = docFrag.querySelector('div');
queryDiv.innerHTML = params;
var numberOfChildren = queryDiv.children.length;
/* loop over nodelist and fill object, needs to be done
because a string of html can be passed with siblings
*/
for(var z=0; z<numberOfChildren; z++) {
this[z] = queryDiv.children[z];
}
this.length = numberOfChildren;
return this;
}
//if a single node reference is passed, fill object, return object
if(typeof params === 'object' && params.nodeName) {
this.length = 1;
this[0] = params;
return this;
}
/*
if it's an object but not a node assume nodelist or array,
else it's string selector, so create nodelist
*/
var nodes;
if(typeof params !== 'string') {
nodes = params;
} else {
nodes = currentContext.querySelectorAll(params.trim());
}
//loop over array or nodelist created above and fill object
var nodeLength = nodes.length;
for(var i=0; i<nodeLength; i++) {
this[i] = nodes[i];
}
//give the object a length value
this.length = nodeLength;
return this;
};
//expose dom to global scope
global.dom = dom;
//short cut to prototype
dom.fn = GetOrMakeDom.prototype;
dom.fn.each = function(callback) {
var len = this.length; // this returned by calling dom()
for(var i=0; i<len; i++) {
callback.call(this[i], i, this[i]);
}
return this;
};
dom.fn.html = function(htmlString) {
if(htmlString) {
return this.each(function() {
this.innerHTML = htmlString;
});
} else {
return this[0].innerHTML;
}
};
dom.fn.text = function(textString) {
if(textString) {
return this.each(function() {
this.textContent = textString;
})
} else {
return this[0].textContent.trim();
}
};
dom.fn.append = function(StringOrObject) {
return this.each(function() {
if(typeof stringOrObject === 'string') {
this.insertAdjacentHTML('beforeend', StringOrObject);
} else {
var that = this;
dom(stringOrObject).each(function(name,value) {
that.insertAdjacentHTML('beforeend',value.outerHTML);
})
}
})
};
})(window);
// use
dom();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment