-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdom.js
More file actions
503 lines (448 loc) · 16.5 KB
/
dom.js
File metadata and controls
503 lines (448 loc) · 16.5 KB
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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/**
* Copyright © 2015-2017 STRG.AT GmbH, Vienna, Austria
*
* This file is part of the The SCORE Framework.
*
* The SCORE Framework and all its parts are free software: you can redistribute
* them and/or modify them under the terms of the GNU Lesser General Public
* License version 3 as published by the Free Software Foundation which is in the
* file named COPYING.LESSER.txt.
*
* The SCORE Framework and all its parts are distributed without any WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. For more details see the GNU Lesser General Public
* License.
*
* If you have not received a copy of the GNU Lesser General Public License see
* http://www.gnu.org/licenses/.
*
* The License-Agreement realised between you as Licensee and STRG.AT GmbH as
* Licenser including the issue of its valid conclusion and its pre- and
* post-contractual effects is governed by the laws of Austria. Any disputes
* concerning this License-Agreement including the issue of its valid conclusion
* and its pre- and post-contractual effects are exclusively decided by the
* competent court, in whose district STRG.AT GmbH has its registered seat, at
* the discretion of STRG.AT GmbH also the competent court, in whose district the
* Licensee has his registered seat, an establishment or assets.
*/
// Universal Module Loader
// https://github.com/umdjs/umd
// https://github.com/umdjs/umd/blob/v1.0.0/returnExports.js
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
// AMD. Register as an anonymous module.
define(factory);
} else if (typeof module === 'object' && module.exports) {
// Node. Does not work with strict CommonJS, but
// only CommonJS-like environments that support module.exports,
// like Node.
module.exports = factory();
} else {
// Browser globals (root is window)
root.score.extend('dom', [], factory);
}
})(this, function() {
var i, j, result, tmp, wrapped, re, existing,
matches = ['matches', 'webkitMatchesSelector', 'msMatchesSelector'].filter(function(func) {
return func in document.documentElement;
})[0],
dom = function(arg) {
result = Object.create(dom.proto);
if (arg) {
if (typeof arg == 'object' && Object.getPrototypeOf(arg) == dom.proto) {
result = arg;
} else if (Array.isArray(arg) ||
/\[object (HTMLCollection|NodeList)\]/.test(Object.prototype.toString.call(arg))) {
for (i = 0; i < arg.length; i++) {
result.push(arg[i]);
}
} else if (typeof arg == 'object') {
result.push(arg);
} else {
tmp = dom.queryGlobal(arg);
for (i = tmp.length; i--; result.unshift(tmp[i]));
}
}
return result;
};
dom.proto = Object.create(Array.prototype, {
// filtering
first: {get: function() {
if (!this.length) {
throw new Error('Empty list');
}
return dom(this[0]);
}},
DOMNode: {get: function() {
if (!this.length) {
throw new Error('Empty list');
} else if (this.length > 1) {
throw new Error('Attempting Single-Node-Operation on multiple nodes');
}
return this[0];
}},
last: {get: function() {
if (!this.length) {
throw new Error('Empty list');
}
return dom(this[this.length - 1]);
}},
eq: {value: function(index) {
if (index < 0 || index >= this.length) {
throw new Error('Invalid index');
}
return dom(this[index]);
}},
uniq: {value: function() {
result = Object.create(dom.proto);
for (i = 0; i < this.length; i++) {
if (result.indexOf(this[i]) < 0) {
result.push(this[i]);
}
}
return result;
}},
// clone
clone: {value: function(deep) {
if (typeof deep == 'undefined') {
deep = true;
}
result = Object.create(dom.proto);
for (i = 0; i < this.length; i++) {
result.push(this[i].cloneNode(deep));
}
return result;
}},
// queries
matches: {value: function(selector) {
if (!this.length) {
throw new Error('Empty list');
}
for (i = 0; i < this.length; i++) {
if (!dom.testMatch(this[i], selector)) {
return false;
}
}
return true;
}},
empty: {value: function() {
return !this.length;
}},
assertOne: {value: function() {
if (!this.length) {
throw new Error('AssertionError: Empty list');
} else if (this.length > 1) {
throw new Error('AssertionError: Multiple nodes found');
}
return this;
}},
// array functions
toArray: {value: function() {
return Array.prototype.slice.call(this);
}},
concat: {value: function() {
var arr = this.toArray();
var args = Array.prototype.slice.call(arguments);
args = args.reduce(function(result, arg) {
return result.concat(dom(arg).toArray());
}, []);
return dom(arr.concat(args));
}},
// iterators
forEach: {value: function(callback, thisArg) {
for (var i = 0; i < this.length; i++) {
callback.call(thisArg, dom(this[i]), i, this);
}
return this;
}},
map: {value: function(callback, thisArg) {
var result = [];
for (var i = 0; i < this.length; i++) {
result.push(callback.call(thisArg, dom(this[i]), i, this));
}
return result;
}},
filter: {value: function(callback, thisArg) {
var result = Object.create(dom.proto),
i;
if (typeof callback === "string") {
var selector = callback;
for (i = 0; i < this.length; i++) {
if (dom.testMatch(this[i], selector)) {
result.push(this[i]);
}
}
} else {
for (i = 0; i < this.length; i++) {
if (callback.call(thisArg, dom(this[i]), i, this)) {
result.push(this[i]);
}
}
}
return result;
}},
reduce: {value: function(callback /*, intialValue*/) {
var result;
var i = 0;
if (arguments.length == 2) {
result = arguments[1];
} else {
if (this.length < 1) {
throw new Error('Reduce of empty list with no initial value');
}
result = dom(this[0]);
i = 1;
}
for (; i < this.length; i++) {
result = callback(result, dom(this[i]), i, this);
}
return result;
}},
// DOM traversal
children: {value: function(selector) {
result = Object.create(dom.proto);
for (i = 0; i < this.length; i++) {
tmp = this[i].children;
for (j = 0; j < tmp.length; j++) {
if (!selector || dom.testMatch(tmp[j], selector)) {
result.push(tmp[j]);
}
}
}
return result;
}},
parent: {value: function(selector) {
result = Object.create(dom.proto);
for (i = 0; i < this.length; i++) {
tmp = this[i].parentNode;
if (!tmp) {
throw new Error('Element has no parent');
}
if (!selector || dom.testMatch(tmp, selector)) {
result.push(tmp);
}
}
return result;
}},
find: {value: function(selector) {
result = Object.create(dom.proto);
for (i = 0; i < this.length; i++) {
tmp = dom.queryLocal(this[i], selector);
for (j = 0; j < tmp.length; j++) {
result.push(tmp[j]);
}
}
return result;
}},
closest: {value: function(selector) {
result = Object.create(dom.proto);
for (i = 0; i < this.length; i++) {
tmp = this[i].parentNode;
while (tmp && tmp !== document) {
if (dom.testMatch(tmp, selector)) {
result.push(tmp);
break;
}
tmp = tmp.parentNode;
}
}
return result;
}},
// node operations
text: {value: function(value) {
if (typeof value == 'undefined') {
if (!this.length) {
throw new Error('Empty list');
} else if (this.length > 1) {
throw new Error('Attempting Single-Node-Operation on multiple nodes');
}
return this[0].textContent;
}
for (i = 0; i < this.length; i++) {
this[i].textContent = value;
}
return this;
}},
attr: {value: function(attribute, value) {
if (typeof value == 'undefined') {
if (!this.length) {
throw new Error('Empty list');
} else if (this.length > 1) {
throw new Error('Attempting Single-Node-Operation on multiple nodes');
}
return this[0].getAttribute(attribute);
}
if (value === null) {
for (i = 0; i < this.length; i++) {
this[i].removeAttribute(attribute);
}
} else {
for (i = 0; i < this.length; i++) {
this[i].setAttribute(attribute, value);
}
}
return this;
}},
// restructuring
detach: {value: function() {
for (i = 0; i < this.length; i++) {
tmp = this[i].parentNode;
if (!tmp) {
throw new Error('Element has no parent');
}
tmp.removeChild(this[i]);
}
return this;
}},
append: {value: function(value, pivot) {
if (!this.length) {
throw new Error('Empty list');
} else if (this.length > 1) {
throw new Error('Attempting Single-Node-Operation on multiple nodes');
}
wrapped = dom(value);
if (!pivot) {
for (i = 0; i < wrapped.length; i++) {
this[0].appendChild(wrapped[i]);
}
} else {
pivot = dom(pivot);
if (!pivot.length) {
throw new Error('Empty pivot');
} else if (pivot.length > 1) {
throw new Error('Received multiple pivot nodes');
}
if (pivot[0].parentNode !== this[0]) {
throw new Error('Pivot node is not a direct child of `this`');
}
if (pivot[0].nextSibling) {
pivot = pivot[0].nextSibling;
for (i = 0; i < wrapped.length; i++) {
this[0].insertBefore(wrapped[i], pivot);
}
} else {
for (i = 0; i < wrapped.length; i++) {
this[0].appendChild(wrapped[i]);
}
}
}
return this;
}},
prepend: {value: function(value, pivot) {
if (!this.length) {
throw new Error('Empty list');
} else if (this.length > 1) {
throw new Error('Attempting Single-Node-Operation on multiple nodes');
}
wrapped = dom(value);
if (!pivot) {
if (!this[0].childNodes.length) {
return this.append(value);
}
tmp = this[0].childNodes[0];
for (i = 0; i < wrapped.length; i++) {
this[0].insertBefore(wrapped[i], tmp);
}
} else {
pivot = dom(pivot);
if (!pivot.length) {
throw new Error('Empty pivot');
} else if (pivot.length > 1) {
throw new Error('Received multiple pivot nodes');
}
if (pivot[0].parentNode !== this[0]) {
throw new Error('Pivot node is not a direct child of `this`');
}
tmp = pivot[0];
for (i = 0; i < wrapped.length; i++) {
this[0].insertBefore(wrapped[i], tmp);
}
}
return this;
}},
// css classes
// TODO: Use Element.classList when changing minimum supported
// Internet Explorer version to IE10:
// https://developer.mozilla.org/en-US/docs/Web/API/Element/classList
hasClass: {value: function(cls) {
if (!this.length) {
return false;
}
re = new RegExp('(^|\\s+)(' + cls + ')(\\s+|$)');
for (i = 0; i < this.length; i++) {
if (!this[i].className.match(re)) {
return false;
}
}
return true;
}},
addClass: {value: function(cls) {
re = new RegExp('(^|\\s+)(' + cls + ')(\\s+|$)');
for (i = 0; i < this.length; i++) {
existing = this[i].className;
if (!existing) {
this[i].className = cls;
} else if (!existing.match(re)) {
this[i].className = existing + ' ' + cls;
}
}
return this;
}},
toggleClass: {value: function(cls) {
re = new RegExp('(^|\\s+)(' + cls + ')(\\s+|$)');
for (i = 0; i < this.length; i++) {
existing = this[i].className;
if (!existing) {
this[i].className = cls;
} else if (existing.match(re)) {
this[i].className = existing.replace(re, '$3');
} else {
this[i].className = existing + ' ' + cls;
}
}
return this;
}},
removeClass: {value: function(cls) {
re = new RegExp('(^|\\s+)(' + cls + ')(\\s+|$)');
for (i = 0; i < this.length; i++) {
existing = this[i].className;
if (existing) {
this[i].className = existing.replace(re, '$3');
}
}
return this;
}},
// events
on: {value: function(event, callback) {
for (i = 0; i < this.length; i++) {
this[i].addEventListener(event, callback);
}
return this;
}},
off: {value: function(event, callback) {
for (i = 0; i < this.length; i++) {
this[i].removeEventListener(event, callback);
}
return this;
}}
});
dom.create = function(node) {
result = Object.create(dom.proto);
result.push(document.createElement(node));
return result;
};
dom.fromString = function(html) {
tmp = document.createElement('div');
tmp.insertAdjacentHTML('afterbegin', html);
return dom(tmp.children).detach();
};
dom.queryGlobal = document.querySelectorAll.bind(document);
dom.queryLocal = function(root, selector) {
return root.querySelectorAll(selector);
};
dom.testMatch = function(node, selector) {
return node[matches](selector);
};
dom.__version__ = '0.1.1';
return dom;
});