@highlight-run/rrweb 2.0.1 → 2.0.4

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 (117) hide show
  1. package/dist/plugins/console-record.js +408 -609
  2. package/dist/plugins/console-record.min.js +12 -15
  3. package/dist/plugins/console-record.min.js.map +1 -1
  4. package/dist/plugins/console-replay.js +142 -271
  5. package/dist/plugins/console-replay.min.js +4 -15
  6. package/dist/plugins/console-replay.min.js.map +1 -1
  7. package/dist/plugins/sequential-id-record.js +23 -26
  8. package/dist/plugins/sequential-id-record.min.js +1 -1
  9. package/dist/plugins/sequential-id-record.min.js.map +1 -1
  10. package/dist/plugins/sequential-id-replay.js +26 -30
  11. package/dist/plugins/sequential-id-replay.min.js +1 -1
  12. package/dist/plugins/sequential-id-replay.min.js.map +1 -1
  13. package/dist/record/rrweb-record-pack.js +63 -146
  14. package/dist/record/rrweb-record-pack.min.js +3 -15
  15. package/dist/record/rrweb-record-pack.min.js.map +1 -1
  16. package/dist/record/rrweb-record.js +3002 -3118
  17. package/dist/record/rrweb-record.min.js +4 -15
  18. package/dist/record/rrweb-record.min.js.map +1 -1
  19. package/dist/replay/rrweb-replay-unpack.js +4354 -4054
  20. package/dist/replay/rrweb-replay-unpack.min.js +5 -17
  21. package/dist/replay/rrweb-replay-unpack.min.js.map +1 -1
  22. package/dist/replay/rrweb-replay.js +4286 -3984
  23. package/dist/replay/rrweb-replay.min.js +5 -17
  24. package/dist/replay/rrweb-replay.min.js.map +1 -1
  25. package/dist/rrweb-all.js +7634 -7453
  26. package/dist/rrweb-all.min.js +16 -17
  27. package/dist/rrweb-all.min.js.map +1 -1
  28. package/dist/rrweb.js +7339 -7075
  29. package/dist/rrweb.min.js +6 -17
  30. package/dist/rrweb.min.js.map +1 -1
  31. package/es/rrweb/_virtual/_rollup-plugin-web-worker-loader__helper__auto__createBase64WorkerFactory.js +12 -0
  32. package/es/rrweb/_virtual/_rollup-plugin-web-worker-loader__helper__auto__isNodeJS.js +7 -0
  33. package/es/rrweb/_virtual/_rollup-plugin-web-worker-loader__helper__browser__createBase64WorkerFactory.js +31 -0
  34. package/es/rrweb/_virtual/_rollup-plugin-web-worker-loader__helper__node__WorkerClass.js +11 -0
  35. package/es/rrweb/_virtual/_rollup-plugin-web-worker-loader__helper__node__createBase64WorkerFactory.js +18 -0
  36. package/es/rrweb/_virtual/image-bitmap-data-url-worker.js +6 -0
  37. package/es/rrweb/ext/mitt/dist/mitt.es.js +1 -1
  38. package/es/rrweb/ext/tslib/tslib.es6.js +38 -0
  39. package/es/rrweb/packages/rrdom/es/virtual-dom.js +1099 -0
  40. package/es/rrweb/packages/rrweb/src/index.js +6 -4
  41. package/es/rrweb/packages/rrweb/src/packer/base.js +1 -1
  42. package/es/rrweb/packages/rrweb/src/packer/pack.js +4 -5
  43. package/es/rrweb/packages/rrweb/src/packer/unpack.js +24 -24
  44. package/es/rrweb/packages/rrweb/src/plugins/console/record/error-stack-parser.js +175 -176
  45. package/es/rrweb/packages/rrweb/src/plugins/console/record/index.js +109 -128
  46. package/es/rrweb/packages/rrweb/src/plugins/console/record/stringify.js +125 -138
  47. package/es/rrweb/packages/rrweb/src/plugins/console/replay/index.js +88 -105
  48. package/es/rrweb/packages/rrweb/src/plugins/sequential-id/record/index.js +19 -20
  49. package/es/rrweb/packages/rrweb/src/plugins/sequential-id/replay/index.js +25 -25
  50. package/es/rrweb/packages/rrweb/src/record/iframe-manager.js +29 -30
  51. package/es/rrweb/packages/rrweb/src/record/index.js +348 -373
  52. package/es/rrweb/packages/rrweb/src/record/mutation.js +487 -528
  53. package/es/rrweb/packages/rrweb/src/record/observer.js +584 -659
  54. package/es/rrweb/packages/rrweb/src/record/observers/canvas/2d.js +44 -73
  55. package/es/rrweb/packages/rrweb/src/record/observers/canvas/canvas-manager.js +166 -87
  56. package/es/rrweb/packages/rrweb/src/record/observers/canvas/canvas.js +21 -26
  57. package/es/rrweb/packages/rrweb/src/record/observers/canvas/serialize-args.js +119 -112
  58. package/es/rrweb/packages/rrweb/src/record/observers/canvas/webgl.js +53 -72
  59. package/es/rrweb/packages/rrweb/src/record/shadow-dom-manager.js +38 -40
  60. package/es/rrweb/packages/rrweb/src/record/stylesheet-manager.js +31 -0
  61. package/es/rrweb/packages/rrweb/src/replay/canvas/2d.js +27 -23
  62. package/es/rrweb/packages/rrweb/src/replay/canvas/deserialize-args.js +67 -0
  63. package/es/rrweb/packages/rrweb/src/replay/canvas/index.js +37 -29
  64. package/es/rrweb/packages/rrweb/src/replay/canvas/webgl.js +58 -120
  65. package/es/rrweb/packages/rrweb/src/replay/index.js +1545 -1854
  66. package/es/rrweb/packages/rrweb/src/replay/machine.js +275 -315
  67. package/es/rrweb/packages/rrweb/src/replay/smoothscroll.js +216 -218
  68. package/es/rrweb/packages/rrweb/src/replay/styles/inject-style.js +7 -7
  69. package/es/rrweb/packages/rrweb/src/replay/timer.js +87 -91
  70. package/es/rrweb/packages/rrweb/src/types.js +72 -72
  71. package/es/rrweb/packages/rrweb/src/utils.js +312 -506
  72. package/es/rrweb/packages/rrweb-snapshot/es/rrweb-snapshot.js +195 -71
  73. package/lib/plugins/console-record.js +469 -627
  74. package/lib/plugins/console-replay.js +198 -268
  75. package/lib/plugins/sequential-id-record.js +19 -20
  76. package/lib/plugins/sequential-id-replay.js +25 -25
  77. package/lib/record/rrweb-record-pack.js +184 -139
  78. package/lib/record/rrweb-record.js +2473 -2425
  79. package/lib/replay/rrweb-replay-unpack.js +3797 -3343
  80. package/lib/replay/rrweb-replay.js +3742 -3288
  81. package/lib/rrweb-all.js +6725 -6247
  82. package/lib/rrweb.js +6157 -5632
  83. package/package.json +14 -13
  84. package/typings/packer/base.d.ts +1 -1
  85. package/typings/plugins/console/record/index.d.ts +1 -1
  86. package/typings/plugins/console/record/stringify.d.ts +1 -1
  87. package/typings/plugins/sequential-id/record/index.d.ts +1 -1
  88. package/typings/plugins/sequential-id/replay/index.d.ts +1 -1
  89. package/typings/record/iframe-manager.d.ts +3 -3
  90. package/typings/record/index.d.ts +1 -1
  91. package/typings/record/mutation.d.ts +2 -1
  92. package/typings/record/observers/canvas/2d.d.ts +2 -1
  93. package/typings/record/observers/canvas/canvas-manager.d.ts +5 -2
  94. package/typings/record/observers/canvas/canvas.d.ts +1 -1
  95. package/typings/record/observers/canvas/serialize-args.d.ts +5 -5
  96. package/typings/record/observers/canvas/webgl.d.ts +2 -1
  97. package/typings/record/shadow-dom-manager.d.ts +2 -1
  98. package/typings/record/stylesheet-manager.d.ts +12 -0
  99. package/typings/record/workers/image-bitmap-data-url-worker.d.ts +5 -0
  100. package/typings/replay/canvas/2d.d.ts +3 -3
  101. package/typings/replay/canvas/deserialize-args.d.ts +7 -0
  102. package/typings/replay/canvas/index.d.ts +4 -3
  103. package/typings/replay/canvas/webgl.d.ts +3 -5
  104. package/typings/replay/index.d.ts +7 -10
  105. package/typings/types.d.ts +45 -17
  106. package/typings/utils.d.ts +17 -44
  107. package/dist/replay/rrweb-replay-unpack.min.css +0 -2
  108. package/dist/replay/rrweb-replay-unpack.min.css.map +0 -1
  109. package/dist/replay/rrweb-replay.min.css +0 -2
  110. package/dist/replay/rrweb-replay.min.css.map +0 -1
  111. package/dist/rrweb-all.min.css +0 -2
  112. package/dist/rrweb-all.min.css.map +0 -1
  113. package/dist/rrweb.min.css +0 -2
  114. package/dist/rrweb.min.css.map +0 -1
  115. package/es/rrweb/packages/rrweb/ext/tslib/tslib.es6.js +0 -78
  116. package/es/rrweb/packages/rrweb/src/replay/virtual-styles.js +0 -121
  117. package/typings/replay/virtual-styles.d.ts +0 -43
@@ -1,665 +1,590 @@
1
- import { __read, __values, __spreadArray, __assign } from '../../ext/tslib/tslib.es6.js';
2
1
  import { maskInputValue } from '../../../rrweb-snapshot/es/rrweb-snapshot.js';
3
- import { on, throttle, isTouchEvent, isBlocked, getWindowHeight, getWindowWidth, hookSetter, patch, isCanvasNode } from '../utils.js';
4
- import { IncrementalSource, MouseInteractions } from '../types.js';
2
+ import { on, throttle, isBlocked, getWindowHeight, getWindowWidth, hookSetter, patch, isTouchEvent, isCanvasNode } from '../utils.js';
3
+ import { MouseInteractions, IncrementalSource } from '../types.js';
5
4
  import MutationBuffer from './mutation.js';
6
5
 
7
- var mutationBuffers = [];
8
- var isCSSGroupingRuleSupported = typeof CSSGroupingRule !== 'undefined';
9
- var isCSSMediaRuleSupported = typeof CSSMediaRule !== 'undefined';
10
- var isCSSSupportsRuleSupported = typeof CSSSupportsRule !== 'undefined';
11
- var isCSSConditionRuleSupported = typeof CSSConditionRule !== 'undefined';
12
- function getEventTarget(event) {
13
- try {
14
- if ('composedPath' in event) {
15
- var path = event.composedPath();
16
- if (path.length) {
17
- return path[0];
18
- }
19
- }
20
- else if ('path' in event && event.path.length) {
21
- return event.path[0];
22
- }
23
- return event.target;
24
- }
25
- catch (_a) {
26
- return event.target;
27
- }
28
- }
29
- function initMutationObserver(options, rootEl) {
30
- var _a, _b;
31
- var mutationBuffer = new MutationBuffer();
32
- mutationBuffers.push(mutationBuffer);
33
- mutationBuffer.init(options);
34
- var mutationObserverCtor = window.MutationObserver ||
35
- window.__rrMutationObserver;
36
- var angularZoneSymbol = (_b = (_a = window === null || window === void 0 ? void 0 : window.Zone) === null || _a === void 0 ? void 0 : _a.__symbol__) === null || _b === void 0 ? void 0 : _b.call(_a, 'MutationObserver');
37
- if (angularZoneSymbol &&
38
- window[angularZoneSymbol]) {
39
- mutationObserverCtor = window[angularZoneSymbol];
40
- }
41
- var observer = new mutationObserverCtor(mutationBuffer.processMutations.bind(mutationBuffer));
42
- observer.observe(rootEl, {
43
- attributes: true,
44
- attributeOldValue: true,
45
- characterData: true,
46
- characterDataOldValue: true,
47
- childList: true,
48
- subtree: true,
49
- });
50
- return observer;
51
- }
52
- function initMoveObserver(_a) {
53
- var mousemoveCb = _a.mousemoveCb, sampling = _a.sampling, doc = _a.doc, mirror = _a.mirror;
54
- if (sampling.mousemove === false) {
55
- return function () { };
56
- }
57
- var threshold = typeof sampling.mousemove === 'number' ? sampling.mousemove : 50;
58
- var callbackThreshold = typeof sampling.mousemoveCallback === 'number'
59
- ? sampling.mousemoveCallback
60
- : 500;
61
- var positions = [];
62
- var timeBaseline;
63
- var wrappedCb = throttle(function (source) {
64
- var totalOffset = Date.now() - timeBaseline;
65
- mousemoveCb(positions.map(function (p) {
66
- p.timeOffset -= totalOffset;
67
- return p;
68
- }), source);
69
- positions = [];
70
- timeBaseline = null;
71
- }, callbackThreshold);
72
- var updatePosition = throttle(function (evt) {
73
- var target = getEventTarget(evt);
74
- var _a = isTouchEvent(evt)
75
- ? evt.changedTouches[0]
76
- : evt, clientX = _a.clientX, clientY = _a.clientY;
77
- if (!timeBaseline) {
78
- timeBaseline = Date.now();
79
- }
80
- positions.push({
81
- x: clientX,
82
- y: clientY,
83
- id: mirror.getId(target),
84
- timeOffset: Date.now() - timeBaseline,
85
- });
86
- wrappedCb(typeof DragEvent !== 'undefined' && evt instanceof DragEvent
87
- ? IncrementalSource.Drag
88
- : evt instanceof MouseEvent
89
- ? IncrementalSource.MouseMove
90
- : IncrementalSource.TouchMove);
91
- }, threshold, {
92
- trailing: false,
93
- });
94
- var handlers = [
95
- on('mousemove', updatePosition, doc),
96
- on('touchmove', updatePosition, doc),
97
- on('drag', updatePosition, doc),
98
- ];
99
- return function () {
100
- handlers.forEach(function (h) { return h(); });
101
- };
102
- }
103
- function initMouseInteractionObserver(_a) {
104
- var mouseInteractionCb = _a.mouseInteractionCb, doc = _a.doc, mirror = _a.mirror, blockClass = _a.blockClass, sampling = _a.sampling;
105
- if (sampling.mouseInteraction === false) {
106
- return function () { };
107
- }
108
- var disableMap = sampling.mouseInteraction === true ||
109
- sampling.mouseInteraction === undefined
110
- ? {}
111
- : sampling.mouseInteraction;
112
- var handlers = [];
113
- var getHandler = function (eventKey) {
114
- return function (event) {
115
- var target = getEventTarget(event);
116
- if (isBlocked(target, blockClass) ||
117
- isCanvasNode(target)) {
118
- return;
119
- }
120
- var e = isTouchEvent(event) ? event.changedTouches[0] : event;
121
- if (!e) {
122
- return;
123
- }
124
- var id = mirror.getId(target);
125
- var clientX = e.clientX, clientY = e.clientY;
126
- mouseInteractionCb({
127
- type: MouseInteractions[eventKey],
128
- id: id,
129
- x: clientX,
130
- y: clientY,
131
- });
132
- };
133
- };
134
- Object.keys(MouseInteractions)
135
- .filter(function (key) {
136
- return Number.isNaN(Number(key)) &&
137
- !key.endsWith('_Departed') &&
138
- disableMap[key] !== false;
139
- })
140
- .forEach(function (eventKey) {
141
- var eventName = eventKey.toLowerCase();
142
- var handler = getHandler(eventKey);
143
- handlers.push(on(eventName, handler, doc));
144
- });
145
- return function () {
146
- handlers.forEach(function (h) { return h(); });
147
- };
148
- }
149
- function initScrollObserver(_a) {
150
- var scrollCb = _a.scrollCb, doc = _a.doc, mirror = _a.mirror, blockClass = _a.blockClass, sampling = _a.sampling;
151
- var updatePosition = throttle(function (evt) {
152
- var target = getEventTarget(evt);
153
- if (!target || isBlocked(target, blockClass)) {
154
- return;
155
- }
156
- var id = mirror.getId(target);
157
- if (target === doc) {
158
- var scrollEl = (doc.scrollingElement || doc.documentElement);
159
- scrollCb({
160
- id: id,
161
- x: scrollEl.scrollLeft,
162
- y: scrollEl.scrollTop,
163
- });
164
- }
165
- else {
166
- scrollCb({
167
- id: id,
168
- x: target.scrollLeft,
169
- y: target.scrollTop,
170
- });
171
- }
172
- }, sampling.scroll || 100);
173
- return on('scroll', updatePosition, doc);
174
- }
175
- function initViewportResizeObserver(_a) {
176
- var viewportResizeCb = _a.viewportResizeCb;
177
- var lastH = -1;
178
- var lastW = -1;
179
- var updateDimension = throttle(function () {
180
- var height = getWindowHeight();
181
- var width = getWindowWidth();
182
- if (lastH !== height || lastW !== width) {
183
- viewportResizeCb({
184
- width: Number(width),
185
- height: Number(height),
186
- });
187
- lastH = height;
188
- lastW = width;
189
- }
190
- }, 200);
191
- return on('resize', updateDimension, window);
192
- }
193
- function wrapEventWithUserTriggeredFlag(v, enable) {
194
- var value = __assign({}, v);
195
- if (!enable)
196
- delete value.userTriggered;
197
- return value;
198
- }
199
- var INPUT_TAGS = ['INPUT', 'TEXTAREA', 'SELECT'];
200
- var lastInputValueMap = new WeakMap();
201
- function initInputObserver(_a) {
202
- var inputCb = _a.inputCb, doc = _a.doc, mirror = _a.mirror, blockClass = _a.blockClass, ignoreClass = _a.ignoreClass, maskInputOptions = _a.maskInputOptions, maskInputFn = _a.maskInputFn, sampling = _a.sampling, userTriggeredOnInput = _a.userTriggeredOnInput;
203
- function eventHandler(event) {
204
- var target = getEventTarget(event);
205
- var userTriggered = event.isTrusted;
206
- if (target && target.tagName === 'OPTION')
207
- target = target.parentElement;
208
- if (!target ||
209
- !target.tagName ||
210
- INPUT_TAGS.indexOf(target.tagName) < 0 ||
211
- isBlocked(target, blockClass)) {
212
- return;
213
- }
214
- var type = target.type;
215
- if (target.classList.contains(ignoreClass)) {
216
- return;
217
- }
218
- var text = target.value;
219
- var isChecked = false;
220
- if (type === 'radio' || type === 'checkbox') {
221
- isChecked = target.checked;
222
- }
223
- else if (maskInputOptions[target.tagName.toLowerCase()] ||
224
- maskInputOptions[type]) {
225
- text = maskInputValue({
226
- maskInputOptions: maskInputOptions,
227
- tagName: target.tagName,
228
- type: type,
229
- value: text,
230
- maskInputFn: maskInputFn,
231
- });
232
- }
233
- cbWithDedup(target, wrapEventWithUserTriggeredFlag({ text: text, isChecked: isChecked, userTriggered: userTriggered }, userTriggeredOnInput));
234
- var name = target.name;
235
- if (type === 'radio' && name && isChecked) {
236
- doc
237
- .querySelectorAll("input[type=\"radio\"][name=\"".concat(name, "\"]"))
238
- .forEach(function (el) {
239
- if (el !== target) {
240
- cbWithDedup(el, wrapEventWithUserTriggeredFlag({
241
- text: el.value,
242
- isChecked: !isChecked,
243
- userTriggered: false,
244
- }, userTriggeredOnInput));
245
- }
246
- });
247
- }
248
- }
249
- function cbWithDedup(target, v) {
250
- var lastInputValue = lastInputValueMap.get(target);
251
- if (!lastInputValue ||
252
- lastInputValue.text !== v.text ||
253
- lastInputValue.isChecked !== v.isChecked) {
254
- lastInputValueMap.set(target, v);
255
- var id = mirror.getId(target);
256
- inputCb(__assign(__assign({}, v), { id: id }));
257
- }
258
- }
259
- var events = sampling.input === 'last' ? ['change'] : ['input', 'change'];
260
- var handlers = events.map(function (eventName) { return on(eventName, eventHandler, doc); });
261
- var propertyDescriptor = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value');
262
- var hookProperties = [
263
- [HTMLInputElement.prototype, 'value'],
264
- [HTMLInputElement.prototype, 'checked'],
265
- [HTMLSelectElement.prototype, 'value'],
266
- [HTMLTextAreaElement.prototype, 'value'],
267
- [HTMLSelectElement.prototype, 'selectedIndex'],
268
- [HTMLOptionElement.prototype, 'selected'],
269
- ];
270
- if (propertyDescriptor && propertyDescriptor.set) {
271
- handlers.push.apply(handlers, __spreadArray([], __read(hookProperties.map(function (p) {
272
- return hookSetter(p[0], p[1], {
273
- set: function () {
274
- eventHandler({ target: this });
275
- },
276
- });
277
- })), false));
278
- }
279
- return function () {
280
- handlers.forEach(function (h) { return h(); });
281
- };
282
- }
283
- function getNestedCSSRulePositions(rule) {
284
- var positions = [];
285
- function recurse(childRule, pos) {
286
- if ((isCSSGroupingRuleSupported &&
287
- childRule.parentRule instanceof CSSGroupingRule) ||
288
- (isCSSMediaRuleSupported &&
289
- childRule.parentRule instanceof CSSMediaRule) ||
290
- (isCSSSupportsRuleSupported &&
291
- childRule.parentRule instanceof CSSSupportsRule) ||
292
- (isCSSConditionRuleSupported &&
293
- childRule.parentRule instanceof CSSConditionRule)) {
294
- var rules = Array.from(childRule.parentRule.cssRules);
295
- var index = rules.indexOf(childRule);
296
- pos.unshift(index);
297
- }
298
- else {
299
- var rules = Array.from(childRule.parentStyleSheet.cssRules);
300
- var index = rules.indexOf(childRule);
301
- pos.unshift(index);
302
- }
303
- return pos;
304
- }
305
- return recurse(rule, positions);
306
- }
307
- function initStyleSheetObserver(_a, _b) {
308
- var styleSheetRuleCb = _a.styleSheetRuleCb, mirror = _a.mirror;
309
- var win = _b.win;
310
- var insertRule = win.CSSStyleSheet.prototype.insertRule;
311
- win.CSSStyleSheet.prototype.insertRule = function (rule, index) {
312
- var id = mirror.getId(this.ownerNode);
313
- if (id !== -1) {
314
- styleSheetRuleCb({
315
- id: id,
316
- adds: [{ rule: rule, index: index }],
317
- });
318
- }
319
- return insertRule.apply(this, arguments);
320
- };
321
- var deleteRule = win.CSSStyleSheet.prototype.deleteRule;
322
- win.CSSStyleSheet.prototype.deleteRule = function (index) {
323
- var id = mirror.getId(this.ownerNode);
324
- if (id !== -1) {
325
- styleSheetRuleCb({
326
- id: id,
327
- removes: [{ index: index }],
328
- });
329
- }
330
- return deleteRule.apply(this, arguments);
331
- };
332
- var supportedNestedCSSRuleTypes = {};
333
- if (isCSSGroupingRuleSupported) {
334
- supportedNestedCSSRuleTypes.CSSGroupingRule = win.CSSGroupingRule;
335
- }
336
- else {
337
- if (isCSSMediaRuleSupported) {
338
- supportedNestedCSSRuleTypes.CSSMediaRule = win.CSSMediaRule;
339
- }
340
- if (isCSSConditionRuleSupported) {
341
- supportedNestedCSSRuleTypes.CSSConditionRule = win.CSSConditionRule;
342
- }
343
- if (isCSSSupportsRuleSupported) {
344
- supportedNestedCSSRuleTypes.CSSSupportsRule = win.CSSSupportsRule;
345
- }
346
- }
347
- var unmodifiedFunctions = {};
348
- Object.entries(supportedNestedCSSRuleTypes).forEach(function (_a) {
349
- var _b = __read(_a, 2), typeKey = _b[0], type = _b[1];
350
- unmodifiedFunctions[typeKey] = {
351
- insertRule: type.prototype.insertRule,
352
- deleteRule: type.prototype.deleteRule,
353
- };
354
- type.prototype.insertRule = function (rule, index) {
355
- var id = mirror.getId(this.parentStyleSheet.ownerNode);
356
- if (id !== -1) {
357
- styleSheetRuleCb({
358
- id: id,
359
- adds: [
360
- {
361
- rule: rule,
362
- index: __spreadArray(__spreadArray([], __read(getNestedCSSRulePositions(this)), false), [
363
- index || 0,
364
- ], false),
365
- },
366
- ],
367
- });
368
- }
369
- return unmodifiedFunctions[typeKey].insertRule.apply(this, arguments);
370
- };
371
- type.prototype.deleteRule = function (index) {
372
- var id = mirror.getId(this.parentStyleSheet.ownerNode);
373
- if (id !== -1) {
374
- styleSheetRuleCb({
375
- id: id,
376
- removes: [{ index: __spreadArray(__spreadArray([], __read(getNestedCSSRulePositions(this)), false), [index], false) }],
377
- });
378
- }
379
- return unmodifiedFunctions[typeKey].deleteRule.apply(this, arguments);
380
- };
381
- });
382
- return function () {
383
- win.CSSStyleSheet.prototype.insertRule = insertRule;
384
- win.CSSStyleSheet.prototype.deleteRule = deleteRule;
385
- Object.entries(supportedNestedCSSRuleTypes).forEach(function (_a) {
386
- var _b = __read(_a, 2), typeKey = _b[0], type = _b[1];
387
- type.prototype.insertRule = unmodifiedFunctions[typeKey].insertRule;
388
- type.prototype.deleteRule = unmodifiedFunctions[typeKey].deleteRule;
389
- });
390
- };
391
- }
392
- function initStyleDeclarationObserver(_a, _b) {
393
- var styleDeclarationCb = _a.styleDeclarationCb, mirror = _a.mirror;
394
- var win = _b.win;
395
- var setProperty = win.CSSStyleDeclaration.prototype.setProperty;
396
- win.CSSStyleDeclaration.prototype.setProperty = function (property, value, priority) {
397
- var _a, _b;
398
- var id = mirror.getId((_b = (_a = this.parentRule) === null || _a === void 0 ? void 0 : _a.parentStyleSheet) === null || _b === void 0 ? void 0 : _b.ownerNode);
399
- if (id !== -1) {
400
- styleDeclarationCb({
401
- id: id,
402
- set: {
403
- property: property,
404
- value: value,
405
- priority: priority,
406
- },
407
- index: getNestedCSSRulePositions(this.parentRule),
408
- });
409
- }
410
- return setProperty.apply(this, arguments);
411
- };
412
- var removeProperty = win.CSSStyleDeclaration.prototype.removeProperty;
413
- win.CSSStyleDeclaration.prototype.removeProperty = function (property) {
414
- var _a, _b;
415
- var id = mirror.getId((_b = (_a = this.parentRule) === null || _a === void 0 ? void 0 : _a.parentStyleSheet) === null || _b === void 0 ? void 0 : _b.ownerNode);
416
- if (id !== -1) {
417
- styleDeclarationCb({
418
- id: id,
419
- remove: {
420
- property: property,
421
- },
422
- index: getNestedCSSRulePositions(this.parentRule),
423
- });
424
- }
425
- return removeProperty.apply(this, arguments);
426
- };
427
- return function () {
428
- win.CSSStyleDeclaration.prototype.setProperty = setProperty;
429
- win.CSSStyleDeclaration.prototype.removeProperty = removeProperty;
430
- };
431
- }
432
- function initMediaInteractionObserver(_a) {
433
- var mediaInteractionCb = _a.mediaInteractionCb, blockClass = _a.blockClass, mirror = _a.mirror, sampling = _a.sampling;
434
- var handler = function (type) {
435
- return throttle(function (event) {
436
- var target = getEventTarget(event);
437
- if (!target || isBlocked(target, blockClass)) {
438
- return;
439
- }
440
- var _a = target, currentTime = _a.currentTime, volume = _a.volume, muted = _a.muted;
441
- mediaInteractionCb({
442
- type: type,
443
- id: mirror.getId(target),
444
- currentTime: currentTime,
445
- volume: volume,
446
- muted: muted,
447
- });
448
- }, sampling.media || 500);
449
- };
450
- var handlers = [
451
- on('play', handler(0)),
452
- on('pause', handler(1)),
453
- on('seeked', handler(2)),
454
- on('volumechange', handler(3)),
455
- ];
456
- return function () {
457
- handlers.forEach(function (h) { return h(); });
458
- };
459
- }
460
- function initFontObserver(_a) {
461
- var fontCb = _a.fontCb, doc = _a.doc;
462
- var win = doc.defaultView;
463
- if (!win) {
464
- return function () { };
465
- }
466
- var handlers = [];
467
- var fontMap = new WeakMap();
468
- var originalFontFace = win.FontFace;
469
- win.FontFace = function FontFace(family, source, descriptors) {
470
- var fontFace = new originalFontFace(family, source, descriptors);
471
- fontMap.set(fontFace, {
472
- family: family,
473
- buffer: typeof source !== 'string',
474
- descriptors: descriptors,
475
- fontSource: typeof source === 'string'
476
- ? source
477
- :
478
- JSON.stringify(Array.from(new Uint8Array(source))),
479
- });
480
- return fontFace;
481
- };
482
- var restoreHandler = patch(doc.fonts, 'add', function (original) {
483
- return function (fontFace) {
484
- setTimeout(function () {
485
- var p = fontMap.get(fontFace);
486
- if (p) {
487
- fontCb(p);
488
- fontMap.delete(fontFace);
489
- }
490
- }, 0);
491
- return original.apply(this, [fontFace]);
492
- };
493
- });
494
- handlers.push(function () {
495
- win.FontFace = originalFontFace;
496
- });
497
- handlers.push(restoreHandler);
498
- return function () {
499
- handlers.forEach(function (h) { return h(); });
500
- };
501
- }
502
- function mergeHooks(o, hooks) {
503
- var mutationCb = o.mutationCb, mousemoveCb = o.mousemoveCb, mouseInteractionCb = o.mouseInteractionCb, scrollCb = o.scrollCb, viewportResizeCb = o.viewportResizeCb, inputCb = o.inputCb, mediaInteractionCb = o.mediaInteractionCb, styleSheetRuleCb = o.styleSheetRuleCb, styleDeclarationCb = o.styleDeclarationCb, canvasMutationCb = o.canvasMutationCb, fontCb = o.fontCb;
504
- o.mutationCb = function () {
505
- var p = [];
506
- for (var _i = 0; _i < arguments.length; _i++) {
507
- p[_i] = arguments[_i];
508
- }
509
- if (hooks.mutation) {
510
- hooks.mutation.apply(hooks, __spreadArray([], __read(p), false));
511
- }
512
- mutationCb.apply(void 0, __spreadArray([], __read(p), false));
513
- };
514
- o.mousemoveCb = function () {
515
- var p = [];
516
- for (var _i = 0; _i < arguments.length; _i++) {
517
- p[_i] = arguments[_i];
518
- }
519
- if (hooks.mousemove) {
520
- hooks.mousemove.apply(hooks, __spreadArray([], __read(p), false));
521
- }
522
- mousemoveCb.apply(void 0, __spreadArray([], __read(p), false));
523
- };
524
- o.mouseInteractionCb = function () {
525
- var p = [];
526
- for (var _i = 0; _i < arguments.length; _i++) {
527
- p[_i] = arguments[_i];
528
- }
529
- if (hooks.mouseInteraction) {
530
- hooks.mouseInteraction.apply(hooks, __spreadArray([], __read(p), false));
531
- }
532
- mouseInteractionCb.apply(void 0, __spreadArray([], __read(p), false));
533
- };
534
- o.scrollCb = function () {
535
- var p = [];
536
- for (var _i = 0; _i < arguments.length; _i++) {
537
- p[_i] = arguments[_i];
538
- }
539
- if (hooks.scroll) {
540
- hooks.scroll.apply(hooks, __spreadArray([], __read(p), false));
541
- }
542
- scrollCb.apply(void 0, __spreadArray([], __read(p), false));
543
- };
544
- o.viewportResizeCb = function () {
545
- var p = [];
546
- for (var _i = 0; _i < arguments.length; _i++) {
547
- p[_i] = arguments[_i];
548
- }
549
- if (hooks.viewportResize) {
550
- hooks.viewportResize.apply(hooks, __spreadArray([], __read(p), false));
551
- }
552
- viewportResizeCb.apply(void 0, __spreadArray([], __read(p), false));
553
- };
554
- o.inputCb = function () {
555
- var p = [];
556
- for (var _i = 0; _i < arguments.length; _i++) {
557
- p[_i] = arguments[_i];
558
- }
559
- if (hooks.input) {
560
- hooks.input.apply(hooks, __spreadArray([], __read(p), false));
561
- }
562
- inputCb.apply(void 0, __spreadArray([], __read(p), false));
563
- };
564
- o.mediaInteractionCb = function () {
565
- var p = [];
566
- for (var _i = 0; _i < arguments.length; _i++) {
567
- p[_i] = arguments[_i];
568
- }
569
- if (hooks.mediaInteaction) {
570
- hooks.mediaInteaction.apply(hooks, __spreadArray([], __read(p), false));
571
- }
572
- mediaInteractionCb.apply(void 0, __spreadArray([], __read(p), false));
573
- };
574
- o.styleSheetRuleCb = function () {
575
- var p = [];
576
- for (var _i = 0; _i < arguments.length; _i++) {
577
- p[_i] = arguments[_i];
578
- }
579
- if (hooks.styleSheetRule) {
580
- hooks.styleSheetRule.apply(hooks, __spreadArray([], __read(p), false));
581
- }
582
- styleSheetRuleCb.apply(void 0, __spreadArray([], __read(p), false));
583
- };
584
- o.styleDeclarationCb = function () {
585
- var p = [];
586
- for (var _i = 0; _i < arguments.length; _i++) {
587
- p[_i] = arguments[_i];
588
- }
589
- if (hooks.styleDeclaration) {
590
- hooks.styleDeclaration.apply(hooks, __spreadArray([], __read(p), false));
591
- }
592
- styleDeclarationCb.apply(void 0, __spreadArray([], __read(p), false));
593
- };
594
- o.canvasMutationCb = function () {
595
- var p = [];
596
- for (var _i = 0; _i < arguments.length; _i++) {
597
- p[_i] = arguments[_i];
598
- }
599
- if (hooks.canvasMutation) {
600
- hooks.canvasMutation.apply(hooks, __spreadArray([], __read(p), false));
601
- }
602
- canvasMutationCb.apply(void 0, __spreadArray([], __read(p), false));
603
- };
604
- o.fontCb = function () {
605
- var p = [];
606
- for (var _i = 0; _i < arguments.length; _i++) {
607
- p[_i] = arguments[_i];
608
- }
609
- if (hooks.font) {
610
- hooks.font.apply(hooks, __spreadArray([], __read(p), false));
611
- }
612
- fontCb.apply(void 0, __spreadArray([], __read(p), false));
613
- };
614
- }
615
- function initObservers(o, hooks) {
616
- var e_1, _a;
617
- if (hooks === void 0) { hooks = {}; }
618
- var currentWindow = o.doc.defaultView;
619
- if (!currentWindow) {
620
- return function () { };
621
- }
622
- mergeHooks(o, hooks);
623
- var mutationObserver = initMutationObserver(o, o.doc);
624
- var mousemoveHandler = initMoveObserver(o);
625
- var mouseInteractionHandler = initMouseInteractionObserver(o);
626
- var scrollHandler = initScrollObserver(o);
627
- var viewportResizeHandler = initViewportResizeObserver(o);
628
- var inputHandler = initInputObserver(o);
629
- var mediaInteractionHandler = initMediaInteractionObserver(o);
630
- var styleSheetObserver = initStyleSheetObserver(o, { win: currentWindow });
631
- var styleDeclarationObserver = initStyleDeclarationObserver(o, {
632
- win: currentWindow,
633
- });
634
- var fontObserver = o.collectFonts ? initFontObserver(o) : function () { };
635
- var pluginHandlers = [];
636
- try {
637
- for (var _b = __values(o.plugins), _c = _b.next(); !_c.done; _c = _b.next()) {
638
- var plugin = _c.value;
639
- pluginHandlers.push(plugin.observer(plugin.callback, currentWindow, plugin.options));
640
- }
641
- }
642
- catch (e_1_1) { e_1 = { error: e_1_1 }; }
643
- finally {
644
- try {
645
- if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
646
- }
647
- finally { if (e_1) throw e_1.error; }
648
- }
649
- return function () {
650
- mutationBuffers.forEach(function (b) { return b.reset(); });
651
- mutationObserver.disconnect();
652
- mousemoveHandler();
653
- mouseInteractionHandler();
654
- scrollHandler();
655
- viewportResizeHandler();
656
- inputHandler();
657
- mediaInteractionHandler();
658
- styleSheetObserver();
659
- styleDeclarationObserver();
660
- fontObserver();
661
- pluginHandlers.forEach(function (h) { return h(); });
662
- };
6
+ const mutationBuffers = [];
7
+ const isCSSGroupingRuleSupported = typeof CSSGroupingRule !== 'undefined';
8
+ const isCSSMediaRuleSupported = typeof CSSMediaRule !== 'undefined';
9
+ const isCSSSupportsRuleSupported = typeof CSSSupportsRule !== 'undefined';
10
+ const isCSSConditionRuleSupported = typeof CSSConditionRule !== 'undefined';
11
+ function getEventTarget(event) {
12
+ try {
13
+ if ('composedPath' in event) {
14
+ const path = event.composedPath();
15
+ if (path.length) {
16
+ return path[0];
17
+ }
18
+ }
19
+ else if ('path' in event && event.path.length) {
20
+ return event.path[0];
21
+ }
22
+ return event.target;
23
+ }
24
+ catch (_a) {
25
+ return event.target;
26
+ }
27
+ }
28
+ function initMutationObserver(options, rootEl) {
29
+ var _a, _b;
30
+ const mutationBuffer = new MutationBuffer();
31
+ mutationBuffers.push(mutationBuffer);
32
+ mutationBuffer.init(options);
33
+ let mutationObserverCtor = window.MutationObserver ||
34
+ window.__rrMutationObserver;
35
+ const angularZoneSymbol = (_b = (_a = window === null || window === void 0 ? void 0 : window.Zone) === null || _a === void 0 ? void 0 : _a.__symbol__) === null || _b === void 0 ? void 0 : _b.call(_a, 'MutationObserver');
36
+ if (angularZoneSymbol &&
37
+ window[angularZoneSymbol]) {
38
+ mutationObserverCtor = window[angularZoneSymbol];
39
+ }
40
+ const observer = new mutationObserverCtor(mutationBuffer.processMutations.bind(mutationBuffer));
41
+ observer.observe(rootEl, {
42
+ attributes: true,
43
+ attributeOldValue: true,
44
+ characterData: true,
45
+ characterDataOldValue: true,
46
+ childList: true,
47
+ subtree: true,
48
+ });
49
+ return observer;
50
+ }
51
+ function initMoveObserver({ mousemoveCb, sampling, doc, mirror, }) {
52
+ if (sampling.mousemove === false) {
53
+ return () => { };
54
+ }
55
+ const threshold = typeof sampling.mousemove === 'number' ? sampling.mousemove : 50;
56
+ const callbackThreshold = typeof sampling.mousemoveCallback === 'number'
57
+ ? sampling.mousemoveCallback
58
+ : 500;
59
+ let positions = [];
60
+ let timeBaseline;
61
+ const wrappedCb = throttle((source) => {
62
+ const totalOffset = Date.now() - timeBaseline;
63
+ mousemoveCb(positions.map((p) => {
64
+ p.timeOffset -= totalOffset;
65
+ return p;
66
+ }), source);
67
+ positions = [];
68
+ timeBaseline = null;
69
+ }, callbackThreshold);
70
+ const updatePosition = throttle((evt) => {
71
+ const target = getEventTarget(evt);
72
+ const { clientX, clientY } = isTouchEvent(evt)
73
+ ? evt.changedTouches[0]
74
+ : evt;
75
+ if (!timeBaseline) {
76
+ timeBaseline = Date.now();
77
+ }
78
+ positions.push({
79
+ x: clientX,
80
+ y: clientY,
81
+ id: mirror.getId(target),
82
+ timeOffset: Date.now() - timeBaseline,
83
+ });
84
+ wrappedCb(typeof DragEvent !== 'undefined' && evt instanceof DragEvent
85
+ ? IncrementalSource.Drag
86
+ : evt instanceof MouseEvent
87
+ ? IncrementalSource.MouseMove
88
+ : IncrementalSource.TouchMove);
89
+ }, threshold, {
90
+ trailing: false,
91
+ });
92
+ const handlers = [
93
+ on('mousemove', updatePosition, doc),
94
+ on('touchmove', updatePosition, doc),
95
+ on('drag', updatePosition, doc),
96
+ ];
97
+ return () => {
98
+ handlers.forEach((h) => h());
99
+ };
100
+ }
101
+ function initMouseInteractionObserver({ mouseInteractionCb, doc, mirror, blockClass, sampling, }) {
102
+ if (sampling.mouseInteraction === false) {
103
+ return () => { };
104
+ }
105
+ const disableMap = sampling.mouseInteraction === true ||
106
+ sampling.mouseInteraction === undefined
107
+ ? {}
108
+ : sampling.mouseInteraction;
109
+ const handlers = [];
110
+ const getHandler = (eventKey) => {
111
+ return (event) => {
112
+ const target = getEventTarget(event);
113
+ if (isBlocked(target, blockClass) ||
114
+ isCanvasNode(target)) {
115
+ return;
116
+ }
117
+ const e = isTouchEvent(event) ? event.changedTouches[0] : event;
118
+ if (!e) {
119
+ return;
120
+ }
121
+ const id = mirror.getId(target);
122
+ const { clientX, clientY } = e;
123
+ mouseInteractionCb({
124
+ type: MouseInteractions[eventKey],
125
+ id,
126
+ x: clientX,
127
+ y: clientY,
128
+ });
129
+ };
130
+ };
131
+ Object.keys(MouseInteractions)
132
+ .filter((key) => Number.isNaN(Number(key)) &&
133
+ !key.endsWith('_Departed') &&
134
+ disableMap[key] !== false)
135
+ .forEach((eventKey) => {
136
+ const eventName = eventKey.toLowerCase();
137
+ const handler = getHandler(eventKey);
138
+ handlers.push(on(eventName, handler, doc));
139
+ });
140
+ return () => {
141
+ handlers.forEach((h) => h());
142
+ };
143
+ }
144
+ function initScrollObserver({ scrollCb, doc, mirror, blockClass, sampling, }) {
145
+ const updatePosition = throttle((evt) => {
146
+ const target = getEventTarget(evt);
147
+ if (!target || isBlocked(target, blockClass)) {
148
+ return;
149
+ }
150
+ const id = mirror.getId(target);
151
+ if (target === doc) {
152
+ const scrollEl = (doc.scrollingElement || doc.documentElement);
153
+ scrollCb({
154
+ id,
155
+ x: scrollEl.scrollLeft,
156
+ y: scrollEl.scrollTop,
157
+ });
158
+ }
159
+ else {
160
+ scrollCb({
161
+ id,
162
+ x: target.scrollLeft,
163
+ y: target.scrollTop,
164
+ });
165
+ }
166
+ }, sampling.scroll || 100);
167
+ return on('scroll', updatePosition, doc);
168
+ }
169
+ function initViewportResizeObserver({ viewportResizeCb, }) {
170
+ let lastH = -1;
171
+ let lastW = -1;
172
+ const updateDimension = throttle(() => {
173
+ const height = getWindowHeight();
174
+ const width = getWindowWidth();
175
+ if (lastH !== height || lastW !== width) {
176
+ viewportResizeCb({
177
+ width: Number(width),
178
+ height: Number(height),
179
+ });
180
+ lastH = height;
181
+ lastW = width;
182
+ }
183
+ }, 200);
184
+ return on('resize', updateDimension, window);
185
+ }
186
+ function wrapEventWithUserTriggeredFlag(v, enable) {
187
+ const value = Object.assign({}, v);
188
+ if (!enable)
189
+ delete value.userTriggered;
190
+ return value;
191
+ }
192
+ const INPUT_TAGS = ['INPUT', 'TEXTAREA', 'SELECT'];
193
+ const lastInputValueMap = new WeakMap();
194
+ function initInputObserver({ inputCb, doc, mirror, blockClass, ignoreClass, maskInputOptions, maskInputFn, sampling, userTriggeredOnInput, }) {
195
+ function eventHandler(event) {
196
+ let target = getEventTarget(event);
197
+ const userTriggered = event.isTrusted;
198
+ if (target && target.tagName === 'OPTION')
199
+ target = target.parentElement;
200
+ if (!target ||
201
+ !target.tagName ||
202
+ INPUT_TAGS.indexOf(target.tagName) < 0 ||
203
+ isBlocked(target, blockClass)) {
204
+ return;
205
+ }
206
+ const type = target.type;
207
+ if (target.classList.contains(ignoreClass)) {
208
+ return;
209
+ }
210
+ let text = target.value;
211
+ let isChecked = false;
212
+ if (type === 'radio' || type === 'checkbox') {
213
+ isChecked = target.checked;
214
+ }
215
+ else if (maskInputOptions[target.tagName.toLowerCase()] ||
216
+ maskInputOptions[type]) {
217
+ text = maskInputValue({
218
+ maskInputOptions,
219
+ tagName: target.tagName,
220
+ type,
221
+ value: text,
222
+ maskInputFn,
223
+ });
224
+ }
225
+ cbWithDedup(target, wrapEventWithUserTriggeredFlag({ text, isChecked, userTriggered }, userTriggeredOnInput));
226
+ const name = target.name;
227
+ if (type === 'radio' && name && isChecked) {
228
+ doc
229
+ .querySelectorAll(`input[type="radio"][name="${name}"]`)
230
+ .forEach((el) => {
231
+ if (el !== target) {
232
+ cbWithDedup(el, wrapEventWithUserTriggeredFlag({
233
+ text: el.value,
234
+ isChecked: !isChecked,
235
+ userTriggered: false,
236
+ }, userTriggeredOnInput));
237
+ }
238
+ });
239
+ }
240
+ }
241
+ function cbWithDedup(target, v) {
242
+ const lastInputValue = lastInputValueMap.get(target);
243
+ if (!lastInputValue ||
244
+ lastInputValue.text !== v.text ||
245
+ lastInputValue.isChecked !== v.isChecked) {
246
+ lastInputValueMap.set(target, v);
247
+ const id = mirror.getId(target);
248
+ inputCb(Object.assign(Object.assign({}, v), { id }));
249
+ }
250
+ }
251
+ const events = sampling.input === 'last' ? ['change'] : ['input', 'change'];
252
+ const handlers = events.map((eventName) => on(eventName, eventHandler, doc));
253
+ const propertyDescriptor = Object.getOwnPropertyDescriptor(HTMLInputElement.prototype, 'value');
254
+ const hookProperties = [
255
+ [HTMLInputElement.prototype, 'value'],
256
+ [HTMLInputElement.prototype, 'checked'],
257
+ [HTMLSelectElement.prototype, 'value'],
258
+ [HTMLTextAreaElement.prototype, 'value'],
259
+ [HTMLSelectElement.prototype, 'selectedIndex'],
260
+ [HTMLOptionElement.prototype, 'selected'],
261
+ ];
262
+ if (propertyDescriptor && propertyDescriptor.set) {
263
+ handlers.push(...hookProperties.map((p) => hookSetter(p[0], p[1], {
264
+ set() {
265
+ eventHandler({ target: this });
266
+ },
267
+ })));
268
+ }
269
+ return () => {
270
+ handlers.forEach((h) => h());
271
+ };
272
+ }
273
+ function getNestedCSSRulePositions(rule) {
274
+ const positions = [];
275
+ function recurse(childRule, pos) {
276
+ if ((isCSSGroupingRuleSupported &&
277
+ childRule.parentRule instanceof CSSGroupingRule) ||
278
+ (isCSSMediaRuleSupported &&
279
+ childRule.parentRule instanceof CSSMediaRule) ||
280
+ (isCSSSupportsRuleSupported &&
281
+ childRule.parentRule instanceof CSSSupportsRule) ||
282
+ (isCSSConditionRuleSupported &&
283
+ childRule.parentRule instanceof CSSConditionRule)) {
284
+ const rules = Array.from(childRule.parentRule.cssRules);
285
+ const index = rules.indexOf(childRule);
286
+ pos.unshift(index);
287
+ }
288
+ else {
289
+ const rules = Array.from(childRule.parentStyleSheet.cssRules);
290
+ const index = rules.indexOf(childRule);
291
+ pos.unshift(index);
292
+ }
293
+ return pos;
294
+ }
295
+ return recurse(rule, positions);
296
+ }
297
+ function initStyleSheetObserver({ styleSheetRuleCb, mirror }, { win }) {
298
+ const insertRule = win.CSSStyleSheet.prototype.insertRule;
299
+ win.CSSStyleSheet.prototype.insertRule = function (rule, index) {
300
+ const id = mirror.getId(this.ownerNode);
301
+ if (id !== -1) {
302
+ styleSheetRuleCb({
303
+ id,
304
+ adds: [{ rule, index }],
305
+ });
306
+ }
307
+ return insertRule.apply(this, arguments);
308
+ };
309
+ const deleteRule = win.CSSStyleSheet.prototype.deleteRule;
310
+ win.CSSStyleSheet.prototype.deleteRule = function (index) {
311
+ const id = mirror.getId(this.ownerNode);
312
+ if (id !== -1) {
313
+ styleSheetRuleCb({
314
+ id,
315
+ removes: [{ index }],
316
+ });
317
+ }
318
+ return deleteRule.apply(this, arguments);
319
+ };
320
+ const supportedNestedCSSRuleTypes = {};
321
+ if (isCSSGroupingRuleSupported) {
322
+ supportedNestedCSSRuleTypes.CSSGroupingRule = win.CSSGroupingRule;
323
+ }
324
+ else {
325
+ if (isCSSMediaRuleSupported) {
326
+ supportedNestedCSSRuleTypes.CSSMediaRule = win.CSSMediaRule;
327
+ }
328
+ if (isCSSConditionRuleSupported) {
329
+ supportedNestedCSSRuleTypes.CSSConditionRule = win.CSSConditionRule;
330
+ }
331
+ if (isCSSSupportsRuleSupported) {
332
+ supportedNestedCSSRuleTypes.CSSSupportsRule = win.CSSSupportsRule;
333
+ }
334
+ }
335
+ const unmodifiedFunctions = {};
336
+ Object.entries(supportedNestedCSSRuleTypes).forEach(([typeKey, type]) => {
337
+ unmodifiedFunctions[typeKey] = {
338
+ insertRule: (type).prototype.insertRule,
339
+ deleteRule: (type).prototype.deleteRule,
340
+ };
341
+ type.prototype.insertRule = function (rule, index) {
342
+ const id = mirror.getId(this.parentStyleSheet.ownerNode);
343
+ if (id !== -1) {
344
+ styleSheetRuleCb({
345
+ id,
346
+ adds: [
347
+ {
348
+ rule,
349
+ index: [
350
+ ...getNestedCSSRulePositions(this),
351
+ index || 0,
352
+ ],
353
+ },
354
+ ],
355
+ });
356
+ }
357
+ return unmodifiedFunctions[typeKey].insertRule.apply(this, arguments);
358
+ };
359
+ type.prototype.deleteRule = function (index) {
360
+ const id = mirror.getId(this.parentStyleSheet.ownerNode);
361
+ if (id !== -1) {
362
+ styleSheetRuleCb({
363
+ id,
364
+ removes: [{ index: [...getNestedCSSRulePositions(this), index] }],
365
+ });
366
+ }
367
+ return unmodifiedFunctions[typeKey].deleteRule.apply(this, arguments);
368
+ };
369
+ });
370
+ return () => {
371
+ win.CSSStyleSheet.prototype.insertRule = insertRule;
372
+ win.CSSStyleSheet.prototype.deleteRule = deleteRule;
373
+ Object.entries(supportedNestedCSSRuleTypes).forEach(([typeKey, type]) => {
374
+ type.prototype.insertRule = unmodifiedFunctions[typeKey].insertRule;
375
+ type.prototype.deleteRule = unmodifiedFunctions[typeKey].deleteRule;
376
+ });
377
+ };
378
+ }
379
+ function initStyleDeclarationObserver({ styleDeclarationCb, mirror }, { win }) {
380
+ const setProperty = win.CSSStyleDeclaration.prototype.setProperty;
381
+ win.CSSStyleDeclaration.prototype.setProperty = function (property, value, priority) {
382
+ var _a, _b;
383
+ const id = mirror.getId((_b = (_a = this.parentRule) === null || _a === void 0 ? void 0 : _a.parentStyleSheet) === null || _b === void 0 ? void 0 : _b.ownerNode);
384
+ if (id !== -1) {
385
+ styleDeclarationCb({
386
+ id,
387
+ set: {
388
+ property,
389
+ value,
390
+ priority,
391
+ },
392
+ index: getNestedCSSRulePositions(this.parentRule),
393
+ });
394
+ }
395
+ return setProperty.apply(this, arguments);
396
+ };
397
+ const removeProperty = win.CSSStyleDeclaration.prototype.removeProperty;
398
+ win.CSSStyleDeclaration.prototype.removeProperty = function (property) {
399
+ var _a, _b;
400
+ const id = mirror.getId((_b = (_a = this.parentRule) === null || _a === void 0 ? void 0 : _a.parentStyleSheet) === null || _b === void 0 ? void 0 : _b.ownerNode);
401
+ if (id !== -1) {
402
+ styleDeclarationCb({
403
+ id,
404
+ remove: {
405
+ property,
406
+ },
407
+ index: getNestedCSSRulePositions(this.parentRule),
408
+ });
409
+ }
410
+ return removeProperty.apply(this, arguments);
411
+ };
412
+ return () => {
413
+ win.CSSStyleDeclaration.prototype.setProperty = setProperty;
414
+ win.CSSStyleDeclaration.prototype.removeProperty = removeProperty;
415
+ };
416
+ }
417
+ function initMediaInteractionObserver({ mediaInteractionCb, blockClass, mirror, sampling, }) {
418
+ const handler = (type) => throttle((event) => {
419
+ const target = getEventTarget(event);
420
+ if (!target || isBlocked(target, blockClass)) {
421
+ return;
422
+ }
423
+ const { currentTime, volume, muted } = target;
424
+ mediaInteractionCb({
425
+ type,
426
+ id: mirror.getId(target),
427
+ currentTime,
428
+ volume,
429
+ muted,
430
+ });
431
+ }, sampling.media || 500);
432
+ const handlers = [
433
+ on('play', handler(0)),
434
+ on('pause', handler(1)),
435
+ on('seeked', handler(2)),
436
+ on('volumechange', handler(3)),
437
+ ];
438
+ return () => {
439
+ handlers.forEach((h) => h());
440
+ };
441
+ }
442
+ function initFontObserver({ fontCb, doc }) {
443
+ const win = doc.defaultView;
444
+ if (!win) {
445
+ return () => { };
446
+ }
447
+ const handlers = [];
448
+ const fontMap = new WeakMap();
449
+ const originalFontFace = win.FontFace;
450
+ win.FontFace = function FontFace(family, source, descriptors) {
451
+ const fontFace = new originalFontFace(family, source, descriptors);
452
+ fontMap.set(fontFace, {
453
+ family,
454
+ buffer: typeof source !== 'string',
455
+ descriptors,
456
+ fontSource: typeof source === 'string'
457
+ ? source
458
+ :
459
+ JSON.stringify(Array.from(new Uint8Array(source))),
460
+ });
461
+ return fontFace;
462
+ };
463
+ const restoreHandler = patch(doc.fonts, 'add', function (original) {
464
+ return function (fontFace) {
465
+ setTimeout(() => {
466
+ const p = fontMap.get(fontFace);
467
+ if (p) {
468
+ fontCb(p);
469
+ fontMap.delete(fontFace);
470
+ }
471
+ }, 0);
472
+ return original.apply(this, [fontFace]);
473
+ };
474
+ });
475
+ handlers.push(() => {
476
+ win.FontFace = originalFontFace;
477
+ });
478
+ handlers.push(restoreHandler);
479
+ return () => {
480
+ handlers.forEach((h) => h());
481
+ };
482
+ }
483
+ function mergeHooks(o, hooks) {
484
+ const { mutationCb, mousemoveCb, mouseInteractionCb, scrollCb, viewportResizeCb, inputCb, mediaInteractionCb, styleSheetRuleCb, styleDeclarationCb, canvasMutationCb, fontCb, } = o;
485
+ o.mutationCb = (...p) => {
486
+ if (hooks.mutation) {
487
+ hooks.mutation(...p);
488
+ }
489
+ mutationCb(...p);
490
+ };
491
+ o.mousemoveCb = (...p) => {
492
+ if (hooks.mousemove) {
493
+ hooks.mousemove(...p);
494
+ }
495
+ mousemoveCb(...p);
496
+ };
497
+ o.mouseInteractionCb = (...p) => {
498
+ if (hooks.mouseInteraction) {
499
+ hooks.mouseInteraction(...p);
500
+ }
501
+ mouseInteractionCb(...p);
502
+ };
503
+ o.scrollCb = (...p) => {
504
+ if (hooks.scroll) {
505
+ hooks.scroll(...p);
506
+ }
507
+ scrollCb(...p);
508
+ };
509
+ o.viewportResizeCb = (...p) => {
510
+ if (hooks.viewportResize) {
511
+ hooks.viewportResize(...p);
512
+ }
513
+ viewportResizeCb(...p);
514
+ };
515
+ o.inputCb = (...p) => {
516
+ if (hooks.input) {
517
+ hooks.input(...p);
518
+ }
519
+ inputCb(...p);
520
+ };
521
+ o.mediaInteractionCb = (...p) => {
522
+ if (hooks.mediaInteaction) {
523
+ hooks.mediaInteaction(...p);
524
+ }
525
+ mediaInteractionCb(...p);
526
+ };
527
+ o.styleSheetRuleCb = (...p) => {
528
+ if (hooks.styleSheetRule) {
529
+ hooks.styleSheetRule(...p);
530
+ }
531
+ styleSheetRuleCb(...p);
532
+ };
533
+ o.styleDeclarationCb = (...p) => {
534
+ if (hooks.styleDeclaration) {
535
+ hooks.styleDeclaration(...p);
536
+ }
537
+ styleDeclarationCb(...p);
538
+ };
539
+ o.canvasMutationCb = (...p) => {
540
+ if (hooks.canvasMutation) {
541
+ hooks.canvasMutation(...p);
542
+ }
543
+ canvasMutationCb(...p);
544
+ };
545
+ o.fontCb = (...p) => {
546
+ if (hooks.font) {
547
+ hooks.font(...p);
548
+ }
549
+ fontCb(...p);
550
+ };
551
+ }
552
+ function initObservers(o, hooks = {}) {
553
+ const currentWindow = o.doc.defaultView;
554
+ if (!currentWindow) {
555
+ return () => { };
556
+ }
557
+ mergeHooks(o, hooks);
558
+ const mutationObserver = initMutationObserver(o, o.doc);
559
+ const mousemoveHandler = initMoveObserver(o);
560
+ const mouseInteractionHandler = initMouseInteractionObserver(o);
561
+ const scrollHandler = initScrollObserver(o);
562
+ const viewportResizeHandler = initViewportResizeObserver(o);
563
+ const inputHandler = initInputObserver(o);
564
+ const mediaInteractionHandler = initMediaInteractionObserver(o);
565
+ const styleSheetObserver = initStyleSheetObserver(o, { win: currentWindow });
566
+ const styleDeclarationObserver = initStyleDeclarationObserver(o, {
567
+ win: currentWindow,
568
+ });
569
+ const fontObserver = o.collectFonts ? initFontObserver(o) : () => { };
570
+ const pluginHandlers = [];
571
+ for (const plugin of o.plugins) {
572
+ pluginHandlers.push(plugin.observer(plugin.callback, currentWindow, plugin.options));
573
+ }
574
+ return () => {
575
+ mutationBuffers.forEach((b) => b.reset());
576
+ mutationObserver.disconnect();
577
+ mousemoveHandler();
578
+ mouseInteractionHandler();
579
+ scrollHandler();
580
+ viewportResizeHandler();
581
+ inputHandler();
582
+ mediaInteractionHandler();
583
+ styleSheetObserver();
584
+ styleDeclarationObserver();
585
+ fontObserver();
586
+ pluginHandlers.forEach((h) => h());
587
+ };
663
588
  }
664
589
 
665
590
  export { INPUT_TAGS, initMutationObserver, initObservers, initScrollObserver, mutationBuffers };