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(rule): xml-lang-mismatch #999

Merged
merged 14 commits into from
Aug 16, 2018

Conversation

jeeyyy
Copy link
Contributor

@jeeyyy jeeyyy commented Jul 10, 2018

The rule checks that for the html element, there is no mismatch between the primary language in non-empty lang and xml:lang attributes, if both are used.

See also - https://auto-wcag.github.io/auto-wcag/rules/SC3-1-1-html-xml-lang-match.html for further details of the rule.

Closes issue:

Reviewer checks

Required fields, to be filled out by PR reviewer(s)

  • Follows the commit message policy, appropriate for next version
  • Has documentation updated, a DU ticket, or requires no documentation change
  • Includes new tests, or was unnecessary
  • Code is reviewed for security by: @WilcoFiers

@jeeyyy jeeyyy requested a review from WilcoFiers as a code owner July 10, 2018 09:22
@jeeyyy jeeyyy changed the title feat(rule): xml-lang-mismatch [WIP] feat(rule): xml-lang-mismatch Jul 11, 2018
@jeeyyy
Copy link
Contributor Author

jeeyyy commented Aug 6, 2018

Tidying up. Closing PR, will re-open when work on this rule is started again.

@jeeyyy jeeyyy closed this Aug 6, 2018
@jeeyyy jeeyyy reopened this Aug 13, 2018
@jeeyyy jeeyyy requested a review from dylanb as a code owner August 13, 2018 16:23
@jeeyyy jeeyyy changed the title [WIP] feat(rule): xml-lang-mismatch feat(rule): xml-lang-mismatch Aug 13, 2018
@jeeyyy
Copy link
Contributor Author

jeeyyy commented Aug 13, 2018

Updated PR. Reviews welcome.

* @param {String} value value specified as lang or xml:lang attribute
* @return {String}
*/
axe.utils.getBaseLang = function getBaseLang(lang) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be on axe.commons, not on axe.utils. axe.utils is reserved for things necessary in core.

@@ -0,0 +1,18 @@
{
"id": "xml-lang-mismatch",
"matches": "xml-lang-mismatch-matches.js",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We could simplify the matches by offloading some of the work to the selector: "selector": "html[lang][xml:lang]"

const langAndXmlLangAttrs = Array.from(node.attributes).filter(attr => {
const attrName = attr.nodeName.toLowerCase();
const attrValue = attr.nodeValue;
if (!attrValue) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check that the subtag is valid here (I.e. its defined in the subtag registry). If not, it's inapplicable.

"impact": "moderate",
"messages": {
"pass": "Value of lang or xml:lang attribute match if both are specified and included in the list of valid languages",
"fail": "Value of lang or xml:lang attribute either does not match or not included in the list of valid languages"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should simplify, something like "Lang and xml:lang attributes do not have the same base language"

const isValid = axe.utils.validLangs().includes(langSubTagsToValidate[0]);

// return
return isValid;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This shouldn't look at valid langs. That's covered by another rule (see my comment on matches). Instead, all this should do is

const { getBaseLang } = axe.commons.text;
return getBaseLang(node.getAttribute('lang')) === getBaseLang(node.getAttribute('xml:lang'));


// unique the produced array, if identical primary subtag values were supplied, this would result in an array of length 1.
// eg: if lang='en-GB' and xml:lang='en-US', the below will produce langSubTagsToValidate = ['en']
langSubTagsToValidate = axe.utils.uniqueArray(langSubTagsToValidate, []);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deduplicating an array is a very round-about way to check that two values are the same. Please just use two variables and do an equality comparison.


it('returns false when expecting fr instead of en', function() {
var actual = axe.utils.getBaseLang('en');
assert.notEqual(actual, 'fr');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This seems unnecessary. What you should check is case sensitivity though.

@@ -0,0 +1,18 @@
describe('axe.utils.getBaseLang', function() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also check things with multiple dashes. What should this do 'foo-bar-baz'? It's clearly invalid, so maybe this needs to return empty or null?

var checkContext = axe.testUtils.MockCheckContext();

beforeEach(function() {
node = document.createElement('html');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could use a div instead, so you don't get into browser support trouble. The check is agnostic to what node its testing.

@@ -0,0 +1,11 @@
{
"id": "xml-lang-mismatch",

This comment was marked as outdated.

This comment was marked as resolved.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorted!

@jeeyyy jeeyyy requested a review from a team as a code owner August 14, 2018 16:14
@jeeyyy jeeyyy dismissed WilcoFiers’s stale review August 14, 2018 16:14

Updated. Review again.

const primaryLangValue = getBaseLang(node.getAttribute('lang') || '');
const primaryXmlLangValue = getBaseLang(node.getAttribute('xml:lang') || '');

return primaryLangValue === primaryXmlLangValue;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be the cleanest looking check we've got. Great job Jey.

Copy link
Contributor

@WilcoFiers WilcoFiers left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couple of very minor points.

* @param {String} value value specified as lang or xml:lang attribute
* @return {String}
*/
axe.utils.getBaseLang = function getBaseLang(lang) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very minor: I'd suggest letting this function handle the null case, so that you don't have to do it separately a bunch of times.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorted!

@@ -0,0 +1,16 @@
// Rule: xml-lang-mismatch
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This comment is wrong. I'd also rename this to xml-lang-mismatch-matches.js. It's a fair bet we're going to reuse this for the non-HTML version.

],
"metadata": {
"description": "Ensure that HTML elements with both valid lang and xml:lang attributes agree on the base language of the page",
"help": "HTML elements with lang and xml:lang must have the same base language"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent spacing.

@jeeyyy
Copy link
Contributor Author

jeeyyy commented Aug 15, 2018

@WilcoFiers
All minor points, attended to. Cheers.

@WilcoFiers WilcoFiers merged commit 7452a51 into develop Aug 16, 2018
@WilcoFiers WilcoFiers deleted the new-rule-wcag20-xml-lang-mismatch-issue-558 branch August 16, 2018 10:14
@jeeyyy jeeyyy mentioned this pull request Sep 5, 2018
@jeeyyy jeeyyy mentioned this pull request Nov 2, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants