Skip to content

Commit 068645e

Browse files
authoredJun 11, 2021
fix: Unexpected behavior of the html lang require rule (#655)
* fix(rules): html-lang-require rule should be applied only for HTML tag * fix(rules): html-lang-require rule show false result in valid cases because of reusing of the RegExp object which keeps the state after previous check
1 parent 891a6fb commit 068645e

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed
 

‎src/core/rules/html-lang-require.ts

+22-20
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ const langtag =
2222
`(-${privateUse})?` +
2323
')'
2424
const languageTag = `(${grandfathered}|${langtag}|${privateUse2})`
25-
const LANG_VALIDITY_PATTERN = new RegExp(languageTag, 'g')
2625

2726
export default {
2827
id: 'html-lang-require',
@@ -33,33 +32,36 @@ export default {
3332
const tagName = event.tagName.toLowerCase()
3433
const mapAttrs = parser.getMapAttrs(event.attrs)
3534
const col = event.col + tagName.length + 1
35+
const langValidityPattern = new RegExp(languageTag, 'g')
3636

37-
if (tagName === 'html' && 'lang' in mapAttrs) {
38-
if (!mapAttrs['lang']) {
37+
if (tagName === 'html') {
38+
if ('lang' in mapAttrs) {
39+
if (!mapAttrs['lang']) {
40+
reporter.warn(
41+
'The lang attribute of <html> element must have a value.',
42+
event.line,
43+
col,
44+
this,
45+
event.raw
46+
)
47+
} else if (!langValidityPattern.test(mapAttrs['lang'])) {
48+
reporter.warn(
49+
'The lang attribute value of <html> element must be a valid BCP47.',
50+
event.line,
51+
col,
52+
this,
53+
event.raw
54+
)
55+
}
56+
} else {
3957
reporter.warn(
40-
'The lang attribute of <html> element must have a value.',
41-
event.line,
42-
col,
43-
this,
44-
event.raw
45-
)
46-
} else if (!LANG_VALIDITY_PATTERN.test(mapAttrs['lang'])) {
47-
reporter.warn(
48-
'The lang attribute value of <html> element must be a valid BCP47.',
58+
'An lang attribute must be present on <html> elements.',
4959
event.line,
5060
col,
5161
this,
5262
event.raw
5363
)
5464
}
55-
} else {
56-
reporter.warn(
57-
'An lang attribute must be present on <html> elements.',
58-
event.line,
59-
col,
60-
this,
61-
event.raw
62-
)
6365
}
6466
})
6567
},

‎test/rules/html-lang-require.spec.js

+11-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,11 @@ const ruleOptions = {}
88
ruleOptions[ruldId] = true
99

1010
describe(`Rules: ${ruldId}`, () => {
11+
it('All the rest(non HTML) tags should not result in an error', () => {
12+
const code = '<html lang="en-EN"><body><p></p></body></html>'
13+
const messages = HTMLHint.verify(code, ruleOptions)
14+
expect(messages.length).to.be(0)
15+
})
1116
it('HTML tag have no a lang attribute should result in an error', () => {
1217
const code = '<html></html>'
1318
const messages = HTMLHint.verify(code, ruleOptions)
@@ -23,9 +28,14 @@ describe(`Rules: ${ruldId}`, () => {
2328
const messages = HTMLHint.verify(code, ruleOptions)
2429
expect(messages.length).to.be(1)
2530
})
26-
it('HTML tag have an non emtpy and valid lang attribute should not result in an error', () => {
31+
it('HTML tag have an non emtpy and valid(en-EN) lang attribute should not result in an error', () => {
2732
const code = '<html lang="en-EN"></html>'
2833
const messages = HTMLHint.verify(code, ruleOptions)
2934
expect(messages.length).to.be(0)
3035
})
36+
it('HTML tag have an non emtpy and valid(en) lang attribute should not result in an error', () => {
37+
const code = '<html lang="en"></html>'
38+
const messages = HTMLHint.verify(code, ruleOptions)
39+
expect(messages.length).to.be(0)
40+
})
3141
})

0 commit comments

Comments
 (0)