-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathplugin.html-validate.link.js
More file actions
106 lines (93 loc) · 3.05 KB
/
plugin.html-validate.link.js
File metadata and controls
106 lines (93 loc) · 3.05 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
const { Rule } = require('html-validate');
const { nodeIgnore } = require('./plugin.html-validate.utils');
const ENV = require('./app.env');
/**
* @typedef { import('html-validate').DOMReadyEvent } DOMReadyEvent
*/
/**
* Lint empty `a[href]` attribute.
*/
class LinkNoEmpty extends Rule {
/**
* @param {object} options - plugin options
*/
constructor(options) {
super({ ignore: '.wysiwyg a', ...options });
this.domReady = this.domReady.bind(this);
}
/**
* Setup plugin events.
*/
setup() {
this.on('dom:ready', this.domReady);
}
/**
* Lint `a[href]` nodes.
* @param {DOMReadyEvent.document} document - document object
*/
domReady({ document }) {
const links = document.querySelectorAll('a');
const ignores = this.options.ignore ? document.querySelectorAll(this.options.ignore) : [];
links.forEach((item) => {
if (nodeIgnore(item, ignores)) {
return;
}
const href = String(item.getAttributeValue('href') || '');
if (href === '') {
this.report(item, '<a> required `href` attribute.');
} else if (href === '#') {
this.report(item, '<a> required `href` attribute.');
} else if (href.startsWith('#')) {
const target = document.querySelector(href);
if (!target) {
this.report(item, `<a> not found target node (\`href=${JSON.stringify(href)}\`).`);
}
}
});
}
}
/**
* Lint trailing `a[href]` attribute.
*/
class LinkTrailingSlash extends Rule {
sitemapUrls = [];
/**
* @param {object} options - plugin options
*/
constructor(options) {
super({ ignore: '.wysiwyg a', ...options });
this.domReady = this.domReady.bind(this);
}
/**
* Setup plugin events.
*/
setup() {
this.on('dom:ready', this.domReady);
this.sitemapUrls = ENV.SITEMAP.map((i) => i.PAGE.URL);
}
/**
* Lint `a[href]` nodes.
* @param {DOMReadyEvent.document} document - document object
*/
domReady({ document }) {
const links = document.querySelectorAll('a');
const ignores = this.options.ignore ? document.querySelectorAll(this.options.ignore) : [];
links.forEach((item) => {
if (nodeIgnore(item, ignores)) {
return;
}
const href = String(item.getAttributeValue('href') || '');
if (href.startsWith('/') && !href.startsWith('//')) {
const url = new URL(href, 'https://localhost/');
if (!url.pathname.endsWith('/') && this.sitemapUrls.includes(`${url.pathname}/`)) {
this.report(item, `<a> trailing slash required (\`href=${JSON.stringify(href)}\`).`);
}
}
});
}
}
module.exports = { LinkNoEmpty, LinkTrailingSlash };
module.exports.rules = {
'pitcher/link-no-empty': LinkNoEmpty,
'pitcher/link-trailing-slash': LinkTrailingSlash,
};