Skip to content

Commit

Permalink
add more testcases and implement rule better
Browse files Browse the repository at this point in the history
Co-authored-by: Michiel Pauw <[email protected]>
Co-authored-by: NioniosSpeed <[email protected]>
  • Loading branch information
3 people authored and Kristján Oddsson committed May 28, 2024
1 parent 21aa07c commit 9d81dcf
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 4 deletions.
5 changes: 4 additions & 1 deletion src/rules/html-has-lang.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ const url = `https://dequeuniversity.com/rules/axe/4.4/${id}`;

export default function (el: Element): AccessibilityError[] {
const htmlElement = el.ownerDocument.documentElement;
if (!htmlElement.hasAttribute("lang")) {
const langAttribute = htmlElement.getAttribute("lang");

// Report error if the `lang` attribute is not on the element or if it's just whitespace
if (!langAttribute || langAttribute?.trim() === "") {
return [{ element: htmlElement, url, text }];
}
return [];
Expand Down
72 changes: 69 additions & 3 deletions tests/html-has-lang.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,76 @@
import { expect } from "@open-wc/testing";
import { expect, fixture, html, nextFrame } from "@open-wc/testing";
import { scan } from "../src/scanner";
import htmlHasLang from "../src/rules/html-has-lang";

/**
* Create a `<html></html>` in a iframe so that we aren't using the `<html>` element of the test runner.
*/
async function createHTMLElement(htmlString: string): Promise<HTMLElement> {
return (
await fixture(html`<iframe srcdoc="<!DOCTYPE html>${htmlString}"></iframe>`)
).contentDocument // @ts-expect-error Just trust me bro
.querySelector("html")!;
}

describe("html-has-lang", function () {
it("empty alt attribute fails", async () => {
const container = document.createElement("html");
it("html element without a lang attribute fails", async () => {
const container = await createHTMLElement("<html></html>");

// Not sure why there's a timing issue here but this seems to fix it
await nextFrame();

const results = (await scan(container, [htmlHasLang])).map(
({ text, url }) => {
return { text, url };
},
);

expect(results).to.eql([
{
text: "<html> element must have a lang attribute",
url: "https://dequeuniversity.com/rules/axe/4.4/html-has-lang",
},
]);
});

it("html element with a empty lang attribute fails", async () => {
const container = await createHTMLElement('<html lang=""></html>');

const results = (await scan(container, [htmlHasLang])).map(
({ text, url }) => {
return { text, url };
},
);

expect(results).to.eql([
{
text: "<html> element must have a lang attribute",
url: "https://dequeuniversity.com/rules/axe/4.4/html-has-lang",
},
]);
});

it("html element with a boolean lang attribute fails", async () => {
const container = await createHTMLElement("<html lang></html>");

const results = (await scan(container, [htmlHasLang])).map(
({ text, url }) => {
return { text, url };
},
);

expect(results).to.eql([
{
text: "<html> element must have a lang attribute",
url: "https://dequeuniversity.com/rules/axe/4.4/html-has-lang",
},
]);
});

it("html element with a whitespace only lang attribute fails", async () => {
const container = await createHTMLElement(
'<html lang=" "></html>',
);

const results = (await scan(container, [htmlHasLang])).map(
({ text, url }) => {
Expand Down

0 comments on commit 9d81dcf

Please sign in to comment.