UNPKG

mostly-dom

Version:
30 lines 1.22 kB
// ~ and * : value contains // | and ^ : value starts with // $ : value ends with var attrModifiers = ['~', '*', '|', '^', '$']; export 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]; } //# sourceMappingURL=matchAttribute.js.map