about summary refs log tree commit diff stats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-04-20 14:03:35 +0200
committerbptato <nincsnevem662@gmail.com>2024-04-20 14:03:48 +0200
commiteabac7f43867e40cde927ec7b552b011968946dc (patch)
tree28c8f0b2651ea54181231f36ac133bb0623aa45e /src/html
parent2df3a394d96c116301bb4df2fd1f67b8311fddee (diff)
downloadchawan-eabac7f43867e40cde927ec7b552b011968946dc.tar.gz
dom: add isSameNode, isEqualNode
TODO: isEqualNode is not quite correct yet, because we don't sort attrs.
Diffstat (limited to 'src/html')
-rw-r--r--src/html/dom.nim54
1 files changed, 54 insertions, 0 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 4237ebd3..c4f73350 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -3935,6 +3935,60 @@ proc cloneNode(node: Node; deep = false): Node {.jsfunc.} =
   #TODO shadow root
   return node.clone(deep = deep)
 
+func equals(a, b: AttrData): bool =
+  return a.qualifiedName == b.qualifiedName and
+    a.namespace == b.namespace and
+    a.value == b.value
+
+func isEqualNode(node, other: Node): bool {.jsfunc.} =
+  if node.childList.len != other.childList.len:
+    return false
+  if node of DocumentType:
+    if not (other of DocumentType):
+      return false
+    let node = DocumentType(node)
+    let other = DocumentType(other)
+    if node.name != other.name or node.publicId != other.publicId or
+        node.systemId != other.systemId:
+      return false
+  elif node of Element:
+    if not (other of Element):
+      return false
+    let node = Element(node)
+    let other = Element(other)
+    if node.namespace != other.namespace or
+        node.namespacePrefix != other.namespacePrefix or
+        node.localName != other.localName or
+        node.attrs.len != other.attrs.len:
+      return false
+    for i, attr in node.attrs:
+      if not attr.equals(other.attrs[i]):
+        return false
+  elif node of Attr:
+    if not (other of Attr):
+      return false
+    if not Attr(node).data.equals(Attr(other).data):
+      return false
+  elif node of ProcessingInstruction:
+    if not (other of ProcessingInstruction):
+      return false
+    let node = ProcessingInstruction(node)
+    let other = ProcessingInstruction(other)
+    if node.target != other.target or node.data != other.data:
+      return false
+  elif node of CharacterData:
+    if node of Text and not (other of Text) or
+        node of Comment and not (other of Comment):
+      return false
+    return CharacterData(node).data == CharacterData(other).data
+  for i, child in node.childList:
+    if not child.isEqualNode(other.childList[i]):
+      return false
+  true
+
+func isSameNode(node, other: Node): bool {.jsfunc.} =
+  return node == other
+
 # Forward definition hack (these are set in selectors.nim)
 var doqsa*: proc (node: Node; q: string): seq[Element]
 var doqs*: proc (node: Node; q: string): Element
href='#n219'>219 220 221 222 223 224 225 226