about summary refs log tree commit diff stats
path: root/src/xhr
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-06-02 00:36:54 +0200
committerbptato <nincsnevem662@gmail.com>2023-06-05 03:58:21 +0200
commit8027e52cb221c432bed64517015ebf3182e6166d (patch)
tree18991f9e74c8dcfc0ed7439f3bc78a0cfec9b2d6 /src/xhr
parentb3b97465805b7367df461a4b7b830fabaccf3a89 (diff)
downloadchawan-8027e52cb221c432bed64517015ebf3182e6166d.tar.gz
Add support for canvas and multipart
Quite incomplete canvas implementation. Crucially, the layout engine
can't do much with whatever is drawn because it doesn't support images
yet.

I've re-introduced multipart as well, with the FormData API. For the
append function I've also introduced a hack to the JS binding generator
that allows requesting the JSContext pointer in nim procs. Really I
should just fix the union generator thing and add support for overloading.

In conclusion, for now the only thing canvas can be used for is exporting
it as PNG and uploading it somewhere. Also, we now have PNG encoding and
decoding too. (Now if only we had sixels as well...)
Diffstat (limited to 'src/xhr')
-rw-r--r--src/xhr/formdata.nim159
1 files changed, 159 insertions, 0 deletions
diff --git a/src/xhr/formdata.nim b/src/xhr/formdata.nim
new file mode 100644
index 00000000..9b45e7ba
--- /dev/null
+++ b/src/xhr/formdata.nim
@@ -0,0 +1,159 @@
+import html/dom
+import html/tags
+import js/javascript
+import types/blob
+import types/formdata
+import utils/twtstr
+
+proc constructEntryList*(form: HTMLFormElement, submitter: Element = nil,
+    encoding: string = ""): Option[seq[FormDataEntry]]
+
+proc newFormData*(form: HTMLFormElement = nil,
+    submitter: HTMLElement = nil): FormData {.jserr, jsctor.} =
+  let this = FormData()
+  if form != nil:
+    if submitter != nil:
+      if not submitter.isSubmitButton():
+        JS_ERR JS_TypeError, "Submitter must be a submit button"
+      if FormAssociatedElement(submitter).form != form:
+        #TODO should be DOMException
+        JS_ERR JS_TypeError, "InvalidStateError"
+    this.entries = constructEntryList(form, submitter).get(@[])
+  return this
+
+#TODO as jsfunc
+proc append*(this: FormData, name: string, svalue: string, filename = "") =
+  this.entries.add(FormDataEntry(
+    name: name,
+    isstr: true,
+    svalue: svalue,
+    filename: filename
+  ))
+
+proc append*(this: FormData, name: string, value: Blob,
+    filename = "blob") =
+  this.entries.add(FormDataEntry(
+    name: name,
+    isstr: false,
+    value: value,
+    filename: filename
+  ))
+
+#TODO hack
+proc append(this: FormData, name: string, value: JSObject,
+    filename = none(string)) {.jsfunc.} =
+  let blob = fromJS[Blob](value.ctx, value.val)
+  if blob.isSome:
+    this.append(name, blob.get, filename.get("blob"))
+  else:
+    let s = fromJS[string](value.ctx, value.val)
+    # toString should never fail (?)
+    this.append(name, s.get, filename.get(""))
+
+proc delete(this: FormData, name: string) {.jsfunc.} =
+  for i in countdown(this.entries.high, 0):
+    if this.entries[i].name == name:
+      this.entries.delete(i)
+
+proc get(ctx: JSContext, this: FormData, name: string): JSValue {.jsfunc.} =
+  for entry in this.entries:
+    if entry.name == name:
+      if entry.isstr:
+        return toJS(ctx, entry.svalue)
+      else:
+        return toJS(ctx, entry.value)
+  return JS_NULL
+
+proc getAll(this: FormData, name: string): seq[Blob] {.jsfunc.} =
+  for entry in this.entries:
+    if entry.name == name:
+      result.add(entry.value) # may be null
+
+proc add(list: var seq[FormDataEntry], entry: tuple[name, value: string]) =
+  list.add(FormDataEntry(
+    name: entry.name,
+    isstr: true,
+    svalue: entry.value
+  ))
+
+func toNameValuePairs*(list: seq[FormDataEntry]):
+    seq[tuple[name, value: string]] =
+  for entry in list:
+    if entry.isstr:
+      result.add((entry.name, entry.svalue))
+    else:
+      result.add((entry.name, entry.name))
+
+# https://html.spec.whatwg.org/multipage/form-control-infrastructure.html#constructing-the-form-data-set
+proc constructEntryList*(form: HTMLFormElement, submitter: Element = nil,
+    encoding: string = ""): Option[seq[FormDataEntry]] =
+  if form.constructingentrylist:
+    return
+  form.constructingentrylist = true
+
+  var entrylist: seq[FormDataEntry]
+  for field in form.controls:
+    if field.findAncestor({TAG_DATALIST}) != nil or
+        field.attrb("disabled") or
+        field.isButton() and Element(field) != submitter:
+      continue
+
+    if field.tagType == TAG_INPUT:
+      let field = HTMLInputElement(field)
+      if field.inputType == INPUT_IMAGE:
+        let name = if field.attr("name") != "":
+          field.attr("name") & '.'
+        else:
+          ""
+        entrylist.add((name & 'x', $field.xcoord))
+        entrylist.add((name & 'y', $field.ycoord))
+        continue
+
+    #TODO custom elements
+
+    let name = field.attr("name")
+
+    if name == "":
+      continue
+
+    if field.tagType == TAG_SELECT:
+      let field = HTMLSelectElement(field)
+      for option in field.options:
+        if option.selected or option.disabled:
+          entrylist.add((name, option.value))
+    elif field.tagType == TAG_INPUT and HTMLInputElement(field).inputType in {INPUT_CHECKBOX, INPUT_RADIO}:
+      let value = if field.attr("value") != "":
+        field.attr("value")
+      else:
+        "on"
+      entrylist.add((name, value))
+    elif field.tagType == TAG_INPUT and HTMLInputElement(field).inputType == INPUT_FILE:
+      #TODO file
+      discard
+    elif field.tagType == TAG_INPUT and HTMLInputElement(field).inputType == INPUT_HIDDEN and name.equalsIgnoreCase("_charset_"):
+      let charset = if encoding != "":
+        encoding
+      else:
+        "UTF-8"
+      entrylist.add((name, charset))
+    else:
+      case field.tagType
+      of TAG_INPUT:
+        entrylist.add((name, HTMLInputElement(field).value))
+      of TAG_BUTTON:
+        entrylist.add((name, HTMLButtonElement(field).value))
+      of TAG_TEXTAREA:
+        entrylist.add((name, HTMLTextAreaElement(field).value))
+      else: assert false, "Tag type " & $field.tagType & " not accounted for in constructEntryList"
+    if field.tagType == TAG_TEXTAREA or
+        field.tagType == TAG_INPUT and HTMLInputElement(field).inputType in {INPUT_TEXT, INPUT_SEARCH}:
+      if field.attr("dirname") != "":
+        let dirname = field.attr("dirname")
+        let dir = "ltr" #TODO bidi
+        entrylist.add((dirname, dir))
+
+  form.constructingentrylist = false
+  return some(entrylist)
+
+proc addFormDataModule*(ctx: JSContext) =
+  ctx.registerType(FormData)
evision' href='/akkartik/mu/blame/html/061recipe.cc.html?h=main&id=2da43c9462c7b7c1bb78d2f2826b3b97b4874973'>^
4690ce81 ^
d009e158 ^











cd9bb850 ^




















298f8065 ^
d009e158 ^
cd9bb850 ^













44c1aeef ^

d009e158 ^

cd9bb850 ^











d009e158 ^




4690ce81 ^


d009e158 ^

4690ce81 ^
2f02189d ^
d009e158 ^




4690ce81 ^
d009e158 ^
4690ce81 ^
d009e158 ^



298f8065 ^



4690ce81 ^
d009e158 ^









4690ce81 ^
d009e158 ^

4690ce81 ^
d009e158 ^








4690ce81 ^


d009e158 ^
4690ce81 ^
d009e158 ^



4690ce81 ^

d009e158 ^



4690ce81 ^
d009e158 ^
4690ce81 ^
a654e4ec ^
4690ce81 ^








c842d90b ^
d009e158 ^
4690ce81 ^


c842d90b ^
d009e158 ^



4690ce81 ^
0ca56ed8 ^
d009e158 ^

0ca56ed8 ^




4690ce81 ^
44c1aeef ^

d009e158 ^
0ca56ed8 ^
d009e158 ^
44c1aeef ^
d009e158 ^
0ca56ed8 ^






44c1aeef ^


298f8065 ^
44c1aeef ^
d009e158 ^


0ca56ed8 ^
























44c1aeef ^
0ca56ed8 ^
298f8065 ^







2f02189d ^
4690ce81 ^
44c1aeef ^



d009e158 ^




4690ce81 ^
d009e158 ^



4690ce81 ^
d009e158 ^



2f02189d ^
d009e158 ^


4690ce81 ^
d009e158 ^

4690ce81 ^
d009e158 ^



2f02189d ^
d009e158 ^
b2566a84 ^
4690ce81 ^


d009e158 ^

4690ce81 ^



d009e158 ^

4690ce81 ^

d009e158 ^



2f02189d ^
298f8065 ^












2f02189d ^












c842d90b ^
2f02189d ^











d009e158 ^


a654e4ec ^
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