Skip to content
This repository has been archived by the owner on Sep 7, 2021. It is now read-only.

Commit

Permalink
Only flag mismatched name attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
ddbeck committed Jul 22, 2020
1 parent b0654f2 commit df0874d
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
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;
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
const { process } = require("./framework/utils");

describe("html-h2-avoid-name-attribute", () => {
describe("html-h2-name-attribute-mismatch", () => {
const recipe = { body: ["data.browser_compatibility"] };
const messageId = /h2-name-attr/;
const messageId = /h2-name-attr-mismatch/;

test("warning-on-h2-name-attr", () => {
const file = process(
`<h2 id="Browser_compatability" name="Browser_compatability">Browser compatability</h2>`,
`<h2 id="Browser_compatability" name="Browser_Compatability">Browser compatability</h2>`,
recipe
);

expect(file).hasMessageWithId(messageId);
});

test("no-warning-without-h2-attr-mismatch", () => {
const file = process(
`<h2 id="Browser_compatability" name="Browser_compatability">Browser compatability</h2>`,
recipe
);

expect(file).not.hasMessageWithId(messageId);
});

test("no-warning-without-h2-attr", () => {
const file = process(
`<h2 id="Browser_compatability">Browser compatability</h2>`,
Expand Down

0 comments on commit df0874d

Please sign in to comment.