Skip to content
This repository has been archived by the owner on Jul 16, 2023. It is now read-only.

Commit

Permalink
Allow devs to pass optional DOM elements into objectFitPolyfill()
Browse files Browse the repository at this point in the history
This will greatly help performance for devs who require polyfilling extra JS functionality such as lazyloading, carousels, and so forth.
  • Loading branch information
cee-chen committed Jun 25, 2017
1 parent 1932a85 commit e3b2ca8
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 8 deletions.
2 changes: 1 addition & 1 deletion dist/objectFitPolyfill.basic.min.js

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

2 changes: 1 addition & 1 deletion dist/objectFitPolyfill.min.js

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

21 changes: 18 additions & 3 deletions src/objectFitPolyfill.basic.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,11 +122,26 @@

/**
* Initialize plugin
*
* @param {node} media - Optional specific DOM node(s) to be polyfilled
*/
var objectFitPolyfill = function() {
var media = document.querySelectorAll("[data-object-fit]");
var objectFitPolyfill = function(media) {
if (typeof media === "undefined") {
// If left blank, all media on the page will be polyfilled.
media = document.querySelectorAll("[data-object-fit]");
} else if (media && media.nodeName) {
// If it's a single node, wrap it in an array so it works.
media = [media];
} else if (typeof media === "object" && media.length && media[0].nodeName) {
// If it's an array of DOM nodes (e.g. a jQuery selector), it's fine as-is.
media = media;
} else {
// Otherwise, if it's invalid or an incorrect type, return false to let people know.
return false;
}

for (var i = 0; i < media.length; i ++) {
for (var i = 0; i < media.length; i++) {
if (!media[i].nodeName) { continue; }
var mediaType = media[i].nodeName.toLowerCase();

if (mediaType === "img") {
Expand Down
22 changes: 19 additions & 3 deletions src/objectFitPolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -218,11 +218,27 @@

/**
* Initialize plugin
*
* @param {node} media - Optional specific DOM node(s) to be polyfilled
*/
var objectFitPolyfill = function() {
var media = document.querySelectorAll("[data-object-fit]");
var objectFitPolyfill = function(media) {
if (typeof media === "undefined") {
// If left blank, all media on the page will be polyfilled.
media = document.querySelectorAll("[data-object-fit]");
} else if (media && media.nodeName) {
// If it's a single node, wrap it in an array so it works.
media = [media];
} else if (typeof media === "object" && media.length && media[0].nodeName) {
// If it's an array of DOM nodes (e.g. a jQuery selector), it's fine as-is.
media = media;
} else {
// Otherwise, if it's invalid or an incorrect type, return false to let people know.
return false;
}

for (var i = 0; i < media.length; i++) {
if (!media[i].nodeName) { continue; }

for (var i = 0; i < media.length; i ++) {
var mediaType = media[i].nodeName.toLowerCase();

if (mediaType === "img") {
Expand Down

0 comments on commit e3b2ca8

Please sign in to comment.