Skip to content

Commit 1efb435

Browse files
committed
Adding parser for fill-opacity.
1 parent a496fd1 commit 1efb435

File tree

2 files changed

+31
-0
lines changed

2 files changed

+31
-0
lines changed

syntax/attribute_value.ml

+25
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,31 @@ let paint ?separated_by:_ ?default:_ loc name s =
484484
`Icc ([%e iri], Some [%e paint_without_icc loc name remainder])]
485485
end [@metaloc loc]
486486

487+
let fill_opacity =
488+
let bad_form name loc =
489+
Common.error loc "Value of %s must be a number or percentage" name in
490+
491+
let regexp = Re_str.regexp "\\([-+0-9eE.]+\\)\\(%\\)?" in
492+
493+
fun ?separated_by:_ ?default:_ loc name s ->
494+
if not @@ does_match regexp s then bad_form name loc;
495+
496+
begin
497+
let n =
498+
match float_exp loc (Re_str.matched_group 1 s) with
499+
| Some n -> n
500+
| None -> bad_form name loc
501+
in
502+
503+
let v =
504+
if group_matched 2 s then [%expr [%e n] /. 100.]
505+
else [%expr [%e n]] in
506+
507+
if v >= 0. && v <= 1. then Some v
508+
else
509+
Common.error loc "Value of %s must be between 0 and 1." name in
510+
end [@metaloc loc]
511+
487512
let fill_rule ?separated_by:_ ?default:_ loc _name s =
488513
begin match s with
489514
| "nonzero" ->

syntax/attribute_value.mli

+6
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,12 @@ val paint : parser
198198
{:{https://www.w3.org/TR/SVG/painting.html#SpecifyingPaint} Specifying
199199
paint}. *)
200200

201+
val fill_opacity : parser
202+
(** Parses an SVG fill-opacity value, converting it into a number between 0. and 1.
203+
204+
@see <https://developer.mozilla.org/en-US/docs/Web/SVG/Attribute/fill-opacity>
205+
*)
206+
201207
val fill_rule : parser
202208
(** Parses an SVG fill-rule value.
203209

0 commit comments

Comments
 (0)