fix(selector): not throw StringIndexOut in js

https://github.com/gkd-kit/inspect/issues/7
修复在 js 里 string[i] 不会抛 StringIndexOutOfBoundsException 导致无限循环申请内存最终内存溢出
This commit is contained in:
lisonge 2023-09-23 01:23:11 +08:00
parent 30a6f59468
commit aa706b9fd5
3 changed files with 19 additions and 15 deletions

View File

@ -1,8 +0,0 @@
package li.songe.selector
import kotlin.js.ExperimentalJsExport
import kotlin.js.JsExport
@OptIn(ExperimentalJsExport::class)
@JsExport
const val version = "0.0.6"

View File

@ -198,9 +198,12 @@ internal object ParserSet {
ExtSyntaxError.assert(source, i, prefix)
val startChar = source[i]
i++
if (i >= source.length) {
ExtSyntaxError.throwError(source, i, "any char")
}
var data = ""
while (source[i] != startChar) {
if (i == source.length - 1) {
if (i >= source.length - 1) {
ExtSyntaxError.assert(source, i, startChar.toString())
break
}
@ -442,7 +445,10 @@ internal object ParserSet {
val selectorList = mutableListOf<Pair<ConnectSegment, PropertySegment>>()
while (i < source.length && whiteCharParser.prefix.contains(source[i])) {
i += whiteCharStrictParser(source, i).length
val combinator = if (combinatorParser.prefix.contains((source[i]))) {
if (i >= source.length) {
break
}
val combinator = if (combinatorParser.prefix.contains(source[i])) {
val combinatorResult = combinatorParser(source, i)
i += combinatorResult.length
i += whiteCharStrictParser(source, i).length

View File

@ -72,10 +72,9 @@ class ParserTest {
}
@Test
fun check_query(){
fun check_query() {
val projectCwd = File("../").absolutePath
val text =
"@TextView[text^='跳过'] + LinearLayout TextView[text*=`跳转`]"
val text = "@TextView[text^='跳过'] + LinearLayout TextView[text*=`跳转`]"
val selector = Selector.parse(text)
println("selector: $selector")
println(selector.trackIndex)
@ -94,8 +93,8 @@ class ParserTest {
}
}
val transform = Transform<TestNode>(getAttr = { node, name ->
if (name=="_id") return@Transform node.id
if (name=="_pid") return@Transform node.pid
if (name == "_id") return@Transform node.id
if (name == "_pid") return@Transform node.pid
val value = node.attr[name] ?: return@Transform null
if (value is JsonNull) return@Transform null
value.intOrNull ?: value.booleanOrNull ?: value.content
@ -106,4 +105,11 @@ class ParserTest {
println("target_size: " + targets.size)
println(targets.firstOrNull())
}
@Test
fun check_quote() {
// https://github.com/gkd-kit/inspect/issues/7
val selector = Selector.parse("a[a=''] ")
println("check_quote:$selector")
}
}