1
1
package kweb
2
2
3
3
import kotlinx.serialization.json.*
4
+ import kweb.WebBrowser.CatcherType.RENDER
4
5
import kweb.html.ElementReader
5
6
import kweb.html.events.Event
6
7
import kweb.html.events.EventGenerator
@@ -111,19 +112,14 @@ open class Element(
111
112
112
113
*/
113
114
fun setAttribute (name : String , value : JsonPrimitive , namespace : String? = null): Element {
114
- val htmlDoc = browser.htmlDocument.get()
115
+ val jsoupDoc = browser.htmlDocument.get()
115
116
val setAttributeJavaScript = when (namespace) {
116
117
null -> " document.getElementById({}).setAttribute({}, {});"
117
118
else -> " document.getElementById({}).setAttributeNS(\" $namespace \" , {}, {});"
118
119
}
119
120
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)
127
123
}
128
124
else -> {
129
125
callJsFunction(
@@ -178,10 +174,10 @@ open class Element(
178
174
}
179
175
180
176
fun removeAttribute (name : String ): Element {
181
- val htmlDoc = browser.htmlDocument.get()
177
+ val jsoupDoc = browser.htmlDocument.get()
182
178
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)
185
181
}
186
182
else -> {
187
183
callJsFunction(" document.getElementById({}).removeAttribute({})" , id.json, JsonPrimitive (name))
@@ -196,10 +192,10 @@ open class Element(
196
192
* of a DOM element.
197
193
*/
198
194
fun innerHTML (html : String ): Element {
199
- val htmlDoc = browser.htmlDocument.get()
195
+ val jsoupDoc = browser.htmlDocument.get()
200
196
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)!!
203
199
thisEl.html(html)
204
200
}
205
201
else -> {
@@ -325,10 +321,10 @@ open class Element(
325
321
}
326
322
327
323
fun removeChildren (): Element {
328
- val htmlDoc = browser.htmlDocument.get()
324
+ val jsoupDoc = browser.htmlDocument.get()
329
325
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)
332
328
jsoupElement!! .children().remove()
333
329
}
334
330
else -> {
@@ -349,11 +345,11 @@ open class Element(
349
345
}
350
346
351
347
fun removeChildrenBetweenSpans (startSpanId : String , endSpanId : String ) : Element {
352
- val htmlDoc = browser.htmlDocument.get()
348
+ val jsoupDoc = browser.htmlDocument.get()
353
349
when {
354
- htmlDoc != null -> {
350
+ jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER ) -> {
355
351
// 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 ->
357
353
val startSpan = jsoupElement!! .getElementById(startSpanId)
358
354
val endSpan = jsoupElement!! .getElementById(endSpanId)
359
355
var nextSibling = startSpan!! .nextElementSibling()
@@ -380,10 +376,10 @@ open class Element(
380
376
}
381
377
382
378
fun removeChildAt (position : Int ): Element {
383
- val htmlDoc = browser.htmlDocument.get()
379
+ val jsoupDoc = browser.htmlDocument.get()
384
380
when {
385
- htmlDoc != null -> {
386
- htmlDoc
381
+ jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER ) -> {
382
+ jsoupDoc
387
383
.getElementById(this .id)!!
388
384
.children()[position]
389
385
.remove()
@@ -407,13 +403,10 @@ open class Element(
407
403
// language=JavaScript
408
404
val setTextJS = """ document.getElementById({}).textContent = {};""" .trimIndent()
409
405
when {
410
- jsoupDoc != null -> {
406
+ jsoupDoc != null && (browser.isCatchingOutbound() == null || browser.isCatchingOutbound() == RENDER ) -> {
411
407
val element = jsoupDoc.getElementById(this .id)
412
408
element!! .text(value)
413
409
}
414
- browser.isCatchingOutbound() != null -> {
415
- callJsFunction(setTextJS, id.json, JsonPrimitive (value))
416
- }
417
410
else -> {
418
411
callJsFunction(setTextJS, id.json, JsonPrimitive (value))
419
412
}
@@ -454,10 +447,7 @@ open class Element(
454
447
document.getElementById({}).appendChild(ntn);
455
448
""" .trimIndent()
456
449
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 ) -> {
461
451
val element = jsoupDoc.getElementById(this .id)
462
452
element!! .appendText(value)
463
453
}
0 commit comments