UNPKG

mostly-dom

Version:
58 lines 2.17 kB
var defaultParent = Object.freeze({ children: [] }); export function matchPsuedoSelector(cssSelector, vNode) { var parent = vNode.parent || defaultParent; var children = parent.children; if (cssSelector.indexOf(":nth-child") === 0) return matchNthChild(cssSelector.slice(11).split(')')[0], vNode); if (cssSelector.indexOf(":contains") === 0) return vNodeContainsText(cssSelector.slice(10).split(')')[0], vNode); switch (cssSelector) { case ':first-child': return children && children[0] === vNode; case ':last-child': return children && children[children.length - 1] === vNode; case ':empty': return !vNode.children || vNode.children.length === 0; case ':root': return isRoot(vNode); default: return false; } } function vNodeContainsText(text, vNode) { if (vNode.text) return text === vNode.text; var children = vNode.children; if (!children || children.length === 0) return false; for (var i = 0; i < children.length; ++i) { var child = children[i]; if (child.text === text) return true; } return false; } function isRoot(vNode) { return !vNode.parent; } function matchNthChild(index, vNode) { var parent = vNode.parent || defaultParent; var children = parent.children; if (!children || children.length === 0) return false; if (index.indexOf('+') === -1 && !isNaN(parseInt(index, 10))) return children[parseInt(index, 10)] === vNode; var childIndex = children.indexOf(vNode); if (index === 'odd') return childIndex % 2 !== 0; if (index === 'even') return childIndex % 2 === 0; if (index.indexOf('+') > -1) { var _a = index.split('+'), multipleString = _a[0], offsetString = _a[1]; var multiple = parseInt(multipleString.split('n')[0], 10); var offset = parseInt(offsetString, 10); if (multiple === 0) return true; return childIndex !== 0 && childIndex % (multiple + offset) === 0; } return false; } function isNaN(x) { return (x | 0) !== x; } //# sourceMappingURL=matchPsuedoSelector.js.map