Skip to content
This repository has been archived by the owner on Aug 10, 2024. It is now read-only.

Commit

Permalink
last fix was incorrect, this one passes
Browse files Browse the repository at this point in the history
  • Loading branch information
sanity committed Sep 17, 2022
1 parent 6b59491 commit ed4d449
Showing 1 changed file with 21 additions and 31 deletions.
52 changes: 21 additions & 31 deletions src/main/kotlin/kweb/Element.kt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package kweb

import kotlinx.serialization.json.*
import kweb.WebBrowser.CatcherType.RENDER
import kweb.html.ElementReader
import kweb.html.events.Event
import kweb.html.events.EventGenerator
Expand Down Expand Up @@ -111,19 +112,14 @@ open class Element(
*/
fun setAttribute(name: String, value: JsonPrimitive, namespace : String? = null): Element {
val htmlDoc = browser.htmlDocument.get()
val jsoupDoc = browser.htmlDocument.get()
val setAttributeJavaScript = when(namespace) {
null -> "document.getElementById({}).setAttribute({}, {});"
else -> "document.getElementById({}).setAttributeNS(\"$namespace\", {}, {});"
}
when {
htmlDoc != null -> {
htmlDoc.getElementById(this.id)!!.attr(name, value.content)
}
browser.isCatchingOutbound() != null -> {
callJsFunction(
setAttributeJavaScript,
id.json, name.json, value)
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
jsoupDoc.getElementById(this.id)!!.attr(name, value.content)
}
else -> {
callJsFunction(
Expand Down Expand Up @@ -178,10 +174,10 @@ open class Element(
}

fun removeAttribute(name: String): Element {
val htmlDoc = browser.htmlDocument.get()
val jsoupDoc = browser.htmlDocument.get()
when {
htmlDoc != null -> {
htmlDoc.getElementById(id)!!.removeAttr(name)
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
jsoupDoc.getElementById(id)!!.removeAttr(name)
}
else -> {
callJsFunction("document.getElementById({}).removeAttribute({})", id.json, JsonPrimitive(name))
Expand All @@ -196,10 +192,10 @@ open class Element(
* of a DOM element.
*/
fun innerHTML(html: String): Element {
val htmlDoc = browser.htmlDocument.get()
val jsoupDoc = browser.htmlDocument.get()
when {
htmlDoc != null -> {
val thisEl = htmlDoc.getElementById(this.id)!!
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
val thisEl = jsoupDoc.getElementById(this.id)!!
thisEl.html(html)
}
else -> {
Expand Down Expand Up @@ -325,10 +321,10 @@ open class Element(
}

fun removeChildren(): Element {
val htmlDoc = browser.htmlDocument.get()
val jsoupDoc = browser.htmlDocument.get()
when {
htmlDoc != null -> {
val jsoupElement = htmlDoc.getElementById(this.id)
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
val jsoupElement = jsoupDoc.getElementById(this.id)
jsoupElement!!.children().remove()
}
else -> {
Expand All @@ -349,11 +345,11 @@ open class Element(
}

fun removeChildrenBetweenSpans(startSpanId : String, endSpanId: String) : Element{
val htmlDoc = browser.htmlDocument.get()
val jsoupDoc = browser.htmlDocument.get()
when {
htmlDoc != null -> {
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
//TODO this will only run during initial page render, and is currently untested.
htmlDoc.getElementById(this.id).let { jsoupElement ->
jsoupDoc.getElementById(this.id).let { jsoupElement ->
val startSpan = jsoupElement!!.getElementById(startSpanId)
val endSpan = jsoupElement!!.getElementById(endSpanId)
var nextSibling = startSpan!!.nextElementSibling()
Expand All @@ -380,10 +376,10 @@ open class Element(
}

fun removeChildAt(position: Int): Element {
val htmlDoc = browser.htmlDocument.get()
val jsoupDoc = browser.htmlDocument.get()
when {
htmlDoc != null -> {
htmlDoc
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
jsoupDoc
.getElementById(this.id)!!
.children()[position]
.remove()
Expand All @@ -407,13 +403,10 @@ open class Element(
//language=JavaScript
val setTextJS = """document.getElementById({}).textContent = {};""".trimIndent()
when {
jsoupDoc != null -> {
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
val element = jsoupDoc.getElementById(this.id)
element!!.text(value)
}
browser.isCatchingOutbound() != null -> {
callJsFunction(setTextJS, id.json, JsonPrimitive(value))
}
else -> {
callJsFunction(setTextJS, id.json, JsonPrimitive(value))
}
Expand Down Expand Up @@ -454,10 +447,7 @@ open class Element(
document.getElementById({}).appendChild(ntn);
""".trimIndent()
when {
browser.isCatchingOutbound() != null -> {
callJsFunction(createTextNodeJs, JsonPrimitive(value), id.json)
}
jsoupDoc != null -> {
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
val element = jsoupDoc.getElementById(this.id)
element!!.appendText(value)
}
Expand Down

0 comments on commit ed4d449

Please sign in to comment.