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

Commit ed4d449

Browse files
committed
last fix was incorrect, this one passes
1 parent 6b59491 commit ed4d449

File tree

1 file changed

+21
-31
lines changed

1 file changed

+21
-31
lines changed

src/main/kotlin/kweb/Element.kt

+21-31
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package kweb
22

33
import kotlinx.serialization.json.*
4+
import kweb.WebBrowser.CatcherType.RENDER
45
import kweb.html.ElementReader
56
import kweb.html.events.Event
67
import kweb.html.events.EventGenerator
@@ -111,19 +112,14 @@ open class Element(
111112
112113
*/
113114
fun setAttribute(name: String, value: JsonPrimitive, namespace : String? = null): Element {
114-
val htmlDoc = browser.htmlDocument.get()
115+
val jsoupDoc = browser.htmlDocument.get()
115116
val setAttributeJavaScript = when(namespace) {
116117
null -> "document.getElementById({}).setAttribute({}, {});"
117118
else -> "document.getElementById({}).setAttributeNS(\"$namespace\", {}, {});"
118119
}
119120
when {
120-
htmlDoc != null -> {
121-
htmlDoc.getElementById(this.id)!!.attr(name, value.content)
122-
}
123-
browser.isCatchingOutbound() != null -> {
124-
callJsFunction(
125-
setAttributeJavaScript,
126-
id.json, name.json, value)
121+
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
122+
jsoupDoc.getElementById(this.id)!!.attr(name, value.content)
127123
}
128124
else -> {
129125
callJsFunction(
@@ -178,10 +174,10 @@ open class Element(
178174
}
179175

180176
fun removeAttribute(name: String): Element {
181-
val htmlDoc = browser.htmlDocument.get()
177+
val jsoupDoc = browser.htmlDocument.get()
182178
when {
183-
htmlDoc != null -> {
184-
htmlDoc.getElementById(id)!!.removeAttr(name)
179+
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
180+
jsoupDoc.getElementById(id)!!.removeAttr(name)
185181
}
186182
else -> {
187183
callJsFunction("document.getElementById({}).removeAttribute({})", id.json, JsonPrimitive(name))
@@ -196,10 +192,10 @@ open class Element(
196192
* of a DOM element.
197193
*/
198194
fun innerHTML(html: String): Element {
199-
val htmlDoc = browser.htmlDocument.get()
195+
val jsoupDoc = browser.htmlDocument.get()
200196
when {
201-
htmlDoc != null -> {
202-
val thisEl = htmlDoc.getElementById(this.id)!!
197+
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
198+
val thisEl = jsoupDoc.getElementById(this.id)!!
203199
thisEl.html(html)
204200
}
205201
else -> {
@@ -325,10 +321,10 @@ open class Element(
325321
}
326322

327323
fun removeChildren(): Element {
328-
val htmlDoc = browser.htmlDocument.get()
324+
val jsoupDoc = browser.htmlDocument.get()
329325
when {
330-
htmlDoc != null -> {
331-
val jsoupElement = htmlDoc.getElementById(this.id)
326+
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
327+
val jsoupElement = jsoupDoc.getElementById(this.id)
332328
jsoupElement!!.children().remove()
333329
}
334330
else -> {
@@ -349,11 +345,11 @@ open class Element(
349345
}
350346

351347
fun removeChildrenBetweenSpans(startSpanId : String, endSpanId: String) : Element{
352-
val htmlDoc = browser.htmlDocument.get()
348+
val jsoupDoc = browser.htmlDocument.get()
353349
when {
354-
htmlDoc != null -> {
350+
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
355351
//TODO this will only run during initial page render, and is currently untested.
356-
htmlDoc.getElementById(this.id).let { jsoupElement ->
352+
jsoupDoc.getElementById(this.id).let { jsoupElement ->
357353
val startSpan = jsoupElement!!.getElementById(startSpanId)
358354
val endSpan = jsoupElement!!.getElementById(endSpanId)
359355
var nextSibling = startSpan!!.nextElementSibling()
@@ -380,10 +376,10 @@ open class Element(
380376
}
381377

382378
fun removeChildAt(position: Int): Element {
383-
val htmlDoc = browser.htmlDocument.get()
379+
val jsoupDoc = browser.htmlDocument.get()
384380
when {
385-
htmlDoc != null -> {
386-
htmlDoc
381+
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
382+
jsoupDoc
387383
.getElementById(this.id)!!
388384
.children()[position]
389385
.remove()
@@ -407,13 +403,10 @@ open class Element(
407403
//language=JavaScript
408404
val setTextJS = """document.getElementById({}).textContent = {};""".trimIndent()
409405
when {
410-
jsoupDoc != null -> {
406+
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
411407
val element = jsoupDoc.getElementById(this.id)
412408
element!!.text(value)
413409
}
414-
browser.isCatchingOutbound() != null -> {
415-
callJsFunction(setTextJS, id.json, JsonPrimitive(value))
416-
}
417410
else -> {
418411
callJsFunction(setTextJS, id.json, JsonPrimitive(value))
419412
}
@@ -454,10 +447,7 @@ open class Element(
454447
document.getElementById({}).appendChild(ntn);
455448
""".trimIndent()
456449
when {
457-
browser.isCatchingOutbound() != null -> {
458-
callJsFunction(createTextNodeJs, JsonPrimitive(value), id.json)
459-
}
460-
jsoupDoc != null -> {
450+
jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER) -> {
461451
val element = jsoupDoc.getElementById(this.id)
462452
element!!.appendText(value)
463453
}

0 commit comments

Comments
 (0)