Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(has-lang): add option for which attributes to look at #2239

Merged
merged 2 commits into from
May 20, 2020
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
23 changes: 18 additions & 5 deletions lib/checks/language/has-lang-evaluate.js
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
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));
});
});