Skip to content

Commit e7aa747

Browse files
jetpacmonkeymeeber
authored andcommitted
fix: improve global object and DOM detection
Fixes #91
1 parent e10c4e5 commit e7aa747

File tree

1 file changed

+17
-5
lines changed

1 file changed

+17
-5
lines changed

index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,20 @@
66
* MIT Licensed
77
*/
88
var promiseExists = typeof Promise === 'function';
9-
var globalObject = typeof window !== 'undefined' ? window : typeof global !== 'undefined' ? global : self; // eslint-disable-line
10-
var isDom = 'location' in globalObject && 'document' in globalObject;
9+
10+
/* eslint-disable no-undef */
11+
var globalObject = typeof self === 'object' ? self : global; // eslint-disable-line id-blacklist
12+
13+
/*
14+
* All of these attributes must be available on the global object for the current environment
15+
* to be considered a DOM environment (browser)
16+
*/
17+
var isDom = typeof window === 'object' &&
18+
'document' in window &&
19+
'navigator' in window &&
20+
'HTMLElement' in window;
21+
/* eslint-enable */
22+
1123
var symbolExists = typeof Symbol !== 'undefined';
1224
var mapExists = typeof Map !== 'undefined';
1325
var setExists = typeof Set !== 'undefined';
@@ -160,7 +172,7 @@ module.exports = function typeDetect(obj) {
160172
* Test: `Object.prototype.toString.call(document.createElement('blockquote'))``
161173
* - IE <=10 === "[object HTMLBlockElement]"
162174
*/
163-
if (obj instanceof HTMLElement && obj.tagName === 'BLOCKQUOTE') {
175+
if (obj instanceof globalObject.HTMLElement && obj.tagName === 'BLOCKQUOTE') {
164176
return 'HTMLQuoteElement';
165177
}
166178

@@ -176,7 +188,7 @@ module.exports = function typeDetect(obj) {
176188
* - Firefox === "[object HTMLTableCellElement]"
177189
* - Safari === "[object HTMLTableCellElement]"
178190
*/
179-
if (obj instanceof HTMLElement && obj.tagName === 'TD') {
191+
if (obj instanceof globalObject.HTMLElement && obj.tagName === 'TD') {
180192
return 'HTMLTableDataCellElement';
181193
}
182194

@@ -192,7 +204,7 @@ module.exports = function typeDetect(obj) {
192204
* - Firefox === "[object HTMLTableCellElement]"
193205
* - Safari === "[object HTMLTableCellElement]"
194206
*/
195-
if (obj instanceof HTMLElement && obj.tagName === 'TH') {
207+
if (obj instanceof globalObject.HTMLElement && obj.tagName === 'TH') {
196208
return 'HTMLTableHeaderCellElement';
197209
}
198210
}

0 commit comments

Comments
 (0)