-
Notifications
You must be signed in to change notification settings - Fork 45
/
dom4.js
740 lines (714 loc) · 21.9 KB
/
dom4.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
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
(function(window){'use strict';
/* jshint loopfunc: true, noempty: false*/
// http://www.w3.org/TR/dom/#element
function createDocumentFragment() {
return document.createDocumentFragment();
}
function createElement(nodeName) {
return document.createElement(nodeName);
}
function enoughArguments(length, name) {
if (!length) throw new Error(
'Failed to construct ' +
name +
': 1 argument required, but only 0 present.'
);
}
function mutationMacro(nodes) {
if (nodes.length === 1) {
return textNodeIfPrimitive(nodes[0]);
}
for (var
fragment = createDocumentFragment(),
list = slice.call(nodes),
i = 0; i < nodes.length; i++
) {
fragment.appendChild(textNodeIfPrimitive(list[i]));
}
return fragment;
}
function textNodeIfPrimitive(node) {
return typeof node === 'object' ? node : document.createTextNode(node);
}
for(var
head,
property,
TemporaryPrototype,
TemporaryTokenList,
wrapVerifyToken,
document = window.document,
hOP = Object.prototype.hasOwnProperty,
defineProperty = Object.defineProperty || function (object, property, descriptor) {
if (hOP.call(descriptor, 'value')) {
object[property] = descriptor.value;
} else {
if (hOP.call(descriptor, 'get'))
object.__defineGetter__(property, descriptor.get);
if (hOP.call(descriptor, 'set'))
object.__defineSetter__(property, descriptor.set);
}
return object;
},
indexOf = [].indexOf || function indexOf(value){
var length = this.length;
while(length--) {
if (this[length] === value) {
break;
}
}
return length;
},
// http://www.w3.org/TR/domcore/#domtokenlist
verifyToken = function (token) {
if (!token) {
throw 'SyntaxError';
} else if (spaces.test(token)) {
throw 'InvalidCharacterError';
}
return token;
},
DOMTokenList = function (node) {
var
noClassName = typeof node.className === 'undefined',
className = noClassName ?
(node.getAttribute('class') || '') : node.className,
isSVG = noClassName || typeof className === 'object',
value = (isSVG ?
(noClassName ? className : className.baseVal) :
className
).replace(trim, '')
;
if (value.length) {
properties.push.apply(
this,
value.split(spaces)
);
}
this._isSVG = isSVG;
this._ = node;
},
classListDescriptor = {
get: function get() {
return new DOMTokenList(this);
},
set: function(){}
},
trim = /^\s+|\s+$/g,
spaces = /\s+/,
SPACE = '\x20',
CLASS_LIST = 'classList',
toggle = function toggle(token, force) {
if (this.contains(token)) {
if (!force) {
// force is not true (either false or omitted)
this.remove(token);
}
} else if(force === undefined || force) {
force = true;
this.add(token);
}
return !!force;
},
DocumentFragmentPrototype = window.DocumentFragment && DocumentFragment.prototype,
Node = window.Node,
NodePrototype = (Node || Element).prototype,
CharacterData = window.CharacterData || Node,
CharacterDataPrototype = CharacterData && CharacterData.prototype,
DocumentType = window.DocumentType,
DocumentTypePrototype = DocumentType && DocumentType.prototype,
ElementPrototype = (window.Element || Node || window.HTMLElement).prototype,
HTMLSelectElement = window.HTMLSelectElement || createElement('select').constructor,
selectRemove = HTMLSelectElement.prototype.remove,
SVGElement = window.SVGElement,
properties = [
'matches', (
ElementPrototype.matchesSelector ||
ElementPrototype.webkitMatchesSelector ||
ElementPrototype.khtmlMatchesSelector ||
ElementPrototype.mozMatchesSelector ||
ElementPrototype.msMatchesSelector ||
ElementPrototype.oMatchesSelector ||
function matches(selector) {
var parentNode = this.parentNode;
return !!parentNode && -1 < indexOf.call(
parentNode.querySelectorAll(selector),
this
);
}
),
'closest', function closest(selector) {
var parentNode = this, matches;
while (
// document has no .matches
(matches = parentNode && parentNode.matches) &&
!parentNode.matches(selector)
) {
parentNode = parentNode.parentNode;
}
return matches ? parentNode : null;
},
'prepend', function prepend() {
var firstChild = this.firstChild,
node = mutationMacro(arguments);
if (firstChild) {
this.insertBefore(node, firstChild);
} else {
this.appendChild(node);
}
},
'append', function append() {
this.appendChild(mutationMacro(arguments));
},
'before', function before() {
var parentNode = this.parentNode;
if (parentNode) {
parentNode.insertBefore(
mutationMacro(arguments), this
);
}
},
'after', function after() {
var parentNode = this.parentNode,
nextSibling = this.nextSibling,
node = mutationMacro(arguments);
if (parentNode) {
if (nextSibling) {
parentNode.insertBefore(node, nextSibling);
} else {
parentNode.appendChild(node);
}
}
},
// https://dom.spec.whatwg.org/#dom-element-toggleattribute
'toggleAttribute', function toggleAttribute(name, force) {
var had = this.hasAttribute(name);
if (1 < arguments.length) {
if (had && !force)
this.removeAttribute(name);
else if (force && !had)
this.setAttribute(name, "");
}
else if (had)
this.removeAttribute(name);
else
this.setAttribute(name, "");
return this.hasAttribute(name);
},
// WARNING - DEPRECATED - use .replaceWith() instead
'replace', function replace() {
this.replaceWith.apply(this, arguments);
},
'replaceWith', function replaceWith() {
var parentNode = this.parentNode;
if (parentNode) {
parentNode.replaceChild(
mutationMacro(arguments),
this
);
}
},
'remove', function remove() {
var parentNode = this.parentNode;
if (parentNode) {
parentNode.removeChild(this);
}
}
],
slice = properties.slice,
i = properties.length; i; i -= 2
) {
property = properties[i - 2];
if (!(property in ElementPrototype)) {
ElementPrototype[property] = properties[i - 1];
}
// avoid unnecessary re-patch when the script is included
// gazillion times without any reason whatsoever
// https://github.com/WebReflection/dom4/pull/48
if (property === 'remove' && !selectRemove._dom4) {
// see https://github.com/WebReflection/dom4/issues/19
(HTMLSelectElement.prototype[property] = function () {
return 0 < arguments.length ?
selectRemove.apply(this, arguments) :
ElementPrototype.remove.call(this);
})._dom4 = true;
}
// see https://github.com/WebReflection/dom4/issues/18
if (/^(?:before|after|replace|replaceWith|remove)$/.test(property)) {
if (CharacterData && !(property in CharacterDataPrototype)) {
CharacterDataPrototype[property] = properties[i - 1];
}
if (DocumentType && !(property in DocumentTypePrototype)) {
DocumentTypePrototype[property] = properties[i - 1];
}
}
// see https://github.com/WebReflection/dom4/pull/26
if (/^(?:append|prepend)$/.test(property)) {
if (DocumentFragmentPrototype) {
if (!(property in DocumentFragmentPrototype)) {
DocumentFragmentPrototype[property] = properties[i - 1];
}
} else {
try {
createDocumentFragment().constructor.prototype[property] = properties[i - 1];
} catch(o_O) {}
}
}
}
// most likely an IE9 only issue
// see https://github.com/WebReflection/dom4/issues/6
if (!createElement('a').matches('a')) {
ElementPrototype[property] = function(matches){
return function (selector) {
return matches.call(
this.parentNode ?
this :
createDocumentFragment().appendChild(this),
selector
);
};
}(ElementPrototype[property]);
}
// used to fix both old webkit and SVG
DOMTokenList.prototype = {
length: 0,
add: function add() {
for(var j = 0, token; j < arguments.length; j++) {
token = arguments[j];
if(!this.contains(token)) {
properties.push.call(this, property);
}
}
if (this._isSVG) {
this._.setAttribute('class', '' + this);
} else {
this._.className = '' + this;
}
},
contains: (function(indexOf){
return function contains(token) {
i = indexOf.call(this, property = verifyToken(token));
return -1 < i;
};
}([].indexOf || function (token) {
i = this.length;
while(i-- && this[i] !== token){}
return i;
})),
item: function item(i) {
return this[i] || null;
},
remove: function remove() {
for(var j = 0, token; j < arguments.length; j++) {
token = arguments[j];
if(this.contains(token)) {
properties.splice.call(this, i, 1);
}
}
if (this._isSVG) {
this._.setAttribute('class', '' + this);
} else {
this._.className = '' + this;
}
},
toggle: toggle,
toString: function toString() {
return properties.join.call(this, SPACE);
}
};
if (SVGElement && !(CLASS_LIST in SVGElement.prototype)) {
defineProperty(SVGElement.prototype, CLASS_LIST, classListDescriptor);
}
// http://www.w3.org/TR/dom/#domtokenlist
// iOS 5.1 has completely screwed this property
// classList in ElementPrototype is false
// but it's actually there as getter
if (!(CLASS_LIST in document.documentElement)) {
defineProperty(ElementPrototype, CLASS_LIST, classListDescriptor);
} else {
// iOS 5.1 and Nokia ASHA do not support multiple add or remove
// trying to detect and fix that in here
TemporaryTokenList = createElement('div')[CLASS_LIST];
TemporaryTokenList.add('a', 'b', 'a');
if ('a\x20b' != TemporaryTokenList) {
// no other way to reach original methods in iOS 5.1
TemporaryPrototype = TemporaryTokenList.constructor.prototype;
if (!('add' in TemporaryPrototype)) {
// ASHA double fails in here
TemporaryPrototype = window.TemporaryTokenList.prototype;
}
wrapVerifyToken = function (original) {
return function () {
var i = 0;
while (i < arguments.length) {
original.call(this, arguments[i++]);
}
};
};
TemporaryPrototype.add = wrapVerifyToken(TemporaryPrototype.add);
TemporaryPrototype.remove = wrapVerifyToken(TemporaryPrototype.remove);
// toggle is broken too ^_^ ... let's fix it
TemporaryPrototype.toggle = toggle;
}
}
if (!('contains' in NodePrototype)) {
defineProperty(NodePrototype, 'contains', {
value: function (el) {
while (el && el !== this) el = el.parentNode;
return this === el;
}
});
}
if (!('head' in document)) {
defineProperty(document, 'head', {
get: function () {
return head || (
head = document.getElementsByTagName('head')[0]
);
}
});
}
// requestAnimationFrame partial polyfill
(function () {
for (var
raf,
rAF = window.requestAnimationFrame,
cAF = window.cancelAnimationFrame,
prefixes = ['o', 'ms', 'moz', 'webkit'],
i = prefixes.length;
!cAF && i--;
) {
rAF = rAF || window[prefixes[i] + 'RequestAnimationFrame'];
cAF = window[prefixes[i] + 'CancelAnimationFrame'] ||
window[prefixes[i] + 'CancelRequestAnimationFrame'];
}
if (!cAF) {
// some FF apparently implemented rAF but no cAF
if (rAF) {
raf = rAF;
rAF = function (callback) {
var goOn = true;
raf(function () {
if (goOn) callback.apply(this, arguments);
});
return function () {
goOn = false;
};
};
cAF = function (id) {
id();
};
} else {
rAF = function (callback) {
return setTimeout(callback, 15, 15);
};
cAF = function (id) {
clearTimeout(id);
};
}
}
window.requestAnimationFrame = rAF;
window.cancelAnimationFrame = cAF;
}());
// http://www.w3.org/TR/dom/#customevent
try{new window.CustomEvent('?');}catch(o_O){
window.CustomEvent = function(
eventName,
defaultInitDict
){
// the infamous substitute
function CustomEvent(type, eventInitDict) {
/*jshint eqnull:true */
var event = document.createEvent(eventName);
if (typeof type != 'string') {
throw new Error('An event name must be provided');
}
if (eventName == 'Event') {
event.initCustomEvent = initCustomEvent;
}
if (eventInitDict == null) {
eventInitDict = defaultInitDict;
}
event.initCustomEvent(
type,
eventInitDict.bubbles,
eventInitDict.cancelable,
eventInitDict.detail
);
return event;
}
// attached at runtime
function initCustomEvent(
type, bubbles, cancelable, detail
) {
/*jshint validthis:true*/
this.initEvent(type, bubbles, cancelable);
this.detail = detail;
}
// that's it
return CustomEvent;
}(
// is this IE9 or IE10 ?
// where CustomEvent is there
// but not usable as construtor ?
window.CustomEvent ?
// use the CustomEvent interface in such case
'CustomEvent' : 'Event',
// otherwise the common compatible one
{
bubbles: false,
cancelable: false,
detail: null
}
);
}
// window.Event as constructor
try { new Event('_'); } catch (o_O) {
/* jshint -W022 */
o_O = (function ($Event) {
function Event(type, init) {
enoughArguments(arguments.length, 'Event');
var out = document.createEvent('Event');
if (!init) init = {};
out.initEvent(
type,
!!init.bubbles,
!!init.cancelable
);
return out;
}
Event.prototype = $Event.prototype;
return Event;
}(window.Event || function Event() {}));
defineProperty(window, 'Event', {value: o_O});
// Android 4 gotcha
if (Event !== o_O) Event = o_O;
}
// window.KeyboardEvent as constructor
try { new KeyboardEvent('_', {}); } catch (o_O) {
/* jshint -W022 */
o_O = (function ($KeyboardEvent) {
// code inspired by https://gist.github.com/termi/4654819
var
initType = 0,
defaults = {
char: '',
key: '',
location: 0,
ctrlKey: false,
shiftKey: false,
altKey: false,
metaKey: false,
altGraphKey: false,
repeat: false,
locale: navigator.language,
detail: 0,
bubbles: false,
cancelable: false,
keyCode: 0,
charCode: 0,
which: 0
},
eventType
;
try {
var e = document.createEvent('KeyboardEvent');
e.initKeyboardEvent(
'keyup', false, false, window, '+', 3,
true, false, true, false, false
);
initType = (
(e.keyIdentifier || e.key) == '+' &&
(e.keyLocation || e.location) == 3
) && (
e.ctrlKey ? e.altKey ? 1 : 3 : e.shiftKey ? 2 : 4
) || 9;
} catch(o_O) {}
eventType = 0 < initType ? 'KeyboardEvent' : 'Event';
function getModifier(init) {
for (var
out = [],
keys = [
'ctrlKey',
'Control',
'shiftKey',
'Shift',
'altKey',
'Alt',
'metaKey',
'Meta',
'altGraphKey',
'AltGraph'
],
i = 0; i < keys.length; i += 2
) {
if (init[keys[i]])
out.push(keys[i + 1]);
}
return out.join(' ');
}
function withDefaults(target, source) {
for (var key in source) {
if (
source.hasOwnProperty(key) &&
!source.hasOwnProperty.call(target, key)
) target[key] = source[key];
}
return target;
}
function withInitValues(key, out, init) {
try {
out[key] = init[key];
} catch(o_O) {}
}
function KeyboardEvent(type, init) {
enoughArguments(arguments.length, 'KeyboardEvent');
init = withDefaults(init || {}, defaults);
var
out = document.createEvent(eventType),
ctrlKey = init.ctrlKey,
shiftKey = init.shiftKey,
altKey = init.altKey,
metaKey = init.metaKey,
altGraphKey = init.altGraphKey,
modifiers = initType > 3 ? getModifier(init) : null,
key = String(init.key),
chr = String(init.char),
location = init.location,
keyCode = init.keyCode || (
(init.keyCode = key) &&
key.charCodeAt(0)
) || 0,
charCode = init.charCode || (
(init.charCode = chr) &&
chr.charCodeAt(0)
) || 0,
bubbles = init.bubbles,
cancelable = init.cancelable,
repeat = init.repeat,
locale = init.locale,
view = init.view || window,
args
;
if (!init.which) init.which = init.keyCode;
if ('initKeyEvent' in out) {
out.initKeyEvent(
type, bubbles, cancelable, view,
ctrlKey, altKey, shiftKey, metaKey, keyCode, charCode
);
} else if (0 < initType && 'initKeyboardEvent' in out) {
args = [type, bubbles, cancelable, view];
switch (initType) {
case 1:
args.push(key, location, ctrlKey, shiftKey, altKey, metaKey, altGraphKey);
break;
case 2:
args.push(ctrlKey, altKey, shiftKey, metaKey, keyCode, charCode);
break;
case 3:
args.push(key, location, ctrlKey, altKey, shiftKey, metaKey, altGraphKey);
break;
case 4:
args.push(key, location, modifiers, repeat, locale);
break;
default:
args.push(char, key, location, modifiers, repeat, locale);
}
out.initKeyboardEvent.apply(out, args);
} else {
out.initEvent(type, bubbles, cancelable);
}
for (key in out) {
if (defaults.hasOwnProperty(key) && out[key] !== init[key]) {
withInitValues(key, out, init);
}
}
return out;
}
KeyboardEvent.prototype = $KeyboardEvent.prototype;
return KeyboardEvent;
}(window.KeyboardEvent || function KeyboardEvent() {}));
defineProperty(window, 'KeyboardEvent', {value: o_O});
// Android 4 gotcha
if (KeyboardEvent !== o_O) KeyboardEvent = o_O;
}
// window.MouseEvent as constructor
try { new MouseEvent('_', {}); } catch (o_O) {
/* jshint -W022 */
o_O = (function ($MouseEvent) {
function MouseEvent(type, init) {
enoughArguments(arguments.length, 'MouseEvent');
var out = document.createEvent('MouseEvent');
if (!init) init = {};
out.initMouseEvent(
type,
!!init.bubbles,
!!init.cancelable,
init.view || window,
init.detail || 1,
init.screenX || 0,
init.screenY || 0,
init.clientX || 0,
init.clientY || 0,
!!init.ctrlKey,
!!init.altKey,
!!init.shiftKey,
!!init.metaKey,
init.button || 0,
init.relatedTarget || null
);
return out;
}
MouseEvent.prototype = $MouseEvent.prototype;
return MouseEvent;
}(window.MouseEvent || function MouseEvent() {}));
defineProperty(window, 'MouseEvent', {value: o_O});
// Android 4 gotcha
if (MouseEvent !== o_O) MouseEvent = o_O;
}
if (!document.querySelectorAll('*').forEach) {
(function () {
function patch(what) {
var querySelectorAll = what.querySelectorAll;
what.querySelectorAll = function qSA(css) {
var result = querySelectorAll.call(this, css);
result.forEach = Array.prototype.forEach;
return result;
};
}
patch(document);
patch(Element.prototype);
}());
}
try {
// https://drafts.csswg.org/selectors-4/#the-scope-pseudo
document.querySelector(':scope *');
} catch(o_O) {
(function () {
var dataScope = 'data-scope-' + (Math.random() * 1e9 >>> 0);
var proto = Element.prototype;
var querySelector = proto.querySelector;
var querySelectorAll = proto.querySelectorAll;
proto.querySelector = function qS(css) {
return find(this, querySelector, css);
};
proto.querySelectorAll = function qSA(css) {
return find(this, querySelectorAll, css);
};
function find(node, method, css) {
if (node.type != document.ELEMENT_NODE) return method.call(node, css);
node.setAttribute(dataScope, null);
var result = method.call(
node,
String(css).replace(
/(^|,\s*)(:scope([ >]|$))/g,
function ($0, $1, $2, $3) {
return $1 + '[' + dataScope + ']' + ($3 || ' ');
}
)
);
node.removeAttribute(dataScope);
return result;
}
}());
}
}(window));