diff --git a/lib/checks/language/has-lang-evaluate.js b/lib/checks/language/has-lang-evaluate.js index 84dd357d6c..a06e0a8fd7 100644 --- a/lib/checks/language/has-lang-evaluate.js +++ b/lib/checks/language/has-lang-evaluate.js @@ -1,17 +1,30 @@ import { isXHTML } from '../../core/utils'; -function hasLangEvaluate(node) { - const langValue = (node.getAttribute(`lang`) || '').trim(); - const xmlLangValue = (node.getAttribute(`xml:lang`) || '').trim(); +function hasValue(value) { + return (value || '').trim() !== ''; +} - if (!langValue && xmlLangValue && !isXHTML(document)) { +function hasLangEvaluate(node, options, virtualNode) { + // special case when xml:lang has a value and lang does not + // but the document is not XHTML + if ( + options.attributes.includes('xml:lang') && + options.attributes.includes('lang') && + hasValue(virtualNode.attr('xml:lang')) && + !hasValue(virtualNode.attr('lang')) && + !isXHTML(document) + ) { this.data({ messageKey: 'noXHTML' }); return false; } - if (!(langValue || xmlLangValue)) { + const hasLang = options.attributes.some(name => { + return hasValue(virtualNode.attr(name)); + }); + + if (!hasLang) { this.data({ messageKey: 'noLang' }); diff --git a/lib/checks/language/has-lang.json b/lib/checks/language/has-lang.json index be85e8a395..9c932016ab 100644 --- a/lib/checks/language/has-lang.json +++ b/lib/checks/language/has-lang.json @@ -1,6 +1,9 @@ { "id": "has-lang", "evaluate": "has-lang-evaluate", + "options": { + "attributes": ["lang", "xml:lang"] + }, "metadata": { "impact": "serious", "messages": { diff --git a/test/checks/language/has-lang.js b/test/checks/language/has-lang.js index ef07a2a7d2..ebedcddd00 100644 --- a/test/checks/language/has-lang.js +++ b/test/checks/language/has-lang.js @@ -45,4 +45,12 @@ describe('has-lang', function() { assert.isFalse(hasLangEvaluate.apply(checkContext, params)); assert.equal(checkContext._data.messageKey, 'noLang'); }); + + it('should support options.attributes', function() { + var params = checkSetup('
', { + attributes: ['foo'] + }); + + assert.isTrue(hasLangEvaluate.apply(checkContext, params)); + }); });