about summary refs log tree commit diff stats
path: root/src/html
diff options
context:
space:
mode:
Diffstat (limited to 'src/html')
-rw-r--r--src/html/dom.nim100
-rw-r--r--src/html/script.nim47
-rw-r--r--src/html/xmlhttprequest.nim18
3 files changed, 112 insertions, 53 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 7bd3ff9a..707c35f5 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -903,6 +903,21 @@ func baseURL*(document: Document): URL
 proc delAttr(element: Element; i: int; keep = false)
 proc reflectAttr(element: Element; name: CAtom; value: Option[string])
 
+# Forward declaration hacks
+# set in css/cascade
+var appliesFwdDecl*: proc(mqlist: MediaQueryList; window: Window): bool
+  {.nimcall, noSideEffect.}
+# set in css/match
+var doqsa*: proc (node: Node; q: string): seq[Element] {.nimcall.} = nil
+var doqs*: proc (node: Node; q: string): Element {.nimcall.} = nil
+# set in html/chadombuilder
+var domParseHTMLFragment*: proc(element: Element; s: string): seq[Node]
+  {.nimcall.}
+# set in html/env
+var windowFetch*: proc(window: Window; input: JSValue;
+  init = RequestInit(window: JS_UNDEFINED)): JSResult[FetchPromise]
+  {.nimcall.} = nil
+
 # For now, these are the same; on an API level however, getGlobal is guaranteed
 # to be non-null, while getWindow may return null in the future. (This is in
 # preparation for Worker support.)
@@ -2992,10 +3007,6 @@ proc style*(element: Element): CSSStyleDeclaration {.jsfget.} =
     element.cachedStyle = CSSStyleDeclaration(element: element)
   return element.cachedStyle
 
-# Forward declaration hack
-var appliesFwdDecl*: proc(mqlist: MediaQueryList; window: Window): bool
-  {.nimcall, noSideEffect.}
-
 # see https://html.spec.whatwg.org/multipage/links.html#link-type-stylesheet
 #TODO make this somewhat compliant with ^this
 proc loadResource(window: Window; link: HTMLLinkElement) =
@@ -3811,17 +3822,6 @@ proc markAsReady(element: HTMLScriptElement; res: ScriptResult) =
     element.onReady = nil
   element.delayingTheLoadEvent = false
 
-proc createClassicScript(ctx: JSContext; source: string; baseURL: URL;
-    options: ScriptOptions; mutedErrors = false): Script =
-  let urls = baseURL.serialize(excludepassword = true)
-  let record = compileScript(ctx, source, urls)
-  return Script(
-    record: record,
-    baseURL: baseURL,
-    options: options,
-    mutedErrors: mutedErrors
-  )
-
 type OnCompleteProc = proc(element: HTMLScriptElement, res: ScriptResult)
 
 proc fetchClassicScript(element: HTMLScriptElement; url: URL;
@@ -3843,8 +3843,8 @@ proc fetchClassicScript(element: HTMLScriptElement; url: URL;
   let cs = if cs == CHARSET_UNKNOWN: CHARSET_UTF_8 else: cs
   let source = s.decodeAll(cs)
   response.body.sclose()
-  let script = window.jsctx.createClassicScript(source, url, options, false)
-  element.onComplete(ScriptResult(t: srtScript, script: script))
+  let script = window.jsctx.newClassicScript(source, url, options, false)
+  element.onComplete(script)
 
 #TODO settings object
 proc fetchDescendantsAndLink(element: HTMLScriptElement; script: Script;
@@ -3883,10 +3883,10 @@ proc fetchSingleModule(element: HTMLScriptElement; url: URL;
     destination: RequestDestination; options: ScriptOptions,
     referrer: URL; isTopLevel: bool; onComplete: OnCompleteProc) =
   discard #TODO implement
-  #[
   let moduleType = "javascript"
   #TODO moduleRequest
-  let settings = element.document.window.settings
+  let window = element.document.window
+  let settings = window.settings
   let i = settings.moduleMap.find(url, moduleType)
   if i != -1:
     if settings.moduleMap[i].value.t == srtFetching:
@@ -3894,21 +3894,55 @@ proc fetchSingleModule(element: HTMLScriptElement; url: URL;
       assert false
     element.onComplete(settings.moduleMap[i].value)
     return
-  let destination = fetchDestinationFromModuleType(destination, moduleType)
+  let destination = moduleType.moduleTypeToRequestDest(destination)
   let mode = if destination in {rdWorker, rdSharedworker, rdServiceworker}:
     rmSameOrigin
   else:
     rmCors
   #TODO client
   #TODO initiator type
-  let request = newRequest(
-    url,
-    mode = mode,
-    referrer = referrer,
-    destination = destination
+  let request = JSRequest(
+    request: newRequest(
+      url,
+      referrer = referrer,
+    ),
+    destination: destination,
+    mode: mode
   )
-  discard request #TODO
-  ]#
+  #TODO set up module script request
+  #TODO performFetch
+  let ctx = window.jsctx
+  let v = ctx.toJS(request)
+  let p = window.windowFetch(v)
+  JS_FreeValue(ctx, v)
+  if p.isSome:
+    p.get.then(proc(res: JSResult[Response]) =
+      if res.isNone:
+        let res = ScriptResult(t: srtNull)
+        settings.moduleMap.set(url, moduleType, res, ctx)
+        element.onComplete(res)
+        return
+      let res = res.get
+      let contentType = res.getContentType()
+      let referrerPolicy = res.getReferrerPolicy()
+      res.text().then(proc(s: JSResult[string]) =
+        if s.isNone:
+          let res = ScriptResult(t: srtNull)
+          settings.moduleMap.set(url, moduleType, res, ctx)
+          element.onComplete(res)
+          return
+        if contentType.isJavaScriptType():
+          let res = ctx.newJSModuleScript(s.get, element.document.baseURL,
+            options)
+          if referrerPolicy.isSome:
+            res.script.options.referrerPolicy = referrerPolicy
+          settings.moduleMap.set(url, moduleType, res, ctx)
+          element.onComplete(res)
+        else:
+          #TODO non-JS modules
+          discard
+      )
+    )
 
 proc execute*(element: HTMLScriptElement) =
   let document = element.document
@@ -4042,8 +4076,8 @@ proc prepare*(element: HTMLScriptElement) =
     let baseURL = element.document.baseURL
     if element.ctype == stClassic:
       let ctx = element.document.window.jsctx
-      let script = ctx.createClassicScript(sourceText, baseURL, options)
-      element.markAsReady(ScriptResult(t: srtScript, script: script))
+      let script = ctx.newClassicScript(sourceText, baseURL, options)
+      element.markAsReady(script)
     else:
       #TODO stModule, stImportMap
       element.markAsReady(ScriptResult(t: srtNull))
@@ -4290,10 +4324,6 @@ func isEqualNode(node, other: Node): bool {.jsfunc.} =
 func isSameNode(node, other: Node): bool {.jsfunc.} =
   return node == other
 
-# Forward declaration hack (these are set in selectors.nim)
-var doqsa*: proc (node: Node; q: string): seq[Element] {.nimcall.} = nil
-var doqs*: proc (node: Node; q: string): Element {.nimcall.} = nil
-
 proc querySelectorAll(node: Node; q: string): seq[Element] {.jsfunc.} =
   return doqsa(node, q)
 
@@ -4498,10 +4528,6 @@ proc toBlob(ctx: JSContext; this: HTMLCanvasElement; callback: JSValue;
   )
   return JS_UNDEFINED
 
-# Forward declaration hack
-var domParseHTMLFragment*: proc(element: Element; s: string): seq[Node]
-  {.nimcall.}
-
 # https://w3c.github.io/DOM-Parsing/#dfn-fragment-parsing-algorithm
 proc fragmentParsingAlgorithm*(element: Element; s: string): DocumentFragment =
   #TODO xml
diff --git a/src/html/script.nim b/src/html/script.nim
index ab445516..1cc80c5d 100644
--- a/src/html/script.nim
+++ b/src/html/script.nim
@@ -46,7 +46,7 @@ type
     moduleMap*: ModuleMap
     origin*: Origin
 
-  Script* = object
+  Script* = ref object
     #TODO setings
     baseURL*: URL
     options*: ScriptOptions
@@ -77,6 +77,10 @@ type
 
   ModuleMap* = seq[ModuleMapEntry]
 
+# Forward declaration hack
+# set in html/dom
+var windowConsoleError*: proc(ctx: JSContext; ss: varargs[string]) {.nimcall.}
+
 proc find*(moduleMap: ModuleMap; url: URL; moduleType: string): int =
   let surl = $url
   for i, entry in moduleMap:
@@ -84,15 +88,50 @@ proc find*(moduleMap: ModuleMap; url: URL; moduleType: string): int =
       return i
   return -1
 
-func fetchDestinationFromModuleType*(default: RequestDestination;
-    moduleType: string): RequestDestination =
+proc set*(moduleMap: var ModuleMap; url: URL; moduleType: string;
+    value: ScriptResult; ctx: JSContext) =
+  let i = moduleMap.find(url, moduleType)
+  if i != -1:
+    if moduleMap[i].value.t == srtScript:
+      JS_FreeValue(ctx, moduleMap[i].value.script.record)
+    moduleMap[i].value = value
+  else:
+    moduleMap.add(ModuleMapEntry(key: ($url, moduleType), value: value))
+
+func moduleTypeToRequestDest*(moduleType: string; default: RequestDestination):
+    RequestDestination =
   if moduleType == "json":
     return rdJson
   if moduleType == "css":
     return rdStyle
   return default
 
-var windowConsoleError*: proc(ctx: JSContext; ss: varargs[string]) {.nimcall.}
+proc newClassicScript*(ctx: JSContext; source: string; baseURL: URL;
+    options: ScriptOptions; mutedErrors = false): ScriptResult =
+  let urls = baseURL.serialize(excludepassword = true)
+  let record = ctx.compileScript(source, urls)
+  return ScriptResult(
+    t: srtScript,
+    script: Script(
+      record: record,
+      baseURL: baseURL,
+      options: options,
+      mutedErrors: mutedErrors
+    )
+  )
+
+proc newJSModuleScript*(ctx: JSContext; source: string; baseURL: URL;
+    options: ScriptOptions): ScriptResult =
+  let urls = baseURL.serialize(excludepassword = true)
+  let record = ctx.compileModule(source, urls)
+  return ScriptResult(
+    t: srtScript,
+    script: Script(
+      record: record,
+      baseURL: baseURL,
+      options: options
+    )
+  )
 
 proc logException*(ctx: JSContext) =
   windowConsoleError(ctx, ctx.getExceptionMsg())
diff --git a/src/html/xmlhttprequest.nim b/src/html/xmlhttprequest.nim
index 68039978..cffd2357 100644
--- a/src/html/xmlhttprequest.nim
+++ b/src/html/xmlhttprequest.nim
@@ -196,11 +196,6 @@ proc fireProgressEvent(window: Window; target: EventTarget; name: StaticAtom;
   ))
   discard window.jsctx.dispatch(target, event)
 
-# Forward declaration hack
-var windowFetch*: proc(window: Window; input: JSValue;
-  init = RequestInit(window: JS_UNDEFINED)): JSResult[FetchPromise]
-  {.nimcall.} = nil
-
 proc errorSteps(window: Window; this: XMLHttpRequest; name: StaticAtom) =
   this.readyState = xhrsDone
   this.response = makeNetworkError()
@@ -425,13 +420,14 @@ func getContentType(this: XMLHttpRequest): string =
     return this.contentTypeOverride
   return this.response.getContentType()
 
-proc ptrify(s: var string): pointer =
+proc ptrify(s: var string):
+    tuple[opaque: pointer; p: ptr UncheckedArray[uint8]] =
   if s.len == 0:
-    return nil
+    return (nil, nil)
   var sr = new(string)
   sr[] = move(s)
   GC_ref(sr)
-  return cast[pointer](sr)
+  return (cast[pointer](sr), cast[ptr UncheckedArray[uint8]](addr sr[0]))
 
 proc deallocPtrified(p: pointer) =
   if p != nil:
@@ -453,14 +449,12 @@ proc response(ctx: JSContext; this: XMLHttpRequest): JSValue {.jsfget.} =
     case this.responseType
     of xhrtArraybuffer:
       let len = csize_t(this.received.len)
-      let p = cast[ptr UncheckedArray[uint8]](cstring(this.received))
-      let opaque = this.received.ptrify()
+      let (opaque, p) = this.received.ptrify()
       this.responseObject = JS_NewArrayBuffer(ctx, p, len, abufFree, opaque,
         false)
     of xhrtBlob:
       let len = this.received.len
-      let p = cast[ptr UncheckedArray[uint8]](cstring(this.received))
-      let opaque = this.received.ptrify()
+      let (opaque, p) = this.received.ptrify()
       let blob = newBlob(p, len, this.getContentType(), blobFree, opaque)
       this.responseObject = ctx.toJS(blob)
     of xhrtDocument:
<vc@akkartik.com> 2020-03-21 17:25:52 -0700 committer Kartik Agaram <vc@akkartik.com> 2020-03-21 17:26:31 -0700 6159' href='/akkartik/mu/commit/mu_instructions?h=main&id=fa805850240423ed3aa079270da26839a68c763e'>fa805850 ^
611e9f2e ^
8d32a9aa ^


f9f419af ^
























ccadc6e6 ^










8d32a9aa ^

fa805850 ^

924ed08a ^
606c1ab4 ^
114641e2 ^
be19cb87 ^

114641e2 ^
985f7f79 ^
fee1bbd8 ^
72980059 ^
1696ed83 ^
888b4cd8 ^


1696ed83 ^
72980059 ^
1696ed83 ^

72980059 ^
888b4cd8 ^


1696ed83 ^
72980059 ^
1696ed83 ^

3cf03158 ^

d89af9bd ^
3cf03158 ^
d89af9bd ^
72980059 ^
888b4cd8 ^


1696ed83 ^
fee1bbd8 ^
15676346 ^
d89af9bd ^
72980059 ^
d89af9bd ^

15676346 ^


27b1e19e ^
15676346 ^



















d89af9bd ^
606c1ab4 ^
8b85a07f ^



b3998440 ^
e74050ad ^


b3998440 ^
fa805850 ^
8b85a07f ^
e74050ad ^

5e0ae917 ^







af983921 ^


2715d377 ^

002f2609 ^


af983921 ^





0ef7f1d2 ^
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442