diff --git a/Gruntfile.js b/Gruntfile.js index 11aa044..6e4f514 100644 --- a/Gruntfile.js +++ b/Gruntfile.js @@ -5,7 +5,7 @@ module.exports = function(grunt) { grunt.initConfig({ // Metadata. meta: { - version: '2.0.3' + version: '2.0.4' }, banner: '/*! picturePolyfill - v<%= meta.version %> - ' + '<%= grunt.template.today("yyyy-mm-dd") %>\n' + diff --git a/bower.json b/bower.json index 8ee066d..afc316f 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "picturePolyfill", - "version": "2.0.3", + "version": "2.0.4", "main": "picturePolyfill.js", "ignore": [ ".idea", diff --git a/picturePolyfill.js b/picturePolyfill.js index 6c25fe4..2e6ed15 100644 --- a/picturePolyfill.js +++ b/picturePolyfill.js @@ -221,10 +221,15 @@ } } - initialize(); + // Check if picture is supported. If not, initialize module and expose picturePolyfill to the global namespace + // else expose an empty function to avoid errors when calling picturePolyfill - // Exposing picturePolyfill to the global environment, - // to gain the ability to call picturePolyfill on a slice of DOM (eg: after an AJAX call) - w.picturePolyfill = parsePictures; + if (!window.HTMLPictureElement) { + initialize(); + w.picturePolyfill = parsePictures; + } + else { + w.picturePolyfill = function() {}; + } }(window)); \ No newline at end of file diff --git a/picturePolyfill.min.js b/picturePolyfill.min.js index fd7ce0c..2419bad 100644 --- a/picturePolyfill.min.js +++ b/picturePolyfill.min.js @@ -1,4 +1,4 @@ -/*! picturePolyfill - v2.0.3 - 2014-03-18 +/*! picturePolyfill - v2.0.4 - 2014-03-19 * https://github.com/verlok/picturePolyfill/ * Copyright (c) 2014 Andrea "verlok" Verlicchi; Licensed MIT */ -!function(a){"use strict";function b(){var a=document.createElement("img"),b=document.getElementsByTagName("picture")[0];try{return b&&(b.appendChild(a),b.removeChild(a)),!0}catch(c){return!1}}function c(a,b,c){var d=document.createElement("img");d.setAttribute("alt",c),d.setAttribute("src",b),a.appendChild(d)}function d(a,b,d){var e=document.createElement("picture");c(e,b,d),a.parentNode.replaceChild(e,a)}function e(a){for(var b,c,d,e={},f=a.split(","),g=0,h=f.length;h>g;g+=1)b=f[g].trim().split(" "),d=b[1]?b[1].trim():"1x",c=b[0].trim(),e[d]=c;return e}function f(a,b){var c;do c=a[b+"x"],b-=1;while(void 0===c&&b>0);return c}function g(b){for(var c,d=0,e=b.length;e>d;d+=1){var g=b[d],h=g.media,i=g.srcset;(!h||a.matchMedia(h).matches)&&(c=i?f(i,m):g.src)}return c}function h(a,b){var e,f,h=a.getElementsByTagName("img");e=n&&b.length?g(b):a.getAttribute("data-default-src"),h.length?h[0].setAttribute("src",e):(f=a.getAttribute("data-alt"),o?c(a,e,f):d(a,e,f))}function i(a){for(var b=[],c=a.getElementsByTagName("source"),d=0,f=c.length;f>d;d+=1){var g=c[d],h=g.getAttribute("srcset");b.push({media:g.getAttribute("media"),src:g.getAttribute("src"),srcset:h?e(h):null})}return b}function j(a){for(var b,c,d=a.getElementsByTagName("picture"),e=0,f=d.length;f>e;e+=1)c=d[e],b=p[c.getAttribute("data-cache-index")],b||(b=i(c),p[q]=b,c.setAttribute("data-cache-index",q),q+=1),h(c,b)}function k(){function c(){j(document)}m=a.devicePixelRatio?Math.ceil(a.devicePixelRatio):1,n=a.matchMedia&&null!==a.matchMedia("only all")&&a.matchMedia("only all").matches,o=b(),p=[],q=0,a.addEventListener?(a.addEventListener("resize",function(){clearTimeout(l),l=setTimeout(c,100)}),a.addEventListener("DOMContentLoaded",function(){c(),a.removeEventListener("load",c)}),a.addEventListener("load",c)):a.attachEvent&&a.attachEvent("onload",c)}var l,m,n,o,p,q;k(),a.picturePolyfill=j}(window); \ No newline at end of file +!function(a){"use strict";function b(){var a=document.createElement("img"),b=document.getElementsByTagName("picture")[0];try{return b&&(b.appendChild(a),b.removeChild(a)),!0}catch(c){return!1}}function c(a,b,c){var d=document.createElement("img");d.setAttribute("alt",c),d.setAttribute("src",b),a.appendChild(d)}function d(a,b,d){var e=document.createElement("picture");c(e,b,d),a.parentNode.replaceChild(e,a)}function e(a){for(var b,c,d,e={},f=a.split(","),g=0,h=f.length;h>g;g+=1)b=f[g].trim().split(" "),d=b[1]?b[1].trim():"1x",c=b[0].trim(),e[d]=c;return e}function f(a,b){var c;do c=a[b+"x"],b-=1;while(void 0===c&&b>0);return c}function g(b){for(var c,d=0,e=b.length;e>d;d+=1){var g=b[d],h=g.media,i=g.srcset;(!h||a.matchMedia(h).matches)&&(c=i?f(i,m):g.src)}return c}function h(a,b){var e,f,h=a.getElementsByTagName("img");e=n&&b.length?g(b):a.getAttribute("data-default-src"),h.length?h[0].setAttribute("src",e):(f=a.getAttribute("data-alt"),o?c(a,e,f):d(a,e,f))}function i(a){for(var b=[],c=a.getElementsByTagName("source"),d=0,f=c.length;f>d;d+=1){var g=c[d],h=g.getAttribute("srcset");b.push({media:g.getAttribute("media"),src:g.getAttribute("src"),srcset:h?e(h):null})}return b}function j(a){for(var b,c,d=a.getElementsByTagName("picture"),e=0,f=d.length;f>e;e+=1)c=d[e],b=p[c.getAttribute("data-cache-index")],b||(b=i(c),p[q]=b,c.setAttribute("data-cache-index",q),q+=1),h(c,b)}function k(){function c(){j(document)}m=a.devicePixelRatio?Math.ceil(a.devicePixelRatio):1,n=a.matchMedia&&null!==a.matchMedia("only all")&&a.matchMedia("only all").matches,o=b(),p=[],q=0,a.addEventListener?(a.addEventListener("resize",function(){clearTimeout(l),l=setTimeout(c,100)}),a.addEventListener("DOMContentLoaded",function(){c(),a.removeEventListener("load",c)}),a.addEventListener("load",c)):a.attachEvent&&a.attachEvent("onload",c)}var l,m,n,o,p,q;window.HTMLPictureElement?a.picturePolyfill=function(){}:(k(),a.picturePolyfill=j)}(window); \ No newline at end of file