-
Notifications
You must be signed in to change notification settings - Fork 49
/
index.js
225 lines (185 loc) · 3.79 KB
/
index.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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/**
* Module dependencies.
*/
var domify = require('domify');
var each = require('each');
var events = require('event');
var getKeys = require('keys');
var query = require('query');
var trim = require('trim');
var slice = [].slice;
var isArray = Array.isArray || function (val) {
return !! val && '[object Array]' === Object.prototype.toString.call(val);
};
/**
* Attributes supported.
*/
var attrs = [
'id',
'src',
'rel',
'cols',
'rows',
'type',
'name',
'href',
'title',
'style',
'width',
'height',
'action',
'method',
'tabindex',
'placeholder'
];
/*
* A simple way to check for HTML strings or ID strings
*/
var quickExpr = /^(?:[^#<]*(<[\w\W]+>)[^>]*$|#([\w\-]*)$)/;
/**
* Expose `dom()`.
*/
module.exports = dom;
/**
* Return a dom `List` for the given
* `html`, selector, or element.
*
* @param {String|Element|List} selector
* @param {String|ELement|context} context
* @return {List}
* @api public
*/
function dom(selector, context) {
// array
if (isArray(selector)) {
return new List(selector);
}
// List
if (selector instanceof List) {
return selector;
}
// node
if (selector.nodeName) {
return new List([selector]);
}
if ('string' != typeof selector) {
throw new TypeError('invalid selector');
}
// html
var htmlselector = trim.left(selector);
if (isHTML(htmlselector)) {
return new List([domify(htmlselector)], htmlselector);
}
// selector
var ctx = context
? (context instanceof List ? context[0] : context)
: document;
return new List(query.all(selector, ctx), selector);
}
/**
* Static: Expose `List`
*/
dom.List = List;
/**
* Static: Expose supported attrs.
*/
dom.attrs = attrs;
/**
* Static: Mixin a function
*
* @param {Object|String} name
* @param {Object|Function} obj
* @return {List} self
*/
dom.use = function(name, fn) {
var keys = [];
var tmp;
if (2 == arguments.length) {
keys.push(name);
tmp = {};
tmp[name] = fn;
fn = tmp;
} else if (name.name) {
// use function name
fn = name;
name = name.name;
keys.push(name);
tmp = {};
tmp[name] = fn;
fn = tmp;
} else {
keys = getKeys(name);
fn = name;
}
for(var i = 0, len = keys.length; i < len; i++) {
List.prototype[keys[i]] = fn[keys[i]];
}
return this;
}
/**
* Initialize a new `List` with the
* given array-ish of `els` and `selector`
* string.
*
* @param {Mixed} els
* @param {String} selector
* @api private
*/
function List(els, selector) {
els = els || [];
var len = this.length = els.length;
for(var i = 0; i < len; i++) this[i] = els[i];
this.selector = selector;
}
/**
* Remake the list
*
* @param {String|ELement|context} context
* @return {List}
* @api private
*/
List.prototype.dom = dom;
/**
* Make `List` an array-like object
*/
List.prototype.length = 0;
List.prototype.splice = Array.prototype.splice;
/**
* Array-like object to array
*
* @return {Array}
*/
List.prototype.toArray = function() {
return slice.call(this);
}
/**
* Attribute accessors.
*/
each(attrs, function(name){
List.prototype[name] = function(val){
if (0 == arguments.length) return this.attr(name);
return this.attr(name, val);
};
});
/**
* Mixin the API
*/
dom.use(require('./lib/attributes'));
dom.use(require('./lib/classes'));
dom.use(require('./lib/events'));
dom.use(require('./lib/manipulate'));
dom.use(require('./lib/traverse'));
/**
* Check if the string is HTML
*
* @param {String} str
* @return {Boolean}
* @api private
*/
function isHTML(str) {
// Faster than running regex, if str starts with `<` and ends with `>`, assume it's HTML
if (str.charAt(0) === '<' && str.charAt(str.length - 1) === '>' && str.length >= 3) return true;
// Run the regex
var match = quickExpr.exec(str);
return !!(match && match[1]);
}