LibWeb: Store ShadowStyleValue's color as a StyleValue

Colors can be specified in a way that `Gfx::Color` can't represent, such
as named system colors, `currentColor`, or functions involving `calc()`.
This commit is contained in:
Sam Atkins 2024-08-15 11:06:18 +01:00 committed by Sam Atkins
parent 581d00293c
commit 4e48afd9a7
Notes: github-actions[bot] 2024-08-21 09:53:11 +00:00
6 changed files with 20 additions and 21 deletions

View file

@ -4280,7 +4280,7 @@ RefPtr<CSSStyleValue> Parser::parse_single_shadow_value(TokenStream<ComponentVal
{
auto transaction = tokens.begin_transaction();
Optional<Color> color;
RefPtr<CSSStyleValue> color;
RefPtr<CSSStyleValue> offset_x;
RefPtr<CSSStyleValue> offset_y;
RefPtr<CSSStyleValue> blur_radius;
@ -4296,10 +4296,10 @@ RefPtr<CSSStyleValue> Parser::parse_single_shadow_value(TokenStream<ComponentVal
};
while (tokens.has_next_token()) {
if (auto maybe_color = parse_color(tokens); maybe_color.has_value()) {
if (color.has_value())
if (auto maybe_color = parse_color_value(tokens); maybe_color) {
if (color)
return nullptr;
color = maybe_color.release_value();
color = maybe_color.release_nonnull();
continue;
}
@ -4355,9 +4355,9 @@ RefPtr<CSSStyleValue> Parser::parse_single_shadow_value(TokenStream<ComponentVal
return nullptr;
}
// FIXME: If color is absent, default to `currentColor`
if (!color.has_value())
color = Color::NamedColor::Black;
// If color is absent, default to `currentColor`
if (!color)
color = CSSKeywordValue::create(Keyword::Currentcolor);
// x/y offsets are required
if (!offset_x || !offset_y)
@ -4374,7 +4374,7 @@ RefPtr<CSSStyleValue> Parser::parse_single_shadow_value(TokenStream<ComponentVal
placement = ShadowPlacement::Outer;
transaction.commit();
return ShadowStyleValue::create(color.release_value(), offset_x.release_nonnull(), offset_y.release_nonnull(), blur_radius.release_nonnull(), spread_distance.release_nonnull(), placement.release_value());
return ShadowStyleValue::create(color.release_nonnull(), offset_x.release_nonnull(), offset_y.release_nonnull(), blur_radius.release_nonnull(), spread_distance.release_nonnull(), placement.release_value());
}
RefPtr<CSSStyleValue> Parser::parse_content_value(TokenStream<ComponentValue>& tokens)

View file

@ -175,7 +175,7 @@ static RefPtr<CSSStyleValue const> style_value_for_shadow(Vector<ShadowData> con
auto make_shadow_style_value = [](ShadowData const& shadow) {
return ShadowStyleValue::create(
shadow.color,
CSSColorValue::create(shadow.color),
style_value_for_length_percentage(shadow.offset_x),
style_value_for_length_percentage(shadow.offset_y),
style_value_for_length_percentage(shadow.blur_radius),

View file

@ -1316,7 +1316,7 @@ static NonnullRefPtr<CSSStyleValue const> interpolate_box_shadow(DOM::Element& e
values.ensure_capacity(other.size());
for (size_t i = values.size(); i < other.size(); i++) {
values.unchecked_append(ShadowStyleValue::create(
Color::Transparent,
CSSColorValue::create(Color::Transparent),
LengthStyleValue::create(Length::make_px(0)),
LengthStyleValue::create(Length::make_px(0)),
LengthStyleValue::create(Length::make_px(0)),
@ -1339,7 +1339,7 @@ static NonnullRefPtr<CSSStyleValue const> interpolate_box_shadow(DOM::Element& e
auto const& from_shadow = from_shadows[i]->as_shadow();
auto const& to_shadow = to_shadows[i]->as_shadow();
auto result_shadow = ShadowStyleValue::create(
interpolate_color(from_shadow.color(), to_shadow.color(), delta),
CSSColorValue::create(interpolate_color(from_shadow.color()->to_color({}), to_shadow.color()->to_color({}), delta)),
interpolate_value(element, from_shadow.offset_x(), to_shadow.offset_x(), delta),
interpolate_value(element, from_shadow.offset_y(), to_shadow.offset_y(), delta),
interpolate_value(element, from_shadow.blur_radius(), to_shadow.blur_radius(), delta),

View file

@ -895,7 +895,7 @@ Vector<ShadowData> StyleProperties::shadow(PropertyID property_id, Layout::Node
return {};
};
auto make_shadow_data = [resolve_to_length](ShadowStyleValue const& value) -> Optional<ShadowData> {
auto make_shadow_data = [resolve_to_length, &layout_node](ShadowStyleValue const& value) -> Optional<ShadowData> {
auto maybe_offset_x = resolve_to_length(value.offset_x());
if (!maybe_offset_x.has_value())
return {};
@ -909,7 +909,7 @@ Vector<ShadowData> StyleProperties::shadow(PropertyID property_id, Layout::Node
if (!maybe_spread_distance.has_value())
return {};
return ShadowData {
value.color(),
value.color()->to_color(verify_cast<Layout::NodeWithStyle>(layout_node)),
maybe_offset_x.release_value(),
maybe_offset_y.release_value(),
maybe_blur_radius.release_value(),

View file

@ -15,8 +15,7 @@ namespace Web::CSS {
String ShadowStyleValue::to_string() const
{
StringBuilder builder;
serialize_a_srgb_value(builder, m_properties.color);
builder.appendff(" {} {} {} {}", m_properties.offset_x->to_string(), m_properties.offset_y->to_string(), m_properties.blur_radius->to_string(), m_properties.spread_distance->to_string());
builder.appendff("{} {} {} {} {}", m_properties.color->to_string(), m_properties.offset_x->to_string(), m_properties.offset_y->to_string(), m_properties.blur_radius->to_string(), m_properties.spread_distance->to_string());
if (m_properties.placement == ShadowPlacement::Inner)
builder.append(" inset"sv);
return MUST(builder.to_string());

View file

@ -23,18 +23,18 @@ enum class ShadowPlacement {
class ShadowStyleValue final : public StyleValueWithDefaultOperators<ShadowStyleValue> {
public:
static ValueComparingNonnullRefPtr<ShadowStyleValue> create(
Color color,
ValueComparingNonnullRefPtr<CSSStyleValue const> color,
ValueComparingNonnullRefPtr<CSSStyleValue const> offset_x,
ValueComparingNonnullRefPtr<CSSStyleValue const> offset_y,
ValueComparingNonnullRefPtr<CSSStyleValue const> blur_radius,
ValueComparingNonnullRefPtr<CSSStyleValue const> spread_distance,
ShadowPlacement placement)
{
return adopt_ref(*new (nothrow) ShadowStyleValue(color, move(offset_x), move(offset_y), move(blur_radius), move(spread_distance), placement));
return adopt_ref(*new (nothrow) ShadowStyleValue(move(color), move(offset_x), move(offset_y), move(blur_radius), move(spread_distance), placement));
}
virtual ~ShadowStyleValue() override = default;
Color color() const { return m_properties.color; }
ValueComparingNonnullRefPtr<CSSStyleValue const> const& color() const { return m_properties.color; }
ValueComparingNonnullRefPtr<CSSStyleValue const> const& offset_x() const { return m_properties.offset_x; }
ValueComparingNonnullRefPtr<CSSStyleValue const> const& offset_y() const { return m_properties.offset_y; }
ValueComparingNonnullRefPtr<CSSStyleValue const> const& blur_radius() const { return m_properties.blur_radius; }
@ -47,7 +47,7 @@ public:
private:
ShadowStyleValue(
Color color,
ValueComparingNonnullRefPtr<CSSStyleValue const> color,
ValueComparingNonnullRefPtr<CSSStyleValue const> offset_x,
ValueComparingNonnullRefPtr<CSSStyleValue const> offset_y,
ValueComparingNonnullRefPtr<CSSStyleValue const> blur_radius,
@ -55,7 +55,7 @@ private:
ShadowPlacement placement)
: StyleValueWithDefaultOperators(Type::Shadow)
, m_properties {
.color = color,
.color = move(color),
.offset_x = move(offset_x),
.offset_y = move(offset_y),
.blur_radius = move(blur_radius),
@ -68,7 +68,7 @@ private:
virtual ValueComparingNonnullRefPtr<CSSStyleValue const> absolutized(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const override;
struct Properties {
Color color;
ValueComparingNonnullRefPtr<CSSStyleValue const> color;
ValueComparingNonnullRefPtr<CSSStyleValue const> offset_x;
ValueComparingNonnullRefPtr<CSSStyleValue const> offset_y;
ValueComparingNonnullRefPtr<CSSStyleValue const> blur_radius;