LibWeb: Replace GridTrackSizeListShorthandStyleValue with ShorthandSV

This commit is contained in:
Sam Atkins 2023-09-20 14:50:44 +01:00 committed by Sam Atkins
parent 48f3603119
commit f5cb2e8dc2
Notes: sideshowbarker 2024-07-17 21:26:19 +09:00
12 changed files with 49 additions and 125 deletions

View file

@ -18,7 +18,6 @@ source_set("StyleValues") {
"GridTemplateAreaStyleValue.cpp",
"GridTrackPlacementShorthandStyleValue.cpp",
"GridTrackPlacementStyleValue.cpp",
"GridTrackSizeListShorthandStyleValue.cpp",
"GridTrackSizeListStyleValue.cpp",
"IdentifierStyleValue.cpp",
"ImageStyleValue.cpp",

View file

@ -95,7 +95,6 @@ set(SOURCES
CSS/StyleValues/GridTemplateAreaStyleValue.cpp
CSS/StyleValues/GridTrackPlacementStyleValue.cpp
CSS/StyleValues/GridTrackPlacementShorthandStyleValue.cpp
CSS/StyleValues/GridTrackSizeListShorthandStyleValue.cpp
CSS/StyleValues/GridTrackSizeListStyleValue.cpp
CSS/StyleValues/IdentifierStyleValue.cpp
CSS/StyleValues/ImageStyleValue.cpp

View file

@ -50,7 +50,6 @@
#include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackPlacementShorthandStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackSizeListShorthandStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
#include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
#include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
@ -5554,7 +5553,7 @@ RefPtr<StyleValue> Parser::parse_grid_track_placement_shorthand_value(Vector<Com
// https://www.w3.org/TR/css-grid-2/#explicit-grid-shorthand
// 7.4. Explicit Grid Shorthand: the grid-template property
RefPtr<StyleValue> Parser::parse_grid_track_size_list_shorthand_value(Vector<ComponentValue> const& component_values)
RefPtr<StyleValue> Parser::parse_grid_track_size_list_shorthand_value(PropertyID property_id, Vector<ComponentValue> const& component_values)
{
// The grid-template property is a shorthand for setting grid-template-columns, grid-template-rows,
// and grid-template-areas in a single declaration. It has several distinct syntax forms:
@ -5593,10 +5592,9 @@ RefPtr<StyleValue> Parser::parse_grid_track_size_list_shorthand_value(Vector<Com
auto parsed_template_areas_values = parse_grid_template_areas_value(template_area_tokens);
auto parsed_template_rows_values = parse_grid_track_size_list(template_rows_tokens, true);
auto parsed_template_columns_values = parse_grid_track_size_list(template_columns_tokens);
return GridTrackSizeListShorthandStyleValue::create(
parsed_template_areas_values.release_nonnull()->as_grid_template_area(),
parsed_template_rows_values.release_nonnull()->as_grid_track_size_list(),
parsed_template_columns_values.release_nonnull()->as_grid_track_size_list());
return ShorthandStyleValue::create(property_id,
{ PropertyID::GridTemplateAreas, PropertyID::GridTemplateRows, PropertyID::GridTemplateColumns },
{ parsed_template_areas_values.release_nonnull(), parsed_template_rows_values.release_nonnull(), parsed_template_columns_values.release_nonnull() });
}
RefPtr<StyleValue> Parser::parse_grid_area_shorthand_value(Vector<ComponentValue> const& component_values)
@ -5682,7 +5680,7 @@ RefPtr<StyleValue> Parser::parse_grid_shorthand_value(Vector<ComponentValue> con
// <'grid-template'> |
// FIXME: <'grid-template-rows'> / [ auto-flow && dense? ] <'grid-auto-columns'>? |
// FIXME: [ auto-flow && dense? ] <'grid-auto-rows'>? / <'grid-template-columns'>
return parse_grid_track_size_list_shorthand_value(component_value);
return parse_grid_track_size_list_shorthand_value(PropertyID::Grid, component_value);
}
RefPtr<StyleValue> Parser::parse_grid_template_areas_value(Vector<ComponentValue> const& component_values)
@ -5899,7 +5897,7 @@ Parser::ParseErrorOr<NonnullRefPtr<StyleValue>> Parser::parse_css_value(Property
return parsed_value.release_nonnull();
return ParseError::SyntaxError;
case PropertyID::GridTemplate:
if (auto parsed_value = parse_grid_track_size_list_shorthand_value(component_values))
if (auto parsed_value = parse_grid_track_size_list_shorthand_value(property_id, component_values))
return parsed_value.release_nonnull();
return ParseError::SyntaxError;
case PropertyID::GridTemplateColumns:

View file

@ -258,7 +258,7 @@ private:
RefPtr<StyleValue> parse_grid_track_size_list(Vector<ComponentValue> const&, bool allow_separate_line_name_blocks = false);
RefPtr<StyleValue> parse_grid_auto_track_sizes(Vector<ComponentValue> const&);
[[nodiscard]] RefPtr<GridAutoFlowStyleValue> parse_grid_auto_flow_value(Vector<ComponentValue> const&);
RefPtr<StyleValue> parse_grid_track_size_list_shorthand_value(Vector<ComponentValue> const&);
RefPtr<StyleValue> parse_grid_track_size_list_shorthand_value(PropertyID, 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&);

View file

@ -20,7 +20,6 @@
#include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackPlacementShorthandStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackSizeListShorthandStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
#include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
#include <LibWeb/CSS/StyleValues/InitialStyleValue.h>
@ -349,21 +348,23 @@ RefPtr<StyleValue const> ResolvedCSSStyleDeclaration::style_value_for_property(L
auto maybe_grid_template_areas = property(PropertyID::GridTemplateAreas);
auto maybe_grid_template_rows = property(PropertyID::GridTemplateRows);
auto maybe_grid_template_columns = property(PropertyID::GridTemplateColumns);
RefPtr<GridTemplateAreaStyleValue const> grid_template_areas;
RefPtr<GridTrackSizeListStyleValue const> grid_template_rows, grid_template_columns;
RefPtr<StyleValue const> grid_template_areas;
RefPtr<StyleValue const> grid_template_rows, grid_template_columns;
if (maybe_grid_template_areas.has_value()) {
VERIFY(maybe_grid_template_areas.value().value->is_grid_template_area());
grid_template_areas = maybe_grid_template_areas.value().value->as_grid_template_area();
grid_template_areas = maybe_grid_template_areas.value().value;
}
if (maybe_grid_template_rows.has_value()) {
VERIFY(maybe_grid_template_rows.value().value->is_grid_track_size_list());
grid_template_rows = maybe_grid_template_rows.value().value->as_grid_track_size_list();
grid_template_rows = maybe_grid_template_rows.value().value;
}
if (maybe_grid_template_columns.has_value()) {
VERIFY(maybe_grid_template_columns.value().value->is_grid_track_size_list());
grid_template_columns = maybe_grid_template_columns.value().value->as_grid_track_size_list();
grid_template_columns = maybe_grid_template_columns.value().value;
}
return GridTrackSizeListShorthandStyleValue::create(grid_template_areas.release_nonnull(), grid_template_rows.release_nonnull(), grid_template_columns.release_nonnull());
return ShorthandStyleValue::create(property_id,
{ PropertyID::GridTemplateAreas, PropertyID::GridTemplateRows, PropertyID::GridTemplateColumns },
{ grid_template_areas.release_nonnull(), grid_template_rows.release_nonnull(), grid_template_columns.release_nonnull() });
}
case PropertyID::Height:
return style_value_for_size(layout_node.computed_values().height());

View file

@ -38,7 +38,6 @@
#include <LibWeb/CSS/StyleValues/FilterValueListStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackPlacementShorthandStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackSizeListShorthandStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
#include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
#include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
@ -779,13 +778,6 @@ static void set_property_expanding_shorthands(StyleProperties& style, CSS::Prope
}
if (property_id == CSS::PropertyID::GridTemplate || property_id == CSS::PropertyID::Grid) {
if (value.is_grid_track_size_list_shorthand()) {
auto const& shorthand = value.as_grid_track_size_list_shorthand();
set_longhand_property(CSS::PropertyID::GridTemplateAreas, shorthand.areas());
set_longhand_property(CSS::PropertyID::GridTemplateRows, shorthand.rows());
set_longhand_property(CSS::PropertyID::GridTemplateColumns, shorthand.columns());
return;
}
set_longhand_property(CSS::PropertyID::GridTemplateAreas, value);
set_longhand_property(CSS::PropertyID::GridTemplateRows, value);
set_longhand_property(CSS::PropertyID::GridTemplateColumns, value);

View file

@ -29,7 +29,6 @@
#include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackPlacementShorthandStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackSizeListShorthandStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
#include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
#include <LibWeb/CSS/StyleValues/ImageStyleValue.h>

View file

@ -102,7 +102,6 @@ using StyleValueVector = Vector<ValueComparingNonnullRefPtr<StyleValue const>>;
__ENUMERATE_STYLE_VALUE_TYPE(GridTrackPlacement, grid_track_placement) \
__ENUMERATE_STYLE_VALUE_TYPE(GridTrackPlacementShorthand, grid_track_placement_shorthand) \
__ENUMERATE_STYLE_VALUE_TYPE(GridTrackSizeList, grid_track_size_list) \
__ENUMERATE_STYLE_VALUE_TYPE(GridTrackSizeListShorthand, grid_track_size_list_shorthand) \
__ENUMERATE_STYLE_VALUE_TYPE(Identifier, identifier) \
__ENUMERATE_STYLE_VALUE_TYPE(Image, image) \
__ENUMERATE_STYLE_VALUE_TYPE(Inherit, inherit) \

View file

@ -1,48 +0,0 @@
/*
* Copyright (c) 2023, Martin Falisse <mfalisse@outlook.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "GridTrackSizeListShorthandStyleValue.h"
#include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
namespace Web::CSS {
ValueComparingNonnullRefPtr<GridTrackSizeListShorthandStyleValue> GridTrackSizeListShorthandStyleValue::create(
ValueComparingNonnullRefPtr<GridTemplateAreaStyleValue const> areas,
ValueComparingNonnullRefPtr<GridTrackSizeListStyleValue const> rows,
ValueComparingNonnullRefPtr<GridTrackSizeListStyleValue const> columns)
{
return adopt_ref(*new (nothrow) GridTrackSizeListShorthandStyleValue(move(areas), move(rows), move(columns)));
}
String GridTrackSizeListShorthandStyleValue::to_string() const
{
auto construct_rows_string = [&]() {
StringBuilder builder;
size_t idx = 0;
for (auto const& row : m_properties.rows->grid_track_size_list().track_list()) {
if (m_properties.areas->grid_template_area().size() > idx) {
builder.append("\""sv);
for (size_t y = 0; y < m_properties.areas->grid_template_area()[idx].size(); ++y) {
builder.append(m_properties.areas->grid_template_area()[idx][y]);
if (y != m_properties.areas->grid_template_area()[idx].size() - 1)
builder.append(" "sv);
}
builder.append("\" "sv);
}
builder.append(row.to_string());
if (idx < m_properties.rows->grid_track_size_list().track_list().size() - 1)
builder.append(' ');
idx++;
}
return MUST(builder.to_string());
};
if (m_properties.columns->grid_track_size_list().track_list().size() == 0)
return MUST(String::formatted("{}", construct_rows_string()));
return MUST(String::formatted("{} / {}", construct_rows_string(), m_properties.columns->grid_track_size_list().to_string()));
}
}

View file

@ -1,48 +0,0 @@
/*
* Copyright (c) 2023, Martin Falisse <mfalisse@outlook.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/CSS/StyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
namespace Web::CSS {
class GridTrackSizeListShorthandStyleValue final : public StyleValueWithDefaultOperators<GridTrackSizeListShorthandStyleValue> {
public:
static ValueComparingNonnullRefPtr<GridTrackSizeListShorthandStyleValue> create(
ValueComparingNonnullRefPtr<GridTemplateAreaStyleValue const> areas,
ValueComparingNonnullRefPtr<GridTrackSizeListStyleValue const> rows,
ValueComparingNonnullRefPtr<GridTrackSizeListStyleValue const> columns);
virtual ~GridTrackSizeListShorthandStyleValue() override = default;
auto rows() const { return m_properties.rows; }
auto columns() const { return m_properties.columns; }
auto areas() const { return m_properties.areas; }
virtual String to_string() const override;
bool properties_equal(GridTrackSizeListShorthandStyleValue const& other) const { return m_properties == other.m_properties; }
private:
GridTrackSizeListShorthandStyleValue(
ValueComparingNonnullRefPtr<GridTemplateAreaStyleValue const> areas,
ValueComparingNonnullRefPtr<GridTrackSizeListStyleValue const> rows,
ValueComparingNonnullRefPtr<GridTrackSizeListStyleValue const> columns)
: StyleValueWithDefaultOperators(Type::GridTrackSizeListShorthand)
, m_properties { .areas = move(areas), .rows = move(rows), .columns = move(columns) }
{
}
struct Properties {
ValueComparingNonnullRefPtr<GridTemplateAreaStyleValue const> areas;
ValueComparingNonnullRefPtr<GridTrackSizeListStyleValue const> rows;
ValueComparingNonnullRefPtr<GridTrackSizeListStyleValue const> columns;
bool operator==(Properties const&) const = default;
} m_properties;
};
}

View file

@ -8,7 +8,9 @@
#include "ShorthandStyleValue.h"
#include <LibWeb/CSS/PropertyID.h>
#include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
#include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
#include <LibWeb/CSS/StyleValues/StyleValueList.h>
namespace Web::CSS {
@ -110,6 +112,38 @@ String ShorthandStyleValue::to_string() const
if (!column_end.grid_track_placement().is_auto())
builder.appendff(" / {}", column_end.grid_track_placement().to_string());
return MUST(builder.to_string());
}
// FIXME: Serialize Grid differently once we support it better!
case PropertyID::Grid:
case PropertyID::GridTemplate: {
auto& areas = longhand(PropertyID::GridTemplateAreas)->as_grid_template_area();
auto& rows = longhand(PropertyID::GridTemplateRows)->as_grid_track_size_list();
auto& columns = longhand(PropertyID::GridTemplateColumns)->as_grid_track_size_list();
auto construct_rows_string = [&]() {
StringBuilder builder;
size_t idx = 0;
for (auto const& row : rows.grid_track_size_list().track_list()) {
if (areas.grid_template_area().size() > idx) {
builder.append("\""sv);
for (size_t y = 0; y < areas.grid_template_area()[idx].size(); ++y) {
builder.append(areas.grid_template_area()[idx][y]);
if (y != areas.grid_template_area()[idx].size() - 1)
builder.append(" "sv);
}
builder.append("\" "sv);
}
builder.append(row.to_string());
if (idx < rows.grid_track_size_list().track_list().size() - 1)
builder.append(' ');
idx++;
}
return MUST(builder.to_string());
};
if (columns.grid_track_size_list().track_list().size() == 0)
return MUST(String::formatted("{}", construct_rows_string()));
return MUST(String::formatted("{} / {}", construct_rows_string(), columns.grid_track_size_list().to_string()));
}
default:
StringBuilder builder;

View file

@ -115,7 +115,6 @@ class GridTrackPlacement;
class GridTrackPlacementShorthandStyleValue;
class GridTrackPlacementStyleValue;
class GridTrackSizeList;
class GridTrackSizeListShorthandStyleValue;
class GridTrackSizeListStyleValue;
class IdentifierStyleValue;
class ImageStyleValue;