From 2a10fb5220980fa6351212bd982d22470f47c8d8 Mon Sep 17 00:00:00 2001 From: Valery Baleyko Date: Fri, 11 Jun 2021 13:37:27 +0300 Subject: [PATCH 1/2] fix(rules): html-lang-require rule should be applied only for HTML tag --- src/core/rules/html-lang-require.ts | 40 +++++++++++++++------------- test/rules/html-lang-require.spec.js | 5 ++++ 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/core/rules/html-lang-require.ts b/src/core/rules/html-lang-require.ts index 9324a270a..8e6ad43aa 100644 --- a/src/core/rules/html-lang-require.ts +++ b/src/core/rules/html-lang-require.ts @@ -34,32 +34,34 @@ export default { const mapAttrs = parser.getMapAttrs(event.attrs) const col = event.col + tagName.length + 1 - 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 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 element must be a valid BCP47.', + event.line, + col, + this, + event.raw + ) + } + } else { reporter.warn( - 'The lang attribute of 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 element must be a valid BCP47.', + 'An lang attribute must be present on elements.', event.line, col, this, event.raw ) } - } else { - reporter.warn( - 'An lang attribute must be present on elements.', - event.line, - col, - this, - event.raw - ) } }) }, diff --git a/test/rules/html-lang-require.spec.js b/test/rules/html-lang-require.spec.js index b64928bb2..7f313560d 100644 --- a/test/rules/html-lang-require.spec.js +++ b/test/rules/html-lang-require.spec.js @@ -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 = '

' + 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 = '' const messages = HTMLHint.verify(code, ruleOptions) From 403b817965c246e1f65f597d0e908eeec83eb513 Mon Sep 17 00:00:00 2001 From: Valery Baleyko Date: Fri, 11 Jun 2021 13:42:11 +0300 Subject: [PATCH 2/2] 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 --- src/core/rules/html-lang-require.ts | 4 ++-- test/rules/html-lang-require.spec.js | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/src/core/rules/html-lang-require.ts b/src/core/rules/html-lang-require.ts index 8e6ad43aa..dfb20a923 100644 --- a/src/core/rules/html-lang-require.ts +++ b/src/core/rules/html-lang-require.ts @@ -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', @@ -33,6 +32,7 @@ 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') { if ('lang' in mapAttrs) { @@ -44,7 +44,7 @@ export default { this, event.raw ) - } else if (!LANG_VALIDITY_PATTERN.test(mapAttrs['lang'])) { + } else if (!langValidityPattern.test(mapAttrs['lang'])) { reporter.warn( 'The lang attribute value of element must be a valid BCP47.', event.line, diff --git a/test/rules/html-lang-require.spec.js b/test/rules/html-lang-require.spec.js index 7f313560d..3123d0aab 100644 --- a/test/rules/html-lang-require.spec.js +++ b/test/rules/html-lang-require.spec.js @@ -28,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 = '' 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 = '' + const messages = HTMLHint.verify(code, ruleOptions) + expect(messages.length).to.be(0) + }) })