|
1 | 1 | import { captureException, getCurrentHub, withScope } from '@sentry/core'; |
2 | 2 | import { Event as SentryEvent, Mechanism, WrappedFunction } from '@sentry/types'; |
3 | | -import { addExceptionMechanism, addExceptionTypeValue, isString, normalize } from '@sentry/utils'; |
| 3 | +import { addExceptionMechanism, addExceptionTypeValue, htmlTreeAsString, normalize } from '@sentry/utils'; |
4 | 4 |
|
5 | 5 | const debounceDuration: number = 1000; |
6 | 6 | let keypressTimeout: number | undefined; |
@@ -188,17 +188,7 @@ export function breadcrumbEventHandler(eventName: string, debounce: boolean = fa |
188 | 188 | lastCapturedEvent = event; |
189 | 189 |
|
190 | 190 | const captureBreadcrumb = () => { |
191 | | - // try/catch both: |
192 | | - // - accessing event.target (see getsentry/raven-js#838, #768) |
193 | | - // - `htmlTreeAsString` because it's complex, and just accessing the DOM incorrectly |
194 | | - // can throw an exception in some circumstances. |
195 | | - let target; |
196 | | - try { |
197 | | - target = event.target ? _htmlTreeAsString(event.target as Node) : _htmlTreeAsString((event as unknown) as Node); |
198 | | - } catch (e) { |
199 | | - target = '<unknown>'; |
200 | | - } |
201 | | - |
| 191 | + const target = htmlTreeAsString(event.target as Node); |
202 | 192 | if (target.length === 0) { |
203 | 193 | return; |
204 | 194 | } |
@@ -268,79 +258,3 @@ export function keypressEventHandler(): (event: Event) => void { |
268 | 258 | }, debounceDuration) as any) as number; |
269 | 259 | }; |
270 | 260 | } |
271 | | - |
272 | | -/** |
273 | | - * Given a child DOM element, returns a query-selector statement describing that |
274 | | - * and its ancestors |
275 | | - * e.g. [HTMLElement] => body > div > input#foo.btn[name=baz] |
276 | | - * @returns generated DOM path |
277 | | - */ |
278 | | -function _htmlTreeAsString(elem: Node): string { |
279 | | - let currentElem: Node | null = elem; |
280 | | - const MAX_TRAVERSE_HEIGHT = 5; |
281 | | - const MAX_OUTPUT_LEN = 80; |
282 | | - const out = []; |
283 | | - let height = 0; |
284 | | - let len = 0; |
285 | | - const separator = ' > '; |
286 | | - const sepLength = separator.length; |
287 | | - let nextStr; |
288 | | - |
289 | | - while (currentElem && height++ < MAX_TRAVERSE_HEIGHT) { |
290 | | - nextStr = _htmlElementAsString(currentElem as HTMLElement); |
291 | | - // bail out if |
292 | | - // - nextStr is the 'html' element |
293 | | - // - the length of the string that would be created exceeds MAX_OUTPUT_LEN |
294 | | - // (ignore this limit if we are on the first iteration) |
295 | | - if (nextStr === 'html' || (height > 1 && len + out.length * sepLength + nextStr.length >= MAX_OUTPUT_LEN)) { |
296 | | - break; |
297 | | - } |
298 | | - |
299 | | - out.push(nextStr); |
300 | | - |
301 | | - len += nextStr.length; |
302 | | - currentElem = currentElem.parentNode; |
303 | | - } |
304 | | - |
305 | | - return out.reverse().join(separator); |
306 | | -} |
307 | | - |
308 | | -/** |
309 | | - * Returns a simple, query-selector representation of a DOM element |
310 | | - * e.g. [HTMLElement] => input#foo.btn[name=baz] |
311 | | - * @returns generated DOM path |
312 | | - */ |
313 | | -function _htmlElementAsString(elem: HTMLElement): string { |
314 | | - const out = []; |
315 | | - let className; |
316 | | - let classes; |
317 | | - let key; |
318 | | - let attr; |
319 | | - let i; |
320 | | - |
321 | | - if (!elem || !elem.tagName) { |
322 | | - return ''; |
323 | | - } |
324 | | - |
325 | | - out.push(elem.tagName.toLowerCase()); |
326 | | - if (elem.id) { |
327 | | - out.push(`#${elem.id}`); |
328 | | - } |
329 | | - |
330 | | - className = elem.className; |
331 | | - if (className && isString(className)) { |
332 | | - classes = className.split(/\s+/); |
333 | | - for (i = 0; i < classes.length; i++) { |
334 | | - out.push(`.${classes[i]}`); |
335 | | - } |
336 | | - } |
337 | | - const attrWhitelist = ['type', 'name', 'title', 'alt']; |
338 | | - for (i = 0; i < attrWhitelist.length; i++) { |
339 | | - key = attrWhitelist[i]; |
340 | | - attr = elem.getAttribute(key); |
341 | | - if (attr) { |
342 | | - out.push(`[${key}="${attr}"]`); |
343 | | - } |
344 | | - } |
345 | | - return out.join(''); |
346 | | -} |
0 commit comments