@@ -42,13 +42,10 @@ module Xml = struct
4242 type keyboard_event_handler = Dom_html .keyboardEvent Js .t -> bool
4343 type attrib_k =
4444 | Event of biggest_event_handler
45- | Attr of Dom .attr Js .t
45+ | Attr of Js .js_string Js .t option React .S .t
4646 type attrib = aname * attrib_k
4747
48- let attr name v =
49- let a = Dom_html. document##createAttribute (Js. string name) in
50- a##value < - v;
51- name,Attr a
48+ let attr name v = name,Attr (React.S. const (Some v))
5249
5350 let float_attrib name value : attrib = attr name (js_string_of_float value)
5451 let int_attrib name value = attr name (js_string_of_int value)
@@ -78,9 +75,15 @@ module Xml = struct
7875
7976 let attach_attribs e l =
8077 List. iter (fun (n ,att ) ->
78+ let n = Js. string n in
8179 match att with
82- | Attr a -> ignore(e##setAttributeNode (a))
83- | Event h -> Js.Unsafe. set e (Js. string n) (fun ev -> Js. bool (h ev))
80+ | Attr a ->
81+ (* Note that once we have weak pointers working, we'll need to React.S.retain *)
82+ let _ : unit React.S.t = React.S. map (function
83+ | Some v -> ignore(e##setAttribute (n, v))
84+ | None -> ignore(e##removeAttribute (n))) a
85+ in ()
86+ | Event h -> Js.Unsafe. set e n (fun ev -> Js. bool (h ev))
8487 ) l
8588
8689 let leaf ?(a =[] ) name =
@@ -190,6 +193,7 @@ module Util = struct
190193
191194 let update_children (dom : Dom.node Js.t ) (nodes : Dom.node Js.t t ) =
192195 removeChildren dom;
196+ (* Note that once we have weak pointers working, we'll need to React.S.retain *)
193197 let _s : unit React.S.t = fold (fun () msg -> merge_msg dom msg) nodes ()
194198 in ()
195199end
@@ -209,10 +213,7 @@ module R = struct
209213 type attrib = Xml .attrib
210214
211215 let attr name f s =
212- let a = Dom_html. document##createAttribute (Js. string name) in
213- let _ = Xml_wrap. fmap (fun s -> match f s with
214- | None -> ()
215- | Some v -> a##value < - v) s in
216+ let a = Xml_wrap. fmap f s in
216217 name,Xml. Attr a
217218
218219 let float_attrib name s = attr name (fun f -> Some (js_string_of_float f)) s
@@ -263,6 +264,15 @@ module R = struct
263264
264265 module Svg = Svg_f. MakeWrapped (Xml_wrap )(Xml_wed_svg )
265266 module Html5 = Html5_f. MakeWrapped (Xml_wrap )(Xml_wed )(Svg )
267+ let filter_attrib (name ,a ) on =
268+ match a with
269+ | Xml. Event _ ->
270+ raise (Invalid_argument " filter_attrib not implemented for event handler" )
271+ | Xml. Attr a ->
272+ name,
273+ Xml. Attr
274+ (React.S. l2
275+ (fun on a -> if on then a else None ) on a)
266276end
267277
268278module To_dom = Tyxml_cast. MakeTo (struct
0 commit comments