@qualweb/wcag-techniques
Version:
Implementation of the WCAG 2.1 techniques
120 lines (119 loc) • 5.53 kB
JavaScript
;
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
return c > 3 && r && Object.defineProperty(target, key, r), r;
};
var __metadata = (this && this.__metadata) || function (k, v) {
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.QW_WCAG_T23 = void 0;
const applicability_1 = require("@qualweb/util/applicability");
const evaluation_1 = require("@qualweb/core/evaluation");
const Technique_object_1 = require("../lib/Technique.object");
class QW_WCAG_T23 extends Technique_object_1.Technique {
execute(element) {
const test = new evaluation_1.Test();
const children = element.getElementChildren();
if (children !== null && children.length > 0) {
const firstFocusableElem = findFirstFocusableElement(element);
if (firstFocusableElem) {
const firstFocusableElemName = firstFocusableElem.getElementTagName();
const firstFocusableElemHREF = firstFocusableElem.getElementAttribute('href');
if (firstFocusableElemName === 'a' && firstFocusableElemHREF && firstFocusableElemHREF.trim()) {
const url = window.qwPage.getURL();
const urlConcatWithId = url + '#';
const lastSlash = url.lastIndexOf('/');
const filename = url.substring(lastSlash + 1);
if (firstFocusableElemHREF.startsWith('#') ||
firstFocusableElemHREF.startsWith(urlConcatWithId) ||
firstFocusableElemHREF.startsWith(filename)) {
const idSymbol = firstFocusableElemHREF.indexOf('#');
const idReferenced = firstFocusableElemHREF.substring(idSymbol + 1);
if (idReferenced.length > 0) {
const idElementReferenced = element.getElement('[id="' + idReferenced + '"]');
if (idElementReferenced !== null) {
if (hasMainElementAsParent(idElementReferenced)) {
test.verdict = evaluation_1.Verdict.WARNING;
test.resultCode = 'W1';
}
else {
test.verdict = evaluation_1.Verdict.WARNING;
test.resultCode = 'W2';
}
}
else {
test.verdict = evaluation_1.Verdict.FAILED;
test.resultCode = 'F1';
}
}
else {
test.verdict = evaluation_1.Verdict.FAILED;
test.resultCode = 'F2';
}
}
else {
test.verdict = evaluation_1.Verdict.FAILED;
test.resultCode = 'F3';
}
}
else {
test.verdict = evaluation_1.Verdict.FAILED;
test.resultCode = 'F4';
}
}
else {
test.verdict = evaluation_1.Verdict.FAILED;
test.resultCode = 'F5';
}
if (firstFocusableElem) {
test.addElement(firstFocusableElem);
}
this.addTestResult(test);
}
}
}
exports.QW_WCAG_T23 = QW_WCAG_T23;
__decorate([
applicability_1.ElementExists,
applicability_1.IsInMainContext,
__metadata("design:type", Function),
__metadata("design:paramtypes", [Function]),
__metadata("design:returntype", void 0)
], QW_WCAG_T23.prototype, "execute", null);
function findFirstFocusableElement(element) {
let foundFirstFocusableElem = false;
let firstFocusableElem;
const children = element.getElementChildren();
if (children && children.length > 0) {
let i = 0;
while (!foundFirstFocusableElem && i < children.length) {
if (children[i]) {
if (window.AccessibilityUtils.isElementFocusable(children[i])) {
firstFocusableElem = children[i];
foundFirstFocusableElem = true;
}
else {
firstFocusableElem = findFirstFocusableElement(children[i]);
if (firstFocusableElem) {
foundFirstFocusableElem = true;
}
}
i++;
}
else {
foundFirstFocusableElem = true;
}
}
}
return firstFocusableElem;
}
function hasMainElementAsParent(element) {
if (element) {
const pointer = element.getElementSelector();
return pointer.indexOf('main:') > 0;
}
return false;
}