Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

This should fix #326. #328

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 25 additions & 6 deletions syntax/attribute_value.ml
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,11 @@ let list
|> Common.list loc
|> fun e -> Some e

let spaces = list (Re_str.regexp " +") "space"
let commas = list (Re_str.regexp " *, *") "comma"
let semicolons = list (Re_str.regexp " *; *") "semicolon"
let spaces = list (Re_str.regexp "[ \t\r\n]+") "space"
let commas = list (Re_str.regexp "[ \t\r\n]*,[ \t\r\n]*") "comma"
let semicolons = list (Re_str.regexp "[ \t\r\n]*;[ \t\r\n]*") "semicolon"

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

Expand Down Expand Up @@ -347,8 +347,8 @@ let offset =
else Some [%expr `Number [%e n]]
end [@metaloc loc]

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

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

Some e

let rec transform =
let regexp_wsp = Re_str.regexp "[ \t\r\n]*" in
let regexp = Re_str.regexp "[ \t\r\n]*\\([a-zA-Z]+[ \t\r\n]*([^)]*)\\)\\(.*\\)" in

fun ?separated_by:_ ?default:_ loc name s ->
if does_match regexp_wsp s then
Some [%expr []]
else if does_match regexp s then
begin
let item = Re_str.matched_group 1 s in
let rest = Re_str.matched_group 2 s in
Option.bind (transform_item loc name item) (fun item ->
Option.bind (transform loc name rest) (fun l ->
Some [%expr [%e item] :: [%e l]]))
end
else
Common.error loc "Value of %s is not a list of SVG transform" name
[@metaloc loc]



(* String-like. *)
Expand Down
2 changes: 1 addition & 1 deletion syntax/reflect/reflect.ml
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ let rec to_attribute_parser lang name ~loc = function
[%expr spaces_or_commas svg_length]

| [[%type: transforms]] ->
[%expr spaces_or_commas transform]
[%expr transform]

| [[%type: paint]] ->
[%expr paint]
Expand Down
5 changes: 5 additions & 0 deletions test/test_jsx.re
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,11 @@ let svg = (
[<path fill_rule="evenodd" />],
[path(~a=[a_fill_rule(`Evenodd)], [])],
),
(
"transform with random spacing",
[<g transform="translate ( 200 , 200) matrix(-0,1,.1,.0,1e-5,1.E5) scale ( -1. , 0 )"></g>],
[g(~a=[a_transform([`Translate((200., Some(200.))), `Matrix((-0., 1., 0.1, 0., 1e-5, 1e5)), `Scale((-1., Some(0.)))])], [])],
),
],
),
);
Expand Down
4 changes: 4 additions & 0 deletions test/test_ppx.ml
Original file line number Diff line number Diff line change
Expand Up @@ -424,6 +424,10 @@ let svg = "svg", SvgTests.make Svg.[
[[%svg "<path fill-rule='evenodd'/>"]],
[path ~a:[a_fill_rule `Evenodd] []] ;

"transform with random spacing",
[[%svg "<g transform='translate ( 200 , 200) matrix(-0,1,.1,.0,1e-5,1.E5) scale ( -1. , 0 )'></g>"]],
[g ~a:[a_transform [`Translate (200., Some 200.); `Matrix (-0., 1., 0.1, 0., 1e-5, 1e5); `Scale (-1., Some 0.)]] []] ;

]

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