@@ -8,16 +8,6 @@ const promiseExists = typeof Promise === 'function';
8
8
/* eslint-disable no-undef */
9
9
const globalObject = typeof self === 'object' ? self : global ; // eslint-disable-line id-blacklist
10
10
11
- /*
12
- * All of these attributes must be available on the global object for the current environment
13
- * to be considered a DOM environment (browser)
14
- */
15
- const isDom = typeof window === 'object' &&
16
- 'document' in window &&
17
- 'navigator' in window &&
18
- 'HTMLElement' in window ;
19
- /* eslint-enable */
20
-
21
11
const symbolExists = typeof Symbol !== 'undefined' ;
22
12
const mapExists = typeof Map !== 'undefined' ;
23
13
const setExists = typeof Set !== 'undefined' ;
@@ -36,6 +26,14 @@ const stringIteratorExists = symbolIteratorExists && typeof String.prototype[Sym
36
26
const stringIteratorPrototype = stringIteratorExists && Object . getPrototypeOf ( '' [ Symbol . iterator ] ( ) ) ;
37
27
const toStringLeftSliceLength = 8 ;
38
28
const toStringRightSliceLength = - 1 ;
29
+ const windowExists = typeof window === 'object' ;
30
+ const windowLocationExists = windowExists && typeof window . location === 'object' ;
31
+ const windowDocumentExists = windowExists && typeof window . document === 'object' ;
32
+ const windowNavigatorExists = windowExists && typeof window . navigator === 'object' ;
33
+ const windowNavigatorMimeTypesExists = windowNavigatorExists && typeof window . navigator . mimeTypes === 'object' ;
34
+ const windowNavigatorPluginsExists = windowNavigatorExists && typeof window . navigator . plugins === 'object' ;
35
+ const windowHTMLElementExists = windowExists &&
36
+ ( typeof window . HTMLElement === 'function' || typeof window . HTMLElement === 'object' ) ;
39
37
/**
40
38
* ### typeOf (obj)
41
39
*
@@ -109,15 +107,15 @@ export default function typeDetect(obj) {
109
107
return 'Array' ;
110
108
}
111
109
112
- if ( isDom ) {
110
+ if ( windowExists ) {
113
111
/* ! Spec Conformance
114
112
* (https://html.spec.whatwg.org/multipage/browsers.html#location)
115
113
* WhatWG HTML$7.7.3 - The `Location` interface
116
114
* Test: `Object.prototype.toString.call(window.location)``
117
115
* - IE <=11 === "[object Object]"
118
116
* - IE Edge <=13 === "[object Object]"
119
117
*/
120
- if ( obj === globalObject . location ) {
118
+ if ( windowLocationExists && obj === window . location ) {
121
119
return 'Location' ;
122
120
}
123
121
@@ -140,7 +138,7 @@ export default function typeDetect(obj) {
140
138
* - IE 11 === "[object HTMLDocument]"
141
139
* - IE Edge <=13 === "[object HTMLDocument]"
142
140
*/
143
- if ( obj === globalObject . document ) {
141
+ if ( windowDocumentExists && obj === window . document ) {
144
142
return 'Document' ;
145
143
}
146
144
@@ -150,7 +148,7 @@ export default function typeDetect(obj) {
150
148
* Test: `Object.prototype.toString.call(navigator.mimeTypes)``
151
149
* - IE <=10 === "[object MSMimeTypesCollection]"
152
150
*/
153
- if ( obj === ( globalObject . navigator || { } ) . mimeTypes ) {
151
+ if ( windowNavigatorMimeTypesExists && obj === window . navigator . mimeTypes ) {
154
152
return 'MimeTypeArray' ;
155
153
}
156
154
@@ -160,50 +158,52 @@ export default function typeDetect(obj) {
160
158
* Test: `Object.prototype.toString.call(navigator.plugins)``
161
159
* - IE <=10 === "[object MSPluginsCollection]"
162
160
*/
163
- if ( obj === ( globalObject . navigator || { } ) . plugins ) {
161
+ if ( windowNavigatorPluginsExists && obj === window . navigator . plugins ) {
164
162
return 'PluginArray' ;
165
163
}
166
164
167
- /* ! Spec Conformance
168
- * (https://html.spec.whatwg.org/multipage/webappapis.html#pluginarray)
169
- * WhatWG HTML$4.4.4 - The `blockquote` element - Interface `HTMLQuoteElement`
170
- * Test: `Object.prototype.toString.call(document.createElement('blockquote'))``
171
- * - IE <=10 === "[object HTMLBlockElement]"
172
- */
173
- if ( obj instanceof globalObject . HTMLElement && obj . tagName === 'BLOCKQUOTE' ) {
174
- return 'HTMLQuoteElement' ;
175
- }
165
+ if ( windowHTMLElementExists && obj instanceof window . HTMLElement ) {
166
+ /* ! Spec Conformance
167
+ * (https://html.spec.whatwg.org/multipage/webappapis.html#pluginarray)
168
+ * WhatWG HTML$4.4.4 - The `blockquote` element - Interface `HTMLQuoteElement`
169
+ * Test: `Object.prototype.toString.call(document.createElement('blockquote'))``
170
+ * - IE <=10 === "[object HTMLBlockElement]"
171
+ */
172
+ if ( obj . tagName === 'BLOCKQUOTE' ) {
173
+ return 'HTMLQuoteElement' ;
174
+ }
176
175
177
- /* ! Spec Conformance
178
- * (https://html.spec.whatwg.org/#htmltabledatacellelement)
179
- * WhatWG HTML$4.9.9 - The `td` element - Interface `HTMLTableDataCellElement`
180
- * Note: Most browsers currently adher to the W3C DOM Level 2 spec
181
- * (https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-82915075)
182
- * which suggests that browsers should use HTMLTableCellElement for
183
- * both TD and TH elements. WhatWG separates these.
184
- * Test: Object.prototype.toString.call(document.createElement('td'))
185
- * - Chrome === "[object HTMLTableCellElement]"
186
- * - Firefox === "[object HTMLTableCellElement]"
187
- * - Safari === "[object HTMLTableCellElement]"
188
- */
189
- if ( obj instanceof globalObject . HTMLElement && obj . tagName === 'TD' ) {
190
- return 'HTMLTableDataCellElement' ;
191
- }
176
+ /* ! Spec Conformance
177
+ * (https://html.spec.whatwg.org/#htmltabledatacellelement)
178
+ * WhatWG HTML$4.9.9 - The `td` element - Interface `HTMLTableDataCellElement`
179
+ * Note: Most browsers currently adher to the W3C DOM Level 2 spec
180
+ * (https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-82915075)
181
+ * which suggests that browsers should use HTMLTableCellElement for
182
+ * both TD and TH elements. WhatWG separates these.
183
+ * Test: Object.prototype.toString.call(document.createElement('td'))
184
+ * - Chrome === "[object HTMLTableCellElement]"
185
+ * - Firefox === "[object HTMLTableCellElement]"
186
+ * - Safari === "[object HTMLTableCellElement]"
187
+ */
188
+ if ( obj . tagName === 'TD' ) {
189
+ return 'HTMLTableDataCellElement' ;
190
+ }
192
191
193
- /* ! Spec Conformance
194
- * (https://html.spec.whatwg.org/#htmltableheadercellelement)
195
- * WhatWG HTML$4.9.9 - The `td` element - Interface `HTMLTableHeaderCellElement`
196
- * Note: Most browsers currently adher to the W3C DOM Level 2 spec
197
- * (https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-82915075)
198
- * which suggests that browsers should use HTMLTableCellElement for
199
- * both TD and TH elements. WhatWG separates these.
200
- * Test: Object.prototype.toString.call(document.createElement('th'))
201
- * - Chrome === "[object HTMLTableCellElement]"
202
- * - Firefox === "[object HTMLTableCellElement]"
203
- * - Safari === "[object HTMLTableCellElement]"
204
- */
205
- if ( obj instanceof globalObject . HTMLElement && obj . tagName === 'TH' ) {
206
- return 'HTMLTableHeaderCellElement' ;
192
+ /* ! Spec Conformance
193
+ * (https://html.spec.whatwg.org/#htmltableheadercellelement)
194
+ * WhatWG HTML$4.9.9 - The `td` element - Interface `HTMLTableHeaderCellElement`
195
+ * Note: Most browsers currently adher to the W3C DOM Level 2 spec
196
+ * (https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-82915075)
197
+ * which suggests that browsers should use HTMLTableCellElement for
198
+ * both TD and TH elements. WhatWG separates these.
199
+ * Test: Object.prototype.toString.call(document.createElement('th'))
200
+ * - Chrome === "[object HTMLTableCellElement]"
201
+ * - Firefox === "[object HTMLTableCellElement]"
202
+ * - Safari === "[object HTMLTableCellElement]"
203
+ */
204
+ if ( obj . tagName === 'TH' ) {
205
+ return 'HTMLTableHeaderCellElement' ;
206
+ }
207
207
}
208
208
}
209
209
0 commit comments