Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(core/linter-rules): added linter for checking charset is utf-8 #2075

Merged
merged 26 commits into from
Feb 7, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/core/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Sets the core defaults
*/
export const name = "core/defaults";
import { rule as checkCharset } from "./linter-rules/check-charset";
import { rule as checkInternalSlots } from "./linter-rules/check-internal-slots";
import { rule as checkPunctuation } from "./linter-rules/check-punctuation";
import linter from "./linter";
Expand All @@ -14,7 +15,8 @@ linter.register(
noHeadinglessSectionsRule,
checkPunctuation,
localRefsExist,
checkInternalSlots
checkInternalSlots,
checkCharset
);

export const coreDefaults = {
Expand All @@ -24,6 +26,7 @@ export const coreDefaults = {
"check-punctuation": false,
"local-refs-exist": true,
"check-internal-slots": false,
"check-charset": false,
},
pluralize: false,
specStatus: "base",
Expand Down
52 changes: 52 additions & 0 deletions src/core/linter-rules/check-charset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Linter rule "check-charset".
*
* Checks whether the document has `<meta charset="utf-8">` properly.
*/
import LinterRule from "../LinterRule";
import { lang as defaultLang } from "../l10n";

const name = "check-charset";
const meta = {
en: {
description: `Document must only contain one \`<meta>\` tag with charset set to 'utf-8'`,
howToFix: `Add this line in your document \`<head>\` section - \`<meta charset="utf-8">\` or set charset to "utf-8" if not set already.`,
},
};

// Fall back to english, if language is missing
const lang = defaultLang in meta ? defaultLang : "en";

/**
* Runs linter rule.
*
* @param {Object} conf The ReSpec config.
* @param {Document} doc The document to be checked.
*/
function linterFunction(conf, doc) {
const metas = doc.querySelectorAll("meta[charset]");
const val = [];
for (const meta of metas) {
val.push(
meta
.getAttribute("charset")
.trim()
.toLowerCase()
);
}
const utfExists = val.includes("utf-8");

//only a single meta[charset] and is set to utf-8, correct case
if (utfExists && metas.length === 1) {
return [];
marcoscaceres marked this conversation as resolved.
Show resolved Hide resolved
}
//if more than one meta[charset] tag defined along with utf-8
//or
//no meta[charset] present in the document
return {
name,
occurrences: metas.length,
...meta[lang],
};
}
export const rule = new LinterRule(name, linterFunction);
72 changes: 72 additions & 0 deletions tests/spec/core/linter-rules/check-charset-spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
describe("Core Linter Rule - 'check-charset'", () => {
const ruleName = "check-charset";
const config = {
lint: { [ruleName]: true },
};
let rule;
beforeAll(async () => {
rule = await new Promise(resolve => {
require([`core/linter-rules/${ruleName}`], ({ rule }) => resolve(rule));
});
});

it("checks if meta[charset] is set to utf-8", async () => {
const doc = document.implementation.createHTMLDocument("test doc");
doc.head.innerHTML = `
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>ReSpec</title>
`;

const results = await rule.lint(config, doc);
expect(results.length).toBe(0);
});

it("doesn't give an error when written in capitals", async () => {
const doc = document.implementation.createHTMLDocument("test doc");
doc.head.innerHTML = `
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<title>ReSpec</title>
`;

const results = await rule.lint(config, doc);
expect(results.length).toBe(0);
});

it("checks if meta[charset] is present or not", async () => {
const doc = document.implementation.createHTMLDocument("test doc");
doc.head.innerHTML = `
<meta name="viewport" content="width=device-width">
<title>ReSpec</title>
`;

const results = await rule.lint(config, doc);
expect(results.occurrences).toBe(0);
});

it("returns error when more then one meta[charset] present", async () => {
const doc = document.implementation.createHTMLDocument("test doc");
doc.head.innerHTML = `
<meta charset="utf-8">
<meta charset="ascii-128">
<meta name="viewport" content="width=device-width">
<title>ReSpec</title>
`;

const results = await rule.lint(config, doc);
expect(results.occurrences).toBe(2);
});

it("return error when some other charset defined", async () => {
const doc = document.implementation.createHTMLDocument("test doc");
doc.head.innerHTML = `
<meta charset="ascii-128">
<meta name="viewport" content="width=device-width">
<title>ReSpec</title>
`;

const results = await rule.lint(config, doc);
expect(results.occurrences).toBe(1);
});
});
2 changes: 2 additions & 0 deletions tests/spec/w3c/defaults-spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ describe("W3C — Defaults", () => {
"local-refs-exist": true,
"check-punctuation": false,
"check-internal-slots": false,
"check-charset": false,
});
expect(rsConf.highlightVars).toEqual(true);
expect(rsConf.license).toEqual("w3c-software-doc");
Expand Down Expand Up @@ -51,6 +52,7 @@ describe("W3C — Defaults", () => {
"check-punctuation": false,
"fake-linter-rule": "foo",
"check-internal-slots": true,
"check-charset": false,
});
expect(rsConf.highlightVars).toEqual(false);
expect(rsConf.license).toEqual("c0");
Expand Down