Source file vdom.ml
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
open Fmlib_js.Base
module Dictionary = Dictionary.Make (String)
type 'msg handlers = 'msg Handler.Virtual.t list Dictionary.t
module Attributes =
struct
type 'msg t = {
styles: string Dictionary.t;
props: Value.t Dictionary.t;
attrs: string Dictionary.t;
handlers: 'msg handlers
}
let of_list (lst: 'msg Attribute.t list): 'msg t =
let open Attribute in
List.fold_left
(fun attrs -> function
| Style (name, value) ->
{
attrs with
styles = Dictionary.add name value attrs.styles
}
| Property (name, value) ->
{
attrs with
props = Dictionary.add name value attrs.props
}
| Attribute (name, value) ->
{
attrs with
attrs = Dictionary.add name value attrs.attrs
}
| Handler (name, handler) ->
{
attrs with
handlers =
Dictionary.set
name
(function
| None -> [handler]
| Some lst -> handler :: lst)
attrs.handlers
}
)
{
styles = Dictionary.empty;
props = Dictionary.empty;
attrs = Dictionary.empty;
handlers = Dictionary.empty;
}
lst
end
type ('msg, 'el) t0 =
| Text of string
| Node of string * 'msg Attributes.t * ('msg, 'el) t1 list
| Node_ns of string * string * 'msg Attributes.t * ('msg, 'el) t1 list
| Keyed of string * 'msg Attributes.t * ('msg, 'el) t1 Dictionary.t
and ('msg, 'el) t1 =
('msg, 'el) t0 * 'el
type 'msg t =
('msg, unit) t1
let text (s: string): 'msg t =
Text s, ()
let node
(tag: string)
(attrs: 'msg Attribute.t list)
(lst: 'msg t list)
: 'msg t
=
Node (tag, Attributes.of_list attrs, lst), ()
let node_ns
(namespace: string)
(tag: string)
(attrs: 'msg Attribute.t list)
(lst: 'msg t list)
: 'msg t
=
Node_ns (namespace, tag, Attributes.of_list attrs, lst), ()
let keyed
(tag: string)
(attrs: 'msg Attribute.t list)
(lst: (string * 'msg t) list)
: 'msg t
=
Keyed (tag, Attributes.of_list attrs, Dictionary.of_list lst), ()
let element: ('msg, 'el) t1 -> 'el =
snd
type ('msg, 'el) operations = {
make_text: string -> 'el;
make_element: string -> 'el list -> 'el;
make_element_ns: string -> string -> 'el list -> 'el;
add_child: 'el -> 'el -> unit;
remove_child: 'el -> 'el -> unit;
replace_child: 'el -> 'el -> 'el -> unit;
remove_children: 'el -> unit;
set_style: 'el -> string -> string -> unit;
set_attribute: 'el -> string -> string -> unit;
set_property: 'el -> string -> Value.t -> unit;
remove_style: 'el -> string -> unit;
remove_attribute: 'el -> string -> unit;
remove_property: 'el -> string -> unit;
set_handlers: 'el -> 'msg handlers -> unit;
update_handlers: 'el -> 'msg handlers -> 'msg handlers -> unit;
}
let add_attributes
(ops: ('msg, 'el) operations)
(attrs: 'msg Attributes.t)
(el: 'el)
: unit
=
Dictionary.(
iter (ops.set_style el) attrs.styles;
iter (ops.set_attribute el) attrs.attrs;
iter (ops.set_property el) attrs.props);
ops.set_handlers el attrs.handlers
let make
(ops: ('msg, 'el) operations)
(vdom: 'msg t)
: ('msg, 'el) t1
=
let rec make vdom =
match vdom with
| Text s, () ->
Text s, ops.make_text s
| Node (tag, attrs, lst), () ->
let combined_children, real_children =
make_children lst
in
let parent = ops.make_element tag real_children
in
add_attributes ops attrs parent;
Node (tag, attrs, combined_children), parent
| Node_ns (namespace, tag, attrs, lst), () ->
let combined_children, real_children =
make_children lst
in
let parent = ops.make_element_ns namespace tag real_children
in
add_attributes ops attrs parent;
Node_ns (namespace, tag, attrs, combined_children), parent
| Keyed (tag, attrs, _), () ->
let combined_children, real_children =
assert false
in
let parent = ops.make_element tag real_children
in
add_attributes ops attrs parent;
Keyed (tag, attrs, combined_children), parent
and make_children (lst: 'msg t list): ('msg, 'el) t1 list * 'el list =
match lst with
| [] ->
[], []
| hd :: tl ->
let _, hd2 as hd = make hd
and tl1, tl2 = make_children tl in
hd :: tl1, hd2 :: tl2
in
make vdom
let update_attributes
(ops: ('msg, 'el) operations)
(par: 'el)
(attrs1: 'msg Attributes.t)
(attrs2: 'msg Attributes.t)
: unit
=
let open Dictionary in
let set = ops.set_style par in
diff
set set (ops.remove_style par)
attrs1.styles attrs2.styles;
let set = ops.set_attribute par in
diff
set set (ops.remove_attribute par)
attrs1.attrs attrs2.attrs;
let set = ops.set_property par in
diff
set set (ops.remove_property par)
attrs1.props attrs2.props;
ops.update_handlers par attrs1.handlers attrs2.handlers
let rec update
(ops: ('msg, 'el) operations)
(vdom: 'msg t)
(dom: ('msg, 'el) t1)
: ('msg, 'el) t1 * bool
=
match vdom, dom with
| (Text s1, ()), (Text s2, _) when s1 = s2 ->
dom, false
| (Node (tag1, attrs1, lst1), ()),
(Node (tag2, attrs2, lst2), par) when tag1 = tag2 ->
update_attributes ops par attrs1 attrs2;
let children = List.rev (update_children ops par lst1 lst2 [])
in
(Node (tag2, attrs1, children), par), false
| (Node_ns (ns1, tag1, attrs1, lst1), ()),
(Node_ns (ns2, tag2, attrs2, lst2), par) when tag1 = tag2 && ns1 = ns2 ->
update_attributes ops par attrs1 attrs2;
let children = List.rev (update_children ops par lst1 lst2 [])
in
(Node_ns (ns2, tag2, attrs1, children), par), false
| (Keyed (tag1, attrs1, d1), ()),
(Keyed (tag2, attrs2, d2), par) when tag1 = tag2 ->
update_attributes ops par attrs1 attrs2;
let children = update_keyed ops par d1 d2
in
(Keyed (tag2, attrs1, children), par), false
| _, _ ->
make ops vdom, true
and update_keyed
(ops: ('msg, 'el) operations)
(par: 'el)
(d1: 'msg t Dictionary.t)
(d2: ('msg, 'el) t1 Dictionary.t)
: ('msg, 'el) t1 Dictionary.t
=
ops.remove_children par;
let d = ref Dictionary.empty
in
Dictionary.iter
(fun key vdom ->
match Dictionary.find_opt key d2 with
| None ->
let (_, el) as dom = make ops vdom in
ops.add_child el par;
d := Dictionary.add key dom !d;
| Some dom ->
let ((_, el) as dom), _ = update ops vdom dom in
ops.add_child el par;
d := Dictionary.add key dom !d;
)
d1;
!d
and update_children
(ops: ('msg, 'el) operations)
(par: 'el)
(lst1: 'msg t list)
(lst2: ('msg, 'el) t1 list)
(nlst2: ('msg, 'el) t1 list)
: ('msg, 'el) t1 list
=
match lst1, lst2 with
| [], [] ->
nlst2
| [], (_, hd) :: tl ->
ops.remove_child hd par;
update_children ops par [] tl nlst2
| hd1 :: tl1, [] ->
let (_, hd11) as hd1 = make ops hd1 in
ops.add_child hd11 par;
update_children ops par tl1 [] (hd1 :: nlst2)
| hd1 :: tl1, ((_, old_el) as hd2) :: tl2 ->
let (_, new_el) as hd2 , created = update ops hd1 hd2 in
if created then
ops.replace_child old_el new_el par;
update_children ops par tl1 tl2 (hd2 :: nlst2)