UNPKG

nehan

Version:

Html layout engine for paged-media written in Typescript

93 lines 2.77 kB
import { Selector } from "./public-api"; export class AttrSelector extends Selector { constructor(left, operator, right) { super(); this.left = left; this.operator = operator; this.right = right; this.specificity.b = 1; } toString() { let str = "[" + this.left; if (this.operator) { str += this.operator; } if (this.right) { str += this.right; } str += "]"; return str; } test(element) { if (!this.operator) { return this.testAttr(element); } switch (this.operator) { case "=": return this.testEqual(element); case "*=": return this.testStarEqual(element); case "^=": return this.testCaretEqual(element); case "$=": return this.testDollarEqual(element); case "~=": return this.testTildeEqual(element); case "|=": return this.testPipeEqual(element); } throw new Error("invalid operator[" + this.operator + "](attr selector)"); } testAttr(element) { return element.hasAttribute(this.left); } testEqual(element) { let attr = element.getAttribute(this.left); if (!attr || !this.right) { return false; } return attr === this.right; } testStarEqual(element) { let attr = element.getAttribute(this.left); if (!attr || !this.right) { return false; } return attr.indexOf(this.right) >= 0; } testCaretEqual(element) { let attr = element.getAttribute(this.left); if (!attr || !this.right) { return false; } let rex = new RegExp("^" + this.right); return rex.test(attr); } testDollarEqual(element) { let attr = element.getAttribute(this.left); if (!attr || !this.right) { return false; } let rex = new RegExp(this.right + "$"); return rex.test(attr); } testTildeEqual(element) { let attr = element.getAttribute(this.left); if (!attr || !this.right) { return false; } let list = attr.trim().replace(/\s+/g, " ").split(" "); let right = this.right; return list.some(function (value) { return value === right; }); } testPipeEqual(element) { let attr = element.getAttribute(this.left); if (!attr || !this.right) { return false; } return attr === this.right || attr.indexOf(this.right + "-") >= 0; } } //# sourceMappingURL=attr-selector.js.map