-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add new lint rule “no-entities-in-attributes”
- Loading branch information
Justin Kimbrell
committed
Dec 21, 2023
1 parent
536ee2e
commit b060474
Showing
8 changed files
with
120 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
<div style="font-family: | ||
"Franklin Gothic Medium", | ||
"Arial Narrow", | ||
Arial, sans-serif;"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
module.exports = [ | ||
{ | ||
type: 'error', | ||
col: 1, | ||
evidence: ""Franklin Gothic Medium",", | ||
line: 2, | ||
message: "Invalid entity [ " ] encapsulated in the [ style ] attribute on line 2.", | ||
raw: """, | ||
rule: { | ||
description: "No HTML entities within tag attributes.", | ||
id: "no-enties-in-attributes", | ||
link: "https://thecapsule.email/docs/codes/no-enties-in-attributes", | ||
} | ||
}, | ||
{ | ||
type: 'error', | ||
col: 29, | ||
evidence: ""Franklin Gothic Medium",", | ||
line: 2, | ||
message: "Invalid entity [ " ] encapsulated in the [ style ] attribute on line 2.", | ||
raw: """, | ||
rule: { | ||
description: "No HTML entities within tag attributes.", | ||
id: "no-enties-in-attributes", | ||
link: "https://thecapsule.email/docs/codes/no-enties-in-attributes", | ||
} | ||
}, | ||
{ | ||
type: 'error', | ||
col: 1, | ||
evidence: ""Arial Narrow",", | ||
line: 3, | ||
message: "Invalid entity [ " ] encapsulated in the [ style ] attribute on line 3.", | ||
raw: """, | ||
rule: { | ||
description: "No HTML entities within tag attributes.", | ||
id: "no-enties-in-attributes", | ||
link: "https://thecapsule.email/docs/codes/no-enties-in-attributes", | ||
} | ||
}, | ||
{ | ||
type: 'error', | ||
col: 19, | ||
evidence: ""Arial Narrow",", | ||
line: 3, | ||
message: "Invalid entity [ " ] encapsulated in the [ style ] attribute on line 3.", | ||
raw: """, | ||
rule: { | ||
description: "No HTML entities within tag attributes.", | ||
id: "no-enties-in-attributes", | ||
link: "https://thecapsule.email/docs/codes/no-enties-in-attributes", | ||
} | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Rule } from 'htmlhint/types'; | ||
|
||
const rule: Rule = { | ||
id: 'no-enties-in-attributes', | ||
description: 'No HTML entities within tag attributes.', | ||
init(parser, reporter) { | ||
parser.addListener('tagstart', event => { | ||
event.attrs.forEach(({ value, name }) => { | ||
value.split(/[\n\r\v]/gi).reduce(({ line, col }, str) => { | ||
const matches = str.match( | ||
/&([a-z0-9]+|#[0-9]{1,6}|#x[0-9a-fA-F]{1,6});/ig | ||
); | ||
|
||
if(!matches) { | ||
return { | ||
line: line + 1, | ||
col: 0 | ||
}; | ||
} | ||
|
||
let offset = 0; | ||
|
||
for(const match of matches) { | ||
const index = str.indexOf(match, offset); | ||
|
||
reporter.error( | ||
`Invalid entity [ ${match} ] encapsulated in the [ ${name} ] attribute on line ${line}.`, | ||
line, | ||
col + index + 1, | ||
this, | ||
match | ||
); | ||
|
||
offset = str.indexOf(match, offset) + match.length; | ||
} | ||
|
||
return { | ||
line: line + 1, | ||
col: 0 | ||
}; | ||
}, { | ||
line: event.line, | ||
col: event.raw.indexOf(value) | ||
}); | ||
}); | ||
}); | ||
} | ||
} | ||
|
||
export default rule; |