Skip to content

Commit

Permalink
fix: Unexpected behavior of the html lang require rule (#655)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
baleyko authored Jun 11, 2021
1 parent 891a6fb commit 068645e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 21 deletions.
42 changes: 22 additions & 20 deletions src/core/rules/html-lang-require.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ const langtag =
`(-${privateUse})?` +
')'
const languageTag = `(${grandfathered}|${langtag}|${privateUse2})`
const LANG_VALIDITY_PATTERN = new RegExp(languageTag, 'g')

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

if (tagName === 'html' && 'lang' in mapAttrs) {
if (!mapAttrs['lang']) {
if (tagName === 'html') {
if ('lang' in mapAttrs) {
if (!mapAttrs['lang']) {
reporter.warn(
'The lang attribute of <html> element must have a value.',
event.line,
col,
this,
event.raw
)
} else if (!langValidityPattern.test(mapAttrs['lang'])) {
reporter.warn(
'The lang attribute value of <html> element must be a valid BCP47.',
event.line,
col,
this,
event.raw
)
}
} else {
reporter.warn(
'The lang attribute of <html> element must have a value.',
event.line,
col,
this,
event.raw
)
} else if (!LANG_VALIDITY_PATTERN.test(mapAttrs['lang'])) {
reporter.warn(
'The lang attribute value of <html> element must be a valid BCP47.',
'An lang attribute must be present on <html> elements.',
event.line,
col,
this,
event.raw
)
}
} else {
reporter.warn(
'An lang attribute must be present on <html> elements.',
event.line,
col,
this,
event.raw
)
}
})
},
Expand Down
12 changes: 11 additions & 1 deletion test/rules/html-lang-require.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ const ruleOptions = {}
ruleOptions[ruldId] = true

describe(`Rules: ${ruldId}`, () => {
it('All the rest(non HTML) tags should not result in an error', () => {
const code = '<html lang="en-EN"><body><p></p></body></html>'
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(0)
})
it('HTML tag have no a lang attribute should result in an error', () => {
const code = '<html></html>'
const messages = HTMLHint.verify(code, ruleOptions)
Expand All @@ -23,9 +28,14 @@ describe(`Rules: ${ruldId}`, () => {
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(1)
})
it('HTML tag have an non emtpy and valid lang attribute should not result in an error', () => {
it('HTML tag have an non emtpy and valid(en-EN) lang attribute should not result in an error', () => {
const code = '<html lang="en-EN"></html>'
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(0)
})
it('HTML tag have an non emtpy and valid(en) lang attribute should not result in an error', () => {
const code = '<html lang="en"></html>'
const messages = HTMLHint.verify(code, ruleOptions)
expect(messages.length).to.be(0)
})
})

0 comments on commit 068645e

Please sign in to comment.