-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathimage-set-polyfill.js
225 lines (197 loc) · 6.3 KB
/
image-set-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
(function (window, document) {
var EMPTY = 'data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==',
IMAGE_SET_RE_TEPMLATE = 'url\\([\'"]?([^\'"\\)]+)[\'"]?\\)\\s*([\\d\\.]+)x?',
GET_IMAGE_SET_RE = new RegExp(IMAGE_SET_RE_TEPMLATE, 'g'),
GET_IMAGE_SET_IMAGE_URL_RE = new RegExp(IMAGE_SET_RE_TEPMLATE, ''),
forEach = Array.prototype.forEach,
devicePixelRatio = window.devicePixelRatio || 1;
/**
* image-set() feature detect
*
* @returns {Boolean}
*/
function testImageSet() {
var elem = document.createElement('div'),
style = [
'background-image: -webkit-image-set(url(' + EMPTY + ') 1.0x)',
'background-image: image-set(url(' + EMPTY + ') 1.0x)'
].join(';'),
support;
elem.setAttribute('style', style);
document.body.appendChild(elem);
support = GET_IMAGE_SET_RE.test(window.getComputedStyle(elem).background);
elem.parentNode.removeChild(elem);
return support;
}
/**
* Gerring background from inline styles
*
* @returns {String}
*/
function getBackground(elem) {
var style = elem.getAttribute('style'),
bgs;
if (!style) {
return '';
}
bgs = style.split(/\s*;\s*/).filter(function (s) {
return s.indexOf('background') !== -1;
});
if (bgs.length) {
return bgs.pop();
}
return '';
}
/**
* Getting best image for current DPI
*
* @param {Array} array of devicePixelRatio - image url pairs
* [[1, 'url1'], [2, 'url2']]
*
* @returns {String} 'url(url1)'
*/
function getBestSize(sizes) {
var key, set;
for (key in sizes) {
if (sizes.hasOwnProperty(key)) {
set = sizes[key];
if (set[0] >= devicePixelRatio) {
break;
}
}
}
return 'url(' + set[1] + ')';
}
/**
* Trying polyfill DOM node
*
* @param {HTMLElement}
*/
function polyfillNode(elem) {
var bg = getBackground(elem),
match = bg.match(GET_IMAGE_SET_RE),
sizes = [],
best;
GET_IMAGE_SET_RE.lastIndex = 0;
while ((match = GET_IMAGE_SET_RE.exec(bg))) {
sizes.push([
Number(match[2]),
match[1]
]);
}
if (!sizes.length) {
return;
}
best = getBestSize(sizes);
elem.style.backgroundImage = best;
}
/**
* Making polyfilled styles based on non-polyfilled
*
* @param {String} styles
*
* @return {String} new styles
*/
function getPolyfilledStyles(styles) {
var findImageSetRE = /(?:\-(?:webkit|moz)\-)?image\-set\(([^;}]*)\)/mg,
newStyles = '',
lastIndex = 0,
match, urls, best;
function urlMap(item) {
var m = item.match(GET_IMAGE_SET_IMAGE_URL_RE);
return [
Number(m[2]),
m[1]
];
}
while ((match = findImageSetRE.exec(styles))) {
urls = match[1].split(/\s*,\s*/).map(urlMap);
best = getBestSize(urls);
newStyles += styles.slice(lastIndex, match.index) + best;
lastIndex = match.index + match[0].length;
}
newStyles += styles.slice(lastIndex);
return newStyles;
}
/**
* Trying modify current styles
*
* @param {HTMLStyleElement}
*/
function polyfillStyle(styleTag) {
styleTag.innerHTML = getPolyfilledStyles(styleTag.innerHTML);
}
/**
* Trying polyfill link[rel="stylesheet"] tag
*
* @param {HTMLLinkElement}
*/
function polifillLink(link) {
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function () {
var styleText, styleTag;
if (xhr.readyState === 4) {
styleText = getPolyfilledStyles(xhr.responseText);
styleTag = document.createElement('style');
styleTag.innerHTML = styleText;
link.parentNode.replaceChild(styleTag, link);
}
};
xhr.open('get', link.href);
xhr.send();
}
/**
* Running on document load
*/
function main() {
// replacing inline styles
forEach.call(document.querySelectorAll('*'), polyfillNode);
// replacing css styles
forEach.call(document.querySelectorAll('style'), polyfillStyle);
forEach.call(document.querySelectorAll('link[rel="stylesheet"]'), polifillLink);
}
/**
* Make polyfill on new nodes and after modify styles
*/
function eventsAttach() {
var appendChild = HTMLElement.prototype.appendChild,
setAttribute = HTMLElement.prototype.setAttribute,
observer;
if ('MutationObserver' in window) {
observer = new MutationObserver(function (mutations) {
mutations.forEach(function (mutation) {
if (mutation.type === 'childList') {
[].forEach.call(mutation.addedNodes, polyfillNode);
} else if (mutation.type === 'attributes') {
polyfillNode(mutation.target);
}
});
});
observer.observe(document, {
subtree: true,
attributes: true,
childList: true,
characterData: true
});
} else {
HTMLElement.prototype.appendChild = function (elem) {
polyfillNode(elem);
return appendChild.apply(this, arguments);
};
HTMLElement.prototype.setAttribute = function (name, val) {
var r = setAttribute.apply(this, arguments);
if (name === 'style' && val.indexOf('background') !== -1) {
polyfillNode(this);
}
return r;
};
}
}
document.addEventListener('DOMContentLoaded', function () {
if (testImageSet()) {
return;
}
main();
eventsAttach();
});
}(window, document));