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
156 lines (137 loc) · 4.1 KB
/
Copy pathdq-element.js
File metadata and controls
156 lines (137 loc) · 4.1 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
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 getSource from './get-element-source';
const CACHE_KEY = 'DqElm.RunOptions';
/**
* "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;