Always return the dispose function

For the useable API, we always need to get a function back so that `unuse()`
works correctly.

Moved variable definitions to the top for cleanliness and better uglification.

All modern browsers support `document.head`. Use it and fallback to querying.
master
Matthew Pietz 2014-08-26 10:53:50 -07:00
parent 5e18452d9e
commit be4a7636f1
1 changed files with 10 additions and 3 deletions

View File

@ -6,9 +6,9 @@ module.exports = function addStyle(cssCode) {
if(typeof DEBUG !== "undefined" && DEBUG) {
if(typeof document !== "object") throw new Error("The style-loader cannot be used in a non-browser environment");
}
var styleElement = document.createElement("style");
var styleElement = document.createElement("style"),
head = document.head || document.getElementsByTagName("head")[0];
styleElement.type = "text/css";
var head = document.getElementsByTagName("head")[0];
head.appendChild(styleElement);
if (styleElement.styleSheet) {
styleElement.styleSheet.cssText = cssCode;
@ -24,8 +24,15 @@ module.exports = function addStyle(cssCode) {
styleElement.childNodes[0].nodeValue = cssCode;
}
} else {
head.removeChild(styleElement);
dispose();
}
};
} else {
// For the useable API, provide a function to remove the stylesheet.
return dispose;
}
function dispose() {
head.removeChild(styleElement);
}
};