Skip to content

Commit d65824f

Browse files
committed
Merge pull request #345 from ocsigen/R_filter_attrib
Lib: fix #333
2 parents ce660de + c027ecd commit d65824f

File tree

2 files changed

+22
-11
lines changed

2 files changed

+22
-11
lines changed

lib/tyxml/tyxml_js.ml

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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 ()
195199
end
@@ -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)
266276
end
267277

268278
module To_dom = Tyxml_cast.MakeTo(struct

lib/tyxml/tyxml_js.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ module R : sig
6363
module Html5 : Html5_sigs.MakeWrapped(Xml_wrap)(Xml)(Svg).T
6464
with type +'a elt = 'a Html5.elt
6565
and type +'a attrib = 'a Html5.attrib
66+
val filter_attrib : 'a Html5.attrib -> bool React.signal -> 'a Html5.attrib
6667
end
6768

6869
module To_dom : Tyxml_cast_sigs.TO with type 'a elt = 'a Html5.elt

0 commit comments

Comments
 (0)