Skip to content

Commit

Permalink
fix(valid-lang): fail when lang attribute contains only whitespace (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
straker committed Sep 8, 2020
1 parent ebd0407 commit 8455a7f
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/checks/language/valid-lang-evaluate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { validLangs, getBaseLang } from '../../core/utils';
import { sanitize } from '../../commons/text';

function validLangEvaluate(node, options, virtualNode) {
const langs = (options.value ? options.value : validLangs()).map(getBaseLang);
Expand All @@ -14,7 +15,11 @@ function validLangEvaluate(node, options, virtualNode) {

// Edge sets lang to an empty string when xml:lang is set
// so we need to ignore empty strings here
if (baselangVal !== '' && langs.indexOf(baselangVal) === -1) {
if (
(baselangVal !== '' && langs.indexOf(baselangVal) === -1) ||
// whitespace only lang value is invalid
(langVal !== '' && !sanitize(langVal))
) {
invalid.push(langAttr + '="' + virtualNode.attr(langAttr) + '"');
}
});
Expand Down
6 changes: 6 additions & 0 deletions test/checks/language/valid-lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,10 @@ describe('valid-lang', function() {
assert.isTrue(validLangEvaluate.apply(checkContext, params));
assert.deepEqual(checkContext._data, ['custom-lang="en_US"']);
});

it('should return true if lang value is just whitespace', function() {
var params = checkSetup('<div id="target" lang=" "></div>');

assert.isTrue(validLangEvaluate.apply(checkContext, params));
});
});
1 change: 1 addition & 0 deletions test/integration/rules/valid-lang/valid-lang.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@
<p lang="gibberish-US" id="fail1">Not English</p>
<p xml:lang="gibberish-US" id="fail2">Not English</p>
<p xml:lang="en-US" lang="gibberish-US" id="fail3">Mix</p>
<p lang=" " id="fail4">English</p>
2 changes: 1 addition & 1 deletion test/integration/rules/valid-lang/valid-lang.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"description": "valid-lang test",
"rule": "valid-lang",
"violations": [["#fail1"], ["#fail2"], ["#fail3"]],
"violations": [["#fail1"], ["#fail2"], ["#fail3"], ["#fail4"]],
"passes": [
["#pass1"],
["#pass2"],
Expand Down

0 comments on commit 8455a7f

Please sign in to comment.