diff --git a/docker/api.js b/docker/api.js index f8639bdb..d9c0077c 100644 --- a/docker/api.js +++ b/docker/api.js @@ -183,4 +183,5 @@ exports.htmlHintConfig = { "common-spelling-mistakes": true, "phone-numbers-without-links": true, "auth-terms-spelling-mistakes": true, + "favicon-must-be-added": true }; diff --git a/docker/customHtmlRules.js b/docker/customHtmlRules.js index dc7fdf39..88c41652 100644 --- a/docker/customHtmlRules.js +++ b/docker/customHtmlRules.js @@ -614,5 +614,46 @@ exports.addCustomHtmlRule = async (apiToken, url) => { }); }, }); + + HTMLHint.addRule({ + id: "favicon-must-be-added", + description: + "Detect missing favicon in your website.", + init: function (parser, reporter) { + var self = this; + + let headBegin = false; + + parser.addListener("tagstart", (event) => { + let tagName = event.tagName.toLowerCase(), + mapAttrs = parser.getMapAttrs(event.attrs); + if (tagName === 'head') { + headBegin = true + } + + if (headBegin && tagName === 'link') { + if (mapAttrs["rel"]) { + if (!mapAttrs["rel"].includes('icon')) { + reporter.warn( + "Page must include favicon.", + event.line, + event.col, + self, + event.raw + ); + } + } else { + reporter.warn( + "Page must include favicon.", + event.line, + event.col, + self, + event.raw + ); + } + } + }); + }, + }); // Add new custom rule below }; \ No newline at end of file diff --git a/docker/rules.js b/docker/rules.js index 30694859..7c83392c 100644 --- a/docker/rules.js +++ b/docker/rules.js @@ -273,6 +273,12 @@ const customHtmlHintRules = [ ruleLink: 'https://rules.sonarsource.com/html/RSPEC-1094', type: RuleType.Warning, }, + { + rule: 'favicon-must-be-added', + displayName: 'Header - Must include favicon', + ruleLink: 'https://www.ssw.com.au/rules/favicon/', + type: RuleType.Warning, + }, // Add new rule id below ]; diff --git a/docker/test/html-rules-testing/favicon-must-be-added.spec.js b/docker/test/html-rules-testing/favicon-must-be-added.spec.js new file mode 100644 index 00000000..55b92e57 --- /dev/null +++ b/docker/test/html-rules-testing/favicon-must-be-added.spec.js @@ -0,0 +1,40 @@ +const expect = require('expect.js') +const HTMLHint = require("htmlhint").default; + +const ruldId = 'favicon-must-be-added' + +const ruleOptions = {} + +ruleOptions[ruldId] = true + +describe(`Rules: ${ruldId}`, () => { + it('Page that includes favicon must not be reported', () => { + const code = + ` + Page Title + + ` + const messages = HTMLHint.verify(code, ruleOptions) + expect(messages.length).to.be(0) + }) + + it('Page without favicon must be reported', () => { + const code = + ` + Page Title + + ` + const messages = HTMLHint.verify(code, ruleOptions) + expect(messages.length).to.be(1) + }) + + it('Page without favicon must be reported', () => { + const code = + ` + Page Title + + ` + const messages = HTMLHint.verify(code, ruleOptions) + expect(messages.length).to.be(1) + }) +})