Skip to content

Commit 48cb3cc

Browse files
committed
Revert some of #1769 for compatibility
1 parent 7c6c209 commit 48cb3cc

File tree

4 files changed

+34
-16
lines changed

4 files changed

+34
-16
lines changed

examples/hyperbolic/hypertree.ml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -543,14 +543,14 @@ let default_language () =
543543

544544
let language =
545545
ref
546-
(Js.Opt.get
547-
(Html.window##.localStorage##getItem (Js.string "hyp_lang"))
548-
default_language)
546+
(Js.Optdef.case Html.window##.localStorage default_language (fun st ->
547+
Js.Opt.get (st##getItem (Js.string "hyp_lang")) default_language))
549548

550549
let _ = Console.console##log !language
551550

552551
let set_language lang =
553-
Html.window##.localStorage##setItem (Js.string "hyp_lang") lang;
552+
Js.Optdef.iter Html.window##.localStorage (fun st ->
553+
st##setItem (Js.string "hyp_lang") lang);
554554
language := lang
555555

556556
let load_messages () =

lib/js_of_ocaml/dom_html.ml

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,9 @@ and keyboardEvent = object
341341

342342
method location : int readonly_prop
343343

344-
method key : js_string t readonly_prop
344+
method key : js_string t optdef readonly_prop
345345

346-
method code : js_string t readonly_prop
346+
method code : js_string t optdef readonly_prop
347347

348348
method isComposing : bool t readonly_prop
349349

@@ -2351,9 +2351,9 @@ class type window = object
23512351

23522352
method scrollBy : number_t -> number_t -> unit meth
23532353

2354-
method sessionStorage : storage t readonly_prop
2354+
method sessionStorage : storage t optdef readonly_prop
23552355

2356-
method localStorage : storage t readonly_prop
2356+
method localStorage : storage t optdef readonly_prop
23572357

23582358
method top : window t readonly_prop
23592359

@@ -3368,6 +3368,10 @@ module Keyboard_code = struct
33683368

33693369
let make_unidentified _ = Unidentified
33703370

3371+
let try_next value f = function
3372+
| Unidentified -> Optdef.case value make_unidentified f
3373+
| v -> v
3374+
33713375
let run_next value f = function
33723376
| Unidentified -> f value
33733377
| v -> v
@@ -3383,8 +3387,9 @@ module Keyboard_code = struct
33833387

33843388
let ( |> ) x f = f x
33853389

3386-
let of_event (evt : keyboardEvent Js.t) =
3387-
try_code evt##.code
3390+
let of_event evt =
3391+
Unidentified
3392+
|> try_next evt##.code try_code
33883393
|> try_key_location evt
33893394
|> run_next (get_key_code evt) try_key_code_normal
33903395

@@ -3397,10 +3402,12 @@ module Keyboard_key = struct
33973402
let char_of_int value =
33983403
if 0 < value then try Some (Uchar.of_int value) with _ -> None else None
33993404

3405+
let empty_string _ = Js.string ""
3406+
34003407
let none _ = None
34013408

34023409
let of_event evt =
3403-
let key = evt##.key in
3410+
let key = Optdef.get evt##.key empty_string in
34043411
match key##.length with
34053412
| 0 -> Optdef.case evt##.charCode none char_of_int
34063413
| 1 -> char_of_int (int_of_float (Js.to_float (key##charCodeAt 0)))

lib/js_of_ocaml/dom_html.mli

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,12 @@ and keyboardEvent = object
349349

350350
method location : int readonly_prop
351351

352-
method key : js_string t readonly_prop
352+
(* Chrome can send fake keyboard events without any of the expected
353+
properties (https://chromium-review.googlesource.com/771674), so
354+
we keep the [optdef] annotation for now *)
355+
method key : js_string t optdef readonly_prop
353356

354-
method code : js_string t readonly_prop
357+
method code : js_string t optdef readonly_prop
355358

356359
method isComposing : bool t readonly_prop
357360

@@ -2201,9 +2204,13 @@ class type window = object
22012204

22022205
method scrollBy : number_t -> number_t -> unit meth
22032206

2204-
method sessionStorage : storage t readonly_prop
2207+
(* These two properties are not available on non-Web environments
2208+
(for instance, Web workers, node). So we keep the [optdef]
2209+
annotation for now. *)
22052210

2206-
method localStorage : storage t readonly_prop
2211+
method sessionStorage : storage t optdef readonly_prop
2212+
2213+
method localStorage : storage t optdef readonly_prop
22072214

22082215
method top : window t readonly_prop
22092216

toplevel/examples/lwt_toplevel/toplevel.ml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,11 @@ module History = struct
308308

309309
let idx = ref 0
310310

311-
let get_storage () = Dom_html.window##.localStorage
311+
let get_storage () =
312+
match Js.Optdef.to_option Dom_html.window##.localStorage with
313+
| exception _ -> raise Not_found
314+
| None -> raise Not_found
315+
| Some t -> t
312316

313317
let setup () =
314318
try

0 commit comments

Comments
 (0)