Skip to content

Inconsistent handling of xml:lang attributes for XHTML pages between html-has-lang and html-lang-valid #3623

@thibaudcolas

Description

@thibaudcolas

Product

axe-core

Product Version

4.4.2

Latest Version

  • I have tested the issue with the latest version of the product

Issue Description

html-has-lang’s has-lang-evaluate reports a failure for certain XHTML pages failing its XHTML validation, while html-lang-valid’s valid-lang-evaluate passes.

Expectation

html-has-lang and html-lang-valid should be consistent in their handling of xml:lang attributes, so it’s simpler to understand what the problem might be with a page. They should either both have the same XHTML validation, so they both fail pages with invalid language declarations – or both skip any XHTML validation.

If the discrepancy is intended, then I would expect the documentation of html-lang-valid to explain it also checks xml:lang. As-is, its description of "Ensures the lang attribute of the element has a valid value" is confusing when there is no lang attribute on the element.

Actual

Right now, when a page has a xml:lang but doesn’t pass Axe’s XHTML check,

  • html-has-lang fails with "Ensures every HTML document has a lang attribute […] The xml:lang attribute is not valid on HTML pages, use the lang attribute.".
  • html-lang-valid passes, with its description / help message suggesting everything is fine ("<html> element must have a valid value for the lang attribute").

I find it confusing that it seems lang is valid even when absent. And the XHTML validation is also confusing – I wasn’t expecting this kind of check from Axe, and the error message isn’t necessarily suggesting an actionable fix (the issue could be the page showing as HTML even though it’s not, rather than using the wrong attribute).

How to Reproduce

Here are sites where this can be tested with the Axe CLI:

axe -r html-has-lang,html-lang-valid https://www.nyjuror.gov/ --save html-lang-1.json
axe -r html-has-lang,html-lang-valid https://www.keolis-idf.com/ --save html-lang-2.json
axe -r html-has-lang,html-lang-valid https://www.cecil.fr/ --save html-lang-3.json

In each case, see the html-has-lang violation while html-lang-valid is marked as a "pass".

Additional context

Validation

Of the above three sites, the first one uses an invalid namespace and fails validator.w3.org (HTTPS scheme in xmlns="https://www.w3.org/1999/xhtml"). The other two seem to pass validation of the DTD + namespaces, but are nonetheless not detected as XHTML by Axe.

Lighthouse

I was also tempted to file this as an issue with Lighthouse, as their two audits based on Axe’s rules are even more confusing. In the above cases, it reports:

  • An error "<html> element does not have a [lang] attribute". No mention of the xml:lang / XHTML support.
  • A pass "<html> element has a valid value for its [lang] attribute"

Their web.dev resources (https://web.dev/html-has-lang/, https://web.dev/html-lang-valid/) make no mention of XHTML / XML, so it takes a lot of digging to understand the discrepancy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions