-
Notifications
You must be signed in to change notification settings - Fork 743
/
page-no-duplicate-evaluate.js
48 lines (40 loc) · 1.46 KB
/
page-no-duplicate-evaluate.js
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
import cache from '../../core/base/cache';
import { querySelectorAllFilter } from '../../core/utils';
import { isVisibleToScreenReaders, findUpVirtual } from '../../commons/dom';
import { getRole } from '../../commons/aria';
function pageNoDuplicateEvaluate(node, options, virtualNode) {
if (!options || !options.selector || typeof options.selector !== 'string') {
throw new TypeError(
'page-no-duplicate requires options.selector to be a string'
);
}
// only look at the first node and it's related nodes
const key = 'page-no-duplicate;' + options.selector;
if (cache.get(key)) {
this.data('ignored');
return;
}
cache.set(key, true);
let elms = querySelectorAllFilter(axe._tree[0], options.selector, elm =>
isVisibleToScreenReaders(elm)
);
// @deprecated options.nativeScopeFilter
// Filter elements that, within certain contexts, don't map their role.
// e.g. a <footer> inside a <main> is not a banner, but in the <body> context it is
if (typeof options.nativeScopeFilter === 'string') {
elms = elms.filter(elm => {
return (
elm.actualNode.hasAttribute('role') ||
!findUpVirtual(elm, options.nativeScopeFilter)
);
});
}
if (typeof options.role === 'string') {
elms = elms.filter(elm => getRole(elm) === options.role);
}
this.relatedNodes(
elms.filter(elm => elm !== virtualNode).map(elm => elm.actualNode)
);
return elms.length <= 1;
}
export default pageNoDuplicateEvaluate;