about summary refs log tree commit diff stats
path: root/src/buffer/buffer.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-06-20 01:23:50 +0200
committerbptato <nincsnevem662@gmail.com>2023-06-20 01:23:50 +0200
commita8f218df98677bdc76c15d2a025b932094b08629 (patch)
treeddfb1d43bd04a0fcabec06d37c1fce36384b37c4 /src/buffer/buffer.nim
parentc2aa85449f7540037cf67a8725053325ab7a3912 (diff)
downloadchawan-a8f218df98677bdc76c15d2a025b932094b08629.tar.gz
Do not allow text() twice, etc...
Diffstat (limited to 'src/buffer/buffer.nim')
-rw-r--r--src/buffer/buffer.nim31
1 files changed, 17 insertions, 14 deletions
diff --git a/src/buffer/buffer.nim b/src/buffer/buffer.nim
index e8f8eaaa..90505c34 100644
--- a/src/buffer/buffer.nim
+++ b/src/buffer/buffer.nim
@@ -3,14 +3,12 @@ import nativesockets
 import net
 import options
 import os
+import posix
 import selectors
 import streams
 import tables
 import unicode
 
-when defined(posix):
-  import posix
-
 import buffer/cell
 import config/config
 import css/cascade
@@ -34,6 +32,7 @@ import io/window
 import ips/serialize
 import ips/serversocket
 import ips/socketstream
+import js/exception
 import js/javascript
 import js/regex
 import js/timeout
@@ -562,15 +561,15 @@ proc loadResource(buffer: Buffer, elem: HTMLLinkElement): EmptyPromise =
       let media = parseMediaQueryList(cvals)
       if not media.applies(document.window): return
     return buffer.loader.fetch(newRequest(url))
-      .then(proc(res: Result[Response, JSError]): Opt[Promise[string]] =
+      .then(proc(res: Result[Response, JSError]): Promise[JSResult[string]] =
         if res.isOk:
           let res = res.get
           #TODO we should use ReadableStreams for this (which would allow us to
           # parse CSS asynchronously)
           if res.contenttype == "text/css":
-            return ok(res.text())
+            return res.text()
           res.unregisterFun()
-      ).then(proc(s: Opt[string]) =
+      ).then(proc(s: JSResult[string]) =
         if s.isOk:
           #TODO this is extremely inefficient, and text() should return
           # utf8 anyways
@@ -588,14 +587,18 @@ proc loadResource(buffer: Buffer, elem: HTMLImageElement): EmptyPromise =
   if url.isSome:
     let url = url.get
     return buffer.loader.fetch(newRequest(url))
-      .then(proc(res: Result[Response, JSError]): Promise[string] =
-        if res.isOk:
-          let res = res.get
-          if res.contenttype == "image/png":
-            #TODO using text() for PNG is wrong
-            return res.text()
-    ).then(proc(pngData: string) =
-      elem.bitmap = fromPNG(toOpenArrayByte(pngData, 0, pngData.high)))
+      .then(proc(res: Result[Response, JSError]): Promise[JSResult[string]] =
+        if res.isErr:
+          return
+        let res = res.get
+        if res.contenttype == "image/png":
+          #TODO using text() for PNG is wrong
+          return res.text()
+      ).then(proc(pngData: JSResult[string]) =
+        if pngData.isErr:
+          return
+        let pngData = pngData.get
+        elem.bitmap = fromPNG(toOpenArrayByte(pngData, 0, pngData.high)))
 
 proc loadResources(buffer: Buffer): EmptyPromise =
   let document = buffer.document