about summary refs log tree commit diff stats
path: root/src/html
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-04-20 15:00:20 +0200
committerbptato <nincsnevem662@gmail.com>2024-04-20 15:00:20 +0200
commitd0ca9b255b3c3c18f3ac62b836a1f9ed0aa08157 (patch)
tree6a83aca680e6f9dbbff5658b463a8dac1d654f83 /src/html
parente59de8e03cbc9633d8906a8d04fe89638cf31ba1 (diff)
downloadchawan-d0ca9b255b3c3c18f3ac62b836a1f9ed0aa08157.tar.gz
dom: sort attributes
needed for isEqualNode to work correctly
Diffstat (limited to 'src/html')
-rw-r--r--src/html/dom.nim51
1 files changed, 30 insertions, 21 deletions
diff --git a/src/html/dom.nim b/src/html/dom.nim
index c4f73350..417237bb 100644
--- a/src/html/dom.nim
+++ b/src/html/dom.nim
@@ -1,3 +1,4 @@
+import std/algorithm
 import std/deques
 import std/math
 import std/options
@@ -219,7 +220,7 @@ type
     id*: CAtom
     name*: CAtom
     classList* {.jsget.}: DOMTokenList
-    attrs: seq[AttrData]
+    attrs: seq[AttrData] # sorted by int(qualifiedName)
     attributesInternal: NamedNodeMap
     hover*: bool
     invalid*: bool
@@ -2503,8 +2504,7 @@ func newComment(ctx: JSContext; data: string = ""): Comment {.jsctor.} =
 
 #TODO custom elements
 proc newHTMLElement*(document: Document; localName: CAtom;
-    namespace = Namespace.HTML; prefix = NO_PREFIX;
-    attrs = newSeq[AttrData]()): HTMLElement =
+    namespace = Namespace.HTML; prefix = NO_PREFIX): HTMLElement =
   let tagType = document.toTagType(localName)
   case tagType
   of TAG_INPUT:
@@ -2588,13 +2588,10 @@ proc newHTMLElement*(document: Document; localName: CAtom;
   result.classList = DOMTokenList(element: result, localName: localName)
   result.index = -1
   result.dataset = DOMStringMap(target: result)
-  result.attrs = attrs
 
-proc newHTMLElement*(document: Document; tagType: TagType;
-    namespace = Namespace.HTML; prefix = NO_PREFIX;
-    attrs = newSeq[AttrData]()): HTMLElement =
+proc newHTMLElement*(document: Document; tagType: TagType): HTMLElement =
   let localName = document.toAtom(tagType)
-  return document.newHTMLElement(localName, namespace, prefix, attrs)
+  return document.newHTMLElement(localName, Namespace.HTML, NO_PREFIX)
 
 func newDocument*(factory: CAtomFactory): Document =
   assert factory != nil
@@ -2961,19 +2958,31 @@ proc reflectAttrs(element: Element; name: CAtom; value: string) =
         window.loadResource(image)
   else: discard
 
+func cmpAttrName(a: AttrData; b: CAtom): int =
+  return cmp(int(a.qualifiedName), int(b))
+
+# Returns the attr index if found, or the negation - 1 of an upper bound
+# (where a new attr with the passed name may be inserted).
+func findAttrOrNext(element: Element; qualName: CAtom): int =
+  for i, data in element.attrs:
+    if data.qualifiedName == qualName:
+      return i
+    if int(data.qualifiedName) > int(qualName):
+      return -(i + 1)
+  return -(element.attrs.len + 1)
+
 proc attr*(element: Element; name: CAtom; value: string) =
-  let i = element.findAttr(name)
-  if i != -1:
+  let i = element.findAttrOrNext(name)
+  if i >= 0:
     element.attrs[i].value = value
     element.invalidateCollections()
     element.invalid = true
   else:
-    #TODO sort?
-    element.attrs.add(AttrData(
+    element.attrs.insert(AttrData(
       qualifiedName: name,
       localName: name,
       value: value
-    ))
+    ), -(i + 1))
   element.reflectAttrs(name, value)
 
 proc attr*(element: Element; name: StaticAtom; value: string) =
@@ -3000,14 +3009,13 @@ proc attrns*(element: Element; localName: CAtom; prefix: NamespacePrefix;
     element.invalidateCollections()
     element.invalid = true
   else:
-    #TODO sort?
-    element.attrs.add(AttrData(
+    element.attrs.insert(AttrData(
       prefix: prefixAtom,
       localName: localName,
       qualifiedName: qualifiedName,
       namespace: namespace,
       value: value
-    ))
+    ), element.attrs.upperBound(qualifiedName, cmpAttrName))
   element.reflectAttrs(qualifiedName, value)
 
 proc attrl(element: Element; name: StaticAtom; value: int32) =
@@ -3821,15 +3829,15 @@ proc createHTMLDocument(ctx: JSContext; implementation: ptr DOMImplementation;
   let doc = newDocument(ctx)
   doc.contentType = "text/html"
   doc.append(doc.newDocumentType("html", "", ""))
-  let html = doc.newHTMLElement(TAG_HTML, Namespace.HTML)
+  let html = doc.newHTMLElement(TAG_HTML)
   doc.append(html)
-  let head = doc.newHTMLElement(TAG_HEAD, Namespace.HTML)
+  let head = doc.newHTMLElement(TAG_HEAD)
   html.append(head)
   if title.isSome:
-    let titleElement = doc.newHTMLElement(TAG_TITLE, Namespace.HTML)
+    let titleElement = doc.newHTMLElement(TAG_TITLE)
     titleElement.append(doc.newText(title.get))
     head.append(titleElement)
-  html.append(doc.newHTMLElement(TAG_BODY, Namespace.HTML))
+  html.append(doc.newHTMLElement(TAG_BODY))
   #TODO set origin
   return doc
 
@@ -3862,7 +3870,8 @@ proc clone(node: Node; document = none(Document), deep = false): Node =
     #TODO is value
     let element = Element(node)
     let x = document.newHTMLElement(element.localName, element.namespace,
-      element.namespacePrefix, element.attrs)
+      element.namespacePrefix)
+    x.attrs = element.attrs
     #TODO namespaced attrs?
     # Cloning steps
     if x of HTMLScriptElement:
evem662@gmail.com> 2022-09-12 00:30:21 +0200 Add JS binding generation' href='/ahoang/chawan/commit/src/bindings/quickjs.nim?id=51ea622d58bfca19212fac1800cfb033bb85ec39'>51ea622d ^
bfaf210d ^
























51ea622d ^




bfaf210d ^



51ea622d ^














cea4aa03 ^
e780de79 ^





51ea622d ^



10d3154e ^

51ea622d ^

















































e780de79 ^


















51ea622d ^










e780de79 ^
51ea622d ^










51d83224 ^
51ea622d ^







51ea622d ^




10d3154e ^



51ea622d ^





10d3154e ^












51ea622d ^
51d83224 ^
51ea622d ^






bd12a8be ^
e780de79 ^
51ea622d ^


e780de79 ^
51ea622d ^

e780de79 ^
51d83224 ^
51ea622d ^


51d83224 ^
80b45c0d ^
51ea622d ^


3803b94f ^
51ea622d ^






e780de79 ^
51ea622d ^




51ea622d ^

10d3154e ^
51ea622d ^







7eee2e1f ^
51ea622d ^


a04485f2 ^
51ea622d ^
51d83224 ^


51ea622d ^


e780de79 ^
51ea622d ^
18350159 ^
51ea622d ^
bfaf210d ^
51ea622d ^




51d83224 ^
51ea622d ^




















e780de79 ^
51ea622d ^


e780de79 ^
51ea622d ^











e780de79 ^
51ea622d ^

e780de79 ^
51ea622d ^


bfaf210d ^
51ea622d ^




e780de79 ^
80b45c0d ^



51ea622d ^

e780de79 ^
51ea622d ^

6f7bcc54 ^
51ea622d ^

3803b94f ^
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434