summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authormartinfalisse <martinmotteditfalisse@gmail.com>2023-01-16 18:17:05 +0100
committerAndreas Kling <kling@serenityos.org>2023-01-21 14:35:00 +0100
commita6548c4d80532113e36d42cc983afe3524f92676 (patch)
tree32aa641ecf2d38ae49bb10cec954857ccd13bbd4
parent0448547553815c5f4243befb5ee91c08508e720c (diff)
downloadserenity-a6548c4d80532113e36d42cc983afe3524f92676.zip
LibWeb: Parse `grid-template-areas` CSS property
-rw-r--r--Userland/Libraries/LibWeb/CSS/ComputedValues.h4
-rw-r--r--Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp20
-rw-r--r--Userland/Libraries/LibWeb/CSS/Parser/Parser.h1
-rw-r--r--Userland/Libraries/LibWeb/CSS/Properties.json10
-rw-r--r--Userland/Libraries/LibWeb/CSS/StyleProperties.cpp6
-rw-r--r--Userland/Libraries/LibWeb/CSS/StyleProperties.h1
-rw-r--r--Userland/Libraries/LibWeb/CSS/StyleValue.cpp34
-rw-r--r--Userland/Libraries/LibWeb/CSS/StyleValue.h23
-rw-r--r--Userland/Libraries/LibWeb/Forward.h1
-rw-r--r--Userland/Libraries/LibWeb/Layout/Node.cpp1
10 files changed, 101 insertions, 0 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/ComputedValues.h b/Userland/Libraries/LibWeb/CSS/ComputedValues.h
index 39e87261d5..6221428ea3 100644
--- a/Userland/Libraries/LibWeb/CSS/ComputedValues.h
+++ b/Userland/Libraries/LibWeb/CSS/ComputedValues.h
@@ -73,6 +73,7 @@ public:
static CSS::Size column_gap() { return CSS::Size::make_auto(); }
static CSS::Size row_gap() { return CSS::Size::make_auto(); }
static CSS::BorderCollapse border_collapse() { return CSS::BorderCollapse::Separate; }
+ static Vector<Vector<String>> grid_template_areas() { return {}; }
};
struct BackgroundLayerData {
@@ -194,6 +195,7 @@ public:
CSS::Size const& column_gap() const { return m_noninherited.column_gap; }
CSS::Size const& row_gap() const { return m_noninherited.row_gap; }
CSS::BorderCollapse border_collapse() const { return m_noninherited.border_collapse; }
+ Vector<Vector<String>> const& grid_template_areas() const { return m_noninherited.grid_template_areas; }
CSS::LengthBox const& inset() const { return m_noninherited.inset; }
const CSS::LengthBox& margin() const { return m_noninherited.margin; }
@@ -319,6 +321,7 @@ protected:
CSS::Size column_gap { InitialValues::column_gap() };
CSS::Size row_gap { InitialValues::row_gap() };
CSS::BorderCollapse border_collapse { InitialValues::border_collapse() };
+ Vector<Vector<String>> grid_template_areas { InitialValues::grid_template_areas() };
} m_noninherited;
};
@@ -400,6 +403,7 @@ public:
void set_column_gap(CSS::Size const& column_gap) { m_noninherited.column_gap = column_gap; }
void set_row_gap(CSS::Size const& row_gap) { m_noninherited.row_gap = row_gap; }
void set_border_collapse(CSS::BorderCollapse const& border_collapse) { m_noninherited.border_collapse = border_collapse; }
+ void set_grid_template_areas(Vector<Vector<String>> const& grid_template_areas) { m_noninherited.grid_template_areas = grid_template_areas; }
void set_fill(Color value) { m_inherited.fill = value; }
void set_stroke(Color value) { m_inherited.stroke = value; }
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
index a1c08a666c..ddac708bb7 100644
--- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
+++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
@@ -6292,6 +6292,22 @@ RefPtr<StyleValue> Parser::parse_grid_track_placement_shorthand_value(Vector<Com
return {};
}
+RefPtr<StyleValue> Parser::parse_grid_template_areas_value(Vector<ComponentValue> const& component_values)
+{
+ Vector<Vector<String>> grid_area_rows;
+ for (auto& component_value : component_values) {
+ Vector<String> grid_area_columns;
+ if (component_value.is(Token::Type::String)) {
+ auto const parts = String::from_utf8(component_value.token().string()).release_value_but_fixme_should_propagate_errors().split(' ').release_value_but_fixme_should_propagate_errors();
+ for (auto& part : parts) {
+ grid_area_columns.append(part);
+ }
+ }
+ grid_area_rows.append(move(grid_area_columns));
+ }
+ return GridTemplateAreaStyleValue::create(grid_area_rows);
+}
+
Parser::ParseErrorOr<NonnullRefPtr<StyleValue>> Parser::parse_css_value(PropertyID property_id, TokenStream<ComponentValue>& tokens)
{
auto function_contains_var_or_attr = [](Function const& function, auto&& recurse) -> bool {
@@ -6430,6 +6446,10 @@ Parser::ParseErrorOr<NonnullRefPtr<StyleValue>> Parser::parse_css_value(Property
if (auto parsed_value = parse_grid_track_placement_shorthand_value(component_values))
return parsed_value.release_nonnull();
return ParseError::SyntaxError;
+ case PropertyID::GridTemplateAreas:
+ if (auto parsed_value = parse_grid_template_areas_value(component_values))
+ return parsed_value.release_nonnull();
+ return ParseError::SyntaxError;
case PropertyID::GridColumnEnd:
if (auto parsed_value = parse_grid_track_placement(component_values))
return parsed_value.release_nonnull();
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h
index 4c6eb8a559..fc46c0bf21 100644
--- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.h
+++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.h
@@ -315,6 +315,7 @@ private:
RefPtr<StyleValue> parse_grid_track_sizes(Vector<ComponentValue> const&);
RefPtr<StyleValue> parse_grid_track_placement(Vector<ComponentValue> const&);
RefPtr<StyleValue> parse_grid_track_placement_shorthand_value(Vector<ComponentValue> const&);
+ RefPtr<StyleValue> parse_grid_template_areas_value(Vector<ComponentValue> const&);
// calc() parsing, according to https://www.w3.org/TR/css-values-3/#calc-syntax
OwnPtr<CalculatedStyleValue::CalcSum> parse_calc_sum(TokenStream<ComponentValue>&);
diff --git a/Userland/Libraries/LibWeb/CSS/Properties.json b/Userland/Libraries/LibWeb/CSS/Properties.json
index 8f8fa96fe1..d59bf2b45c 100644
--- a/Userland/Libraries/LibWeb/CSS/Properties.json
+++ b/Userland/Libraries/LibWeb/CSS/Properties.json
@@ -855,6 +855,16 @@
"string"
]
},
+ "grid-template-areas": {
+ "inherited": false,
+ "initial": "auto",
+ "valid-identifiers": [
+ "auto"
+ ],
+ "valid-types": [
+ "string"
+ ]
+ },
"grid-template-columns": {
"inherited": false,
"initial": "auto",
diff --git a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp
index 0cfcf08547..4653601e82 100644
--- a/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp
+++ b/Userland/Libraries/LibWeb/CSS/StyleProperties.cpp
@@ -745,4 +745,10 @@ Optional<CSS::BorderCollapse> StyleProperties::border_collapse() const
return value_id_to_border_collapse(value->to_identifier());
}
+Vector<Vector<String>> StyleProperties::grid_template_areas() const
+{
+ auto value = property(CSS::PropertyID::GridTemplateAreas);
+ return value->as_grid_template_area().grid_template_area();
+}
+
}
diff --git a/Userland/Libraries/LibWeb/CSS/StyleProperties.h b/Userland/Libraries/LibWeb/CSS/StyleProperties.h
index 452f8c3cba..3567e4fb90 100644
--- a/Userland/Libraries/LibWeb/CSS/StyleProperties.h
+++ b/Userland/Libraries/LibWeb/CSS/StyleProperties.h
@@ -91,6 +91,7 @@ public:
CSS::GridTrackPlacement grid_row_end() const;
CSS::GridTrackPlacement grid_row_start() const;
Optional<CSS::BorderCollapse> border_collapse() const;
+ Vector<Vector<String>> grid_template_areas() const;
Vector<CSS::Transformation> transformations() const;
CSS::TransformOrigin transform_origin() const;
diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp
index 43d7989f40..27c9b10555 100644
--- a/Userland/Libraries/LibWeb/CSS/StyleValue.cpp
+++ b/Userland/Libraries/LibWeb/CSS/StyleValue.cpp
@@ -140,6 +140,12 @@ GridTrackPlacementShorthandStyleValue const& StyleValue::as_grid_track_placement
return static_cast<GridTrackPlacementShorthandStyleValue const&>(*this);
}
+GridTemplateAreaStyleValue const& StyleValue::as_grid_template_area() const
+{
+ VERIFY(is_grid_template_area());
+ return static_cast<GridTemplateAreaStyleValue const&>(*this);
+}
+
GridTrackPlacementStyleValue const& StyleValue::as_grid_track_placement() const
{
VERIFY(is_grid_track_placement());
@@ -1434,6 +1440,29 @@ bool GridTrackPlacementStyleValue::equals(StyleValue const& other) const
return m_grid_track_placement == typed_other.grid_track_placement();
}
+ErrorOr<String> GridTemplateAreaStyleValue::to_string() const
+{
+ StringBuilder builder;
+ for (size_t y = 0; y < m_grid_template_area.size(); ++y) {
+ for (size_t x = 0; x < m_grid_template_area[y].size(); ++x) {
+ builder.appendff("{}", m_grid_template_area[y][x]);
+ if (x < m_grid_template_area[y].size() - 1)
+ builder.append(" "sv);
+ }
+ if (y < m_grid_template_area.size() - 1)
+ builder.append(", "sv);
+ }
+ return builder.to_string();
+}
+
+bool GridTemplateAreaStyleValue::equals(StyleValue const& other) const
+{
+ if (type() != other.type())
+ return false;
+ auto const& typed_other = other.as_grid_template_area();
+ return m_grid_template_area == typed_other.grid_template_area();
+}
+
ErrorOr<String> GridTrackSizeStyleValue::to_string() const
{
return m_grid_track_size_list.to_string();
@@ -2528,6 +2557,11 @@ NonnullRefPtr<ColorStyleValue> ColorStyleValue::create(Color color)
return adopt_ref(*new ColorStyleValue(color));
}
+NonnullRefPtr<GridTemplateAreaStyleValue> GridTemplateAreaStyleValue::create(Vector<Vector<String>> grid_template_area)
+{
+ return adopt_ref(*new GridTemplateAreaStyleValue(grid_template_area));
+}
+
NonnullRefPtr<GridTrackPlacementStyleValue> GridTrackPlacementStyleValue::create(CSS::GridTrackPlacement grid_track_placement)
{
return adopt_ref(*new GridTrackPlacementStyleValue(grid_track_placement));
diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h
index 33d3502738..0bb1178e0d 100644
--- a/Userland/Libraries/LibWeb/CSS/StyleValue.h
+++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h
@@ -228,6 +228,7 @@ public:
FlexFlow,
Font,
Frequency,
+ GridTemplateArea,
GridTrackPlacement,
GridTrackPlacementShorthand,
GridTrackSizeList,
@@ -275,6 +276,7 @@ public:
bool is_flex_flow() const { return type() == Type::FlexFlow; }
bool is_font() const { return type() == Type::Font; }
bool is_frequency() const { return type() == Type::Frequency; }
+ bool is_grid_template_area() const { return type() == Type::GridTemplateArea; }
bool is_grid_track_placement() const { return type() == Type::GridTrackPlacement; }
bool is_grid_track_placement_shorthand() const { return type() == Type::GridTrackPlacementShorthand; }
bool is_grid_track_size_list() const { return type() == Type::GridTrackSizeList; }
@@ -320,6 +322,7 @@ public:
FlexStyleValue const& as_flex() const;
FontStyleValue const& as_font() const;
FrequencyStyleValue const& as_frequency() const;
+ GridTemplateAreaStyleValue const& as_grid_template_area() const;
GridTrackPlacementShorthandStyleValue const& as_grid_track_placement_shorthand() const;
GridTrackPlacementStyleValue const& as_grid_track_placement() const;
GridTrackSizeStyleValue const& as_grid_track_size_list() const;
@@ -363,6 +366,7 @@ public:
FlexStyleValue& as_flex() { return const_cast<FlexStyleValue&>(const_cast<StyleValue const&>(*this).as_flex()); }
FontStyleValue& as_font() { return const_cast<FontStyleValue&>(const_cast<StyleValue const&>(*this).as_font()); }
FrequencyStyleValue& as_frequency() { return const_cast<FrequencyStyleValue&>(const_cast<StyleValue const&>(*this).as_frequency()); }
+ GridTemplateAreaStyleValue& as_grid_template_area() { return const_cast<GridTemplateAreaStyleValue&>(const_cast<StyleValue const&>(*this).as_grid_template_area()); }
GridTrackPlacementShorthandStyleValue& as_grid_track_placement_shorthand() { return const_cast<GridTrackPlacementShorthandStyleValue&>(const_cast<StyleValue const&>(*this).as_grid_track_placement_shorthand()); }
GridTrackPlacementStyleValue& as_grid_track_placement() { return const_cast<GridTrackPlacementStyleValue&>(const_cast<StyleValue const&>(*this).as_grid_track_placement()); }
GridTrackSizeStyleValue& as_grid_track_size_list() { return const_cast<GridTrackSizeStyleValue&>(const_cast<StyleValue const&>(*this).as_grid_track_size_list()); }
@@ -1052,6 +1056,25 @@ private:
Frequency m_frequency;
};
+class GridTemplateAreaStyleValue final : public StyleValue {
+public:
+ static NonnullRefPtr<GridTemplateAreaStyleValue> create(Vector<Vector<String>> grid_template_area);
+ virtual ~GridTemplateAreaStyleValue() override = default;
+
+ Vector<Vector<String>> const& grid_template_area() const { return m_grid_template_area; }
+ virtual ErrorOr<String> to_string() const override;
+ virtual bool equals(StyleValue const& other) const override;
+
+private:
+ explicit GridTemplateAreaStyleValue(Vector<Vector<String>> grid_template_area)
+ : StyleValue(Type::GridTemplateArea)
+ , m_grid_template_area(grid_template_area)
+ {
+ }
+
+ Vector<Vector<String>> m_grid_template_area;
+};
+
class GridTrackPlacementStyleValue final : public StyleValue {
public:
static NonnullRefPtr<GridTrackPlacementStyleValue> create(CSS::GridTrackPlacement grid_track_placement);
diff --git a/Userland/Libraries/LibWeb/Forward.h b/Userland/Libraries/LibWeb/Forward.h
index 712e879b31..311b61168c 100644
--- a/Userland/Libraries/LibWeb/Forward.h
+++ b/Userland/Libraries/LibWeb/Forward.h
@@ -65,6 +65,7 @@ class FrequencyStyleValue;
class GridMinMax;
class GridRepeat;
class GridSize;
+class GridTemplateAreaStyleValue;
class GridTrackPlacement;
class GridTrackPlacementShorthandStyleValue;
class GridTrackPlacementStyleValue;
diff --git a/Userland/Libraries/LibWeb/Layout/Node.cpp b/Userland/Libraries/LibWeb/Layout/Node.cpp
index 74632dac01..8eaa2a28ed 100644
--- a/Userland/Libraries/LibWeb/Layout/Node.cpp
+++ b/Userland/Libraries/LibWeb/Layout/Node.cpp
@@ -592,6 +592,7 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& computed_style)
computed_values.set_grid_column_start(computed_style.grid_column_start());
computed_values.set_grid_row_end(computed_style.grid_row_end());
computed_values.set_grid_row_start(computed_style.grid_row_start());
+ computed_values.set_grid_template_areas(computed_style.grid_template_areas());
if (auto fill = computed_style.property(CSS::PropertyID::Fill); fill->has_color())
computed_values.set_fill(fill->to_color(*this));