Fix first-child/last-child matching.
Showing
1 changed file
with
6 additions
and
3 deletions
... | @@ -237,16 +237,19 @@ const compileMatcher = (ast: AST, selector: string): MatcherProducer => { | ... | @@ -237,16 +237,19 @@ const compileMatcher = (ast: AST, selector: string): MatcherProducer => { |
237 | } | 237 | } |
238 | case 'first-child': | 238 | case 'first-child': |
239 | return (context, node, parent, i, debug) => { | 239 | return (context, node, parent, i, debug) => { |
240 | return parent?.children.findFirst(child => child.type === ELEMENT_NODE) === node | 240 | const children = findChildren(context, parent, 'ELEMENT', filterChildElementsMatcher) |
241 | return children[ 0 ] == node | ||
241 | } | 242 | } |
242 | case 'last-child': | 243 | case 'last-child': |
243 | return (context, node, parent, i, debug) => { | 244 | return (context, node, parent, i, debug) => { |
244 | return parent?.children.findLast(child => child.type === ELEMENT_NODE) === node | 245 | const children = findChildren(context, parent, 'ELEMENT', filterChildElementsMatcher) |
246 | return children[ children.length - 1 ] == node | ||
245 | } | 247 | } |
246 | case 'only-child': | 248 | case 'only-child': |
247 | return (context, node, parent, i, debug) => { | 249 | return (context, node, parent, i, debug) => { |
248 | // TODO: This can break-early after it finds the second element | 250 | // TODO: This can break-early after it finds the second element |
249 | return findChildren(context, parent, 'ELEMENT', filterChildElementsMatcher).length === 1 | 251 | const children = findChildren(context, parent, 'ELEMENT', filterChildElementsMatcher) |
252 | return children.length === 1 | ||
250 | } | 253 | } |
251 | // case 'nth-of-type': | 254 | // case 'nth-of-type': |
252 | // case 'nth-last-of-type': | 255 | // case 'nth-last-of-type': | ... | ... |
-
Please register or sign in to post a comment