From 7634ee338ce0a37f284b60bcb5cd34b914484bd3 Mon Sep 17 00:00:00 2001 From: Steven Lambert Date: Mon, 18 May 2020 14:43:49 -0600 Subject: [PATCH 1/2] feat(has-lang): add option for which attributes to look at --- lib/checks/language/has-lang-evaluate.js | 23 ++++++++++++++++++----- lib/checks/language/has-lang.json | 3 +++ test/checks/language/has-lang.js | 8 ++++++++ 3 files changed, 29 insertions(+), 5 deletions(-) diff --git a/lib/checks/language/has-lang-evaluate.js b/lib/checks/language/has-lang-evaluate.js index 84dd357d6c..57ef1fa7d2 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 but + // 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)); + }); }); From 5218aafad6fc1119e2cfed65f65ac50b9a0c8a8b Mon Sep 17 00:00:00 2001 From: Steven Lambert Date: Mon, 18 May 2020 14:44:50 -0600 Subject: [PATCH 2/2] typo --- lib/checks/language/has-lang-evaluate.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/checks/language/has-lang-evaluate.js b/lib/checks/language/has-lang-evaluate.js index 57ef1fa7d2..a06e0a8fd7 100644 --- a/lib/checks/language/has-lang-evaluate.js +++ b/lib/checks/language/has-lang-evaluate.js @@ -5,7 +5,7 @@ function hasValue(value) { } function hasLangEvaluate(node, options, virtualNode) { - // special case when xml:lang has a value and lang does but + // special case when xml:lang has a value and lang does not // but the document is not XHTML if ( options.attributes.includes('xml:lang') &&