@cloudcare/browser-rum 2.1.6 → 2.2.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 (134) hide show
  1. package/README.md +1 -2
  2. package/bundle/dataflux-rum.js +1 -1
  3. package/cjs/boot/buildEnv.js +1 -1
  4. package/cjs/boot/rum.js +57 -48
  5. package/cjs/boot/rumPublicApi.js +169 -134
  6. package/cjs/domain/assembly.js +58 -44
  7. package/cjs/domain/configuration.js +75 -0
  8. package/cjs/domain/contexts/internalContext.js +45 -0
  9. package/cjs/domain/contexts/urlContexts.js +68 -0
  10. package/cjs/domain/contexts/viewContexts.js +43 -0
  11. package/cjs/domain/domMutationCollection.js +44 -31
  12. package/cjs/domain/{rumEventsCollection/view/trackLocationChanges.js → locationChangeObservable.js} +25 -25
  13. package/cjs/domain/performanceCollection.js +55 -24
  14. package/cjs/domain/requestCollection.js +117 -0
  15. package/cjs/domain/rumEventsCollection/actions/actionCollection.js +22 -4
  16. package/cjs/domain/rumEventsCollection/actions/clickChain.js +82 -0
  17. package/cjs/domain/rumEventsCollection/actions/computeFrustration.js +78 -0
  18. package/cjs/domain/rumEventsCollection/actions/getActionNameFromElement.js +60 -47
  19. package/cjs/domain/rumEventsCollection/actions/getSelectorsFromElement.js +163 -0
  20. package/cjs/domain/rumEventsCollection/actions/listenActionEvents.js +74 -0
  21. package/cjs/domain/rumEventsCollection/actions/trackClickActions.js +263 -0
  22. package/cjs/domain/rumEventsCollection/error/errorCollection.js +33 -53
  23. package/cjs/domain/rumEventsCollection/error/trackConsoleError.js +26 -0
  24. package/cjs/domain/rumEventsCollection/error/trackReportError.js +26 -0
  25. package/cjs/domain/rumEventsCollection/longTask/longTaskCollection.js +46 -17
  26. package/cjs/domain/rumEventsCollection/resource/resourceCollection.js +49 -9
  27. package/cjs/domain/rumEventsCollection/view/trackFirstHidden.js +7 -5
  28. package/cjs/domain/rumEventsCollection/view/trackInitialViewTimings.js +61 -24
  29. package/cjs/domain/rumEventsCollection/view/trackViewMetrics.js +91 -51
  30. package/cjs/domain/rumEventsCollection/view/trackViews.js +119 -86
  31. package/cjs/domain/rumEventsCollection/view/viewCollection.js +41 -31
  32. package/cjs/domain/rumSessionManager.js +95 -0
  33. package/cjs/domain/tracing/tracer.js +8 -9
  34. package/cjs/domain/trackEventCounts.js +9 -6
  35. package/cjs/domain/usr/index.js +30 -0
  36. package/cjs/domain/waitPageActivityEnd.js +163 -0
  37. package/cjs/transport/startRumBatch.js +38 -0
  38. package/esm/boot/buildEnv.js +1 -1
  39. package/esm/boot/rum.js +55 -48
  40. package/esm/boot/rumPublicApi.js +167 -135
  41. package/esm/domain/assembly.js +58 -44
  42. package/esm/domain/configuration.js +60 -0
  43. package/esm/domain/contexts/internalContext.js +38 -0
  44. package/esm/domain/contexts/urlContexts.js +58 -0
  45. package/esm/domain/contexts/viewContexts.js +32 -0
  46. package/esm/domain/domMutationCollection.js +43 -33
  47. package/esm/domain/{rumEventsCollection/view/trackLocationChanges.js → locationChangeObservable.js} +26 -24
  48. package/esm/domain/performanceCollection.js +56 -26
  49. package/esm/domain/requestCollection.js +102 -0
  50. package/esm/domain/rumEventsCollection/actions/actionCollection.js +23 -5
  51. package/esm/domain/rumEventsCollection/actions/clickChain.js +69 -0
  52. package/esm/domain/rumEventsCollection/actions/computeFrustration.js +64 -0
  53. package/esm/domain/rumEventsCollection/actions/getActionNameFromElement.js +60 -47
  54. package/esm/domain/rumEventsCollection/actions/getSelectorsFromElement.js +139 -0
  55. package/esm/domain/rumEventsCollection/actions/listenActionEvents.js +66 -0
  56. package/esm/domain/rumEventsCollection/actions/trackClickActions.js +244 -0
  57. package/esm/domain/rumEventsCollection/error/errorCollection.js +32 -54
  58. package/esm/domain/rumEventsCollection/error/trackConsoleError.js +18 -0
  59. package/esm/domain/rumEventsCollection/error/trackReportError.js +18 -0
  60. package/esm/domain/rumEventsCollection/longTask/longTaskCollection.js +30 -18
  61. package/esm/domain/rumEventsCollection/resource/resourceCollection.js +31 -9
  62. package/esm/domain/rumEventsCollection/view/trackFirstHidden.js +8 -6
  63. package/esm/domain/rumEventsCollection/view/trackInitialViewTimings.js +43 -26
  64. package/esm/domain/rumEventsCollection/view/trackViewMetrics.js +75 -51
  65. package/esm/domain/rumEventsCollection/view/trackViews.js +120 -85
  66. package/esm/domain/rumEventsCollection/view/viewCollection.js +42 -32
  67. package/esm/domain/rumSessionManager.js +80 -0
  68. package/esm/domain/tracing/skywalkingTracer.js +1 -1
  69. package/esm/domain/tracing/tracer.js +7 -8
  70. package/esm/domain/trackEventCounts.js +9 -6
  71. package/esm/domain/usr/index.js +17 -0
  72. package/esm/domain/waitPageActivityEnd.js +147 -0
  73. package/esm/transport/startRumBatch.js +30 -0
  74. package/package.json +3 -3
  75. package/src/boot/rum.js +79 -59
  76. package/src/boot/rumPublicApi.js +165 -171
  77. package/src/domain/assembly.js +77 -54
  78. package/src/domain/configuration.js +92 -0
  79. package/src/domain/contexts/internalContext.js +39 -0
  80. package/src/domain/contexts/urlContexts.js +76 -0
  81. package/src/domain/contexts/viewContexts.js +37 -0
  82. package/src/domain/domMutationCollection.js +48 -32
  83. package/src/domain/{rumEventsCollection/view/trackLocationChanges.js → locationChangeObservable.js} +28 -28
  84. package/src/domain/performanceCollection.js +62 -45
  85. package/src/domain/requestCollection.js +103 -0
  86. package/src/domain/rumEventsCollection/actions/actionCollection.js +34 -11
  87. package/src/domain/rumEventsCollection/actions/clickChain.js +90 -0
  88. package/src/domain/rumEventsCollection/actions/computeFrustration.js +84 -0
  89. package/src/domain/rumEventsCollection/actions/getActionNameFromElement.js +73 -62
  90. package/src/domain/rumEventsCollection/actions/getSelectorsFromElement.js +157 -0
  91. package/src/domain/rumEventsCollection/actions/listenActionEvents.js +85 -0
  92. package/src/domain/rumEventsCollection/actions/trackClickActions.js +326 -0
  93. package/src/domain/rumEventsCollection/error/errorCollection.js +53 -69
  94. package/src/domain/rumEventsCollection/error/trackConsoleError.js +21 -0
  95. package/src/domain/rumEventsCollection/error/trackReportError.js +22 -0
  96. package/src/domain/rumEventsCollection/longTask/longTaskCollection.js +29 -18
  97. package/src/domain/rumEventsCollection/resource/resourceCollection.js +39 -23
  98. package/src/domain/rumEventsCollection/view/trackFirstHidden.js +9 -6
  99. package/src/domain/rumEventsCollection/view/trackInitialViewTimings.js +63 -42
  100. package/src/domain/rumEventsCollection/view/trackViewMetrics.js +80 -61
  101. package/src/domain/rumEventsCollection/view/trackViews.js +171 -134
  102. package/src/domain/rumEventsCollection/view/viewCollection.js +69 -49
  103. package/src/domain/rumSessionManager.js +90 -0
  104. package/src/domain/tracing/jaegerTracer.js +5 -5
  105. package/src/domain/tracing/skywalkingTracer.js +3 -3
  106. package/src/domain/tracing/tracer.js +50 -38
  107. package/src/domain/tracing/w3cTraceParentTracer.js +5 -5
  108. package/src/domain/tracing/zipkinMultiTracer.js +5 -5
  109. package/src/domain/tracing/zipkinSingleTracer.js +5 -5
  110. package/src/domain/trackEventCounts.js +10 -6
  111. package/src/domain/usr/index.js +16 -0
  112. package/src/domain/waitPageActivityEnd.js +192 -0
  113. package/src/transport/startRumBatch.js +48 -0
  114. package/cjs/domain/internalContext.js +0 -34
  115. package/cjs/domain/parentContexts.js +0 -143
  116. package/cjs/domain/requestCollections.js +0 -131
  117. package/cjs/domain/rumEventsCollection/actions/trackActions.js +0 -114
  118. package/cjs/domain/rumSession.js +0 -82
  119. package/cjs/domain/trackPageActivities.js +0 -166
  120. package/cjs/transport/batch.js +0 -81
  121. package/esm/domain/internalContext.js +0 -27
  122. package/esm/domain/parentContexts.js +0 -130
  123. package/esm/domain/requestCollections.js +0 -118
  124. package/esm/domain/rumEventsCollection/actions/trackActions.js +0 -103
  125. package/esm/domain/rumSession.js +0 -70
  126. package/esm/domain/trackPageActivities.js +0 -149
  127. package/esm/transport/batch.js +0 -74
  128. package/src/domain/internalContext.js +0 -28
  129. package/src/domain/parentContexts.js +0 -171
  130. package/src/domain/requestCollections.js +0 -118
  131. package/src/domain/rumEventsCollection/actions/trackActions.js +0 -113
  132. package/src/domain/rumSession.js +0 -92
  133. package/src/domain/trackPageActivities.js +0 -170
  134. package/src/transport/batch.js +0 -85
@@ -1,5 +1,9 @@
1
- import { map, filter, safeTruncate } from '@cloudcare/browser-core'
2
- export function getActionNameFromElement(element) {
1
+ import { safeTruncate, isIE, find, map, filter } from '@cloudcare/browser-core'
2
+
3
+
4
+ var DEFAULT_PROGRAMMATIC_ATTRIBUTE = 'data-guance-action-name'
5
+
6
+ export function getActionNameFromElement(element, userProgrammaticAttribute) {
3
7
  // Proceed to get the action name in two steps:
4
8
  // * first, get the name programmatically, explicitly defined by the user.
5
9
  // * then, use strategies that are known to return good results. Those strategies will be used on
@@ -7,31 +11,26 @@ export function getActionNameFromElement(element) {
7
11
  // * if no name is found this way, use strategies returning less accurate names as a fallback.
8
12
  // Those are much likely to succeed.
9
13
  return (
10
- getActionNameFromElementProgrammatically(element) ||
11
- getActionNameFromElementForStrategies(element, priorityStrategies) ||
12
- getActionNameFromElementForStrategies(element, fallbackStrategies) ||
14
+ getActionNameFromElementProgrammatically(element, DEFAULT_PROGRAMMATIC_ATTRIBUTE) ||
15
+ (userProgrammaticAttribute && getActionNameFromElementProgrammatically(element, userProgrammaticAttribute)) ||
16
+ getActionNameFromElementForStrategies(element, userProgrammaticAttribute, priorityStrategies) ||
17
+ getActionNameFromElementForStrategies(element, userProgrammaticAttribute, fallbackStrategies) ||
13
18
  ''
14
19
  )
15
20
  }
16
21
 
17
- /**
18
- * Get the action name from the attribute 'data-dd-action-name' on the element or any of its parent.
19
- */
20
- var PROGRAMMATIC_ATTRIBUTE = 'data-df-action-name'
21
- function getActionNameFromElementProgrammatically(targetElement) {
22
+ function getActionNameFromElementProgrammatically(targetElement, programmaticAttribute) {
22
23
  var elementWithAttribute
23
24
  // We don't use getActionNameFromElementForStrategies here, because we want to consider all parents,
24
25
  // without limit. It is up to the user to declare a relevant naming strategy.
25
26
  // If available, use element.closest() to match get the attribute from the element or any of its
26
27
  // parent. Else fallback to a more traditional implementation.
27
28
  if (supportsElementClosest()) {
28
- elementWithAttribute = targetElement.closest(
29
- '[' + PROGRAMMATIC_ATTRIBUTE + ']'
30
- )
29
+ elementWithAttribute = targetElement.closest('[' + programmaticAttribute + ']')
31
30
  } else {
32
31
  var element = targetElement
33
32
  while (element) {
34
- if (element.hasAttribute(PROGRAMMATIC_ATTRIBUTE)) {
33
+ if (element.hasAttribute(programmaticAttribute)) {
35
34
  elementWithAttribute = element
36
35
  break
37
36
  }
@@ -42,30 +41,28 @@ function getActionNameFromElementProgrammatically(targetElement) {
42
41
  if (!elementWithAttribute) {
43
42
  return
44
43
  }
45
- var name = elementWithAttribute.getAttribute(PROGRAMMATIC_ATTRIBUTE)
44
+ var name = elementWithAttribute.getAttribute(programmaticAttribute)
46
45
  return truncate(normalizeWhitespace(name.trim()))
47
46
  }
48
47
 
49
48
  var priorityStrategies = [
50
49
  // associated LABEL text
51
- function (element) {
50
+ function(element, userProgrammaticAttribute){
52
51
  // IE does not support element.labels, so we fallback to a CSS selector based on the element id
53
52
  // instead
54
53
  if (supportsLabelProperty()) {
55
54
  if ('labels' in element && element.labels && element.labels.length > 0) {
56
- return getTextualContent(element.labels[0])
55
+ return getTextualContent(element.labels[0], userProgrammaticAttribute)
57
56
  }
58
57
  } else if (element.id) {
59
58
  var label =
60
59
  element.ownerDocument &&
61
- element.ownerDocument.querySelector(
62
- 'label[for="' + element.id.replace('"', '\\"') + '"]'
63
- )
64
- return label && getTextualContent(label)
60
+ find(element.ownerDocument.querySelectorAll('label'), function(label) { return label.htmlFor === element.id})
61
+ return label && getTextualContent(label, userProgrammaticAttribute)
65
62
  }
66
63
  },
67
64
  // INPUT button (and associated) value
68
- function (element) {
65
+ function(element) {
69
66
  if (element.nodeName === 'INPUT') {
70
67
  var input = element
71
68
  var type = input.getAttribute('type')
@@ -75,20 +72,14 @@ var priorityStrategies = [
75
72
  }
76
73
  },
77
74
  // BUTTON, LABEL or button-like element text
78
- function (element) {
79
- if (
80
- element.nodeName === 'BUTTON' ||
81
- element.nodeName === 'LABEL' ||
82
- element.getAttribute('role') === 'button'
83
- ) {
84
- return getTextualContent(element)
75
+ function(element, userProgrammaticAttribute) {
76
+ if (element.nodeName === 'BUTTON' || element.nodeName === 'LABEL' || element.getAttribute('role') === 'button') {
77
+ return getTextualContent(element, userProgrammaticAttribute)
85
78
  }
86
79
  },
87
- function (element) {
88
- return element.getAttribute('aria-label')
89
- },
80
+ function (element){ return element.getAttribute('aria-label')},
90
81
  // associated element text designated by the aria-labelledby attribute
91
- function (element) {
82
+ function (element, userProgrammaticAttribute){
92
83
  var labelledByAttribute = element.getAttribute('aria-labelledby')
93
84
  if (labelledByAttribute) {
94
85
  labelledByAttribute = labelledByAttribute.split(/\s+/)
@@ -121,13 +112,11 @@ var priorityStrategies = [
121
112
  if ('options' in element && element.options.length > 0) {
122
113
  return getTextualContent(element.options[0])
123
114
  }
124
- }
115
+ },
125
116
  ]
126
117
 
127
118
  var fallbackStrategies = [
128
- function (element) {
129
- return getTextualContent(element)
130
- }
119
+ function(element, userProgrammaticAttribute){ return getTextualContent(element, userProgrammaticAttribute)},
131
120
  ]
132
121
 
133
122
  /**
@@ -135,7 +124,11 @@ var fallbackStrategies = [
135
124
  * Each strategies are applied on each element, stopping as soon as a non-empty value is returned.
136
125
  */
137
126
  var MAX_PARENTS_TO_CONSIDER = 10
138
- function getActionNameFromElementForStrategies(targetElement, strategies) {
127
+ function getActionNameFromElementForStrategies(
128
+ targetElement,
129
+ userProgrammaticAttribute,
130
+ strategies
131
+ ) {
139
132
  var element = targetElement
140
133
  var recursionCounter = 0
141
134
  while (
@@ -146,7 +139,7 @@ function getActionNameFromElementForStrategies(targetElement, strategies) {
146
139
  element.nodeName !== 'HEAD'
147
140
  ) {
148
141
  for (var strategy of strategies) {
149
- var name = strategy(element)
142
+ var name = strategy(element, userProgrammaticAttribute)
150
143
  if (typeof name === 'string') {
151
144
  var trimmedName = name.trim()
152
145
  if (trimmedName) {
@@ -172,6 +165,7 @@ function truncate(s) {
172
165
  return s.length > 100 ? safeTruncate(s, 100) + ' [...]' : s
173
166
  }
174
167
 
168
+
175
169
  function getElementById(refElement, id) {
176
170
  // Use the element ownerDocument here, because tests are executed in an iframe, so
177
171
  // document.getElementById won't work.
@@ -180,25 +174,40 @@ function getElementById(refElement, id) {
180
174
  : null
181
175
  }
182
176
 
183
- function getTextualContent(element) {
177
+ function getTextualContent(element, userProgrammaticAttribute) {
184
178
  if (element.isContentEditable) {
185
179
  return
186
180
  }
187
181
 
188
182
  if ('innerText' in element) {
189
183
  var text = element.innerText
184
+
185
+ var removeTextFromElements = function(query){
186
+ var list = element.querySelectorAll(query)
187
+ for (var index = 0; index < list.length; index += 1) {
188
+ var _element = list[index]
189
+ if ('innerText' in _element) {
190
+ var textToReplace = _element.innerText
191
+ if (textToReplace && textToReplace.trim().length > 0) {
192
+ text = text.replace(textToReplace, '')
193
+ }
194
+ }
195
+ }
196
+ }
197
+
190
198
  if (!supportsInnerTextScriptAndStyleRemoval()) {
191
199
  // remove the inner text of SCRIPT and STYLES from the result. This is a bit dirty, but should
192
200
  // be relatively fast and work in most cases.
193
- var elementsTextToRemove = element.querySelectorAll('script, style')
194
- // eslint-disable-next-line @typescript-eslint/prefer-for-of
195
- for (var i = 0; i < elementsTextToRemove.length; i += 1) {
196
- var innerText = elementsTextToRemove[i].innerText
197
- if (innerText.trim().length > 0) {
198
- text = text.replace(innerText, '')
199
- }
200
- }
201
+ removeTextFromElements('script, style')
201
202
  }
203
+
204
+ // remove the text of elements with programmatic attribute value
205
+ removeTextFromElements('[' + DEFAULT_PROGRAMMATIC_ATTRIBUTE + ']')
206
+
207
+ if (userProgrammaticAttribute) {
208
+ removeTextFromElements('[' + userProgrammaticAttribute + ']')
209
+ }
210
+
202
211
  return text
203
212
  }
204
213
 
@@ -206,22 +215,24 @@ function getTextualContent(element) {
206
215
  }
207
216
 
208
217
  /**
209
- * Returns true if element.innerText excludes the text from inline SCRIPT and STYLE element. This
210
- * should be the case everywhere except on some version of Internet Explorer.
211
- * See http://perfectionkills.com/the-poor-misunderstood-innerText/#diff-with-textContent
218
+ * Returns true if element.innerText excludes the text from inline SCRIPT and STYLE element. This
219
+ * should be the case everywhere except on Internet Explorer 10 and 11 (see [1])
220
+ *
221
+ * The innerText property relies on what is actually rendered to compute its output, so to check if
222
+ * it actually excludes SCRIPT and STYLE content, a solution would be to create a style element, set
223
+ * its content to '*', inject it in the document body, and check if the style element innerText
224
+ * property returns '*'. Using a new `document` instance won't work as it is not rendered.
225
+ *
226
+ * This solution requires specific CSP rules (see [2]) to be set by the customer. We want to avoid
227
+ * this, so instead we rely on browser detection. In case of false negative, the impact should be
228
+ * low, since we rely on this result to remove the SCRIPT and STYLE innerText (which will be empty)
229
+ * from a parent element innerText.
230
+ *
231
+ * [1]: https://web.archive.org/web/20210602165716/http://perfectionkills.com/the-poor-misunderstood-innerText/#diff-with-textContent
232
+ * [2]: https://github.com/DataDog/browser-sdk/issues/1084
212
233
  */
213
- var supportsInnerTextScriptAndStyleRemovalResult
214
234
  function supportsInnerTextScriptAndStyleRemoval() {
215
- if (supportsInnerTextScriptAndStyleRemovalResult === undefined) {
216
- var style = document.createElement('style')
217
- style.textContent = '*'
218
- var div = document.createElement('div')
219
- div.appendChild(style)
220
- document.body.appendChild(div)
221
- supportsInnerTextScriptAndStyleRemovalResult = div.innerText === ''
222
- document.body.removeChild(div)
223
- }
224
- return supportsInnerTextScriptAndStyleRemovalResult
235
+ return !isIE()
225
236
  }
226
237
 
227
238
  /**
@@ -0,0 +1,157 @@
1
+ import { arrayFrom, cssEscape, elementMatches,map} from '@cloudcare/browser-core'
2
+
3
+ /**
4
+ * Stable attributes are attributes that are commonly used to identify parts of a UI (ex:
5
+ * component). Those attribute values should not be generated randomly (hardcoded most of the time)
6
+ * and stay the same across deploys. They are not necessarily unique across the document.
7
+ */
8
+ var STABLE_ATTRIBUTES = [
9
+ 'data-guance-action-name',
10
+ // Common test attributes (list provided by google recorder)
11
+ 'data-testid',
12
+ 'data-test',
13
+ 'data-qa',
14
+ 'data-cy',
15
+ 'data-test-id',
16
+ 'data-qa-id',
17
+ 'data-testing',
18
+ // FullStory decorator attributes:
19
+ 'data-component',
20
+ 'data-element',
21
+ 'data-source-file',
22
+ ]
23
+
24
+ export function getSelectorsFromElement(element, actionNameAttribute) {
25
+ var attributeSelectors = getStableAttributeSelectors()
26
+ if (actionNameAttribute) {
27
+ attributeSelectors = [function(element){return getAttributeSelector(actionNameAttribute, element)}].concat(
28
+ attributeSelectors
29
+ )
30
+ }
31
+ return {
32
+ selector: getSelectorFromElement(element, [getIDSelector], [getClassSelector]),
33
+ selector_with_stable_attributes: getSelectorFromElement(
34
+ element,
35
+ attributeSelectors.concat(getIDSelector),
36
+ attributeSelectors.concat(getClassSelector)
37
+ ),
38
+ selector_without_classes: getSelectorFromElement(
39
+ element,
40
+ attributeSelectors.concat(getIDSelector),
41
+ attributeSelectors
42
+ ),
43
+ }
44
+ }
45
+
46
+ function getSelectorFromElement(
47
+ targetElement,
48
+ globallyUniqueSelectorStrategies,
49
+ uniqueAmongChildrenSelectorStrategies
50
+ ){
51
+ var targetElementSelector = []
52
+ var element = targetElement
53
+
54
+ while (element && element.nodeName !== 'HTML') {
55
+ var globallyUniqueSelector = findSelector(element, globallyUniqueSelectorStrategies, isSelectorUniqueGlobally)
56
+ if (globallyUniqueSelector) {
57
+ targetElementSelector.unshift(globallyUniqueSelector)
58
+ break
59
+ }
60
+
61
+ var uniqueSelectorAmongChildren = findSelector(
62
+ element,
63
+ uniqueAmongChildrenSelectorStrategies,
64
+ isSelectorUniqueAmongChildren
65
+ )
66
+ if (uniqueSelectorAmongChildren) {
67
+ targetElementSelector.unshift(uniqueSelectorAmongChildren)
68
+ } else {
69
+ targetElementSelector.unshift(getPositionSelector(element))
70
+ }
71
+
72
+ element = element.parentElement
73
+ }
74
+
75
+ return targetElementSelector.join('>')
76
+ }
77
+
78
+ function getIDSelector(element) {
79
+ if (element.id) {
80
+ return '#' + cssEscape(element.id)
81
+ }
82
+ }
83
+
84
+ function getClassSelector(element){
85
+ if (element.classList.length > 0) {
86
+ var orderedClassList = arrayFrom(element.classList).sort()
87
+ return element.tagName + map(orderedClassList, function(className){return '.' + cssEscape(className)}).join('')
88
+ }
89
+ }
90
+
91
+ var stableAttributeSelectorsCache
92
+ function getStableAttributeSelectors() {
93
+ if (!stableAttributeSelectorsCache) {
94
+ stableAttributeSelectorsCache = map(STABLE_ATTRIBUTES,
95
+ function(attribute) { return function(element) { return getAttributeSelector(attribute, element) }}
96
+ )
97
+ }
98
+ return stableAttributeSelectorsCache
99
+ }
100
+
101
+ function getAttributeSelector(attributeName, element) {
102
+ if (element.hasAttribute(attributeName)) {
103
+ return element.tagName + '[' + attributeName + '="' + cssEscape(element.getAttribute(attributeName))+'"]'
104
+ }
105
+ }
106
+
107
+ function getPositionSelector(element) {
108
+ var parent = element.parentElement
109
+ var sibling = parent.firstElementChild
110
+ var currentIndex = 0
111
+ var elementIndex
112
+
113
+ while (sibling) {
114
+ if (sibling.tagName === element.tagName) {
115
+ currentIndex += 1
116
+ if (sibling === element) {
117
+ elementIndex = currentIndex
118
+ }
119
+
120
+ if (elementIndex !== undefined && currentIndex > 1) {
121
+ // Performance improvement: avoid iterating over all children, stop as soon as we are sure
122
+ // the element is not alone
123
+ break
124
+ }
125
+ }
126
+ sibling = sibling.nextElementSibling
127
+ }
128
+
129
+ return currentIndex === 1 ? element.tagName : element.tagName + ':nth-of-type(' + elementIndex + ')'
130
+ }
131
+
132
+ function findSelector(
133
+ element,
134
+ selectorGetters,
135
+ predicate
136
+ ) {
137
+ for (var selectorGetter of selectorGetters) {
138
+ var selector = selectorGetter(element)
139
+ if (selector && predicate(element, selector)) {
140
+ return selector
141
+ }
142
+ }
143
+ }
144
+
145
+ function isSelectorUniqueGlobally(element, selector) {
146
+ return element.ownerDocument.body.querySelectorAll(selector).length === 1
147
+ }
148
+
149
+ function isSelectorUniqueAmongChildren(element, selector) {
150
+ for (var i = 0; i < element.parentElement.children.length; i++) {
151
+ var sibling = element.parentElement.children[i]
152
+ if (sibling !== element && elementMatches(sibling, selector)) {
153
+ return false
154
+ }
155
+ }
156
+ return true
157
+ }
@@ -0,0 +1,85 @@
1
+ import { addEventListener, DOM_EVENT, each } from '@cloudcare/browser-core'
2
+
3
+ export function listenActionEvents(events) {
4
+ var onClick = events.onClick
5
+ var onPointerDown = events.onPointerDown
6
+ var hasSelectionChanged = false
7
+ var selectionEmptyAtPointerDown
8
+ var hasInputChanged = false
9
+ var clickContext
10
+ var listeners = [
11
+ addEventListener(
12
+ window,
13
+ DOM_EVENT.POINTER_DOWN,
14
+ function (event) {
15
+ hasSelectionChanged = false
16
+ selectionEmptyAtPointerDown = isSelectionEmpty()
17
+ if (isMouseEventOnElement(event)) {
18
+ clickContext = onPointerDown(event)
19
+ }
20
+ },
21
+ { capture: true }
22
+ ),
23
+
24
+ addEventListener(
25
+ window,
26
+ DOM_EVENT.SELECTION_CHANGE,
27
+ function () {
28
+ if (!selectionEmptyAtPointerDown || !isSelectionEmpty()) {
29
+ hasSelectionChanged = true
30
+ }
31
+ },
32
+ { capture: true }
33
+ ),
34
+
35
+ addEventListener(
36
+ window,
37
+ DOM_EVENT.CLICK,
38
+ function (clickEvent) {
39
+ if (isMouseEventOnElement(clickEvent) && clickContext) {
40
+ // Use a scoped variable to make sure the value is not changed by other clicks
41
+ var userActivity = {
42
+ selection: hasSelectionChanged,
43
+ input: hasInputChanged
44
+ }
45
+
46
+ if (!hasInputChanged) {
47
+ setTimeout(function () {
48
+ userActivity.input = hasInputChanged
49
+ })
50
+ }
51
+ onClick(clickContext, clickEvent, function () {
52
+ return userActivity
53
+ })
54
+ clickContext = undefined
55
+ }
56
+ },
57
+ { capture: true }
58
+ ),
59
+
60
+ addEventListener(
61
+ window,
62
+ DOM_EVENT.INPUT,
63
+ function () {
64
+ hasInputChanged = true
65
+ },
66
+ { capture: true }
67
+ )
68
+ ]
69
+
70
+ return {
71
+ stop: function () {
72
+ each(listeners, function (listener) {
73
+ return listener.stop()
74
+ })
75
+ }
76
+ }
77
+ }
78
+
79
+ function isSelectionEmpty() {
80
+ var selection = window.getSelection()
81
+ return !selection || selection.isCollapsed
82
+ }
83
+ function isMouseEventOnElement(event) {
84
+ return event.target instanceof Element
85
+ }