{"version":3,"names":[],"mappings":"","sources":["ofi.js"],"sourcesContent":["/*! npm.im/object-fit-images 3.2.4 */\r\nvar objectFitImages = (function () {\r\n'use strict';\r\n\r\nvar OFI = 'bfred-it:object-fit-images';\r\nvar propRegex = /(object-fit|object-position)\\s*:\\s*([-.\\w\\s%]+)/g;\r\nvar testImg = typeof Image === 'undefined' ? {style: {'object-position': 1}} : new Image();\r\nvar supportsObjectFit = 'object-fit' in testImg.style;\r\nvar supportsObjectPosition = 'object-position' in testImg.style;\r\nvar supportsOFI = 'background-size' in testImg.style;\r\nvar supportsCurrentSrc = typeof testImg.currentSrc === 'string';\r\nvar nativeGetAttribute = testImg.getAttribute;\r\nvar nativeSetAttribute = testImg.setAttribute;\r\nvar autoModeEnabled = false;\r\n\r\nfunction createPlaceholder(w, h) {\r\n\treturn (\"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='\" + w + \"' height='\" + h + \"'%3E%3C/svg%3E\");\r\n}\r\n\r\nfunction polyfillCurrentSrc(el) {\r\n\tif (el.srcset && !supportsCurrentSrc && window.picturefill) {\r\n\t\tvar pf = window.picturefill._;\r\n\t\t// parse srcset with picturefill where currentSrc isn't available\r\n\t\tif (!el[pf.ns] || !el[pf.ns].evaled) {\r\n\t\t\t// force synchronous srcset parsing\r\n\t\t\tpf.fillImg(el, {reselect: true});\r\n\t\t}\r\n\r\n\t\tif (!el[pf.ns].curSrc) {\r\n\t\t\t// force picturefill to parse srcset\r\n\t\t\tel[pf.ns].supported = false;\r\n\t\t\tpf.fillImg(el, {reselect: true});\r\n\t\t}\r\n\r\n\t\t// retrieve parsed currentSrc, if any\r\n\t\tel.currentSrc = el[pf.ns].curSrc || el.src;\r\n\t}\r\n}\r\n\r\nfunction getStyle(el) {\r\n\tvar style = getComputedStyle(el).fontFamily;\r\n\tvar parsed;\r\n\tvar props = {};\r\n\twhile ((parsed = propRegex.exec(style)) !== null) {\r\n\t\tprops[parsed[1]] = parsed[2];\r\n\t}\r\n\treturn props;\r\n}\r\n\r\nfunction setPlaceholder(img, width, height) {\r\n\t// Default: fill width, no height\r\n\tvar placeholder = createPlaceholder(width || 1, height || 0);\r\n\r\n\t// Only set placeholder if it's different\r\n\tif (nativeGetAttribute.call(img, 'src') !== placeholder) {\r\n\t\tnativeSetAttribute.call(img, 'src', placeholder);\r\n\t}\r\n}\r\n\r\nfunction onImageReady(img, callback) {\r\n\t// naturalWidth is only available when the image headers are loaded,\r\n\t// this loop will poll it every 100ms.\r\n\tif (img.naturalWidth) {\r\n\t\tcallback(img);\r\n\t} else {\r\n\t\tsetTimeout(onImageReady, 100, img, callback);\r\n\t}\r\n}\r\n\r\nfunction fixOne(el) {\r\n\tvar style = getStyle(el);\r\n\tvar ofi = el[OFI];\r\n\tstyle['object-fit'] = style['object-fit'] || 'fill'; // default value\r\n\r\n\t// Avoid running where unnecessary, unless OFI had already done its deed\r\n\tif (!ofi.img) {\r\n\t\t// fill is the default behavior so no action is necessary\r\n\t\tif (style['object-fit'] === 'fill') {\r\n\t\t\treturn;\r\n\t\t}\r\n\r\n\t\t// Where object-fit is supported and object-position isn't (Safari < 10)\r\n\t\tif (\r\n\t\t\t!ofi.skipTest && // unless user wants to apply regardless of browser support\r\n\t\t\tsupportsObjectFit && // if browser already supports object-fit\r\n\t\t\t!style['object-position'] // unless object-position is used\r\n\t\t) {\r\n\t\t\treturn;\r\n\t\t}\r\n\t}\r\n\r\n\t// keep a clone in memory while resetting the original to a blank\r\n\tif (!ofi.img) {\r\n\t\tofi.img = new Image(el.width, el.height);\r\n\t\tofi.img.srcset = nativeGetAttribute.call(el, \"data-ofi-srcset\") || el.srcset;\r\n\t\tofi.img.src = nativeGetAttribute.call(el, \"data-ofi-src\") || el.src;\r\n\r\n\t\t// preserve for any future cloneNode calls\r\n\t\t// https://github.com/bfred-it/object-fit-images/issues/53\r\n\t\tnativeSetAttribute.call(el, \"data-ofi-src\", el.src);\r\n\t\tif (el.srcset) {\r\n\t\t\tnativeSetAttribute.call(el, \"data-ofi-srcset\", el.srcset);\r\n\t\t}\r\n\r\n\t\tsetPlaceholder(el, el.naturalWidth || el.width, el.naturalHeight || el.height);\r\n\r\n\t\t// remove srcset because it overrides src\r\n\t\tif (el.srcset) {\r\n\t\t\tel.srcset = '';\r\n\t\t}\r\n\t\ttry {\r\n\t\t\tkeepSrcUsable(el);\r\n\t\t} catch (err) {\r\n\t\t\tif (window.console) {\r\n\t\t\t\tconsole.warn('https://bit.ly/ofi-old-browser');\r\n\t\t\t}\r\n\t\t}\r\n\t}\r\n\r\n\tpolyfillCurrentSrc(ofi.img);\r\n\r\n\tel.style.backgroundImage = \"url(\\\"\" + ((ofi.img.currentSrc || ofi.img.src).replace(/\"/g, '\\\\\"')) + \"\\\")\";\r\n\tel.style.backgroundPosition = style['object-position'] || 'center';\r\n\tel.style.backgroundRepeat = 'no-repeat';\r\n\tel.style.backgroundOrigin = 'content-box';\r\n\r\n\tif (/scale-down/.test(style['object-fit'])) {\r\n\t\tonImageReady(ofi.img, function () {\r\n\t\t\tif (ofi.img.naturalWidth > el.width || ofi.img.naturalHeight > el.height) {\r\n\t\t\t\tel.style.backgroundSize = 'contain';\r\n\t\t\t} else {\r\n\t\t\t\tel.style.backgroundSize = 'auto';\r\n\t\t\t}\r\n\t\t});\r\n\t} else {\r\n\t\tel.style.backgroundSize = style['object-fit'].replace('none', 'auto').replace('fill', '100% 100%');\r\n\t}\r\n\r\n\tonImageReady(ofi.img, function (img) {\r\n\t\tsetPlaceholder(el, img.naturalWidth, img.naturalHeight);\r\n\t});\r\n}\r\n\r\nfunction keepSrcUsable(el) {\r\n\tvar descriptors = {\r\n\t\tget: function get(prop) {\r\n\t\t\treturn el[OFI].img[prop ? prop : 'src'];\r\n\t\t},\r\n\t\tset: function set(value, prop) {\r\n\t\t\tel[OFI].img[prop ? prop : 'src'] = value;\r\n\t\t\tnativeSetAttribute.call(el, (\"data-ofi-\" + prop), value); // preserve for any future cloneNode\r\n\t\t\tfixOne(el);\r\n\t\t\treturn value;\r\n\t\t}\r\n\t};\r\n\tObject.defineProperty(el, 'src', descriptors);\r\n\tObject.defineProperty(el, 'currentSrc', {\r\n\t\tget: function () { return descriptors.get('currentSrc'); }\r\n\t});\r\n\tObject.defineProperty(el, 'srcset', {\r\n\t\tget: function () { return descriptors.get('srcset'); },\r\n\t\tset: function (ss) { return descriptors.set(ss, 'srcset'); }\r\n\t});\r\n}\r\n\r\nfunction hijackAttributes() {\r\n\tfunction getOfiImageMaybe(el, name) {\r\n\t\treturn el[OFI] && el[OFI].img && (name === 'src' || name === 'srcset') ? el[OFI].img : el;\r\n\t}\r\n\tif (!supportsObjectPosition) {\r\n\t\tHTMLImageElement.prototype.getAttribute = function (name) {\r\n\t\t\treturn nativeGetAttribute.call(getOfiImageMaybe(this, name), name);\r\n\t\t};\r\n\r\n\t\tHTMLImageElement.prototype.setAttribute = function (name, value) {\r\n\t\t\treturn nativeSetAttribute.call(getOfiImageMaybe(this, name), name, String(value));\r\n\t\t};\r\n\t}\r\n}\r\n\r\nfunction fix(imgs, opts) {\r\n\tvar startAutoMode = !autoModeEnabled && !imgs;\r\n\topts = opts || {};\r\n\timgs = imgs || 'img';\r\n\r\n\tif ((supportsObjectPosition && !opts.skipTest) || !supportsOFI) {\r\n\t\treturn false;\r\n\t}\r\n\r\n\t// use imgs as a selector or just select all images\r\n\tif (imgs === 'img') {\r\n\t\timgs = document.getElementsByTagName('img');\r\n\t} else if (typeof imgs === 'string') {\r\n\t\timgs = document.querySelectorAll(imgs);\r\n\t} else if (!('length' in imgs)) {\r\n\t\timgs = [imgs];\r\n\t}\r\n\r\n\t// apply fix to all\r\n\tfor (var i = 0; i < imgs.length; i++) {\r\n\t\timgs[i][OFI] = imgs[i][OFI] || {\r\n\t\t\tskipTest: opts.skipTest\r\n\t\t};\r\n\t\tfixOne(imgs[i]);\r\n\t}\r\n\r\n\tif (startAutoMode) {\r\n\t\tdocument.body.addEventListener('load', function (e) {\r\n\t\t\tif (e.target.tagName === 'IMG') {\r\n\t\t\t\tfix(e.target, {\r\n\t\t\t\t\tskipTest: opts.skipTest\r\n\t\t\t\t});\r\n\t\t\t}\r\n\t\t}, true);\r\n\t\tautoModeEnabled = true;\r\n\t\timgs = 'img'; // reset to a generic selector for watchMQ\r\n\t}\r\n\r\n\t// if requested, watch media queries for object-fit change\r\n\tif (opts.watchMQ) {\r\n\t\twindow.addEventListener('resize', fix.bind(null, imgs, {\r\n\t\t\tskipTest: opts.skipTest\r\n\t\t}));\r\n\t}\r\n}\r\n\r\nfix.supportsObjectFit = supportsObjectFit;\r\nfix.supportsObjectPosition = supportsObjectPosition;\r\n\r\nhijackAttributes();\r\n\r\nreturn fix;\r\n\r\n}());\r\n"],"file":"../../../../../ofi.js"}