LibWeb: Replace BorderStyleValue with ShorthandStyleValue

And also expand builtin values to the longhands, which we weren't doing
before.
This commit is contained in:
Sam Atkins 2023-09-20 12:50:48 +01:00 committed by Sam Atkins
parent 66300794a6
commit 23d59a6caf
Notes: sideshowbarker 2024-07-17 17:49:11 +09:00
11 changed files with 85 additions and 168 deletions

View file

@ -6,7 +6,6 @@ source_set("StyleValues") {
"BackgroundRepeatStyleValue.cpp",
"BackgroundSizeStyleValue.cpp",
"BorderRadiusStyleValue.cpp",
"BorderStyleValue.cpp",
"CalculatedStyleValue.cpp",
"ColorStyleValue.cpp",
"ConicGradientStyleValue.cpp",

View file

@ -83,7 +83,6 @@ set(SOURCES
CSS/StyleValues/BackgroundRepeatStyleValue.cpp
CSS/StyleValues/BackgroundSizeStyleValue.cpp
CSS/StyleValues/BorderRadiusStyleValue.cpp
CSS/StyleValues/BorderStyleValue.cpp
CSS/StyleValues/CalculatedStyleValue.cpp
CSS/StyleValues/ColorStyleValue.cpp
CSS/StyleValues/ConicGradientStyleValue.cpp

View file

@ -38,7 +38,6 @@
#include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
#include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
#include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
#include <LibWeb/CSS/StyleValues/BorderStyleValue.h>
#include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
#include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
#include <LibWeb/CSS/StyleValues/CustomIdentStyleValue.h>
@ -3207,7 +3206,7 @@ RefPtr<StyleValue> Parser::parse_single_background_size_value(TokenStream<Compon
return BackgroundSizeStyleValue::create(x_size.release_value(), y_size.release_value());
}
RefPtr<StyleValue> Parser::parse_border_value(Vector<ComponentValue> const& component_values)
RefPtr<StyleValue> Parser::parse_border_value(PropertyID property_id, Vector<ComponentValue> const& component_values)
{
if (component_values.size() > 3)
return nullptr;
@ -3216,7 +3215,41 @@ RefPtr<StyleValue> Parser::parse_border_value(Vector<ComponentValue> const& comp
RefPtr<StyleValue> border_color;
RefPtr<StyleValue> border_style;
auto remaining_longhands = Vector { PropertyID::BorderWidth, PropertyID::BorderColor, PropertyID::BorderStyle };
auto color_property = PropertyID::Invalid;
auto style_property = PropertyID::Invalid;
auto width_property = PropertyID::Invalid;
switch (property_id) {
case PropertyID::Border:
color_property = PropertyID::BorderColor;
style_property = PropertyID::BorderStyle;
width_property = PropertyID::BorderWidth;
break;
case PropertyID::BorderBottom:
color_property = PropertyID::BorderBottomColor;
style_property = PropertyID::BorderBottomStyle;
width_property = PropertyID::BorderBottomWidth;
break;
case PropertyID::BorderLeft:
color_property = PropertyID::BorderLeftColor;
style_property = PropertyID::BorderLeftStyle;
width_property = PropertyID::BorderLeftWidth;
break;
case PropertyID::BorderRight:
color_property = PropertyID::BorderRightColor;
style_property = PropertyID::BorderRightStyle;
width_property = PropertyID::BorderRightWidth;
break;
case PropertyID::BorderTop:
color_property = PropertyID::BorderTopColor;
style_property = PropertyID::BorderTopStyle;
width_property = PropertyID::BorderTopWidth;
break;
default:
VERIFY_NOT_REACHED();
}
auto remaining_longhands = Vector { width_property, color_property, style_property };
auto tokens = TokenStream { component_values };
while (tokens.has_next_token()) {
@ -3226,35 +3259,30 @@ RefPtr<StyleValue> Parser::parse_border_value(Vector<ComponentValue> const& comp
auto& value = property_and_value->style_value;
remove_property(remaining_longhands, property_and_value->property);
switch (property_and_value->property) {
case PropertyID::BorderWidth: {
if (property_and_value->property == width_property) {
VERIFY(!border_width);
border_width = value.release_nonnull();
continue;
}
case PropertyID::BorderColor: {
} else if (property_and_value->property == color_property) {
VERIFY(!border_color);
border_color = value.release_nonnull();
continue;
}
case PropertyID::BorderStyle: {
} else if (property_and_value->property == style_property) {
VERIFY(!border_style);
border_style = value.release_nonnull();
continue;
}
default:
} else {
VERIFY_NOT_REACHED();
}
}
if (!border_width)
border_width = property_initial_value(m_context.realm(), PropertyID::BorderWidth);
border_width = property_initial_value(m_context.realm(), width_property);
if (!border_style)
border_style = property_initial_value(m_context.realm(), PropertyID::BorderStyle);
border_style = property_initial_value(m_context.realm(), style_property);
if (!border_color)
border_color = property_initial_value(m_context.realm(), PropertyID::BorderColor);
border_color = property_initial_value(m_context.realm(), color_property);
return BorderStyleValue::create(border_width.release_nonnull(), border_style.release_nonnull(), border_color.release_nonnull());
return ShorthandStyleValue::create(property_id,
{ width_property, style_property, color_property },
{ border_width.release_nonnull(), border_style.release_nonnull(), border_color.release_nonnull() });
}
RefPtr<StyleValue> Parser::parse_border_radius_value(Vector<ComponentValue> const& component_values)
@ -5785,7 +5813,7 @@ Parser::ParseErrorOr<NonnullRefPtr<StyleValue>> Parser::parse_css_value(Property
case PropertyID::BorderLeft:
case PropertyID::BorderRight:
case PropertyID::BorderTop:
if (auto parsed_value = parse_border_value(component_values))
if (auto parsed_value = parse_border_value(property_id, component_values))
return parsed_value.release_nonnull();
return ParseError::SyntaxError;
case PropertyID::BorderTopLeftRadius:

View file

@ -228,7 +228,7 @@ private:
RefPtr<StyleValue> parse_single_background_position_x_or_y_value(TokenStream<ComponentValue>&, PropertyID);
RefPtr<StyleValue> parse_single_background_repeat_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue> parse_single_background_size_value(TokenStream<ComponentValue>&);
RefPtr<StyleValue> parse_border_value(Vector<ComponentValue> const&);
RefPtr<StyleValue> parse_border_value(PropertyID, Vector<ComponentValue> const&);
RefPtr<StyleValue> parse_border_radius_value(Vector<ComponentValue> const&);
RefPtr<StyleValue> parse_border_radius_shorthand_value(Vector<ComponentValue> const&);
RefPtr<StyleValue> parse_content_value(Vector<ComponentValue> const&);

View file

@ -15,7 +15,6 @@
#include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
#include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
#include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
#include <LibWeb/CSS/StyleValues/BorderStyleValue.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
#include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
#include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
@ -193,14 +192,18 @@ RefPtr<StyleValue const> ResolvedCSSStyleDeclaration::style_value_for_property(L
auto width = LengthStyleValue::create(Length::make_px(top.width));
auto style = IdentifierStyleValue::create(to_value_id(top.line_style));
auto color = ColorStyleValue::create(top.color);
return BorderStyleValue::create(width, style, color);
return ShorthandStyleValue::create(property_id,
{ PropertyID::BorderWidth, PropertyID::BorderStyle, PropertyID::BorderColor },
{ width, style, color });
}
case PropertyID::BorderBottom: {
auto border = layout_node.computed_values().border_bottom();
auto width = LengthStyleValue::create(Length::make_px(border.width));
auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
auto color = ColorStyleValue::create(border.color);
return BorderStyleValue::create(width, style, color);
return ShorthandStyleValue::create(property_id,
{ PropertyID::BorderBottomWidth, PropertyID::BorderBottomStyle, PropertyID::BorderBottomColor },
{ width, style, color });
}
case PropertyID::BorderBottomColor:
return ColorStyleValue::create(layout_node.computed_values().border_bottom().color);
@ -216,7 +219,9 @@ RefPtr<StyleValue const> ResolvedCSSStyleDeclaration::style_value_for_property(L
auto width = LengthStyleValue::create(Length::make_px(border.width));
auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
auto color = ColorStyleValue::create(border.color);
return BorderStyleValue::create(width, style, color);
return ShorthandStyleValue::create(property_id,
{ PropertyID::BorderLeftWidth, PropertyID::BorderLeftStyle, PropertyID::BorderLeftColor },
{ width, style, color });
}
case PropertyID::BorderLeftColor:
return ColorStyleValue::create(layout_node.computed_values().border_left().color);
@ -252,7 +257,9 @@ RefPtr<StyleValue const> ResolvedCSSStyleDeclaration::style_value_for_property(L
auto width = LengthStyleValue::create(Length::make_px(border.width));
auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
auto color = ColorStyleValue::create(border.color);
return BorderStyleValue::create(width, style, color);
return ShorthandStyleValue::create(property_id,
{ PropertyID::BorderRightWidth, PropertyID::BorderRightStyle, PropertyID::BorderRightColor },
{ width, style, color });
}
case PropertyID::BorderRightColor:
return ColorStyleValue::create(layout_node.computed_values().border_right().color);
@ -268,7 +275,9 @@ RefPtr<StyleValue const> ResolvedCSSStyleDeclaration::style_value_for_property(L
auto width = LengthStyleValue::create(Length::make_px(border.width));
auto style = IdentifierStyleValue::create(to_value_id(border.line_style));
auto color = ColorStyleValue::create(border.color);
return BorderStyleValue::create(width, style, color);
return ShorthandStyleValue::create(property_id,
{ PropertyID::BorderTopWidth, PropertyID::BorderTopStyle, PropertyID::BorderTopColor },
{ width, style, color });
}
case PropertyID::BorderTopColor:
return ColorStyleValue::create(layout_node.computed_values().border_top().color);

View file

@ -31,7 +31,6 @@
#include <LibWeb/CSS/StyleSheet.h>
#include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
#include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
#include <LibWeb/CSS/StyleValues/BorderStyleValue.h>
#include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
#include <LibWeb/CSS/StyleValues/CustomIdentStyleValue.h>
#include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
@ -554,63 +553,28 @@ static void set_property_expanding_shorthands(StyleProperties& style, CSS::Prope
return;
}
if (property_id == CSS::PropertyID::BorderTop
|| property_id == CSS::PropertyID::BorderRight
|| property_id == CSS::PropertyID::BorderBottom
|| property_id == CSS::PropertyID::BorderLeft) {
if (value.is_border()) {
auto const& border = value.as_border();
if (property_id == CSS::PropertyID::BorderTop) {
set_longhand_property(PropertyID::BorderTopWidth, border.border_width());
set_longhand_property(PropertyID::BorderTopStyle, border.border_style());
set_longhand_property(PropertyID::BorderTopColor, border.border_color());
return;
}
if (property_id == CSS::PropertyID::BorderRight) {
set_longhand_property(PropertyID::BorderRightWidth, border.border_width());
set_longhand_property(PropertyID::BorderRightStyle, border.border_style());
set_longhand_property(PropertyID::BorderRightColor, border.border_color());
return;
}
if (property_id == CSS::PropertyID::BorderBottom) {
set_longhand_property(PropertyID::BorderBottomWidth, border.border_width());
set_longhand_property(PropertyID::BorderBottomStyle, border.border_style());
set_longhand_property(PropertyID::BorderBottomColor, border.border_color());
return;
}
if (property_id == CSS::PropertyID::BorderLeft) {
set_longhand_property(PropertyID::BorderLeftWidth, border.border_width());
set_longhand_property(PropertyID::BorderLeftStyle, border.border_style());
set_longhand_property(PropertyID::BorderLeftColor, border.border_color());
return;
}
return;
}
if (property_id == CSS::PropertyID::BorderTop) {
set_longhand_property(PropertyID::BorderTopWidth, value);
set_longhand_property(PropertyID::BorderTopStyle, value);
set_longhand_property(PropertyID::BorderTopColor, value);
return;
}
if (property_id == CSS::PropertyID::BorderRight) {
set_longhand_property(PropertyID::BorderRightWidth, value);
set_longhand_property(PropertyID::BorderRightStyle, value);
set_longhand_property(PropertyID::BorderRightColor, value);
return;
}
if (property_id == CSS::PropertyID::BorderBottom) {
set_longhand_property(PropertyID::BorderBottomWidth, value);
set_longhand_property(PropertyID::BorderBottomStyle, value);
set_longhand_property(PropertyID::BorderBottomColor, value);
return;
}
if (property_id == CSS::PropertyID::BorderLeft) {
set_longhand_property(PropertyID::BorderLeftWidth, value);
set_longhand_property(PropertyID::BorderLeftStyle, value);
set_longhand_property(PropertyID::BorderLeftColor, value);
return
}
if (property_id == CSS::PropertyID::BorderTop) {
set_longhand_property(PropertyID::BorderTopWidth, value);
set_longhand_property(PropertyID::BorderTopStyle, value);
set_longhand_property(PropertyID::BorderTopColor, value);
return;
}
if (property_id == CSS::PropertyID::BorderRight) {
set_longhand_property(PropertyID::BorderRightWidth, value);
set_longhand_property(PropertyID::BorderRightStyle, value);
set_longhand_property(PropertyID::BorderRightColor, value);
return;
}
if (property_id == CSS::PropertyID::BorderBottom) {
set_longhand_property(PropertyID::BorderBottomWidth, value);
set_longhand_property(PropertyID::BorderBottomStyle, value);
set_longhand_property(PropertyID::BorderBottomColor, value);
return;
}
if (property_id == CSS::PropertyID::BorderLeft) {
set_longhand_property(PropertyID::BorderLeftWidth, value);
set_longhand_property(PropertyID::BorderLeftStyle, value);
set_longhand_property(PropertyID::BorderLeftColor, value);
return;
}

View file

@ -15,7 +15,6 @@
#include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
#include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
#include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
#include <LibWeb/CSS/StyleValues/BorderStyleValue.h>
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
#include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
#include <LibWeb/CSS/StyleValues/ConicGradientStyleValue.h>

View file

@ -86,7 +86,6 @@ using StyleValueVector = Vector<ValueComparingNonnullRefPtr<StyleValue const>>;
__ENUMERATE_STYLE_VALUE_TYPE(Angle, angle) \
__ENUMERATE_STYLE_VALUE_TYPE(BackgroundRepeat, background_repeat) \
__ENUMERATE_STYLE_VALUE_TYPE(BackgroundSize, background_size) \
__ENUMERATE_STYLE_VALUE_TYPE(Border, border) \
__ENUMERATE_STYLE_VALUE_TYPE(BorderRadius, border_radius) \
__ENUMERATE_STYLE_VALUE_TYPE(Calculated, calculated) \
__ENUMERATE_STYLE_VALUE_TYPE(Color, color) \

View file

@ -1,30 +0,0 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "BorderStyleValue.h"
namespace Web::CSS {
BorderStyleValue::BorderStyleValue(
ValueComparingNonnullRefPtr<StyleValue> border_width,
ValueComparingNonnullRefPtr<StyleValue> border_style,
ValueComparingNonnullRefPtr<StyleValue> border_color)
: StyleValueWithDefaultOperators(Type::Border)
, m_properties { .border_width = move(border_width), .border_style = move(border_style), .border_color = move(border_color) }
{
}
BorderStyleValue::~BorderStyleValue() = default;
String BorderStyleValue::to_string() const
{
return MUST(String::formatted("{} {} {}", m_properties.border_width->to_string(), m_properties.border_style->to_string(), m_properties.border_color->to_string()));
}
}

View file

@ -1,49 +0,0 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/CSS/StyleValue.h>
namespace Web::CSS {
class BorderStyleValue final : public StyleValueWithDefaultOperators<BorderStyleValue> {
public:
static ValueComparingNonnullRefPtr<BorderStyleValue> create(
ValueComparingNonnullRefPtr<StyleValue> border_width,
ValueComparingNonnullRefPtr<StyleValue> border_style,
ValueComparingNonnullRefPtr<StyleValue> border_color)
{
return adopt_ref(*new (nothrow) BorderStyleValue(move(border_width), move(border_style), move(border_color)));
}
virtual ~BorderStyleValue() override;
ValueComparingNonnullRefPtr<StyleValue> border_width() const { return m_properties.border_width; }
ValueComparingNonnullRefPtr<StyleValue> border_style() const { return m_properties.border_style; }
ValueComparingNonnullRefPtr<StyleValue> border_color() const { return m_properties.border_color; }
virtual String to_string() const override;
bool properties_equal(BorderStyleValue const& other) const { return m_properties == other.m_properties; }
private:
BorderStyleValue(
ValueComparingNonnullRefPtr<StyleValue> border_width,
ValueComparingNonnullRefPtr<StyleValue> border_style,
ValueComparingNonnullRefPtr<StyleValue> border_color);
struct Properties {
ValueComparingNonnullRefPtr<StyleValue> border_width;
ValueComparingNonnullRefPtr<StyleValue> border_style;
ValueComparingNonnullRefPtr<StyleValue> border_color;
bool operator==(Properties const&) const = default;
} m_properties;
};
}

View file

@ -75,7 +75,6 @@ class AngleStyleValue;
class BackgroundRepeatStyleValue;
class BackgroundSizeStyleValue;
class BorderRadiusStyleValue;
class BorderStyleValue;
class CSSConditionRule;
class CSSFontFaceRule;
class CSSGroupingRule;