Skip to content
This repository has been archived by the owner on Jun 18, 2022. It is now read-only.

Commit

Permalink
Added non-minified version
Browse files Browse the repository at this point in the history
Closes #59 #49 #37 #36 #33
  • Loading branch information
bfred-it committed Jan 7, 2017
1 parent ebcfb1d commit 134dcf4
Show file tree
Hide file tree
Showing 6 changed files with 265 additions and 37 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> Polyfill `object-fit` and `object-position` on images on IE9, IE10, IE11, Edge, Safari, ...
[![gzipped size](https://badges.herokuapp.com/size/github/bfred-it/object-fit-images/master/dist/ofi.browser.js?gzip=true&label=gzipped%20size)](#readme) [![Travis build status](https://api.travis-ci.org/bfred-it/object-fit-images.svg)](https://travis-ci.org/bfred-it/object-fit-images) [![npm version](https://img.shields.io/npm/v/object-fit-images.svg)](https://www.npmjs.com/package/object-fit-images)
[![gzipped size](https://badges.herokuapp.com/size/github/bfred-it/object-fit-images/master/dist/ofi.min.js?gzip=true&label=gzipped%20size)](#readme) [![Travis build status](https://api.travis-ci.org/bfred-it/object-fit-images.svg)](https://travis-ci.org/bfred-it/object-fit-images) [![npm version](https://img.shields.io/npm/v/object-fit-images.svg)](https://www.npmjs.com/package/object-fit-images)

This adds support for `object-fit` and `object-position` to **IEdge 9-13, Android < 5, Safari < 10** and skips browsers that already support them.

Expand Down Expand Up @@ -136,13 +136,13 @@ var objectFitImages = require('object-fit-images');
If you don't use browserify/webpack, include this instead:

```html
<script src="dist/ofi.browser.js"></script>
<script src="dist/ofi.min.js"></script>
```

or from the [unpkg.com](https://unpkg.com) CDN:

```html
<script src="https://unpkg.com/object-fit-images@VERSION/dist/ofi.browser.js"></script>
<script src="https://unpkg.com/object-fit-images@VERSION/dist/ofi.min.js"></script>
```

Instead of `@VERSION` you should specify exact version like `@1.1.1`. The latest version is <img src="https://img.shields.io/npm/v/object-fit-images.svg?label=%20" height="16">
Expand Down
2 changes: 1 addition & 1 deletion demo/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>object-fit-images demo</title>
<script src="../dist/ofi.browser.js"></script>
<script src="../dist/ofi.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/picturefill/3.0.2/picturefill.min.js"></script>
<link rel="stylesheet" href="index.css">
</head>
Expand Down
226 changes: 226 additions & 0 deletions dist/ofi.js
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;

}());
2 changes: 1 addition & 1 deletion dist/ofi.browser.js → dist/ofi.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,14 @@
"build:js": "bfred-npm-bundler ofi objectFitImages",
"build": "npm-run-all --silent jsfix build:*",
"jsfix": "xo --fix",
"watch:server": "browser-sync start --startPath demo --no-ghost-mode --reload-delay 300 --no-open --server --files 'dist/*.browser.js,demo/**'",
"watch:server": "browser-sync start --startPath demo --no-ghost-mode --reload-delay 300 --no-open --server --files 'dist/*.min.js,demo/**'",
"watch:build": "onchange 'index.js' --initial -- npm run build -- --continue-on-error",
"watch": "npm-run-all --parallel --silent watch:*",
"prepublish": "npm run build",
"test": "xo; npm run build"
},
"devDependencies": {
"bfred-npm-bundler": "^7.1.2",
"bfred-npm-bundler": "^8.0.1",
"browser-sync": "^2.17.5",
"npm-run-all": "^4.0.0",
"onchange": "^3.0.2",
Expand Down
Loading

0 comments on commit 134dcf4

Please sign in to comment.