Skip to content

Commit

Permalink
feat(attr-sort): sort unknown attributes alphabetically
Browse files Browse the repository at this point in the history
Previously unknown attributes were just ignored. Now they get sorted alphabetically.

BREAKING CHANGE: HTMLHint will now throw an error if unkown attributes are not sorted.

fix #661
  • Loading branch information
chrisguttandin committed Aug 16, 2021
1 parent fbd10ac commit 68423d7
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/core/rules/attr-sorted.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export default {
const originalAttrs = JSON.stringify(listOfAttributes)
listOfAttributes.sort((a, b) => {
if (orderMap[a] == undefined && orderMap[b] == undefined) {
return 0
return a.localeCompare(b)
}
if (orderMap[a] == undefined) {
return 1
Expand Down
14 changes: 12 additions & 2 deletions test/rules/attr-sort.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@ describe(`Rules: ${ruleId}`, () => {
expect(messages[0].message).to.contain('["id","class","title"]')
})

it('Attribute unsorted tags that are unrecognizable should not throw error', () => {
const code = '<div img="image" meta="meta" font="font"></div>'
it('Attribute sorted tags that are unrecognizable should not throw error', () => {
const code = '<div font="font" img="image" meta="meta"></div>'

const messages = HTMLHint.verify(code, ruleOptions)

expect(messages.length).to.be(0)
})

it('Attribute unsorted tags that are unrecognizable should throw error', () => {
const code = '<div img="image" meta="meta" font="font"></div>'

const messages = HTMLHint.verify(code, ruleOptions)

expect(messages.length).to.be(1)
expect(messages[0].rule.id).to.be(ruleId)
expect(messages[0].message).to.contain('["img","meta","font"]')
})

it('Attribute unsorted of tags of various types should throw error', () => {
const code = '<div type="type" img="image" id="id" font="font"></div>'

Expand Down

0 comments on commit 68423d7

Please sign in to comment.