forked from fregante/object-fit-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
226 lines (196 loc) · 6.46 KB
/
index.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
const OFI = 'fregante:object-fit-images';
const propRegex = /(object-fit|object-position)\s*:\s*([-.\w\s%]+)/g;
const testImg = typeof Image === 'undefined' ? {style: {'object-position': 1}} : new Image();
const supportsObjectFit = 'object-fit' in testImg.style;
const supportsObjectPosition = 'object-position' in testImg.style;
const supportsOFI = 'background-size' in testImg.style;
const supportsCurrentSrc = typeof testImg.currentSrc === 'string';
const nativeGetAttribute = testImg.getAttribute;
const nativeSetAttribute = testImg.setAttribute;
let autoModeEnabled = false;
function createPlaceholder(w, h) {
return `data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='${w}' height='${h}'%3E%3C/svg%3E`;
}
function polyfillCurrentSrc(el) {
if (el.srcset && !supportsCurrentSrc && window.picturefill) {
const pf = window.picturefill._;
// parse srcset with picturefill where currentSrc isn't available
if (!el[pf.ns] || !el[pf.ns].evaled) {
// force synchronous srcset parsing
pf.fillImg(el, {reselect: true});
}
if (!el[pf.ns].curSrc) {
// force picturefill to parse srcset
el[pf.ns].supported = false;
pf.fillImg(el, {reselect: true});
}
// retrieve parsed currentSrc, if any
el.currentSrc = el[pf.ns].curSrc || el.src;
}
}
function getStyle(el) {
const style = getComputedStyle(el).fontFamily;
let parsed;
const props = {};
while ((parsed = propRegex.exec(style)) !== null) {
props[parsed[1]] = parsed[2];
}
return props;
}
function setPlaceholder(img, width, height) {
// Default: fill width, no height
const placeholder = createPlaceholder(width || 1, height || 0);
// Only set placeholder if it's different
if (nativeGetAttribute.call(img, 'src') !== placeholder) {
nativeSetAttribute.call(img, 'src', placeholder);
}
}
function onImageReady(img, callback) {
// naturalWidth is only available when the image headers are loaded,
// this loop will poll it every 100ms.
if (img.naturalWidth) {
callback(img);
} else {
setTimeout(onImageReady, 100, img, callback);
}
}
function fixOne(el) {
const style = getStyle(el);
const ofi = el[OFI];
style['object-fit'] = style['object-fit'] || 'fill'; // default value
// Avoid running where unnecessary, unless OFI had already done its deed
if (!ofi.img) {
// fill is the default behavior so no action is necessary
if (style['object-fit'] === 'fill') {
return;
}
// Where object-fit is supported and object-position isn't (Safari < 10)
if (
!ofi.skipTest && // unless user wants to apply regardless of browser support
supportsObjectFit && // if browser already supports object-fit
!style['object-position'] // unless object-position is used
) {
return;
}
}
// keep a clone in memory while resetting the original to a blank
if (!ofi.img) {
ofi.img = new Image(el.width, el.height);
ofi.img.srcset = nativeGetAttribute.call(el, `data-ofi-srcset`) || el.srcset;
ofi.img.src = nativeGetAttribute.call(el, `data-ofi-src`) || el.src;
// preserve for any future cloneNode calls
// https://github.com/fregante/object-fit-images/issues/53
nativeSetAttribute.call(el, `data-ofi-src`, el.src);
if (el.srcset) {
nativeSetAttribute.call(el, `data-ofi-srcset`, el.srcset);
}
setPlaceholder(el, el.naturalWidth || el.width, el.naturalHeight || el.height);
// remove srcset because it overrides src
if (el.srcset) {
el.srcset = '';
}
try {
keepSrcUsable(el);
} catch (err) {
if (window.console) {
console.warn('https://bit.ly/ofi-old-browser');
}
}
}
polyfillCurrentSrc(ofi.img);
el.style.backgroundImage = `url("${(ofi.img.currentSrc || ofi.img.src).replace(/"/g, '\\"')}")`;
el.style.backgroundPosition = style['object-position'] || 'center';
el.style.backgroundRepeat = 'no-repeat';
el.style.backgroundOrigin = 'content-box';
if (/scale-down/.test(style['object-fit'])) {
onImageReady(ofi.img, () => {
if (ofi.img.naturalWidth > el.width || ofi.img.naturalHeight > el.height) {
el.style.backgroundSize = 'contain';
} else {
el.style.backgroundSize = 'auto';
}
});
} else {
el.style.backgroundSize = style['object-fit'].replace('none', 'auto').replace('fill', '100% 100%');
}
onImageReady(ofi.img, img => {
setPlaceholder(el, img.naturalWidth, img.naturalHeight);
});
}
function keepSrcUsable(el) {
const descriptors = {
get(prop) {
return el[OFI].img[prop ? prop : 'src'];
},
set(value, prop) {
el[OFI].img[prop ? prop : 'src'] = value;
nativeSetAttribute.call(el, `data-ofi-${prop}`, value); // preserve for any future cloneNode
fixOne(el);
return value;
}
};
Object.defineProperty(el, 'src', descriptors);
Object.defineProperty(el, 'currentSrc', {
get: () => descriptors.get('currentSrc')
});
Object.defineProperty(el, 'srcset', {
get: () => descriptors.get('srcset'),
set: ss => descriptors.set(ss, 'srcset')
});
}
function hijackAttributes() {
function getOfiImageMaybe(el, name) {
return el[OFI] && el[OFI].img && (name === 'src' || name === 'srcset') ? el[OFI].img : el;
}
if (!supportsObjectPosition) {
HTMLImageElement.prototype.getAttribute = function (name) {
return nativeGetAttribute.call(getOfiImageMaybe(this, name), name);
};
HTMLImageElement.prototype.setAttribute = function (name, value) {
return nativeSetAttribute.call(getOfiImageMaybe(this, name), name, String(value));
};
}
}
export default function fix(imgs, opts) {
const startAutoMode = !autoModeEnabled && !imgs;
opts = opts || {};
imgs = imgs || 'img';
if ((supportsObjectPosition && !opts.skipTest) || !supportsOFI) {
return false;
}
// use imgs as a selector or just select all images
if (imgs === 'img') {
imgs = document.getElementsByTagName('img');
} else if (typeof imgs === 'string') {
imgs = document.querySelectorAll(imgs);
} else if (!('length' in imgs)) {
imgs = [imgs];
}
// apply fix to all
for (let i = 0; i < imgs.length; i++) {
imgs[i][OFI] = imgs[i][OFI] || {
skipTest: opts.skipTest
};
fixOne(imgs[i]);
}
if (startAutoMode) {
document.body.addEventListener('load', e => {
if (e.target.tagName === 'IMG') {
fix(e.target, {
skipTest: opts.skipTest
});
}
}, true);
autoModeEnabled = true;
imgs = 'img'; // reset to a generic selector for watchMQ
}
// if requested, watch media queries for object-fit change
if (opts.watchMQ) {
window.addEventListener('resize', fix.bind(null, imgs, {
skipTest: opts.skipTest
}));
}
}
fix.supportsObjectFit = supportsObjectFit;
fix.supportsObjectPosition = supportsObjectPosition;
hijackAttributes();