From e7aa74715ae8db9d47431add47f1c9729b2cb115 Mon Sep 17 00:00:00 2001 From: Jeremy Tice Date: Mon, 30 Oct 2017 20:24:55 -0400 Subject: [PATCH] fix: improve global object and DOM detection Fixes #91 --- index.js | 22 +++++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/index.js b/index.js index 1c5406e..406dca2 100644 --- a/index.js +++ b/index.js @@ -6,8 +6,20 @@ * MIT Licensed */ var promiseExists = typeof Promise === 'function'; -var globalObject = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : self; // eslint-disable-line -var isDom = 'location' in globalObject && 'document' in globalObject; + +/* eslint-disable no-undef */ +var globalObject = typeof self === 'object' ? self : global; // eslint-disable-line id-blacklist + +/* + * All of these attributes must be available on the global object for the current environment + * to be considered a DOM environment (browser) + */ +var isDom = typeof window === 'object' && + 'document' in window && + 'navigator' in window && + 'HTMLElement' in window; +/* eslint-enable */ + var symbolExists = typeof Symbol !== 'undefined'; var mapExists = typeof Map !== 'undefined'; var setExists = typeof Set !== 'undefined'; @@ -160,7 +172,7 @@ module.exports = function typeDetect(obj) { * Test: `Object.prototype.toString.call(document.createElement('blockquote'))`` * - IE <=10 === "[object HTMLBlockElement]" */ - if (obj instanceof HTMLElement && obj.tagName === 'BLOCKQUOTE') { + if (obj instanceof globalObject.HTMLElement && obj.tagName === 'BLOCKQUOTE') { return 'HTMLQuoteElement'; } @@ -176,7 +188,7 @@ module.exports = function typeDetect(obj) { * - Firefox === "[object HTMLTableCellElement]" * - Safari === "[object HTMLTableCellElement]" */ - if (obj instanceof HTMLElement && obj.tagName === 'TD') { + if (obj instanceof globalObject.HTMLElement && obj.tagName === 'TD') { return 'HTMLTableDataCellElement'; } @@ -192,7 +204,7 @@ module.exports = function typeDetect(obj) { * - Firefox === "[object HTMLTableCellElement]" * - Safari === "[object HTMLTableCellElement]" */ - if (obj instanceof HTMLElement && obj.tagName === 'TH') { + if (obj instanceof globalObject.HTMLElement && obj.tagName === 'TH') { return 'HTMLTableHeaderCellElement'; } }