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

Added custom rule to check if website has favicon #856

Merged
merged 1 commit into from
Mar 14, 2024
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
1 change: 1 addition & 0 deletions docker/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
41 changes: 41 additions & 0 deletions docker/customHtmlRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
};
6 changes: 6 additions & 0 deletions docker/rules.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
];

Expand Down
40 changes: 40 additions & 0 deletions docker/test/html-rules-testing/favicon-must-be-added.spec.js
Original file line number Diff line number Diff line change
@@ -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 =
`<head>
<title>Page Title</title>
<link rel="shortcut icon" href="/images/favicon.ico" type="image/x-icon" />
</head>`
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(0)
})

it('Page without favicon must be reported', () => {
const code =
`<head>
<title>Page Title</title>
<link rel="shortcut" href="/images/favicon.ico" type="image/x-icon" />
</head>`
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(1)
})

it('Page without favicon must be reported', () => {
const code =
`<head>
<title>Page Title</title>
<link href="/images/favicon.ico" type="image/x-icon" />
</head>`
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(1)
})
})
Loading