Skip to content

Commit 6dff852

Browse files
committedDec 4, 2023
This should fix ocsigen#326.
1 parent d291653 commit 6dff852

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed
 

‎syntax/attribute_value.ml

+25-6
Original file line numberDiff line numberDiff line change
@@ -74,11 +74,11 @@ let list
7474
|> Common.list loc
7575
|> fun e -> Some e
7676

77-
let spaces = list (Re_str.regexp " +") "space"
78-
let commas = list (Re_str.regexp " *, *") "comma"
79-
let semicolons = list (Re_str.regexp " *; *") "semicolon"
77+
let spaces = list (Re_str.regexp "[ \t\r\f]+") "space"
78+
let commas = list (Re_str.regexp "[ \t\r\f]*,[ \t\r\f]*") "comma"
79+
let semicolons = list (Re_str.regexp "[ \t\r\f]*;[ \t\r\f]*") "semicolon"
8080

81-
let spaces_or_commas_regexp = Re_str.regexp "\\( *, *\\)\\| +"
81+
let spaces_or_commas_regexp = Re_str.regexp "\\([ \t\r\f]*,[ \t\r\f]*\\)\\|[ \t\r\f]+"
8282
let spaces_or_commas_ = exp_list spaces_or_commas_regexp "space- or comma"
8383
let spaces_or_commas = list spaces_or_commas_regexp "space- or comma"
8484

@@ -347,8 +347,8 @@ let offset =
347347
else Some [%expr `Number [%e n]]
348348
end [@metaloc loc]
349349

350-
let transform =
351-
let regexp = Re_str.regexp "\\([^(]+\\)(\\([^)]*\\))" in
350+
let transform_item =
351+
let regexp = Re_str.regexp "\\([a-zA-Z]+\\)[ \t\r\f]*(\\([^)]*\\))" in
352352

353353
fun ?separated_by:_ ?default:_ loc name s ->
354354
if not @@ does_match regexp s then
@@ -408,6 +408,25 @@ let transform =
408408

409409
Some e
410410

411+
let rec transform =
412+
let regexp_wsp = Re_str.regexp "[ \t\r\f]*" in
413+
let regexp = Re_str.regexp "[ \t\r\f]*\\([a-zA-Z]+[ \t\r\f]*([^)]*)\\)\\(.*\\)" in
414+
415+
fun ?separated_by:_ ?default:_ loc name s ->
416+
if does_match regexp_wsp s then
417+
Some [%expr []]
418+
else if does_match regexp_wsp s then
419+
begin
420+
let item = Re_str.matched_group 1 s in
421+
let rest = Re_str.matched_group 2 s in
422+
Option.bind (transform_item ~separated_by ~default loc name item) (fun item ->
423+
Option.bind (transform ~separated_by ~default loc name rest) (fun l ->
424+
Some (item :: l)))
425+
end
426+
else
427+
Common.error loc "Value of %s is not a list of SVG transform" name
428+
[@metaloc loc]
429+
411430

412431

413432
(* String-like. *)

‎syntax/reflect/reflect.ml

+1-1
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ let rec to_attribute_parser lang name ~loc = function
200200
[%expr spaces_or_commas svg_length]
201201

202202
| [[%type: transforms]] ->
203-
[%expr spaces_or_commas transform]
203+
[%expr transform]
204204

205205
| [[%type: paint]] ->
206206
[%expr paint]

‎test/test_jsx.re

+5
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ let svg = (
341341
[<path fill_rule="evenodd" />],
342342
[path(~a=[a_fill_rule(`Evenodd)], [])],
343343
),
344+
(
345+
"transform with random spacing",
346+
[<g transform="translate ( 200 , 200) rotate(1) matrix(-0,1,.1,.0,1e-5,1.E5) scale ( 1 , 0 ) skewY(-0.)"></g>],
347+
[g(~a:[a_transform([`Translate((200., Some(200.))), `Rotate((1., None)), `Matrix((-0., 1, 0.1, 0., 1e-5, 1e5)), `Scale((1., Some 0.)), `SkewY(-0.)])], [])],
348+
),
344349
],
345350
),
346351
);

‎test/test_ppx.ml

+4
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,10 @@ let svg = "svg", SvgTests.make Svg.[
424424
[[%svg "<path fill-rule='evenodd'/>"]],
425425
[path ~a:[a_fill_rule `Evenodd] []] ;
426426

427+
"transform with random spacing",
428+
[[%svg "<g transform='translate ( 200 , 200) rotate(1) matrix(-0,1,.1,.0,1e-5,1.E5) scale ( 1 , 0 ) skewY(-0.)'></g>"]],
429+
[g ~a:[a_transform [`Translate (200., Some 200.); `Rotate (1., None); `Matrix (-0., 1, 0.1, 0., 1e-5, 1e5); `Scale (1., Some 0.); `SkewY (-0.)]] []] ;
430+
427431
]
428432

429433
let svg_element_names = "svg element names", SvgTests.make Svg.[

0 commit comments

Comments
 (0)
Please sign in to comment.