@qualweb/util
Version:
Utilities module for qualweb
491 lines (490 loc) • 19.2 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.ElementExists = ElementExists;
exports.ElementIsHTMLElement = ElementIsHTMLElement;
exports.ElementHasAttributes = ElementHasAttributes;
exports.ElementHasAttribute = ElementHasAttribute;
exports.ElementHasNonEmptyAttribute = ElementHasNonEmptyAttribute;
exports.ElementHasAttributeRole = ElementHasAttributeRole;
exports.ElementHasAttributeValue = ElementHasAttributeValue;
exports.IfElementHasTagNameMustHaveAttributeRole = IfElementHasTagNameMustHaveAttributeRole;
exports.ElementHasText = ElementHasText;
exports.ElementHasTextNode = ElementHasTextNode;
exports.ElementIsInAccessibilityTree = ElementIsInAccessibilityTree;
exports.ElementHasNegativeTabIndex = ElementHasNegativeTabIndex;
exports.ElementIsNotHidden = ElementIsNotHidden;
exports.ElementSrcAttributeFilenameEqualsAccessibleName = ElementSrcAttributeFilenameEqualsAccessibleName;
exports.IsInMainContext = IsInMainContext;
exports.ElementIsVisible = ElementIsVisible;
exports.ElementIsNot = ElementIsNot;
exports.ElementHasOneOfTheFollowingRoles = ElementHasOneOfTheFollowingRoles;
exports.ElementIsWidget = ElementIsWidget;
exports.ElementIsNotWidget = ElementIsNotWidget;
exports.ElementAllowsNameFromContent = ElementAllowsNameFromContent;
exports.ElementHasCSSRules = ElementHasCSSRules;
exports.IsLangSubTagValid = IsLangSubTagValid;
exports.ElementIsImage = ElementIsImage;
exports.ElementIsNonText = ElementIsNonText;
exports.ElementIsSemanticLink = ElementIsSemanticLink;
exports.ElementIsDataTable = ElementIsDataTable;
exports.ElementHasAccessibleName = ElementHasAccessibleName;
exports.ElementHasChild = ElementHasChild;
exports.ElementHasVisibleChild = ElementHasVisibleChild;
exports.ElementDoesNotHaveChild = ElementDoesNotHaveChild;
exports.ElementHasParent = ElementHasParent;
exports.ElementIsNotChildOf = ElementIsNotChildOf;
exports.ElementIsNotInert = ElementIsNotInert;
exports.ElementIsVisibleOrInAccessibilityTree = ElementIsVisibleOrInAccessibilityTree;
exports.IsHTMLDocument = IsHTMLDocument;
function ElementExists(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
if (arguments[0]) {
return method.apply(this, arguments);
}
};
}
function ElementIsHTMLElement(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const element = arguments[0];
if (element.isElementHTMLElement()) {
return method.apply(this, arguments);
}
};
}
function ElementHasAttributes(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const hasAttributes = arguments[0].elementHasAttributes();
if (hasAttributes) {
return method.apply(this, arguments);
}
};
}
function ElementHasAttribute(attribute) {
return function (_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const attr = arguments[0].elementHasAttribute(attribute);
if (attr) {
return method.apply(this, arguments);
}
};
};
}
function ElementHasNonEmptyAttribute(attribute) {
return function (_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const attr = arguments[0].getElementAttribute(attribute);
if (attr && attr.trim()) {
return method.apply(this, arguments);
}
};
};
}
function ElementHasAttributeRole(role) {
return function (_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const _role = window.AccessibilityUtils.getElementRole(arguments[0]);
if (!_role || _role === role) {
return method.apply(this, arguments);
}
};
};
}
function ElementHasAttributeValue(attribute, value) {
return function (_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const attr = arguments[0].getElementAttribute(attribute);
if (attr && attr === value) {
return method.apply(this, arguments);
}
};
};
}
function IfElementHasTagNameMustHaveAttributeRole(tagName, role) {
return function (_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const _tagName = arguments[0].getElementTagName();
if (_tagName === tagName) {
const _role = arguments[0].getElementAttribute('role');
if (!_role || _role === role) {
return method.apply(this, arguments);
}
}
else {
return method.apply(this, arguments);
}
};
};
}
function ElementHasText(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const element = arguments[0];
if (window.DomUtils.getTrimmedText(element) !== '') {
return method.apply(this, arguments);
}
};
}
function ElementHasTextNode(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const element = arguments[0];
if (element.elementHasTextNode()) {
return method.apply(this, arguments);
}
};
}
function ElementIsInAccessibilityTree(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const isInAT = window.AccessibilityUtils.isElementInAT(arguments[0]);
if (isInAT) {
return method.apply(this, arguments);
}
};
}
function ElementHasNegativeTabIndex(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const tabindex = arguments[0].getElementAttribute('tabindex');
if (tabindex && parseInt(tabindex) <= -1) {
return method.apply(this, arguments);
}
};
}
function ElementIsNotHidden(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const notHidden = !window.DomUtils.isElementHidden(arguments[0]);
if (notHidden) {
return method.apply(this, arguments);
}
};
}
function ElementSrcAttributeFilenameEqualsAccessibleName(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const src = arguments[0].getElementAttribute('src');
const srcSet = arguments[0].getElementAttribute('srcset');
const parent = arguments[0].getElementParent();
let filenameWithExtension = new Array();
if (src) {
const filePath = src.split('/');
filenameWithExtension = [filePath[filePath.length - 1].trim().toLowerCase()];
}
if (srcSet) {
const srcSetElements = srcSet.split(',');
for (const srcsetElement of srcSetElements || []) {
const srcValue = srcsetElement.split(' ')[0];
const fileSrc = srcValue.split('/');
filenameWithExtension.push(fileSrc[fileSrc.length - 1].trim().toLowerCase());
}
}
if (parent) {
const parentTag = parent.getElementTagName();
if (parentTag === 'picture') {
const sourceElements = parent.getElements('source');
for (const sourceElement of sourceElements) {
const src = sourceElement.getElementAttribute('srcset');
if (src) {
const filePath = src.split('/');
filenameWithExtension.push(filePath[filePath.length - 1].trim().toLowerCase());
}
}
}
}
const accessibleName = window.AccessibilityUtils.getAccessibleName(arguments[0]);
if (accessibleName && filenameWithExtension && filenameWithExtension.includes(accessibleName.toLowerCase())) {
return method.apply(this, arguments);
}
};
}
function IsInMainContext(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const differentContext = arguments[0].getElementAttribute('_documentSelector');
if (!differentContext || !differentContext.includes('>')) {
return method.apply(this, arguments);
}
};
}
function ElementIsVisible(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const isVisible = window.DomUtils.isElementVisible(arguments[0]);
if (isVisible) {
return method.apply(this, arguments);
}
};
}
function ElementIsNot(names) {
return function (_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const name = arguments[0].getElementTagName();
if (name && !names.includes(name)) {
return method.apply(this, arguments);
}
};
};
}
function ElementHasOneOfTheFollowingRoles(roles) {
return function (_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const role = window.AccessibilityUtils.getElementRole(arguments[0]);
if (!!role && roles.includes(role)) {
return method.apply(this, arguments);
}
};
};
}
function ElementIsWidget(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const isWidget = window.AccessibilityUtils.isElementWidget(arguments[0]);
if (isWidget) {
return method.apply(this, arguments);
}
};
}
function ElementIsNotWidget(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const isWidget = window.AccessibilityUtils.isElementWidget(arguments[0]);
if (!isWidget) {
return method.apply(this, arguments);
}
};
}
function ElementAllowsNameFromContent(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const supportsNameFromContent = window.AccessibilityUtils.allowsNameFromContent(arguments[0]);
if (supportsNameFromContent) {
return method.apply(this, arguments);
}
};
}
function ElementHasCSSRules(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const element = arguments[0];
if (element.getCSSRules()) {
return method.apply(this, arguments);
}
};
}
function IsLangSubTagValid(attribute) {
return function (_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const attr = arguments[0].getElementAttribute(attribute);
if (attr && isSubTagValid(attr.split('-')[0])) {
return method.apply(this, arguments);
}
};
};
}
function isSubTagValid(subTag) {
const languages = window.AccessibilityUtils.languages;
return subTag.toLowerCase() in languages;
}
function ElementIsImage(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const element = arguments[0];
const role = window.AccessibilityUtils.getElementRole(element);
if (element.getElementTagName() === 'img' || role === 'img') {
return method.apply(this, arguments);
}
};
}
function ElementIsNonText(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const element = arguments[0];
const isNonText = window.DomUtils.objectElementIsNonText(element);
if (isNonText) {
return method.apply(this, arguments);
}
};
}
const semanticLinkRoles = ['link', 'doc-backlink', 'doc-biblioref', 'doc-glossref', 'doc-noteref'];
function ElementIsSemanticLink(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const element = arguments[0];
const role = window.AccessibilityUtils.getElementRole(element);
if (!!role && semanticLinkRoles.includes(role)) {
return method.apply(this, arguments);
}
};
}
function ElementIsDataTable(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const isDataTable = window.AccessibilityUtils.isDataTable(arguments[0]);
if (isDataTable) {
return method.apply(this, arguments);
}
};
}
function ElementHasAccessibleName(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const accessibleName = window.AccessibilityUtils.getAccessibleName(arguments[0]);
if ((accessibleName === null || accessibleName === void 0 ? void 0 : accessibleName.trim()) !== '') {
return method.apply(this, arguments);
}
};
}
function ElementHasChild(child) {
return function (_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
var arguments_1 = arguments;
return __awaiter(this, void 0, void 0, function* () {
const children = arguments_1[0].getElements(child);
if (children.length !== 0) {
return method.apply(this, arguments_1);
}
});
};
};
}
function ElementHasVisibleChild(child) {
return function (_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
var arguments_2 = arguments;
return __awaiter(this, void 0, void 0, function* () {
const children = arguments_2[0].getElements(child);
if (children.length !== 0) {
let isVisible = false;
for (const child of children !== null && children !== void 0 ? children : []) {
isVisible || (isVisible = window.DomUtils.isElementVisible(child));
}
if (isVisible) {
return method.apply(this, arguments_2);
}
}
});
};
};
}
function ElementDoesNotHaveChild(child) {
return function (_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
var arguments_3 = arguments;
return __awaiter(this, void 0, void 0, function* () {
const children = arguments_3[0].getElements(child);
if (children.length === 0) {
return method.apply(this, arguments_3);
}
});
};
};
}
function ElementHasParent(parent) {
return function (_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
var arguments_4 = arguments;
return __awaiter(this, void 0, void 0, function* () {
const element = arguments_4[0].elementHasParent(parent);
if (element) {
return method.apply(this, arguments_4);
}
});
};
};
}
function ElementIsNotChildOf(parent) {
return function (_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
var arguments_5 = arguments;
return __awaiter(this, void 0, void 0, function* () {
const hasParent = arguments_5[0].elementHasParent(parent);
if (!hasParent) {
return method.apply(this, arguments_5);
}
});
};
};
}
function ElementIsNotInert(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
let notInert = true;
const hasInert = arguments[0].elementHasAttribute('inert');
if (hasInert) {
const inert = arguments[0].getElementAttribute('inert');
if (inert !== 'false') {
notInert = false;
}
if (!notInert) {
return;
}
}
let noParentInert = true;
let parent = arguments[0].getElementParent();
while (parent) {
const inert = parent.getElementAttribute('inert');
if (inert && inert === 'true') {
noParentInert = false;
}
parent = parent.getElementParent();
}
if (!noParentInert) {
return;
}
if (window.qwPage.pageHasOpenDialog()) {
return;
}
return method.apply(this, arguments);
};
}
function ElementIsVisibleOrInAccessibilityTree(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
const page = window.qwPage;
const elements = page.getElements('*').filter((element) => {
return (element.hasTextNode() &&
(window.DomUtils.isElementVisible(element) || window.AccessibilityUtils.isElementInAT(element)));
});
if (elements.length > 0) {
return method.apply(this, arguments);
}
};
}
function IsHTMLDocument(_target, _propertyKey, descriptor) {
const method = descriptor.value;
descriptor.value = function () {
let IsNonHTMLDocument = false;
const htmlElement = window.qwPage.getElement('html');
if (htmlElement)
IsNonHTMLDocument = htmlElement.getElementAttribute('nonHTMLPage') === 'true';
if (!IsNonHTMLDocument) {
return method.apply(this, arguments);
}
};
}