UNPKG

@qualweb/act-rules

Version:

ACT rules module for qualweb web accessibility evaluator

118 lines (117 loc) 5.68 kB
"use strict"; 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_ACT_R68 = void 0; const applicability_1 = require("@qualweb/util/applicability"); const evaluation_1 = require("@qualweb/core/evaluation"); const AtomicRule_object_1 = require("../lib/AtomicRule.object"); class QW_ACT_R68 extends AtomicRule_object_1.AtomicRule { execute(element) { const test = new evaluation_1.Test(); if (element.hasCSSProperty('line-height') || this.findParentWithCSSProperty(element) !== null) { const styleAttribute = element.getElementAttribute('style'); const declaredLineHeight = this.parseStyle(styleAttribute); const computedRawLineHeight = element.getCSSProperty('line-height'); const computedLineHeight = element.getElementStyleProperty('line-height', null); const fontSize = element.getElementStyleProperty('font-size', null); const numLines = this.computeLineNumber(element, computedLineHeight); if (numLines < 2) { return; } if (element.hasCSSProperty('line-height') && !this.isImportant(computedRawLineHeight, element)) { test.verdict = evaluation_1.Verdict.PASSED; test.resultCode = 'P1'; } else if (!this.isNormal(computedLineHeight, element) && this.isLarge(computedLineHeight, fontSize)) { test.verdict = evaluation_1.Verdict.PASSED; test.resultCode = 'P2'; } else if (!this.isCascade(declaredLineHeight, computedRawLineHeight)) { test.verdict = evaluation_1.Verdict.PASSED; test.resultCode = 'P3'; } else { test.verdict = evaluation_1.Verdict.FAILED; test.resultCode = 'F1'; } test.addElement(element, true, false, true); this.addTestResult(test); } } parseStyle(style) { if (style === null) { style = ''; } const startLS = style.indexOf('line-height:'); let endLS = style.indexOf(';', startLS); if (endLS === -1) { endLS = style.length; } return style.substring(startLS + 12, endLS); } isImportant(cssValue, element) { if (cssValue.value === 'inherit' || cssValue.value === 'unset') { const parent = this.findParentWithCSSProperty(element.getElementParent()); if (parent === null) return false; return this.isImportant(parent === null || parent === void 0 ? void 0 : parent.getCSSProperty('line-height'), parent); } return cssValue.important; } isNormal(cssValue, element) { if (cssValue.value === 'inherit' || cssValue.value === 'unset') { const parent = this.findParentWithCSSProperty(element.getElementParent()); if (parent === null) return false; return this.isImportant(parent === null || parent === void 0 ? void 0 : parent.getCSSProperty('line-height'), parent); } return cssValue.value === 'normal'; } isLarge(lineHeight, fontSize) { const line = parseFloat(lineHeight.slice(0, -2)); const font = parseFloat(fontSize.slice(0, -2)); return line >= font * 1.5; } isCascade(declaredStyle, computedStyle) { if (computedStyle && computedStyle.value) { return declaredStyle.includes(computedStyle.value); } return false; } findParentWithCSSProperty(element) { while (element !== null) { if (element === null || element === void 0 ? void 0 : element.getCSSProperty('line-height')) { return element; } element = element.getElementParent(); } return null; } computeLineNumber(element, lineHeight) { const elementHeight = parseInt(element.getElementProperty('offsetHeight')); const padding_top = parseInt(element.getElementStyleProperty('padding-top', null)); const padding_bottom = parseInt(element.getElementStyleProperty('padding-bottom', null)); const border_top = parseInt(element.getElementStyleProperty('border-top', null)); const border_bottom = parseInt(element.getElementStyleProperty('border-bottom', null)); const textHeight = elementHeight - padding_top - padding_bottom - border_top - border_bottom; const numLines = textHeight / parseInt(lineHeight); return numLines; } } exports.QW_ACT_R68 = QW_ACT_R68; __decorate([ applicability_1.ElementExists, applicability_1.ElementIsVisible, applicability_1.ElementHasTextNode, __metadata("design:type", Function), __metadata("design:paramtypes", [Function]), __metadata("design:returntype", void 0) ], QW_ACT_R68.prototype, "execute", null);