forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdq-element.js
More file actions
246 lines (208 loc) · 6.3 KB
/
dq-element.js
File metadata and controls
246 lines (208 loc) · 6.3 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
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';
import memoize from './memoize';
import getNodeAttributes from './get-node-attributes';
import VirtualNode from '../../core/base/virtual-node/virtual-node';
const CACHE_KEY = 'DqElm.RunOptions';
function getOuterHtml(element) {
let source = element.outerHTML;
if (!source && typeof window.XMLSerializer === 'function') {
source = new window.XMLSerializer().serializeToString(element);
}
return source || '';
}
/**
* Truncates the outerHTML property of an element
* @param {Node} element the element node which needs to be truncated
*/
export function truncateElement(element) {
const maxLen = 300;
const maxAttrNameOrValueLen = 20;
const deepStr = getOuterHtml(element);
let vNode = getNodeFromTree(element);
if (!vNode) {
vNode = new VirtualNode(element);
}
const { nodeName } = vNode.props;
if (deepStr.length < maxLen) {
return deepStr;
}
const attributeStrList = [];
const shallowNode = element.cloneNode(false);
const elementNodeMap = getNodeAttributes(shallowNode);
let str = getOuterHtml(shallowNode);
if (str.length < maxLen) {
let attrString = '';
for (const { name, value } of elementNodeMap) {
const attr = { name, value };
attrString += ` ${attr.name}="${attr.value}"`;
}
str = `<${nodeName}${attrString}>`;
return str;
}
let strLen = `<${nodeName}>`.length;
for (const { name, value } of elementNodeMap) {
if (strLen > maxLen) {
break;
}
const attr = { name, value };
let attrName = attr.name;
let attrValue = attr.value;
attrName =
attrName.length > maxAttrNameOrValueLen
? attrName.substring(0, maxAttrNameOrValueLen) + '...'
: attrName;
attrValue =
attrValue.length > maxAttrNameOrValueLen
? attrValue.substring(0, maxAttrNameOrValueLen) + '...'
: attrValue;
const strAttr = `${attrName}="${attrValue}"`;
strLen += (' ' + strAttr).length;
attributeStrList.push(strAttr);
}
str = `<${nodeName} ${attributeStrList.join(' ')}>`;
if (str.length > maxLen) {
str = str.substring(0, maxLen) + ' ...>';
} else if (attributeStrList.length < elementNodeMap.length) {
str = str.substring(0, str.length - 1) + ' ...>';
}
return str;
}
function getSource(element) {
if (!element) {
return '';
}
return truncateElement(element);
}
/**
* "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
*/
const DqElement = memoize(function DqElement(elm, options, spec) {
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) {
this.source = this.spec.source ?? getSource(this._element);
}
return this;
});
DqElement.prototype = {
/**
* A unique CSS selector for the element, designed for readability
* @return {String}
*/
get selector() {
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;
}
};
/** @deprecated */
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;