fix: compare tag and attribute names case insensitively by arpitjain099 · Pull Request #1937 · htmlhint/HTMLHint · GitHub
Skip to content
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
4 changes: 2 additions & 2 deletions dist/core/rules/attr-no-duplication.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions dist/core/rules/src-not-empty.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion src/core/rules/attr-no-duplication.ts
12 changes: 8 additions & 4 deletions src/core/rules/src-not-empty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,23 @@ export default {
description: 'The src attribute of an img(script,link) must have a value.',
init(parser, reporter) {
parser.addListener('tagstart', (event) => {
const tagName = event.tagName
// HTML tag and attribute names are ASCII case insensitive, so compare
// against the lowercased form. The parser hands them over exactly as they
// were written.
const tagName = event.tagName.toLowerCase()
const attrs = event.attrs
let attr
const col = event.col + tagName.length + 1

for (let i = 0, l = attrs.length; i < l; i++) {
attr = attrs[i]
const attrName = attr.name.toLowerCase()

if (
((/^(img|script|embed|bgsound|iframe)$/.test(tagName) === true &&
attr.name === 'src') ||
(tagName === 'link' && attr.name === 'href') ||
(tagName === 'object' && attr.name === 'data')) &&
attrName === 'src') ||
(tagName === 'link' && attrName === 'href') ||
(tagName === 'object' && attrName === 'data')) &&
attr.value === ''
) {
reporter.error(
Expand Down
9 changes: 9 additions & 0 deletions test/rules/attr-no-duplication.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,15 @@ describe(`Rules: ${ruleId}`, () => {
expect(messages[0].col).toBe(12)
})

it('Attribute name duplicated in a different case should result in an error', () => {
// HTML attribute names are ASCII case insensitive, so href and HREF on the
// same element are the same attribute.
const code = '<a href="a" HREF="b">bbb</a>'
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).toBe(1)
expect(messages[0].rule.id).toBe(ruleId)
})

it('Attribute name not been duplication should not result in an error', () => {
const code = '<a href="a">bbb</a>'
const messages = HTMLHint.verify(code, ruleOptions)
Expand Down
14 changes: 14 additions & 0 deletions test/rules/src-not-empty.spec.js
Loading