-
-
Notifications
You must be signed in to change notification settings - Fork 393
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added linter for checking charset is utf-8
- Loading branch information
Showing
4 changed files
with
136 additions
and
2 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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 |
---|---|---|
@@ -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); |
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
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 |
---|---|---|
@@ -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); | ||
}); | ||
|
||
}) |