Skip to content

Commit

Permalink
WIP: Calc things
Browse files Browse the repository at this point in the history
This switches to CSSNumericType, sets those on construction, and also
distinguishes between arg types being the same, and them being
consistent.

Probably this should be split up but I'm not sure how.
  • Loading branch information
AtkinsSJ committed Dec 17, 2024
1 parent 2ceefe7 commit 6b5f550
Show file tree
Hide file tree
Showing 7 changed files with 432 additions and 1,158 deletions.
8 changes: 7 additions & 1 deletion Documentation/CSSGeneratedFiles.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,13 @@ This is a single JSON object, describing each [CSS math function](https://www.w3
with the keys being the function name and the values being objects describing that function's properties.
This generates `MathFunctions.h` and `MathFunctions.cpp`.

Each entry currently has a single property, `parameters`, which is an array of parameter definition objects.
Each entry has two properties:

| Field | Description |
|------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `parameter-validation` | Optional string. Either "same" or "consistent", depending on whether the spec says the input calculations should be the same type or consistent types. Defaults to "same". Ignore this if there is only one parameter. |
| `parameters` | An array of parameter definition objects, see below. |

Parameter definitions have the following properties:

| Field | Description |
Expand Down
10 changes: 10 additions & 0 deletions Libraries/LibWeb/CSS/MathFunctions.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
]
},
"atan2": {
"parameter-validation": "consistent",
"parameters": [
{
"name": "y",
Expand All @@ -50,6 +51,7 @@
]
},
"clamp": {
"parameter-validation": "consistent",
"parameters": [
{
"name": "min",
Expand Down Expand Up @@ -87,6 +89,7 @@
]
},
"hypot": {
"parameter-validation": "consistent",
"is-variadic": true,
"parameters": [
{
Expand All @@ -97,6 +100,7 @@
]
},
"log": {
"parameter-validation": "same",
"parameters": [
{
"name": "value",
Expand All @@ -112,6 +116,7 @@
]
},
"max": {
"parameter-validation": "consistent",
"is-variadic": true,
"parameters": [
{
Expand All @@ -122,6 +127,7 @@
]
},
"min": {
"parameter-validation": "consistent",
"is-variadic": true,
"parameters": [
{
Expand All @@ -132,6 +138,7 @@
]
},
"mod": {
"parameter-validation": "same",
"parameters": [
{
"name": "value",
Expand All @@ -146,6 +153,7 @@
]
},
"pow": {
"parameter-validation": "consistent",
"parameters": [
{
"name": "value",
Expand All @@ -160,6 +168,7 @@
]
},
"rem": {
"parameter-validation": "same",
"parameters": [
{
"name": "value",
Expand All @@ -174,6 +183,7 @@
]
},
"round": {
"parameter-validation": "consistent",
"parameters": [
{
"name": "strategy",
Expand Down
13 changes: 8 additions & 5 deletions Libraries/LibWeb/CSS/Parser/Parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1924,7 +1924,7 @@ RefPtr<CalculatedStyleValue> Parser::parse_calculated_value(ComponentValue const
if (!function_node)
return nullptr;

auto function_type = function_node->determine_type(m_context.current_property_id());
auto function_type = function_node->numeric_type();
if (!function_type.has_value())
return nullptr;

Expand All @@ -1936,7 +1936,7 @@ OwnPtr<CalculationNode> Parser::parse_a_calc_function_node(Function const& funct
if (function.name.equals_ignoring_ascii_case("calc"sv))
return parse_a_calculation(function.value);

if (auto maybe_function = parse_math_function(m_context.current_property_id(), function))
if (auto maybe_function = parse_math_function(function))
return maybe_function;

return nullptr;
Expand Down Expand Up @@ -8681,15 +8681,18 @@ OwnPtr<CalculationNode> Parser::convert_to_calculation_node(CalcParsing::Node co
[](Number const& number) -> OwnPtr<CalculationNode> {
return NumericCalculationNode::create(number);
},
[](Dimension const& dimension) -> OwnPtr<CalculationNode> {
[this](Dimension const& dimension) -> OwnPtr<CalculationNode> {
if (dimension.is_angle())
return NumericCalculationNode::create(dimension.angle());
if (dimension.is_frequency())
return NumericCalculationNode::create(dimension.frequency());
if (dimension.is_length())
return NumericCalculationNode::create(dimension.length());
if (dimension.is_percentage())
return NumericCalculationNode::create(dimension.percentage());
if (dimension.is_percentage()) {
// FIXME: Figure this out in non-property contexts
auto percentage_resolved_type = property_resolves_percentages_relative_to(m_context.current_property_id());
return NumericCalculationNode::create(dimension.percentage(), percentage_resolved_type);
}
if (dimension.is_resolution())
return NumericCalculationNode::create(dimension.resolution());
if (dimension.is_time())
Expand Down
2 changes: 1 addition & 1 deletion Libraries/LibWeb/CSS/Parser/Parser.h
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ class Parser {
RefPtr<CalculatedStyleValue> parse_calculated_value(ComponentValue const&);
RefPtr<CustomIdentStyleValue> parse_custom_ident_value(TokenStream<ComponentValue>&, std::initializer_list<StringView> blacklist);
// NOTE: Implemented in generated code. (GenerateCSSMathFunctions.cpp)
OwnPtr<CalculationNode> parse_math_function(PropertyID, Function const&);
OwnPtr<CalculationNode> parse_math_function(Function const&);
OwnPtr<CalculationNode> parse_a_calc_function_node(Function const&);
RefPtr<CSSStyleValue> parse_keyword_value(TokenStream<ComponentValue>&);
RefPtr<CSSStyleValue> parse_hue_none_value(TokenStream<ComponentValue>&);
Expand Down
Loading

0 comments on commit 6b5f550

Please sign in to comment.