/
IEService.ts
95 lines (84 loc) · 3.17 KB
/
IEService.ts
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
import EdgeService from './EdgeService';
import 'innersvg-polyfill';
import cssVars from 'css-vars-ponyfill';
const classListToggle = function (name: string, flag = !this.contains(name)) {
return flag ? (this.add(name), true) : (this.remove(name), false);
};
type classList = {
add: Function,
remove: Function,
contains: Function,
toggle?: Function
};
export default class IEService extends EdgeService {
static initialize() {
DOMTokenList.prototype.toggle = function(name, flag = !this.contains(name)) {
return flag ? (this.add(name), true) : (this.remove(name), false);
};
const originAdd = DOMTokenList.prototype.add;
DOMTokenList.prototype.add = function() {
[...arguments].map(name => originAdd.call(this, name));
};
const oldRemove = DOMTokenList.prototype.remove;
DOMTokenList.prototype.remove = function() {
[...arguments].map(name => oldRemove.call(this, name));
};
super.initialize();
this.__addCssVariables();
this.__addDOMClassListToggle();
this.__addSVGClassList();
this.__addChildNodeRemove();
}
static __addCssVariables() {
cssVars({
onlyLegacy: true
});
}
static __addDOMClassListToggle() {
DOMTokenList.prototype.toggle = classListToggle;
}
static __addSVGClassList() {
if ('classList' in SVGElement.prototype) {
return;
}
Object.defineProperty(SVGElement.prototype, 'classList', {
get() {
const classList: classList = {
contains: (className: string) => this.className.baseVal.split(' ').includes(className),
add: (className: string) => {
this.setAttribute('class', `${this.getAttribute('class')} ${className}`);
},
remove: (className: string) => {
if (!this.classList.contains(className)) {
return;
}
const removedClass = this.getAttribute('class').replace(new RegExp(`(\\s|^)${className}(\\s|$)`, 'g'), '$2');
this.setAttribute('class', removedClass);
}
};
classList.toggle = classListToggle.bind(classList);
return classList;
}
});
}
static __addChildNodeRemove() {
(function (prototypes) {
prototypes.forEach(function (prototype) {
if (prototype.hasOwnProperty('remove')) {
return;
}
Object.defineProperty(prototype, 'remove', {
configurable: true,
enumerable: true,
writable: true,
value: function remove() {
if (this.parentNode === null) {
return;
}
this.parentNode.removeChild(this);
}
});
});
})([Element.prototype, CharacterData.prototype, DocumentType.prototype]);
}
}