Skip to content

Commit 2c63ab0

Browse files
feat(attr-sort): sort unknown attributes alphabetically (#668)
* feat(attr-sort): sort unknown attributes alphabetically 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 * Fix failing test I used GitHub Copilot to help find the issue and fix... > The error occurs because the test is using .to.be() and .to.contain() methods, which are not part of the Jest assertion library. These methods are typically used in Chai, another assertion library. In Jest, the equivalent methods are .toBe() and .toContain(). --------- Co-authored-by: coliff <[email protected]>
1 parent 32d427f commit 2c63ab0

File tree

5 files changed

+17
-7
lines changed

5 files changed

+17
-7
lines changed

dist/core/rules/attr-sorted.js

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/htmlhint.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@
484484
const originalAttrs = JSON.stringify(listOfAttributes);
485485
listOfAttributes.sort((a, b) => {
486486
if (orderMap[a] == undefined && orderMap[b] == undefined) {
487-
return 0;
487+
return a.localeCompare(b);
488488
}
489489
if (orderMap[a] == undefined) {
490490
return 1;

dist/htmlhint.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/core/rules/attr-sorted.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ export default {
3434
const originalAttrs = JSON.stringify(listOfAttributes)
3535
listOfAttributes.sort((a, b) => {
3636
if (orderMap[a] == undefined && orderMap[b] == undefined) {
37-
return 0
37+
return a.localeCompare(b)
3838
}
3939
if (orderMap[a] == undefined) {
4040
return 1

test/rules/attr-sorted.spec.js

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,24 @@ describe(`Rules: ${ruleId}`, () => {
1616
expect(messages[0].message).toContain('["id","class","title"]')
1717
})
1818

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

2222
const messages = HTMLHint.verify(code, ruleOptions)
2323

2424
expect(messages.length).toBe(0)
2525
})
2626

27+
it('Attribute unsorted tags that are unrecognizable should throw error', () => {
28+
const code = '<div img="image" meta="meta" font="font"></div>'
29+
30+
const messages = HTMLHint.verify(code, ruleOptions)
31+
32+
expect(messages.length).toBe(1)
33+
expect(messages[0].rule.id).toBe(ruleId)
34+
expect(messages[0].message).toContain('["img","meta","font"]')
35+
})
36+
2737
it('Attribute unsorted of tags of various types should throw error', () => {
2838
const code = '<div type="type" img="image" id="id" font="font"></div>'
2939

0 commit comments

Comments
 (0)