Skip to content

Commit c26d400

Browse files
committed
Lib: fix #333
1 parent ce660de commit c26d400

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

lib/tyxml/tyxml_js.ml

Lines changed: 19 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,14 @@ 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+
let _ : unit React.S.t = React.S.map (function
82+
| Some v -> ignore(e##setAttribute(n, v))
83+
| None -> ignore(e##removeAttribute(n))) a
84+
in ()
85+
| Event h -> Js.Unsafe.set e n (fun ev -> Js.bool (h ev))
8486
) l
8587

8688
let leaf ?(a=[]) name =
@@ -209,10 +211,7 @@ module R = struct
209211
type attrib = Xml.attrib
210212

211213
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
214+
let a = Xml_wrap.fmap f s in
216215
name,Xml.Attr a
217216

218217
let float_attrib name s = attr name (fun f -> Some (js_string_of_float f)) s
@@ -263,6 +262,15 @@ module R = struct
263262

264263
module Svg = Svg_f.MakeWrapped(Xml_wrap)(Xml_wed_svg)
265264
module Html5 = Html5_f.MakeWrapped(Xml_wrap)(Xml_wed)(Svg)
265+
let filter_attrib (name,a) on =
266+
match a with
267+
| Xml.Event _ ->
268+
raise (Invalid_argument "filter_attrib not implemented for event handler")
269+
| Xml.Attr a ->
270+
name,
271+
Xml.Attr
272+
(React.S.l2
273+
(fun on a -> if on then a else None) on a)
266274
end
267275

268276
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)