Skip to content

Commit

Permalink
Fixes #669: use the language-tags package to check the lang rule
Browse files Browse the repository at this point in the history
The `language-tags` package provides language tag validation
functionality based on the latest IANA language subtag registry.

This PR makes use of this package instead of manually parsing tags
like before. File `src/util/attributes/ISO.json` is removed then.

Signed-off-by: imtsuki <me@qjx.app>
  • Loading branch information
imtsuki committed Mar 13, 2020
1 parent 7bcea20 commit 42a2016
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 401 deletions.
4 changes: 4 additions & 0 deletions __tests__/src/rules/lang-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ ruleTester.run('lang', rule, {
{ code: '<div lang="foo" />;' },
{ code: '<html lang="en" />' },
{ code: '<html lang="en-US" />' },
{ code: '<html lang="zh-Hans" />' },
{ code: '<html lang="zh-Hant-HK" />' },
{ code: '<html lang="zh-yue-Hant" />' },
{ code: '<html lang="ja-Latn" />' },
{ code: '<html lang={foo} />' },
{ code: '<HTML lang="foo" />' },
{ code: '<Foo lang="bar" />' },
Expand Down
8 changes: 5 additions & 3 deletions docs/rules/lang.md
Original file line number Diff line number Diff line change
@@ -1,17 +1,19 @@
# lang

The `lang` prop on the `<html>` element must have a valid value based on ISO country and language codes.
The `lang` prop on the `<html>` element must be a valid IETF's BCP 47 language tag.

#### References

1. [axe-core, valid-lang](https://dequeuniversity.com/rules/axe/3.2/valid-lang)
2. [ISO Language Codes](http://www.w3schools.com/tags/ref_language_codes.asp)
3. [ISO Country Codes](http://www.w3schools.com/tags/ref_country_codes.asp)
2. [Language tags in HTML and XML](https://www.w3.org/International/articles/language-tags/)
3. [IANA Language Subtag Registry](https://www.iana.org/assignments/language-subtag-registry/language-subtag-registry)

## Rule details

This rule takes no arguments.

### Succeed

```jsx
<html lang="en">
<html lang="en-US">
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
"damerau-levenshtein": "^1.0.4",
"emoji-regex": "^7.0.2",
"has": "^1.0.3",
"jsx-ast-utils": "^2.2.1"
"jsx-ast-utils": "^2.2.1",
"language-tags": "^1.0.5"
},
"peerDependencies": {
"eslint": "^3 || ^4 || ^5 || ^6"
Expand Down
9 changes: 2 additions & 7 deletions src/rules/lang.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@
// ----------------------------------------------------------------------------

import { propName, elementType, getLiteralPropValue } from 'jsx-ast-utils';
import tags from 'language-tags';
import { generateObjSchema } from '../util/schemas';
import ISO_CODES from '../util/attributes/ISO.json';

const errorMessage = 'lang attribute must have a valid value.';

Expand Down Expand Up @@ -51,12 +51,7 @@ module.exports = {
return;
}

const hyphen = value.indexOf('-');
const lang = hyphen > -1 ? value.substring(0, hyphen) : value;
const country = hyphen > -1 ? value.substring(3) : undefined;

if (ISO_CODES.languages.indexOf(lang) > -1
&& (country === undefined || ISO_CODES.countries.indexOf(country) > -1)) {
if (tags.check(value)) {
return;
}

Expand Down

0 comments on commit 42a2016

Please sign in to comment.