aboutsummaryrefslogtreecommitdiffstats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2022-01-24 15:53:11 +0100
committerbptato <nincsnevem662@gmail.com>2022-01-24 15:53:11 +0100
commit06696ec0a2c7486a35550bfb9a6f6caba439d0a2 (patch)
tree4c3f539e011e0ad14b452eb6ccc0a96eb8fa69ba /src/html
parent0b40abbaa7fdfe6bd0150fd25456e7207d58ba1f (diff)
downloadchawan-06696ec0a2c7486a35550bfb9a6f6caba439d0a2.tar.gz
Fix node to cell mapping
Diffstat (limited to 'src/html')
-rw-r--r--src/html/dom.nim37
1 files changed, 29 insertions, 8 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 5537491c..c3506a72 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -129,6 +129,30 @@ iterator textNodes*(node: Node): Text {.inline.} =
if node.nodeType == TEXT_NODE:
yield Text(node)
+# Returns the node's ancestors
+iterator ancestors*(node: Node): Element {.inline.} =
+ var element = node.parentElement
+ while element != nil:
+ yield element
+ element = element.parentElement
+
+# Returns the node itself and its ancestors
+iterator branch*(node: Node): Node {.inline.} =
+ var node = node
+ while node != nil:
+ yield node
+ node = node.parentElement
+
+# a == b or b in a's ancestors
+func contains*(a, b: Node): bool =
+ for node in a.branch:
+ if node == b: return true
+ return false
+
+func branch*(node: Node): seq[Node] =
+ for node in node.branch:
+ result.add(node)
+
func firstChild(node: Node): Node =
if node.childNodes.len == 0:
return nil
@@ -223,13 +247,10 @@ func toInputType*(str: string): InputType =
of "week": INPUT_WEEK
else: INPUT_UNKNOWN
-func ancestor(node: Node, tagTypes: set[TagType]): Element =
- var elem = node.parentElement
- while elem != nil:
- if elem.tagType in tagTypes:
- return elem
-
- elem = elem.parentElement
+func findAncestor*(node: Node, tagTypes: set[TagType]): Element =
+ for element in node.ancestors:
+ if element.tagType in tagTypes:
+ return element
return nil
func attr*(element: Element, s: string): string =
@@ -247,7 +268,7 @@ proc applyOrdinal*(elem: HTMLLIElement) =
if val.issome:
elem.ordinalvalue = val.get
else:
- let owner = elem.ancestor({TAG_OL, TAG_UL, TAG_MENU})
+ let owner = elem.findAncestor({TAG_OL, TAG_UL, TAG_MENU})
if owner == nil:
elem.ordinalvalue = 1
else: