Skip to content

Commit

Permalink
Fixed ParseDOM parameter bug
Browse files Browse the repository at this point in the history
  • Loading branch information
verlok committed Mar 14, 2014
1 parent 2a1676a commit a47da00
Showing 1 changed file with 17 additions and 10 deletions.
27 changes: 17 additions & 10 deletions picturePolyfill.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"use strict";

var timerId,
pixelRatio = w.devicePixelRatio || 1, // The pixel density (2 is for HD aka Retina displays)
pixelRatio = Math.ceil(w.devicePixelRatio || 1), // The pixel density (2 is for HD aka Retina displays)
mediaQueriesSupported = w.matchMedia && w.matchMedia("only all") !== null && w.matchMedia("only all").matches;


Expand All @@ -22,7 +22,6 @@
if (typeof arrayOrString === 'string') {
return arrayOrString;
}
position = Math.ceil(position);
while (arrayOrString[position]===undefined && position>0) {
position-=1;
}
Expand Down Expand Up @@ -98,7 +97,7 @@
* @param element the starting element to parse DOM into. If not passed, it parses the whole document.
*/

function parseDOM(element) {
function parseDOMTree(element) {
var pictureData, imageHolder,
imageHolders = element.querySelectorAll('[data-picture]');

Expand All @@ -119,34 +118,42 @@
}
}

/**
* Private function to call w.picturePolyfill on the whole document
*/
function picturePolyfillDocument() {
w.picturePolyfill(document);
}

/**
* Expose the function to the global environment, if browser is supported, else empty function
* @type {Function}
*/

w.picturePolyfill = (!document.querySelectorAll) ? function(){} : function(element){
parseDOM(element || document);
parseDOMTree(element || document);
};


/**
* Manage resize event calling the parseDOM function
* Manage resize event calling the parseDOMTree function
* only if they've passed 100 milliseconds between a resize event and another
* to avoid the script to slower the browser on animated resize or browser edge dragging
*/

if (w.addEventListener) {
w.addEventListener('resize', function() {
clearTimeout(timerId);
timerId = setTimeout(w.picturePolyfill, 100);
timerId = setTimeout(picturePolyfillDocument, 100);
});
w.addEventListener('DOMContentLoaded', function(){
w.picturePolyfill();
w.removeEventListener('load', w.picturePolyfill);
picturePolyfillDocument();
w.removeEventListener('load', picturePolyfillDocument);
});
w.addEventListener('load', w.picturePolyfill);
w.addEventListener('load', picturePolyfillDocument);
}
else if (w.attachEvent) {
w.attachEvent('onload', w.picturePolyfill);
w.attachEvent('onload', picturePolyfillDocument);
}

}(this));

0 comments on commit a47da00

Please sign in to comment.