Skip to content

Commit

Permalink
#56: Added support for 'insertAt' option.
Browse files Browse the repository at this point in the history
  • Loading branch information
elsbree committed Jul 7, 2015
1 parent 2823eb4 commit 5459dd6
Showing 1 changed file with 11 additions and 4 deletions.
15 changes: 11 additions & 4 deletions addStyles.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ module.exports = function(list, options) {
// tags it will allow on a page
if (typeof options.singleton === "undefined") options.singleton = isOldIE();

// By default, add <style> tags to the bottom of <head>.
if (options.insertAt !== "top" && options.insertAt !== "bottom") options.insertAt = "bottom";

var styles = listToStyles(list);
addStylesToDom(styles, options);

Expand Down Expand Up @@ -95,11 +98,15 @@ function listToStyles(list) {
return styles;
}

function createStyleElement() {
function createStyleElement(options) {
var styleElement = document.createElement("style");
var head = getHeadElement();
styleElement.type = "text/css";
head.appendChild(styleElement);
if (options.insertAt === "top") {
head.insertBefore(styleElement, head.firstChild);
} else if (options.insertAt === "bottom") {
head.appendChild(styleElement);
}
return styleElement;
}

Expand All @@ -116,7 +123,7 @@ function addStyle(obj, options) {

if (options.singleton) {
var styleIndex = singletonCounter++;
styleElement = singletonElement || (singletonElement = createStyleElement());
styleElement = singletonElement || (singletonElement = createStyleElement(options));
update = applyToSingletonTag.bind(null, styleElement, styleIndex, false);
remove = applyToSingletonTag.bind(null, styleElement, styleIndex, true);
} else if(obj.sourceMap &&
Expand All @@ -133,7 +140,7 @@ function addStyle(obj, options) {
URL.revokeObjectURL(styleElement.href);
};
} else {
styleElement = createStyleElement();
styleElement = createStyleElement(options);
update = applyToTag.bind(null, styleElement);
remove = function() {
styleElement.parentNode.removeChild(styleElement);
Expand Down

0 comments on commit 5459dd6

Please sign in to comment.