-
Notifications
You must be signed in to change notification settings - Fork 43
/
dl.js
386 lines (361 loc) · 14.1 KB
/
dl.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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/* eslint-disable */
// must have dl, with:
// dts containing "This Version", "Latest Version", "Previous Version" in that order
// this version is https://www.w3.org/TR/2013/WD-shortname-2013MMDD/
// latest version is https://www.w3.org/TR/shortname/
// #w3c-state date and this version date must match
// dt for editor or author
import superagent from 'superagent';
import doAsync from 'doasync';
const self = {
name: 'headers.dl',
section: 'front-matter',
rule: 'docIDFormat',
};
const thisError = {
name: 'headers.dl',
section: 'front-matter',
rule: 'docIDThisVersion',
};
const latestError = {
name: 'headers.dl',
section: 'front-matter',
rule: 'docIDLatestVersion',
};
const historyError = {
name: 'headers.dl',
section: 'front-matter',
rule: 'docIDHistory',
};
const editorError = {
name: 'headers.dl',
section: 'front-matter',
rule: 'editorSection',
};
export const name = self.name;
/**
* Check if link and href are consistent.
* @param {} Object place to find <a>, current error message title when there is error.
* @returns boolean whether element exists and can continue
*/
function checkLink({ sr, rule = self, element, linkName, mustHave = true }) {
if (!element || !element.href) {
if (mustHave) sr.error(rule, 'not-found', { linkName });
return false;
}
const text = sr.norm(element.textContent).trim();
const href = element.href.trim();
if (href !== text) sr.error(rule, 'link-diff', { text, href, linkName });
return true;
}
export async function check(sr, done) {
const { rescinds } = sr.config;
const { obsoletes } = sr.config;
const { supersedes } = sr.config;
const subType = sr.config.submissionType;
let topLevel = 'TR';
let thisURI = '';
let latestURI = '';
if (subType === 'member') topLevel = 'Submission';
else if (subType === 'team') topLevel = 'TeamSubmission';
const dts = sr.extractHeaders();
if (!dts.This) sr.error(self, 'this-version');
if (!dts.Latest) sr.error(self, 'latest-version');
if (!dts.History) sr.error(self, 'no-history');
if (rescinds && !dts.Rescinds) sr.error(self, 'rescinds');
if (!rescinds && dts.Rescinds) sr.warning(self, 'rescinds-not-needed');
if (obsoletes && !dts.Obsoletes) sr.error(self, 'obsoletes');
if (!obsoletes && dts.Obsoletes) sr.warning(self, 'obsoletes-not-needed');
if (supersedes && !dts.Supersedes) sr.error(self, 'supersedes');
if (!supersedes && dts.Supersedes)
sr.warning(self, 'supersedes-not-needed');
if (dts.This && dts.Latest && dts.This.pos > dts.Latest.pos)
sr.error(self, 'this-latest-order');
// TODO: What's the order for History?
if (dts.Latest && dts.Rescinds && dts.Latest.pos > dts.Rescinds.pos)
sr.error(self, 'latest-rescinds-order');
if (dts.Latest && dts.Obsoletes && dts.Latest.pos > dts.Obsoletes.pos)
sr.error(self, 'latest-obsoletes-order');
if (dts.Latest && dts.Supersedes && dts.Latest.pos > dts.Supersedes.pos)
sr.error(self, 'latest-supersedes-order');
let shortname;
let seriesShortname;
let matches;
if (dts.This) {
const linkThis = dts.This.dd.querySelector('a');
const exist = checkLink({
sr,
rule: self,
element: linkThis,
linkName: 'This version',
});
if (exist) {
const vThisRex = `^https:\\/\\/www\\.w3\\.org\\/${topLevel}\\/(\\d{4})\\/${
sr.config.status || '[A-Z]+'
}-(.+)-(\\d{4})(\\d\\d)(\\d\\d)\\/?$`;
matches = (linkThis.getAttribute('href') || '')
.trim()
.match(new RegExp(vThisRex));
const docDate = sr.getDocumentDate();
if (matches) {
[thisURI] = matches;
const year = matches[1] * 1;
const year2 = matches[3] * 1;
const month = matches[4] * 1;
const day = matches[5] * 1;
[, , shortname] = matches;
const pattern = /-?\d[\d.]*$/;
const dashPattern = /\d[\d.]*-/;
seriesShortname = shortname
.replace(pattern, '')
.replace(dashPattern, '-');
if (docDate) {
if (
year !== docDate.getFullYear() ||
year2 !== docDate.getFullYear() ||
month - 1 !== docDate.getMonth() ||
day !== docDate.getDate()
)
sr.error(self, 'this-date');
} else sr.warning(self, 'no-date');
// Require new /TR shortnames to use lowercase.
const shortnameChange =
dts.History &&
dts.History.dd.querySelector('a').dataset.previousShortname;
const needLowercase = shortnameChange || (await sr.isFP());
if (needLowercase && shortname.toLowerCase() !== shortname)
sr.error(thisError, 'shortname-lowercase', { shortname });
} else sr.error(thisError, 'this-syntax');
}
}
let sn;
if (dts.Latest) {
const linkLate = dts.Latest.dd.querySelector('a');
const exist = checkLink({
sr,
rule: self,
element: linkLate,
linkName: 'Latest published version',
});
if (exist) {
const lateRex = `^https:\\/\\/www\\.w3\\.org\\/${topLevel}\\/(.+?)\\/?$`;
matches = (linkLate.getAttribute('href') || '')
.trim()
.match(new RegExp(lateRex));
if (matches) {
sn = matches[1];
// latest version link can be the shortlink or the series shortlink
if (sn === shortname) {
latestURI = linkLate.textContent;
} else if (sn === seriesShortname) {
latestURI = `https://www.w3.org/TR/${shortname}/`;
}
if (sn !== shortname && sn !== seriesShortname)
sr.error(self, 'this-latest-shortname', {
thisShortname: shortname,
latestShortname: sn,
});
} else sr.error(latestError, 'latest-syntax');
}
}
if (dts.History) {
const linkHistory = dts.History.dd.querySelector('a');
const exist = checkLink({
sr,
rule: historyError,
element: linkHistory,
linkName: 'History',
});
if (exist) {
// e.g. https://www.w3.org/standards/history/hr-time-10086
const historyReg =
/^https:\/\/www\.w3\.org\/standards\/history\/(.+)$/;
matches = (linkHistory.getAttribute('href') || '')
.trim()
.match(historyReg);
if (matches) {
const [, historyShortname] = matches;
if (historyShortname !== shortname) {
sr.error(historyError, 'history-syntax', { shortname });
} else {
const historyHref = linkHistory.getAttribute('href');
// Check if the history link exist
let historyStatusCode;
try {
const res = await doAsync(superagent).head(historyHref);
historyStatusCode = res.statusCode;
} catch (err) {
historyStatusCode = err.status;
}
var hasPreviousVersion = !(await sr.isFP());
if (hasPreviousVersion && historyStatusCode === 404) {
// it's a none FP spec, but the history page doesn't exist. There should be a 'valid' previous-shortname.
const previousShortname = linkHistory.getAttribute(
'data-previous-shortname'
);
if (previousShortname) {
// prettier-ignore
sr.warning(historyError, 'this-previous-shortname',
{
previousShortname,
thisShortname: shortname,
}
);
// check previous shortname is valid.
const previousHistoryHref = `https://www.w3.org/standards/history/${previousShortname}`;
let previousHistoryStatusCode;
try {
const res = await doAsync(superagent).head(
previousHistoryHref
);
previousHistoryStatusCode = res.statusCode;
} catch (err) {
previousHistoryStatusCode = err.status;
}
if (previousHistoryStatusCode === 404) {
sr.error(historyError, 'history-bad-previous', {
previousShortname,
url: previousHistoryHref,
});
}
}
}
}
}
}
}
if (dts.Rescinds) {
const linkRescinds = dts.Rescinds.dd.querySelector('a');
const exist = checkLink({
sr,
rule: self,
element: linkRescinds,
linkName: 'Rescinds this Recommendation',
});
if (exist) {
matches = (linkRescinds.getAttribute('href') || '')
.trim()
.match(
/^https:\/\/www\.w3\.org\/TR\/\d{4}\/REC-(.+)-\d{8}\/?$/
);
if (matches) {
sn = matches[1];
if (sn !== shortname)
sr.error(self, 'this-rescinds-shortname', {
rescindsShortname: sn,
thisShortname: shortname,
});
} else sr.error(self, 'rescinds-syntax');
}
}
if (dts.Obsoletes) {
const linkObsl = dts.Obsoletes.dd.querySelector('a');
const exist = checkLink({
sr,
rule: self,
element: linkObsl,
linkName: 'Obsoletes this Recommendation',
});
if (exist) {
matches = (linkObsl.getAttribute('href') || '')
.trim()
.match(
/^https:\/\/www\.w3\.org\/TR\/\d{4}\/REC-(.+)-\d{8}\/?$/
);
if (matches) {
sn = matches[1];
if (sn !== shortname)
sr.error(self, 'this-obsoletes-shortname', {
obsoletesShortname: sn,
thisShortname: shortname,
});
} else sr.error(self, 'obsoletes-syntax');
}
}
if (dts.Supersedes) {
const linkSpsd = dts.Supersedes.dd.querySelector('a');
const exist = checkLink({
sr,
rule: self,
element: linkSpsd,
linkName: 'Supersedes this Recommendation',
});
if (exist) {
matches = (linkSpsd.getAttribute('href') || '')
.trim()
.match(
/^https:\/\/www\.w3\.org\/TR\/\d{4}\/REC-(.+)-\d{8}\/?$/
);
if (matches) {
sn = matches[1];
if (sn !== shortname)
sr.error(self, 'this-supersedes-shortname');
} else sr.error(self, 'supersedes-syntax');
}
}
// check "Implementation report" link. Unless in Sotd saying there's none.
const needImplementation =
['CR', 'CRD', 'PR', 'REC'].indexOf(sr.config.status) !== -1;
const sotd = sr.getSotDSection();
const noImplementation =
sr
.norm(sotd && sotd.textContent)
.indexOf('There is no preliminary implementation report.') > -1;
const linkImplementation =
dts.Implementation && dts.Implementation.dd.querySelector('a');
const implementationExist = checkLink({
sr,
rule: self,
element: linkImplementation,
linkName: 'Implementation report',
mustHave: noImplementation ? false : needImplementation,
});
if (
implementationExist &&
!linkImplementation.href.trim().toLowerCase().startsWith('https://')
) {
sr.error(self, 'implelink-should-be-https', {
link: linkImplementation,
});
}
if (noImplementation && needImplementation) {
sr.warning(self, 'implelink-confirm-no');
}
// check "Editor's draft" link
if (dts.EditorDraft) {
const editorsDraftElement = dts.EditorDraft.dd.querySelector('a');
const exist = checkLink({
sr,
rule: self,
element: editorsDraftElement,
linkName: 'Implementation report',
});
if (exist) {
const editorsDraft = editorsDraftElement.href;
if (!editorsDraft.trim().toLowerCase().startsWith('https://'))
sr.error(self, 'editors-draft-should-be-https', {
link: editorsDraft,
});
}
}
if (dts.Editor && dts.Editor.dd && dts.Editor.dd.length) {
const editors = dts.Editor.dd;
// 'missingElement' is array of HTML elements without editorId
const missingElement = editors.filter(
editor =>
!editor.dataset.editorId &&
!editor.textContent.match(/(working|interest) group/i)
);
if (missingElement.length) {
sr.error(editorError, 'editor-missing-id', {
names: missingElement
.map(editor => editor.textContent)
.join(', '),
});
}
} else {
// should at least have 1 editor
sr.error(editorError, 'editor-not-found');
}
done();
}