File tree Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Expand file tree Collapse file tree 1 file changed +30
-0
lines changed Original file line number Diff line number Diff line change
1
+ 'use strict' ;
2
+ const { has_non_empty_attribute, is_tag_node, attribute_value } = require ( '@linthtml/dom-utils' ) ;
3
+
4
+ const emailRegex = / [ a - z A - Z 0 - 9 . _ % + - ] + @ [ a - z A - Z 0 - 9 . - ] + \. [ a - z A - 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
+ } ;
You can’t perform that action at this time.
0 commit comments