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

Commit

Permalink
remove trimIndent to reduce memory usage
Browse files Browse the repository at this point in the history
  • Loading branch information
sanity committed May 27, 2023
1 parent cf587e9 commit 72bf50d
Showing 1 changed file with 42 additions and 45 deletions.
87 changes: 42 additions & 45 deletions src/main/kotlin/kweb/ElementCreator.kt
Original file line number Diff line number Diff line change
Expand Up @@ -94,29 +94,27 @@ open class ElementCreator<out PARENT_TYPE : Element>(
element.browser.isCatchingOutbound() != null -> {
//language=JavaScript
val createElementJs = """
console.log("Creating new element")
let tag = {};
let attributes = {};
let myId = {};
let parentId = {};
let insertBefore = {};
console.log("insertBefore = " + insertBefore)
let newEl = $createElementStatement
newEl.setAttribute("id", myId);
for (const key in attributes) {
if ( key !== "id") {
newEl.setAttribute(key, attributes[key]);
}
}
let parentElement = document.getElementById(parentId);
let startNode = document.getElementById(insertBefore)
if (insertBefore !== undefined) {
parentElement.insertBefore(newEl, startNode)
} else {
parentElement.appendChild(newEl);
}
""".trimIndent()
let tag = {};
let attributes = {};
let myId = {};
let parentId = {};
let insertBefore = {};
let newEl = $createElementStatement
newEl.setAttribute("id", myId);
for (const key in attributes) {
if ( key !== "id") {
newEl.setAttribute(key, attributes[key]);
}
}
let parentElement = document.getElementById(parentId);
let startNode = document.getElementById(insertBefore)
if (insertBefore !== undefined) {
parentElement.insertBefore(newEl, startNode)
} else {
parentElement.appendChild(newEl);
}
"""
browser.callJsFunction(
createElementJs, JsonPrimitive(tag), JsonObject(mutAttributes), id.json,
JsonPrimitive(element.id), JsonPrimitive(insertBefore ?: ""), JsonPrimitive(elementsCreatedCount)
Expand All @@ -127,28 +125,27 @@ open class ElementCreator<out PARENT_TYPE : Element>(
//The way I have written this function, instead of attributes.get(), we now use attributes[].
//language=JavaScript
val createElementJs = """
console.log("Creating new element in other place")
let tag = {};
let attributes = {};
let myId = {};
let parentId = {};
let insertBefore = {};
let newEl = document.createElement(tag);
if (attributes["id"] === undefined) {
newEl.setAttribute("id", myId);
}
for (const key in attributes) {
newEl.setAttribute(key, attributes[key]);
}
let parentElement = document.getElementById(parentId);
let startNode = document.getElementById(insertBefore)
if (insertBefore !== undefined) {
parentElement.insertBefore(newEl, startNode)
} else {
parentElement.appendChild(newEl);
}
""".trimIndent()
let tag = {};
let attributes = {};
let myId = {};
let parentId = {};
let insertBefore = {};
let newEl = document.createElement(tag);
if (attributes["id"] === undefined) {
newEl.setAttribute("id", myId);
}
for (const key in attributes) {
newEl.setAttribute(key, attributes[key]);
}
let parentElement = document.getElementById(parentId);
let startNode = document.getElementById(insertBefore)
if (insertBefore !== undefined) {
parentElement.insertBefore(newEl, startNode)
} else {
parentElement.appendChild(newEl);
}
"""
element.browser.callJsFunction(
createElementJs, tag.json, JsonObject(mutAttributes), id.json,
element.id.json, JsonPrimitive(insertBefore ?: ""), JsonPrimitive(elementsCreatedCount)
Expand Down

0 comments on commit 72bf50d

Please sign in to comment.