about summary refs log tree commit diff stats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-06-29 20:40:35 +0200
committerbptato <nincsnevem662@gmail.com>2023-06-29 20:40:35 +0200
commita7b784589a6667865669d54db801e975ad3d5cd1 (patch)
tree7cbbd4faf614f5d306c69e73f2291558bb9820df /src/html
parent6aa9ec1c15a3a4b17f6283d605dd116e62d7ce64 (diff)
downloadchawan-a7b784589a6667865669d54db801e975ad3d5cd1.tar.gz
dom: synchronously load scripts
fetch() does not work here because we can only resume parsing after
the script has been executed
Diffstat (limited to 'src/html')
-rw-r--r--src/html/dom.nim30
1 files changed, 13 insertions, 17 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 727eab77..048e40af 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -18,7 +18,6 @@ import img/painter
 import img/path
 import img/png
 import io/loader
-import io/promise
 import io/request
 import io/window
 import js/exception
@@ -2540,23 +2539,20 @@ proc fetchClassicScript(element: HTMLScriptElement, url: URL,
   else:
     let loader = element.document.window.loader
     if loader.isSome:
+      let loader = loader.get
       let request = createPotentialCORSRequest(url, RequestDestination.SCRIPT, cors)
-      loader.get.fetch(request).then(proc(r: Result[Response, JSError]): auto =
-        if r.isErr or r.get.body == nil:
-          element.onComplete(ScriptResult(t: RESULT_NULL))
-        else:
-          #TODO use charset from content-type
-          #TODO text() should decode
-          return r.get.text()
-      ).then(proc(s: JSResult[string]) =
-        if s.isErr:
-          return
-        let ss = newStringStream(s.get) #TODO unnecessary copy
-        let cs = if cs == CHARSET_UNKNOWN: CHARSET_UTF_8 else: cs
-        let source = newDecoderStream(ss, cs = cs).readAll()
-        #TODO use response url
-        let script = createClassicScript(source, url, options, false)
-        element.markAsReady(ScriptResult(t: RESULT_SCRIPT, script: script)))
+      let response = loader.doRequest(request)
+      if response.res != 0:
+        element.onComplete(ScriptResult(t: RESULT_NULL))
+        return
+      let cs = if cs == CHARSET_UNKNOWN:
+        CHARSET_UTF_8
+      else:
+        cs
+      let decoder = newDecoderStream(response.body, cs = cs)
+      let source = decoder.readAll()
+      let script = createClassicScript(source, url, options, false)
+      element.markAsReady(ScriptResult(t: RESULT_SCRIPT, script: script))
 
 proc log*(console: console, ss: varargs[string]) {.jsfunc.} =
   var s = ""