Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't assume HTMLElement is in the global scope #91

Merged
merged 10 commits into from
Oct 31, 2017
27 changes: 22 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
'use strict';

/* !
* type-detect
* Copyright(c) 2013 jake luer <jake@alogicalparadox.com>
* 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 */
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, forgot to re-enable this one. Can't right now - but will get to it later.

var isDom = false;
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)
*/
if (typeof window === 'object' &&
'document' in window &&
'navigator' in window &&
'HTMLElement' in window
) {
isDom = true;
globalObject = window; // eslint-disable-line no-undef
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this line: if we have window object, it is equal to self

}

var symbolExists = typeof Symbol !== 'undefined';
var mapExists = typeof Map !== 'undefined';
var setExists = typeof Set !== 'undefined';
Expand Down Expand Up @@ -156,7 +173,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';
}

Expand All @@ -172,7 +189,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';
}

Expand All @@ -188,7 +205,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';
}
}
Expand Down