From b5bafcfdc861ff6f8dfc82b38766906453ea7c0e Mon Sep 17 00:00:00 2001 From: Nick Elsbree Date: Thu, 15 Oct 2015 16:57:52 -0700 Subject: [PATCH] Fixed ordering of stylesheets inserted with insertAt=top. --- addStyles.js | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/addStyles.js b/addStyles.js index 4255674..b1907d0 100644 --- a/addStyles.js +++ b/addStyles.js @@ -17,7 +17,8 @@ var stylesInDom = {}, return document.head || document.getElementsByTagName("head")[0]; }), singletonElement = null, - singletonCounter = 0; + singletonCounter = 0, + styleElementsInsertedAtTop = []; module.exports = function(list, options) { if(typeof DEBUG !== "undefined" && DEBUG) { @@ -101,9 +102,15 @@ function listToStyles(list) { function createStyleElement(options) { var styleElement = document.createElement("style"); var head = getHeadElement(); + var lastStyleElementInsertedAtTop = styleElementsInsertedAtTop[styleElementsInsertedAtTop.length - 1]; styleElement.type = "text/css"; if (options.insertAt === "top") { - head.insertBefore(styleElement, head.firstChild); + if(lastStyleElementInsertedAtTop) { + head.insertBefore(styleElement, lastStyleElementInsertedAtTop.nextSibling); + } else { + head.insertBefore(styleElement, head.firstChild); + } + styleElementsInsertedAtTop.push(styleElement); } else if (options.insertAt === "bottom") { head.appendChild(styleElement); } else {