about summary refs log tree commit diff stats
path: root/403unicode.mu
diff options
context:
space:
mode:
authorKartik Agaram <vc@akkartik.com>2021-01-24 07:51:58 -0800
committerKartik Agaram <vc@akkartik.com>2021-01-24 19:43:23 -0800
commitdba18f7f6f07840bf981d21a9acabd33f651c860 (patch)
tree7c0687889876814e8b29eeaaafc65ab485a23288 /403unicode.mu
parent0e4e8355f0a4dce9cafe2c762e8d38a8b7975a86 (diff)
downloadmu-dba18f7f6f07840bf981d21a9acabd33f651c860.tar.gz
7554 - bug in an error handler
Diffstat (limited to '403unicode.mu')
0 files changed, 0 insertions, 0 deletions
9 ^
8027e52c ^



aea27f52 ^

66b9574b ^
492a9e7c ^
8027e52c ^
ab203acf ^






82fb1f70 ^
ab203acf ^
82fb1f70 ^
66b9574b ^

ab203acf ^
8027e52c ^


b7570a38 ^

8027e52c ^
b7570a38 ^

492a9e7c ^

82fb1f70 ^
8027e52c ^
31cca818 ^

66b9574b ^
cc7eacf7 ^
8430c095 ^
00d7836d ^

8430c095 ^

00d7836d ^

31cca818 ^





8430c095 ^
31cca818 ^




8027e52c ^
66b9574b ^
8027e52c ^



66b9574b ^
8027e52c ^







66b9574b ^
31cca818 ^
8027e52c ^

31cca818 ^



8027e52c ^
















492a9e7c ^

64d80e44 ^
492a9e7c ^


1960de26 ^
8027e52c ^

4a897f27 ^
8027e52c ^

1960de26 ^
8027e52c ^
64d80e44 ^
1960de26 ^
64d80e44 ^
4a897f27 ^
065001ec ^

8027e52c ^


8027e52c ^
4a897f27 ^
8027e52c ^

1960de26 ^
8027e52c ^

492a9e7c ^
8027e52c ^
1960de26 ^


64d80e44 ^
4a897f27 ^
1960de26 ^




64d80e44 ^
1960de26 ^

64d80e44 ^
1960de26 ^
492a9e7c ^
1960de26 ^

8027e52c ^
1960de26 ^




8027e52c ^
1960de26 ^

065001ec ^

1960de26 ^
4a897f27 ^
065001ec ^
8027e52c ^

492a9e7c ^

8027e52c ^


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


                  
                 
               
                 
                      
                    
              



                     

                 
                                                                         
                                           
 






                                                                   
                                
                                               
 

                                                                             
                           


                                        

                                                                   
                                                       

                                                                    

                                                        
                 
 

                                                                            
                                                                    
                                         
                 

                                      

                          

            





                                   
                




                                   
 
                                                      



                                           
                                                                            







                                      
                                                                       
                

                            



                                           
















                                                                                                        

                                                                             
                                                                         


                                             
                                         

                                                   
                                   

                                                         
                                 
                                         
                                                                        
                
                                    
                                      

                      


                                                  
                         
                                  

                  
                                  

                                          
                                                     
                                             


                                         
                             
                                    




                                    
                

                  
                  
                                              
                                         

                                            
           




                                                             
         

                                                  

                                      
                                                          
                                          
                       

                                     

                                    


                                         
import std/base64
import std/streams

import html/catom
import html/dom
import html/enums
import js/domexception
import js/javascript
import js/tojs
import types/blob
import types/formdata
import utils/twtstr

import chame/tags

proc constructEntryList*(form: HTMLFormElement; submitter: Element = nil;
    encoding = "UTF-8"): seq[FormDataEntry]

proc generateBoundary(): string =
  let urandom = newFileStream("/dev/urandom")
  let s = urandom.readStr(32)
  urandom.close()
  # 32 * 4 / 3 (padded) = 44 + prefix string is 22 bytes = 66 bytes
  return "----WebKitFormBoundary" & base64.encode(s)

proc newFormData0*(): FormData =
  return FormData(boundary: generateBoundary())

proc newFormData*(form: HTMLFormElement = nil; submitter: HTMLElement = nil):
    DOMResult[FormData] {.jsctor.} =
  let this = newFormData0()
  if form != nil:
    if submitter != nil:
      if not submitter.isSubmitButton():
        return errDOMException("Submitter must be a submit button",
          "InvalidStateError")
      if FormAssociatedElement(submitter).form != form:
        return errDOMException("Submitter's form owner is not form",
          "InvalidStateError")
    if not form.constructingEntryList:
      this.entries = constructEntryList(form, submitter)
  return ok(this)

#TODO filename should not be allowed for string entries
# in other words, this should be an overloaded function, not just an or type
proc append*[T: string|Blob](this: FormData; name: string; value: T;
    filename = none(string)) {.jsfunc.} =
  when T is Blob:
    let filename = if filename.isSome:
      filename.get
    elif value of WebFile:
      WebFile(value).name
    else:
      "blob"
    this.entries.add(FormDataEntry(
      name: name,
      isstr: false,
      value: value,
      filename: filename
    ))
  else: # string
    this.entries.add(FormDataEntry(
      name: name,
      isstr: true,
      svalue: value
    ))

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(ctx: JSContext; this: FormData; name: string): seq[JSValue]
    {.jsfunc.} =
  for entry in this.entries:
    if entry.name == name:
      if entry.isstr:
        result.add(toJS(ctx, entry.svalue))
      else:
        result.add(toJS(ctx, entry.value))

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
# Warning: we skip the first "constructing entry list" check; the caller must
# do it.
proc constructEntryList*(form: HTMLFormElement; submitter: Element = nil;
    encoding = "UTF-8"): seq[FormDataEntry] =
  assert not form.constructingEntryList
  form.constructingEntryList = true
  var entrylist: seq[FormDataEntry] = @[]
  for field in form.controls:
    if field.findAncestor({TAG_DATALIST}) != nil or
        field.attrb(satDisabled) or
        field.isButton() and Element(field) != submitter:
      continue
    if field of HTMLInputElement:
      let field = HTMLInputElement(field)
      if field.inputType in {itCheckbox, itRadio} and not field.checked:
        continue
      if field.inputType == itImage:
        var name = field.attr(satName)
        if name != "":
          name &= '.'
        entrylist.add((name & 'x', $field.xcoord))
        entrylist.add((name & 'y', $field.ycoord))
        continue
    #TODO custom elements
    let name = field.attr(satName)
    if name == "":
      continue
    if field of HTMLSelectElement:
      let field = HTMLSelectElement(field)
      for option in field.options:
        if option.selected and not option.isDisabled:
          entrylist.add((name, option.value))
    elif field of HTMLInputElement:
      let field = HTMLInputElement(field)
      case field.inputType
      of itCheckbox, itRadio:
        let v = field.attr(satValue)
        let value = if v != "":
          v
        else:
          "on"
        entrylist.add((name, value))
      of itFile:
        #TODO file
        discard
      of itHidden:
        if name.equalsIgnoreCase("_charset_"):
          entrylist.add((name, encoding))
        else:
          entrylist.add((name, field.value))
      else:
        entrylist.add((name, field.value))
    elif field of HTMLButtonElement:
      entrylist.add((name, HTMLButtonElement(field).value))
    elif field of HTMLTextAreaElement:
      entrylist.add((name, HTMLTextAreaElement(field).value))
    else:
      assert false, "Tag type " & $field.tagType &
        " not accounted for in constructEntryList"
    if field of HTMLTextAreaElement or
        field of HTMLInputElement and
        HTMLInputElement(field).inputType in AutoDirInput:
      let dirname = field.attr(satDirname)
      if dirname != "":
        let dir = "ltr" #TODO bidi
        entrylist.add((dirname, dir))
  form.constructingEntryList = false
  return entrylist

proc addFormDataModule*(ctx: JSContext) =
  ctx.registerType(FormData)