about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-11-10 18:26:26 +0100
committerbptato <nincsnevem662@gmail.com>2024-11-10 18:26:26 +0100
commit83975d1e0bc08d99ca1bf1353c663e87279e7abd (patch)
tree808ad6582f80b75a825a617b1cf59fbc8deebc93 /src
parentdb250b08282de0ca06be4653ac3fdaac423a2b43 (diff)
downloadchawan-83975d1e0bc08d99ca1bf1353c663e87279e7abd.tar.gz
dom: add replaceChild, requestAnimationFrame
Diffstat (limited to 'src')
-rw-r--r--src/html/dom.nim6
-rw-r--r--src/html/env.nim9
-rw-r--r--src/html/event.nim7
-rw-r--r--src/js/timeout.nim2
4 files changed, 17 insertions, 7 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 2f285899..c6b14c27 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -3906,8 +3906,6 @@ proc appendChild(parent, node: Node): DOMResult[Node] {.jsfunc.} =
 proc append*(parent, node: Node) =
   discard parent.appendChild(node)
 
-#TODO replaceChild
-
 proc removeChild(parent, node: Node): DOMResult[Node] {.jsfunc.} =
   if node.parentNode != parent:
     return errDOMException("Node is not a child of parent", "NotFoundError")
@@ -3981,6 +3979,10 @@ proc replaceAll(parent, node: Node) =
       parent.append(node)
   #TODO tree mutation record
 
+proc replaceChild(parent, node, child: Node): DOMResult[Node] {.jsfunc.} =
+  ?parent.replace(child, node)
+  return ok(child)
+
 proc createTextNode*(document: Document; data: string): Text {.jsfunc.} =
   return newText(document, data)
 
diff --git a/src/html/env.nim b/src/html/env.nim
index 8678bdcc..b8c4fc31 100644
--- a/src/html/env.nim
+++ b/src/html/env.nim
@@ -255,6 +255,15 @@ proc btoa(ctx: JSContext; window: Window; data: JSValue): JSValue
 proc alert(window: Window; s: string) {.jsfunc.} =
   window.console.error(s)
 
+proc requestAnimationFrame(ctx: JSContext; window: Window; callback: JSValue):
+    JSValue {.jsfunc.} =
+  if not JS_IsFunction(ctx, callback):
+    return JS_ThrowTypeError(ctx, "callback is not a function")
+  let handler = ctx.newFunction(["callback"], """
+callback(new Event("").timeStamp);
+""")
+  return ctx.toJS(window.setTimeout(handler, 0, callback))
+
 proc getComputedStyle(window: Window; element: Element;
     pseudoElt = none(string)): JSResult[CSSStyleDeclaration] {.jsfunc.} =
   #TODO implement this properly
diff --git a/src/html/event.nim b/src/html/event.nim
index 247a88a4..61885b57 100644
--- a/src/html/event.nim
+++ b/src/html/event.nim
@@ -1,10 +1,10 @@
 import std/math
 import std/options
-import std/times
 
 import html/catom
 import html/script
 import js/domexception
+import js/timeout
 import monoucha/fromjs
 import monoucha/javascript
 import monoucha/jserror
@@ -83,9 +83,8 @@ type
 # Event
 proc innerEventCreationSteps*(event: Event; eventInitDict: EventInit) =
   event.flags = {efInitialized}
-  #TODO this is probably incorrect?
-  # I think it measures the time since the first fork. not sure though
-  event.timeStamp = round(cpuTime())
+  #TODO this should measure time starting from when the script was started.
+  event.timeStamp = float64(getUnixMillis())
   event.bubbles = eventInitDict.bubbles
   event.cancelable = eventInitDict.cancelable
   if eventInitDict.composed:
diff --git a/src/js/timeout.nim b/src/js/timeout.nim
index f996a0fe..6c905836 100644
--- a/src/js/timeout.nim
+++ b/src/js/timeout.nim
@@ -59,7 +59,7 @@ proc clearTimeout*(state: var TimeoutState; id: int32) =
       entry.dead = true
       break
 
-proc getUnixMillis(): int64 =
+proc getUnixMillis*(): int64 =
   let now = getTime()
   return now.toUnix() * 1000 + now.nanosecond div 1_000_000