@qualweb/util
Version:
Utilities module for qualweb
125 lines (124 loc) • 4.55 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const constants_1 = require("./constants");
const isElementReferencedByAriaLabel_1 = __importDefault(require("./isElementReferencedByAriaLabel"));
function getAccessibleNameSVGRecursion(element, recursion) {
let AName, ariaLabelBy, tag;
tag = element.getElementTagName();
if (!tag)
tag = '';
const regex = new RegExp('^fe[a-zA-Z]+');
ariaLabelBy = element.getElementAttribute('aria-labelledby');
if (ariaLabelBy !== null && window.qwPage.getElementByID(ariaLabelBy) === null) {
ariaLabelBy = '';
}
const ariaLabel = element.getElementAttribute('aria-label');
const referencedByAriaLabel = (0, isElementReferencedByAriaLabel_1.default)(element);
const title = element.getElementChildTextContent('title');
const titleAtt = element.getElementAttribute('xlink:title');
const href = element.getElementAttribute('href');
const roleLink = tag === 'a' && href !== undefined;
if ((window.DomUtils.isElementHidden(element) ||
hasParentOfName(element, constants_1.noAccessibleObjectOrChild) ||
constants_1.noAccessibleObject.indexOf(tag) >= 0 ||
constants_1.noAccessibleObjectOrChild.indexOf(tag) >= 0 ||
regex.test(tag)) &&
!recursion) {
}
else if (ariaLabelBy && ariaLabelBy !== '' && !(referencedByAriaLabel && recursion)) {
AName = getAccessibleNameFromAriaLabelledBy(element, ariaLabelBy);
}
else if (constants_1.elementsLikeHtml.indexOf(tag) >= 0) {
AName = window.AccessibilityUtils.getAccessibleName(element);
}
else if (ariaLabel && ariaLabel.trim() !== '') {
AName = ariaLabel;
}
else if (title && title.trim() !== '') {
AName = title;
}
else if (titleAtt && titleAtt.trim() !== '' && roleLink) {
AName = titleAtt;
}
else if (roleLink) {
AName = getTextFromCss(element);
}
else if (tag && (tag === 'text' || tag === 'desc' || tag === 'title')) {
AName = window.DomUtils.getTrimmedText(element);
}
return AName;
}
function hasParentOfName(element, name) {
const parent = element.getElementParent();
if (parent) {
const parentName = parent.getElementTagName();
return (parentName && name.indexOf(parentName) >= 0) || hasParentOfName(parent, name);
}
else {
return false;
}
}
function getAccessibleNameFromAriaLabelledBy(element, ariaLabelId) {
const ListIdRefs = ariaLabelId.split(' ');
let result;
let accessNameFromId;
let elem;
const elementID = element.getElementAttribute('id');
for (const id of ListIdRefs) {
if (id !== '' && elementID !== id)
elem = window.qwPage.getElementByID(id);
if (elem)
accessNameFromId = getAccessibleNameSVGRecursion(elem, true);
if (accessNameFromId) {
if (result) {
result += accessNameFromId;
}
else {
result = accessNameFromId;
}
}
}
return result;
}
function getAccessibleNameFromChildren(element) {
let aName;
const children = element.getElementChildren();
const elementAnames = new Array();
if (children) {
for (const child of children) {
const name = child.getElementTagName();
if (constants_1.textContainer.indexOf(name) >= 0) {
aName = getAccessibleNameSVGRecursion(child, true);
if (aName) {
elementAnames.push(aName);
}
else {
elementAnames.push('');
}
}
}
}
return elementAnames;
}
function getTextFromCss(element) {
let before = element.getElementStyleProperty('content', ':before');
let after = element.getElementStyleProperty('content', ':after');
const aNameList = getAccessibleNameFromChildren(element);
const textValue = getConcatenatedText(aNameList);
if (after === 'none')
after = '';
if (before === 'none')
before = '';
return before.replace(/["']/g, '') + textValue + after.replace(/["']/g, '');
}
function getConcatenatedText(aNames) {
let result = '';
for (const aName of aNames) {
result += aName;
}
return result;
}
exports.default = getAccessibleNameSVGRecursion;