-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathdq-element.js
More file actions
332 lines (296 loc) · 9.03 KB
/
Copy pathdq-element.js
File metadata and controls
332 lines (296 loc) · 9.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import getSelector from './get-selector';
import getAncestry from './get-ancestry';
import getXpath from './get-xpath';
import getNodeFromTree from './get-node-from-tree';
import AbstractVirtualNode from '../base/virtual-node/abstract-virtual-node';
import cache from '../base/cache';
const CACHE_KEY = 'DqElm.RunOptions';
function truncate(str, maxLength) {
maxLength = maxLength || 300;
if (str.length > maxLength) {
var index = str.indexOf('>');
str = str.substring(0, index + 1);
}
return str;
}
/**
* Escapes a string for use in CSS selectors
* @param {String} str - The string to escape
* @returns {String} The escaped string
*/
function escapeCSSSelector(str) {
// Use the CSS.escape method if available
if (window.CSS && window.CSS.escape) {
return window.CSS.escape(str);
}
// Simple fallback for browsers that don't support CSS.escape
return str
.replace(/[!"#$%&'()*+,./:;<=>?@[\\\]^`{|}~]/g, '\\$&')
.replace(/^\d/, '\\3$& ');
}
function generateSelectorWithShadow(elm) {
const selectors = getShadowSelector(elm);
if (typeof selectors === 'string') {
return selectors;
} else {
// merge selectors of an array with ,
return selectors.join(',').replace(/,$/, '');
}
}
function getShadowSelector(elm) {
if (!elm) {
return '';
}
let doc = (elm.getRootNode && elm.getRootNode()) || document;
// Not a DOCUMENT_FRAGMENT - shadow DOM
if (doc.nodeType !== 11) {
return getFullPathSelector(elm);
}
const stack = [];
while (doc.nodeType === 11) {
if (!doc.host) {
return '';
}
stack.unshift({ elm, doc });
elm = doc.host;
doc = elm.getRootNode();
}
stack.unshift({ elm, doc });
return stack.map(item => getFullPathSelector(item.elm));
}
function getFullPathSelector(elm) {
if (elm.nodeName === 'HTML' || elm.nodeName === 'BODY') {
return elm.nodeName.toLowerCase();
}
if (cache.get('getFullPathSelector') === undefined) {
cache.set('getFullPathSelector', new WeakMap());
}
// Check cache first
const sourceCache = cache.get('getFullPathSelector');
if (sourceCache.has(elm)) {
return sourceCache.get(elm);
}
const element = elm;
const names = [];
while (elm.parentElement && elm.nodeName !== 'BODY') {
if (sourceCache.has(elm)) {
names.unshift(sourceCache.get(elm));
break;
} else if (elm.id) {
// Check if the ID is unique in the document before using it
const escapedId = escapeCSSSelector(elm.getAttribute('id'));
const elementsWithSameId = document.querySelectorAll(`#${escapedId}`);
if (elementsWithSameId.length === 1) {
// ID is unique, safe to use
names.unshift('#' + escapedId);
break;
} else {
// ID is not unique, fallback to position-based selector
let c = 1;
let e = elm;
for (; e.previousElementSibling; e = e.previousElementSibling, c++) {
// Increment counter for each previous sibling
}
names.unshift(`${elm.nodeName.toLowerCase()}:nth-child(${c})`);
}
} else {
let c = 1;
let e = elm;
for (; e.previousElementSibling; e = e.previousElementSibling, c++) {
// Increment counter for each previous sibling
}
names.unshift(`${elm.nodeName.toLowerCase()}:nth-child(${c})`);
}
elm = elm.parentElement;
}
const selector = names.join('>');
sourceCache.set(element, selector);
return selector;
}
function getSourceOpt(element) {
if (!element) {
return '';
}
// Initialize cache if needed
if (cache.get('getSourceEfficient') === undefined) {
cache.set('getSourceEfficient', new WeakMap());
}
// Check cache first
const sourceCache = cache.get('getSourceEfficient');
if (sourceCache.has(element)) {
return sourceCache.get(element);
}
// Compute value if not cached
const tagName = element.nodeName?.toLowerCase();
if (!tagName) {
return '';
}
let result;
try {
const attributes = Array.from(element.attributes || [])
.filter(attr => !attr.name.startsWith('data-percy-'))
.map(attr => `${attr.name}="${attr.value}"`)
.join(' ');
const closingTag = element.children.length ? false : true;
if (closingTag) {
result = `<${tagName} ${attributes}>${element.textContent}</${tagName}>`;
} else {
result = attributes ? `<${tagName} ${attributes}>` : `<${tagName}>`;
}
result = truncate(result, 300); // Truncate to 300 characters
// Store in cache
sourceCache.set(element, result);
} catch (e) {
// Handle potential errors (like accessing attributes on non-element nodes)
result = `<${tagName || 'unknown'}>`;
}
return result;
}
function getSource(element) {
if (!element?.outerHTML) {
return '';
}
var source = element.outerHTML;
if (!source && typeof window.XMLSerializer === 'function') {
source = new window.XMLSerializer().serializeToString(element);
}
let htmlString = truncate(source || '');
// Remove unwanted attributes
const regex = /\s*data-percy-[^=]+="[^"]*"/g;
htmlString = htmlString.replace(regex, '');
return htmlString;
}
/**
* "Serialized" `HTMLElement`. It will calculate the CSS selector,
* grab the source (outerHTML) and offer an array for storing frame paths
* @param {HTMLElement} element The element to serialize
* @param {Object} options Propagated from axe.run/etc
* @param {Object} spec Properties to use in place of the element when instantiated on Elements from other frames
*/
function DqElement(elm, options = null, spec = {}) {
if (!options) {
options = cache.get(CACHE_KEY) ?? {};
}
this.spec = spec;
if (elm instanceof AbstractVirtualNode) {
this._virtualNode = elm;
this._element = elm.actualNode;
} else {
this._element = elm;
this._virtualNode = getNodeFromTree(elm);
}
/**
* Whether DqElement was created from an iframe
* @type {boolean}
*/
this.fromFrame = this.spec.selector?.length > 1;
this._includeElementInJson = options.elementRef;
if (options.absolutePaths) {
this._options = { toRoot: true };
}
/**
* Number by which nodes in the flat tree can be sorted
* @type {Number}
*/
this.nodeIndexes = [];
if (Array.isArray(this.spec.nodeIndexes)) {
this.nodeIndexes = this.spec.nodeIndexes;
} else if (typeof this._virtualNode?.nodeIndex === 'number') {
this.nodeIndexes = [this._virtualNode.nodeIndex];
}
/**
* The generated HTML source code of the element
* @type {String|null}
*/
this.source = null;
// TODO: es-modules_audit
if (!axe._audit.noHtml) {
if (axe._cache.get('runTypeAOpt')) {
this.source = this.spec.source ?? getSourceOpt(this._element);
} else {
this.source = this.spec.source ?? getSource(this._element);
}
}
}
DqElement.prototype = {
/**
* A unique CSS selector for the element, designed for readability
* @return {String}
*/
get selector() {
if (axe._cache.get('targetFormat') === 'ancestry') {
return this.spec.selector || [getAncestry(this.element)];
}
if (axe._cache.get('runTypeAOpt')) {
return this.spec.selector || [generateSelectorWithShadow(this.element)];
}
return this.spec.selector || [getSelector(this.element, this._options)];
},
/**
* A unique CSS selector for the element, including its ancestors down to the root node
* @return {String}
*/
get ancestry() {
return this.spec.ancestry || [getAncestry(this.element)];
},
/**
* Xpath to the element
* @return {String}
*/
get xpath() {
return this.spec.xpath || [getXpath(this.element)];
},
/**
* Direct reference to the `HTMLElement` wrapped by this `DQElement`.
*/
get element() {
return this._element;
},
/**
* Converts to a "spec", a form suitable for use with JSON.stringify
* (*not* to pre-stringified JSON)
* @returns {Object}
*/
toJSON() {
const spec = {
selector: this.selector,
source: this.source,
xpath: this.xpath,
ancestry: this.ancestry,
nodeIndexes: this.nodeIndexes,
fromFrame: this.fromFrame
};
if (this._includeElementInJson) {
spec.element = this._element;
}
return spec;
}
};
DqElement.fromFrame = function fromFrame(node, options, frame) {
const spec = DqElement.mergeSpecs(node, frame);
return new DqElement(frame.element, options, spec);
};
DqElement.mergeSpecs = function mergeSpecs(child, parentFrame) {
// Parameter order reversed for backcompat
return {
...child,
selector: [...parentFrame.selector, ...child.selector],
ancestry: [...parentFrame.ancestry, ...child.ancestry],
xpath: [...parentFrame.xpath, ...child.xpath],
nodeIndexes: [...parentFrame.nodeIndexes, ...child.nodeIndexes],
fromFrame: true
};
};
/**
* Set the default options to be used
* @param {Object} RunOptions Options passed to axe.run()
* @property {boolean} elementRef Add element when toJSON is called
* @property {boolean} absolutePaths Use absolute path fro selectors
*/
DqElement.setRunOptions = function setRunOptions({
elementRef,
absolutePaths
}) {
cache.set(CACHE_KEY, { elementRef, absolutePaths });
};
export default DqElement;