-
Notifications
You must be signed in to change notification settings - Fork 44
/
html.js
108 lines (104 loc) · 3.58 KB
/
html.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
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
import { get, post } from '../../throttled-ua.js';
const self = {
name: 'validation.html',
section: 'format',
rule: 'validHTML',
};
const TIMEOUT = 10000;
export const { name } = self;
/**
* @param sr
* @param done
*/
export function check(sr, done) {
let service = null;
if (sr.config.htmlValidator !== undefined) {
service = sr.config.htmlValidator;
} else {
service = 'https://validator.w3.org/nu/';
}
if (sr.config.skipValidation) {
sr.warning(self, 'skipped');
return done();
}
if (!sr.url && !sr.source) {
sr.warning(self, 'no-source');
return done();
}
let req;
const ua = `W3C-Pubrules/${sr.version}`;
if (sr.url) {
req = get(service).set('User-Agent', ua);
req.query({ doc: sr.url, out: 'json' });
} else {
req = post(service)
.set('User-Agent', ua)
.set('Content-Type', 'text/html')
.send(sr.source)
.query({ out: 'json' });
}
req.timeout(TIMEOUT);
req.end((err, res) => {
if (err) {
if (err.timeout === TIMEOUT) {
sr.warning(self, 'timeout');
} else {
sr.error(self, 'no-response');
}
} else if (!res.ok) {
sr.error(self, 'failure', { status: res.status });
} else {
const json = res.body;
if (!json) return sr.throw('No JSON input.');
if (json.messages && json.messages.length) {
for (let i = 0, n = json.messages.length; i < n; i += 1) {
const msg = json.messages[i];
// {
// "type": "error",
// "lastLine": 26,
// "lastColumn": 14,
// "firstColumn": 7,
// "message": "blah",
// "extract": "er>\n <hgroup>\n ",
// "hiliteStart": 10,
// "hiliteLength": 8
// }
if (msg.type === 'error') {
sr.error(self, 'error', {
line: msg.lastLine,
column: msg.lastColumn,
message: msg.message,
link: `${service}?doc=${sr.url}`,
});
}
// {
// "type": "info",
// "url": "https://www.google.com/",
// "subType": "warning",
// "message": "blah"
// }
else if (msg.type === 'info') {
if (msg.subtype === 'warning') {
sr.warning(self, 'warning', {
message: msg.message,
link: `${service}?doc=${sr.url}`,
});
}
}
// {
// "type":"non-document-error",
// "subType":"io",
// "message":"HTTP resource not retrievable. The HTTP status from the remote server was: 404."
// }
else if (msg.type === 'non-document-error') {
sr.error(self, 'non-document-error', {
subType: msg.subType,
message: msg.message,
});
}
}
}
}
done();
});
}