Skip to content

Commit

Permalink
Fixed ordering of stylesheets inserted with insertAt=top.
Browse files Browse the repository at this point in the history
  • Loading branch information
elsbree committed Oct 15, 2015
1 parent a081f99 commit b5bafcf
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions addStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit b5bafcf

Please sign in to comment.