Skip to content

Commit

Permalink
added linter for checking charset is utf-8
Browse files Browse the repository at this point in the history
  • Loading branch information
CodHeK committed Feb 6, 2019
1 parent 68fef6e commit 9ee308a
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 2 deletions.
2 changes: 1 addition & 1 deletion builds/respec-w3c-common.js.map

Large diffs are not rendered by default.

54 changes: 54 additions & 0 deletions src/core/linter-rules/check-charset.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/**
* 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";
const getCharset = (element) => {
return !(/charset=\"(utf-8|UTF-8)\"/.test(element));
}

/**
* Runs linter rule.
*
* @param {Object} config The ReSpec config.
* @param {Document} doc The document to be checked.
*/
function linterFunction(conf, doc) {
let metas = doc.querySelectorAll('meta[charset]');
let val = [];
for(let i = 0; i < metas.length; i++) {
val.push(metas[i].outerHTML);
}
const offendingElements = val.filter(getCharset);

//only a single meta[charset] and is set to utf-8, correct case
if(!offendingElements.length && metas.length > 0) {
return [];
}
//if more than one meta[charset] tag defined along with utf-8
//or
//no meta[charset] present in the document
else if(offendingElements.length <= metas.length) {
return {
name,
offendingElements,
occurrences: offendingElements.length,
...meta[lang]
}
}
}
export const rule = new LinterRule(name, linterFunction);
5 changes: 4 additions & 1 deletion src/w3c/defaults.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,16 @@ import { rule as localRefsExist } from "../core/linter-rules/local-refs-exist";
import { rule as noHeadinglessSectionsRule } from "../core/linter-rules/no-headingless-sections";
import { rule as noHttpPropsRule } from "../core/linter-rules/no-http-props";
import { rule as privsecSectionRule } from "./linter-rules/privsec-section";
import { rule as checkCharset } from './linter-rules/check-charset';

linter.register(
noHttpPropsRule,
privsecSectionRule,
noHeadinglessSectionsRule,
checkPunctuation,
localRefsExist,
checkInternalSlots
checkInternalSlots,
checkCharset
);

const w3cDefaults = {
Expand All @@ -28,6 +30,7 @@ const w3cDefaults = {
"check-punctuation": false,
"local-refs-exist": true,
"check-internal-slots": false,
"check-charset": true
},
pluralize: true,
highlightVars: true,
Expand Down
77 changes: 77 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,77 @@
describe("Core Linter Rule - 'check-charset'", () => {
const ruleName = "check-charset";
const config = {
lint: { [ruleName]: true },
};
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);
const [result] = results;
expect(result.length).toEqual(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);
const [result] = results;
expect(result.length).toEqual(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);
const [result] = results;
expect(result.offendingElements.length).toEqual(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);
const [result] = results;
expect(result.offendingElements.length).toEqual(1);
});

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);
const [result] = results;
expect(result.offendingElements.length).toEqual(1);
});

})

0 comments on commit 9ee308a

Please sign in to comment.