Context: Kotlin Multiplatform JavaScript.
I'm trying to use a Document.evaluate() but getting Unresolved reference: evaluate
and Unresolved reference: XPathResult
.
import org.w3c.dom.Document
import org.w3c.dom.parsing.DOMParser
object JavascriptTest {
// language=HTML
private val html = """
<body>
<div>
<a class="button" href="http://exaple.com">Example</a>
</div>
</body>
""".trimIndent()
fun parseXpath() {
val parser = DOMParser()
val document: Document = parser.parseFromString(html, "text/html")
val xpath = "//div/a[contains(@class, \"button\")]"
document.evaluate(xpath, document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null)
}
}
build.gradle.kts
kotlin {
js(IR) {
useCommonJs()
browser {}
binaries.executable()
}
sourceSets {
val jsTest by getting
}
As far as I know there is no bindings for XPathEvaluator
interface out of the box right now. But you could add them by yourself using asDynamic
:
val XPathResult = window.asDynamic().XPathResult
fun Document.evaluate(
xpathExpression: String,
contextNode: Node,
namespaceResolver: Any?,
resultType: Any,
result: Any?
): dynamic =
asDynamic().evaluate(xpathExpression, contextNode, namespaceResolver, resultType, result)