In complex, rename 'seen' to leftMatches.
Showing
1 changed file
with
13 additions
and
13 deletions
| ... | @@ -152,39 +152,39 @@ const compileMatcher = (ast: AST, selector: string): MatcherProducer => { | ... | @@ -152,39 +152,39 @@ const compileMatcher = (ast: AST, selector: string): MatcherProducer => { |
| 152 | case 'complex': { | 152 | case 'complex': { |
| 153 | const { left, right, combinator, pos } = ast | 153 | const { left, right, combinator, pos } = ast |
| 154 | const leftMatcher = makeMatcher(left) | 154 | const leftMatcher = makeMatcher(left) |
| 155 | const leftCounter = counter++ | ||
| 156 | neededContext[ leftCounter ] = () => new WeakSet() | ||
| 155 | const rightMatcher = makeMatcher(right) | 157 | const rightMatcher = makeMatcher(right) |
| 156 | const setCounter = counter++ | ||
| 157 | neededContext[ setCounter ] = () => new WeakSet() | ||
| 158 | return (context, node, parent, i, debug) => { | 158 | return (context, node, parent, i, debug) => { |
| 159 | const seen = context[ setCounter ] | 159 | const { [ leftCounter ]: leftMatches } = context |
| 160 | if (leftMatcher(context, node, parent, i, debug)) { | 160 | if (leftMatcher(context, node, parent, i, debug)) { |
| 161 | if (debug) console.log('matched on left', { left, right, combinator, pos, parent }) | 161 | if (debug) console.log('matched on left', { left, right, combinator, pos, parent }) |
| 162 | // TODO: Check seen.has(), and maybe skip calling leftMatcher? | 162 | // TODO: Check leftMatches.has(), and maybe skip calling leftMatcher? |
| 163 | seen.add(node) | 163 | leftMatches.add(node) |
| 164 | } else if (parent && seen.has(parent) && combinator === ' ') { | 164 | } else if (parent && leftMatches.has(parent) && combinator === ' ') { |
| 165 | seen.add(node) | 165 | leftMatches.add(node) |
| 166 | } | 166 | } |
| 167 | if (!rightMatcher(context, node, parent, i, debug)) return false | 167 | if (!rightMatcher(context, node, parent, i, debug)) return false |
| 168 | seen.add(node) | 168 | leftMatches.add(node) |
| 169 | if (debug) console.log('matched on right', { left, right, combinator, pos, node, parent }) | 169 | if (debug) console.log('matched on right', { left, right, combinator, pos, node, parent }) |
| 170 | switch (combinator) { | 170 | switch (combinator) { |
| 171 | case ' ': | 171 | case ' ': |
| 172 | let parentPtr = parent | 172 | let parentPtr = parent |
| 173 | while (parentPtr) { | 173 | while (parentPtr) { |
| 174 | if (seen.has(parentPtr)) return true | 174 | if (leftMatches.has(parentPtr)) return true |
| 175 | parentPtr = parentPtr.parent | 175 | parentPtr = parentPtr.parent |
| 176 | } | 176 | } |
| 177 | return false | 177 | return false |
| 178 | case '>': | 178 | case '>': |
| 179 | if (debug) console.log('seen parent', seen.has(parent)) | 179 | if (debug) console.log('leftMatches parent', leftMatches.has(parent)) |
| 180 | return parent ? seen.has(parent) : false | 180 | return parent ? leftMatches.has(parent) : false |
| 181 | case '+': { | 181 | case '+': { |
| 182 | if (!parent) return false | 182 | if (!parent) return false |
| 183 | let prevSiblings = parent.children.slice(0, i).filter((el) => el.type === ELEMENT_NODE) | 183 | let prevSiblings = parent.children.slice(0, i).filter((el) => el.type === ELEMENT_NODE) |
| 184 | if (prevSiblings.length === 0) return false | 184 | if (prevSiblings.length === 0) return false |
| 185 | const prev = prevSiblings[prevSiblings.length - 1] | 185 | const prev = prevSiblings[prevSiblings.length - 1] |
| 186 | if (!prev) return false | 186 | if (!prev) return false |
| 187 | if (seen.has(prev)) return true | 187 | if (leftMatches.has(prev)) return true |
| 188 | return false | 188 | return false |
| 189 | } | 189 | } |
| 190 | case '~': { | 190 | case '~': { |
| ... | @@ -192,7 +192,7 @@ const compileMatcher = (ast: AST, selector: string): MatcherProducer => { | ... | @@ -192,7 +192,7 @@ const compileMatcher = (ast: AST, selector: string): MatcherProducer => { |
| 192 | let prevSiblings = parent.children.slice(0, i).filter((el) => el.type === ELEMENT_NODE) | 192 | let prevSiblings = parent.children.slice(0, i).filter((el) => el.type === ELEMENT_NODE) |
| 193 | if (prevSiblings.length === 0) return false | 193 | if (prevSiblings.length === 0) return false |
| 194 | for (const prev of prevSiblings) { | 194 | for (const prev of prevSiblings) { |
| 195 | if (seen.has(prev)) return true | 195 | if (leftMatches.has(prev)) return true |
| 196 | } | 196 | } |
| 197 | return false | 197 | return false |
| 198 | } | 198 | } | ... | ... |
-
Please register or sign in to post a comment