diff options
| author | bptato <nincsnevem662@gmail.com> | 2023-12-01 22:04:20 +0100 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2023-12-01 22:08:35 +0100 |
| commit | 0a993deab62fb0ae749b9275dcfca35871f27292 (patch) | |
| tree | d80fce7ff57393d35e010f45523c0533b9ffc73c /src/html | |
| parent | e2e046ce148d5197a4e5681e5e23bf05647d54b2 (diff) | |
| download | chawan-0a993deab62fb0ae749b9275dcfca35871f27292.tar.gz | |
dom: functionize parentElement, improve Node.document
* parentElement is now just a function.
* Remove TODO from node.document; it is useful and not going anywhere.
* Check for Nim type in Node.document, not nodeType
Diffstat (limited to 'src/html')
| -rw-r--r-- | src/html/dom.nim | 14 |
1 files changed, 7 insertions, 7 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim index 0cd61602..ada36a19 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -152,7 +152,6 @@ type nodeType*: NodeType childList*: seq[Node] parentNode* {.jsget.}: Node - parentElement* {.jsget.}: Element root: Node index*: int # Index in parents children. -1 for nodes without a parent. # Live collection cache: ids of live collections are saved in all @@ -878,13 +877,17 @@ func `$`*(node: Node): string = else: result = "Node of " & $node.nodeType -#TODO this should be an alias of ownerDocument, but I think we have plenty -# of code depending on the fact that this returns identity for document nodes. func document*(node: Node): Document = - if node.nodeType == DOCUMENT_NODE: + if node of Document: return Document(node) return node.document_internal +func parentElement*(node: Node): Element {.jsfget.} = + let p = node.parentNode + if p != nil and p of Element: + return Element(p) + return nil + iterator elementList*(node: Node): Element {.inline.} = for child in node.childList: if child.nodeType == ELEMENT_NODE: @@ -2600,7 +2603,6 @@ proc remove*(node: Node, suppressObservers: bool) = parent.childList.setLen(parent.childList.len - 1) node.parentNode.invalidateCollections() node.parentNode = nil - node.parentElement = nil node.root = nil node.index = -1 if node.nodeType == ELEMENT_NODE: @@ -2795,8 +2797,6 @@ proc insertNode(parent, node, before: Node) = parent.childList[node.index] = node node.root = parent.rootNode node.parentNode = parent - if parent.nodeType == ELEMENT_NODE: - node.parentElement = Element(parent) node.invalidateCollections() parent.invalidateCollections() if node.nodeType == ELEMENT_NODE: |