This repository has been archived by the owner on Sep 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Only flag mismatched name attributes
- Loading branch information
Showing
2 changed files
with
26 additions
and
6 deletions.
There are no files selected for viewing
17 changes: 14 additions & 3 deletions
17
...-ng/rules/html-h2-avoid-name-attribute.js → .../html-h2-avoid-name-attribute-mismatch.js
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 |
---|---|---|
@@ -1,19 +1,30 @@ | ||
const { selectAll } = require("hast-util-select"); | ||
const toText = require("hast-util-to-text"); | ||
|
||
const source = "html-h2-avoid-name-attribute"; | ||
const id = "h2-name-attr"; | ||
const id = "h2-name-attr-mismatch"; | ||
const reason = (node) => | ||
`H2 has name attribute (h2[name="${node.properties.name}"])`; | ||
`H2 has name attribute mismatch (h2[name="${node.properties.name}"])`; | ||
|
||
/** | ||
* Warn about H2s with name attributes, since this can lead to surprises. | ||
*/ | ||
function htmlH2AvoidNameAttributePlugin() { | ||
return function warnOnH2NameAttr(tree, file) { | ||
for (const node of selectAll("h2[name]", tree)) { | ||
file.message(reason(node), node, `${source}:${id}`); | ||
if (isMismatch(node)) { | ||
file.message(reason(node), node, `${source}:${id}`); | ||
} | ||
} | ||
}; | ||
} | ||
|
||
function isMismatch(node) { | ||
return toAttr(toText(node)) !== node.properties.name; | ||
} | ||
|
||
function toAttr(str) { | ||
return str.replace(/ /g, "_"); | ||
} | ||
|
||
module.exports = htmlH2AvoidNameAttributePlugin; |
15 changes: 12 additions & 3 deletions
15
...test/html-h2-avoid-name-attribute.test.js → ...-h2-avoid-name-attribute-mismatch.test.js
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