forked from inexorabletash/polyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyfill.js
1222 lines (1052 loc) · 36.9 KB
/
polyfill.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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//----------------------------------------------------------------------
//
// ECMAScript 5 Polyfills
//
//----------------------------------------------------------------------
//----------------------------------------------------------------------
// ES5 15.2 Object Objects
//----------------------------------------------------------------------
//
// ES5 15.2.3 Properties of the Object Constructor
//
// ES5 15.2.3.2 Object.getPrototypeOf ( O )
// From http://ejohn.org/blog/objectgetprototypeof/
// NOTE: won't work for typical function T() {}; T.prototype = {}; new T; case
// since the constructor property is destroyed.
if (!Object.getPrototypeOf) {
Object.getPrototypeOf = function (o) {
if (o !== Object(o)) { throw TypeError("Object.getPrototypeOf called on non-object"); }
return o.__proto__ || o.constructor.prototype || Object.prototype;
};
}
// // ES5 15.2.3.3 Object.getOwnPropertyDescriptor ( O, P )
// if (typeof Object.getOwnPropertyDescriptor !== "function") {
// Object.getOwnPropertyDescriptor = function (o, name) {
// if (o !== Object(o)) { throw TypeError(); }
// if (o.hasOwnProperty(name)) {
// return {
// value: o[name],
// enumerable: true,
// writable: true,
// configurable: true
// };
// }
// };
// }
// ES5 15.2.3.4 Object.getOwnPropertyNames ( O )
if (typeof Object.getOwnPropertyNames !== "function") {
Object.getOwnPropertyNames = function (o) {
if (o !== Object(o)) { throw TypeError("Object.getOwnPropertyNames called on non-object"); }
var props = [], p;
for (p in o) {
if (Object.prototype.hasOwnProperty.call(o, p)) {
props.push(p);
}
}
return props;
};
}
// ES5 15.2.3.5 Object.create ( O [, Properties] )
if (typeof Object.create !== "function") {
Object.create = function (prototype, properties) {
if (typeof prototype !== "object") { throw TypeError(); }
/** @constructor */
function Ctor() {}
Ctor.prototype = prototype;
var o = new Ctor();
if (prototype) { o.constructor = Ctor; }
if (properties !== undefined) {
if (properties !== Object(properties)) { throw TypeError(); }
Object.defineProperties(o, properties);
}
return o;
};
}
// ES 15.2.3.6 Object.defineProperty ( O, P, Attributes )
// Partial support for most common case - getters, setters, and values
(function() {
if (!Object.defineProperty ||
!(function () { try { Object.defineProperty({}, 'x', {}); return true; } catch (e) { return false; } } ())) {
var orig = Object.defineProperty;
Object.defineProperty = function (o, prop, desc) {
// In IE8 try built-in implementation for defining properties on DOM prototypes.
if (orig) { try { return orig(o, prop, desc); } catch (e) {} }
if (o !== Object(o)) { throw TypeError("Object.defineProperty called on non-object"); }
if (Object.prototype.__defineGetter__ && ('get' in desc)) {
Object.prototype.__defineGetter__.call(o, prop, desc.get);
}
if (Object.prototype.__defineSetter__ && ('set' in desc)) {
Object.prototype.__defineSetter__.call(o, prop, desc.set);
}
if ('value' in desc) {
o[prop] = desc.value;
}
return o;
};
}
}());
// ES 15.2.3.7 Object.defineProperties ( O, Properties )
if (typeof Object.defineProperties !== "function") {
Object.defineProperties = function (o, properties) {
if (o !== Object(o)) { throw TypeError("Object.defineProperties called on non-object"); }
var name;
for (name in properties) {
if (Object.prototype.hasOwnProperty.call(properties, name)) {
Object.defineProperty(o, name, properties[name]);
}
}
return o;
};
}
// ES5 15.2.3.14 Object.keys ( O )
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Object/keys
if (!Object.keys) {
Object.keys = function (o) {
if (o !== Object(o)) { throw TypeError('Object.keys called on non-object'); }
var ret = [], p;
for (p in o) {
if (Object.prototype.hasOwnProperty.call(o, p)) {
ret.push(p);
}
}
return ret;
};
}
//----------------------------------------------------------------------
// ES5 15.3 Function Objects
//----------------------------------------------------------------------
//
// ES5 15.3.4 Properties of the Function Prototype Object
//
// ES5 15.3.4.5 Function.prototype.bind ( thisArg [, arg1 [, arg2, ... ]] )
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function/bind
if (!Function.prototype.bind) {
Function.prototype.bind = function (o) {
if (typeof this !== 'function') { throw TypeError("Bind must be called on a function"); }
var slice = [].slice,
args = slice.call(arguments, 1),
self = this,
bound = function () {
return self.apply(this instanceof nop ? this : (o || {}),
args.concat(slice.call(arguments)));
};
/** @constructor */
function nop() {}
nop.prototype = self.prototype;
bound.prototype = new nop();
return bound;
};
}
//----------------------------------------------------------------------
// ES5 15.4 Array Objects
//----------------------------------------------------------------------
//
// ES5 15.4.3 Properties of the Array Constructor
//
// ES5 15.4.3.2 Array.isArray ( arg )
// https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/isArray
Array.isArray = Array.isArray || function (o) { return Boolean(o && Object.prototype.toString.call(Object(o)) === '[object Array]'); };
//
// ES5 15.4.4 Properties of the Array Prototype Object
//
// ES5 15.4.4.14 Array.prototype.indexOf ( searchElement [ , fromIndex ] )
// From https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/indexOf
if (!Array.prototype.indexOf) {
Array.prototype.indexOf = function (searchElement /*, fromIndex */) {
if (this === void 0 || this === null) { throw TypeError(); }
var t = Object(this);
var len = t.length >>> 0;
if (len === 0) { return -1; }
var n = 0;
if (arguments.length > 0) {
n = Number(arguments[1]);
if (isNaN(n)) {
n = 0;
} else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
}
if (n >= len) { return -1; }
var k = n >= 0 ? n : Math.max(len - Math.abs(n), 0);
for (; k < len; k++) {
if (k in t && t[k] === searchElement) {
return k;
}
}
return -1;
};
}
// ES5 15.4.4.15 Array.prototype.lastIndexOf ( searchElement [ , fromIndex ] )
// From https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/lastIndexOf
if (!Array.prototype.lastIndexOf) {
Array.prototype.lastIndexOf = function (searchElement /*, fromIndex*/) {
if (this === void 0 || this === null) { throw TypeError(); }
var t = Object(this);
var len = t.length >>> 0;
if (len === 0) { return -1; }
var n = len;
if (arguments.length > 1) {
n = Number(arguments[1]);
if (n !== n) {
n = 0;
} else if (n !== 0 && n !== (1 / 0) && n !== -(1 / 0)) {
n = (n > 0 || -1) * Math.floor(Math.abs(n));
}
}
var k = n >= 0 ? Math.min(n, len - 1) : len - Math.abs(n);
for (; k >= 0; k--) {
if (k in t && t[k] === searchElement) {
return k;
}
}
return -1;
};
}
// ES5 15.4.4.16 Array.prototype.every ( callbackfn [ , thisArg ] )
// From https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/every
if (!Array.prototype.every) {
Array.prototype.every = function (fun /*, thisp */) {
if (this === void 0 || this === null) { throw TypeError(); }
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function") { throw TypeError(); }
var thisp = arguments[1], i;
for (i = 0; i < len; i++) {
if (i in t && !fun.call(thisp, t[i], i, t)) {
return false;
}
}
return true;
};
}
// ES5 15.4.4.17 Array.prototype.some ( callbackfn [ , thisArg ] )
// From https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/some
if (!Array.prototype.some) {
Array.prototype.some = function (fun /*, thisp */) {
if (this === void 0 || this === null) { throw TypeError(); }
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function") { throw TypeError(); }
var thisp = arguments[1], i;
for (i = 0; i < len; i++) {
if (i in t && fun.call(thisp, t[i], i, t)) {
return true;
}
}
return false;
};
}
// ES5 15.4.4.18 Array.prototype.forEach ( callbackfn [ , thisArg ] )
// From https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/forEach
if (!Array.prototype.forEach) {
Array.prototype.forEach = function (fun /*, thisp */) {
if (this === void 0 || this === null) { throw TypeError(); }
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function") { throw TypeError(); }
var thisp = arguments[1], i;
for (i = 0; i < len; i++) {
if (i in t) {
fun.call(thisp, t[i], i, t);
}
}
};
}
// ES5 15.4.4.19 Array.prototype.map ( callbackfn [ , thisArg ] )
// From https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Map
if (!Array.prototype.map) {
Array.prototype.map = function (fun /*, thisp */) {
if (this === void 0 || this === null) { throw TypeError(); }
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function") { throw TypeError(); }
var res = []; res.length = len;
var thisp = arguments[1], i;
for (i = 0; i < len; i++) {
if (i in t) {
res[i] = fun.call(thisp, t[i], i, t);
}
}
return res;
};
}
// ES5 15.4.4.20 Array.prototype.filter ( callbackfn [ , thisArg ] )
// From https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Filter
if (!Array.prototype.filter) {
Array.prototype.filter = function (fun /*, thisp */) {
if (this === void 0 || this === null) { throw TypeError(); }
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function") { throw TypeError(); }
var res = [];
var thisp = arguments[1], i;
for (i = 0; i < len; i++) {
if (i in t) {
var val = t[i]; // in case fun mutates this
if (fun.call(thisp, val, i, t)) {
res.push(val);
}
}
}
return res;
};
}
// ES5 15.4.4.21 Array.prototype.reduce ( callbackfn [ , initialValue ] )
// From https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/Reduce
if (!Array.prototype.reduce) {
Array.prototype.reduce = function (fun /*, initialValue */) {
if (this === void 0 || this === null) { throw TypeError(); }
var t = Object(this);
var len = t.length >>> 0;
if (typeof fun !== "function") { throw TypeError(); }
// no value to return if no initial value and an empty array
if (len === 0 && arguments.length === 1) { throw TypeError(); }
var k = 0;
var accumulator;
if (arguments.length >= 2) {
accumulator = arguments[1];
} else {
do {
if (k in t) {
accumulator = t[k++];
break;
}
// if array contains no values, no initial value to return
if (++k >= len) { throw TypeError(); }
}
while (true);
}
while (k < len) {
if (k in t) {
accumulator = fun.call(undefined, accumulator, t[k], k, t);
}
k++;
}
return accumulator;
};
}
// ES5 15.4.4.22 Array.prototype.reduceRight ( callbackfn [, initialValue ] )
// From https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Array/ReduceRight
if (!Array.prototype.reduceRight) {
Array.prototype.reduceRight = function (callbackfn /*, initialValue */) {
if (this === void 0 || this === null) { throw TypeError(); }
var t = Object(this);
var len = t.length >>> 0;
if (typeof callbackfn !== "function") { throw TypeError(); }
// no value to return if no initial value, empty array
if (len === 0 && arguments.length === 1) { throw TypeError(); }
var k = len - 1;
var accumulator;
if (arguments.length >= 2) {
accumulator = arguments[1];
} else {
do {
if (k in this) {
accumulator = this[k--];
break;
}
// if array contains no values, no initial value to return
if (--k < 0) { throw TypeError(); }
}
while (true);
}
while (k >= 0) {
if (k in t) {
accumulator = callbackfn.call(undefined, accumulator, t[k], k, t);
}
k--;
}
return accumulator;
};
}
//----------------------------------------------------------------------
// ES5 15.5 String Objects
//----------------------------------------------------------------------
//
// ES5 15.5.4 Properties of the String Prototype Object
//
// ES5 15.5.4.20 String.prototype.trim()
if (!String.prototype.trim) {
String.prototype.trim = function () {
return String(this).replace(/^\s+/, '').replace(/\s+$/, '');
};
}
//----------------------------------------------------------------------
// ES5 15.9 Date Objects
//----------------------------------------------------------------------
//
// ES 15.9.4 Properties of the Date Constructor
//
// ES5 15.9.4.4 Date.now ( )
// From https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Date/now
if (!Date.now) {
Date.now = function now() {
return Number(new Date());
};
}
//
// ES5 15.9.5 Properties of the Date Prototype Object
//
// ES5 15.9.4.43 Date.prototype.toISOString ( )
// Inspired by http://www.json.org/json2.js
if (!Date.prototype.toISOString) {
Date.prototype.toISOString = function () {
function pad2(n) { return ('00' + n).slice(-2); }
function pad3(n) { return ('000' + n).slice(-3); }
return this.getUTCFullYear() + '-' +
pad2(this.getUTCMonth() + 1) + '-' +
pad2(this.getUTCDate()) + 'T' +
pad2(this.getUTCHours()) + ':' +
pad2(this.getUTCMinutes()) + ':' +
pad2(this.getUTCSeconds()) + '.' +
pad3(this.getUTCMilliseconds()) + 'Z';
};
}
//----------------------------------------------------------------------
//
// Browser Polyfills
//
// This assumes ES5 or ES3 + es5.js
// (polyfill.js is es5.js + web.js for convenience)
//
//----------------------------------------------------------------------
if ('window' in this && 'document' in this) {
//----------------------------------------------------------------------
//
// Web Standards Polyfills
//
//----------------------------------------------------------------------
//
// document.head (HTML5)
//
document.head = document.head || document.getElementsByTagName('head')[0];
//
// XMLHttpRequest (http://www.w3.org/TR/XMLHttpRequest/)
//
window.XMLHttpRequest = window.XMLHttpRequest || function () {
/*global ActiveXObject*/
try { return new ActiveXObject("Msxml2.XMLHTTP.6.0"); } catch (e1) { }
try { return new ActiveXObject("Msxml2.XMLHTTP.3.0"); } catch (e2) { }
try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e3) { }
throw Error("This browser does not support XMLHttpRequest.");
};
XMLHttpRequest.UNSENT = 0;
XMLHttpRequest.OPENED = 1;
XMLHttpRequest.HEADERS_RECEIVED = 2;
XMLHttpRequest.LOADING = 3;
XMLHttpRequest.DONE = 4;
//
// FormData (http://www.w3.org/TR/XMLHttpRequest2/#interface-formdata)
//
if (!('FormData' in window)) {
(function(global) {
function FormData(form) {
this._data = [];
if (!form) return;
for (var i = 0; i < form.elements.length; ++i)
this.append(form.elements[i].name, form.elements[i].value);
}
FormData.prototype.append = function(name, value /*, filename */) {
if ('Blob' in global && value instanceof global.Blob) throw TypeError("Blob not supported");
name = String(name);
this._data.push([name, value]);
};
FormData.prototype.toString = function() {
return this._data.map(function(pair) {
return encodeURIComponent(pair[0]) + '=' + encodeURIComponent(pair[1]);
}).join('&');
};
global.FormData = FormData;
var send = global.XMLHttpRequest.prototype.send;
global.XMLHttpRequest.prototype.send = function(body) {
if (body instanceof FormData) {
this.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
arguments[0] = body.toString();
}
return send.apply(this, arguments);
};
}(this));
}
//----------------------------------------------------------------------
//
// Performance
//
//----------------------------------------------------------------------
// requestAnimationFrame
// http://www.w3.org/TR/animation-timing/
(function(global) {
var TARGET_FPS = 60,
requests = Object.create(null),
raf_handle = 1,
timeout_handle = -1;
function isVisible(element) {
return element.offsetWidth > 0 && element.offsetHeight > 0;
}
function onFrameTimer() {
var cur_requests = requests;
requests = Object.create(null);
timeout_handle = -1;
Object.keys(cur_requests).forEach(function(id) {
var request = cur_requests[id];
if (!request.element || isVisible(request.element)) {
request.callback(Date.now());
}
});
}
function requestAnimationFrame(callback, element) {
var cb_handle = raf_handle++;
requests[cb_handle] = {callback: callback, element: element};
if (timeout_handle === -1) {
timeout_handle = global.setTimeout(onFrameTimer, 1000 / TARGET_FPS);
}
return cb_handle;
}
function cancelAnimationFrame(handle) {
delete requests[handle];
if (Object.keys(requests).length === 0) {
global.clearTimeout(timeout_handle);
timeout_handle = -1;
}
}
global.requestAnimationFrame =
global.requestAnimationFrame ||
global.webkitRequestAnimationFrame ||
global.mozRequestAnimationFrame ||
global.oRequestAnimationFrame ||
global.msRequestAnimationFrame ||
requestAnimationFrame;
// NOTE: Older versions of the spec called this "cancelRequestAnimationFrame"
global.cancelAnimationFrame = global.cancelRequestAnimationFrame =
global.cancelAnimationFrame || global.cancelRequestAnimationFrame ||
global.webkitCancelAnimationFrame || global.webkitCancelRequestAnimationFrame ||
global.mozCancelAnimationFrame || global.mozCancelRequestAnimationFrame ||
global.oCancelAnimationFrame || global.oCancelRequestAnimationFrame ||
global.msCancelAnimationFrame || global.msCancelRequestAnimationFrame ||
cancelAnimationFrame;
}(this));
// setImmediate
// https://dvcs.w3.org/hg/webperf/raw-file/tip/specs/setImmediate/Overview.html
(function (global) {
function setImmediate(callback/*, args*/) {
var params = [].slice.call(arguments, 1);
return global.setTimeout(function() {
callback.apply(null, params);
}, 0);
}
function clearImmediate(handle) {
global.clearTimeout(handle);
}
global.setImmediate =
global.setImmediate ||
global.msSetImmediate ||
setImmediate;
global.clearImmediate =
global.clearImmediate ||
global.msClearImmediate ||
clearImmediate;
}(this));
//----------------------------------------------------------------------
//
// DOM
//
//----------------------------------------------------------------------
//
// Selectors API Level 1 (http://www.w3.org/TR/selectors-api/)
// http://ajaxian.com/archives/creating-a-queryselector-for-ie-that-runs-at-native-speed
//
if (!document.querySelectorAll) {
document.querySelectorAll = function (selectors) {
var style = document.createElement('style'), elements = [], element;
document.documentElement.firstChild.appendChild(style);
document._qsa = [];
style.styleSheet.cssText = selectors + '{x-qsa:expression(document._qsa && document._qsa.push(this))}';
window.scrollBy(0, 0);
style.parentNode.removeChild(style);
while (document._qsa.length) {
element = document._qsa.shift();
element.style.removeAttribute('x-qsa');
elements.push(element);
}
document._qsa = null;
return elements;
};
}
if (!document.querySelector) {
document.querySelector = function (selectors) {
var elements = document.querySelectorAll(selectors);
return (elements.length) ? elements[0] : null;
};
}
if (!document.getElementsByClassName) {
document.getElementsByClassName = function (classNames) {
classNames = String(classNames).replace(/^|\s+/g, '.');
return document.querySelectorAll(classNames);
};
}
// Fix for IE8-'s Element.getBoundingClientRect()
if ('TextRectangle' in this && !('width' in TextRectangle.prototype)) {
Object.defineProperties(TextRectangle.prototype, {
'width': { get: function() { return this.right - this.left; } },
'height': { get: function() { return this.bottom - this.top; } }
});
}
//
// DOM Enumerations (http://www.w3.org/TR/DOM-Level-2-Core/)
//
window.Node = window.Node || function Node() { throw TypeError("Illegal constructor"); };
Node.ELEMENT_NODE = 1;
Node.ATTRIBUTE_NODE = 2;
Node.TEXT_NODE = 3;
Node.CDATA_SECTION_NODE = 4;
Node.ENTITY_REFERENCE_NODE = 5;
Node.ENTITY_NODE = 6;
Node.PROCESSING_INSTRUCTION_NODE = 7;
Node.COMMENT_NODE = 8;
Node.DOCUMENT_NODE = 9;
Node.DOCUMENT_TYPE_NODE = 10;
Node.DOCUMENT_FRAGMENT_NODE = 11;
Node.NOTATION_NODE = 12;
window.DOMException = window.DOMException || function DOMException() { throw TypeError("Illegal constructor"); };
DOMException.INDEX_SIZE_ERR = 1;
DOMException.DOMSTRING_SIZE_ERR = 2;
DOMException.HIERARCHY_REQUEST_ERR = 3;
DOMException.WRONG_DOCUMENT_ERR = 4;
DOMException.INVALID_CHARACTER_ERR = 5;
DOMException.NO_DATA_ALLOWED_ERR = 6;
DOMException.NO_MODIFICATION_ALLOWED_ERR = 7;
DOMException.NOT_FOUND_ERR = 8;
DOMException.NOT_SUPPORTED_ERR = 9;
DOMException.INUSE_ATTRIBUTE_ERR = 10;
DOMException.INVALID_STATE_ERR = 11;
DOMException.SYNTAX_ERR = 12;
DOMException.INVALID_MODIFICATION_ERR = 13;
DOMException.NAMESPACE_ERR = 14;
DOMException.INVALID_ACCESS_ERR = 15;
//
// Events and EventTargets
//
(function(){
if (!('Element' in window) || Element.prototype.addEventListener || !Object.defineProperty)
return;
// interface Event
// PhaseType (const unsigned short)
Event.CAPTURING_PHASE = 1;
Event.AT_TARGET = 2;
Event.BUBBLING_PHASE = 3;
Object.defineProperties(Event.prototype, {
CAPTURING_PHASE: { get: function() { return 1; } },
AT_TARGET: { get: function() { return 2; } },
BUBBLING_PHASE: { get: function() { return 3; } },
target: {
get: function() {
return this.srcElement;
}},
currentTarget: {
get: function() {
return this._currentTarget;
}},
eventPhase: {
get: function() {
return (this.srcElement === this.currentTarget) ? Event.AT_TARGET : Event.BUBBLING_PHASE;
}},
bubbles: {
get: function() {
switch (this.type) {
// Mouse
case 'click':
case 'dblclick':
case 'mousedown':
case 'mouseup':
case 'mouseover':
case 'mousemove':
case 'mouseout':
case 'mousewheel':
// Keyboard
case 'keydown':
case 'keypress':
case 'keyup':
// Frame/Object
case 'resize':
case 'scroll':
// Form
case 'select':
case 'change':
case 'submit':
case 'reset':
return true;
}
return false;
}},
cancelable: {
get: function() {
switch (this.type) {
// Mouse
case 'click':
case 'dblclick':
case 'mousedown':
case 'mouseup':
case 'mouseover':
case 'mouseout':
case 'mousewheel':
// Keyboard
case 'keydown':
case 'keypress':
case 'keyup':
// Form
case 'submit':
return true;
}
return false;
}},
timeStamp: {
get: function() {
return this._timeStamp;
}},
stopPropagation: {
value: function() {
this.cancelBubble = true;
}},
preventDefault: {
value: function() {
this.returnValue = false;
}},
defaultPrevented: {
get: function() {
return this.returnValue === false;
}}
});
// interface EventTarget
function addEventListener(type, listener, useCapture) {
if (type === 'DOMContentLoaded') type = 'load';
var target = this;
var f = function(e) {
e._timeStamp = Date.now();
e._currentTarget = target;
listener.call(this, e);
e._currentTarget = null;
};
this['_' + type + listener] = f;
this.attachEvent('on' + type, f);
}
function removeEventListener(type, listener, useCapture) {
if (type === 'DOMContentLoaded') type = 'load';
var f = this['_' + type + listener];
if (f) {
this.detachEvent('on' + type, f);
this['_' + type + listener] = null;
}
}
[Window, HTMLDocument, Element].forEach(function(o) {
o.prototype.addEventListener = addEventListener;
o.prototype.removeEventListener = removeEventListener;
});
}());
// Shim for DOM Events for IE7-
// http://www.quirksmode.org/blog/archives/2005/10/_and_the_winner_1.html
// Use addEvent(object, event, handler) instead of object.addEventListener(event, handler)
window.addEvent = function (obj, type, fn) {
if (obj.addEventListener) {
obj.addEventListener(type, fn, false);
} else if (obj.attachEvent) {
obj["e" + type + fn] = fn;
obj[type + fn] = function () {
var e = window.event;
e.currentTarget = obj;
e.preventDefault = function () { e.returnValue = false; };
e.stopPropagation = function () { e.cancelBubble = true; };
e.target = e.srcElement;
e.timeStamp = Date.now();
obj["e" + type + fn].call(this, e);
};
obj.attachEvent("on" + type, obj[type + fn]);
}
};
window.removeEvent = function (obj, type, fn) {
if (obj.removeEventListener) {
obj.removeEventListener(type, fn, false);
} else if (obj.detachEvent) {
obj.detachEvent("on" + type, obj[type + fn]);
obj[type + fn] = null;
obj["e" + type + fn] = null;
}
};
//----------------------------------------------------------------------
//
// DOMTokenList - classList and relList shims
//
//----------------------------------------------------------------------
// Shim for http://www.whatwg.org/specs/web-apps/current-work/multipage/elements.html#dom-classlist
// Use getClassList(elem) instead of elem.classList() if IE7- support is needed
// Use getRelList(elem) instead of elem.relList() if IE7- support is needed
(function () {
/** @constructor */
function DOMTokenListShim(o, p) {
function split(s) { return s.length ? s.split(/\s+/g) : []; }
// NOTE: This does not exactly match the spec.
function removeTokenFromString(token, string) {
var tokens = split(string),
index = tokens.indexOf(token);
if (index !== -1) {
tokens.splice(index, 1);
}
return tokens.join(' ');
}
Object.defineProperties(
this,
{
length: {
get: function () { return split(o[p]).length; }
},
item: {
value: function (idx) {
var tokens = split(o[p]);
return 0 <= idx && idx < tokens.length ? tokens[idx] : null;
}
},
contains: {
value: function (token) {
token = String(token);
if (token.length === 0) { throw SyntaxError(); }
if (/\s/.test(token)) { throw Error("InvalidCharacterError"); }
var tokens = split(o[p]);
return tokens.indexOf(token) !== -1;
}
},
add: {
value: function (/*tokens...*/) {
var tokens = Array.prototype.slice.call(arguments).map(String);
if (tokens.some(function(token) { return token.length === 0; })) {
throw SyntaxError();
}
if (tokens.some(function(token) { return (/\s/).test(token); })) {
throw Error("InvalidCharacterError");
}
try {
var underlying_string = o[p];
var token_list = split(underlying_string);
tokens = tokens.filter(function(token) { return token_list.indexOf(token) === -1; });
if (tokens.length === 0) {
return;
}
if (underlying_string.length !== 0 && !(/\s$/).test(underlying_string)) {
underlying_string += ' ';
}
underlying_string += tokens.join(' ');
o[p] = underlying_string;
} finally {
var length = split(o[p]).length;
if (this.length !== length) { this.length = length; }
}
}
},
remove: {
value: function (/*tokens...*/) {
var tokens = Array.prototype.slice.call(arguments).map(String);
if (tokens.some(function(token) { return token.length === 0; })) {
throw SyntaxError();
}
if (tokens.some(function(token) { return (/\s/).test(token); })) {
throw Error("InvalidCharacterError");
}
try {
var underlying_string = o[p];
tokens.forEach(function(token) {