@cloudcare/browser-rum 2.2.15 → 3.0.0

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 (166) hide show
  1. package/bundle/dataflux-rum.js +1 -1
  2. package/cjs/boot/buildEnv.js +1 -1
  3. package/cjs/boot/recorderApi.js +161 -0
  4. package/cjs/boot/rum.entry.js +6 -1
  5. package/cjs/boot/rum.js +15 -5
  6. package/cjs/boot/rumPublicApi.js +8 -4
  7. package/cjs/boot/startRecording.js +55 -0
  8. package/cjs/constants.js +52 -0
  9. package/cjs/domain/assembly.js +1 -7
  10. package/cjs/domain/configuration.js +3 -2
  11. package/cjs/domain/contexts/foregroundContexts.js +138 -0
  12. package/cjs/domain/contexts/internalContext.js +1 -1
  13. package/cjs/domain/contexts/urlContexts.js +9 -2
  14. package/cjs/domain/domMutationCollection.js +4 -5
  15. package/cjs/domain/initViewportObservable.js +52 -0
  16. package/cjs/domain/locationChangeObservable.js +12 -26
  17. package/cjs/domain/performanceCollection.js +2 -13
  18. package/cjs/domain/replay/record/elementsScrollPositions.js +27 -0
  19. package/cjs/domain/replay/record/index.js +41 -0
  20. package/cjs/domain/replay/record/mutationBatch.js +58 -0
  21. package/cjs/domain/replay/record/mutationObserver.js +383 -0
  22. package/cjs/domain/replay/record/observers.js +473 -0
  23. package/cjs/domain/replay/record/privacy.js +256 -0
  24. package/cjs/domain/replay/record/record.js +174 -0
  25. package/cjs/domain/replay/record/serializationUtils.js +137 -0
  26. package/cjs/domain/replay/record/serialize.js +488 -0
  27. package/cjs/domain/replay/record/shadowRootsController.js +61 -0
  28. package/cjs/domain/replay/record/utils.js +53 -0
  29. package/cjs/domain/replay/record/viewports.js +104 -0
  30. package/cjs/domain/replay/replayStats.js +83 -0
  31. package/cjs/domain/replay/segmentCollection/buildReplayPayload.js +43 -0
  32. package/cjs/domain/replay/segmentCollection/deflateWorker.js +4399 -0
  33. package/cjs/domain/replay/segmentCollection/index.js +33 -0
  34. package/cjs/domain/replay/segmentCollection/segment.js +101 -0
  35. package/cjs/domain/replay/segmentCollection/segmentCollection.js +179 -0
  36. package/cjs/domain/replay/segmentCollection/startDeflateWorker.js +126 -0
  37. package/cjs/domain/requestCollection.js +35 -17
  38. package/cjs/domain/rumEventsCollection/actions/actionCollection.js +12 -4
  39. package/cjs/domain/rumEventsCollection/actions/clickChain.js +3 -3
  40. package/cjs/domain/rumEventsCollection/actions/computeFrustration.js +6 -5
  41. package/cjs/domain/rumEventsCollection/actions/getActionNameFromElement.js +8 -6
  42. package/cjs/domain/rumEventsCollection/actions/getSelectorsFromElement.js +111 -75
  43. package/cjs/domain/rumEventsCollection/actions/listenActionEvents.js +22 -28
  44. package/cjs/domain/rumEventsCollection/actions/trackClickActions.js +94 -54
  45. package/cjs/domain/rumEventsCollection/error/errorCollection.js +13 -5
  46. package/cjs/domain/rumEventsCollection/view/trackViewEventCounts.js +45 -0
  47. package/cjs/domain/rumEventsCollection/view/trackViewMetrics.js +1 -19
  48. package/cjs/domain/rumEventsCollection/view/trackViews.js +21 -18
  49. package/cjs/domain/rumEventsCollection/view/viewCollection.js +12 -9
  50. package/cjs/domain/rumSessionManager.js +6 -3
  51. package/cjs/domain/tracing/getDocumentTraceId.js +2 -10
  52. package/cjs/domain/trackEventCounts.js +10 -2
  53. package/cjs/domain/waitPageActivityEnd.js +5 -1
  54. package/cjs/transport/startRumBatch.js +5 -7
  55. package/cjs/types/index.js +18 -0
  56. package/cjs/types/sessionReplayConstants.js +56 -0
  57. package/esm/boot/buildEnv.js +1 -1
  58. package/esm/boot/recorderApi.js +149 -0
  59. package/esm/boot/rum.entry.js +4 -1
  60. package/esm/boot/rum.js +15 -6
  61. package/esm/boot/rumPublicApi.js +8 -4
  62. package/esm/boot/startRecording.js +44 -0
  63. package/esm/constants.js +31 -0
  64. package/esm/domain/assembly.js +1 -7
  65. package/esm/domain/configuration.js +4 -3
  66. package/esm/domain/contexts/foregroundContexts.js +122 -0
  67. package/esm/domain/contexts/internalContext.js +1 -1
  68. package/esm/domain/contexts/urlContexts.js +10 -3
  69. package/esm/domain/domMutationCollection.js +5 -6
  70. package/esm/domain/initViewportObservable.js +39 -0
  71. package/esm/domain/locationChangeObservable.js +13 -27
  72. package/esm/domain/performanceCollection.js +1 -12
  73. package/esm/domain/replay/record/elementsScrollPositions.js +20 -0
  74. package/esm/domain/replay/record/index.js +3 -0
  75. package/esm/domain/replay/record/mutationBatch.js +35 -0
  76. package/esm/domain/replay/record/mutationObserver.js +331 -0
  77. package/esm/domain/replay/record/observers.js +457 -0
  78. package/esm/domain/replay/record/privacy.js +233 -0
  79. package/esm/domain/replay/record/record.js +158 -0
  80. package/esm/domain/replay/record/serializationUtils.js +107 -0
  81. package/esm/domain/replay/record/serialize.js +461 -0
  82. package/esm/domain/replay/record/shadowRootsController.js +49 -0
  83. package/esm/domain/replay/record/utils.js +38 -0
  84. package/esm/domain/replay/record/viewports.js +87 -0
  85. package/esm/domain/replay/replayStats.js +64 -0
  86. package/esm/domain/replay/segmentCollection/buildReplayPayload.js +31 -0
  87. package/esm/domain/replay/segmentCollection/deflateWorker.js +4390 -0
  88. package/esm/domain/replay/segmentCollection/index.js +3 -0
  89. package/esm/domain/replay/segmentCollection/segment.js +84 -0
  90. package/esm/domain/replay/segmentCollection/segmentCollection.js +158 -0
  91. package/esm/domain/replay/segmentCollection/startDeflateWorker.js +113 -0
  92. package/esm/domain/requestCollection.js +36 -18
  93. package/esm/domain/rumEventsCollection/actions/actionCollection.js +12 -4
  94. package/esm/domain/rumEventsCollection/actions/clickChain.js +3 -3
  95. package/esm/domain/rumEventsCollection/actions/computeFrustration.js +6 -5
  96. package/esm/domain/rumEventsCollection/actions/getActionNameFromElement.js +6 -6
  97. package/esm/domain/rumEventsCollection/actions/getSelectorsFromElement.js +105 -74
  98. package/esm/domain/rumEventsCollection/actions/listenActionEvents.js +22 -28
  99. package/esm/domain/rumEventsCollection/actions/trackClickActions.js +99 -59
  100. package/esm/domain/rumEventsCollection/error/errorCollection.js +13 -5
  101. package/esm/domain/rumEventsCollection/view/trackViewEventCounts.js +33 -0
  102. package/esm/domain/rumEventsCollection/view/trackViewMetrics.js +1 -18
  103. package/esm/domain/rumEventsCollection/view/trackViews.js +21 -19
  104. package/esm/domain/rumEventsCollection/view/viewCollection.js +12 -9
  105. package/esm/domain/rumSessionManager.js +6 -3
  106. package/esm/domain/tracing/getDocumentTraceId.js +1 -9
  107. package/esm/domain/trackEventCounts.js +10 -2
  108. package/esm/domain/waitPageActivityEnd.js +5 -1
  109. package/esm/transport/startRumBatch.js +5 -7
  110. package/esm/types/index.js +1 -0
  111. package/esm/types/sessionReplayConstants.js +45 -0
  112. package/package.json +3 -3
  113. package/src/boot/recorderApi.js +166 -0
  114. package/src/boot/rum.entry.js +4 -1
  115. package/src/boot/rum.js +38 -10
  116. package/src/boot/rumPublicApi.js +13 -3
  117. package/src/boot/startRecording.js +77 -0
  118. package/src/constants.js +37 -0
  119. package/src/domain/assembly.js +1 -6
  120. package/src/domain/configuration.js +10 -2
  121. package/src/domain/contexts/foregroundContexts.js +140 -0
  122. package/src/domain/contexts/internalContext.js +10 -4
  123. package/src/domain/contexts/urlContexts.js +29 -15
  124. package/src/domain/domMutationCollection.js +14 -6
  125. package/src/domain/initViewportObservable.js +46 -0
  126. package/src/domain/locationChangeObservable.js +17 -25
  127. package/src/domain/performanceCollection.js +42 -42
  128. package/src/domain/replay/record/elementsScrollPositions.js +22 -0
  129. package/src/domain/replay/record/index.js +7 -0
  130. package/src/domain/replay/record/mutationBatch.js +38 -0
  131. package/src/domain/replay/record/mutationObserver.js +393 -0
  132. package/src/domain/replay/record/observers.js +562 -0
  133. package/src/domain/replay/record/privacy.js +304 -0
  134. package/src/domain/replay/record/record.js +158 -0
  135. package/src/domain/replay/record/serializationUtils.js +134 -0
  136. package/src/domain/replay/record/serialize.js +497 -0
  137. package/src/domain/replay/record/shadowRootsController.js +58 -0
  138. package/src/domain/replay/record/utils.js +44 -0
  139. package/src/domain/replay/record/viewports.js +89 -0
  140. package/src/domain/replay/replayStats.js +67 -0
  141. package/src/domain/replay/segmentCollection/buildReplayPayload.js +34 -0
  142. package/src/domain/replay/segmentCollection/deflateWorker.js +4577 -0
  143. package/src/domain/replay/segmentCollection/index.js +6 -0
  144. package/src/domain/replay/segmentCollection/segment.js +87 -0
  145. package/src/domain/replay/segmentCollection/segmentCollection.js +205 -0
  146. package/src/domain/replay/segmentCollection/startDeflateWorker.js +101 -0
  147. package/src/domain/requestCollection.js +64 -28
  148. package/src/domain/rumEventsCollection/actions/actionCollection.js +11 -4
  149. package/src/domain/rumEventsCollection/actions/clickChain.js +3 -3
  150. package/src/domain/rumEventsCollection/actions/computeFrustration.js +7 -4
  151. package/src/domain/rumEventsCollection/actions/getActionNameFromElement.js +9 -6
  152. package/src/domain/rumEventsCollection/actions/getSelectorsFromElement.js +130 -108
  153. package/src/domain/rumEventsCollection/actions/listenActionEvents.js +26 -27
  154. package/src/domain/rumEventsCollection/actions/trackClickActions.js +162 -92
  155. package/src/domain/rumEventsCollection/error/errorCollection.js +11 -5
  156. package/src/domain/rumEventsCollection/view/trackViewEventCounts.js +38 -0
  157. package/src/domain/rumEventsCollection/view/trackViewMetrics.js +1 -19
  158. package/src/domain/rumEventsCollection/view/trackViews.js +98 -68
  159. package/src/domain/rumEventsCollection/view/viewCollection.js +15 -7
  160. package/src/domain/rumSessionManager.js +33 -19
  161. package/src/domain/tracing/getDocumentTraceId.js +6 -9
  162. package/src/domain/trackEventCounts.js +10 -4
  163. package/src/domain/waitPageActivityEnd.js +4 -1
  164. package/src/transport/startRumBatch.js +29 -24
  165. package/src/types/index.js +1 -0
  166. package/src/types/sessionReplayConstants.js +49 -0
@@ -1,6 +1,5 @@
1
1
  import {
2
2
  addEventListeners,
3
- addEventListener,
4
3
  getRelativeTime,
5
4
  isNumber,
6
5
  includes,
@@ -10,7 +9,8 @@ import {
10
9
  dateNow,
11
10
  relativeNow,
12
11
  DOM_EVENT,
13
- LifeCycleEventType
12
+ LifeCycleEventType,
13
+ runOnReadyState
14
14
  } from '@cloudcare/browser-core'
15
15
  import {
16
16
  FAKE_INITIAL_DOCUMENT,
@@ -36,29 +36,36 @@ export function startPerformanceCollection(lifeCycle, configuration) {
36
36
  var performanceEntries = performance.getEntries()
37
37
  // Because the performance entry list can be quite large
38
38
  // delay the computation to prevent the SDK from blocking the main thread on init
39
- setTimeout(function() {
39
+ setTimeout(function () {
40
40
  handleRumPerformanceEntries(lifeCycle, configuration, performanceEntries)
41
41
  })
42
42
  }
43
43
  if (window.PerformanceObserver) {
44
- var handlePerformanceEntryList = function(entries) {
45
- handleRumPerformanceEntries(lifeCycle, configuration, entries.getEntries())
44
+ var handlePerformanceEntryList = function (entries) {
45
+ handleRumPerformanceEntries(
46
+ lifeCycle,
47
+ configuration,
48
+ entries.getEntries()
49
+ )
46
50
  }
47
51
  var mainEntries = ['resource', 'navigation', 'longtask', 'paint']
48
- var experimentalEntries = ['largest-contentful-paint', 'first-input', 'layout-shift']
52
+ var experimentalEntries = [
53
+ 'largest-contentful-paint',
54
+ 'first-input',
55
+ 'layout-shift'
56
+ ]
49
57
  try {
50
58
  // Experimental entries are not retrieved by performance.getEntries()
51
59
  // use a single PerformanceObserver with buffered flag by type
52
60
  // to get values that could happen before SDK init
53
- each(experimentalEntries, function(type) {
61
+ each(experimentalEntries, function (type) {
54
62
  var observer = new PerformanceObserver(handlePerformanceEntryList)
55
- observer.observe({ type:type, buffered: true })
63
+ observer.observe({ type: type, buffered: true })
56
64
  })
57
-
58
65
  } catch (e) {
59
66
  // Some old browser versions (ex: chrome 67) don't support the PerformanceObserver type and buffered options
60
67
  // In these cases, fallback to PerformanceObserver with entryTypes
61
- each(experimentalEntries, function(type) {
68
+ each(experimentalEntries, function (type) {
62
69
  mainEntries.push(type)
63
70
  })
64
71
  }
@@ -77,7 +84,7 @@ export function startPerformanceCollection(lifeCycle, configuration) {
77
84
  })
78
85
  }
79
86
  if (!supportPerformanceTimingEvent('first-input')) {
80
- retrieveFirstInputTiming(function(timing) {
87
+ retrieveFirstInputTiming(function (timing) {
81
88
  return handleRumPerformanceEntries(lifeCycle, configuration, [timing])
82
89
  })
83
90
  }
@@ -206,21 +213,6 @@ function retrieveFirstInputTiming(callback) {
206
213
  }
207
214
  }
208
215
 
209
- function runOnReadyState(expectedReadyState, callback) {
210
- if (
211
- document.readyState === expectedReadyState ||
212
- document.readyState === 'complete'
213
- ) {
214
- callback()
215
- } else {
216
- var eventName =
217
- expectedReadyState === 'complete'
218
- ? DOM_EVENT.LOAD
219
- : DOM_EVENT.DOM_CONTENT_LOADED
220
- addEventListener(window, eventName, callback, { once: true })
221
- }
222
- }
223
-
224
216
  function computeRelativePerformanceTiming() {
225
217
  var result = {}
226
218
  var timing = performance.timing
@@ -233,27 +225,36 @@ function computeRelativePerformanceTiming() {
233
225
  }
234
226
 
235
227
  function handleRumPerformanceEntries(lifeCycle, configuration, entries) {
236
- var rumPerformanceEntries = filter(entries,
237
- function(entry){
238
- return entry.entryType === 'resource' ||
239
- entry.entryType === 'navigation' ||
240
- entry.entryType === 'paint' ||
241
- entry.entryType === 'longtask' ||
242
- entry.entryType === 'largest-contentful-paint' ||
243
- entry.entryType === 'first-input' ||
244
- entry.entryType === 'layout-shift'
245
- }
246
- )
228
+ var rumPerformanceEntries = filter(entries, function (entry) {
229
+ return (
230
+ entry.entryType === 'resource' ||
231
+ entry.entryType === 'navigation' ||
232
+ entry.entryType === 'paint' ||
233
+ entry.entryType === 'longtask' ||
234
+ entry.entryType === 'largest-contentful-paint' ||
235
+ entry.entryType === 'first-input' ||
236
+ entry.entryType === 'layout-shift'
237
+ )
238
+ })
247
239
 
248
- var rumAllowedPerformanceEntries = filter(rumPerformanceEntries, function(entry) { return !isIncompleteNavigation(entry) && !isForbiddenResource(configuration, entry)})
240
+ var rumAllowedPerformanceEntries = filter(
241
+ rumPerformanceEntries,
242
+ function (entry) {
243
+ return (
244
+ !isIncompleteNavigation(entry) &&
245
+ !isForbiddenResource(configuration, entry)
246
+ )
247
+ }
248
+ )
249
249
 
250
250
  if (rumAllowedPerformanceEntries.length) {
251
- lifeCycle.notify(LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED, rumAllowedPerformanceEntries)
251
+ lifeCycle.notify(
252
+ LifeCycleEventType.PERFORMANCE_ENTRIES_COLLECTED,
253
+ rumAllowedPerformanceEntries
254
+ )
252
255
  }
253
256
  }
254
257
 
255
-
256
-
257
258
  function isIncompleteNavigation(entry) {
258
259
  return entry.entryType === 'navigation' && entry.loadEventEnd <= 0
259
260
  }
@@ -268,4 +269,3 @@ export function supportPerformanceEntry() {
268
269
  // Safari 10 doesn't support PerformanceEntry
269
270
  return typeof PerformanceEntry === 'function'
270
271
  }
271
-
@@ -0,0 +1,22 @@
1
+ export function createElementsScrollPositions() {
2
+ var scrollPositionsByElement = new WeakMap()
3
+ return {
4
+ set: function (element, scrollPositions) {
5
+ if (element === document && !document.scrollingElement) {
6
+ // cf https://drafts.csswg.org/cssom-view/#dom-document-scrollingelement,
7
+ // in some cases scrolling elements can not be defined, we don't support those for now
8
+ return
9
+ }
10
+ scrollPositionsByElement.set(
11
+ element === document ? document.scrollingElement : element,
12
+ scrollPositions
13
+ )
14
+ },
15
+ get: function (element) {
16
+ return scrollPositionsByElement.get(element)
17
+ },
18
+ has: function (element) {
19
+ return scrollPositionsByElement.has(element)
20
+ }
21
+ }
22
+ }
@@ -0,0 +1,7 @@
1
+ export { record } from './record'
2
+ export {
3
+ serializeNodeWithId,
4
+ serializeDocument,
5
+ SerializationContextStatus
6
+ } from './serialize'
7
+ export { createElementsScrollPositions } from './elementsScrollPositions'
@@ -0,0 +1,38 @@
1
+ import { noop, requestIdleCallback } from '@cloudcare/browser-core'
2
+
3
+ /**
4
+ * Maximum duration to wait before processing mutations. If the browser is idle, mutations will be
5
+ * processed more quickly. If the browser is busy executing small tasks (ex: rendering frames), the
6
+ * mutations will wait MUTATION_PROCESS_MAX_DELAY milliseconds before being processed. If the
7
+ * browser is busy executing a longer task, mutations will be processed after this task.
8
+ */
9
+ var MUTATION_PROCESS_MAX_DELAY = 100
10
+
11
+ export function createMutationBatch(processMutationBatch) {
12
+ var cancelScheduledFlush = noop
13
+ var pendingMutations = []
14
+
15
+ function flush() {
16
+ cancelScheduledFlush()
17
+ processMutationBatch(pendingMutations)
18
+ pendingMutations = []
19
+ }
20
+
21
+ return {
22
+ addMutations: function (mutations) {
23
+ if (pendingMutations.length === 0) {
24
+ cancelScheduledFlush = requestIdleCallback(flush, {
25
+ timeout: MUTATION_PROCESS_MAX_DELAY
26
+ })
27
+ }
28
+ pendingMutations.push(...mutations)
29
+ // Array.prototype.push.apply(pendingMutations, mutations)
30
+ },
31
+
32
+ flush: flush,
33
+
34
+ stop: function () {
35
+ cancelScheduledFlush()
36
+ }
37
+ }
38
+ }
@@ -0,0 +1,393 @@
1
+ import {
2
+ noop,
3
+ getChildNodes,
4
+ isNodeShadowHost,
5
+ getParentNode
6
+ } from '@cloudcare/browser-core'
7
+ import { getMutationObserverConstructor } from '../../domMutationCollection'
8
+ import { NodePrivacyLevel } from '../../../constants'
9
+ import { getNodePrivacyLevel, getTextContent } from './privacy'
10
+ import {
11
+ getElementInputValue,
12
+ getSerializedNodeId,
13
+ hasSerializedNode,
14
+ nodeAndAncestorsHaveSerializedNode
15
+ } from './serializationUtils'
16
+ import {
17
+ serializeNodeWithId,
18
+ serializeAttribute,
19
+ SerializationContextStatus
20
+ } from './serialize'
21
+ import { createMutationBatch } from './mutationBatch'
22
+
23
+ /**
24
+ * Buffers and aggregate mutations generated by a MutationObserver into MutationPayload
25
+ */
26
+ export function startMutationObserver(
27
+ mutationCallback,
28
+ configuration,
29
+ shadowRootsController,
30
+ target
31
+ ) {
32
+ var MutationObserver = getMutationObserverConstructor()
33
+ if (!MutationObserver) {
34
+ return { stop: noop, flush: noop }
35
+ }
36
+
37
+ var mutationBatch = createMutationBatch(function (mutations) {
38
+ processMutations(
39
+ mutations.concat(observer.takeRecords()),
40
+ mutationCallback,
41
+ configuration,
42
+ shadowRootsController,
43
+ target
44
+ )
45
+ })
46
+
47
+ var observer = new MutationObserver(mutationBatch.addMutations)
48
+
49
+ observer.observe(target, {
50
+ attributeOldValue: true,
51
+ attributes: true,
52
+ characterData: true,
53
+ characterDataOldValue: true,
54
+ childList: true,
55
+ subtree: true
56
+ })
57
+
58
+ return {
59
+ stop: function () {
60
+ observer.disconnect()
61
+ mutationBatch.stop()
62
+ },
63
+ flush: function () {
64
+ mutationBatch.flush()
65
+ }
66
+ }
67
+ }
68
+
69
+ function processMutations(
70
+ mutations,
71
+ mutationCallback,
72
+ configuration,
73
+ shadowRootsController,
74
+ target
75
+ ) {
76
+ mutations
77
+ .filter(function (mutation) {
78
+ return mutation.type === 'childList'
79
+ })
80
+ .forEach(function (mutation) {
81
+ mutation.removedNodes.forEach(function (removedNode) {
82
+ traverseRemovedShadowDom(
83
+ removedNode,
84
+ shadowRootsController.removeShadowRoot
85
+ )
86
+ })
87
+ })
88
+
89
+ // Discard any mutation with a 'target' node that:
90
+ // * isn't injected in the current document or isn't known/serialized yet: those nodes are likely
91
+ // part of a mutation occurring in a parent Node
92
+ // * should be hidden or ignored
93
+ var filteredMutations = mutations.filter(function (mutation) {
94
+ return (
95
+ target.contains(mutation.target) &&
96
+ nodeAndAncestorsHaveSerializedNode(mutation.target) &&
97
+ getNodePrivacyLevel(
98
+ mutation.target,
99
+ configuration.defaultPrivacyLevel
100
+ ) !== NodePrivacyLevel.HIDDEN
101
+ )
102
+ })
103
+ var _processChildListMutations = processChildListMutations(
104
+ filteredMutations.filter(function (mutation) {
105
+ return mutation.type === 'childList'
106
+ }),
107
+ configuration,
108
+ shadowRootsController
109
+ )
110
+ var adds = _processChildListMutations.adds
111
+ var removes = _processChildListMutations.removes
112
+ var hasBeenSerialized = _processChildListMutations.hasBeenSerialized
113
+ var texts = processCharacterDataMutations(
114
+ filteredMutations.filter(function (mutation) {
115
+ return (
116
+ mutation.type === 'characterData' && !hasBeenSerialized(mutation.target)
117
+ )
118
+ }),
119
+ configuration
120
+ )
121
+
122
+ var attributes = processAttributesMutations(
123
+ filteredMutations.filter(function (mutation) {
124
+ mutation.type === 'attributes' && !hasBeenSerialized(mutation.target)
125
+ }),
126
+ configuration
127
+ )
128
+
129
+ if (!texts.length && !attributes.length && !removes.length && !adds.length) {
130
+ return
131
+ }
132
+
133
+ mutationCallback({
134
+ adds: adds,
135
+ removes: removes,
136
+ texts: texts,
137
+ attributes: attributes
138
+ })
139
+ }
140
+
141
+ function processChildListMutations(
142
+ mutations,
143
+ configuration,
144
+ shadowRootsController
145
+ ) {
146
+ // First, we iterate over mutations to collect:
147
+ //
148
+ // * nodes that have been added in the document and not removed by a subsequent mutation
149
+ // * nodes that have been removed from the document but were not added in a previous mutation
150
+ //
151
+ // For this second category, we also collect their previous parent (mutation.target) because we'll
152
+ // need it to emit a 'remove' mutation.
153
+ //
154
+ // Those two categories may overlap: if a node moved from a position to another, it is reported as
155
+ // two mutation records, one with a "removedNodes" and the other with "addedNodes". In this case,
156
+ // the node will be in both sets.
157
+ var addedAndMovedNodes = new Set()
158
+ var removedNodes = new Map()
159
+ for (var mutation of mutations) {
160
+ mutation.addedNodes.forEach(function (node) {
161
+ addedAndMovedNodes.add(node)
162
+ })
163
+ mutation.removedNodes.forEach(function (node) {
164
+ if (!addedAndMovedNodes.has(node)) {
165
+ removedNodes.set(node, mutation.target)
166
+ }
167
+ addedAndMovedNodes.delete(node)
168
+ })
169
+ }
170
+
171
+ // Then, we sort nodes that are still in the document by topological order, for two reasons:
172
+ //
173
+ // * We will serialize each added nodes with their descendants. We don't want to serialize a node
174
+ // twice, so we need to iterate over the parent nodes first and skip any node that is contained in
175
+ // a precedent node.
176
+ //
177
+ // * To emit "add" mutations, we need references to the parent and potential next sibling of each
178
+ // added node. So we need to iterate over the parent nodes first, and when multiple nodes are
179
+ // siblings, we want to iterate from last to first. This will ensure that any "next" node is
180
+ // already serialized and have an id.
181
+ var sortedAddedAndMovedNodes = Array.from(addedAndMovedNodes)
182
+ sortAddedAndMovedNodes(sortedAddedAndMovedNodes)
183
+
184
+ // Then, we iterate over our sorted node sets to emit mutations. We collect the newly serialized
185
+ // node ids in a set to be able to skip subsequent related mutations.
186
+ var serializedNodeIds = new Set()
187
+
188
+ var addedNodeMutations = []
189
+ for (var node of sortedAddedAndMovedNodes) {
190
+ if (hasBeenSerialized(node)) {
191
+ continue
192
+ }
193
+
194
+ var parentNodePrivacyLevel = getNodePrivacyLevel(
195
+ node.parentNode,
196
+ configuration.defaultPrivacyLevel
197
+ )
198
+ if (
199
+ parentNodePrivacyLevel === NodePrivacyLevel.HIDDEN ||
200
+ parentNodePrivacyLevel === NodePrivacyLevel.IGNORE
201
+ ) {
202
+ continue
203
+ }
204
+
205
+ var serializedNode = serializeNodeWithId(node, {
206
+ serializedNodeIds: serializedNodeIds,
207
+ parentNodePrivacyLevel: parentNodePrivacyLevel,
208
+ serializationContext: {
209
+ status: SerializationContextStatus.MUTATION,
210
+ shadowRootsController: shadowRootsController
211
+ },
212
+ configuration
213
+ })
214
+ if (!serializedNode) {
215
+ continue
216
+ }
217
+
218
+ var parentNode = getParentNode(node)
219
+ addedNodeMutations.push({
220
+ nextId: getNextSibling(node),
221
+ parentId: getSerializedNodeId(parentNode),
222
+ node: serializedNode
223
+ })
224
+ }
225
+ // Finally, we emit remove mutations.
226
+ var removedNodeMutations = []
227
+ removedNodes.forEach(function (parent, node) {
228
+ if (hasSerializedNode(node)) {
229
+ removedNodeMutations.push({
230
+ parentId: getSerializedNodeId(parent),
231
+ id: getSerializedNodeId(node)
232
+ })
233
+ }
234
+ })
235
+
236
+ return {
237
+ adds: addedNodeMutations,
238
+ removes: removedNodeMutations,
239
+ hasBeenSerialized: hasBeenSerialized
240
+ }
241
+
242
+ function hasBeenSerialized(node) {
243
+ return (
244
+ hasSerializedNode(node) &&
245
+ serializedNodeIds.has(getSerializedNodeId(node))
246
+ )
247
+ }
248
+
249
+ function getNextSibling(node) {
250
+ var nextSibling = node.nextSibling
251
+ while (nextSibling) {
252
+ if (hasSerializedNode(nextSibling)) {
253
+ return getSerializedNodeId(nextSibling)
254
+ }
255
+ nextSibling = nextSibling.nextSibling
256
+ }
257
+
258
+ return null
259
+ }
260
+ }
261
+
262
+ function processCharacterDataMutations(mutations, configuration) {
263
+ var textMutations = []
264
+
265
+ // Deduplicate mutations based on their target node
266
+ var handledNodes = new Set()
267
+ var filteredMutations = mutations.filter(function (mutation) {
268
+ if (handledNodes.has(mutation.target)) {
269
+ return false
270
+ }
271
+ handledNodes.add(mutation.target)
272
+ return true
273
+ })
274
+
275
+ // Emit mutations
276
+ for (var mutation of filteredMutations) {
277
+ var value = mutation.target.textContent
278
+ if (value === mutation.oldValue) {
279
+ continue
280
+ }
281
+
282
+ var parentNodePrivacyLevel = getNodePrivacyLevel(
283
+ getParentNode(mutation.target),
284
+ configuration.defaultPrivacyLevel
285
+ )
286
+ if (
287
+ parentNodePrivacyLevel === NodePrivacyLevel.HIDDEN ||
288
+ parentNodePrivacyLevel === NodePrivacyLevel.IGNORE
289
+ ) {
290
+ continue
291
+ }
292
+
293
+ textMutations.push({
294
+ id: getSerializedNodeId(mutation.target),
295
+ // TODO: pass a valid "ignoreWhiteSpace" argument
296
+ value:
297
+ getTextContent(mutation.target, false, parentNodePrivacyLevel) ?? null
298
+ })
299
+ }
300
+
301
+ return textMutations
302
+ }
303
+
304
+ function processAttributesMutations(mutations, configuration) {
305
+ var attributeMutations = []
306
+
307
+ // Deduplicate mutations based on their target node and changed attribute
308
+ var handledElements = new Map()
309
+ var filteredMutations = mutations.filter(function (mutation) {
310
+ var handledAttributes = handledElements.get(mutation.target)
311
+ if (handledAttributes?.has(mutation.attributeName)) {
312
+ return false
313
+ }
314
+ if (!handledAttributes) {
315
+ handledElements.set(mutation.target, new Set([mutation.attributeName]))
316
+ } else {
317
+ handledAttributes.add(mutation.attributeName)
318
+ }
319
+ return true
320
+ })
321
+
322
+ // Emit mutations
323
+ var emittedMutations = new Map()
324
+ for (var mutation of filteredMutations) {
325
+ var uncensoredValue = mutation.target.getAttribute(mutation.attributeName)
326
+ if (uncensoredValue === mutation.oldValue) {
327
+ continue
328
+ }
329
+ var privacyLevel = getNodePrivacyLevel(
330
+ mutation.target,
331
+ configuration.defaultPrivacyLevel
332
+ )
333
+ var attributeValue = serializeAttribute(
334
+ mutation.target,
335
+ privacyLevel,
336
+ mutation.attributeName,
337
+ configuration
338
+ )
339
+
340
+ var transformedValue
341
+ if (mutation.attributeName === 'value') {
342
+ var inputValue = getElementInputValue(mutation.target, privacyLevel)
343
+ if (inputValue === undefined) {
344
+ continue
345
+ }
346
+ transformedValue = inputValue
347
+ } else if (typeof attributeValue === 'string') {
348
+ transformedValue = attributeValue
349
+ } else {
350
+ transformedValue = null
351
+ }
352
+
353
+ var emittedMutation = emittedMutations.get(mutation.target)
354
+ if (!emittedMutation) {
355
+ emittedMutation = {
356
+ id: getSerializedNodeId(mutation.target),
357
+ attributes: {}
358
+ }
359
+ attributeMutations.push(emittedMutation)
360
+ emittedMutations.set(mutation.target, emittedMutation)
361
+ }
362
+
363
+ emittedMutation.attributes[mutation.attributeName] = transformedValue
364
+ }
365
+
366
+ return attributeMutations
367
+ }
368
+
369
+ export function sortAddedAndMovedNodes(nodes) {
370
+ nodes.sort(function (a, b) {
371
+ var position = a.compareDocumentPosition(b)
372
+ /* eslint-disable no-bitwise */
373
+ if (position & Node.DOCUMENT_POSITION_CONTAINED_BY) {
374
+ return -1
375
+ } else if (position & Node.DOCUMENT_POSITION_CONTAINS) {
376
+ return 1
377
+ } else if (position & Node.DOCUMENT_POSITION_FOLLOWING) {
378
+ return 1
379
+ } else if (position & Node.DOCUMENT_POSITION_PRECEDING) {
380
+ return -1
381
+ }
382
+ /* eslint-enable no-bitwise */
383
+ return 0
384
+ })
385
+ }
386
+ function traverseRemovedShadowDom(removedNode, shadowDomRemovedCallback) {
387
+ if (isNodeShadowHost(removedNode)) {
388
+ shadowDomRemovedCallback(removedNode.shadowRoot)
389
+ }
390
+ getChildNodes(removedNode).forEach(function (child) {
391
+ traverseRemovedShadowDom(child, shadowDomRemovedCallback)
392
+ })
393
+ }