clarity-js 0.8.42 → 0.8.43

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 (114) 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 +6043 -6030
  5. package/build/clarity.min.js +1 -1
  6. package/build/clarity.module.js +6043 -6030
  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/dynamic.ts +13 -7
  15. package/src/core/event.ts +53 -53
  16. package/src/core/hash.ts +19 -19
  17. package/src/core/history.ts +71 -71
  18. package/src/core/index.ts +81 -81
  19. package/src/core/measure.ts +19 -19
  20. package/src/core/report.ts +28 -28
  21. package/src/core/scrub.ts +204 -202
  22. package/src/core/task.ts +181 -181
  23. package/src/core/throttle.ts +46 -46
  24. package/src/core/time.ts +26 -26
  25. package/src/core/timeout.ts +10 -10
  26. package/src/core/version.ts +2 -2
  27. package/src/data/baseline.ts +162 -162
  28. package/src/data/compress.ts +31 -31
  29. package/src/data/consent.ts +77 -77
  30. package/src/data/custom.ts +23 -23
  31. package/src/data/dimension.ts +53 -53
  32. package/src/data/encode.ts +155 -155
  33. package/src/data/envelope.ts +53 -53
  34. package/src/data/extract.ts +211 -211
  35. package/src/data/index.ts +50 -50
  36. package/src/data/limit.ts +44 -44
  37. package/src/data/metadata.ts +411 -408
  38. package/src/data/metric.ts +51 -51
  39. package/src/data/ping.ts +36 -36
  40. package/src/data/signal.ts +30 -30
  41. package/src/data/summary.ts +34 -34
  42. package/src/data/token.ts +39 -39
  43. package/src/data/upgrade.ts +44 -44
  44. package/src/data/upload.ts +333 -333
  45. package/src/data/variable.ts +83 -83
  46. package/src/diagnostic/encode.ts +40 -40
  47. package/src/diagnostic/fraud.ts +36 -36
  48. package/src/diagnostic/index.ts +13 -13
  49. package/src/diagnostic/internal.ts +28 -28
  50. package/src/diagnostic/script.ts +35 -35
  51. package/src/dynamic/agent/blank.ts +2 -2
  52. package/src/dynamic/agent/crisp.ts +40 -40
  53. package/src/dynamic/agent/encode.ts +25 -25
  54. package/src/dynamic/agent/index.ts +8 -8
  55. package/src/dynamic/agent/livechat.ts +58 -58
  56. package/src/dynamic/agent/tidio.ts +44 -44
  57. package/src/global.ts +6 -6
  58. package/src/index.ts +9 -9
  59. package/src/insight/blank.ts +14 -14
  60. package/src/insight/encode.ts +60 -60
  61. package/src/insight/snapshot.ts +114 -114
  62. package/src/interaction/change.ts +38 -38
  63. package/src/interaction/click.ts +173 -173
  64. package/src/interaction/clipboard.ts +32 -32
  65. package/src/interaction/encode.ts +210 -210
  66. package/src/interaction/index.ts +60 -60
  67. package/src/interaction/input.ts +57 -57
  68. package/src/interaction/pointer.ts +137 -137
  69. package/src/interaction/resize.ts +50 -50
  70. package/src/interaction/scroll.ts +129 -129
  71. package/src/interaction/selection.ts +66 -66
  72. package/src/interaction/submit.ts +30 -30
  73. package/src/interaction/timeline.ts +69 -69
  74. package/src/interaction/unload.ts +26 -26
  75. package/src/interaction/visibility.ts +27 -27
  76. package/src/layout/animation.ts +133 -133
  77. package/src/layout/custom.ts +42 -42
  78. package/src/layout/discover.ts +31 -31
  79. package/src/layout/document.ts +46 -46
  80. package/src/layout/dom.ts +439 -439
  81. package/src/layout/encode.ts +154 -154
  82. package/src/layout/index.ts +42 -42
  83. package/src/layout/mutation.ts +411 -411
  84. package/src/layout/node.ts +294 -294
  85. package/src/layout/offset.ts +19 -19
  86. package/src/layout/region.ts +151 -151
  87. package/src/layout/schema.ts +63 -63
  88. package/src/layout/selector.ts +82 -82
  89. package/src/layout/style.ts +159 -159
  90. package/src/layout/target.ts +32 -32
  91. package/src/layout/traverse.ts +27 -27
  92. package/src/performance/blank.ts +9 -9
  93. package/src/performance/encode.ts +31 -31
  94. package/src/performance/index.ts +12 -12
  95. package/src/performance/interaction.ts +125 -125
  96. package/src/performance/navigation.ts +31 -31
  97. package/src/performance/observer.ts +112 -112
  98. package/src/queue.ts +33 -33
  99. package/test/core.test.ts +139 -139
  100. package/test/helper.ts +162 -162
  101. package/test/html/core.html +27 -27
  102. package/test/stub.test.ts +7 -7
  103. package/test/tsconfig.test.json +5 -5
  104. package/tsconfig.json +21 -21
  105. package/tslint.json +32 -32
  106. package/types/agent.d.ts +39 -39
  107. package/types/core.d.ts +150 -150
  108. package/types/data.d.ts +572 -571
  109. package/types/diagnostic.d.ts +24 -24
  110. package/types/global.d.ts +30 -30
  111. package/types/index.d.ts +40 -40
  112. package/types/interaction.d.ts +177 -177
  113. package/types/layout.d.ts +276 -276
  114. package/types/performance.d.ts +31 -31
@@ -1,58 +1,58 @@
1
- import {
2
- Action,
3
- Visibility,
4
- Availability,
5
- VisibilityEvent,
6
- AvailabilityEvent,
7
- NewEvent,
8
- AuthorType,
9
- } from "@clarity-types/agent";
10
- import encode from "./encode";
11
-
12
- function visible(event: VisibilityEvent): void {
13
- const visibility = event.visibility;
14
- switch (visibility) {
15
- case Visibility.Maximized:
16
- encode(Action.AgentMaximized);
17
- break;
18
- case Visibility.Minimized:
19
- encode(Action.AgentMinimized);
20
- break;
21
- case Visibility.Hidden:
22
- encode(Action.AgentHidden);
23
- break;
24
- }
25
- }
26
-
27
- function avail(event: AvailabilityEvent): void {
28
- const isOnline = event.availability === Availability.Online;
29
- encode(isOnline ? Action.AgentOnline : Action.AgentOffline);
30
- }
31
-
32
- function message(event: NewEvent): void {
33
- if (event.author.type === AuthorType.Agent) {
34
- encode(Action.AgentMessage);
35
- } else {
36
- encode(Action.HumanMessage);
37
- }
38
- }
39
-
40
- export function start(): void {
41
- if (window.LiveChatWidget) {
42
- window.LiveChatWidget.on("visibility_changed", visible);
43
- window.LiveChatWidget.on("new_event", message);
44
- window.LiveChatWidget.on("availability_changed", avail);
45
- }
46
- // Register stop callback with main Clarity
47
- if (typeof window !== "undefined" && (window as any).clarity) {
48
- (window as any).clarity("register", stop);
49
- }
50
- }
51
-
52
- export function stop(): void {
53
- if (window.LiveChatWidget) {
54
- window.LiveChatWidget.off("visibility_changed", visible);
55
- window.LiveChatWidget.off("availability_changed", avail);
56
- window.LiveChatWidget.off("new_event", message);
57
- }
58
- }
1
+ import {
2
+ Action,
3
+ Visibility,
4
+ Availability,
5
+ VisibilityEvent,
6
+ AvailabilityEvent,
7
+ NewEvent,
8
+ AuthorType,
9
+ } from "@clarity-types/agent";
10
+ import encode from "./encode";
11
+
12
+ function visible(event: VisibilityEvent): void {
13
+ const visibility = event.visibility;
14
+ switch (visibility) {
15
+ case Visibility.Maximized:
16
+ encode(Action.AgentMaximized);
17
+ break;
18
+ case Visibility.Minimized:
19
+ encode(Action.AgentMinimized);
20
+ break;
21
+ case Visibility.Hidden:
22
+ encode(Action.AgentHidden);
23
+ break;
24
+ }
25
+ }
26
+
27
+ function avail(event: AvailabilityEvent): void {
28
+ const isOnline = event.availability === Availability.Online;
29
+ encode(isOnline ? Action.AgentOnline : Action.AgentOffline);
30
+ }
31
+
32
+ function message(event: NewEvent): void {
33
+ if (event.author.type === AuthorType.Agent) {
34
+ encode(Action.AgentMessage);
35
+ } else {
36
+ encode(Action.HumanMessage);
37
+ }
38
+ }
39
+
40
+ export function start(): void {
41
+ if (window.LiveChatWidget) {
42
+ window.LiveChatWidget.on("visibility_changed", visible);
43
+ window.LiveChatWidget.on("new_event", message);
44
+ window.LiveChatWidget.on("availability_changed", avail);
45
+ }
46
+ // Register stop callback with main Clarity
47
+ if (typeof window !== "undefined" && (window as any).clarity) {
48
+ (window as any).clarity("register", stop);
49
+ }
50
+ }
51
+
52
+ export function stop(): void {
53
+ if (window.LiveChatWidget) {
54
+ window.LiveChatWidget.off("visibility_changed", visible);
55
+ window.LiveChatWidget.off("availability_changed", avail);
56
+ window.LiveChatWidget.off("new_event", message);
57
+ }
58
+ }
@@ -1,44 +1,44 @@
1
- import { Action } from "@clarity-types/agent";
2
- import encode from "./encode";
3
-
4
- let isActive = false;
5
-
6
- function open() {
7
- if (!isActive) return;
8
- encode(Action.AgentMaximized);
9
- }
10
-
11
- function close() {
12
- if (!isActive) return;
13
- encode(Action.AgentMinimized);
14
- }
15
-
16
- function human() {
17
- if (!isActive) return;
18
- encode(Action.HumanMessage);
19
- }
20
-
21
- function agent() {
22
- if (!isActive) return;
23
- encode(Action.AgentMessage);
24
- }
25
-
26
- export function start(): void {
27
- if (window.tidioChatApi) {
28
- window.tidioChatApi.on("open", open);
29
- window.tidioChatApi.on("close", close);
30
- window.tidioChatApi.on("messageFromVisitor", human);
31
- window.tidioChatApi.on("messageFromOperator", agent);
32
- isActive = true;
33
- }
34
- // Register stop callback with main Clarity
35
- if (typeof window !== "undefined" && (window as any).clarity) {
36
- (window as any).clarity("register", stop);
37
- }
38
- }
39
-
40
- export function stop(): void {
41
- if (window.tidioChatApi) {
42
- isActive = false;
43
- }
44
- }
1
+ import { Action } from "@clarity-types/agent";
2
+ import encode from "./encode";
3
+
4
+ let isActive = false;
5
+
6
+ function open() {
7
+ if (!isActive) return;
8
+ encode(Action.AgentMaximized);
9
+ }
10
+
11
+ function close() {
12
+ if (!isActive) return;
13
+ encode(Action.AgentMinimized);
14
+ }
15
+
16
+ function human() {
17
+ if (!isActive) return;
18
+ encode(Action.HumanMessage);
19
+ }
20
+
21
+ function agent() {
22
+ if (!isActive) return;
23
+ encode(Action.AgentMessage);
24
+ }
25
+
26
+ export function start(): void {
27
+ if (window.tidioChatApi) {
28
+ window.tidioChatApi.on("open", open);
29
+ window.tidioChatApi.on("close", close);
30
+ window.tidioChatApi.on("messageFromVisitor", human);
31
+ window.tidioChatApi.on("messageFromOperator", agent);
32
+ isActive = true;
33
+ }
34
+ // Register stop callback with main Clarity
35
+ if (typeof window !== "undefined" && (window as any).clarity) {
36
+ (window as any).clarity("register", stop);
37
+ }
38
+ }
39
+
40
+ export function stop(): void {
41
+ if (window.tidioChatApi) {
42
+ isActive = false;
43
+ }
44
+ }
package/src/global.ts CHANGED
@@ -1,6 +1,6 @@
1
- import * as queue from "@src/queue";
2
-
3
- // Process anything that was queued up before the script loaded
4
- (function(): void {
5
- queue.process();
6
- })();
1
+ import * as queue from "@src/queue";
2
+
3
+ // Process anything that was queued up before the script loaded
4
+ (function(): void {
5
+ queue.process();
6
+ })();
package/src/index.ts CHANGED
@@ -1,9 +1,9 @@
1
- import * as clarity from "./clarity";
2
- import hash from "./core/hash";
3
- import * as selector from "./layout/selector";
4
- import { get, getNode, lookup } from "./layout/dom";
5
-
6
- const helper = { hash, selector, get, getNode, lookup }
7
- const version = clarity.version;
8
-
9
- export { clarity, version, helper };
1
+ import * as clarity from "./clarity";
2
+ import hash from "./core/hash";
3
+ import * as selector from "./layout/selector";
4
+ import { get, getNode, lookup } from "./layout/dom";
5
+
6
+ const helper = { hash, selector, get, getNode, lookup }
7
+ const version = clarity.version;
8
+
9
+ export { clarity, version, helper };
@@ -1,14 +1,14 @@
1
- export let state = [];
2
- export let sheetAdoptionState = [];
3
- export let sheetUpdateState = [];
4
- export let data = null;
5
-
6
- /* Intentionally blank module with empty code */
7
-
8
- export function start(): void {}
9
- export function reset(): void {}
10
- export function stop(): void {}
11
- export function log(): void {}
12
- export function observe(): void {}
13
- export function compute(): void {}
14
- export function checkDocumentStyles(_d: Document): void {}
1
+ export let state = [];
2
+ export let sheetAdoptionState = [];
3
+ export let sheetUpdateState = [];
4
+ export let data = null;
5
+
6
+ /* Intentionally blank module with empty code */
7
+
8
+ export function start(): void {}
9
+ export function reset(): void {}
10
+ export function stop(): void {}
11
+ export function log(): void {}
12
+ export function observe(): void {}
13
+ export function compute(): void {}
14
+ export function checkDocumentStyles(_d: Document): void {}
@@ -1,61 +1,61 @@
1
- import { Privacy } from "@clarity-types/core";
2
- import { Event, Token } from "@clarity-types/data";
3
- import { Constant, NodeInfo } from "@clarity-types/layout";
4
- import * as scrub from "@src/core/scrub";
5
- import { time } from "@src/core/time";
6
- import * as baseline from "@src/data/baseline";
7
- import tokenize from "@src/data/token";
8
- import { queue } from "@src/data/upload";
9
- import * as snapshot from "@src/insight/snapshot";
10
- import * as doc from "@src/layout/document";
11
-
12
- export default async function (type: Event): Promise<void> {
13
- let eventTime = time()
14
- let tokens: Token[] = [eventTime, type];
15
- switch (type) {
16
- case Event.Document:
17
- let d = doc.data;
18
- tokens.push(d.width);
19
- tokens.push(d.height);
20
- baseline.track(type, d.width, d.height);
21
- queue(tokens);
22
- break;
23
- case Event.Snapshot:
24
- let values = snapshot.values;
25
- // Only encode and queue DOM updates if we have valid updates to report back
26
- if (values.length > 0) {
27
- for (let value of values) {
28
- let privacy = value.metadata.privacy;
29
- let data: NodeInfo = value.data;
30
- for (let key of ["tag", "attributes", "value"]) {
31
- if (data[key]) {
32
- switch (key) {
33
- case "tag":
34
- tokens.push(value.id);
35
- if (value.parent) { tokens.push(value.parent); }
36
- if (value.previous) { tokens.push(value.previous); }
37
- tokens.push(data[key]);
38
- break;
39
- case "attributes":
40
- for (let attr in data[key]) {
41
- if (data[key][attr] !== undefined) {
42
- tokens.push(attribute(attr, data[key][attr], privacy));
43
- }
44
- }
45
- break;
46
- case "value":
47
- tokens.push(scrub.text(data[key], data.tag, privacy));
48
- break;
49
- }
50
- }
51
- }
52
- }
53
- queue(tokenize(tokens), true);
54
- }
55
- break;
56
- }
57
- }
58
-
59
- function attribute(key: string, value: string, privacy: Privacy): string {
60
- return `${key}=${scrub.text(value, key.indexOf(Constant.DataAttribute) === 0 ? Constant.DataAttribute : key, privacy)}`;
1
+ import { Privacy } from "@clarity-types/core";
2
+ import { Event, Token } from "@clarity-types/data";
3
+ import { Constant, NodeInfo } from "@clarity-types/layout";
4
+ import * as scrub from "@src/core/scrub";
5
+ import { time } from "@src/core/time";
6
+ import * as baseline from "@src/data/baseline";
7
+ import tokenize from "@src/data/token";
8
+ import { queue } from "@src/data/upload";
9
+ import * as snapshot from "@src/insight/snapshot";
10
+ import * as doc from "@src/layout/document";
11
+
12
+ export default async function (type: Event): Promise<void> {
13
+ let eventTime = time()
14
+ let tokens: Token[] = [eventTime, type];
15
+ switch (type) {
16
+ case Event.Document:
17
+ let d = doc.data;
18
+ tokens.push(d.width);
19
+ tokens.push(d.height);
20
+ baseline.track(type, d.width, d.height);
21
+ queue(tokens);
22
+ break;
23
+ case Event.Snapshot:
24
+ let values = snapshot.values;
25
+ // Only encode and queue DOM updates if we have valid updates to report back
26
+ if (values.length > 0) {
27
+ for (let value of values) {
28
+ let privacy = value.metadata.privacy;
29
+ let data: NodeInfo = value.data;
30
+ for (let key of ["tag", "attributes", "value"]) {
31
+ if (data[key]) {
32
+ switch (key) {
33
+ case "tag":
34
+ tokens.push(value.id);
35
+ if (value.parent) { tokens.push(value.parent); }
36
+ if (value.previous) { tokens.push(value.previous); }
37
+ tokens.push(data[key]);
38
+ break;
39
+ case "attributes":
40
+ for (let attr in data[key]) {
41
+ if (data[key][attr] !== undefined) {
42
+ tokens.push(attribute(attr, data[key][attr], privacy));
43
+ }
44
+ }
45
+ break;
46
+ case "value":
47
+ tokens.push(scrub.text(data[key], data.tag, privacy));
48
+ break;
49
+ }
50
+ }
51
+ }
52
+ }
53
+ queue(tokenize(tokens), true);
54
+ }
55
+ break;
56
+ }
57
+ }
58
+
59
+ function attribute(key: string, value: string, privacy: Privacy): string {
60
+ return `${key}=${scrub.text(value, key.indexOf(Constant.DataAttribute) === 0 ? Constant.DataAttribute : key, privacy)}`;
61
61
  }
@@ -1,115 +1,115 @@
1
- import { OffsetDistance, Privacy } from "@clarity-types/core";
2
- import { Event } from "@clarity-types/data";
3
- import { Constant, NodeInfo, NodeValue, TargetMetadata } from "@clarity-types/layout";
4
- import * as doc from "@src/layout/document";
5
- import encode from "@src/insight/encode";
6
- import * as interaction from "@src/interaction";
7
- import config from "@src/core/config";
8
- export let values: NodeValue[] = [];
9
- let index: number = 1;
10
- let idMap: WeakMap<Node, number> = null; // Maps node => id.
11
-
12
- export function start(): void {
13
- reset();
14
- doc.start();
15
- getId(document.documentElement); // Pre-discover ID for page root
16
- interaction.observe(document);
17
- }
18
-
19
- export function stop(): void {
20
- reset();
21
- doc.stop();
22
- }
23
-
24
- export function compute(): void { /* Intentionally Blank */ }
25
- export function iframe(): boolean { return false; }
26
- export function offset(): OffsetDistance { return { x: 0, y: 0 }; }
27
- export function hashText(): void { /* Intentionally Blank */ }
28
-
29
- export function target(evt: UIEvent): Node {
30
- let path = evt.composed && evt.composedPath ? evt.composedPath() : null;
31
- let node = (path && path.length > 0 ? path[0] : evt.target) as Node;
32
- return node.nodeType === Node.DOCUMENT_NODE ? (node as Document).documentElement : node;
33
- }
34
-
35
- export function metadata(node: Node): TargetMetadata {
36
- let output: TargetMetadata = { id: 0, hash: null, privacy: config.conversions ? Privacy.Sensitive : Privacy.Snapshot };
37
- if (node) { output.id = idMap.has(node) ? idMap.get(node) : getId(node); }
38
- return output;
39
- }
40
-
41
- export function snapshot(): void {
42
- values = [];
43
- traverse(document);
44
- encode(Event.Snapshot);
45
- }
46
-
47
- function reset(): void {
48
- idMap = new WeakMap();
49
- }
50
-
51
- function traverse(root: Node): void {
52
- let queue = [root];
53
- while (queue.length > 0) {
54
- let attributes = null, tag = null, value = null;
55
- let node = queue.shift();
56
- let next = node.firstChild;
57
- let parent = node.parentElement ? node.parentElement : (node.parentNode ? node.parentNode : null);
58
-
59
- while (next) {
60
- queue.push(next);
61
- next = next.nextSibling;
62
- }
63
-
64
- // Process the node
65
- let type = node.nodeType;
66
- switch (type) {
67
- case Node.DOCUMENT_TYPE_NODE:
68
- let doctype = node as DocumentType;
69
- tag = Constant.DocumentTag;
70
- attributes = { name: doctype.name, publicId: doctype.publicId, systemId: doctype.systemId }
71
- break;
72
- case Node.TEXT_NODE:
73
- value = node.nodeValue;
74
- tag = idMap.get(parent) ? Constant.TextTag : tag;
75
- break;
76
- case Node.ELEMENT_NODE:
77
- let element = node as HTMLElement;
78
- attributes = getAttributes(element);
79
- tag = ["NOSCRIPT", "SCRIPT", "STYLE"].indexOf(element.tagName) < 0 ? element.tagName : tag;
80
- break;
81
- }
82
- add(node, parent, { tag, attributes, value });
83
- }
84
- }
85
-
86
- /* Helper Functions - Snapshot Traversal */
87
- function getAttributes(element: HTMLElement): { [key: string]: string } {
88
- let output = {};
89
- let attributes = element.attributes;
90
- if (attributes && attributes.length > 0) {
91
- for (let i = 0; i < attributes.length; i++) {
92
- output[attributes[i].name] = attributes[i].value;
93
- }
94
- }
95
- return output;
96
- }
97
-
98
- function getId(node: Node): number {
99
- if (node === null) { return null; }
100
- if (idMap.has(node)) { return idMap.get(node); }
101
- idMap.set(node, index);
102
- return index++;
103
- }
104
-
105
- function add(node: Node, parent: Node, data: NodeInfo): void {
106
- if (node && data && data.tag) {
107
- let id = getId(node);
108
- let parentId = parent ? idMap.get(parent) : null;
109
- let previous = node.previousSibling ? idMap.get(node.previousSibling) : null;
110
- let metadata = { active: true, suspend: false, privacy: Privacy.Snapshot, position: null, fraud: null, size: null };
111
- values.push({ id, parent: parentId, previous, children: [], data, selector: null, hash: null, region: null, metadata });
112
- }
113
- }
114
-
1
+ import { OffsetDistance, Privacy } from "@clarity-types/core";
2
+ import { Event } from "@clarity-types/data";
3
+ import { Constant, NodeInfo, NodeValue, TargetMetadata } from "@clarity-types/layout";
4
+ import * as doc from "@src/layout/document";
5
+ import encode from "@src/insight/encode";
6
+ import * as interaction from "@src/interaction";
7
+ import config from "@src/core/config";
8
+ export let values: NodeValue[] = [];
9
+ let index: number = 1;
10
+ let idMap: WeakMap<Node, number> = null; // Maps node => id.
11
+
12
+ export function start(): void {
13
+ reset();
14
+ doc.start();
15
+ getId(document.documentElement); // Pre-discover ID for page root
16
+ interaction.observe(document);
17
+ }
18
+
19
+ export function stop(): void {
20
+ reset();
21
+ doc.stop();
22
+ }
23
+
24
+ export function compute(): void { /* Intentionally Blank */ }
25
+ export function iframe(): boolean { return false; }
26
+ export function offset(): OffsetDistance { return { x: 0, y: 0 }; }
27
+ export function hashText(): void { /* Intentionally Blank */ }
28
+
29
+ export function target(evt: UIEvent): Node {
30
+ let path = evt.composed && evt.composedPath ? evt.composedPath() : null;
31
+ let node = (path && path.length > 0 ? path[0] : evt.target) as Node;
32
+ return node.nodeType === Node.DOCUMENT_NODE ? (node as Document).documentElement : node;
33
+ }
34
+
35
+ export function metadata(node: Node): TargetMetadata {
36
+ let output: TargetMetadata = { id: 0, hash: null, privacy: config.conversions ? Privacy.Sensitive : Privacy.Snapshot };
37
+ if (node) { output.id = idMap.has(node) ? idMap.get(node) : getId(node); }
38
+ return output;
39
+ }
40
+
41
+ export function snapshot(): void {
42
+ values = [];
43
+ traverse(document);
44
+ encode(Event.Snapshot);
45
+ }
46
+
47
+ function reset(): void {
48
+ idMap = new WeakMap();
49
+ }
50
+
51
+ function traverse(root: Node): void {
52
+ let queue = [root];
53
+ while (queue.length > 0) {
54
+ let attributes = null, tag = null, value = null;
55
+ let node = queue.shift();
56
+ let next = node.firstChild;
57
+ let parent = node.parentElement ? node.parentElement : (node.parentNode ? node.parentNode : null);
58
+
59
+ while (next) {
60
+ queue.push(next);
61
+ next = next.nextSibling;
62
+ }
63
+
64
+ // Process the node
65
+ let type = node.nodeType;
66
+ switch (type) {
67
+ case Node.DOCUMENT_TYPE_NODE:
68
+ let doctype = node as DocumentType;
69
+ tag = Constant.DocumentTag;
70
+ attributes = { name: doctype.name, publicId: doctype.publicId, systemId: doctype.systemId }
71
+ break;
72
+ case Node.TEXT_NODE:
73
+ value = node.nodeValue;
74
+ tag = idMap.get(parent) ? Constant.TextTag : tag;
75
+ break;
76
+ case Node.ELEMENT_NODE:
77
+ let element = node as HTMLElement;
78
+ attributes = getAttributes(element);
79
+ tag = ["NOSCRIPT", "SCRIPT", "STYLE"].indexOf(element.tagName) < 0 ? element.tagName : tag;
80
+ break;
81
+ }
82
+ add(node, parent, { tag, attributes, value });
83
+ }
84
+ }
85
+
86
+ /* Helper Functions - Snapshot Traversal */
87
+ function getAttributes(element: HTMLElement): { [key: string]: string } {
88
+ let output = {};
89
+ let attributes = element.attributes;
90
+ if (attributes && attributes.length > 0) {
91
+ for (let i = 0; i < attributes.length; i++) {
92
+ output[attributes[i].name] = attributes[i].value;
93
+ }
94
+ }
95
+ return output;
96
+ }
97
+
98
+ function getId(node: Node): number {
99
+ if (node === null) { return null; }
100
+ if (idMap.has(node)) { return idMap.get(node); }
101
+ idMap.set(node, index);
102
+ return index++;
103
+ }
104
+
105
+ function add(node: Node, parent: Node, data: NodeInfo): void {
106
+ if (node && data && data.tag) {
107
+ let id = getId(node);
108
+ let parentId = parent ? idMap.get(parent) : null;
109
+ let previous = node.previousSibling ? idMap.get(node.previousSibling) : null;
110
+ let metadata = { active: true, suspend: false, privacy: Privacy.Snapshot, position: null, fraud: null, size: null };
111
+ values.push({ id, parent: parentId, previous, children: [], data, selector: null, hash: null, region: null, metadata });
112
+ }
113
+ }
114
+
115
115
  export function get(_node: Node): NodeValue {return null;}