diff options
| author | bptato <nincsnevem662@gmail.com> | 2023-12-01 21:58:54 +0100 |
|---|---|---|
| committer | bptato <nincsnevem662@gmail.com> | 2023-12-01 21:58:54 +0100 |
| commit | e2e046ce148d5197a4e5681e5e23bf05647d54b2 (patch) | |
| tree | dfd3191988dd65e40ad34c2a75dd669a763bec39 /src | |
| parent | b7482b23c98ecd9bf60309fe9c06b5539595d492 (diff) | |
| download | chawan-e2e046ce148d5197a4e5681e5e23bf05647d54b2.tar.gz | |
dom: fix `children' being available for all Nodes
It is only defined for Document, Element, DocumentFragment.
Diffstat (limited to 'src')
| -rw-r--r-- | src/html/dom.nim | 23 |
1 files changed, 17 insertions, 6 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim index bd8d735a..0cd61602 100644 --- a/src/html/dom.nim +++ b/src/html/dom.nim @@ -161,7 +161,6 @@ type # (We can't just store pointers, because those may be invalidated by # the JavaScript finalizers.) liveCollections: HashSet[int] - children_cached: HTMLCollection childNodes_cached: NodeList document_internal: Document # not nil @@ -204,6 +203,7 @@ type all_cached: HTMLAllCollection cachedSheets: seq[CSSStylesheet] cachedSheetsInvalid: bool + children_cached: HTMLCollection CharacterData* = ref object of Node data* {.jsget.}: string @@ -219,6 +219,7 @@ type DocumentFragment* = ref object of Node host*: Element + children_cached*: HTMLCollection DocumentType* = ref object of Node name*: string @@ -239,6 +240,7 @@ type hover*: bool invalid*: bool style_cached*: CSSStyleDeclaration + children_cached: HTMLCollection CSSStyleDeclaration* = ref object decls*: seq[CSSDeclaration] @@ -1044,15 +1046,24 @@ func nodeType(node: Node): uint16 {.jsfget.} = func isElement(node: Node): bool = return node.nodeType == ELEMENT_NODE -func children(node: Node): HTMLCollection {.jsfget.} = - if node.children_cached == nil: - node.children_cached = newCollection[HTMLCollection]( - root = node, +template parentNodeChildrenImpl(parentNode: typed) = + if parentNode.children_cached == nil: + parentNode.children_cached = newCollection[HTMLCollection]( + root = parentNode, match = isElement, islive = true, childonly = true ) - return node.children_cached + return parentNode.children_cached + +func children(parentNode: Document): HTMLCollection {.jsfget.} = + parentNodeChildrenImpl(parentNode) + +func children(parentNode: DocumentFragment): HTMLCollection {.jsfget.} = + parentNodeChildrenImpl(parentNode) + +func children(parentNode: Element): HTMLCollection {.jsfget.} = + parentNodeChildrenImpl(parentNode) func childNodes(node: Node): NodeList {.jsfget.} = if node.childNodes_cached == nil: |