about summary refs log tree commit diff stats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-08 16:13:49 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-08 16:14:35 +0100
commita8fe479d879a3819075d28c048c48d6455832fe7 (patch)
treecc1e494b6122a3816c064623d3f74a2e14eefc74 /src/html
parentf8382a3db564fed31c3d6e1cc0731fe6a345acfe (diff)
downloadchawan-a8fe479d879a3819075d28c048c48d6455832fe7.tar.gz
dom: reduce todos
* enumize insertAdjacentHTML position
* un-extern attrs
Diffstat (limited to 'src/html')
-rw-r--r--src/html/dom.nim30
1 files changed, 20 insertions, 10 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 34696ad3..cdcb1dbf 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -215,7 +215,7 @@ type
     id*: CAtom
     name*: CAtom
     classList* {.jsget.}: DOMTokenList
-    attrs*: seq[AttrData] #TODO TODO TODO unextern
+    attrs: seq[AttrData]
     attributesInternal: NamedNodeMap
     hover*: bool
     invalid*: bool
@@ -3892,20 +3892,30 @@ proc outerHTML(element: Element, s: string): Err[DOMException] {.jsfset.} =
   let fragment = fragmentParsingAlgorithm(parent, s)
   return parent.replace(element, fragment)
 
+type InsertAdjacentPosition = enum
+  iapBeforeBegin = "beforebegin"
+  iapAfterEnd = "afterend"
+  iapAfterBegin = "afterbegin"
+  iapBeforeEnd = "beforeend"
+
+func parseInsertAdjacentPosition(s: string): DOMResult[InsertAdjacentPosition] =
+  for iap in InsertAdjacentPosition.low .. InsertAdjacentPosition.high:
+    if ($iap).equalsIgnoreCase(s):
+      return ok(iap)
+  return errDOMException("Invalid position", "SyntaxError")
+
 # https://w3c.github.io/DOM-Parsing/#dom-element-insertadjacenthtml
 proc insertAdjacentHTML(element: Element, position, text: string):
     Err[DOMException] {.jsfunc.} =
-  #TODO enumize position
+  let position = ?parseInsertAdjacentPosition(position)
   let ctx0 = case position
-  of "beforebegin", "afterend":
+  of iapBeforeBegin, iapAfterEnd:
     if element.parentNode of Document or element.parentNode == nil:
       return errDOMException("Parent is not a valid element",
         "NoModificationAllowedError")
     element.parentNode
-  of "afterbegin", "beforeend":
+  of iapAfterBegin, iapBeforeEnd:
     Node(element)
-  else:
-    return errDOMException("Invalid position", "SyntaxError")
   let document = ctx0.document
   let ctx = if not (ctx0 of Element) or not document.isxml or
       Element(ctx0).namespace == Namespace.HTML:
@@ -3914,13 +3924,13 @@ proc insertAdjacentHTML(element: Element, position, text: string):
     Element(ctx0)
   let fragment = ctx.fragmentParsingAlgorithm(text)
   case position
-  of "beforebegin":
+  of iapBeforeBegin:
     ctx.parentNode.insert(fragment, ctx)
-  of "afterbegin":
+  of iapAfterBegin:
     ctx.insert(fragment, ctx.firstChild)
-  of "beforeend":
+  of iapBeforeEnd:
     ctx.append(fragment)
-  of "afterend":
+  of iapAfterEnd:
     ctx.parentNode.insert(fragment, ctx.nextSibling)
 
 proc registerElements(ctx: JSContext, nodeCID: JSClassID) =