@cloudcare/browser-rum 2.2.15 → 3.0.1

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 +388 -0
  22. package/cjs/domain/replay/record/observers.js +470 -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 +4400 -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 +127 -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 +336 -0
  77. package/esm/domain/replay/record/observers.js +454 -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 +4393 -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 +114 -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 +403 -0
  132. package/src/domain/replay/record/observers.js +565 -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 +4584 -0
  143. package/src/domain/replay/segmentCollection/index.js +6 -0
  144. package/src/domain/replay/segmentCollection/segment.js +86 -0
  145. package/src/domain/replay/segmentCollection/segmentCollection.js +205 -0
  146. package/src/domain/replay/segmentCollection/startDeflateWorker.js +102 -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,403 @@
1
+ import {
2
+ noop,
3
+ getChildNodes,
4
+ isNodeShadowHost,
5
+ getParentNode,
6
+ isNullUndefinedDefaultValue
7
+ } from '@cloudcare/browser-core'
8
+ import { getMutationObserverConstructor } from '../../domMutationCollection'
9
+ import { NodePrivacyLevel } from '../../../constants'
10
+ import { getNodePrivacyLevel, getTextContent } from './privacy'
11
+ import {
12
+ getElementInputValue,
13
+ getSerializedNodeId,
14
+ hasSerializedNode,
15
+ nodeAndAncestorsHaveSerializedNode
16
+ } from './serializationUtils'
17
+ import {
18
+ serializeNodeWithId,
19
+ serializeAttribute,
20
+ SerializationContextStatus
21
+ } from './serialize'
22
+ import { createMutationBatch } from './mutationBatch'
23
+
24
+ /**
25
+ * Buffers and aggregate mutations generated by a MutationObserver into MutationPayload
26
+ */
27
+ export function startMutationObserver(
28
+ mutationCallback,
29
+ configuration,
30
+ shadowRootsController,
31
+ target
32
+ ) {
33
+ var MutationObserver = getMutationObserverConstructor()
34
+ if (!MutationObserver) {
35
+ return { stop: noop, flush: noop }
36
+ }
37
+
38
+ var mutationBatch = createMutationBatch(function (mutations) {
39
+ processMutations(
40
+ mutations.concat(observer.takeRecords()),
41
+ mutationCallback,
42
+ configuration,
43
+ shadowRootsController,
44
+ target
45
+ )
46
+ })
47
+
48
+ var observer = new MutationObserver(mutationBatch.addMutations)
49
+
50
+ observer.observe(target, {
51
+ attributeOldValue: true,
52
+ attributes: true,
53
+ characterData: true,
54
+ characterDataOldValue: true,
55
+ childList: true,
56
+ subtree: true
57
+ })
58
+
59
+ return {
60
+ stop: function () {
61
+ observer.disconnect()
62
+ mutationBatch.stop()
63
+ },
64
+ flush: function () {
65
+ mutationBatch.flush()
66
+ }
67
+ }
68
+ }
69
+
70
+ function processMutations(
71
+ mutations,
72
+ mutationCallback,
73
+ configuration,
74
+ shadowRootsController,
75
+ target
76
+ ) {
77
+ mutations
78
+ .filter(function (mutation) {
79
+ return mutation.type === 'childList'
80
+ })
81
+ .forEach(function (mutation) {
82
+ mutation.removedNodes.forEach(function (removedNode) {
83
+ traverseRemovedShadowDom(
84
+ removedNode,
85
+ shadowRootsController.removeShadowRoot
86
+ )
87
+ })
88
+ })
89
+
90
+ // Discard any mutation with a 'target' node that:
91
+ // * isn't injected in the current document or isn't known/serialized yet: those nodes are likely
92
+ // part of a mutation occurring in a parent Node
93
+ // * should be hidden or ignored
94
+ var filteredMutations = mutations.filter(function (mutation) {
95
+ return (
96
+ target.contains(mutation.target) &&
97
+ nodeAndAncestorsHaveSerializedNode(mutation.target) &&
98
+ getNodePrivacyLevel(
99
+ mutation.target,
100
+ configuration.defaultPrivacyLevel
101
+ ) !== NodePrivacyLevel.HIDDEN
102
+ )
103
+ })
104
+ var _processChildListMutations = processChildListMutations(
105
+ filteredMutations.filter(function (mutation) {
106
+ return mutation.type === 'childList'
107
+ }),
108
+ configuration,
109
+ shadowRootsController
110
+ )
111
+ var adds = _processChildListMutations.adds
112
+ var removes = _processChildListMutations.removes
113
+ // var hasBeenSerialized = _processChildListMutations.hasBeenSerialized
114
+ var serializedNodeIds = _processChildListMutations.serializedNodeIds
115
+ function hasBeenSerialized(node) {
116
+ return (
117
+ hasSerializedNode(node) &&
118
+ serializedNodeIds.has(getSerializedNodeId(node))
119
+ )
120
+ }
121
+ var texts = processCharacterDataMutations(
122
+ filteredMutations.filter(function (mutation) {
123
+ return (
124
+ mutation.type === 'characterData' && !hasBeenSerialized(mutation.target)
125
+ )
126
+ }),
127
+ configuration
128
+ )
129
+
130
+ var attributes = processAttributesMutations(
131
+ filteredMutations.filter(function (mutation) {
132
+ return (
133
+ mutation.type === 'attributes' && !hasBeenSerialized(mutation.target)
134
+ )
135
+ }),
136
+ configuration
137
+ )
138
+ if (!texts.length && !attributes.length && !removes.length && !adds.length) {
139
+ return
140
+ }
141
+ mutationCallback({
142
+ adds: adds,
143
+ removes: removes,
144
+ texts: texts,
145
+ attributes: attributes
146
+ })
147
+ }
148
+
149
+ function processChildListMutations(
150
+ mutations,
151
+ configuration,
152
+ shadowRootsController
153
+ ) {
154
+ // First, we iterate over mutations to collect:
155
+ //
156
+ // * nodes that have been added in the document and not removed by a subsequent mutation
157
+ // * nodes that have been removed from the document but were not added in a previous mutation
158
+ //
159
+ // For this second category, we also collect their previous parent (mutation.target) because we'll
160
+ // need it to emit a 'remove' mutation.
161
+ //
162
+ // Those two categories may overlap: if a node moved from a position to another, it is reported as
163
+ // two mutation records, one with a "removedNodes" and the other with "addedNodes". In this case,
164
+ // the node will be in both sets.
165
+ var addedAndMovedNodes = new Set()
166
+ var removedNodes = new Map()
167
+ for (var mutation of mutations) {
168
+ mutation.addedNodes.forEach(function (node) {
169
+ addedAndMovedNodes.add(node)
170
+ })
171
+ mutation.removedNodes.forEach(function (node) {
172
+ if (!addedAndMovedNodes.has(node)) {
173
+ removedNodes.set(node, mutation.target)
174
+ }
175
+ addedAndMovedNodes.delete(node)
176
+ })
177
+ }
178
+
179
+ // Then, we sort nodes that are still in the document by topological order, for two reasons:
180
+ //
181
+ // * We will serialize each added nodes with their descendants. We don't want to serialize a node
182
+ // twice, so we need to iterate over the parent nodes first and skip any node that is contained in
183
+ // a precedent node.
184
+ //
185
+ // * To emit "add" mutations, we need references to the parent and potential next sibling of each
186
+ // added node. So we need to iterate over the parent nodes first, and when multiple nodes are
187
+ // siblings, we want to iterate from last to first. This will ensure that any "next" node is
188
+ // already serialized and have an id.
189
+ var sortedAddedAndMovedNodes = Array.from(addedAndMovedNodes)
190
+ sortAddedAndMovedNodes(sortedAddedAndMovedNodes)
191
+
192
+ // Then, we iterate over our sorted node sets to emit mutations. We collect the newly serialized
193
+ // node ids in a set to be able to skip subsequent related mutations.
194
+ var serializedNodeIds = new Set()
195
+
196
+ var addedNodeMutations = []
197
+ for (var node of sortedAddedAndMovedNodes) {
198
+ if (hasBeenSerialized(node)) {
199
+ continue
200
+ }
201
+
202
+ var parentNodePrivacyLevel = getNodePrivacyLevel(
203
+ node.parentNode,
204
+ configuration.defaultPrivacyLevel
205
+ )
206
+ if (
207
+ parentNodePrivacyLevel === NodePrivacyLevel.HIDDEN ||
208
+ parentNodePrivacyLevel === NodePrivacyLevel.IGNORE
209
+ ) {
210
+ continue
211
+ }
212
+
213
+ var serializedNode = serializeNodeWithId(node, {
214
+ serializedNodeIds: serializedNodeIds,
215
+ parentNodePrivacyLevel: parentNodePrivacyLevel,
216
+ serializationContext: {
217
+ status: SerializationContextStatus.MUTATION,
218
+ shadowRootsController: shadowRootsController
219
+ },
220
+ configuration
221
+ })
222
+ if (!serializedNode) {
223
+ continue
224
+ }
225
+
226
+ var parentNode = getParentNode(node)
227
+ addedNodeMutations.push({
228
+ nextId: getNextSibling(node),
229
+ parentId: getSerializedNodeId(parentNode),
230
+ node: serializedNode
231
+ })
232
+ }
233
+ // Finally, we emit remove mutations.
234
+ var removedNodeMutations = []
235
+ removedNodes.forEach(function (parent, node) {
236
+ if (hasSerializedNode(node)) {
237
+ removedNodeMutations.push({
238
+ parentId: getSerializedNodeId(parent),
239
+ id: getSerializedNodeId(node)
240
+ })
241
+ }
242
+ })
243
+
244
+ return {
245
+ adds: addedNodeMutations,
246
+ removes: removedNodeMutations,
247
+ serializedNodeIds: serializedNodeIds,
248
+ hasBeenSerialized: hasBeenSerialized
249
+ }
250
+
251
+ function hasBeenSerialized(node) {
252
+ return (
253
+ hasSerializedNode(node) &&
254
+ serializedNodeIds.has(getSerializedNodeId(node))
255
+ )
256
+ }
257
+
258
+ function getNextSibling(node) {
259
+ var nextSibling = node.nextSibling
260
+ while (nextSibling) {
261
+ if (hasSerializedNode(nextSibling)) {
262
+ return getSerializedNodeId(nextSibling)
263
+ }
264
+ nextSibling = nextSibling.nextSibling
265
+ }
266
+
267
+ return null
268
+ }
269
+ }
270
+
271
+ function processCharacterDataMutations(mutations, configuration) {
272
+ var textMutations = []
273
+
274
+ // Deduplicate mutations based on their target node
275
+ var handledNodes = new Set()
276
+ var filteredMutations = mutations.filter(function (mutation) {
277
+ if (handledNodes.has(mutation.target)) {
278
+ return false
279
+ }
280
+ handledNodes.add(mutation.target)
281
+ return true
282
+ })
283
+
284
+ // Emit mutations
285
+ for (var mutation of filteredMutations) {
286
+ var value = mutation.target.textContent
287
+ if (value === mutation.oldValue) {
288
+ continue
289
+ }
290
+
291
+ var parentNodePrivacyLevel = getNodePrivacyLevel(
292
+ getParentNode(mutation.target),
293
+ configuration.defaultPrivacyLevel
294
+ )
295
+ if (
296
+ parentNodePrivacyLevel === NodePrivacyLevel.HIDDEN ||
297
+ parentNodePrivacyLevel === NodePrivacyLevel.IGNORE
298
+ ) {
299
+ continue
300
+ }
301
+
302
+ textMutations.push({
303
+ id: getSerializedNodeId(mutation.target),
304
+ // TODO: pass a valid "ignoreWhiteSpace" argument
305
+ value: isNullUndefinedDefaultValue(
306
+ getTextContent(mutation.target, false, parentNodePrivacyLevel, null)
307
+ )
308
+ })
309
+ }
310
+
311
+ return textMutations
312
+ }
313
+
314
+ function processAttributesMutations(mutations, configuration) {
315
+ var attributeMutations = []
316
+
317
+ // Deduplicate mutations based on their target node and changed attribute
318
+ var handledElements = new Map()
319
+ var filteredMutations = mutations.filter(function (mutation) {
320
+ var handledAttributes = handledElements.get(mutation.target)
321
+ if (handledAttributes && handledAttributes.has(mutation.attributeName)) {
322
+ return false
323
+ }
324
+ if (!handledAttributes) {
325
+ handledElements.set(mutation.target, new Set([mutation.attributeName]))
326
+ } else {
327
+ handledAttributes.add(mutation.attributeName)
328
+ }
329
+ return true
330
+ })
331
+
332
+ // Emit mutations
333
+ var emittedMutations = new Map()
334
+ for (var mutation of filteredMutations) {
335
+ var uncensoredValue = mutation.target.getAttribute(mutation.attributeName)
336
+ if (uncensoredValue === mutation.oldValue) {
337
+ continue
338
+ }
339
+ var privacyLevel = getNodePrivacyLevel(
340
+ mutation.target,
341
+ configuration.defaultPrivacyLevel
342
+ )
343
+ var attributeValue = serializeAttribute(
344
+ mutation.target,
345
+ privacyLevel,
346
+ mutation.attributeName,
347
+ configuration
348
+ )
349
+
350
+ var transformedValue
351
+ if (mutation.attributeName === 'value') {
352
+ var inputValue = getElementInputValue(mutation.target, privacyLevel)
353
+ if (inputValue === undefined) {
354
+ continue
355
+ }
356
+ transformedValue = inputValue
357
+ } else if (typeof attributeValue === 'string') {
358
+ transformedValue = attributeValue
359
+ } else {
360
+ transformedValue = null
361
+ }
362
+
363
+ var emittedMutation = emittedMutations.get(mutation.target)
364
+ if (!emittedMutation) {
365
+ emittedMutation = {
366
+ id: getSerializedNodeId(mutation.target),
367
+ attributes: {}
368
+ }
369
+ attributeMutations.push(emittedMutation)
370
+ emittedMutations.set(mutation.target, emittedMutation)
371
+ }
372
+
373
+ emittedMutation.attributes[mutation.attributeName] = transformedValue
374
+ }
375
+
376
+ return attributeMutations
377
+ }
378
+
379
+ export function sortAddedAndMovedNodes(nodes) {
380
+ nodes.sort(function (a, b) {
381
+ var position = a.compareDocumentPosition(b)
382
+ /* eslint-disable no-bitwise */
383
+ if (position & Node.DOCUMENT_POSITION_CONTAINED_BY) {
384
+ return -1
385
+ } else if (position & Node.DOCUMENT_POSITION_CONTAINS) {
386
+ return 1
387
+ } else if (position & Node.DOCUMENT_POSITION_FOLLOWING) {
388
+ return 1
389
+ } else if (position & Node.DOCUMENT_POSITION_PRECEDING) {
390
+ return -1
391
+ }
392
+ /* eslint-enable no-bitwise */
393
+ return 0
394
+ })
395
+ }
396
+ function traverseRemovedShadowDom(removedNode, shadowDomRemovedCallback) {
397
+ if (isNodeShadowHost(removedNode)) {
398
+ shadowDomRemovedCallback(removedNode.shadowRoot)
399
+ }
400
+ getChildNodes(removedNode).forEach(function (child) {
401
+ traverseRemovedShadowDom(child, shadowDomRemovedCallback)
402
+ })
403
+ }