@sentry/browser
Version:
Official Sentry SDK for browsers
31 lines (30 loc) • 1.38 kB
JavaScript
import { htmlTreeAsString, isElement } from "@sentry/browser-utils";
// src/normalizeStringifyValue.ts
const HTML_ELEMENT_CONSTRUCTOR_NAME_REGEX = /^HTML(\w*)Element$/;
/**
* Browser-specific contributions to `normalize()`'s `stringifyValue`. Plug into
* `setNormalizeStringifier` from the browser SDK so DOM values get a useful string
* representation without forcing core to carry the DOM-specific code.
*
* Handles:
* - `window` → `[Window]`
* - `document` → `[Document]`
* - `HTMLElement` subclasses → `[HTMLElement: <css-selector-path>]` (via `htmlTreeAsString`)
*
* Vue ViewModels and React SyntheticEvents are not handled here — the Vue and React
* SDKs wrap this function in their `init` and add their own checks on top.
*/
function normalizeStringifyValue(value) {
if (typeof window !== "undefined" && value === window) return "[Window]";
if (typeof document !== "undefined" && value === document) return "[Document]";
if (isElement(value)) {
const objName = getConstructorName(value);
if (HTML_ELEMENT_CONSTRUCTOR_NAME_REGEX.test(objName)) return `[HTMLElement: ${htmlTreeAsString(value)}]`;
}
}
function getConstructorName(value) {
const prototype = Object.getPrototypeOf(value);
return prototype?.constructor ? prototype.constructor.name : "null prototype";
}
export { normalizeStringifyValue };
//# sourceMappingURL=normalizeStringifyValue.js.map