UNPKG

@uuv/a11y

Version:

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

44 lines (43 loc) 1.94 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ByRoleQuery = void 0; const _00_query_1 = require("./00-query"); const dom_1 = require("@testing-library/dom"); const lodash_1 = __importDefault(require("lodash")); class ByRoleQuery { role; attributes; excludedTags; constructor(role, attributes = [], excludedTags = []) { this.role = role; this.attributes = attributes; this.excludedTags = excludedTags; } execute() { return (0, dom_1.queryAllByRole)(window.document.documentElement, this.role) ?.filter((element) => { if (lodash_1.default.isEmpty(this.attributes) && lodash_1.default.isEmpty(this.excludedTags)) { return true; } const foundExcludedTags = this.excludedTags.filter(value => { return element.tagName.toLowerCase() === value; }); const foundAttributes = this.attributes.filter(value => { return element.getAttribute(value); }); const hasFoundExcludedTags = !lodash_1.default.isEmpty(this.excludedTags) && !lodash_1.default.isEmpty(foundExcludedTags); const hasFoundAttributes = !lodash_1.default.isEmpty(this.attributes) && !lodash_1.default.isEmpty(foundAttributes); return (!hasFoundExcludedTags && hasFoundAttributes) || lodash_1.default.isEmpty(this.excludedTags) && hasFoundAttributes || lodash_1.default.isEmpty(this.attributes) && !hasFoundExcludedTags; }).map((element) => new _00_query_1.QueryResult(element)); } getSelector() { return `ByRole(${this.role}, ${this.excludedTags}, ${this.attributes})`; } } exports.ByRoleQuery = ByRoleQuery;