-
Notifications
You must be signed in to change notification settings - Fork 0
/
polyfill-svg-uri.js
272 lines (239 loc) · 10.2 KB
/
polyfill-svg-uri.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
/*
* polyfillSvgUri
* https://github.com/anseki/polyfill-svg-uri
*
* Copyright (c) 2018 anseki
* Licensed under the MIT license.
*/
;(function(global) { // eslint-disable-line no-extra-semi
'use strict';
function polyfillSvgUri() {
var IS_IE = !!document.uniqueID,
list2array = Function.prototype.call.bind(Array.prototype.slice),
reDataUri = new RegExp('^data:(.+?),(.*)$'), // Data URI scheme
// [^\1] isn't supported by JS
reUrlFunc = new RegExp('\\burl\\((?:(\'|")(.*?)\\1|(.*?))\\)', 'g');
function convertDataUri(uri) {
return (uri + '').replace(reDataUri, function(s, params, content) {
params = params.split(';');
if (params.indexOf('image/svg+xml') > -1 && params.indexOf('base64') === -1 &&
/^<svg\b/i.test(content)) { // Check more surely than `utf8` in params.
params = params.filter(
function(param) { return param !== 'utf8' && param !== 'charset=utf-8'; });
if (IS_IE) {
// Fragments must have already escaped. `%23` was replaced by `%2523`.
// content.replace(/%23/g, '#')
// But IE doesn't parse those. Therefore, Unescape all escaped characters.
content = encodeURIComponent(decodeURIComponent(content));
params.push('charset=utf-8');
} else {
// Firefox handles `#` as fragment.
content = content.replace(/#/g, '%23'); // escape #
params.push('utf8');
}
}
return 'data:' + params.join(';') + ',' + content;
});
}
function convertCssValue(value) {
return (value + '').replace(reUrlFunc, function(s, qt, val1, val2) {
qt = qt || '';
return 'url(' + qt + convertDataUri(val1 || val2) + qt + ')';
});
}
function parseStyle(style) {
var propName, value, newValue, i;
for (i = style.length - 1; i >= 0; i--) {
propName = style[i];
if ((value = style.getPropertyValue(propName)) &&
// Blink has a bug that break the style when some properties are updated.
// https://bugs.chromium.org/p/chromium/issues/detail?id=652362
(newValue = convertCssValue(value)) !== value) {
try {
style.setProperty(propName, newValue, style.getPropertyPriority(propName));
} catch (e) {
global.console.warn('Couldn\'t set property.', e, propName);
}
}
}
}
function parseRule(cssRule) {
/*
Normally, doesn't have multiple properties of following.
But unknown type should be covered. i.e. Don't use `else if`.
*/
// CSSStyleDeclaration
try {
if (cssRule.style) { parseStyle(cssRule.style); }
} catch (error) { /* ignore */ }
// CSSStyleSheet, CSSMediaRule, CSSKeyframesRule, etc.
try {
if (cssRule.cssRules) { list2array(cssRule.cssRules).forEach(parseRule); }
} catch (error) { /* ignore */ }
// CSSImportRule
try {
if (cssRule.styleSheet) { parseRule(cssRule.styleSheet); }
} catch (error) { /* ignore */ }
}
// ================================ Style Sheets
list2array(document.styleSheets).forEach(parseRule);
// ================================ Attributes
['src', 'data'].forEach(function(attrName) {
list2array(document.querySelectorAll('[' + attrName + '^="data:"]')).forEach(function(element) {
element[attrName] = convertDataUri(element[attrName]);
});
});
// ================================ Inline Style
list2array(document.querySelectorAll('[style]')).forEach(function(element) {
parseStyle(element.style);
});
// ================================ CSSStyleDeclaration
(function(styleDeclarationProto) {
var constructorName, targetProto, properties, checkProto;
if (!styleDeclarationProto) { return; }
// ------------------------ setProperty
(function(nativeMethod) {
if (!nativeMethod) { return; }
styleDeclarationProto.setProperty = function(property, value, priority) {
// IE needs priority as string
nativeMethod.call(this, property, convertCssValue(value), priority || '');
};
})(styleDeclarationProto.setProperty);
// ------------------------ properties
if ((constructorName = (Object.prototype.toString.call(document.body.style) || '')
.replace(/^\[object (.+?)\]$/, '$1'))) {
if (constructorName === 'CSS2Properties' ||
constructorName === 'CSS3Properties' ||
constructorName === 'CSSProperties') {
targetProto = global[constructorName].prototype;
properties = Object.keys(targetProto);
} else if (constructorName === 'MSStyleCSSProperties') {
targetProto = styleDeclarationProto;
properties = Object.keys(targetProto);
} else {
targetProto = styleDeclarationProto;
properties = Object.keys(targetProto);
if (properties.indexOf('color') === -1) {
properties = Object.keys(document.body.style);
checkProto = document.body.style;
if (properties.indexOf('color') === -1) {
properties = Object.keys(global.getComputedStyle(document.body, ''));
}
}
}
properties.forEach(function(property) {
var descriptor =
Object.getOwnPropertyDescriptor(checkProto || targetProto, property);
if (descriptor && descriptor.enumerable && (checkProto || descriptor.set)) {
if (checkProto) {
Object.defineProperty(targetProto, property, {
/* eslint-disable key-spacing */
configurable: true,
enumerable: true,
get: function() {
return targetProto.getPropertyValue.call(this, property);
},
set: function(value) {
// setProperty was already replaced.
targetProto.setProperty.call(this, property, value, '');
}
/* eslint-enable key-spacing */
});
} else {
(function(nativeMethod) {
Object.defineProperty(targetProto, property, {
/* eslint-disable key-spacing */
configurable: descriptor.configurable,
enumerable: descriptor.enumerable,
get: descriptor.get,
set: function(value) {
nativeMethod.call(this, convertCssValue(value));
}
/* eslint-enable key-spacing */
});
})(descriptor.set);
}
}
});
}
// ------------------------ cssText
if (!properties || properties.indexOf('cssText') === -1) {
(function(descriptor) {
if (!descriptor || !descriptor.set) { return; }
(function(nativeMethod) {
Object.defineProperty(styleDeclarationProto, 'cssText', {
/* eslint-disable key-spacing */
configurable: descriptor.configurable,
enumerable: descriptor.enumerable,
get: descriptor.get,
set: function(value) {
nativeMethod.call(this, convertCssValue(value));
}
/* eslint-enable key-spacing */
});
})(descriptor.set);
})(Object.getOwnPropertyDescriptor(styleDeclarationProto, 'cssText'));
}
})(global.CSSStyleDeclaration && global.CSSStyleDeclaration.prototype);
// ------------------------ CSSStyleSheet
(function(styleSheetProto) {
if (!styleSheetProto) { return; }
(function(nativeMethod) {
if (!nativeMethod) { return; }
styleSheetProto.insertRule = function(rule, index) {
nativeMethod.call(this, convertCssValue(rule), index);
};
})(styleSheetProto.insertRule);
(function(nativeMethod) {
if (!nativeMethod) { return; }
styleSheetProto.addRule = function(selector, style, index) {
nativeMethod.call(this, selector, convertCssValue(style), index);
};
})(styleSheetProto.addRule);
})(global.CSSStyleSheet && global.CSSStyleSheet.prototype);
// ------------------------ HTMLElement
(function(HTMLElement) {
if (!HTMLElement) { return; }
Object.getOwnPropertyNames(global).forEach(function(constructorName) {
var htmlElementProto;
try {
htmlElementProto = global[constructorName] && global[constructorName].prototype;
} catch (e) {
global.console.warn('Couldn\'t get prototype.', e, constructorName);
return;
}
if (!htmlElementProto ||
!HTMLElement.prototype.isPrototypeOf(htmlElementProto)) { return; }
/*
If the DOM prototype doesn't have these properties (e.g. old Chrome, etc.),
it can't set descriptor.
https://developers.google.com/web/updates/2015/04/DOM-attributes-now-on-the-prototype-chain
But, those may be able to parse SVG.
*/
['src', 'data'].forEach(function(propName) {
// if (!htmlElementProto.hasOwnProperty(propName)) { return; }
(function(descriptor) {
if (!descriptor || !descriptor.set) { return; }
(function(nativeMethod) {
Object.defineProperty(htmlElementProto, propName, {
/* eslint-disable key-spacing */
configurable: descriptor.configurable,
enumerable: descriptor.enumerable,
get: descriptor.get,
set: function(value) {
nativeMethod.call(this, convertDataUri(value));
}
/* eslint-enable key-spacing */
});
})(descriptor.set);
})(Object.getOwnPropertyDescriptor(htmlElementProto, propName));
});
});
})(global.HTMLElement);
}
if (document.readyState === 'complete') {
polyfillSvgUri();
} else {
document.addEventListener('DOMContentLoaded', polyfillSvgUri, false);
}
})(Function('return this')()); // eslint-disable-line no-new-func