From be4a7636f1f8c885f030ea125757f5820a7bfe5c Mon Sep 17 00:00:00 2001 From: Matthew Pietz Date: Tue, 26 Aug 2014 10:53:50 -0700 Subject: [PATCH] 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. --- addStyle.js | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/addStyle.js b/addStyle.js index 2c73777..1d7aff9 100644 --- a/addStyle.js +++ b/addStyle.js @@ -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); } };