From 40aef0b310bb84628b759bdb3f17ff7229afc634 Mon Sep 17 00:00:00 2001 From: bptato Date: Thu, 31 Aug 2023 22:37:55 +0200 Subject: dom: fix previous/nextElementSibling, add location stringifier --- src/html/dom.nim | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) (limited to 'src/html') diff --git a/src/html/dom.nim b/src/html/dom.nim index 617ec404..4afa953e 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -1187,9 +1187,12 @@ proc setLocation*(document: Document, s: string): Err[JSError] # Note: we do not implement security checks (as documents are in separate # windows anyway). -func href(location: Location): string {.jsuffget.} = +func `$`(location: Location): string {.jsuffunc.} = return location.url.serialize() +func href(location: Location): string {.jsuffget.} = + return $location + proc setHref(location: Location, s: string): Err[JSError] {.jsfset: "href".} = if location.document == nil: @@ -1600,21 +1603,21 @@ func getElementsByClassName(element: Element, classNames: string): HTMLCollectio return element.getElementsByClassName0(classNames) func previousElementSibling*(elem: Element): Element {.jsfget.} = - if elem.parentNode == nil: return nil - var i = elem.index - 1 - while i >= 0: - if elem.parentNode.childList[i].nodeType == ELEMENT_NODE: + let p = elem.parentNode + if p == nil: return nil + for i in countdown(elem.index - 1, 0): + let node = p.childList[i] + if p.childList[i].nodeType == ELEMENT_NODE: return elem - dec i return nil func nextElementSibling*(elem: Element): Element {.jsfget.} = - if elem.parentNode == nil: return nil - var i = elem.index + 1 - while i < elem.parentNode.childList.len: - if elem.parentNode.childList[i].nodeType == ELEMENT_NODE: - return elem - inc i + let p = elem.parentNode + if p == nil: return nil + for i in elem.index + 1 .. p.childList.high: + let node = p.childList[i] + if node.nodeType == ELEMENT_NODE: + return Element(node) return nil func documentElement(document: Document): Element {.jsfget.} = -- cgit 1.4.1-2-gfad0