about summary refs log tree commit diff stats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2023-01-02 02:13:07 +0100
committerbptato <nincsnevem662@gmail.com>2023-01-02 02:13:07 +0100
commitd9edcbddae8da8c9cfad0c3bbe47eac35b55af99 (patch)
treea604d07b22f7f1d3c0d499400d64dedace8ae82f /src/html
parent278e60e1c95069f30adec94362679744b4182251 (diff)
downloadchawan-d9edcbddae8da8c9cfad0c3bbe47eac35b55af99.tar.gz
Add support for <label>
Diffstat (limited to 'src/html')
-rw-r--r--src/html/dom.nim54
1 files changed, 53 insertions, 1 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index 90ec690e..d6820d50 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -303,6 +303,8 @@ type
     form* {.jsget.}: HTMLFormElement
     value* {.jsget.}: string
 
+  HTMLLabelElement* = ref object of HTMLElement
+
 # Forward declarations
 func attrb*(element: Element, s: string): bool
 proc attr*(element: Element, name, value: string)
@@ -404,6 +406,11 @@ iterator elements*(node: Node, tag: TagType): Element {.inline.} =
     if desc.tagType == tag:
       yield desc
 
+iterator elements*(node: Node, tag: set[TagType]): Element {.inline.} =
+  for desc in node.elements:
+    if desc.tagType in tag:
+      yield desc
+
 iterator inputs(form: HTMLFormElement): HTMLInputElement {.inline.} =
   for control in form.controls:
     if control.tagType == TAG_INPUT:
@@ -1136,7 +1143,8 @@ func formmethod*(element: Element): FormMethod =
 
   return FORM_METHOD_GET
 
-func target*(element: Element): string {.jsfunc.} =
+#TODO ??
+func target0*(element: Element): string =
   if element.attrb("target"):
     return element.attr("target")
   for base in element.document.elements(TAG_BASE):
@@ -1144,6 +1152,45 @@ func target*(element: Element): string {.jsfunc.} =
       return base.attr("target")
   return ""
 
+# <base>
+func target(base: HTMLBaseElement): string {.jsfget.} =
+  base.attr("target")
+
+proc target(base: HTMLBaseElement, target: string) {.jsfset.} =
+  base.attr("target", target)
+
+# <anchor>
+func target(anchor: HTMLAnchorElement): string {.jsfget.} =
+  anchor.attr("target")
+
+proc target(anchor: HTMLAnchorElement, target: string) {.jsfset.} =
+  anchor.attr("target", target)
+
+# <label>
+func htmlFor(label: HTMLLabelElement): string {.jsfget.} =
+  label.attr("for")
+
+proc htmlFor(label: HTMLLabelElement, htmlFor: string) {.jsfset.} =
+  label.attr("for", htmlFor)
+
+func control*(label: HTMLLabelElement): FormAssociatedElement {.jsfget.} =
+  let f = label.htmlFor
+  if f != "":
+    let elem = label.document.getElementById(f)
+    #TODO the supported check shouldn't be needed, just labelable
+    if elem.tagType in SupportedFormAssociatedElements and elem.tagType in LabelableElements:
+      return FormAssociatedElement(elem)
+    return nil
+  for elem in label.elements(LabelableElements):
+    if elem.tagType in SupportedFormAssociatedElements: #TODO remove this
+      return FormAssociatedElement(elem)
+    return nil
+
+func form(label: HTMLLabelElement): HTMLFormElement {.jsfget.} =
+  let control = label.control
+  if control != nil:
+    return control.form
+
 func newText(document: Document, data: string): Text =
   return Text(
     nodeType: TEXT_NODE,
@@ -1236,6 +1283,8 @@ func newHTMLElement*(document: Document, tagType: TagType, namespace = Namespace
     result = new(HTMLButtonElement)
   of TAG_TEXTAREA:
     result = new(HTMLTextAreaElement)
+  of TAG_LABEL:
+    result = new(HTMLLabelElement)
   else:
     result = new(HTMLElement)
   result.nodeType = ELEMENT_NODE
@@ -2144,5 +2193,8 @@ proc addDOMModule*(ctx: JSContext) =
   ctx.registerType(HTMLTemplateElement, parent = htmlElementCID)
   ctx.registerType(HTMLUnknownElement, parent = htmlElementCID)
   ctx.registerType(HTMLScriptElement, parent = htmlElementCID)
+  ctx.registerType(HTMLBaseElement, parent = htmlElementCID)
+  ctx.registerType(HTMLAreaElement, parent = htmlElementCID)
   ctx.registerType(HTMLButtonElement, parent = htmlElementCID)
   ctx.registerType(HTMLTextAreaElement, parent = htmlElementCID)
+  ctx.registerType(HTMLLabelElement, parent = htmlElementCID)