From b4749c232c0eb2e2e325aa3497418e6a6804380d Mon Sep 17 00:00:00 2001 From: bptato Date: Sun, 22 Dec 2024 19:09:12 +0100 Subject: dom: optimize textContent, nodeValue; add nodeValue setter --- src/html/dom.nim | 47 +++++++++++++++++++++++++++++++---------------- 1 file changed, 31 insertions(+), 16 deletions(-) (limited to 'src/html') diff --git a/src/html/dom.nim b/src/html/dom.nim index 8c630a47..016da8c3 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -2193,12 +2193,12 @@ func hasPreviousSibling(node: Node; nodeType: type): bool = node = node.previousSibling return false -func nodeValue(node: Node): Option[string] {.jsfget.} = +func nodeValue(ctx: JSContext; node: Node): JSValue {.jsfget.} = if node of CharacterData: - return some(CharacterData(node).data) + return ctx.toJS(CharacterData(node).data) elif node of Attr: - return some(Attr(node).data.value) - return none(string) + return ctx.toJS(Attr(node).data.value) + return JS_NULL func textContent*(node: Node): string = if node of CharacterData: @@ -2209,12 +2209,13 @@ func textContent*(node: Node): string = if not (child of Comment): result &= child.textContent -func jsTextContent(node: Node): Option[string] {.jsfget: "textContent".} = +func textContent(ctx: JSContext; node: Node): JSValue {.jsfget.} = if node of Document or node of DocumentType: - return none(string) # null - return some(node.textContent) + return JS_NULL + return ctx.toJS(node.textContent) func childTextContent*(node: Node): string = + result = "" for child in node.childList: if child of Text: result &= Text(child).data @@ -4276,17 +4277,31 @@ proc replaceChild(parent, node, child: Node): DOMResult[Node] {.jsfunc.} = proc createTextNode*(document: Document; data: string): Text {.jsfunc.} = return newText(document, data) -proc textContent*(node: Node; data: Option[string]) {.jsfset.} = +proc setNodeValue(ctx: JSContext; node: Node; data: JSValue): Err[void] + {.jsfset: "nodeValue".} = + if node of CharacterData: + var res = "" + if not JS_IsNull(data): + ?ctx.fromJS(data, res) + CharacterData(node).data = move(res) + elif node of Attr: + var res = "" + if not JS_IsNull(data): + ?ctx.fromJS(data, res) + Attr(node).value(move(res)) + return ok() + +proc setTextContent(ctx: JSContext; node: Node; data: JSValue): Err[void] + {.jsfset: "textContent".} = if node of Element or node of DocumentFragment: - let x = if data.isSome: - node.document.createTextNode(data.get) + if JS_IsNull(data): + node.replaceAll(nil) else: - nil - node.replaceAll(x) - elif node of CharacterData: - CharacterData(node).data = data.get("") - elif node of Attr: - value(Attr(node), data.get("")) + var res: string + ?ctx.fromJS(data, res) + node.replaceAll(node.document.createTextNode(move(res))) + return ok() + return ctx.setNodeValue(node, data) proc reset*(form: HTMLFormElement) = for control in form.controls: -- cgit