Skip to content

Commit

Permalink
Also look for @import url in stylesheets
Browse files Browse the repository at this point in the history
  • Loading branch information
lubber-de committed Oct 17, 2017
1 parent c8bb7d1 commit a2beb71
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
30 changes: 25 additions & 5 deletions stackload.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,13 @@
eT.removeEventListener("error", stackLoadError);
eT.removeEventListener("load", stackLoadDone);
currentLoadIndex++;

if(eT.jsonp && eT.parentNode) {
eT.parentNode.removeChild(eT);
}
if(!stopAll){
if(eT.href) {
searchCssImport(document.styleSheets[document.styleSheets.length-1]);
}
if(currentLoadIndex===callBacks[0].doneIndex) {
callBacks[0].success();
callBacks.shift();
Expand Down Expand Up @@ -78,6 +80,9 @@
catch (e) {}
},
setup = function(stack){
if(typeof stack === 'undefined') {
return;
}
if(typeof stack === 'string' || stack.url){
stack = {files:[stack]};
} else if(Array.isArray(stack)){
Expand Down Expand Up @@ -119,6 +124,7 @@
if(cL>0){
fullStack = fullStack.concat(cleanedStack);
var fL = fullStack.length;

if (typeof stack.error !== 'function') {
stack.error = function(){};
}
Expand All @@ -134,20 +140,34 @@
stack.success();
}
}
},
searchCssImport = function(styleSheet) {
if (styleSheet.cssRules) {
for (var j = 0, jl = styleSheet.cssRules.length; j < jl; j++) {
if (styleSheet.cssRules[j].href && registry.indexOf(styleSheet.cssRules[j].href)===-1) {
registry.push(styleSheet.cssRules[j].href);
}
}
}
}
;
window.stackLoad = function(stack) {
stopAll=false;
if(registry.length===0){
var elements = document.querySelectorAll('link,script'),i;
var elements = document.querySelectorAll('link,script'),i,il;
//forEach on querySelectorAll not supported in IE/Edge, thus usual for loop
for (i=0;i<elements.length;i++) {
if(elements[i].src) {
for (i=0,il=elements.length;i<il;i++) {
if(elements[i].src && registry.indexOf(elements[i].src)===-1) {
registry.push(elements[i].src);
} else if (elements[i].href) {
} else if (elements[i].href && registry.indexOf(elements[i].href)===-1) {
registry.push(elements[i].href);
}
}
//same for styleSheets, search for @import
var cssStyles = document.styleSheets;
for (i=0,il=cssStyles.length;i<il;i++) {
searchCssImport(cssStyles[i]);
}
}
setup(stack);
return {
Expand Down
2 changes: 1 addition & 1 deletion stackload.min.js

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

0 comments on commit a2beb71

Please sign in to comment.