-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathparser.js
71 lines (61 loc) · 1.78 KB
/
parser.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
const section508 = require('./standard-508')
const wcag2 = require('./standard-wcag2')
/**
* @function
* @description Parser for Accessibility Issue
*/
module.exports = (test, results) => {
// Setup Base Report
let parsed = {
documentTitle: results.documentTitle,
pageUrl: results.pageUrl,
screenCapture: test.screenCapture,
options: test,
total: {
all: 0,
error: 0,
warning: 0,
notice: 0
},
issues: []
}
// Parse each issue to get more helpful code
results.issues.forEach(issue => {
let resources
// Update Report Totals
parsed.total.all += 1
parsed.total[issue.type] += 1
// Generate Help Links
if (test.standard === 'Section508') {
resources = section508(issue.code)
} else if (test.standard.substring(0, 5) === 'WCAG2') {
resources = wcag2(issue.code)
}
let match = issue.message.match(/Recommendation: .+$/g)
let recommendation = null
if (match) {
recommendation = match[0].replace('Recommendation: ', '').trim()
recommendation = recommendation.charAt(0).toUpperCase() + recommendation.slice(1)
}
// Update Issues with Help Links
parsed.issues.push({
code: issue.code,
context: issue.context ? issue.context.replace(/(\r\n|\n|\r)/gm, '').replace(/\s+/g, ' ') : null,
resources: resources,
message: recommendation ? issue.message.replace(match[0], '') : issue.message,
type: issue.type,
typeCode: issue.typeCode,
recommendation: recommendation,
selector: issue.selector
})
})
// Sort Issues by Type
let sortedIssues = parsed.issues.slice(0)
sortedIssues.sort(function(a, b) {
return a.typeCode - b.typeCode
})
parsed.issues = sortedIssues
return new Promise(resolve => {
resolve(parsed)
})
}