about summary refs log tree commit diff stats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-02-08 00:38:30 +0100
committerbptato <nincsnevem662@gmail.com>2024-02-08 00:38:30 +0100
commit6faf5cff21f8c1d382ffa605b7aa56be30ff54db (patch)
tree12a4fccdc7141490a5eece9ae313a2666c05b7b7 /src/html
parenteda44e258b719a4d1e50272e1c447fcfcc1d219f (diff)
downloadchawan-6faf5cff21f8c1d382ffa605b7aa56be30ff54db.tar.gz
dom: reduce tagType use
tagType is now a function call, but usually it's enough to just test for
the object type.
Diffstat (limited to 'src/html')
-rw-r--r--src/html/chadombuilder.nim2
-rw-r--r--src/html/dom.nim93
-rw-r--r--src/html/enums.nim6
3 files changed, 39 insertions, 62 deletions
diff --git a/src/html/chadombuilder.nim b/src/html/chadombuilder.nim
index b46a50ba..68038b7e 100644
--- a/src/html/chadombuilder.nim
+++ b/src/html/chadombuilder.nim
@@ -196,7 +196,7 @@ proc associateWithFormImpl(builder: ChaDOMBuilder, element, form,
     intendedParent: Node) =
   if form.inSameTree(intendedParent):
     #TODO remove following test eventually
-    if Element(element).tagType in SupportedFormAssociatedElements:
+    if element of FormAssociatedElement:
       let element = FormAssociatedElement(element)
       element.setForm(HTMLFormElement(form))
       element.parserInserted = true
diff --git a/src/html/dom.nim b/src/html/dom.nim
index e0698781..cb4d71ee 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -231,7 +231,7 @@ type
     dataset {.jsget.}: DOMStringMap
 
   FormAssociatedElement* = ref object of HTMLElement
-    form: HTMLFormElement
+    form*: HTMLFormElement
     parserInserted*: bool
 
   HTMLInputElement* = ref object of FormAssociatedElement
@@ -968,7 +968,7 @@ iterator elements*(node: Node, tag: set[TagType]): Element {.inline.} =
 
 iterator inputs(form: HTMLFormElement): HTMLInputElement {.inline.} =
   for control in form.controls:
-    if control.tagType == TAG_INPUT:
+    if control of HTMLInputElement:
       yield HTMLInputElement(control)
 
 iterator radiogroup(form: HTMLFormElement): HTMLInputElement {.inline.} =
@@ -997,11 +997,11 @@ iterator textNodes*(node: Node): Text {.inline.} =
 
 iterator options*(select: HTMLSelectElement): HTMLOptionElement {.inline.} =
   for child in select.elementList:
-    if child.tagType == TAG_OPTION:
+    if child of HTMLOptionElement:
       yield HTMLOptionElement(child)
-    elif child.tagType == TAG_OPTGROUP:
+    elif child of HTMLOptGroupElement:
       for opt in child.elementList:
-        if opt.tagType == TAG_OPTION:
+        if opt of HTMLOptionElement:
           yield HTMLOptionElement(opt)
 
 proc populateCollection(collection: Collection) =
@@ -1680,26 +1680,10 @@ func scriptingEnabled*(document: Document): bool =
 func scriptingEnabled*(element: Element): bool =
   return element.document.scriptingEnabled
 
-func form*(element: FormAssociatedElement): HTMLFormElement =
-  case element.tagType
-  of TAG_INPUT: return HTMLInputElement(element).form
-  of TAG_SELECT: return HTMLSelectElement(element).form
-  of TAG_BUTTON: return HTMLButtonElement(element).form
-  of TAG_TEXTAREA: return HTMLTextAreaElement(element).form
-  else: assert false
-
-func `form=`*(element: FormAssociatedElement, form: HTMLFormElement) =
-  case element.tagType
-  of TAG_INPUT: HTMLInputElement(element).form = form
-  of TAG_SELECT:  HTMLSelectElement(element).form = form
-  of TAG_BUTTON: HTMLButtonElement(element).form = form
-  of TAG_TEXTAREA: HTMLTextAreaElement(element).form = form
-  else: assert false
-
 func isSubmitButton*(element: Element): bool =
-  if element.tagType == TAG_BUTTON:
+  if element of HTMLButtonElement:
     return element.attr("type") == "submit"
-  elif element.tagType == TAG_INPUT:
+  elif element of HTMLInputElement:
     let element = HTMLInputElement(element)
     return element.inputType in {INPUT_SUBMIT, INPUT_IMAGE}
   return false
@@ -1712,7 +1696,7 @@ func canSubmitImplicitly*(form: HTMLFormElement): bool =
   }
   var found = false
   for control in form.controls:
-    if control.tagType == TAG_INPUT:
+    if control of HTMLInputElement:
       let input = HTMLInputElement(control)
       if input.inputType in BlocksImplicitSubmission:
         if found:
@@ -1775,7 +1759,7 @@ func body*(document: Document): HTMLElement {.jsfget.} =
 
 func select*(option: HTMLOptionElement): HTMLSelectElement =
   for anc in option.ancestors:
-    if anc.tagType == TAG_SELECT:
+    if anc of HTMLSelectElement:
       return HTMLSelectElement(anc)
   return nil
 
@@ -2107,14 +2091,13 @@ func referrerpolicy(element: HTMLScriptElement): Option[ReferrerPolicy] =
 proc sheets*(document: Document): seq[CSSStylesheet] =
   if document.cachedSheetsInvalid:
     document.cachedSheets.setLen(0)
-    for elem in document.html.elements({TAG_STYLE, TAG_LINK}):
-      case elem.tagType
-      of TAG_STYLE:
+    for elem in document.html.descendants:
+      if elem of HTMLStyleElement:
         let style = HTMLStyleElement(elem)
         style.sheet = parseStylesheet(style.textContent, document.factory)
         if style.sheet != nil:
           document.cachedSheets.add(style.sheet)
-      of TAG_LINK:
+      elif elem of HTMLLinkElement:
         let link = HTMLLinkElement(elem)
         if link.sheet != nil:
           document.cachedSheets.add(link.sheet)
@@ -2158,9 +2141,9 @@ func textAreaString*(textarea: HTMLTextAreaElement): string =
       result &= "[]\n"
 
 func isButton*(element: Element): bool =
-  if element.tagType == TAG_BUTTON:
+  if element of HTMLButtonElement:
     return true
-  if element.tagType == TAG_INPUT:
+  if element of HTMLInputElement:
     let element = HTMLInputElement(element)
     return element.inputType in {INPUT_SUBMIT, INPUT_BUTTON, INPUT_RESET, INPUT_IMAGE}
   return false
@@ -2174,7 +2157,7 @@ func action*(element: Element): string =
     if element.form != nil:
       if element.form.attrb("action"):
         return element.form.attr("action")
-  if element.tagType == TAG_FORM:
+  if element of HTMLFormElement:
     return element.attr("action")
   return ""
 
@@ -2187,7 +2170,7 @@ func enctype*(element: Element): FormEncodingType =
       of "text/plain": FORM_ENCODING_TYPE_TEXT_PLAIN
       else: FORM_ENCODING_TYPE_URLENCODED
 
-  if element.tagType == TAG_INPUT:
+  if element of HTMLInputElement:
     let element = HTMLInputElement(element)
     if element.form != nil:
       if element.form.attrb("enctype"):
@@ -2207,7 +2190,7 @@ func parseFormMethod(s: string): FormMethod =
   else: FORM_METHOD_GET
 
 func formmethod*(element: Element): FormMethod =
-  if element.tagType == TAG_FORM:
+  if element of HTMLFormElement:
     # The standard says nothing about this, but this code path is reached
     # on implicit form submission and other browsers seem to agree on this
     # behavior.
@@ -2217,7 +2200,7 @@ func formmethod*(element: Element): FormMethod =
     if element.attrb("formmethod"):
       return parseFormMethod(element.attr("formmethod"))
 
-  if element.tagType in SupportedFormAssociatedElements:
+  if element of FormAssociatedElement:
     let element = FormAssociatedElement(element)
     if element.form != nil:
       if element.form.attrb("method"):
@@ -2231,7 +2214,7 @@ func findAnchor*(document: Document, id: string): Element =
   for child in document.elements:
     if child.id == id:
       return child
-    if child.tagType == TAG_A and child.attr("name") == id:
+    if child of HTMLAnchorElement and child.attr("name") == id:
       return child
 
 # Forward declaration hack
@@ -2310,13 +2293,14 @@ func control*(label: HTMLLabelElement): FormAssociatedElement {.jsfget.} =
   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:
+    if elem of FormAssociatedElement and elem.tagType in LabelableElements:
       return FormAssociatedElement(elem)
     return nil
   for elem in label.elements(LabelableElements):
-    if elem.tagType in SupportedFormAssociatedElements: #TODO remove this
+    if elem of FormAssociatedElement: #TODO remove this
       return FormAssociatedElement(elem)
     return nil
+  return nil
 
 func form(label: HTMLLabelElement): HTMLFormElement {.jsfget.} =
   let control = label.control
@@ -2566,7 +2550,7 @@ func title*(document: Document): string =
 
 # https://html.spec.whatwg.org/multipage/form-elements.html#concept-option-disabled
 func isDisabled*(option: HTMLOptionElement): bool =
-  if option.parentElement.tagType == TAG_OPTGROUP and
+  if option.parentElement of HTMLOptGroupElement and
       option.parentElement.attrb("disabled"):
     return true
   return option.attrb("disabled")
@@ -2915,9 +2899,9 @@ proc remove*(node: Node, suppressObservers: bool) =
   node.parentNode = nil
   node.root = nil
   node.index = -1
-  if node of Element:
-    if Element(node).tagType in {TAG_STYLE, TAG_LINK} and node.document != nil:
-      node.document.cachedSheetsInvalid = true
+  if node.document != nil and (node of HTMLStyleElement or
+      node of HTMLLinkElement):
+    node.document.cachedSheetsInvalid = true
 
   #TODO assigned, shadow root, shadow root again, custom nodes, registered observers
   #TODO not suppress observers => queue tree mutation record
@@ -3020,20 +3004,18 @@ proc resetFormOwner(element: FormAssociatedElement) =
 proc insertionSteps(insertedNode: Node) =
   if insertedNode of Element:
     let element = Element(insertedNode)
-    let tagType = element.tagType
-    case tagType
-    of TAG_OPTION:
+    if element of HTMLOptionElement:
       if element.parentElement != nil:
         let parent = element.parentElement
         var select: HTMLSelectElement
-        if parent.tagType == TAG_SELECT:
+        if parent of HTMLSelectElement:
           select = HTMLSelectElement(parent)
-        elif parent.tagType == TAG_OPTGROUP and parent.parentElement != nil and parent.parentElement.tagType == TAG_SELECT:
+        elif parent.tagType == TAG_OPTGROUP and parent.parentElement != nil and
+            parent.parentElement of HTMLSelectElement:
           select = HTMLSelectElement(parent.parentElement)
         if select != nil:
           select.resetElement()
-    else: discard
-    if tagType in SupportedFormAssociatedElements:
+    if element of FormAssociatedElement:
       let element = FormAssociatedElement(element)
       if element.parserInserted:
         return
@@ -3112,9 +3094,10 @@ proc insertNode(parent, node, before: Node) =
   node.parentNode = parent
   node.invalidateCollections()
   parent.invalidateCollections()
+  if node.document != nil and (node of HTMLStyleElement or
+      node of HTMLLinkElement):
+    node.document.cachedSheetsInvalid = true
   if node of Element:
-    if Element(node).tagType in {TAG_STYLE, TAG_LINK} and node.document != nil:
-      node.document.cachedSheetsInvalid = true
     #TODO shadow root
     insertionSteps(node)
 
@@ -3252,7 +3235,7 @@ proc reset*(form: HTMLFormElement) =
 proc renderBlocking*(element: Element): bool =
   if "render" in element.attr("blocking").split(AsciiWhitespace):
     return true
-  if element.tagType == TAG_SCRIPT:
+  if element of HTMLScriptElement:
     let element = HTMLScriptElement(element)
     if element.ctype == CLASSIC and element.parserDocument != nil and
         not element.attrb("async") and not element.attrb("defer"):
@@ -3608,11 +3591,11 @@ proc clone(node: Node, document = none(Document), deep = false): Node =
       element.namespacePrefix, element.attrs)
     #TODO namespaced attrs?
     # Cloning steps
-    if x.tagType == TAG_SCRIPT:
+    if x of HTMLScriptElement:
       let x = HTMLScriptElement(x)
       let element = HTMLScriptElement(element)
       x.alreadyStarted = element.alreadyStarted
-    elif x.tagType == TAG_INPUT:
+    elif x of HTMLInputElement:
       let x = HTMLInputElement(x)
       let element = HTMLInputElement(element)
       x.value = element.value
@@ -3825,7 +3808,7 @@ proc fragmentParsingAlgorithm(element: Element, s: string): DocumentFragment =
 proc innerHTML(element: Element, s: string) {.jsfset.} =
   #TODO shadow root
   let fragment = fragmentParsingAlgorithm(element, s)
-  let ctx = if element.tagType == TAG_TEMPLATE:
+  let ctx = if element of HTMLTemplateElement:
     HTMLTemplateElement(element).content
   else:
     element
diff --git a/src/html/enums.nim b/src/html/enums.nim
index 67ae4e76..1ef29400 100644
--- a/src/html/enums.nim
+++ b/src/html/enums.nim
@@ -30,12 +30,6 @@ type
     DOCUMENT_FRAGMENT_NODE = 11,
     NOTATION_NODE = 12
 
-
-#TODO support all the other ones
-const SupportedFormAssociatedElements* = {
-  TAG_BUTTON, TAG_INPUT, TAG_SELECT, TAG_TEXTAREA
-}
-
 const InputTypeWithSize* = {
   INPUT_SEARCH, INPUT_TEXT, INPUT_EMAIL, INPUT_PASSWORD, INPUT_URL, INPUT_TEL
 }
+0200 committer bptato <nincsnevem662@gmail.com> 2023-05-21 13:28:33 +0200 Rewrite new Request binding' href='/ahoang/chawan/commit/src/io/request.nim?id=d09319640c4b1e24b937d1fc37a3f8b82052e612'>d0931964 ^
4b260035 ^
bfaf210d ^

c235e638 ^

d9e430c8 ^




4b260035 ^
bfaf210d ^

c235e638 ^

d9e430c8 ^

80b45c0d ^
4b260035 ^
d0931964 ^

d9e430c8 ^


90d10281 ^
d9e430c8 ^
bfaf210d ^










51d83224 ^
d0931964 ^


























e2203257 ^


d0931964 ^
e2203257 ^





d0931964 ^
e2203257 ^

d0931964 ^
e2203257 ^




51d83224 ^
34b90a0b ^

a02c408f ^
5697627a ^

34b90a0b ^







a02c408f ^




7c612d65 ^
a02c408f ^





10d3154e ^
bfaf210d ^






51d83224 ^
d0931964 ^



a02c408f ^
d0931964 ^
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