Skip to content

Commit e880682

Browse files
committed
ref: Remove dom references from utils for old TS and env interop
1 parent 76ee66c commit e880682

File tree

3 files changed

+40
-21
lines changed

3 files changed

+40
-21
lines changed

packages/utils/src/is.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/// <reference lib="dom" />
2-
31
/**
42
* Checks whether given value's type is one of a few Error or Error-like
53
* {@link isError}.
@@ -93,7 +91,7 @@ export function isPlainObject(wat: any): boolean {
9391
* @param wat A value to be checked.
9492
* @returns A boolean representing the result.
9593
*/
96-
export function isEvent(wat: any): wat is Event {
94+
export function isEvent(wat: any): boolean {
9795
// tslint:disable-next-line:strict-type-predicates
9896
return typeof Event !== 'undefined' && wat instanceof Event;
9997
}
@@ -105,7 +103,7 @@ export function isEvent(wat: any): wat is Event {
105103
* @param wat A value to be checked.
106104
* @returns A boolean representing the result.
107105
*/
108-
export function isElement(wat: any): wat is Element {
106+
export function isElement(wat: any): boolean {
109107
// tslint:disable-next-line:strict-type-predicates
110108
return typeof Element !== 'undefined' && wat instanceof Element;
111109
}

packages/utils/src/misc.ts

+15-6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
/// <reference lib="dom" />
2-
31
import { Event, Integration, WrappedFunction } from '@sentry/types';
42

53
import { isString } from './is';
@@ -258,13 +256,17 @@ export function getLocationHref(): string {
258256
* e.g. [HTMLElement] => body > div > input#foo.btn[name=baz]
259257
* @returns generated DOM path
260258
*/
261-
export function htmlTreeAsString(elem: Node): string {
259+
export function htmlTreeAsString(elem: unknown): string {
260+
type SimpleNode = {
261+
parentNode: SimpleNode;
262+
} | null;
263+
262264
// try/catch both:
263265
// - accessing event.target (see getsentry/raven-js#838, #768)
264266
// - `htmlTreeAsString` because it's complex, and just accessing the DOM incorrectly
265267
// - can throw an exception in some circumstances.
266268
try {
267-
let currentElem: Node | null = elem;
269+
let currentElem = elem as SimpleNode;
268270
const MAX_TRAVERSE_HEIGHT = 5;
269271
const MAX_OUTPUT_LEN = 80;
270272
const out = [];
@@ -275,7 +277,7 @@ export function htmlTreeAsString(elem: Node): string {
275277
let nextStr;
276278

277279
while (currentElem && height++ < MAX_TRAVERSE_HEIGHT) {
278-
nextStr = _htmlElementAsString(currentElem as HTMLElement);
280+
nextStr = _htmlElementAsString(currentElem);
279281
// bail out if
280282
// - nextStr is the 'html' element
281283
// - the length of the string that would be created exceeds MAX_OUTPUT_LEN
@@ -301,7 +303,14 @@ export function htmlTreeAsString(elem: Node): string {
301303
* e.g. [HTMLElement] => input#foo.btn[name=baz]
302304
* @returns generated DOM path
303305
*/
304-
function _htmlElementAsString(elem: HTMLElement): string {
306+
function _htmlElementAsString(el: unknown): string {
307+
const elem = el as {
308+
getAttribute(key: string): string; // tslint:disable-line:completed-docs
309+
tagName?: string;
310+
id?: string;
311+
className?: string;
312+
};
313+
305314
const out = [];
306315
let className;
307316
let classes;

packages/utils/src/object.ts

+23-11
Original file line numberDiff line numberDiff line change
@@ -99,37 +99,49 @@ function getWalkSource(
9999
}
100100

101101
if (isEvent(value)) {
102+
/**
103+
* Event-like interface that's usable in browser and node
104+
*/
105+
interface SimpleEvent {
106+
[key: string]: unknown;
107+
type: string;
108+
target?: unknown;
109+
currentTarget?: unknown;
110+
}
111+
112+
const event = value as SimpleEvent;
113+
102114
const source: {
103115
[key: string]: any;
104116
} = {};
105117

106-
source.type = value.type;
118+
source.type = event.type;
107119

108120
// Accessing event.target can throw (see getsentry/raven-js#838, #768)
109121
try {
110-
source.target = isElement(value.target)
111-
? htmlTreeAsString(value.target)
112-
: Object.prototype.toString.call(value.target);
122+
source.target = isElement(event.target)
123+
? htmlTreeAsString(event.target)
124+
: Object.prototype.toString.call(event.target);
113125
} catch (_oO) {
114126
source.target = '<unknown>';
115127
}
116128

117129
try {
118-
source.currentTarget = isElement(value.currentTarget)
119-
? htmlTreeAsString(value.currentTarget)
120-
: Object.prototype.toString.call(value.currentTarget);
130+
source.currentTarget = isElement(event.currentTarget)
131+
? htmlTreeAsString(event.currentTarget)
132+
: Object.prototype.toString.call(event.currentTarget);
121133
} catch (_oO) {
122134
source.currentTarget = '<unknown>';
123135
}
124136

125137
// tslint:disable-next-line:strict-type-predicates
126138
if (typeof CustomEvent !== 'undefined' && value instanceof CustomEvent) {
127-
source.detail = value.detail;
139+
source.detail = event.detail;
128140
}
129141

130-
for (const i in value) {
131-
if (Object.prototype.hasOwnProperty.call(value, i)) {
132-
source[i] = (value as { [key: string]: any })[i];
142+
for (const i in event) {
143+
if (Object.prototype.hasOwnProperty.call(event, i)) {
144+
source[i] = event;
133145
}
134146
}
135147

0 commit comments

Comments
 (0)