Thanks to visit codestin.com
Credit goes to github.com

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
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)
})
})