UNPKG

@uuv/a11y

Version:

A javascript lib for running a11y validation based on multiple reference(RGAA, etc)

101 lines (100 loc) 4.23 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.BySiblingQuery = void 0; const _00_query_1 = require("./00-query"); const lodash_1 = __importDefault(require("lodash")); const emulate_tab_1 = require("emulate-tab"); const DATA_TESTID = "data-testid"; const BODY = "BODY"; class BySiblingQuery { subQuery; shouldBeFound; siblingTags; constructor(subQuery, shouldBeFound, siblingTags) { this.subQuery = subQuery; this.shouldBeFound = shouldBeFound; this.siblingTags = siblingTags; } execute() { const result = []; this.subQuery.execute().forEach((currentElement) => { const element = currentElement.domNode; element.focus(); if (document.activeElement?.tagName === BODY) { const ghost = this.addGhostInputNode(element); ghost.focus(); this.computeResult(element, result, ghost); ghost.remove(); } else { this.computeResult(element, result); } }); return result; } computeResult(element, result, ghost = undefined) { const elementFocused = ghost ?? element; const siblingElements = []; emulate_tab_1.emulateTab.findSelectableElements().forEach((value, index, collection) => { if (value.getAttribute(DATA_TESTID) === elementFocused.getAttribute(DATA_TESTID)) { const previousExist = index - 1 >= 0; const nextExist = index + 1 <= collection.length - 1; if (this.shouldBeFound) { if (previousExist && this.foundSiblingElement(collection[index - 1])) { siblingElements.push(collection[index - 1]); } if (nextExist && this.foundSiblingElement(collection[index + 1])) { siblingElements.push(collection[index + 1]); } if (!lodash_1.default.isEmpty(siblingElements)) { result.push(new _00_query_1.QueryResult(element, siblingElements)); } } else { if ((previousExist && !this.foundSiblingElement(collection[index - 1]) && nextExist && !this.foundSiblingElement(collection[index + 1]))) { siblingElements.push(collection[index - 1]); siblingElements.push(collection[index + 1]); } if ((previousExist && !this.foundSiblingElement(collection[index - 1]) && !nextExist)) { siblingElements.push(collection[index - 1]); result.push(new _00_query_1.QueryResult(element, siblingElements)); } if ((!previousExist && nextExist && !this.foundSiblingElement(collection[index + 1]))) { siblingElements.push(collection[index + 1]); } if (!lodash_1.default.isEmpty(siblingElements)) { result.push(new _00_query_1.QueryResult(element, siblingElements)); } } } }); } foundSiblingElement(element) { return this.siblingTags.includes(element.tagName.toLowerCase()); } getSelector() { return `${this.subQuery.getSelector()}`; } addGhostInputNode(element) { const input = document.createElement("input"); input.setAttribute(DATA_TESTID, "to-delete-" + String(Math.floor(Math.random() * Date.now()))); if (element.parentNode) { element.parentNode.insertBefore(input, element.nextSibling); } else { element.insertBefore(input, element.nextSibling); } return input; } } exports.BySiblingQuery = BySiblingQuery;