Skip to content

Commit 12155fe

Browse files
Adds new rule htmlacademy/req-mailto
1 parent 04dd670 commit 12155fe

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

rules/req-mailto/index.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
'use strict';
2+
const { has_non_empty_attribute, is_tag_node, attribute_value } = require('@linthtml/dom-utils');
3+
4+
const emailRegex = /[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}/;
5+
const isNotEmptyLink = (node) => node.childNodes.length > 0;
6+
const isEmailContent = (node) => emailRegex.test(node.childNodes[0].data);
7+
const reportError = (node) => ({
8+
position: node.loc,
9+
message: 'The href attribute of the <a> tag must start with "mailto:" if it contains an email'
10+
});
11+
12+
module.exports = {
13+
name: 'htmlacademy/req-mailto',
14+
// eslint-disable-next-line camelcase
15+
lint(node, rule_config, { report }) {
16+
if (is_tag_node(node) && node.name === 'a') {
17+
if (isNotEmptyLink(node) && isEmailContent(node)) {
18+
if (has_non_empty_attribute(node, 'href')) {
19+
const hrefValue = attribute_value(node, 'href').chars;
20+
21+
if (!hrefValue.startsWith('mailto:')) {
22+
report(reportError(node));
23+
}
24+
} else {
25+
report(reportError(node));
26+
}
27+
}
28+
}
29+
}
30+
};

0 commit comments

Comments
 (0)