clarity-js 0.8.41 → 0.8.42

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (113) hide show
  1. package/README.md +26 -26
  2. package/build/clarity.extended.js +1 -1
  3. package/build/clarity.insight.js +1 -1
  4. package/build/clarity.js +6027 -6027
  5. package/build/clarity.min.js +1 -1
  6. package/build/clarity.module.js +6027 -6027
  7. package/build/clarity.performance.js +1 -1
  8. package/package.json +70 -70
  9. package/rollup.config.ts +161 -161
  10. package/src/clarity.ts +65 -65
  11. package/src/core/api.ts +8 -8
  12. package/src/core/config.ts +29 -29
  13. package/src/core/copy.ts +3 -3
  14. package/src/core/event.ts +53 -53
  15. package/src/core/hash.ts +19 -19
  16. package/src/core/history.ts +71 -71
  17. package/src/core/index.ts +81 -81
  18. package/src/core/measure.ts +19 -19
  19. package/src/core/report.ts +28 -28
  20. package/src/core/scrub.ts +202 -202
  21. package/src/core/task.ts +181 -181
  22. package/src/core/throttle.ts +46 -46
  23. package/src/core/time.ts +26 -26
  24. package/src/core/timeout.ts +10 -10
  25. package/src/core/version.ts +2 -2
  26. package/src/data/baseline.ts +162 -162
  27. package/src/data/compress.ts +31 -31
  28. package/src/data/consent.ts +77 -77
  29. package/src/data/custom.ts +23 -23
  30. package/src/data/dimension.ts +53 -53
  31. package/src/data/encode.ts +155 -155
  32. package/src/data/envelope.ts +53 -53
  33. package/src/data/extract.ts +211 -211
  34. package/src/data/index.ts +50 -50
  35. package/src/data/limit.ts +44 -44
  36. package/src/data/metadata.ts +408 -408
  37. package/src/data/metric.ts +51 -51
  38. package/src/data/ping.ts +36 -36
  39. package/src/data/signal.ts +30 -30
  40. package/src/data/summary.ts +34 -34
  41. package/src/data/token.ts +39 -39
  42. package/src/data/upgrade.ts +44 -44
  43. package/src/data/upload.ts +333 -333
  44. package/src/data/variable.ts +83 -83
  45. package/src/diagnostic/encode.ts +40 -40
  46. package/src/diagnostic/fraud.ts +36 -36
  47. package/src/diagnostic/index.ts +13 -13
  48. package/src/diagnostic/internal.ts +28 -28
  49. package/src/diagnostic/script.ts +35 -35
  50. package/src/dynamic/agent/blank.ts +2 -2
  51. package/src/dynamic/agent/crisp.ts +40 -40
  52. package/src/dynamic/agent/encode.ts +25 -25
  53. package/src/dynamic/agent/index.ts +8 -8
  54. package/src/dynamic/agent/livechat.ts +58 -58
  55. package/src/dynamic/agent/tidio.ts +44 -44
  56. package/src/global.ts +6 -6
  57. package/src/index.ts +9 -9
  58. package/src/insight/blank.ts +14 -14
  59. package/src/insight/encode.ts +60 -60
  60. package/src/insight/snapshot.ts +114 -114
  61. package/src/interaction/change.ts +38 -38
  62. package/src/interaction/click.ts +173 -173
  63. package/src/interaction/clipboard.ts +32 -32
  64. package/src/interaction/encode.ts +210 -210
  65. package/src/interaction/index.ts +60 -60
  66. package/src/interaction/input.ts +57 -57
  67. package/src/interaction/pointer.ts +137 -137
  68. package/src/interaction/resize.ts +50 -50
  69. package/src/interaction/scroll.ts +129 -129
  70. package/src/interaction/selection.ts +66 -66
  71. package/src/interaction/submit.ts +30 -30
  72. package/src/interaction/timeline.ts +69 -69
  73. package/src/interaction/unload.ts +26 -26
  74. package/src/interaction/visibility.ts +27 -27
  75. package/src/layout/animation.ts +133 -133
  76. package/src/layout/custom.ts +42 -42
  77. package/src/layout/discover.ts +31 -31
  78. package/src/layout/document.ts +46 -46
  79. package/src/layout/dom.ts +439 -439
  80. package/src/layout/encode.ts +154 -154
  81. package/src/layout/index.ts +42 -42
  82. package/src/layout/mutation.ts +411 -411
  83. package/src/layout/node.ts +294 -294
  84. package/src/layout/offset.ts +19 -19
  85. package/src/layout/region.ts +151 -151
  86. package/src/layout/schema.ts +63 -63
  87. package/src/layout/selector.ts +82 -82
  88. package/src/layout/style.ts +159 -159
  89. package/src/layout/target.ts +32 -32
  90. package/src/layout/traverse.ts +27 -27
  91. package/src/performance/blank.ts +9 -9
  92. package/src/performance/encode.ts +31 -31
  93. package/src/performance/index.ts +12 -12
  94. package/src/performance/interaction.ts +125 -125
  95. package/src/performance/navigation.ts +31 -31
  96. package/src/performance/observer.ts +112 -112
  97. package/src/queue.ts +33 -33
  98. package/test/core.test.ts +139 -139
  99. package/test/helper.ts +162 -162
  100. package/test/html/core.html +27 -27
  101. package/test/stub.test.ts +7 -7
  102. package/test/tsconfig.test.json +5 -5
  103. package/tsconfig.json +21 -21
  104. package/tslint.json +32 -32
  105. package/types/agent.d.ts +39 -39
  106. package/types/core.d.ts +150 -150
  107. package/types/data.d.ts +571 -571
  108. package/types/diagnostic.d.ts +24 -24
  109. package/types/global.d.ts +30 -30
  110. package/types/index.d.ts +40 -40
  111. package/types/interaction.d.ts +177 -177
  112. package/types/layout.d.ts +276 -276
  113. package/types/performance.d.ts +31 -31
@@ -1,294 +1,294 @@
1
- import { Constant, Source } from "@clarity-types/layout";
2
- import { Code, Dimension, Severity } from "@clarity-types/data";
3
- import * as dom from "./dom";
4
- import * as event from "@src/core/event";
5
- import * as dimension from "@src/data/dimension";
6
- import * as internal from "@src/diagnostic/internal";
7
- import * as interaction from "@src/interaction";
8
- import * as mutation from "@src/layout/mutation";
9
- import * as schema from "@src/layout/schema";
10
- import * as custom from "@src/layout/custom";
11
- import { checkDocumentStyles } from "@src/layout/style";
12
- import { electron } from "@src/data/metadata";
13
-
14
- const IGNORE_ATTRIBUTES = ["title", "alt", "onload", "onfocus", "onerror", "data-drupal-form-submit-last", "aria-label"];
15
- const newlineRegex = /[\r\n]+/g;
16
-
17
- export default function (node: Node, source: Source, timestamp: number): Node {
18
- let child: Node = null;
19
-
20
- // Do not track this change if we are attempting to remove a node before discovering it
21
- if (source === Source.ChildListRemove && dom.has(node) === false) { return child; }
22
-
23
- // Special handling for text nodes that belong to style nodes
24
- if (source !== Source.Discover &&
25
- node.nodeType === Node.TEXT_NODE &&
26
- node.parentElement &&
27
- node.parentElement.tagName === "STYLE") {
28
- node = node.parentNode;
29
- }
30
-
31
- let add = dom.has(node) === false;
32
- let call = add ? "add" : "update";
33
- let parent = node.parentElement ? node.parentElement : null;
34
- let insideFrame = node.ownerDocument !== document;
35
- switch (node.nodeType) {
36
- case Node.DOCUMENT_TYPE_NODE:
37
- parent = insideFrame && node.parentNode ? dom.iframe(node.parentNode) : parent;
38
- let docTypePrefix = insideFrame ? Constant.IFramePrefix : Constant.Empty;
39
- let doctype = node as DocumentType;
40
- let docName = doctype.name ? doctype.name : Constant.HTML;
41
- let docAttributes = { name: docName, publicId: doctype.publicId, systemId: doctype.systemId };
42
- let docData = { tag: docTypePrefix + Constant.DocumentTag, attributes: docAttributes };
43
- dom[call](node, parent, docData, source);
44
- break;
45
- case Node.DOCUMENT_NODE:
46
- // We check for regions in the beginning when discovering document and
47
- // later whenever there are new additions or modifications to DOM (mutations)
48
- if (node === document) {
49
- dom.parse(document);
50
- }
51
- checkDocumentStyles(node as Document, timestamp);
52
- observe(node as Document);
53
- break;
54
- case Node.DOCUMENT_FRAGMENT_NODE:
55
- let shadowRoot = (node as ShadowRoot);
56
- if (shadowRoot.host) {
57
- dom.parse(shadowRoot);
58
- let type = typeof (shadowRoot.constructor);
59
- if (type === Constant.Function && shadowRoot.constructor.toString().indexOf(Constant.NativeCode) >= 0) {
60
- observe(shadowRoot);
61
-
62
- // See: https://wicg.github.io/construct-stylesheets/ for more details on adoptedStyleSheets.
63
- // At the moment, we are only able to capture "open" shadow DOM nodes. If they are closed, they are not accessible.
64
- // In future we may decide to proxy "attachShadow" call to gain access, but at the moment, we don't want to
65
- // cause any unintended side effect to the page. We will re-evaluate after we gather more real world data on this.
66
- let style = Constant.Empty as string;
67
- let fragmentData = { tag: Constant.ShadowDomTag, attributes: { style } };
68
- dom[call](node, shadowRoot.host, fragmentData, source);
69
- } else {
70
- // If the browser doesn't support shadow DOM natively, we detect that, and send appropriate tag back.
71
- // The differentiation is important because we don't have to observe pollyfill shadow DOM nodes,
72
- // the same way we observe real shadow DOM nodes (encapsulation provided by the browser).
73
- dom[call](node, shadowRoot.host, { tag: Constant.PolyfillShadowDomTag, attributes: {} }, source);
74
- }
75
- checkDocumentStyles(node as Document, timestamp);
76
- }
77
- break;
78
- case Node.TEXT_NODE:
79
- // In IE11 TEXT_NODE doesn't expose a valid parentElement property. Instead we need to lookup parentNode property.
80
- parent = parent ? parent : node.parentNode as HTMLElement;
81
- // Account for this text node only if we are tracking the parent node
82
- // We do not wish to track text nodes for ignored parent nodes, like script tags
83
- // Also, we do not track text nodes for STYLE tags
84
- // The only exception is when we receive a mutation to remove the text node, in that case
85
- // parent will be null, but we can still process the node by checking it's an update call.
86
- if (call === "update" || (parent && dom.has(parent) && parent.tagName !== "STYLE" && parent.tagName !== "NOSCRIPT")) {
87
- let textData = { tag: Constant.TextTag, value: node.nodeValue };
88
- dom[call](node, parent, textData, source);
89
- }
90
- break;
91
- case Node.ELEMENT_NODE:
92
- let element = (node as HTMLElement);
93
- let tag = element.tagName;
94
- let attributes = getAttributes(element);
95
- // In some cases, external libraries like vue-fragment, can modify parentNode property to not be in sync with the DOM
96
- // For correctness, we first look at parentElement and if it not present then fall back to using parentNode
97
- parent = node.parentElement ? node.parentElement : (node.parentNode ? node.parentNode as HTMLElement : null);
98
- // If we encounter a node that is part of SVG namespace, prefix the tag with SVG_PREFIX
99
- if (element.namespaceURI === Constant.SvgNamespace) { tag = Constant.SvgPrefix + tag; }
100
-
101
- switch (tag) {
102
- case "HTML":
103
- parent = insideFrame && parent ? dom.iframe(parent) : parent;
104
- let htmlPrefix = insideFrame ? Constant.IFramePrefix : Constant.Empty;
105
- let htmlData = { tag: htmlPrefix + tag, attributes };
106
- dom[call](node, parent, htmlData, source);
107
- break;
108
- case "SCRIPT":
109
- if (Constant.Type in attributes && attributes[Constant.Type] === Constant.JsonLD) {
110
- try {
111
- schema.ld(JSON.parse((element as HTMLScriptElement).text.replace(newlineRegex, Constant.Empty)));
112
- } catch { /* do nothing */ }
113
- }
114
- break;
115
- case "NOSCRIPT":
116
- // keeping the noscript tag but ignoring its contents. Some HTML markup relies on having these tags
117
- // to maintain parity with the original css view, but we don't want to execute any noscript in Clarity
118
- let noscriptData = { tag, attributes: {}, value: '' };
119
- dom[call](node, parent, noscriptData, source);
120
- break;
121
- case "META":
122
- var key = (Constant.Property in attributes ?
123
- Constant.Property :
124
- (Constant.Name in attributes ? Constant.Name : null));
125
- if (key && Constant.Content in attributes) {
126
- let content = attributes[Constant.Content]
127
- switch(attributes[key]) {
128
- case Constant.ogTitle:
129
- dimension.log(Dimension.MetaTitle, content)
130
- break;
131
- case Constant.ogType:
132
- dimension.log(Dimension.MetaType, content)
133
- break;
134
- case Constant.Generator:
135
- dimension.log(Dimension.Generator, content)
136
- break;
137
- }
138
- }
139
- break;
140
- case "HEAD":
141
- let head = { tag, attributes };
142
- let l = insideFrame && node.ownerDocument?.location ? node.ownerDocument.location : location;
143
- head.attributes[Constant.Base] = l.protocol + "//" + l.host + l.pathname;
144
- dom[call](node, parent, head, source);
145
- break;
146
- case "BASE":
147
- // Override the auto detected base path to explicit value specified in this tag
148
- let baseHead = dom.get(node.parentElement);
149
- if (baseHead) {
150
- // We create "a" element so we can generate protocol and hostname for relative paths like "/path/"
151
- let a = document.createElement("a");
152
- a.href = attributes["href"];
153
- baseHead.data.attributes[Constant.Base] = a.protocol + "//" + a.host + a.pathname;
154
- }
155
- break;
156
- case "STYLE":
157
- let styleData = { tag, attributes, value: getStyleValue(element as HTMLStyleElement) };
158
- dom[call](node, parent, styleData, source);
159
- break;
160
- case "IFRAME":
161
- let iframe = node as HTMLIFrameElement;
162
- let frameData = { tag, attributes };
163
- if (dom.sameorigin(iframe)) {
164
- mutation.monitor(iframe);
165
- frameData.attributes[Constant.SameOrigin] = "true";
166
- if (iframe.contentDocument && iframe.contentWindow && iframe.contentDocument.readyState !== "loading") {
167
- child = iframe.contentDocument;
168
- }
169
- }
170
- if (source === Source.ChildListRemove) {
171
- removeObserver(iframe);
172
- }
173
- dom[call](node, parent, frameData, source);
174
- break;
175
- case "LINK":
176
- // electron stylesheets reference the local file system - translating those
177
- // to inline styles so playback can work
178
- if (electron && attributes['rel'] === Constant.StyleSheet) {
179
- for (var styleSheetIndex in Object.keys(document.styleSheets)) {
180
- var currentStyleSheet = document.styleSheets[styleSheetIndex];
181
- if (currentStyleSheet.ownerNode == element) {
182
- let syntheticStyleData = { tag: "STYLE", attributes, value: getCssRules(currentStyleSheet) };
183
- dom[call](node, parent, syntheticStyleData, source);
184
- break;
185
- }
186
- }
187
- break;
188
- }
189
- // for links that aren't electron style sheets we can process them normally
190
- let linkData = { tag, attributes };
191
- dom[call](node, parent, linkData, source);
192
- break;
193
- case "VIDEO":
194
- case "AUDIO":
195
- case "SOURCE":
196
- // Ignoring any base64 src attribute for media elements to prevent big unused tokens to be sent and shock the network
197
- if (Constant.Src in attributes && attributes[Constant.Src].startsWith("data:")) {
198
- attributes[Constant.Src] = "";
199
- }
200
- let mediaTag = { tag, attributes };
201
- dom[call](node, parent, mediaTag, source);
202
- break;
203
- default:
204
- custom.check(element.localName);
205
- let data = { tag, attributes };
206
- if (element.shadowRoot) { child = element.shadowRoot; }
207
- dom[call](node, parent, data, source);
208
- break;
209
- }
210
- break;
211
- default:
212
- break;
213
- }
214
- return child;
215
- }
216
-
217
- function observe(root: Document | ShadowRoot): void {
218
- if (dom.has(root) || event.has(root)) { return; }
219
- mutation.observe(root); // Observe mutations for this root node
220
- interaction.observe(root); // Observe interactions for this root node
221
- }
222
-
223
- export function removeObserver(root: HTMLIFrameElement): void {
224
- // iframes will have load event listeners and they should be removed when iframe is removed
225
- // from the document
226
- event.unbind(root);
227
- const { doc = null, win = null } = dom.iframeContent(root) || {};
228
-
229
- if (win) {
230
- // For iframes, scroll event is observed on content window and this needs to be removed as well
231
- event.unbind(win);
232
- }
233
-
234
- if (doc) {
235
- // When an iframe is removed, we should also remove all listeners attached to its document
236
- // to avoid memory leaks.
237
- event.unbind(doc);
238
- mutation.disconnect(doc);
239
-
240
- // Remove iframe and content document from maps tracking them
241
- dom.removeIFrame(root, doc);
242
- }
243
- }
244
-
245
- function getStyleValue(style: HTMLStyleElement): string {
246
- // Call trim on the text content to ensure we do not process white spaces ( , \n, \r\n, \t, etc.)
247
- // Also, check if stylesheet has any data-* attribute, if so process rules instead of looking up text
248
- // Additionally, check if style node has an id - if so it's at a high risk to have experienced dynamic
249
- // style updates which would make the textContent out of date with its true style contribution.
250
- let value = style.textContent ? style.textContent.trim() : Constant.Empty;
251
- let dataset = style.dataset ? Object.keys(style.dataset).length : 0;
252
- if (value.length === 0 || dataset > 0 || style.id.length > 0) {
253
- value = getCssRules(style.sheet as CSSStyleSheet);
254
- }
255
- return value;
256
- }
257
-
258
- export function getCssRules(sheet: CSSStyleSheet): string {
259
- let value = Constant.Empty as string;
260
- let cssRules = null;
261
- // Firefox throws a SecurityError when trying to access cssRules of a stylesheet from a different domain
262
- try { cssRules = sheet ? sheet.cssRules : []; } catch (e) {
263
- internal.log(Code.CssRules, Severity.Warning, e ? e.name : null);
264
- if (e && e.name !== "SecurityError") { throw e; }
265
- }
266
-
267
- if (cssRules !== null) {
268
- for (let i = 0; i < cssRules.length; i++) {
269
- value += cssRules[i].cssText;
270
- }
271
- }
272
-
273
- return value;
274
- }
275
-
276
- function getAttributes(element: HTMLElement): { [key: string]: string } {
277
- let output = {};
278
- let attributes = element.attributes;
279
- if (attributes && attributes.length > 0) {
280
- for (let i = 0; i < attributes.length; i++) {
281
- let name = attributes[i].name;
282
- if (IGNORE_ATTRIBUTES.indexOf(name) < 0) {
283
- output[name] = attributes[i].value;
284
- }
285
- }
286
- }
287
-
288
- // For INPUT tags read the dynamic "value" property if an explicit "value" attribute is not set
289
- if (element.tagName === Constant.InputTag && !(Constant.Value in output) && (element as HTMLInputElement).value) {
290
- output[Constant.Value] = (element as HTMLInputElement).value;
291
- }
292
-
293
- return output;
294
- }
1
+ import { Constant, Source } from "@clarity-types/layout";
2
+ import { Code, Dimension, Severity } from "@clarity-types/data";
3
+ import * as dom from "./dom";
4
+ import * as event from "@src/core/event";
5
+ import * as dimension from "@src/data/dimension";
6
+ import * as internal from "@src/diagnostic/internal";
7
+ import * as interaction from "@src/interaction";
8
+ import * as mutation from "@src/layout/mutation";
9
+ import * as schema from "@src/layout/schema";
10
+ import * as custom from "@src/layout/custom";
11
+ import { checkDocumentStyles } from "@src/layout/style";
12
+ import { electron } from "@src/data/metadata";
13
+
14
+ const IGNORE_ATTRIBUTES = ["title", "alt", "onload", "onfocus", "onerror", "data-drupal-form-submit-last", "aria-label"];
15
+ const newlineRegex = /[\r\n]+/g;
16
+
17
+ export default function (node: Node, source: Source, timestamp: number): Node {
18
+ let child: Node = null;
19
+
20
+ // Do not track this change if we are attempting to remove a node before discovering it
21
+ if (source === Source.ChildListRemove && dom.has(node) === false) { return child; }
22
+
23
+ // Special handling for text nodes that belong to style nodes
24
+ if (source !== Source.Discover &&
25
+ node.nodeType === Node.TEXT_NODE &&
26
+ node.parentElement &&
27
+ node.parentElement.tagName === "STYLE") {
28
+ node = node.parentNode;
29
+ }
30
+
31
+ let add = dom.has(node) === false;
32
+ let call = add ? "add" : "update";
33
+ let parent = node.parentElement ? node.parentElement : null;
34
+ let insideFrame = node.ownerDocument !== document;
35
+ switch (node.nodeType) {
36
+ case Node.DOCUMENT_TYPE_NODE:
37
+ parent = insideFrame && node.parentNode ? dom.iframe(node.parentNode) : parent;
38
+ let docTypePrefix = insideFrame ? Constant.IFramePrefix : Constant.Empty;
39
+ let doctype = node as DocumentType;
40
+ let docName = doctype.name ? doctype.name : Constant.HTML;
41
+ let docAttributes = { name: docName, publicId: doctype.publicId, systemId: doctype.systemId };
42
+ let docData = { tag: docTypePrefix + Constant.DocumentTag, attributes: docAttributes };
43
+ dom[call](node, parent, docData, source);
44
+ break;
45
+ case Node.DOCUMENT_NODE:
46
+ // We check for regions in the beginning when discovering document and
47
+ // later whenever there are new additions or modifications to DOM (mutations)
48
+ if (node === document) {
49
+ dom.parse(document);
50
+ }
51
+ checkDocumentStyles(node as Document, timestamp);
52
+ observe(node as Document);
53
+ break;
54
+ case Node.DOCUMENT_FRAGMENT_NODE:
55
+ let shadowRoot = (node as ShadowRoot);
56
+ if (shadowRoot.host) {
57
+ dom.parse(shadowRoot);
58
+ let type = typeof (shadowRoot.constructor);
59
+ if (type === Constant.Function && shadowRoot.constructor.toString().indexOf(Constant.NativeCode) >= 0) {
60
+ observe(shadowRoot);
61
+
62
+ // See: https://wicg.github.io/construct-stylesheets/ for more details on adoptedStyleSheets.
63
+ // At the moment, we are only able to capture "open" shadow DOM nodes. If they are closed, they are not accessible.
64
+ // In future we may decide to proxy "attachShadow" call to gain access, but at the moment, we don't want to
65
+ // cause any unintended side effect to the page. We will re-evaluate after we gather more real world data on this.
66
+ let style = Constant.Empty as string;
67
+ let fragmentData = { tag: Constant.ShadowDomTag, attributes: { style } };
68
+ dom[call](node, shadowRoot.host, fragmentData, source);
69
+ } else {
70
+ // If the browser doesn't support shadow DOM natively, we detect that, and send appropriate tag back.
71
+ // The differentiation is important because we don't have to observe pollyfill shadow DOM nodes,
72
+ // the same way we observe real shadow DOM nodes (encapsulation provided by the browser).
73
+ dom[call](node, shadowRoot.host, { tag: Constant.PolyfillShadowDomTag, attributes: {} }, source);
74
+ }
75
+ checkDocumentStyles(node as Document, timestamp);
76
+ }
77
+ break;
78
+ case Node.TEXT_NODE:
79
+ // In IE11 TEXT_NODE doesn't expose a valid parentElement property. Instead we need to lookup parentNode property.
80
+ parent = parent ? parent : node.parentNode as HTMLElement;
81
+ // Account for this text node only if we are tracking the parent node
82
+ // We do not wish to track text nodes for ignored parent nodes, like script tags
83
+ // Also, we do not track text nodes for STYLE tags
84
+ // The only exception is when we receive a mutation to remove the text node, in that case
85
+ // parent will be null, but we can still process the node by checking it's an update call.
86
+ if (call === "update" || (parent && dom.has(parent) && parent.tagName !== "STYLE" && parent.tagName !== "NOSCRIPT")) {
87
+ let textData = { tag: Constant.TextTag, value: node.nodeValue };
88
+ dom[call](node, parent, textData, source);
89
+ }
90
+ break;
91
+ case Node.ELEMENT_NODE:
92
+ let element = (node as HTMLElement);
93
+ let tag = element.tagName;
94
+ let attributes = getAttributes(element);
95
+ // In some cases, external libraries like vue-fragment, can modify parentNode property to not be in sync with the DOM
96
+ // For correctness, we first look at parentElement and if it not present then fall back to using parentNode
97
+ parent = node.parentElement ? node.parentElement : (node.parentNode ? node.parentNode as HTMLElement : null);
98
+ // If we encounter a node that is part of SVG namespace, prefix the tag with SVG_PREFIX
99
+ if (element.namespaceURI === Constant.SvgNamespace) { tag = Constant.SvgPrefix + tag; }
100
+
101
+ switch (tag) {
102
+ case "HTML":
103
+ parent = insideFrame && parent ? dom.iframe(parent) : parent;
104
+ let htmlPrefix = insideFrame ? Constant.IFramePrefix : Constant.Empty;
105
+ let htmlData = { tag: htmlPrefix + tag, attributes };
106
+ dom[call](node, parent, htmlData, source);
107
+ break;
108
+ case "SCRIPT":
109
+ if (Constant.Type in attributes && attributes[Constant.Type] === Constant.JsonLD) {
110
+ try {
111
+ schema.ld(JSON.parse((element as HTMLScriptElement).text.replace(newlineRegex, Constant.Empty)));
112
+ } catch { /* do nothing */ }
113
+ }
114
+ break;
115
+ case "NOSCRIPT":
116
+ // keeping the noscript tag but ignoring its contents. Some HTML markup relies on having these tags
117
+ // to maintain parity with the original css view, but we don't want to execute any noscript in Clarity
118
+ let noscriptData = { tag, attributes: {}, value: '' };
119
+ dom[call](node, parent, noscriptData, source);
120
+ break;
121
+ case "META":
122
+ var key = (Constant.Property in attributes ?
123
+ Constant.Property :
124
+ (Constant.Name in attributes ? Constant.Name : null));
125
+ if (key && Constant.Content in attributes) {
126
+ let content = attributes[Constant.Content]
127
+ switch(attributes[key]) {
128
+ case Constant.ogTitle:
129
+ dimension.log(Dimension.MetaTitle, content)
130
+ break;
131
+ case Constant.ogType:
132
+ dimension.log(Dimension.MetaType, content)
133
+ break;
134
+ case Constant.Generator:
135
+ dimension.log(Dimension.Generator, content)
136
+ break;
137
+ }
138
+ }
139
+ break;
140
+ case "HEAD":
141
+ let head = { tag, attributes };
142
+ let l = insideFrame && node.ownerDocument?.location ? node.ownerDocument.location : location;
143
+ head.attributes[Constant.Base] = l.protocol + "//" + l.host + l.pathname;
144
+ dom[call](node, parent, head, source);
145
+ break;
146
+ case "BASE":
147
+ // Override the auto detected base path to explicit value specified in this tag
148
+ let baseHead = dom.get(node.parentElement);
149
+ if (baseHead) {
150
+ // We create "a" element so we can generate protocol and hostname for relative paths like "/path/"
151
+ let a = document.createElement("a");
152
+ a.href = attributes["href"];
153
+ baseHead.data.attributes[Constant.Base] = a.protocol + "//" + a.host + a.pathname;
154
+ }
155
+ break;
156
+ case "STYLE":
157
+ let styleData = { tag, attributes, value: getStyleValue(element as HTMLStyleElement) };
158
+ dom[call](node, parent, styleData, source);
159
+ break;
160
+ case "IFRAME":
161
+ let iframe = node as HTMLIFrameElement;
162
+ let frameData = { tag, attributes };
163
+ if (dom.sameorigin(iframe)) {
164
+ mutation.monitor(iframe);
165
+ frameData.attributes[Constant.SameOrigin] = "true";
166
+ if (iframe.contentDocument && iframe.contentWindow && iframe.contentDocument.readyState !== "loading") {
167
+ child = iframe.contentDocument;
168
+ }
169
+ }
170
+ if (source === Source.ChildListRemove) {
171
+ removeObserver(iframe);
172
+ }
173
+ dom[call](node, parent, frameData, source);
174
+ break;
175
+ case "LINK":
176
+ // electron stylesheets reference the local file system - translating those
177
+ // to inline styles so playback can work
178
+ if (electron && attributes['rel'] === Constant.StyleSheet) {
179
+ for (var styleSheetIndex in Object.keys(document.styleSheets)) {
180
+ var currentStyleSheet = document.styleSheets[styleSheetIndex];
181
+ if (currentStyleSheet.ownerNode == element) {
182
+ let syntheticStyleData = { tag: "STYLE", attributes, value: getCssRules(currentStyleSheet) };
183
+ dom[call](node, parent, syntheticStyleData, source);
184
+ break;
185
+ }
186
+ }
187
+ break;
188
+ }
189
+ // for links that aren't electron style sheets we can process them normally
190
+ let linkData = { tag, attributes };
191
+ dom[call](node, parent, linkData, source);
192
+ break;
193
+ case "VIDEO":
194
+ case "AUDIO":
195
+ case "SOURCE":
196
+ // Ignoring any base64 src attribute for media elements to prevent big unused tokens to be sent and shock the network
197
+ if (Constant.Src in attributes && attributes[Constant.Src].startsWith("data:")) {
198
+ attributes[Constant.Src] = "";
199
+ }
200
+ let mediaTag = { tag, attributes };
201
+ dom[call](node, parent, mediaTag, source);
202
+ break;
203
+ default:
204
+ custom.check(element.localName);
205
+ let data = { tag, attributes };
206
+ if (element.shadowRoot) { child = element.shadowRoot; }
207
+ dom[call](node, parent, data, source);
208
+ break;
209
+ }
210
+ break;
211
+ default:
212
+ break;
213
+ }
214
+ return child;
215
+ }
216
+
217
+ function observe(root: Document | ShadowRoot): void {
218
+ if (dom.has(root) || event.has(root)) { return; }
219
+ mutation.observe(root); // Observe mutations for this root node
220
+ interaction.observe(root); // Observe interactions for this root node
221
+ }
222
+
223
+ export function removeObserver(root: HTMLIFrameElement): void {
224
+ // iframes will have load event listeners and they should be removed when iframe is removed
225
+ // from the document
226
+ event.unbind(root);
227
+ const { doc = null, win = null } = dom.iframeContent(root) || {};
228
+
229
+ if (win) {
230
+ // For iframes, scroll event is observed on content window and this needs to be removed as well
231
+ event.unbind(win);
232
+ }
233
+
234
+ if (doc) {
235
+ // When an iframe is removed, we should also remove all listeners attached to its document
236
+ // to avoid memory leaks.
237
+ event.unbind(doc);
238
+ mutation.disconnect(doc);
239
+
240
+ // Remove iframe and content document from maps tracking them
241
+ dom.removeIFrame(root, doc);
242
+ }
243
+ }
244
+
245
+ function getStyleValue(style: HTMLStyleElement): string {
246
+ // Call trim on the text content to ensure we do not process white spaces ( , \n, \r\n, \t, etc.)
247
+ // Also, check if stylesheet has any data-* attribute, if so process rules instead of looking up text
248
+ // Additionally, check if style node has an id - if so it's at a high risk to have experienced dynamic
249
+ // style updates which would make the textContent out of date with its true style contribution.
250
+ let value = style.textContent ? style.textContent.trim() : Constant.Empty;
251
+ let dataset = style.dataset ? Object.keys(style.dataset).length : 0;
252
+ if (value.length === 0 || dataset > 0 || style.id.length > 0) {
253
+ value = getCssRules(style.sheet as CSSStyleSheet);
254
+ }
255
+ return value;
256
+ }
257
+
258
+ export function getCssRules(sheet: CSSStyleSheet): string {
259
+ let value = Constant.Empty as string;
260
+ let cssRules = null;
261
+ // Firefox throws a SecurityError when trying to access cssRules of a stylesheet from a different domain
262
+ try { cssRules = sheet ? sheet.cssRules : []; } catch (e) {
263
+ internal.log(Code.CssRules, Severity.Warning, e ? e.name : null);
264
+ if (e && e.name !== "SecurityError") { throw e; }
265
+ }
266
+
267
+ if (cssRules !== null) {
268
+ for (let i = 0; i < cssRules.length; i++) {
269
+ value += cssRules[i].cssText;
270
+ }
271
+ }
272
+
273
+ return value;
274
+ }
275
+
276
+ function getAttributes(element: HTMLElement): { [key: string]: string } {
277
+ let output = {};
278
+ let attributes = element.attributes;
279
+ if (attributes && attributes.length > 0) {
280
+ for (let i = 0; i < attributes.length; i++) {
281
+ let name = attributes[i].name;
282
+ if (IGNORE_ATTRIBUTES.indexOf(name) < 0) {
283
+ output[name] = attributes[i].value;
284
+ }
285
+ }
286
+ }
287
+
288
+ // For INPUT tags read the dynamic "value" property if an explicit "value" attribute is not set
289
+ if (element.tagName === Constant.InputTag && !(Constant.Value in output) && (element as HTMLInputElement).value) {
290
+ output[Constant.Value] = (element as HTMLInputElement).value;
291
+ }
292
+
293
+ return output;
294
+ }
@@ -1,19 +1,19 @@
1
- import { OffsetDistance } from "@clarity-types/core";
2
- import { iframe } from "@src/layout/dom";
3
-
4
- export function offset(element: HTMLElement): OffsetDistance {
5
- let output: OffsetDistance = { x: 0, y: 0 };
6
-
7
- // Walk up the chain to ensure we compute offset distance correctly
8
- // In case where we may have nested IFRAMEs, we keep walking up until we get to the top most parent page
9
- if (element && element.offsetParent) {
10
- do {
11
- let parent = element.offsetParent as HTMLElement;
12
- let frame = parent === null ? iframe(element.ownerDocument) : null;
13
- output.x += element.offsetLeft;
14
- output.y += element.offsetTop;
15
- element = frame ? frame : parent;
16
- } while (element);
17
- }
18
- return output;
19
- }
1
+ import { OffsetDistance } from "@clarity-types/core";
2
+ import { iframe } from "@src/layout/dom";
3
+
4
+ export function offset(element: HTMLElement): OffsetDistance {
5
+ let output: OffsetDistance = { x: 0, y: 0 };
6
+
7
+ // Walk up the chain to ensure we compute offset distance correctly
8
+ // In case where we may have nested IFRAMEs, we keep walking up until we get to the top most parent page
9
+ if (element && element.offsetParent) {
10
+ do {
11
+ let parent = element.offsetParent as HTMLElement;
12
+ let frame = parent === null ? iframe(element.ownerDocument) : null;
13
+ output.x += element.offsetLeft;
14
+ output.y += element.offsetTop;
15
+ element = frame ? frame : parent;
16
+ } while (element);
17
+ }
18
+ return output;
19
+ }