MCPcopy Index your code
hub / github.com/getsentry/sentry-javascript / htmlTreeAsString

Function htmlTreeAsString

packages/core/src/utils/browser.ts:20–64  ·  view source on GitHub ↗
(
  elem: unknown,
  options: string[] | { keyAttrs?: string[]; maxStringLength?: number } = {},
)

Source from the content-addressed store, hash-verified

18 * Import `htmlTreeAsString` from `@sentry/browser-utils` instead.
19 */
20export function htmlTreeAsString(
21 elem: unknown,
22 options: string[] | { keyAttrs?: string[]; maxStringLength?: number } = {},
23): string {
24 if (!elem) {
25 return '<unknown>';
26 }
27
28 // try/catch both:
29 // - accessing event.target (see getsentry/raven-js#838, #768)
30 // - `htmlTreeAsString` because it's complex, and just accessing the DOM incorrectly
31 // - can throw an exception in some circumstances.
32 try {
33 let currentElem = elem as SimpleNode;
34 const MAX_TRAVERSE_HEIGHT = 5;
35 const out = [];
36 let height = 0;
37 let len = 0;
38 const separator = ' > ';
39 const sepLength = separator.length;
40 let nextStr;
41 const keyAttrs = Array.isArray(options) ? options : options.keyAttrs;
42 const maxStringLength = (!Array.isArray(options) && options.maxStringLength) || DEFAULT_MAX_STRING_LENGTH;
43
44 while (currentElem && height++ < MAX_TRAVERSE_HEIGHT) {
45 nextStr = _htmlElementAsString(currentElem, keyAttrs);
46 // bail out if
47 // - nextStr is the 'html' element
48 // - the length of the string that would be created exceeds maxStringLength
49 // (ignore this limit if we are on the first iteration)
50 if (nextStr === 'html' || (height > 1 && len + out.length * sepLength + nextStr.length >= maxStringLength)) {
51 break;
52 }
53
54 out.push(nextStr);
55
56 len += nextStr.length;
57 currentElem = currentElem.parentNode;
58 }
59
60 return out.reverse().join(separator);
61 } catch {
62 return '<unknown>';
63 }
64}
65
66/**
67 * Returns a simple, query-selector representation of a DOM element

Callers 1

browser.test.tsFile · 0.90

Calls 2

pushMethod · 0.80
_htmlElementAsStringFunction · 0.70

Tested by

no test coverage detected