Skip to content

Commit

Permalink
feat(has-lang): add option for which attributes to look at (#2239)
Browse files Browse the repository at this point in the history
* feat(has-lang): add option for which attributes to look at

* typo
  • Loading branch information
straker committed May 20, 2020
1 parent ffee19e commit e69c46a
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 5 deletions.
23 changes: 18 additions & 5 deletions 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'
});
Expand Down
3 changes: 3 additions & 0 deletions 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": {
Expand Down
8 changes: 8 additions & 0 deletions test/checks/language/has-lang.js
Expand Up @@ -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('<div id="target" foo="cats"></div>', {
attributes: ['foo']
});

assert.isTrue(hasLangEvaluate.apply(checkContext, params));
});
});

0 comments on commit e69c46a

Please sign in to comment.