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

ref: Remove dom references from utils for old TS and env interop #2303

Merged
merged 1 commit into from Nov 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 2 additions & 4 deletions packages/utils/src/is.ts
@@ -1,5 +1,3 @@
/// <reference lib="dom" />

/**
* Checks whether given value's type is one of a few Error or Error-like
* {@link isError}.
Expand Down Expand Up @@ -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;
}
Expand All @@ -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;
}
Expand Down
21 changes: 15 additions & 6 deletions packages/utils/src/misc.ts
@@ -1,5 +1,3 @@
/// <reference lib="dom" />

import { Event, Integration, WrappedFunction } from '@sentry/types';

import { isString } from './is';
Expand Down Expand Up @@ -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 = [];
Expand All @@ -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
Expand All @@ -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;
Expand Down
34 changes: 23 additions & 11 deletions packages/utils/src/object.ts
Expand Up @@ -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 = '<unknown>';
}

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 = '<unknown>';
}

// 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;
}
}

Expand Down