forked from javascript-tutorial/server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataset.js
More file actions
executable file
·71 lines (70 loc) · 2.46 KB
/
dataset.js
File metadata and controls
executable file
·71 lines (70 loc) · 2.46 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
// dataset for IE10
if (!document.documentElement.dataset &&
// FF is empty while IE gives empty object
(!Object.getOwnPropertyDescriptor(Element.prototype, 'dataset') ||
!Object.getOwnPropertyDescriptor(Element.prototype, 'dataset').get)
) {
let propDescriptor = {
enumerable: true,
get: function () {
'use strict';
let i,
that = this,
HTML5_DOMStringMap,
attrVal, attrName, propName,
attribute,
attributes = this.attributes,
attsLength = attributes.length,
toUpperCase = function (n0) {
return n0.charAt(1).toUpperCase();
},
getter = function () {
return this;
},
setter = function (attrName, value) {
return (typeof value !== 'undefined') ?
this.setAttribute(attrName, value) :
this.removeAttribute(attrName);
};
try { // Simulate DOMStringMap w/accessor support
// Test setting accessor on normal object
({}).__defineGetter__('test', function () {});
HTML5_DOMStringMap = {};
}
catch (e1) { // Use a DOM object for IE8
HTML5_DOMStringMap = document.createElement('div');
}
for (i = 0; i < attsLength; i++) {
attribute = attributes[i];
// Fix: This test really should allow any XML Name without
// colons (and non-uppercase for XHTML)
if (attribute && attribute.name &&
(/^data-\w[\w\-]*$/).test(attribute.name)) {
attrVal = attribute.value;
attrName = attribute.name;
// Change to CamelCase
propName = attrName.substr(5).replace(/-./g, toUpperCase);
try {
Object.defineProperty(HTML5_DOMStringMap, propName, {
enumerable: this.enumerable,
get: getter.bind(attrVal || ''),
set: setter.bind(that, attrName)
});
}
catch (e2) { // if accessors are not working
HTML5_DOMStringMap[propName] = attrVal;
}
}
}
return HTML5_DOMStringMap;
}
};
try {
// FF enumerates over element's dataset, but not
// Element.prototype.dataset; IE9 iterates over both
Object.defineProperty(Element.prototype, 'dataset', propDescriptor);
} catch (e) {
propDescriptor.enumerable = false; // IE8 does not allow setting to true
Object.defineProperty(Element.prototype, 'dataset', propDescriptor);
}
}