LibWeb: Add mode flag to CSSStyleValue::to_string()

This will be used to differentiate between serialization for resolved
style (i.e window.getComputedStyle()) and serialization for all other
purposes.
This commit is contained in:
Andreas Kling 2024-12-07 00:59:49 +01:00 committed by Sam Atkins
commit e85c3c97fb
Notes: github-actions[bot] 2024-12-07 08:32:08 +00:00
112 changed files with 217 additions and 208 deletions

View file

@ -36,7 +36,7 @@ ValueComparingRefPtr<CSSStyleValue const> ShorthandStyleValue::longhand(Property
return nullptr;
}
String ShorthandStyleValue::to_string() const
String ShorthandStyleValue::to_string(SerializationMode mode) const
{
// If all the longhands are the same CSS-wide keyword, just return that once.
Optional<Keyword> built_in_keyword;
@ -57,7 +57,7 @@ String ShorthandStyleValue::to_string() const
}
}
if (all_same_keyword && built_in_keyword.has_value())
return m_properties.values.first()->to_string();
return m_properties.values.first()->to_string(mode);
// Then special cases
switch (m_properties.shorthand_property) {
@ -78,13 +78,13 @@ String ShorthandStyleValue::to_string() const
auto layer_count = max(get_layer_count(image), max(get_layer_count(position), max(get_layer_count(size), max(get_layer_count(repeat), max(get_layer_count(attachment), max(get_layer_count(origin), get_layer_count(clip)))))));
if (layer_count == 1) {
return MUST(String::formatted("{} {} {} {} {} {} {} {}", color->to_string(), image->to_string(), position->to_string(), size->to_string(), repeat->to_string(), attachment->to_string(), origin->to_string(), clip->to_string()));
return MUST(String::formatted("{} {} {} {} {} {} {} {}", color->to_string(mode), image->to_string(mode), position->to_string(mode), size->to_string(mode), repeat->to_string(mode), attachment->to_string(mode), origin->to_string(mode), clip->to_string(mode)));
}
auto get_layer_value_string = [](ValueComparingRefPtr<CSSStyleValue const> const& style_value, size_t index) {
auto get_layer_value_string = [mode](ValueComparingRefPtr<CSSStyleValue const> const& style_value, size_t index) {
if (style_value->is_value_list())
return style_value->as_value_list().value_at(index, true)->to_string();
return style_value->to_string();
return style_value->as_value_list().value_at(index, true)->to_string(mode);
return style_value->to_string(mode);
};
StringBuilder builder;
@ -92,7 +92,7 @@ String ShorthandStyleValue::to_string() const
if (i)
builder.append(", "sv);
if (i == layer_count - 1)
builder.appendff("{} ", color->to_string());
builder.appendff("{} ", color->to_string(mode));
builder.appendff("{} {} {} {} {} {} {}", get_layer_value_string(image, i), get_layer_value_string(position, i), get_layer_value_string(size, i), get_layer_value_string(repeat, i), get_layer_value_string(attachment, i), get_layer_value_string(origin, i), get_layer_value_string(clip, i));
}
@ -115,8 +115,8 @@ String ShorthandStyleValue::to_string() const
bottom_left.vertical_radius().to_string()));
}
case PropertyID::Columns: {
auto column_width = longhand(PropertyID::ColumnWidth)->to_string();
auto column_count = longhand(PropertyID::ColumnCount)->to_string();
auto column_width = longhand(PropertyID::ColumnWidth)->to_string(mode);
auto column_count = longhand(PropertyID::ColumnCount)->to_string(mode);
if (column_width == column_count)
return column_width;
@ -128,18 +128,18 @@ String ShorthandStyleValue::to_string() const
return MUST(String::formatted("{} {}", column_width, column_count));
}
case PropertyID::Flex:
return MUST(String::formatted("{} {} {}", longhand(PropertyID::FlexGrow)->to_string(), longhand(PropertyID::FlexShrink)->to_string(), longhand(PropertyID::FlexBasis)->to_string()));
return MUST(String::formatted("{} {} {}", longhand(PropertyID::FlexGrow)->to_string(mode), longhand(PropertyID::FlexShrink)->to_string(mode), longhand(PropertyID::FlexBasis)->to_string(mode)));
case PropertyID::FlexFlow:
return MUST(String::formatted("{} {}", longhand(PropertyID::FlexDirection)->to_string(), longhand(PropertyID::FlexWrap)->to_string()));
return MUST(String::formatted("{} {}", longhand(PropertyID::FlexDirection)->to_string(mode), longhand(PropertyID::FlexWrap)->to_string(mode)));
case PropertyID::Font:
return MUST(String::formatted("{} {} {} {} {} / {} {}",
longhand(PropertyID::FontStyle)->to_string(),
longhand(PropertyID::FontVariant)->to_string(),
longhand(PropertyID::FontWeight)->to_string(),
longhand(PropertyID::FontWidth)->to_string(),
longhand(PropertyID::FontSize)->to_string(),
longhand(PropertyID::LineHeight)->to_string(),
longhand(PropertyID::FontFamily)->to_string()));
longhand(PropertyID::FontStyle)->to_string(mode),
longhand(PropertyID::FontVariant)->to_string(mode),
longhand(PropertyID::FontWeight)->to_string(mode),
longhand(PropertyID::FontWidth)->to_string(mode),
longhand(PropertyID::FontSize)->to_string(mode),
longhand(PropertyID::LineHeight)->to_string(mode),
longhand(PropertyID::FontFamily)->to_string(mode)));
case PropertyID::GridArea: {
auto& row_start = longhand(PropertyID::GridRowStart)->as_grid_track_placement();
auto& column_start = longhand(PropertyID::GridColumnStart)->as_grid_track_placement();
@ -192,43 +192,43 @@ String ShorthandStyleValue::to_string() const
auto start = longhand(PropertyID::GridColumnStart);
auto end = longhand(PropertyID::GridColumnEnd);
if (end->as_grid_track_placement().grid_track_placement().is_auto())
return start->to_string();
return MUST(String::formatted("{} / {}", start->to_string(), end->to_string()));
return start->to_string(mode);
return MUST(String::formatted("{} / {}", start->to_string(mode), end->to_string(mode)));
}
case PropertyID::GridRow: {
auto start = longhand(PropertyID::GridRowStart);
auto end = longhand(PropertyID::GridRowEnd);
if (end->as_grid_track_placement().grid_track_placement().is_auto())
return start->to_string();
return MUST(String::formatted("{} / {}", start->to_string(), end->to_string()));
return start->to_string(mode);
return MUST(String::formatted("{} / {}", start->to_string(mode), end->to_string(mode)));
}
case PropertyID::ListStyle:
return MUST(String::formatted("{} {} {}", longhand(PropertyID::ListStylePosition)->to_string(), longhand(PropertyID::ListStyleImage)->to_string(), longhand(PropertyID::ListStyleType)->to_string()));
return MUST(String::formatted("{} {} {}", longhand(PropertyID::ListStylePosition)->to_string(mode), longhand(PropertyID::ListStyleImage)->to_string(mode), longhand(PropertyID::ListStyleType)->to_string(mode)));
case PropertyID::Overflow: {
auto overflow_x = longhand(PropertyID::OverflowX);
auto overflow_y = longhand(PropertyID::OverflowY);
if (overflow_x == overflow_y)
return overflow_x->to_string();
return overflow_x->to_string(mode);
return MUST(String::formatted("{} {}", overflow_x->to_string(), overflow_y->to_string()));
return MUST(String::formatted("{} {}", overflow_x->to_string(mode), overflow_y->to_string(mode)));
}
case PropertyID::PlaceContent: {
auto align_content = longhand(PropertyID::AlignContent)->to_string();
auto justify_content = longhand(PropertyID::JustifyContent)->to_string();
auto align_content = longhand(PropertyID::AlignContent)->to_string(mode);
auto justify_content = longhand(PropertyID::JustifyContent)->to_string(mode);
if (align_content == justify_content)
return align_content;
return MUST(String::formatted("{} {}", align_content, justify_content));
}
case PropertyID::PlaceItems: {
auto align_items = longhand(PropertyID::AlignItems)->to_string();
auto justify_items = longhand(PropertyID::JustifyItems)->to_string();
auto align_items = longhand(PropertyID::AlignItems)->to_string(mode);
auto justify_items = longhand(PropertyID::JustifyItems)->to_string(mode);
if (align_items == justify_items)
return align_items;
return MUST(String::formatted("{} {}", align_items, justify_items));
}
case PropertyID::PlaceSelf: {
auto align_self = longhand(PropertyID::AlignSelf)->to_string();
auto justify_self = longhand(PropertyID::JustifySelf)->to_string();
auto align_self = longhand(PropertyID::AlignSelf)->to_string(mode);
auto justify_self = longhand(PropertyID::JustifySelf)->to_string(mode);
if (align_self == justify_self)
return align_self;
return MUST(String::formatted("{} {}", align_self, justify_self));
@ -242,7 +242,7 @@ String ShorthandStyleValue::to_string() const
if (!value->equals(property_initial_value(property_id))) {
if (!builder.is_empty())
builder.append(' ');
builder.append(value->to_string());
builder.append(value->to_string(mode));
}
};
@ -252,7 +252,7 @@ String ShorthandStyleValue::to_string() const
append_if_non_default(PropertyID::TextDecorationColor);
if (builder.is_empty())
return longhand(PropertyID::TextDecorationLine)->to_string();
return longhand(PropertyID::TextDecorationLine)->to_string(mode);
return builder.to_string_without_validation();
}
@ -264,7 +264,7 @@ String ShorthandStyleValue::to_string() const
first = false;
else
builder.append(' ');
builder.append(value->to_string());
builder.append(value->to_string(mode));
}
return MUST(builder.to_string());
}