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

Fix calc function #506

Merged
merged 4 commits into from
Aug 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion e2e/melange/src/ui/ui.re
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ module Cositas = [%styled.div
display: flex;
flex-direction: column;
gap: $(lola);
width: calc(100% / 3 - 2 * 1em - 2 * 1px);
|}
];

Expand Down Expand Up @@ -160,7 +161,8 @@ let rec modify =

let negate_in_calc = (v: calc_value): calc_value =>
`calc(`mult((v, `num(-1.))));
let half_in_calc = (v: calc_value): calc_value => `calc(`div((v, 2.)));
let half_in_calc = (v: calc_value): calc_value =>
`calc(`div((v, `num(2.))));

let negative = value => modify((~-.), (~-), negate_in_calc, value);
let half = value => modify(x => x /. 2., x => x / 2, half_in_calc, value);
4 changes: 2 additions & 2 deletions packages/css-property-parser/lib/Parser.re
Original file line number Diff line number Diff line change
Expand Up @@ -126,12 +126,12 @@ and border_radius = [%value.rec "<extended-length> | <extended-percentage>"]
and bottom = [%value.rec "<extended-length> | 'auto'"]
and box = [%value.rec "'border-box' | 'padding-box' | 'content-box'"]
and calc_product = [%value.rec
"<calc-value> [ '*' <calc-value> | '/' <number> ]*"
"<calc-value> [ [ '*' | '/' ] <calc-value> ]*"
]
and calc_sum = [%value.rec "<calc-product> [ [ '+' | '-' ] <calc-product> ]*"]
/* and calc_value = [%value.rec "<number> | <dimension> | <extended-percentage> | <calc>"] */
and calc_value = [%value.rec
"<number> | <extended-length> | <extended-percentage> | <extended-angle> | <extended-time>"
"<number> | <extended-length> | <extended-percentage> | <extended-angle> | <extended-time> | '(' <calc-sum> ')'"
]
and cf_final_image = [%value.rec "<image> | <color>"]
and cf_mixing_image = [%value.rec "[ <extended-percentage> ]? && <image>"]
Expand Down
87 changes: 43 additions & 44 deletions packages/ppx/src/Property_to_runtime.re
Original file line number Diff line number Diff line change
Expand Up @@ -320,28 +320,33 @@ let render_length = (~loc) =>
| `Zero => [%expr `zero];

let rec render_function_calc = (~loc, calc_sum) => {
render_calc_sum(~loc, calc_sum);
[%expr `calc([%e render_calc_sum(~loc, calc_sum)])];
}

and render_calc_sum = (~loc, calc_sum) => {
switch (calc_sum) {
| (product, []) => render_product(~loc, product)
| (product, list_of_sums) =>
/* This isn't a great design of the types, but we need to know the operation
which is in the first position of the array, we ensure that there's one value
since we are on this branch of the switch */
let op = pick_operation(List.hd(list_of_sums));
switch (op) {
| `Dash () =>
let first = render_product(~loc, product);
let second = render_list_of_sums(~loc, list_of_sums);
[%expr `calc(`sub(([%e first], [%e second])))];
| `Cross () =>
let first = render_product(~loc, product);
let second = render_list_of_sums(~loc, list_of_sums);
[%expr `calc(`add(([%e first], [%e second])))];
and render_calc_sum = (~loc, (product, sums): Types.calc_sum) => {
let rec go = (left, rest) => {
switch (rest) {
| [] => left
| [x, ...xs] =>
switch (x) {
| (`Cross (), calc_product) =>
go(
[%expr
`add(([%e left], [%e render_calc_product(~loc, calc_product)]))
],
xs,
)
| (`Dash (), calc_product) =>
go(
[%expr
`sub(([%e left], [%e render_calc_product(~loc, calc_product)]))
],
xs,
)
}
};
};
go(render_calc_product(~loc, product), sums);
}
and render_function_min_or_max = (~loc, calc_sums: list(Types.calc_sum)) => {
switch (calc_sums) {
Expand All @@ -357,33 +362,26 @@ and render_function_min = (~loc, calc_sums) => {
and render_function_max = (~loc, calc_sums) => {
[%expr `max([%e render_function_min_or_max(~loc, calc_sums)])];
}
and pick_operation = ((op, _)) => op
and render_list_of_products = (~loc, list_of_products) => {
switch (list_of_products) {
| [one] => render_product_op(~loc, one)
| list => render_list_of_products(~loc, list)
};
}
and render_list_of_sums = (~loc, list_of_sums) => {
switch (list_of_sums) {
| [(_, one)] => render_product(~loc, one)
| list => render_list_of_sums(~loc, list)
};
}
and render_product = (~loc, product) => {
switch (product) {
| (calc_value, []) => render_calc_value(~loc, calc_value)
| (calc_value, list_of_products) =>
let first = render_calc_value(~loc, calc_value);
let second = render_list_of_products(~loc, list_of_products);
[%expr `calc(`mult(([%e first], [%e second])))];
};
}
and render_product_op = (~loc, op) => {
switch (op) {
| `Static_0((), calc_value) => render_calc_value(~loc, calc_value)
| `Static_1((), float) => [%expr `num([%e render_float(~loc, float)])]
and render_calc_product = (~loc, (value, products): Types.calc_product) => {
let rec go = (left, rest) => {
switch (rest) {
| [] => left
| [x, ...xs] =>
switch (x) {
| (`Asterisk (), value) =>
go(
[%expr `mult(([%e left], [%e render_calc_value(~loc, value)]))],
xs,
)
| (`Bar (), value) =>
go(
[%expr `div(([%e left], [%e render_calc_value(~loc, value)]))],
xs,
)
}
};
};
go(render_calc_value(~loc, value), products);
}
and render_angle = (~loc) =>
fun
Expand Down Expand Up @@ -434,6 +432,7 @@ and render_calc_value = (~loc, calc_value) => {
| `Extended_percentage(p) => render_extended_percentage(~loc, p)
| `Extended_angle(a) => render_extended_angle(~loc, a)
| `Extended_time(t) => render_extended_time(~loc, t)
| `Static(_, calc_sum, _) => render_calc_sum(~loc, calc_sum)
};
}
and render_extended_length = (~loc) =>
Expand Down
161 changes: 71 additions & 90 deletions packages/ppx/src/Property_to_string.re
Original file line number Diff line number Diff line change
Expand Up @@ -133,21 +133,22 @@ let render_length =
| `Vw(n) => render_number(n, "vw")
| `Zero => render_string("0");

let rec render_function_calc = calc_sum => {
render_calc_sum(calc_sum);
let rec render_function_calc =
(calc_sum: Css_property_parser.Parser.Types.calc_sum) => {
[%expr "calc(" ++ [%e render_calc_sum(calc_sum)] ++ ")"];
}
and render_calc_sum = calc_sum =>
switch (calc_sum) {
| (product, []) => render_product(product)
| (product, list_of_sums) =>
/* This isn't a great design of the types, but we need to know the operation
which is in the first position of the array, we ensure that there's one value
since we are on this branch of the switch */
let op = pick_operation(List.hd(list_of_sums));
let first = render_product(product);
let second = render_list_of_sums(list_of_sums);
[%expr "calc(" ++ [%e first] ++ [%e op] ++ [%e second] ++ ")"];
}
and render_calc_sum = _calc_sum => failwith("not implemented")
//switch (calc_sum) {
//| (product, None) => render_product(product)
//| (product, Some((`Cross (), calc_product))) =>
// let first = render_product(product);
// let second = render_product(calc_product);
// [%expr [%e first] ++ " + " ++ [%e second]];
//| (product, Some((`Dash (), calc_product))) =>
// let first = render_product(product);
// let second = render_product(calc_product);
// [%expr [%e first] ++ " - " ++ [%e second]];
//}
and render_function_min_or_max = calc_sums => {
switch (calc_sums) {
| [] => raise(Invalid_value("expected at least one argument"))
Expand All @@ -162,82 +163,62 @@ and render_function_min = calc_sums => {
and render_function_max = calc_sums => {
render_function_min_or_max(calc_sums);
}
and render_sum_op = op => {
switch (op) {
| `Dash () => [%expr " - "]
| `Cross () => [%expr " + "]
};
}
and pick_operation = ((op, _)) => render_sum_op(op)
and render_list_of_products = list_of_products => {
switch (list_of_products) {
| [one] => render_product_op(one)
| list => render_list_of_products(list)
};
}
and render_list_of_sums = list_of_sums => {
switch (list_of_sums) {
| [(_, one)] => render_product(one)
| list => render_list_of_sums(list)
};
}
and render_product = product => {
switch (product) {
| (calc_value, []) => render_calc_value(calc_value)
| (calc_value, list_of_products) =>
let first = render_calc_value(calc_value);
let second = render_list_of_products(list_of_products);
[%expr "calc(" ++ [%e first] ++ " * " ++ [%e second] ++ ")"];
};
}
and render_product_op = op => {
switch (op) {
| `Static_0((), calc_value) => render_calc_value(calc_value)
| `Static_1((), float) => render_number(float, "")
};
}
and render_calc_value = calc_value => {
switch (calc_value) {
| `Number(float) => render_number(float, "")
| `Extended_length(l) => render_extended_length(l)
| `Extended_percentage(p) => render_extended_percentage(p)
| `Extended_angle(a) => render_extended_angle(a)
| `Extended_time(t) => render_extended_time(t)
};
}
and render_time_as_int =
fun
| `Ms(f) => {
let value = Float.to_int(f);
render_integer(value);
}
| `S(f) => {
let value = Float.to_int(f);
render_integer(value);
}

and render_extended_time =
fun
| `Time(t) => render_time_as_int(t)
| `Function_calc(fc) => render_function_calc(fc)
| `Interpolation(v) => render_variable(v)
| `Function_min(values) => render_function_min(values)
| `Function_max(values) => render_function_max(values)

and render_angle =
fun
| `Deg(number)
| `Rad(number)
| `Grad(number)
| `Turn(number) => render_number(number, "")

and render_extended_angle =
fun
| `Angle(a) => render_angle(a)
| `Function_calc(fc) => render_function_calc(fc)
| `Interpolation(i) => render_variable(i)
| `Function_min(values) => render_function_min(values)
| `Function_max(values) => render_function_max(values)
//and render_product = product => {
// switch (product) {
// | (value, None) => render_calc_value(value)
// | (left, Some((`Asterisk (), right))) =>
// let first = render_calc_value(left);
// let second = render_calc_value(right);
// [%expr [%e first] ++ " * " ++ [%e second]];
// | (left, Some((`Bar (), right))) =>
// let first = render_calc_value(left);
// let second = render_calc_value(right);
// [%expr [%e first] ++ " / " ++ [%e second]];
// };
//}
//and render_calc_value = calc_value => {
// switch (calc_value) {
// | `Number(float) => render_number(float, "")
// | `Extended_length(l) => render_extended_length(l)
// | `Extended_percentage(p) => render_extended_percentage(p)
// | `Extended_angle(a) => render_extended_angle(a)
// | `Extended_time(t) => render_extended_time(t)
// | `Static(_, calc_sum, _) => render_calc_sum(calc_sum)
// };
//}
//and render_time_as_int =
// fun
// | `Ms(f) => {
// let value = Float.to_int(f);
// render_integer(value);
// }
// | `S(f) => {
// let value = Float.to_int(f);
// render_integer(value);
// }
//
//and render_extended_time =
// fun
// | `Time(t) => render_time_as_int(t)
// | `Function_calc(fc) => render_function_calc(fc)
// | `Interpolation(v) => render_variable(v)
// | `Function_min(values) => render_function_min(values)
// | `Function_max(values) => render_function_max(values)
//
//and render_angle =
// fun
// | `Deg(number)
// | `Rad(number)
// | `Grad(number)
// | `Turn(number) => render_number(number, "")
//
//and render_extended_angle =
// fun
// | `Angle(a) => render_angle(a)
// | `Function_calc(fc) => render_function_calc(fc)
// | `Interpolation(i) => render_variable(i)
// | `Function_min(values) => render_function_min(values)
// | `Function_max(values) => render_function_max(values)

and render_extended_length =
fun
Expand Down
15 changes: 5 additions & 10 deletions packages/runtime/native/shared/Css_types.ml
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ module Calc = struct
| `calc (`mult (a, b)) ->
{js|calc(|js} ^ fn a ^ {js| * |js} ^ fn b ^ {js|)|js}
| `calc (`div (a, b)) ->
{js|calc(|js} ^ fn a ^ {js| / |js} ^ Kloth.Float.to_string b ^ {js|)|js}
{js|calc(|js} ^ fn a ^ {js| / |js} ^ fn b ^ {js|)|js}
| `num n -> Kloth.Float.to_string n
| `min xs -> {js|min(|js} ^ max_or_min_values fn xs ^ {js|)|js}
| `max xs -> {js|max(|js} ^ max_or_min_values fn xs ^ {js|)|js}
Expand All @@ -80,8 +80,7 @@ module Calc = struct
| `add (x, y) -> {js|calc(|js} ^ fn x ^ {js| + |js} ^ fn y ^ {js|)|js}
| `sub (x, y) -> {js|calc(|js} ^ fn x ^ {js| - |js} ^ fn y ^ {js|)|js}
| `mult (x, y) -> {js|calc(|js} ^ fn x ^ {js| * |js} ^ fn y ^ {js|)|js}
| `div (x, y) ->
{js|calc(|js} ^ fn x ^ {js| / |js} ^ Kloth.Float.to_string y ^ {js|)|js}
| `div (x, y) -> {js|calc(|js} ^ fn x ^ {js| / |js} ^ fn y ^ {js|)|js}
| (`min _ | `max _) as x -> fn_max_min x
in
aux x
Expand All @@ -100,7 +99,7 @@ module Time = struct
| `add of calc_value * calc_value
| `sub of calc_value * calc_value
| `mult of calc_value * calc_value
| `div of calc_value * float
| `div of calc_value * calc_value
| (* calc_value can be a nested `calc.
Ej. width: calc(100vh - calc(2rem + 120px))) *)
`calc of
Expand Down Expand Up @@ -190,7 +189,7 @@ module Length = struct
| `add of calc_value * calc_value
| `sub of calc_value * calc_value
| `mult of calc_value * calc_value
| `div of calc_value * float
| `div of calc_value * calc_value
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heads up, this doesn't seem right. You can't divide by something that isn't a number.

Copy link
Owner

@davesnx davesnx Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, it's a bit more complex than this:

Current implementations require that for the * and / operators, one of the operands has to be unitless. For /, the right operand must be unitless. For example font-size: calc(1.25rem / 1.25) is valid but font-size: calc(1.25rem / 125%) is invalid.

https://developer.mozilla.org/en-US/docs/Web/CSS/calc#rules_and_best_practices_while_using_calc

Copy link
Collaborator Author

@zakybilfagih zakybilfagih Aug 15, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah the formal grammar is a bit misleading.

<calc-product> = <calc-value> [ [ '*' |  '/' ] <calc-value>]*

maybe we can have <unitless-calc-value>

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe later, i think float should be sufficient for now

| (* calc_value can be a nested `calc.
Ej. width: calc(100vh - calc(2rem + 120px))) *)
`calc of
Expand Down Expand Up @@ -1324,11 +1323,7 @@ module Color = struct

type 'a calc_min_max =
[ `calc of
[ `add of 'a * 'a
| `sub of 'a * 'a
| `mult of 'a * 'a
| `div of 'a * float
]
[ `add of 'a * 'a | `sub of 'a * 'a | `mult of 'a * 'a | `div of 'a * 'a ]
| `min of 'a array
| `max of 'a array
]
Expand Down
Loading