1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 03:37:43 +00:00

LibWeb: Handle StyleValueList for background-image

Also removed the string-parsing code I'd accidentally missed removing
before. Oops!
This commit is contained in:
Sam Atkins 2021-08-10 16:20:30 +01:00 committed by Andreas Kling
parent 6f9263de04
commit 75450d2250
3 changed files with 40 additions and 35 deletions

View file

@ -1761,16 +1761,17 @@ static inline bool is_background_repeat(StyleValue const& value)
}
}
RefPtr<StyleValue> Parser::parse_background_value(ParsingContext const& context, Vector<StyleComponentValueRule> const& component_values)
static inline bool is_background_image(StyleValue const& value)
{
auto is_background_image = [](StyleValue const& value) -> bool {
if (value.is_image())
return true;
if (value.to_identifier() == ValueID::None)
return true;
return false;
};
}
RefPtr<StyleValue> Parser::parse_background_value(ParsingContext const& context, Vector<StyleComponentValueRule> const& component_values)
{
RefPtr<StyleValue> background_color;
RefPtr<StyleValue> background_image;
RefPtr<StyleValue> repeat_x;
@ -1849,6 +1850,23 @@ RefPtr<StyleValue> Parser::parse_background_value(ParsingContext const& context,
return BackgroundStyleValue::create(background_color.release_nonnull(), background_image.release_nonnull(), repeat_x.release_nonnull(), repeat_y.release_nonnull());
}
RefPtr<StyleValue> Parser::parse_background_image_value(ParsingContext const& context, Vector<StyleComponentValueRule> const& component_values)
{
if (component_values.size() == 1) {
auto maybe_value = parse_css_value(context, PropertyID::BackgroundImage, component_values.first());
if (!maybe_value)
return nullptr;
auto value = maybe_value.release_nonnull();
if (is_background_image(*value))
return value;
return nullptr;
}
// FIXME: Handle multiple sets of comma-separated values.
dbgln("CSS Parser does not yet support multiple comma-separated values for background-image.");
return nullptr;
}
RefPtr<StyleValue> Parser::parse_background_repeat_value(ParsingContext const& context, Vector<StyleComponentValueRule> const& component_values)
{
auto is_directional_repeat = [](StyleValue const& value) -> bool {
@ -2697,6 +2715,10 @@ RefPtr<StyleValue> Parser::parse_css_value(PropertyID property_id, TokenStream<S
if (auto parsed_value = parse_background_value(m_context, component_values))
return parsed_value;
break;
case PropertyID::BackgroundImage:
if (auto parsed_value = parse_background_image_value(m_context, component_values))
return parsed_value;
break;
case PropertyID::BackgroundRepeat:
if (auto parsed_value = parse_background_repeat_value(m_context, component_values))
return parsed_value;

View file

@ -176,6 +176,7 @@ private:
static RefPtr<StyleValue> parse_string_value(ParsingContext const&, StyleComponentValueRule const&);
static RefPtr<StyleValue> parse_image_value(ParsingContext const&, StyleComponentValueRule const&);
static RefPtr<StyleValue> parse_background_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
static RefPtr<StyleValue> parse_background_image_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
static RefPtr<StyleValue> parse_background_repeat_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);
static RefPtr<StyleValue> parse_border_value(ParsingContext const&, PropertyID, Vector<StyleComponentValueRule> const&);
static RefPtr<StyleValue> parse_border_radius_value(ParsingContext const&, Vector<StyleComponentValueRule> const&);

View file

@ -392,39 +392,21 @@ static void set_property_expanding_shorthands(StyleProperties& style, CSS::Prope
}
if (property_id == CSS::PropertyID::BackgroundImage) {
// FIXME: Remove string parsing once DeprecatedCSSParser is gone.
if (value.is_string()) {
return;
auto string = value.to_string();
if (!string.starts_with("url("))
return;
if (!string.ends_with(')'))
return;
auto url = string.substring_view(4, string.length() - 5);
if (url.length() >= 2 && url.starts_with('"') && url.ends_with('"'))
url = url.substring_view(1, url.length() - 2);
else if (url.length() >= 2 && url.starts_with('\'') && url.ends_with('\''))
url = url.substring_view(1, url.length() - 2);
auto background_image_value = ImageStyleValue::create(document.complete_url(url), document);
style.set_property(CSS::PropertyID::BackgroundImage, move(background_image_value));
return;
}
if (value.is_component_value_list()) {
auto parts = static_cast<CSS::ValueListStyleValue const&>(value).values();
if (value.is_value_list()) {
auto& background_image_list = static_cast<CSS::StyleValueList const&>(value).values();
// FIXME: Handle multiple backgrounds.
if (!parts.is_empty()) {
auto first_value = Parser::parse_css_value(context, property_id, parts[0]);
if (first_value)
style.set_property(CSS::PropertyID::BackgroundImage, *first_value);
if (!background_image_list.is_empty()) {
auto& background_image = background_image_list.first();
style.set_property(CSS::PropertyID::BackgroundImage, background_image);
}
return;
}
if (value.is_builtin() || value.is_image() || value.to_identifier() == ValueID::None) {
style.set_property(CSS::PropertyID::BackgroundImage, value);
return;
}
return;
}
if (property_id == CSS::PropertyID::BackgroundRepeat) {
if (value.is_value_list()) {