From 40986ee707f0a775c454a27fed9a3c375d1e8c98 Mon Sep 17 00:00:00 2001 From: Glen Hughes Date: Sat, 11 May 2019 17:23:10 +0100 Subject: [PATCH] Handle when no window or document (nodejs runtime) --- src/supports.js | 39 +++++++++++++++++++++------------------ 1 file changed, 21 insertions(+), 18 deletions(-) diff --git a/src/supports.js b/src/supports.js index be89882..a3bced0 100644 --- a/src/supports.js +++ b/src/supports.js @@ -8,29 +8,32 @@ import camelCase from 'lodash/camelCase'; * @returns {boolean} */ const isSupported = (property, value) => { - // Try the native standard method first - if ('CSS' in window && 'supports' in window.CSS) { - return window.CSS.supports(property, value); - } + // Handle when running in NodeJS and not a browser + if (typeof window !== "undefined" && typeof document !== "undefined") { + // Try the native standard method first + if ('CSS' in window && 'supports' in window.CSS) { + return window.CSS.supports(property, value); + } - // Check Opera's native method - if ('supportsCSS' in window) { - return window.supportsCSS(property, value); - } + // Check Opera's native method + if ('supportsCSS' in window) { + return window.supportsCSS(property, value); + } - // Convert to camel-case for DOM interactions - const camelCaseProperty = camelCase(property); + // Convert to camel-case for DOM interactions + const camelCaseProperty = camelCase(property); - // Check if the property is supported - const element = document.createElement('div'); - const support = (camelCaseProperty in element.style); + // Check if the property is supported + const element = document.createElement('div'); + const support = (camelCaseProperty in element.style); - // Assign the property and value to invoke the CSS interpreter - element.style.cssText = `${property}:${value}`; + // Assign the property and value to invoke the CSS interpreter + element.style.cssText = `${property}:${value}`; - // Ensure both the property and value are - // supported and return - return support && (element.style[camelCaseProperty] !== ''); + // Ensure both the property and value are + // supported and return + return support && (element.style[camelCaseProperty] !== ''); + } }; export default isSupported;