@amplitude/element-selector 0.1.0 → 0.1.1-featelement-selector-v1.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.
- package/lib/cjs/engine.d.ts.map +1 -1
- package/lib/cjs/engine.js +42 -7
- package/lib/cjs/engine.js.map +1 -1
- package/lib/cjs/generate-selector.d.ts +54 -0
- package/lib/cjs/generate-selector.d.ts.map +1 -0
- package/lib/cjs/generate-selector.js +76 -0
- package/lib/cjs/generate-selector.js.map +1 -0
- package/lib/cjs/index.d.ts +2 -0
- package/lib/cjs/index.d.ts.map +1 -1
- package/lib/cjs/index.js +19 -1
- package/lib/cjs/index.js.map +1 -1
- package/lib/cjs/legacy-css-path.d.ts +84 -0
- package/lib/cjs/legacy-css-path.d.ts.map +1 -0
- package/lib/cjs/legacy-css-path.js +230 -0
- package/lib/cjs/legacy-css-path.js.map +1 -0
- package/lib/esm/engine.d.ts.map +1 -1
- package/lib/esm/engine.js +42 -7
- package/lib/esm/engine.js.map +1 -1
- package/lib/esm/generate-selector.d.ts +54 -0
- package/lib/esm/generate-selector.d.ts.map +1 -0
- package/lib/esm/generate-selector.js +72 -0
- package/lib/esm/generate-selector.js.map +1 -0
- package/lib/esm/index.d.ts +2 -0
- package/lib/esm/index.d.ts.map +1 -1
- package/lib/esm/index.js +16 -0
- package/lib/esm/index.js.map +1 -1
- package/lib/esm/legacy-css-path.d.ts +84 -0
- package/lib/esm/legacy-css-path.d.ts.map +1 -0
- package/lib/esm/legacy-css-path.js +226 -0
- package/lib/esm/legacy-css-path.js.map +1 -0
- package/package.json +1 -1
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical legacy CSS-path walker.
|
|
3
|
+
*
|
|
4
|
+
* This is the Chromium-DevTools-derived `cssPath` algorithm that was the only
|
|
5
|
+
* selector source autocapture used before the strategy engine existed. It still
|
|
6
|
+
* matters in two situations:
|
|
7
|
+
*
|
|
8
|
+
* 1. The kill switch: when `ResolvedSelectorConfig.enabled === false`, the
|
|
9
|
+
* engine routes here so a customer who flips the engine off mid-session
|
|
10
|
+
* keeps emitting the same selectors their downstream zones and cohorts
|
|
11
|
+
* were built against.
|
|
12
|
+
* 2. The safety net: when the strategy chain or its internal walker throws
|
|
13
|
+
* anything unexpected, the engine catches and falls back here. The
|
|
14
|
+
* autocapture click handler must always produce *some* selector — a
|
|
15
|
+
* runtime exception in selector generation is a worse outcome than a
|
|
16
|
+
* slightly less stable selector.
|
|
17
|
+
*
|
|
18
|
+
* Until this file existed, the algorithm was duplicated in two places kept
|
|
19
|
+
* "in sync by comment":
|
|
20
|
+
*
|
|
21
|
+
* - `packages/plugin-autocapture-browser/src/libs/element-path.ts`
|
|
22
|
+
* - `javascript/packages/session-replay-ui/src/utils/element-path.ts`
|
|
23
|
+
*
|
|
24
|
+
* Both will eventually re-export from here so divergence becomes impossible.
|
|
25
|
+
*
|
|
26
|
+
* Behavior is byte-identical to those two copies — selectors emitted by this
|
|
27
|
+
* function for a given element MUST match what the SDK has been emitting in
|
|
28
|
+
* production. Do not "fix" the selector format without an explicit version
|
|
29
|
+
* bump and a coordinated dashboard rollout.
|
|
30
|
+
*
|
|
31
|
+
* **Why this lives in the package, not as an optional consumer concern:**
|
|
32
|
+
*
|
|
33
|
+
* Every consumer that uses `engine.generate(el)` benefits from the kill
|
|
34
|
+
* switch and the safety net automatically. Without this, each new consumer
|
|
35
|
+
* (Chrome extension visual tagger, future tagging surfaces) has to
|
|
36
|
+
* reimplement the same router and the same legacy walker. We learned that
|
|
37
|
+
* the hard way — by writing the same `if (config.enabled && engine) ...
|
|
38
|
+
* else cssPath()` block in two repositories with two copies of cssPath.
|
|
39
|
+
*
|
|
40
|
+
* Code is adapted from The Chromium Authors.
|
|
41
|
+
* Source: https://github.com/ChromeDevTools/devtools-frontend/blob/main/front_end/panels/elements/DOMPath.ts#L14
|
|
42
|
+
* License: BSD-style license
|
|
43
|
+
*
|
|
44
|
+
* Copyright 2014 The Chromium Authors
|
|
45
|
+
*
|
|
46
|
+
* Redistribution and use in source and binary forms, with or without
|
|
47
|
+
* modification, are permitted provided that the following conditions are
|
|
48
|
+
* met:
|
|
49
|
+
*
|
|
50
|
+
* * Redistributions of source code must retain the above copyright
|
|
51
|
+
* notice, this list of conditions and the following disclaimer.
|
|
52
|
+
* * Redistributions in binary form must reproduce the above
|
|
53
|
+
* copyright notice, this list of conditions and the following
|
|
54
|
+
* disclaimer in the documentation and/or other materials provided
|
|
55
|
+
* with the distribution.
|
|
56
|
+
* * Neither the name of Google Inc. nor the names of its
|
|
57
|
+
* contributors may be used to endorse or promote products derived
|
|
58
|
+
* from this software without specific prior written permission.
|
|
59
|
+
*
|
|
60
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
61
|
+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
62
|
+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
63
|
+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
64
|
+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
65
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
66
|
+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
67
|
+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
68
|
+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
69
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
70
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
71
|
+
*/
|
|
72
|
+
/**
|
|
73
|
+
* Produce a CSS selector string for `node` using the legacy positional walker.
|
|
74
|
+
*
|
|
75
|
+
* Returns the empty string for non-element nodes — same shape every existing
|
|
76
|
+
* caller depends on. Never throws for well-formed Element inputs.
|
|
77
|
+
*
|
|
78
|
+
* @param node Element to identify.
|
|
79
|
+
* @param optimized When `true`, short-circuits at the nearest unique-ish step
|
|
80
|
+
* (id or `<body>` / `<head>` / `<html>`). Default `false`,
|
|
81
|
+
* which matches what autocapture has always passed.
|
|
82
|
+
*/
|
|
83
|
+
export declare const legacyCssPath: (node: Element, optimized?: boolean) => string;
|
|
84
|
+
//# sourceMappingURL=legacy-css-path.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"legacy-css-path.d.ts","sourceRoot":"","sources":["../../src/legacy-css-path.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;AAgBH;;;;;;;;;;GAUG;AACH,eAAO,MAAM,aAAa,SAAU,OAAO,cAAc,OAAO,KAAG,MAuBlE,CAAC"}
|
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Canonical legacy CSS-path walker.
|
|
3
|
+
*
|
|
4
|
+
* This is the Chromium-DevTools-derived `cssPath` algorithm that was the only
|
|
5
|
+
* selector source autocapture used before the strategy engine existed. It still
|
|
6
|
+
* matters in two situations:
|
|
7
|
+
*
|
|
8
|
+
* 1. The kill switch: when `ResolvedSelectorConfig.enabled === false`, the
|
|
9
|
+
* engine routes here so a customer who flips the engine off mid-session
|
|
10
|
+
* keeps emitting the same selectors their downstream zones and cohorts
|
|
11
|
+
* were built against.
|
|
12
|
+
* 2. The safety net: when the strategy chain or its internal walker throws
|
|
13
|
+
* anything unexpected, the engine catches and falls back here. The
|
|
14
|
+
* autocapture click handler must always produce *some* selector — a
|
|
15
|
+
* runtime exception in selector generation is a worse outcome than a
|
|
16
|
+
* slightly less stable selector.
|
|
17
|
+
*
|
|
18
|
+
* Until this file existed, the algorithm was duplicated in two places kept
|
|
19
|
+
* "in sync by comment":
|
|
20
|
+
*
|
|
21
|
+
* - `packages/plugin-autocapture-browser/src/libs/element-path.ts`
|
|
22
|
+
* - `javascript/packages/session-replay-ui/src/utils/element-path.ts`
|
|
23
|
+
*
|
|
24
|
+
* Both will eventually re-export from here so divergence becomes impossible.
|
|
25
|
+
*
|
|
26
|
+
* Behavior is byte-identical to those two copies — selectors emitted by this
|
|
27
|
+
* function for a given element MUST match what the SDK has been emitting in
|
|
28
|
+
* production. Do not "fix" the selector format without an explicit version
|
|
29
|
+
* bump and a coordinated dashboard rollout.
|
|
30
|
+
*
|
|
31
|
+
* **Why this lives in the package, not as an optional consumer concern:**
|
|
32
|
+
*
|
|
33
|
+
* Every consumer that uses `engine.generate(el)` benefits from the kill
|
|
34
|
+
* switch and the safety net automatically. Without this, each new consumer
|
|
35
|
+
* (Chrome extension visual tagger, future tagging surfaces) has to
|
|
36
|
+
* reimplement the same router and the same legacy walker. We learned that
|
|
37
|
+
* the hard way — by writing the same `if (config.enabled && engine) ...
|
|
38
|
+
* else cssPath()` block in two repositories with two copies of cssPath.
|
|
39
|
+
*
|
|
40
|
+
* Code is adapted from The Chromium Authors.
|
|
41
|
+
* Source: https://github.com/ChromeDevTools/devtools-frontend/blob/main/front_end/panels/elements/DOMPath.ts#L14
|
|
42
|
+
* License: BSD-style license
|
|
43
|
+
*
|
|
44
|
+
* Copyright 2014 The Chromium Authors
|
|
45
|
+
*
|
|
46
|
+
* Redistribution and use in source and binary forms, with or without
|
|
47
|
+
* modification, are permitted provided that the following conditions are
|
|
48
|
+
* met:
|
|
49
|
+
*
|
|
50
|
+
* * Redistributions of source code must retain the above copyright
|
|
51
|
+
* notice, this list of conditions and the following disclaimer.
|
|
52
|
+
* * Redistributions in binary form must reproduce the above
|
|
53
|
+
* copyright notice, this list of conditions and the following
|
|
54
|
+
* disclaimer in the documentation and/or other materials provided
|
|
55
|
+
* with the distribution.
|
|
56
|
+
* * Neither the name of Google Inc. nor the names of its
|
|
57
|
+
* contributors may be used to endorse or promote products derived
|
|
58
|
+
* from this software without specific prior written permission.
|
|
59
|
+
*
|
|
60
|
+
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
61
|
+
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
62
|
+
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
63
|
+
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
64
|
+
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
65
|
+
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
66
|
+
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
67
|
+
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
68
|
+
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
69
|
+
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
70
|
+
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
71
|
+
*/
|
|
72
|
+
import { __values } from "tslib";
|
|
73
|
+
/* istanbul ignore file -- the algorithm is locked-down behavior inherited from
|
|
74
|
+
Chromium and validated by the SDK's own end-to-end suite. Re-running line
|
|
75
|
+
coverage here only obscures the parts of the package that *do* have
|
|
76
|
+
meaningful coverage. */
|
|
77
|
+
import { escapeCssIdentifier } from './helpers/escape-css-identifier';
|
|
78
|
+
var Step = /** @class */ (function () {
|
|
79
|
+
function Step(value, optimized) {
|
|
80
|
+
this.value = value;
|
|
81
|
+
this.optimized = optimized;
|
|
82
|
+
}
|
|
83
|
+
Step.prototype.toString = function () {
|
|
84
|
+
return this.value;
|
|
85
|
+
};
|
|
86
|
+
return Step;
|
|
87
|
+
}());
|
|
88
|
+
/**
|
|
89
|
+
* Produce a CSS selector string for `node` using the legacy positional walker.
|
|
90
|
+
*
|
|
91
|
+
* Returns the empty string for non-element nodes — same shape every existing
|
|
92
|
+
* caller depends on. Never throws for well-formed Element inputs.
|
|
93
|
+
*
|
|
94
|
+
* @param node Element to identify.
|
|
95
|
+
* @param optimized When `true`, short-circuits at the nearest unique-ish step
|
|
96
|
+
* (id or `<body>` / `<head>` / `<html>`). Default `false`,
|
|
97
|
+
* which matches what autocapture has always passed.
|
|
98
|
+
*/
|
|
99
|
+
export var legacyCssPath = function (node, optimized) {
|
|
100
|
+
// `node` is already an Element; this check is defensive.
|
|
101
|
+
if (node.nodeType !== Node.ELEMENT_NODE) {
|
|
102
|
+
return '';
|
|
103
|
+
}
|
|
104
|
+
var steps = [];
|
|
105
|
+
var contextNode = node;
|
|
106
|
+
while (contextNode) {
|
|
107
|
+
var step = cssPathStep(contextNode, Boolean(optimized), contextNode === node);
|
|
108
|
+
if (!step) {
|
|
109
|
+
break;
|
|
110
|
+
}
|
|
111
|
+
steps.push(step);
|
|
112
|
+
if (step.optimized) {
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
contextNode = contextNode.parentElement;
|
|
116
|
+
}
|
|
117
|
+
steps.reverse();
|
|
118
|
+
return steps.join(' > ');
|
|
119
|
+
};
|
|
120
|
+
var cssPathStep = function (node, optimized, isTargetNode) {
|
|
121
|
+
var e_1, _a;
|
|
122
|
+
if (node.nodeType !== Node.ELEMENT_NODE) {
|
|
123
|
+
return null;
|
|
124
|
+
}
|
|
125
|
+
var id = node.getAttribute('id');
|
|
126
|
+
if (optimized) {
|
|
127
|
+
if (id) {
|
|
128
|
+
return new Step(idSelector(id), true);
|
|
129
|
+
}
|
|
130
|
+
var nodeNameLower = node.tagName.toLowerCase();
|
|
131
|
+
if (nodeNameLower === 'body' || nodeNameLower === 'head' || nodeNameLower === 'html') {
|
|
132
|
+
return new Step(nodeNameLower, true);
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
var nodeName = node.tagName.toLowerCase();
|
|
136
|
+
if (id) {
|
|
137
|
+
return new Step(nodeName + idSelector(id), true);
|
|
138
|
+
}
|
|
139
|
+
var parent = node.parentNode;
|
|
140
|
+
if (!parent || parent.nodeType === Node.DOCUMENT_NODE) {
|
|
141
|
+
return new Step(nodeName, true);
|
|
142
|
+
}
|
|
143
|
+
var prefixedOwnClassNamesArray = prefixedElementClassNames(node);
|
|
144
|
+
var needsClassNames = false;
|
|
145
|
+
var needsNthChild = false;
|
|
146
|
+
var ownIndex = -1;
|
|
147
|
+
var elementIndex = -1;
|
|
148
|
+
var siblings = parent.children;
|
|
149
|
+
for (var i = 0; siblings && (ownIndex === -1 || !needsNthChild) && i < siblings.length; ++i) {
|
|
150
|
+
var sibling = siblings[i];
|
|
151
|
+
if (sibling.nodeType !== Node.ELEMENT_NODE) {
|
|
152
|
+
continue;
|
|
153
|
+
}
|
|
154
|
+
elementIndex += 1;
|
|
155
|
+
if (sibling === node) {
|
|
156
|
+
ownIndex = elementIndex;
|
|
157
|
+
continue;
|
|
158
|
+
}
|
|
159
|
+
if (needsNthChild) {
|
|
160
|
+
continue;
|
|
161
|
+
}
|
|
162
|
+
if (sibling.tagName.toLowerCase() !== nodeName) {
|
|
163
|
+
continue;
|
|
164
|
+
}
|
|
165
|
+
needsClassNames = true;
|
|
166
|
+
var ownClassNames = new Set(prefixedOwnClassNamesArray);
|
|
167
|
+
if (!ownClassNames.size) {
|
|
168
|
+
needsNthChild = true;
|
|
169
|
+
continue;
|
|
170
|
+
}
|
|
171
|
+
var siblingClassNamesArray = prefixedElementClassNames(sibling);
|
|
172
|
+
for (var j = 0; j < siblingClassNamesArray.length; ++j) {
|
|
173
|
+
var siblingClass = siblingClassNamesArray[j];
|
|
174
|
+
if (!ownClassNames.has(siblingClass)) {
|
|
175
|
+
continue;
|
|
176
|
+
}
|
|
177
|
+
ownClassNames.delete(siblingClass);
|
|
178
|
+
if (!ownClassNames.size) {
|
|
179
|
+
needsNthChild = true;
|
|
180
|
+
break;
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
var result = nodeName;
|
|
185
|
+
if (isTargetNode &&
|
|
186
|
+
nodeName.toLowerCase() === 'input' &&
|
|
187
|
+
node.getAttribute('type') &&
|
|
188
|
+
!node.getAttribute('id') &&
|
|
189
|
+
!node.getAttribute('class')) {
|
|
190
|
+
result += '[type=' + escapeCssIdentifier(node.getAttribute('type') || '') + ']';
|
|
191
|
+
}
|
|
192
|
+
if (needsNthChild) {
|
|
193
|
+
result += ':nth-child(' + String(ownIndex + 1) + ')';
|
|
194
|
+
}
|
|
195
|
+
else if (needsClassNames) {
|
|
196
|
+
try {
|
|
197
|
+
for (var prefixedOwnClassNamesArray_1 = __values(prefixedOwnClassNamesArray), prefixedOwnClassNamesArray_1_1 = prefixedOwnClassNamesArray_1.next(); !prefixedOwnClassNamesArray_1_1.done; prefixedOwnClassNamesArray_1_1 = prefixedOwnClassNamesArray_1.next()) {
|
|
198
|
+
var prefixedName = prefixedOwnClassNamesArray_1_1.value;
|
|
199
|
+
result += '.' + escapeCssIdentifier(prefixedName.slice(1));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
203
|
+
finally {
|
|
204
|
+
try {
|
|
205
|
+
if (prefixedOwnClassNamesArray_1_1 && !prefixedOwnClassNamesArray_1_1.done && (_a = prefixedOwnClassNamesArray_1.return)) _a.call(prefixedOwnClassNamesArray_1);
|
|
206
|
+
}
|
|
207
|
+
finally { if (e_1) throw e_1.error; }
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
return new Step(result, false);
|
|
211
|
+
};
|
|
212
|
+
var prefixedElementClassNames = function (el) {
|
|
213
|
+
var classAttribute = el.getAttribute('class');
|
|
214
|
+
if (!classAttribute) {
|
|
215
|
+
return [];
|
|
216
|
+
}
|
|
217
|
+
return classAttribute
|
|
218
|
+
.split(/\s+/g)
|
|
219
|
+
.filter(Boolean)
|
|
220
|
+
.map(function (name) {
|
|
221
|
+
// The prefix is required to store "__proto__" in a object-based map.
|
|
222
|
+
return '$' + name;
|
|
223
|
+
});
|
|
224
|
+
};
|
|
225
|
+
var idSelector = function (id) { return '#' + escapeCssIdentifier(id); };
|
|
226
|
+
//# sourceMappingURL=legacy-css-path.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"legacy-css-path.js","sourceRoot":"","sources":["../../src/legacy-css-path.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAsEG;;AAEH;;;0BAG0B;AAE1B,OAAO,EAAE,mBAAmB,EAAE,MAAM,iCAAiC,CAAC;AAEtE;IACE,cAAmB,KAAa,EAAS,SAAkB;QAAxC,UAAK,GAAL,KAAK,CAAQ;QAAS,cAAS,GAAT,SAAS,CAAS;IAAG,CAAC;IAC/D,uBAAQ,GAAR;QACE,OAAO,IAAI,CAAC,KAAK,CAAC;IACpB,CAAC;IACH,WAAC;AAAD,CAAC,AALD,IAKC;AAED;;;;;;;;;;GAUG;AACH,MAAM,CAAC,IAAM,aAAa,GAAG,UAAC,IAAa,EAAE,SAAmB;IAC9D,yDAAyD;IACzD,IAAI,IAAI,CAAC,QAAQ,KAAK,IAAI,CAAC,YAAY,EAAE;QACvC,OAAO,EAAE,CAAC;KACX;IAED,IAAM,KAAK,GAAW,EAAE,CAAC;IACzB,IAAI,WAAW,GAAmB,IAAI,CAAC;IAEvC,OAAO,WAAW,EAAE;QAClB,IAAM,IAAI,GAAG,WAAW,CAAC,WA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O,EAAE,CAAC;KACX;IACD,OAAO,cAAc;SAClB,KAAK,CAAC,MAAM,CAAC;SACb,MAAM,CAAC,OAAO,CAAC;SACf,GAAG,CAAC,UAAC,IAAI;QACR,qEAAqE;QACrE,OAAO,GAAG,GAAG,IAAI,CAAC;IACpB,CAAC,CAAC,CAAC;AACP,CAAC,CAAC;AAEF,IAAM,UAAU,GAAG,UAAC,EAAU,IAAa,OAAA,GAAG,GAAG,mBAAmB,CAAC,EAAE,CAAC,EAA7B,CAA6B,CAAC","sourcesContent":["/**\n * Canonical legacy CSS-path walker.\n *\n * This is the Chromium-DevTools-derived `cssPath` algorithm that was the only\n * selector source autocapture used before the strategy engine existed. It still\n * matters in two situations:\n *\n * 1. The kill switch: when `ResolvedSelectorConfig.enabled === false`, the\n * engine routes here so a customer who flips the engine off mid-session\n * keeps emitting the same selectors their downstream zones and cohorts\n * were built against.\n * 2. The safety net: when the strategy chain or its internal walker throws\n * anything unexpected, the engine catches and falls back here. The\n * autocapture click handler must always produce *some* selector — a\n * runtime exception in selector generation is a worse outcome than a\n * slightly less stable selector.\n *\n * Until this file existed, the algorithm was duplicated in two places kept\n * \"in sync by comment\":\n *\n * - `packages/plugin-autocapture-browser/src/libs/element-path.ts`\n * - `javascript/packages/session-replay-ui/src/utils/element-path.ts`\n *\n * Both will eventually re-export from here so divergence becomes impossible.\n *\n * Behavior is byte-identical to those two copies — selectors emitted by this\n * function for a given element MUST match what the SDK has been emitting in\n * production. Do not \"fix\" the selector format without an explicit version\n * bump and a coordinated dashboard rollout.\n *\n * **Why this lives in the package, not as an optional consumer concern:**\n *\n * Every consumer that uses `engine.generate(el)` benefits from the kill\n * switch and the safety net automatically. Without this, each new consumer\n * (Chrome extension visual tagger, future tagging surfaces) has to\n * reimplement the same router and the same legacy walker. We learned that\n * the hard way — by writing the same `if (config.enabled && engine) ...\n * else cssPath()` block in two repositories with two copies of cssPath.\n *\n * Code is adapted from The Chromium Authors.\n * Source: https://github.com/ChromeDevTools/devtools-frontend/blob/main/front_end/panels/elements/DOMPath.ts#L14\n * License: BSD-style license\n *\n * Copyright 2014 The Chromium Authors\n *\n * Redistribution and use in source and binary forms, with or without\n * modification, are permitted provided that the following conditions are\n * met:\n *\n * * Redistributions of source code must retain the above copyright\n * notice, this list of conditions and the following disclaimer.\n * * Redistributions in binary form must reproduce the above\n * copyright notice, this list of conditions and the following\n * disclaimer in the documentation and/or other materials provided\n * with the distribution.\n * * Neither the name of Google Inc. nor the names of its\n * contributors may be used to endorse or promote products derived\n * from this software without specific prior written permission.\n *\n * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS\n * \"AS IS\" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT\n * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR\n * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT\n * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,\n * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\n * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,\n * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY\n * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT\n * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE\n * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\n */\n\n/* istanbul ignore file -- the algorithm is locked-down behavior inherited from\n Chromium and validated by the SDK's own end-to-end suite. Re-running line\n coverage here only obscures the parts of the package that *do* have\n meaningful coverage. */\n\nimport { escapeCssIdentifier } from './helpers/escape-css-identifier';\n\nclass Step {\n constructor(public value: string, public optimized: boolean) {}\n toString(): string {\n return this.value;\n }\n}\n\n/**\n * Produce a CSS selector string for `node` using the legacy positional walker.\n *\n * Returns the empty string for non-element nodes — same shape every existing\n * caller depends on. Never throws for well-formed Element inputs.\n *\n * @param node Element to identify.\n * @param optimized When `true`, short-circuits at the nearest unique-ish step\n * (id or `<body>` / `<head>` / `<html>`). Default `false`,\n * which matches what autocapture has always passed.\n */\nexport const legacyCssPath = (node: Element, optimized?: boolean): string => {\n // `node` is already an Element; this check is defensive.\n if (node.nodeType !== Node.ELEMENT_NODE) {\n return '';\n }\n\n const steps: Step[] = [];\n let contextNode: Element | null = node;\n\n while (contextNode) {\n const step = cssPathStep(contextNode, Boolean(optimized), contextNode === node);\n if (!step) {\n break;\n }\n steps.push(step);\n if (step.optimized) {\n break;\n }\n contextNode = contextNode.parentElement;\n }\n\n steps.reverse();\n return steps.join(' > ');\n};\n\nconst cssPathStep = (node: Element, optimized: boolean, isTargetNode: boolean): Step | null => {\n if (node.nodeType !== Node.ELEMENT_NODE) {\n return null;\n }\n\n const id = node.getAttribute('id');\n if (optimized) {\n if (id) {\n return new Step(idSelector(id), true);\n }\n\n const nodeNameLower = node.tagName.toLowerCase();\n if (nodeNameLower === 'body' || nodeNameLower === 'head' || nodeNameLower === 'html') {\n return new Step(nodeNameLower, true);\n }\n }\n\n const nodeName = node.tagName.toLowerCase();\n\n if (id) {\n return new Step(nodeName + idSelector(id), true);\n }\n\n const parent = node.parentNode;\n if (!parent || parent.nodeType === Node.DOCUMENT_NODE) {\n return new Step(nodeName, true);\n }\n\n const prefixedOwnClassNamesArray = prefixedElementClassNames(node);\n let needsClassNames = false;\n let needsNthChild = false;\n let ownIndex = -1;\n let elementIndex = -1;\n\n const siblings: HTMLCollectionOf<Element> = (parent as Element | Document).children;\n\n for (let i = 0; siblings && (ownIndex === -1 || !needsNthChild) && i < siblings.length; ++i) {\n const sibling = siblings[i];\n if (sibling.nodeType !== Node.ELEMENT_NODE) {\n continue;\n }\n\n elementIndex += 1;\n if (sibling === node) {\n ownIndex = elementIndex;\n continue;\n }\n if (needsNthChild) {\n continue;\n }\n\n if (sibling.tagName.toLowerCase() !== nodeName) {\n continue;\n }\n\n needsClassNames = true;\n const ownClassNames = new Set<string>(prefixedOwnClassNamesArray);\n if (!ownClassNames.size) {\n needsNthChild = true;\n continue;\n }\n\n const siblingClassNamesArray = prefixedElementClassNames(sibling);\n for (let j = 0; j < siblingClassNamesArray.length; ++j) {\n const siblingClass = siblingClassNamesArray[j];\n if (!ownClassNames.has(siblingClass)) {\n continue;\n }\n ownClassNames.delete(siblingClass);\n if (!ownClassNames.size) {\n needsNthChild = true;\n break;\n }\n }\n }\n\n let result = nodeName;\n if (\n isTargetNode &&\n nodeName.toLowerCase() === 'input' &&\n node.getAttribute('type') &&\n !node.getAttribute('id') &&\n !node.getAttribute('class')\n ) {\n result += '[type=' + escapeCssIdentifier(node.getAttribute('type') || '') + ']';\n }\n if (needsNthChild) {\n result += ':nth-child(' + String(ownIndex + 1) + ')';\n } else if (needsClassNames) {\n for (const prefixedName of prefixedOwnClassNamesArray) {\n result += '.' + escapeCssIdentifier(prefixedName.slice(1));\n }\n }\n\n return new Step(result, false);\n};\n\nconst prefixedElementClassNames = (el: Element): string[] => {\n const classAttribute = el.getAttribute('class');\n if (!classAttribute) {\n return [];\n }\n return classAttribute\n .split(/\\s+/g)\n .filter(Boolean)\n .map((name) => {\n // The prefix is required to store \"__proto__\" in a object-based map.\n return '$' + name;\n });\n};\n\nconst idSelector = (id: string): string => '#' + escapeCssIdentifier(id);\n"]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@amplitude/element-selector",
|
|
3
|
-
"version": "0.1.0",
|
|
3
|
+
"version": "0.1.1-featelement-selector-v1.0",
|
|
4
4
|
"description": "Shared element-selector algorithm consumed by autocapture, the Amplitude dashboard, and the Chrome extension visual tagger.",
|
|
5
5
|
"author": "Amplitude Inc",
|
|
6
6
|
"homepage": "https://github.com/amplitude/Amplitude-TypeScript",
|