-
Notifications
You must be signed in to change notification settings - Fork 5
/
dom.js
126 lines (107 loc) · 3.35 KB
/
dom.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*dom - v0.1.0 - 2012-11-15
* http://http://domenlightenment.com/#12
* Copyright (c) 2012 Cody Lindley; Licensed MIT */
(function(win){
var global = win;
var doc = global.document;
var dom = function(params,context){
return new GetOrMakeDom(params,context);
};
var GetOrMakeDom = function(params,context){
var currentContext = doc;
if(context){
if(context.nodeType){//its either a document node or element node
currentContext = context;
}else{//else its a string selector, use it to selector 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' && /^\s*<(\w+|!)[^>]*>/.test(params)){//yup its forsure html string
//create div & docfrag, append div to docfrag, then set its div's innerHTML to the string, then get first child
var divElm = currentContext.createElement('div');
divElm.className = '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];
}
//give the object a length value
this.length = numberOfChildren;
//return object
return this; //return e.g. {0:ELEMENT_NODE,1:ELEMENT_NODE,length:2}
}
//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 its an object but not a node assume nodelist or array, else its a string selector, so create nodelist
var nodes;
if(typeof params !== 'string'){//nodelist or array
nodes = params;
}else{ //ok string
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 object
return this; //return e.g. {0:ELEMENT_NODE,1:ELEMENT_NODE,length:2}
};
//expose dom to global scope
global.dom = dom;
//global short cut to prototype
dom.fn = GetOrMakeDom.prototype;
})(window);
dom.fn.each = function (callback) {
var len = this.length;
for(var i = 0; i < len; i++){
callback.call(this[i], i, this[i]);
}
return this;
};
dom.fn.html = function(htmlStringOrTextString){
if(htmlStringOrTextString){
return this.each(function(){
this.innerHTML = htmlStringOrTextString;
});
}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);
});
}
});
};