4acd3583 by Adam Heath

Fix first-child/last-child matching.

1 parent abfc737a
......@@ -237,16 +237,19 @@ const compileMatcher = (ast: AST, selector: string): MatcherProducer => {
}
case 'first-child':
return (context, node, parent, i, debug) => {
return parent?.children.findFirst(child => child.type === ELEMENT_NODE) === node
const children = findChildren(context, parent, 'ELEMENT', filterChildElementsMatcher)
return children[ 0 ] == node
}
case 'last-child':
return (context, node, parent, i, debug) => {
return parent?.children.findLast(child => child.type === ELEMENT_NODE) === node
const children = findChildren(context, parent, 'ELEMENT', filterChildElementsMatcher)
return children[ children.length - 1 ] == node
}
case 'only-child':
return (context, node, parent, i, debug) => {
// TODO: This can break-early after it finds the second element
return findChildren(context, parent, 'ELEMENT', filterChildElementsMatcher).length === 1
const children = findChildren(context, parent, 'ELEMENT', filterChildElementsMatcher)
return children.length === 1
}
// case 'nth-of-type':
// case 'nth-last-of-type':
......