Skip to content

Commit ac7975e

Browse files
committed
fix: selector isMatchRoot not working
1 parent f32b634 commit ac7975e

File tree

3 files changed

+16
-2
lines changed

3 files changed

+16
-2
lines changed

selector/src/commonMain/kotlin/li/songe/selector/property/PropertyWrapper.kt

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,12 @@ data class PropertyWrapper(
3636

3737
val isMatchRoot = segment.units.any {
3838
val e = it.expression
39-
e is BinaryExpression && e.operator == CompareOperator.Equal && e.left.value == "parent" && e.right.value == "null"
39+
e is BinaryExpression && e.operator == CompareOperator.Equal && when {
40+
// null == Identifier(name="parent")
41+
e.right.value == null && e.left.value == "parent" -> true
42+
e.left.value == null && e.right.value == "parent" -> true
43+
else -> false
44+
}
4045
}
4146

4247
val fastQueryList by lazy { segment.fastQueryList ?: emptyList() }

selector/src/commonMain/kotlin/li/songe/selector/unit/LogicalSelectorExpression.kt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,10 @@ data class LogicalSelectorExpression(
5757
}
5858

5959
override val isMatchRoot: Boolean
60-
get() = left.isMatchRoot && operator == SelectorLogicalOperator.AndOperator
60+
get() = when (operator) {
61+
SelectorLogicalOperator.AndOperator -> left.isMatchRoot || right.isMatchRoot
62+
SelectorLogicalOperator.OrOperator -> left.isMatchRoot && right.isMatchRoot
63+
}
6164

6265
override val fastQueryList: List<FastQuery>
6366
get() = left.fastQueryList + right.fastQueryList

selector/src/jvmTest/kotlin/li/songe/selector/ParserUnitTest.kt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,4 +163,10 @@ class ParserUnitTest {
163163
println("selector: ${ast.value}")
164164
println("ast: ${ast.stringify()}")
165165
}
166+
167+
@Test
168+
fun root() {
169+
assert(Selector.parse("[null=parent]").isMatchRoot)
170+
assert(Selector.parse("[parent=null]").isMatchRoot)
171+
}
166172
}

0 commit comments

Comments
 (0)