Skip to content

Commit 18def31

Browse files
committed
Add handling of on/off attributes (and improve bools).
1 parent b0991aa commit 18def31

File tree

5 files changed

+52
-11
lines changed

5 files changed

+52
-11
lines changed

lib/html_sigs.mli

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -250,7 +250,7 @@ module type T = sig
250250

251251
(** {3 Other attributes} *)
252252

253-
val a_autocomplete : bool wrap -> [> | `Autocomplete] attrib
253+
val a_autocomplete : (bool[@onoff]) wrap -> [> | `Autocomplete] attrib
254254

255255
val a_async : unit -> [> | `Async] attrib
256256

ppx/ppx_attribute_value.ml

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,9 @@ let float_exp loc s =
142142
with Failure "float_of_string" ->
143143
None
144144

145-
145+
let bool_exp loc b =
146+
let s = if b then "true" else "false" in
147+
Exp.construct ~loc (Location.mkloc (Longident.Lident s) loc) None
146148

147149
(* Numeric. *)
148150

@@ -172,14 +174,23 @@ let char ?separated_by:_ ?default:_ loc name s =
172174

173175
Some (Exp.constant ~loc (Const_char c))
174176

175-
let bool ?separated_by:_ ?default:_ loc name s =
176-
begin
177-
try bool_of_string s |> ignore
178-
with Invalid_argument "bool_of_string" ->
179-
Ppx_common.error loc "Value of %s must be \"true\" or \"false\"" name
180-
end;
177+
let onoff ?separated_by:_ ?default:_ loc name s =
178+
let b = match s with
179+
| "" | "on" -> true
180+
| "off" -> false
181+
| _ ->
182+
Ppx_common.error loc {|Value of %s must be "on", "" or "off"|} name
183+
in
184+
Some (bool_exp loc b)
181185

182-
Some (Exp.construct ~loc (Location.mkloc (Longident.parse s) loc) None)
186+
let bool ?separated_by:_ ?default:_ loc name s =
187+
let b = match s with
188+
| "" | "true" -> true
189+
| "false" -> false
190+
| _ ->
191+
Ppx_common.error loc {|Value of %s must be "true", "" or "false"|} name
192+
in
193+
Some (bool_exp loc b)
183194

184195
let unit ?separated_by:_ ?default:_ loc name s =
185196
if s = "" || s = name then

ppx/ppx_attribute_value.mli

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,12 @@ val char : parser
9494
a parse tree for [c]. *)
9595

9696
val bool : parser
97-
(** [bool _ _ s] produces a parse tree for the boolean [true] if [s = "true"]
98-
and [false] if [s = "false"]. *)
97+
(** [bool _ _ s] produces a parse tree for the boolean [true]
98+
if [s = "true"] or [""] and [false] if [s = "false"]. *)
99+
100+
val onoff : parser
101+
(** [onoff _ _ s] produces a parse tree for the boolean [true]
102+
if [s = "on"] or [""] and [false] if [s = "off"]. *)
99103

100104
val unit : parser
101105
(** [unit _ name s] produces a parse tree for [()]. It fails if [name <> s]. *)

ppx/ppx_reflect.ml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ let rec to_attribute_parser name = function
9696
[%expr wrap [%e to_attribute_parser name [ty]]]
9797

9898
| [[%type: character]] -> [%expr char]
99+
| [[%type: bool] as ty]
100+
when AC.has_attr "onoff" ty.ptyp_attributes -> [%expr onoff]
99101
| [[%type: bool]] -> [%expr bool]
100102
| [[%type: unit]] -> [%expr nowrap unit]
101103

test/test_ppx.ml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,30 @@ let attribs = "ppx attribs", tyxml_tests Html.[
7474
[[%html "<div hidden=hidden></div>"]],
7575
[div ~a:[a_hidden ()] []] ;
7676

77+
"bool default",
78+
[[%html "<div draggable></div>"]],
79+
[div ~a:[a_draggable true] []] ;
80+
81+
"bool true",
82+
[[%html "<div draggable=true></div>"]],
83+
[div ~a:[a_draggable true] []] ;
84+
85+
"bool false",
86+
[[%html "<div draggable=false></div>"]],
87+
[div ~a:[a_draggable false] []] ;
88+
89+
"onoff default",
90+
[[%html "<form autocomplete></form>"]],
91+
[form ~a:[a_autocomplete true] []] ;
92+
93+
"bool true",
94+
[[%html "<form autocomplete=on></form>"]],
95+
[form ~a:[a_autocomplete true] []] ;
96+
97+
"bool false",
98+
[[%html "<form autocomplete=off></form>"]],
99+
[form ~a:[a_autocomplete false] []] ;
100+
77101
]
78102

79103
let ns_nesting = "namespace nesting" , tyxml_tests Html.[

0 commit comments

Comments
 (0)