From e8806821e06f902b38338dc59f7f4e440238686e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kamil=20Og=C3=B3rek?= Date: Fri, 8 Nov 2019 13:54:19 +0100 Subject: [PATCH] ref: Remove dom references from utils for old TS and env interop --- packages/utils/src/is.ts | 6 ++---- packages/utils/src/misc.ts | 21 +++++++++++++++------ packages/utils/src/object.ts | 34 +++++++++++++++++++++++----------- 3 files changed, 40 insertions(+), 21 deletions(-) diff --git a/packages/utils/src/is.ts b/packages/utils/src/is.ts index 5613be4da7f9..fc2b5daa0218 100644 --- a/packages/utils/src/is.ts +++ b/packages/utils/src/is.ts @@ -1,5 +1,3 @@ -/// - /** * Checks whether given value's type is one of a few Error or Error-like * {@link isError}. @@ -93,7 +91,7 @@ export function isPlainObject(wat: any): boolean { * @param wat A value to be checked. * @returns A boolean representing the result. */ -export function isEvent(wat: any): wat is Event { +export function isEvent(wat: any): boolean { // tslint:disable-next-line:strict-type-predicates return typeof Event !== 'undefined' && wat instanceof Event; } @@ -105,7 +103,7 @@ export function isEvent(wat: any): wat is Event { * @param wat A value to be checked. * @returns A boolean representing the result. */ -export function isElement(wat: any): wat is Element { +export function isElement(wat: any): boolean { // tslint:disable-next-line:strict-type-predicates return typeof Element !== 'undefined' && wat instanceof Element; } diff --git a/packages/utils/src/misc.ts b/packages/utils/src/misc.ts index d6b3f35a9546..75ef589d1918 100644 --- a/packages/utils/src/misc.ts +++ b/packages/utils/src/misc.ts @@ -1,5 +1,3 @@ -/// - import { Event, Integration, WrappedFunction } from '@sentry/types'; import { isString } from './is'; @@ -258,13 +256,17 @@ export function getLocationHref(): string { * e.g. [HTMLElement] => body > div > input#foo.btn[name=baz] * @returns generated DOM path */ -export function htmlTreeAsString(elem: Node): string { +export function htmlTreeAsString(elem: unknown): string { + type SimpleNode = { + parentNode: SimpleNode; + } | null; + // try/catch both: // - accessing event.target (see getsentry/raven-js#838, #768) // - `htmlTreeAsString` because it's complex, and just accessing the DOM incorrectly // - can throw an exception in some circumstances. try { - let currentElem: Node | null = elem; + let currentElem = elem as SimpleNode; const MAX_TRAVERSE_HEIGHT = 5; const MAX_OUTPUT_LEN = 80; const out = []; @@ -275,7 +277,7 @@ export function htmlTreeAsString(elem: Node): string { let nextStr; while (currentElem && height++ < MAX_TRAVERSE_HEIGHT) { - nextStr = _htmlElementAsString(currentElem as HTMLElement); + nextStr = _htmlElementAsString(currentElem); // bail out if // - nextStr is the 'html' element // - the length of the string that would be created exceeds MAX_OUTPUT_LEN @@ -301,7 +303,14 @@ export function htmlTreeAsString(elem: Node): string { * e.g. [HTMLElement] => input#foo.btn[name=baz] * @returns generated DOM path */ -function _htmlElementAsString(elem: HTMLElement): string { +function _htmlElementAsString(el: unknown): string { + const elem = el as { + getAttribute(key: string): string; // tslint:disable-line:completed-docs + tagName?: string; + id?: string; + className?: string; + }; + const out = []; let className; let classes; diff --git a/packages/utils/src/object.ts b/packages/utils/src/object.ts index 7ed7e59a6c84..ea2d23a36ab9 100644 --- a/packages/utils/src/object.ts +++ b/packages/utils/src/object.ts @@ -99,37 +99,49 @@ function getWalkSource( } if (isEvent(value)) { + /** + * Event-like interface that's usable in browser and node + */ + interface SimpleEvent { + [key: string]: unknown; + type: string; + target?: unknown; + currentTarget?: unknown; + } + + const event = value as SimpleEvent; + const source: { [key: string]: any; } = {}; - source.type = value.type; + source.type = event.type; // Accessing event.target can throw (see getsentry/raven-js#838, #768) try { - source.target = isElement(value.target) - ? htmlTreeAsString(value.target) - : Object.prototype.toString.call(value.target); + source.target = isElement(event.target) + ? htmlTreeAsString(event.target) + : Object.prototype.toString.call(event.target); } catch (_oO) { source.target = ''; } try { - source.currentTarget = isElement(value.currentTarget) - ? htmlTreeAsString(value.currentTarget) - : Object.prototype.toString.call(value.currentTarget); + source.currentTarget = isElement(event.currentTarget) + ? htmlTreeAsString(event.currentTarget) + : Object.prototype.toString.call(event.currentTarget); } catch (_oO) { source.currentTarget = ''; } // tslint:disable-next-line:strict-type-predicates if (typeof CustomEvent !== 'undefined' && value instanceof CustomEvent) { - source.detail = value.detail; + source.detail = event.detail; } - for (const i in value) { - if (Object.prototype.hasOwnProperty.call(value, i)) { - source[i] = (value as { [key: string]: any })[i]; + for (const i in event) { + if (Object.prototype.hasOwnProperty.call(event, i)) { + source[i] = event; } }