UNPKG

mostly-dom

Version:
33 lines 1.33 kB
"use strict"; // ~ and * : value contains // | and ^ : value starts with // $ : value ends with Object.defineProperty(exports, "__esModule", { value: true }); var attrModifiers = ['~', '*', '|', '^', '$']; function matchAttribute(cssSelector, attrs) { // tslint:disable-next-line:prefer-const var _a = cssSelector.split('='), attribute = _a[0], value = _a[1]; var attributeLength = attribute.length - 1; var modifier = attribute[attributeLength]; var modifierIndex = attrModifiers.indexOf(modifier); if (modifierIndex > -1) { attribute = attribute.slice(0, attributeLength); var attrModifier = attrModifiers[modifierIndex]; var attrValue = String(attrs[attribute]); if (!attrValue) return false; switch (attrModifier) { case '~': return attrValue.indexOf(value) > -1; case '*': return attrValue.indexOf(value) > -1; case '|': return attrValue.indexOf(value) === 0; case '^': return attrValue.indexOf(value) === 0; case '$': return attrValue.slice(-value.length) === value; default: return false; } } if (value) return value === attrs[attribute]; return !!attrs[attribute]; } exports.matchAttribute = matchAttribute; //# sourceMappingURL=matchAttribute.js.map