forked from dequelabs/axe-core
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrule-should-run.js
More file actions
83 lines (72 loc) · 2.5 KB
/
rule-should-run.js
File metadata and controls
83 lines (72 loc) · 2.5 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
/**
* Check if a rule matches the value of runOnly type=tag
* @private
* @param {object} rule
* @param {object} runOnly Value of runOnly with type=tags
* @return {bool}
*/
function matchTags(rule, runOnly) {
let include, exclude;
const defaultExclude =
// TODO: es-modules_audit
axe._audit && axe._audit.tagExclude ? axe._audit.tagExclude : [];
// normalize include/exclude
if (runOnly.hasOwnProperty('include') || runOnly.hasOwnProperty('exclude')) {
// Wrap include and exclude if it's not already an array
include = runOnly.include || [];
include = Array.isArray(include) ? include : [include];
exclude = runOnly.exclude || [];
exclude = Array.isArray(exclude) ? exclude : [exclude];
// add defaults, unless mentioned in include
exclude = exclude.concat(
defaultExclude.filter(tag => {
return include.indexOf(tag) === -1;
})
);
// Otherwise, only use the include value, ignore exclude
} else {
include = Array.isArray(runOnly) ? runOnly : [runOnly];
// exclude the defaults not included
exclude = defaultExclude.filter(tag => {
return include.indexOf(tag) === -1;
});
}
const matching = include.some(tag => {
return rule.tags.indexOf(tag) !== -1;
});
if (matching || (include.length === 0 && rule.enabled !== false)) {
return exclude.every(tag => {
return rule.tags.indexOf(tag) === -1;
});
} else {
return false;
}
}
/**
* Determines whether a rule should run
* @param {Rule} rule The rule to test
* @param {Context} context The context of the Audit
* @param {Object} options Options object
* @return {Boolean}
*/
function ruleShouldRun(rule, context, options) {
const runOnly = options.runOnly || {};
const ruleOptions = (options.rules || {})[rule.id];
// Never run page level rules if the context is not on the page
if (rule.pageLevel && !context.page) {
return false;
// First, runOnly type rule overrides anything else
} else if (runOnly.type === 'rule') {
return runOnly.values.indexOf(rule.id) !== -1;
// Second, if options.rules[rule].enabled is set, it overrides all
} else if (ruleOptions && typeof ruleOptions.enabled === 'boolean') {
return ruleOptions.enabled;
// Third, if tags are set, look at those
} else if (runOnly.type === 'tag' && runOnly.values) {
return matchTags(rule, runOnly.values);
// If nothing is set, only check for default excludes
} else {
return matchTags(rule, []);
}
}
export default ruleShouldRun;