Skip to content

Fixes #669: use the language-tags package to check the lang rule #670

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

Merged
merged 4 commits into from
Jun 15, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
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
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@
"emoji-regex": "^9.0.0",
"has": "^1.0.3",
"jsx-ast-utils": "^2.4.1"
"language-tags": "^1.0.5"
},
"peerDependencies": {
"eslint": "^3 || ^4 || ^5 || ^6 || ^7"
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
Loading