Skip to content

Commit

Permalink
perf(htmlcs): add break on label detection
Browse files Browse the repository at this point in the history
  • Loading branch information
j-mendez committed Aug 9, 2023
1 parent 31edb80 commit 725e569
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
7 changes: 5 additions & 2 deletions fast_htmlcs/HTMLCS.Util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -814,10 +814,13 @@ _global.HTMLCS.util = {
*
* @return void
*/
eachParentNode: function (node, cb) {
eachParentNode: function (node, cb: (node: Element) => boolean) {
while (node && node.parentNode) {
cb(node);
const c = cb(node);
node = node.parentNode as Element;
if (c) {
break;
}
}
},

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,9 +221,9 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_3_1_3_1 = {
* @param {DOMNode} top The top element of the tested code.
*/
testLabelsOnInputs: function (element, _, muteErrors) {
let inputType = element.nodeName.toLowerCase();
let inputType = element.nodeName;

if (inputType === "input") {
if (inputType === "INPUT") {
if (element.hasAttribute("type")) {
inputType = element.getAttribute("type").toLowerCase();
} else {
Expand All @@ -233,7 +233,8 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_3_1_3_1 = {

let hasLabel: boolean | Record<string, any> = false;

let addToLabelList = function (found) {
// this isnt really needed as an object
const addToLabelList = function (found) {
if (!hasLabel) {
hasLabel = {};
}
Expand All @@ -256,33 +257,31 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_3_1_3_1 = {
}

// Find an explicit label.
let explicitLabel = element.ownerDocument.querySelector(
'label[for="' + element.id + '"]'
);
if (explicitLabel) {
if (
element.ownerDocument.querySelector('label[for="' + element.id + '"]')
) {
addToLabelList("explicit");
}

// Find an implicit label.
let foundImplicit = false;

if (element.parentNode) {
HTMLCS.util.eachParentNode(element, function (parent) {
if (parent.nodeName.toLowerCase() === "label") {
if (parent.nodeName === "LABEL") {
foundImplicit = true;
}
return foundImplicit;
});
}

// @ts-ignore
if (foundImplicit === true) {
if (foundImplicit) {
addToLabelList("implicit");
}

// Find a title attribute.
let title = element.getAttribute("title");

if (title !== null) {
if (/^\s*$/.test(title) === true && needsLabel === true) {
if (element.hasAttribute("title")) {
if (/^\s*$/.test(element.getAttribute("title")) === true && needsLabel) {
HTMLCS.addMessage(
HTMLCS.WARNING,
element,
Expand All @@ -295,8 +294,8 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_3_1_3_1 = {
}

// Find an aria-label attribute.
if (element.hasAttribute("aria-label") === true) {
if (HTMLCS.util.hasValidAriaLabel(element) === false) {
if (element.hasAttribute("aria-label")) {
if (!HTMLCS.util.hasValidAriaLabel(element)) {
HTMLCS.addMessage(
HTMLCS.WARNING,
element,
Expand All @@ -309,8 +308,8 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_3_1_3_1 = {
}

// Find an aria-labelledby attribute.
if (element.hasAttribute("aria-labelledby") === true) {
if (HTMLCS.util.hasValidAriaLabel(element) === false) {
if (element.hasAttribute("aria-labelledby")) {
if (!HTMLCS.util.hasValidAriaLabel(element)) {
HTMLCS.addMessage(
HTMLCS.WARNING,
element,
Expand All @@ -326,7 +325,7 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_3_1_3_1 = {
}

if (!(muteErrors === true)) {
if (hasLabel !== false && needsLabel === false) {
if (hasLabel !== false && !needsLabel) {
// Note that it is okay for buttons to have aria-labelledby or
// aria-label, or title. The former two override the button text,
// while title is a lower priority than either: the button text,
Expand All @@ -348,7 +347,7 @@ _global.HTMLCS_WCAG2AAA_Sniffs_Principle1_Guideline1_3_1_3_1 = {
"F68.HiddenAttr"
);
}
} else if (hasLabel === false && needsLabel === true) {
} else if (hasLabel === false && needsLabel) {
// Needs label.
HTMLCS.addMessage(
HTMLCS.ERROR,
Expand Down
2 changes: 1 addition & 1 deletion fast_htmlcs/globals.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ type HTMLCS = {
HSVtosRGB(colour: HSV): RGB;
getElementTextContent(element: Element, hasAlt?: boolean): string;
findParentNode(node: Element, selector: string): Node;
eachParentNode(node: Element, cb: (node: Element) => void): void;
eachParentNode(node: Element, cb: (node: Element) => boolean): void;
getChildrenForTable(table: Element, childNodeName: string): Element[];
testTableHeaders(table: Element): RetVal;
getCellHeaders(tableCell: Element): Element[];
Expand Down
2 changes: 1 addition & 1 deletion fast_htmlcs/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fast_htmlcs",
"version": "0.0.65",
"version": "0.0.66",
"description": "A high performance fork of HTML_CodeSniffer.",
"license": "BSD-3-Clause",
"main": "index.js",
Expand Down
2 changes: 1 addition & 1 deletion kayle/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kayle",
"version": "0.5.30",
"version": "0.5.31",
"description": "Extremely fast and accurate accessibility testing using CDP",
"main": "./build/index.js",
"keywords": [
Expand Down

0 comments on commit 725e569

Please sign in to comment.