This repository has been archived by the owner on Nov 21, 2024. It is now read-only.
forked from inexorabletash/polyfill
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdom.js
416 lines (382 loc) · 13.4 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
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
(function(global) {
if (!('window' in global && 'document' in global))
return;
//----------------------------------------------------------------------
//
// DOM
// https://dom.spec.whatwg.org/
//
//----------------------------------------------------------------------
// Document.querySelectorAll method
// http://ajaxian.com/archives/creating-a-queryselector-for-ie-that-runs-at-native-speed
// Needed for: IE7-
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;
};
}
// Document.querySelector method
// Needed for: IE7-
if (!document.querySelector) {
document.querySelector = function(selectors) {
var elements = document.querySelectorAll(selectors);
return (elements.length) ? elements[0] : null;
};
}
// Document.getElementsByClassName method
// Needed for: IE8-
if (!document.getElementsByClassName) {
document.getElementsByClassName = function(classNames) {
classNames = String(classNames).replace(/^|\s+/g, '.');
return document.querySelectorAll(classNames);
};
}
// Node interface constants
// Needed for: IE8-
global.Node = global.Node || function() { 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;
// DOMException constants
// Needed for: IE8-
global.DOMException = global.DOMException || function() { 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;
// Event and EventTargets interfaces
// Needed for: IE8
(function(){
if (!('Element' in global) || 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 (typeof listener !== 'function') return;
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 (typeof listener !== 'function') return;
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 interface and Element.classList / Element.relList
// Needed for: IE9-
// 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() {
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) {
underlying_string = removeTokenFromString(token, underlying_string);
});
o[p] = underlying_string;
} finally {
var length = split(o[p]).length;
if (this.length !== length) { this.length = length; }
}
}
},
toggle: {
value: function(token, force) {
try {
token = String(token);
if (token.length === 0) { throw SyntaxError(); }
if (/\s/.test(token)) { throw Error("InvalidCharacterError"); }
var tokens = split(o[p]),
index = tokens.indexOf(token);
if (index !== -1 && (!force || force === (void 0))) {
o[p] = removeTokenFromString(token, o[p]);
return false;
}
if (index !== -1 && force) {
return true;
}
var underlying_string = o[p];
if (underlying_string.length !== 0 && !/\s$/.test(underlying_string)) {
underlying_string += ' ';
}
underlying_string += token;
o[p] = underlying_string;
return true;
} finally {
var length = split(o[p]).length;
if (this.length !== length) { this.length = length; }
}
}
},
toString: {
value: function() {
return o[p];
}
}
});
if (!('length' in this)) {
// In case getters are not supported
this.length = split(o[p]).length;
} else {
// If they are, shim in index getters (up to 100)
for (var i = 0; i < 100; ++i) {
Object.defineProperty(this, String(i), {
get: (function(n) { return function() { return this.item(n); }; }(i))
});
}
}
}
function addToElementPrototype(p, f) {
if ('Element' in global && Element.prototype && Object.defineProperty) {
Object.defineProperty(Element.prototype, p, { get: f });
}
}
// HTML - https://html.spec.whatwg.org
// Element.classList
if ('classList' in document.createElement('span')) {
window.getClassList = function(elem) { return elem.classList; };
} else {
window.getClassList = function(elem) { return new DOMTokenListShim(elem, 'className'); };
addToElementPrototype('classList', function() { return new DOMTokenListShim(this, 'className'); } );
}
// HTML - https://html.spec.whatwg.org
// HTMLAnchorElement.relList
// HTMLLinkElement.relList
if ('relList' in document.createElement('link')) {
window.getRelList = function(elem) { return elem.relList; };
} else {
window.getRelList = function(elem) { return new DOMTokenListShim(elem, 'rel'); };
addToElementPrototype('relList', function() { return new DOMTokenListShim(this, 'rel'); } );
}
}());
}(this));