about summary refs log tree commit diff stats
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/html/enums.nim5
-rw-r--r--src/server/buffer.nim26
-rw-r--r--src/xhr/formdata.nim73
3 files changed, 50 insertions, 54 deletions
diff --git a/src/html/enums.nim b/src/html/enums.nim
index baa948a0..6d1a0771 100644
--- a/src/html/enums.nim
+++ b/src/html/enums.nim
@@ -122,3 +122,8 @@ const inputTypeMap = getInputTypeMap()
 
 func inputType*(s: string): InputType =
   return inputTypeMap.getOrDefault(s.toLowerAscii())
+
+const AutoDirInput* = {
+  INPUT_HIDDEN, INPUT_TEXT, INPUT_SEARCH, INPUT_TEL, INPUT_URL, INPUT_EMAIL,
+  INPUT_PASSWORD, INPUT_SUBMIT, INPUT_RESET, INPUT_BUTTON
+}
diff --git a/src/server/buffer.nim b/src/server/buffer.nim
index 7dcca52f..a5858e44 100644
--- a/src/server/buffer.nim
+++ b/src/server/buffer.nim
@@ -332,25 +332,19 @@ func canSubmitOnClick(fae: FormAssociatedElement): bool =
 proc getClickHover(styledNode: StyledNode): string =
   let clickable = styledNode.getClickable()
   if clickable != nil:
-    case clickable.tagType
-    of TAG_A:
+    if clickable of HTMLAnchorElement:
       return HTMLAnchorElement(clickable).href
-    of TAG_INPUT:
+    elif clickable of FormAssociatedElement:
       #TODO this is inefficient and also quite stupid
-      if clickable of FormAssociatedElement:
-        let fae = FormAssociatedElement(clickable)
-        if fae.canSubmitOnClick():
-          let req = fae.form.submitForm(fae)
-          if req.isSome:
-            return $req.get.url
-      return "<input>"
-    of TAG_OPTION:
+      let fae = FormAssociatedElement(clickable)
+      if fae.canSubmitOnClick():
+        let req = fae.form.submitForm(fae)
+        if req.isSome:
+          return $req.get.url
+      return "<" & $clickable.tagType & ">"
+    elif clickable of HTMLOptionElement:
       return "<option>"
-    of TAG_BUTTON:
-      return "<button>"
-    of TAG_TEXTAREA:
-      return "<textarea>"
-    else: discard
+  return ""
 
 func getCursorStyledNode(buffer: Buffer, cursorx, cursory: int): StyledNode =
   let i = buffer.lines[cursory].findFormatN(cursorx) - 1
diff --git a/src/xhr/formdata.nim b/src/xhr/formdata.nim
index 77430509..87fa8dac 100644
--- a/src/xhr/formdata.nim
+++ b/src/xhr/formdata.nim
@@ -108,16 +108,16 @@ proc constructEntryList*(form: HTMLFormElement, submitter: Element = nil,
   if form.constructingentrylist:
     return
   form.constructingentrylist = true
-
-  var entrylist: seq[FormDataEntry]
+  var entrylist: seq[FormDataEntry] = @[]
   for field in form.controls:
     if field.findAncestor({TAG_DATALIST}) != nil or
         field.attrb(atDisabled) or
         field.isButton() and Element(field) != submitter:
       continue
-
-    if field.tagType == TAG_INPUT:
+    if field of HTMLInputElement:
       let field = HTMLInputElement(field)
+      if field.inputType in {INPUT_CHECKBOX, INPUT_RADIO} and not field.checked:
+        continue
       if field.inputType == INPUT_IMAGE:
         var name = field.attr(atName)
         if name != "":
@@ -125,56 +125,53 @@ proc constructEntryList*(form: HTMLFormElement, submitter: Element = nil,
         entrylist.add((name & 'x', $field.xcoord))
         entrylist.add((name & 'y', $field.ycoord))
         continue
-
     #TODO custom elements
-
     let name = field.attr(atName)
-
     if name == "":
       continue
-
-    if field.tagType == TAG_SELECT:
+    if field of HTMLSelectElement:
       let field = HTMLSelectElement(field)
       for option in field.options:
         if option.selected or option.isDisabled:
           entrylist.add((name, option.value))
-    elif field of HTMLInputElement and
-        HTMLInputElement(field).inputType in {INPUT_CHECKBOX, INPUT_RADIO}:
-      let v = field.attr(atValue)
-      let value = if v != "":
-        v
-      else:
-        "on"
-      entrylist.add((name, value))
-    elif field of HTMLInputElement and
-        HTMLInputElement(field).inputType == INPUT_FILE:
-      #TODO file
-      discard
-    elif field of HTMLInputElement and
-        HTMLInputElement(field).inputType == INPUT_HIDDEN and
-        name.equalsIgnoreCase("_charset_"):
-      let charset = if encoding != "":
-        encoding
+    elif field of HTMLInputElement:
+      let field = HTMLInputElement(field)
+      case field.inputType
+      of INPUT_CHECKBOX, INPUT_RADIO:
+        let v = field.attr(atValue)
+        let value = if v != "":
+          v
+        else:
+          "on"
+        entrylist.add((name, value))
+      of INPUT_FILE:
+        #TODO file
+        discard
+      of INPUT_HIDDEN:
+        if name.equalsIgnoreCase("_charset_"):
+          let charset = if encoding != "":
+            encoding
+          else:
+            "UTF-8"
+          entrylist.add((name, charset))
+        else:
+          entrylist.add((name, field.value))
       else:
-        "UTF-8"
-      entrylist.add((name, charset))
+        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:
-      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"
+      assert false, "Tag type " & $field.tagType &
+        " not accounted for in constructEntryList"
     if field of HTMLTextAreaElement or
         field of HTMLInputElement and
-        HTMLInputElement(field).inputType in {INPUT_TEXT, INPUT_SEARCH}:
+        HTMLInputElement(field).inputType in AutoDirInput:
       let dirname = field.attr(atDirname)
       if dirname != "":
         let dir = "ltr" #TODO bidi
         entrylist.add((dirname, dir))
-
   form.constructingentrylist = false
   return some(entrylist)