This repository has been archived by the owner on Jun 18, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 269
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
265 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,226 @@ | ||
/*! npm.im/object-fit-images */ | ||
var objectFitImages = (function () { | ||
'use strict'; | ||
|
||
var OFI = 'bfred-it:object-fit-images'; | ||
var propRegex = /(object-fit|object-position)\s*:\s*([-\w\s%]+)/g; | ||
var testImg = new Image(); | ||
var placeholder = document.createElement('canvas'); | ||
var supportsObjectFit = 'object-fit' in testImg.style; | ||
var supportsObjectPosition = 'object-position' in testImg.style; | ||
var supportsOFI = 'background-size' in testImg.style && window.HTMLCanvasElement; | ||
var supportsCurrentSrc = typeof testImg.currentSrc === 'string'; | ||
var nativeGetAttribute = testImg.getAttribute; | ||
var nativeSetAttribute = testImg.setAttribute; | ||
var autoModeEnabled = false; | ||
|
||
function polyfillCurrentSrc(el) { | ||
if (el.srcset && !supportsCurrentSrc && window.picturefill) { | ||
var 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) { | ||
var style = getComputedStyle(el).fontFamily; | ||
var parsed; | ||
var props = {}; | ||
while ((parsed = propRegex.exec(style)) !== null) { | ||
props[parsed[1]] = parsed[2]; | ||
} | ||
return props; | ||
} | ||
|
||
function setPlaceholder(img, width, height) { | ||
placeholder.width = width || 1; | ||
placeholder.height = height || 1; | ||
if (img[OFI].width !== placeholder.width || img[OFI].height !== placeholder.height) { | ||
// cache size to avoid unnecessary changes | ||
img[OFI].width = placeholder.width; | ||
img[OFI].height = placeholder.height; | ||
nativeSetAttribute.call(img, 'src', placeholder.toDataURL()); | ||
} | ||
} | ||
|
||
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) { | ||
var style = getStyle(el); | ||
var 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/bfred-it/object-fit-images/issues/53 | ||
nativeSetAttribute.call(el, "data-ofi-src", el.src); | ||
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.log('http://bit.ly/ofi-old-browser'); | ||
} | ||
} | ||
} | ||
|
||
polyfillCurrentSrc(ofi.img); | ||
|
||
el.style.backgroundImage = "url(" + ((ofi.img.currentSrc || ofi.img.src).replace('(', '%28').replace(')', '%29')) + ")"; | ||
el.style.backgroundPosition = style['object-position'] || 'center'; | ||
el.style.backgroundRepeat = 'no-repeat'; | ||
|
||
if (/scale-down/.test(style['object-fit'])) { | ||
onImageReady(ofi.img, function () { | ||
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, function (img) { | ||
setPlaceholder(el, img.naturalWidth, img.naturalHeight); | ||
}); | ||
} | ||
|
||
function keepSrcUsable(el) { | ||
var descriptors = { | ||
get: function get(prop) { | ||
return el[OFI].img[prop ? prop : 'src']; | ||
}, | ||
set: function 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: function () { return descriptors.get('currentSrc'); } | ||
}); | ||
Object.defineProperty(el, 'srcset', { | ||
get: function () { return descriptors.get('srcset'); }, | ||
set: function (ss) { return 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)); | ||
}; | ||
} | ||
} | ||
|
||
function fix(imgs, opts) { | ||
var 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 (typeof imgs === 'string') { | ||
imgs = document.querySelectorAll(imgs); | ||
} else if (!('length' in imgs)) { | ||
imgs = [imgs]; | ||
} | ||
|
||
// apply fix to all | ||
for (var i = 0; i < imgs.length; i++) { | ||
imgs[i][OFI] = imgs[i][OFI] || { | ||
skipTest: opts.skipTest | ||
}; | ||
fixOne(imgs[i]); | ||
} | ||
|
||
if (startAutoMode) { | ||
document.body.addEventListener('load', function (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(); | ||
|
||
return fix; | ||
|
||
}()); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.