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

Commit

Permalink
Add warning for h2[name] elements
Browse files Browse the repository at this point in the history
  • Loading branch information
ddbeck committed Jul 17, 2020
1 parent c51275c commit 849d14f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 0 deletions.
1 change: 1 addition & 0 deletions scripts/scraper-ng/preset.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ module.exports = {
[require("./rules/html-require-recipe-ingredients")],
[require("./rules/html-require-ingredient-order")], // Must be after html-require-recipe-ingredients
[require("./rules/html-warn-unknown-headings")],
[require("./rules/html-h2-avoid-name-attribute")],
],
};
19 changes: 19 additions & 0 deletions scripts/scraper-ng/rules/html-h2-avoid-name-attribute.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
const { selectAll } = require("hast-util-select");

const source = "html-h2-avoid-name-attribute";
const id = "h2-name-attr";
const reason = (node) =>
`H2 has name attribute (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}`);
}
};
}

module.exports = htmlH2AvoidNameAttributePlugin;
24 changes: 24 additions & 0 deletions scripts/scraper-ng/test/html-h2-avoid-name-attribute.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const { process } = require("./framework/utils");

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

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

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

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

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

0 comments on commit 849d14f

Please sign in to comment.