-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(preset-wcag): Add
page-has-valid-lang
rule
- Loading branch information
Showing
7 changed files
with
105 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
packages/acot-preset-wcag/docs/rules/page-has-valid-lang.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# page-has-valid-lang | ||
|
||
The `html` element MUST has a valid lang attribute. | ||
|
||
> The objective of this technique is to identify the default language of a document by providing the lang and/or xml:lang attribute on the html element. | ||
[H57: Using language attributes on the html element | Techniques for WCAG 2.0](https://www.w3.org/TR/WCAG20-TECHS/H57.html) | ||
|
||
## :white_check_mark: Correct | ||
|
||
```html acot-template:templates/empty.html | ||
<html lang="en-GB"></html> | ||
``` | ||
|
||
## :warning: Incorrect | ||
|
||
```html acot-template:templates/empty.html | ||
<html lang="en-FOO"></html> | ||
``` | ||
|
||
```html acot-template:templates/empty.html | ||
<!-- missing html element --> | ||
``` | ||
|
||
```html acot-template:templates/empty.html | ||
<html></html> | ||
``` | ||
|
||
```html acot-template:templates/empty.html | ||
<html lang=""></html> | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
<!DOCTYPE html> | ||
{{{ html }}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
packages/acot-preset-wcag/src/rules/page-has-valid-lang.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import type { ElementHandle } from 'puppeteer-core'; | ||
import { createRule } from '@acot/core'; | ||
import tags from 'language-tags'; | ||
|
||
type Options = {}; | ||
|
||
export default createRule<Options>({ | ||
type: 'global', | ||
immutable: true, | ||
meta: { | ||
recommended: true, | ||
}, | ||
|
||
test: async (context) => { | ||
const html: ElementHandle<HTMLElement> | null = await context.page.$( | ||
'html', | ||
); | ||
|
||
if (html == null) { | ||
return await context.report({ | ||
message: 'html element MUST be exists.', | ||
}); | ||
} | ||
|
||
const lang = await html.evaluate( | ||
(el) => el.getAttribute('lang') || el.getAttribute('xml:lang'), | ||
); | ||
|
||
if (!lang) { | ||
return await context.report({ | ||
node: html, | ||
message: | ||
'html element MUST have the "lang" or "xml:lang" attribute specified.', | ||
}); | ||
} | ||
|
||
const valid = tags.check(lang); | ||
context.debug('lang="%s"', lang, valid); | ||
|
||
if (!valid) { | ||
return await context.report({ | ||
node: html, | ||
message: `"${lang}" lang attribute specified for the html element is invalid. The value of the lang attribute MUST be conforms to BCP 47: Tags for the Identification of Languages or its successor.`, | ||
}); | ||
} | ||
}, | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters