clarity-js 0.8.9 → 0.8.10
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.
- package/.lintstagedrc.yml +3 -0
- package/biome.json +43 -0
- package/build/clarity.extended.js +1 -1
- package/build/clarity.insight.js +1 -1
- package/build/clarity.js +3095 -2876
- package/build/clarity.min.js +1 -1
- package/build/clarity.module.js +3095 -2876
- package/build/clarity.performance.js +1 -1
- package/package.json +17 -10
- package/rollup.config.ts +84 -88
- package/src/clarity.ts +34 -28
- package/src/core/config.ts +2 -2
- package/src/core/event.ts +36 -32
- package/src/core/hash.ts +5 -6
- package/src/core/history.ts +10 -11
- package/src/core/index.ts +21 -11
- package/src/core/measure.ts +9 -5
- package/src/core/report.ts +9 -5
- package/src/core/scrub.ts +29 -20
- package/src/core/task.ts +73 -45
- package/src/core/time.ts +3 -3
- package/src/core/timeout.ts +2 -2
- package/src/core/version.ts +1 -1
- package/src/data/baseline.ts +60 -55
- package/src/data/consent.ts +2 -2
- package/src/data/custom.ts +8 -13
- package/src/data/dimension.ts +11 -7
- package/src/data/encode.ts +36 -30
- package/src/data/envelope.ts +38 -38
- package/src/data/extract.ts +86 -77
- package/src/data/index.ts +10 -6
- package/src/data/limit.ts +1 -1
- package/src/data/metadata.ts +305 -266
- package/src/data/metric.ts +18 -8
- package/src/data/ping.ts +8 -4
- package/src/data/signal.ts +18 -18
- package/src/data/summary.ts +6 -4
- package/src/data/token.ts +10 -8
- package/src/data/upgrade.ts +7 -3
- package/src/data/upload.ts +100 -49
- package/src/data/variable.ts +27 -20
- package/src/diagnostic/encode.ts +2 -2
- package/src/diagnostic/fraud.ts +3 -4
- package/src/diagnostic/internal.ts +11 -5
- package/src/diagnostic/script.ts +12 -8
- package/src/global.ts +1 -1
- package/src/insight/blank.ts +4 -4
- package/src/insight/encode.ts +23 -17
- package/src/insight/snapshot.ts +57 -37
- package/src/interaction/change.ts +9 -6
- package/src/interaction/click.ts +34 -28
- package/src/interaction/clipboard.ts +2 -2
- package/src/interaction/encode.ts +35 -31
- package/src/interaction/input.ts +11 -9
- package/src/interaction/pointer.ts +41 -30
- package/src/interaction/resize.ts +5 -5
- package/src/interaction/scroll.ts +20 -17
- package/src/interaction/selection.ts +12 -8
- package/src/interaction/submit.ts +2 -2
- package/src/interaction/timeline.ts +13 -9
- package/src/interaction/unload.ts +1 -1
- package/src/interaction/visibility.ts +2 -2
- package/src/layout/animation.ts +47 -41
- package/src/layout/discover.ts +5 -5
- package/src/layout/document.ts +31 -19
- package/src/layout/dom.ts +141 -91
- package/src/layout/encode.ts +52 -37
- package/src/layout/mutation.ts +321 -318
- package/src/layout/node.ts +104 -81
- package/src/layout/offset.ts +7 -6
- package/src/layout/region.ts +66 -43
- package/src/layout/schema.ts +15 -8
- package/src/layout/selector.ts +47 -25
- package/src/layout/style.ts +45 -37
- package/src/layout/target.ts +14 -10
- package/src/layout/traverse.ts +17 -11
- package/src/performance/blank.ts +1 -1
- package/src/performance/encode.ts +4 -4
- package/src/performance/interaction.ts +58 -70
- package/src/performance/navigation.ts +2 -2
- package/src/performance/observer.ts +59 -26
- package/src/queue.ts +16 -9
- package/tsconfig.json +1 -1
- package/tslint.json +25 -32
- package/types/core.d.ts +13 -13
- package/types/data.d.ts +32 -29
- package/types/diagnostic.d.ts +1 -1
- package/types/interaction.d.ts +5 -4
- package/types/layout.d.ts +36 -21
- package/types/performance.d.ts +6 -5
package/src/layout/mutation.ts
CHANGED
|
@@ -1,25 +1,33 @@
|
|
|
1
|
-
import { Priority, Task, Timer } from "@clarity-types/core";
|
|
1
|
+
import { Priority, Task, type Timer } from "@clarity-types/core";
|
|
2
2
|
import { Code, Event, Metric, Severity } from "@clarity-types/data";
|
|
3
|
-
import {
|
|
3
|
+
import {
|
|
4
|
+
Constant,
|
|
5
|
+
type IWindowWithOverrides,
|
|
6
|
+
type MutationHistory,
|
|
7
|
+
type MutationQueue,
|
|
8
|
+
type MutationRecordWithTime,
|
|
9
|
+
Setting,
|
|
10
|
+
Source,
|
|
11
|
+
} from "@clarity-types/layout";
|
|
4
12
|
import { FunctionNames } from "@clarity-types/performance";
|
|
5
|
-
import api from "@src/core/api";
|
|
6
13
|
import * as core from "@src/core";
|
|
14
|
+
import api from "@src/core/api";
|
|
15
|
+
import config from "@src/core/config";
|
|
7
16
|
import * as event from "@src/core/event";
|
|
8
17
|
import measure from "@src/core/measure";
|
|
9
18
|
import * as task from "@src/core/task";
|
|
10
19
|
import { time } from "@src/core/time";
|
|
11
20
|
import { clearTimeout, setTimeout } from "@src/core/timeout";
|
|
12
21
|
import { id } from "@src/data/metadata";
|
|
22
|
+
import * as metric from "@src/data/metric";
|
|
13
23
|
import * as summary from "@src/data/summary";
|
|
14
24
|
import * as internal from "@src/diagnostic/internal";
|
|
15
25
|
import * as doc from "@src/layout/document";
|
|
16
26
|
import * as dom from "@src/layout/dom";
|
|
17
|
-
import * as metric from "@src/data/metric";
|
|
18
27
|
import encode from "@src/layout/encode";
|
|
19
28
|
import * as region from "@src/layout/region";
|
|
20
29
|
import traverse from "@src/layout/traverse";
|
|
21
30
|
import processNode from "./node";
|
|
22
|
-
import config from "@src/core/config";
|
|
23
31
|
|
|
24
32
|
let observers: Set<MutationObserver> = new Set();
|
|
25
33
|
let mutations: MutationQueue[] = [];
|
|
@@ -35,382 +43,377 @@ let observedNodes: WeakMap<Node, MutationObserver> = new WeakMap<Node, MutationO
|
|
|
35
43
|
const IGNORED_ATTRIBUTES = ["data-google-query-id", "data-load-complete", "data-google-container-id"];
|
|
36
44
|
|
|
37
45
|
export function start(): void {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
+
start.dn = FunctionNames.MutationStart;
|
|
47
|
+
observers = new Set();
|
|
48
|
+
queue = [];
|
|
49
|
+
timeout = null;
|
|
50
|
+
activePeriod = 0;
|
|
51
|
+
history = {};
|
|
52
|
+
observedNodes = new WeakMap<Node, MutationObserver>();
|
|
53
|
+
proxyStyleRules(window);
|
|
46
54
|
}
|
|
47
55
|
|
|
48
56
|
export function observe(node: Document | ShadowRoot): void {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
57
|
+
// Create a new observer for every time a new DOM tree (e.g. root document or shadowdom root) is discovered on the page
|
|
58
|
+
// In the case of shadow dom, any mutations that happen within the shadow dom are not bubbled up to the host document
|
|
59
|
+
// For this reason, we need to wire up mutations every time we see a new shadow dom.
|
|
60
|
+
// Also, wrap it inside a try / catch. In certain browsers (e.g. legacy Edge), observer on shadow dom can throw errors
|
|
61
|
+
try {
|
|
62
|
+
const m = api(Constant.MutationObserver);
|
|
63
|
+
const observer = m in window ? new window[m](measure(handle) as MutationCallback) : null;
|
|
64
|
+
if (observer) {
|
|
65
|
+
observer.observe(node, { attributes: true, childList: true, characterData: true, subtree: true });
|
|
66
|
+
observedNodes.set(node, observer);
|
|
67
|
+
observers.add(observer);
|
|
68
|
+
}
|
|
69
|
+
if (node instanceof Document && node.defaultView) {
|
|
70
|
+
proxyStyleRules(node.defaultView);
|
|
71
|
+
}
|
|
72
|
+
} catch (e) {
|
|
73
|
+
internal.log(Code.MutationObserver, Severity.Info, e ? e.name : null);
|
|
64
74
|
}
|
|
65
|
-
|
|
66
|
-
} catch (e) {
|
|
67
|
-
internal.log(Code.MutationObserver, Severity.Info, e ? e.name : null);
|
|
68
|
-
}
|
|
69
75
|
}
|
|
70
76
|
|
|
71
77
|
export function monitor(frame: HTMLIFrameElement): void {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
+
// Bind to iframe's onload event so we get notified anytime there's an update to iframe content.
|
|
79
|
+
// This includes cases where iframe location is updated without explicitly updating src attribute
|
|
80
|
+
// E.g. iframe.contentWindow.location.href = "new-location";
|
|
81
|
+
if (dom.has(frame) === false) {
|
|
82
|
+
event.bind(frame, Constant.LoadEvent, generate.bind(this, frame, Constant.ChildList), true);
|
|
83
|
+
}
|
|
78
84
|
}
|
|
79
85
|
|
|
80
86
|
export function stop(): void {
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
87
|
+
for (const observer of Array.from(observers)) {
|
|
88
|
+
if (observer) {
|
|
89
|
+
observer.disconnect();
|
|
90
|
+
}
|
|
84
91
|
}
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
observedNodes = new WeakMap();
|
|
92
|
+
observers = new Set();
|
|
93
|
+
history = {};
|
|
94
|
+
mutations = [];
|
|
95
|
+
throttledMutations = {};
|
|
96
|
+
queue = [];
|
|
97
|
+
activePeriod = 0;
|
|
98
|
+
timeout = null;
|
|
99
|
+
observedNodes = new WeakMap();
|
|
94
100
|
}
|
|
95
101
|
|
|
96
102
|
export function active(): void {
|
|
97
|
-
|
|
103
|
+
activePeriod = time() + Setting.MutationActivePeriod;
|
|
98
104
|
}
|
|
99
105
|
|
|
100
106
|
export function disconnect(n: Node): void {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
+
const ob = observedNodes.get(n);
|
|
108
|
+
if (ob) {
|
|
109
|
+
ob.disconnect();
|
|
110
|
+
observers.delete(ob);
|
|
111
|
+
observedNodes.delete(n);
|
|
112
|
+
}
|
|
107
113
|
}
|
|
108
114
|
|
|
109
115
|
function handle(m: MutationRecord[]): void {
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
116
|
+
handle.dn = FunctionNames.MutationHandle;
|
|
117
|
+
// Queue up mutation records for asynchronous processing
|
|
118
|
+
const now = time();
|
|
119
|
+
summary.track(Event.Mutation, now);
|
|
120
|
+
mutations.push({ time: now, mutations: m });
|
|
121
|
+
task.schedule(process, Priority.High).then((): void => {
|
|
122
|
+
setTimeout(doc.compute);
|
|
123
|
+
measure(region.compute)();
|
|
124
|
+
});
|
|
119
125
|
}
|
|
120
126
|
|
|
121
127
|
async function processMutation(timer: Timer, mutation: MutationRecord, instance: number, timestamp: number): Promise<void> {
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
}
|
|
128
|
+
let state = task.state(timer);
|
|
129
|
+
|
|
130
|
+
if (state === Task.Wait) {
|
|
131
|
+
state = await task.suspend(timer);
|
|
132
|
+
}
|
|
133
|
+
if (state === Task.Stop) {
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
const target = mutation.target;
|
|
138
|
+
const type = config.throttleDom ? track(mutation, timer, instance, timestamp) : mutation.type;
|
|
139
|
+
|
|
140
|
+
if (type && target && target.ownerDocument) {
|
|
141
|
+
dom.parse(target.ownerDocument);
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
if (type && target && target.nodeType === Node.DOCUMENT_FRAGMENT_NODE && (target as ShadowRoot).host) {
|
|
145
|
+
dom.parse(target as ShadowRoot);
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
switch (type) {
|
|
149
|
+
case Constant.Attributes:
|
|
150
|
+
if (IGNORED_ATTRIBUTES.indexOf(mutation.attributeName) < 0) {
|
|
151
|
+
processNode(target, Source.Attributes, timestamp);
|
|
152
|
+
}
|
|
153
|
+
break;
|
|
154
|
+
case Constant.CharacterData:
|
|
155
|
+
processNode(target, Source.CharacterData, timestamp);
|
|
156
|
+
break;
|
|
157
|
+
case Constant.ChildList:
|
|
158
|
+
processNodeList(mutation.addedNodes, Source.ChildListAdd, timer, timestamp);
|
|
159
|
+
processNodeList(mutation.removedNodes, Source.ChildListRemove, timer, timestamp);
|
|
160
|
+
break;
|
|
161
|
+
default:
|
|
162
|
+
break;
|
|
163
|
+
}
|
|
159
164
|
}
|
|
160
165
|
async function process(): Promise<void> {
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
166
|
+
const timer: Timer = { id: id(), cost: Metric.LayoutCost };
|
|
167
|
+
task.start(timer);
|
|
168
|
+
while (mutations.length > 0) {
|
|
169
|
+
const record = mutations.shift();
|
|
170
|
+
const instance = time();
|
|
171
|
+
for (const mutation of record.mutations) {
|
|
172
|
+
await processMutation(timer, mutation, instance, record.time);
|
|
173
|
+
}
|
|
174
|
+
await encode(Event.Mutation, timer, record.time);
|
|
168
175
|
}
|
|
169
|
-
await encode(Event.Mutation, timer, record.time);
|
|
170
|
-
}
|
|
171
176
|
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
177
|
+
let processedMutations = false;
|
|
178
|
+
for (const key of Object.keys(throttledMutations)) {
|
|
179
|
+
const throttledMutationToProcess: MutationRecordWithTime = throttledMutations[key];
|
|
180
|
+
delete throttledMutations[key];
|
|
181
|
+
await processMutation(timer, throttledMutationToProcess.mutation, time(), throttledMutationToProcess.timestamp);
|
|
182
|
+
processedMutations = true;
|
|
183
|
+
}
|
|
179
184
|
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
185
|
+
if (Object.keys(throttledMutations).length > 0) {
|
|
186
|
+
processThrottledMutations();
|
|
187
|
+
}
|
|
183
188
|
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
189
|
+
// ensure we encode the previously throttled mutations once we have finished them
|
|
190
|
+
if (Object.keys(throttledMutations).length === 0 && processedMutations) {
|
|
191
|
+
await encode(Event.Mutation, timer, time());
|
|
192
|
+
}
|
|
188
193
|
|
|
189
|
-
|
|
194
|
+
cleanHistory();
|
|
190
195
|
|
|
191
|
-
|
|
196
|
+
task.stop(timer);
|
|
192
197
|
}
|
|
193
198
|
|
|
194
199
|
function cleanHistory(): void {
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
+
const now = time();
|
|
201
|
+
if (Object.keys(history).length > Setting.MaxMutationHistoryCount) {
|
|
202
|
+
history = {};
|
|
203
|
+
metric.count(Metric.HistoryClear);
|
|
204
|
+
}
|
|
200
205
|
|
|
201
|
-
|
|
202
|
-
|
|
203
|
-
|
|
204
|
-
|
|
206
|
+
for (const key of Object.keys(history)) {
|
|
207
|
+
const h = history[key];
|
|
208
|
+
if (now > h[1] + Setting.MaxMutationHistoryTime) {
|
|
209
|
+
delete history[key];
|
|
210
|
+
}
|
|
205
211
|
}
|
|
206
|
-
}
|
|
207
212
|
}
|
|
208
213
|
|
|
209
214
|
function track(m: MutationRecord, timer: Timer, instance: number, timestamp: number): string {
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
215
|
+
const value = m.target ? dom.get(m.target.parentNode) : null;
|
|
216
|
+
|
|
217
|
+
// Check if the parent is already discovered and that the parent is not the document root
|
|
218
|
+
if (value && value.data.tag !== Constant.HTML) {
|
|
219
|
+
// calculate inactive period based on the timestamp of the mutation not when the mutation is processed
|
|
220
|
+
const inactive = timestamp > activePeriod;
|
|
221
|
+
|
|
222
|
+
// Calculate critical period based on when mutation is processed
|
|
223
|
+
const target = dom.get(m.target);
|
|
224
|
+
const element = target?.selector ? target.selector.join() : m.target.nodeName;
|
|
225
|
+
const parent = value.selector ? value.selector.join() : Constant.Empty;
|
|
226
|
+
|
|
227
|
+
// We use selector, instead of id, to determine the key (signature for the mutation) because in some cases
|
|
228
|
+
// repeated mutations can cause elements to be destroyed and then recreated as new DOM nodes
|
|
229
|
+
// In those cases, IDs will change however the selector (which is relative to DOM xPath) remains the same
|
|
230
|
+
const key = [parent, element, m.attributeName, names(m.addedNodes), names(m.removedNodes)].join();
|
|
231
|
+
|
|
232
|
+
// Initialize an entry if it doesn't already exist
|
|
233
|
+
history[key] = key in history ? history[key] : [0, instance];
|
|
234
|
+
const h = history[key];
|
|
235
|
+
|
|
236
|
+
// Lookup any pending nodes queued up for removal, and process them now if we suspended a mutation before
|
|
237
|
+
if (inactive === false && h[0] >= Setting.MutationSuspendThreshold) {
|
|
238
|
+
processNodeList(h[2], Source.ChildListRemove, timer, timestamp);
|
|
239
|
+
}
|
|
235
240
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
|
|
241
|
+
// Update the counter, do not reset counter if its critical period
|
|
242
|
+
h[0] = inactive ? (h[1] === instance ? h[0] : h[0] + 1) : 1;
|
|
243
|
+
h[1] = instance;
|
|
244
|
+
|
|
245
|
+
// Return updated mutation type based on,
|
|
246
|
+
// 1. if we have already hit the threshold or not
|
|
247
|
+
// 2. if its a low priority mutation happening during critical time period
|
|
248
|
+
if (h[0] >= Setting.MutationSuspendThreshold) {
|
|
249
|
+
// Store a reference to removedNodes so we can process them later
|
|
250
|
+
// when we resume mutations again on user interactions
|
|
251
|
+
h[2] = m.removedNodes;
|
|
252
|
+
if (instance > timestamp + Setting.MutationActivePeriod) {
|
|
253
|
+
return m.type;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
// we only store the most recent mutation for a given key if it is being throttled
|
|
257
|
+
throttledMutations[key] = { mutation: m, timestamp };
|
|
258
|
+
|
|
259
|
+
return Constant.Throttle;
|
|
260
|
+
}
|
|
255
261
|
}
|
|
256
|
-
|
|
257
|
-
return m.type;
|
|
262
|
+
return m.type;
|
|
258
263
|
}
|
|
259
264
|
|
|
260
265
|
function names(nodes: NodeList): string {
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
264
|
-
|
|
265
|
-
|
|
266
|
+
const output: string[] = [];
|
|
267
|
+
for (let i = 0; nodes && i < nodes.length; i++) {
|
|
268
|
+
output.push(nodes[i].nodeName);
|
|
269
|
+
}
|
|
270
|
+
return output.join();
|
|
266
271
|
}
|
|
267
272
|
|
|
268
273
|
async function processNodeList(list: NodeList, source: Source, timer: Timer, timestamp: number): Promise<void> {
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
274
|
+
const length = list ? list.length : 0;
|
|
275
|
+
for (let i = 0; i < length; i++) {
|
|
276
|
+
const node = list[i];
|
|
277
|
+
if (source === Source.ChildListAdd) {
|
|
278
|
+
traverse(node, timer, source, timestamp);
|
|
279
|
+
} else {
|
|
280
|
+
let state = task.state(timer);
|
|
281
|
+
if (state === Task.Wait) {
|
|
282
|
+
state = await task.suspend(timer);
|
|
283
|
+
}
|
|
284
|
+
if (state === Task.Stop) {
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
processNode(node, source, timestamp);
|
|
288
|
+
}
|
|
283
289
|
}
|
|
284
|
-
}
|
|
285
290
|
}
|
|
286
291
|
|
|
287
292
|
function processThrottledMutations(): void {
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
293
|
+
if (throttleDelay) {
|
|
294
|
+
clearTimeout(throttleDelay);
|
|
295
|
+
}
|
|
296
|
+
throttleDelay = setTimeout(() => {
|
|
297
|
+
task.schedule(process, Priority.High);
|
|
298
|
+
}, Setting.LookAhead);
|
|
294
299
|
}
|
|
295
300
|
|
|
296
301
|
export function schedule(node: Node): Node {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
|
|
302
|
+
// Only schedule manual trigger for this node if it's not already in the queue
|
|
303
|
+
if (queue.indexOf(node) < 0) {
|
|
304
|
+
queue.push(node);
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// Cancel any previous trigger before scheduling a new one.
|
|
308
|
+
// It's common for a webpage to call multiple synchronous "insertRule" / "deleteRule" calls.
|
|
309
|
+
// And in those cases we do not wish to monitor changes multiple times for the same node.
|
|
310
|
+
if (timeout) {
|
|
311
|
+
clearTimeout(timeout);
|
|
312
|
+
}
|
|
313
|
+
timeout = setTimeout(() => {
|
|
314
|
+
trigger();
|
|
315
|
+
}, Setting.LookAhead);
|
|
316
|
+
|
|
317
|
+
return node;
|
|
313
318
|
}
|
|
314
319
|
|
|
315
320
|
function trigger(): void {
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
|
|
321
|
+
for (const node of queue) {
|
|
322
|
+
// Generate a mutation for this node only if it still exists
|
|
323
|
+
if (node) {
|
|
324
|
+
const shadowRoot = node.nodeType === Node.DOCUMENT_FRAGMENT_NODE;
|
|
325
|
+
// Skip re-processing shadowRoot if it was already discovered
|
|
326
|
+
if (shadowRoot && dom.has(node)) {
|
|
327
|
+
continue;
|
|
328
|
+
}
|
|
329
|
+
generate(node, shadowRoot ? Constant.ChildList : Constant.CharacterData);
|
|
330
|
+
}
|
|
325
331
|
}
|
|
326
|
-
|
|
327
|
-
queue = [];
|
|
332
|
+
queue = [];
|
|
328
333
|
}
|
|
329
334
|
|
|
330
335
|
function generate(target: Node, type: MutationRecordType): void {
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
336
|
+
generate.dn = FunctionNames.MutationGenerate;
|
|
337
|
+
measure(handle)([
|
|
338
|
+
{
|
|
339
|
+
addedNodes: [target],
|
|
340
|
+
attributeName: null,
|
|
341
|
+
attributeNamespace: null,
|
|
342
|
+
nextSibling: null,
|
|
343
|
+
oldValue: null,
|
|
344
|
+
previousSibling: null,
|
|
345
|
+
removedNodes: [],
|
|
346
|
+
target,
|
|
347
|
+
type,
|
|
348
|
+
},
|
|
349
|
+
]);
|
|
345
350
|
}
|
|
346
351
|
|
|
352
|
+
function proxyStyleRules(win: IWindowWithOverrides): void {
|
|
353
|
+
if (win === null || win === undefined) {
|
|
354
|
+
return;
|
|
355
|
+
}
|
|
347
356
|
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
} else {
|
|
409
|
-
return win.clarityOverrides.AttachShadow.apply(this, arguments);
|
|
357
|
+
win.clarityOverrides = win.clarityOverrides || {};
|
|
358
|
+
|
|
359
|
+
// Some popular open source libraries, like styled-components, optimize performance
|
|
360
|
+
// by injecting CSS using insertRule API vs. appending text node. A side effect of
|
|
361
|
+
// using javascript API is that it doesn't trigger DOM mutation and therefore we
|
|
362
|
+
// need to override the insertRule API and listen for changes manually.
|
|
363
|
+
if (win.clarityOverrides.InsertRule === undefined) {
|
|
364
|
+
win.clarityOverrides.InsertRule = win.CSSStyleSheet.prototype.insertRule;
|
|
365
|
+
win.CSSStyleSheet.prototype.insertRule = function (...args): number {
|
|
366
|
+
if (core.active()) {
|
|
367
|
+
schedule(this.ownerNode);
|
|
368
|
+
}
|
|
369
|
+
return win.clarityOverrides.InsertRule.apply(this, args);
|
|
370
|
+
};
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
if ("CSSMediaRule" in win && win.clarityOverrides.MediaInsertRule === undefined) {
|
|
374
|
+
win.clarityOverrides.MediaInsertRule = win.CSSMediaRule.prototype.insertRule;
|
|
375
|
+
win.CSSMediaRule.prototype.insertRule = function (...args): number {
|
|
376
|
+
if (core.active()) {
|
|
377
|
+
schedule(this.parentStyleSheet.ownerNode);
|
|
378
|
+
}
|
|
379
|
+
return win.clarityOverrides.MediaInsertRule.apply(this, args);
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
|
|
383
|
+
if (win.clarityOverrides.DeleteRule === undefined) {
|
|
384
|
+
win.clarityOverrides.DeleteRule = win.CSSStyleSheet.prototype.deleteRule;
|
|
385
|
+
win.CSSStyleSheet.prototype.deleteRule = function (...args): void {
|
|
386
|
+
if (core.active()) {
|
|
387
|
+
schedule(this.ownerNode);
|
|
388
|
+
}
|
|
389
|
+
win.clarityOverrides.DeleteRule.apply(this, args);
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
|
|
393
|
+
if ("CSSMediaRule" in win && win.clarityOverrides.MediaDeleteRule === undefined) {
|
|
394
|
+
win.clarityOverrides.MediaDeleteRule = win.CSSMediaRule.prototype.deleteRule;
|
|
395
|
+
win.CSSMediaRule.prototype.deleteRule = function (...args): void {
|
|
396
|
+
if (core.active()) {
|
|
397
|
+
schedule(this.parentStyleSheet.ownerNode);
|
|
398
|
+
}
|
|
399
|
+
win.clarityOverrides.MediaDeleteRule.apply(this, args);
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
// Add a hook to attachShadow API calls
|
|
404
|
+
// In case we are unable to add a hook and browser throws an exception,
|
|
405
|
+
// reset attachShadow variable and resume processing like before
|
|
406
|
+
if (win.clarityOverrides.AttachShadow === undefined) {
|
|
407
|
+
win.clarityOverrides.AttachShadow = win.Element.prototype.attachShadow;
|
|
408
|
+
try {
|
|
409
|
+
win.Element.prototype.attachShadow = function (...args): ShadowRoot {
|
|
410
|
+
if (core.active()) {
|
|
411
|
+
return schedule(win.clarityOverrides.AttachShadow.apply(this, args)) as ShadowRoot;
|
|
412
|
+
}
|
|
413
|
+
return win.clarityOverrides.AttachShadow.apply(this, args);
|
|
414
|
+
};
|
|
415
|
+
} catch {
|
|
416
|
+
win.clarityOverrides.AttachShadow = null;
|
|
410
417
|
}
|
|
411
|
-
};
|
|
412
|
-
} catch {
|
|
413
|
-
win.clarityOverrides.AttachShadow = null;
|
|
414
418
|
}
|
|
415
|
-
|
|
416
|
-
}
|
|
419
|
+
}
|