Bladeren bron

LibWeb: Parse background-size as part of background shorthand

Sam Atkins 3 jaren geleden
bovenliggende
commit
96936d04d6

+ 18 - 3
Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp

@@ -2394,9 +2394,9 @@ RefPtr<StyleValue> Parser::parse_background_value(ParsingContext const& context,
 {
     RefPtr<StyleValue> background_color;
     RefPtr<StyleValue> background_image;
-    RefPtr<StyleValue> background_repeat;
     RefPtr<StyleValue> background_position;
-    // FIXME: Implement background-size.
+    RefPtr<StyleValue> background_size;
+    RefPtr<StyleValue> background_repeat;
     RefPtr<StyleValue> background_attachment;
     RefPtr<StyleValue> background_clip;
     RefPtr<StyleValue> background_origin;
@@ -2456,7 +2456,19 @@ RefPtr<StyleValue> Parser::parse_background_value(ParsingContext const& context,
             tokens.reconsume_current_input_token();
             if (auto maybe_background_position = parse_single_background_position_value(context, tokens)) {
                 background_position = maybe_background_position.release_nonnull();
-                // FIXME: background-size optionally goes here, after a '/'
+
+                // Attempt to parse `/ <background-size>`
+                auto before_slash = tokens.position();
+                auto& maybe_slash = tokens.next_token();
+                if (maybe_slash.is(Token::Type::Delim) && maybe_slash.token().delim() == "/"sv) {
+                    if (auto maybe_background_size = parse_single_background_size_value(context, tokens)) {
+                        background_size = maybe_background_size.release_nonnull();
+                        continue;
+                    }
+                    return nullptr;
+                }
+
+                tokens.rewind_to_position(before_slash);
                 continue;
             }
             return nullptr;
@@ -2481,6 +2493,8 @@ RefPtr<StyleValue> Parser::parse_background_value(ParsingContext const& context,
         background_image = property_initial_value(PropertyID::BackgroundImage);
     if (!background_position)
         background_position = property_initial_value(PropertyID::BackgroundPosition);
+    if (!background_size)
+        background_size = property_initial_value(PropertyID::BackgroundSize);
     if (!background_repeat)
         background_repeat = property_initial_value(PropertyID::BackgroundRepeat);
     if (!background_attachment)
@@ -2497,6 +2511,7 @@ RefPtr<StyleValue> Parser::parse_background_value(ParsingContext const& context,
         background_color.release_nonnull(),
         background_image.release_nonnull(),
         background_position.release_nonnull(),
+        background_size.release_nonnull(),
         background_repeat.release_nonnull(),
         background_attachment.release_nonnull(),
         background_origin.release_nonnull(),

+ 2 - 0
Userland/Libraries/LibWeb/CSS/ResolvedCSSStyleDeclaration.cpp

@@ -653,6 +653,7 @@ RefPtr<StyleValue> ResolvedCSSStyleDeclaration::style_value_for_property(Layout:
         auto maybe_background_color = property(CSS::PropertyID::BackgroundColor);
         auto maybe_background_image = property(CSS::PropertyID::BackgroundImage);
         auto maybe_background_position = property(CSS::PropertyID::BackgroundPosition);
+        auto maybe_background_size = property(CSS::PropertyID::BackgroundSize);
         auto maybe_background_repeat = property(CSS::PropertyID::BackgroundRepeat);
         auto maybe_background_attachment = property(CSS::PropertyID::BackgroundAttachment);
         auto maybe_background_origin = property(CSS::PropertyID::BackgroundOrigin);
@@ -662,6 +663,7 @@ RefPtr<StyleValue> ResolvedCSSStyleDeclaration::style_value_for_property(Layout:
             value_or_default(maybe_background_color, InitialStyleValue::the()),
             value_or_default(maybe_background_image, IdentifierStyleValue::create(CSS::ValueID::None)),
             value_or_default(maybe_background_position, PositionStyleValue::create(PositionEdge::Left, Length::make_px(0), PositionEdge::Top, Length::make_px(0))),
+            value_or_default(maybe_background_size, IdentifierStyleValue::create(CSS::ValueID::Auto)),
             value_or_default(maybe_background_repeat, BackgroundRepeatStyleValue::create(CSS::Repeat::Repeat, CSS::Repeat::Repeat)),
             value_or_default(maybe_background_attachment, IdentifierStyleValue::create(CSS::ValueID::Scroll)),
             value_or_default(maybe_background_origin, IdentifierStyleValue::create(CSS::ValueID::PaddingBox)),

+ 2 - 0
Userland/Libraries/LibWeb/CSS/StyleComputer.cpp

@@ -297,6 +297,7 @@ static void set_property_expanding_shorthands(StyleProperties& style, CSS::Prope
             set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, background.color(), document);
             set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, background.image(), document);
             set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundPosition, background.position(), document);
+            set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundSize, background.size(), document);
             set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeat, background.repeat(), document);
             set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundAttachment, background.attachment(), document);
             set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundOrigin, background.origin(), document);
@@ -322,6 +323,7 @@ static void set_property_expanding_shorthands(StyleProperties& style, CSS::Prope
         set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundColor, value, document);
         set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundImage, value, document);
         set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundPosition, value, document);
+        set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundSize, value, document);
         set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundRepeat, value, document);
         set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundAttachment, value, document);
         set_property_expanding_shorthands(style, CSS::PropertyID::BackgroundOrigin, value, document);

+ 7 - 3
Userland/Libraries/LibWeb/CSS/StyleValue.h

@@ -398,12 +398,13 @@ public:
         NonnullRefPtr<StyleValue> color,
         NonnullRefPtr<StyleValue> image,
         NonnullRefPtr<StyleValue> position,
+        NonnullRefPtr<StyleValue> size,
         NonnullRefPtr<StyleValue> repeat,
         NonnullRefPtr<StyleValue> attachment,
         NonnullRefPtr<StyleValue> origin,
         NonnullRefPtr<StyleValue> clip)
     {
-        return adopt_ref(*new BackgroundStyleValue(color, image, position, repeat, attachment, origin, clip));
+        return adopt_ref(*new BackgroundStyleValue(color, image, position, size, repeat, attachment, origin, clip));
     }
     virtual ~BackgroundStyleValue() override { }
 
@@ -414,10 +415,11 @@ public:
     NonnullRefPtr<StyleValue> origin() const { return m_origin; }
     NonnullRefPtr<StyleValue> position() const { return m_position; }
     NonnullRefPtr<StyleValue> repeat() const { return m_repeat; }
+    NonnullRefPtr<StyleValue> size() const { return m_size; }
 
     virtual String to_string() const override
     {
-        return String::formatted("{} {} {} {} {} {} {}", m_color->to_string(), m_image->to_string(), m_position->to_string(), m_repeat->to_string(), m_attachment->to_string(), m_origin->to_string(), m_clip->to_string());
+        return String::formatted("{} {} {} {} {} {} {} {}", m_color->to_string(), m_image->to_string(), m_position->to_string(), m_size->to_string(), m_repeat->to_string(), m_attachment->to_string(), m_origin->to_string(), m_clip->to_string());
     }
 
 private:
@@ -425,6 +427,7 @@ private:
         NonnullRefPtr<StyleValue> color,
         NonnullRefPtr<StyleValue> image,
         NonnullRefPtr<StyleValue> position,
+        NonnullRefPtr<StyleValue> size,
         NonnullRefPtr<StyleValue> repeat,
         NonnullRefPtr<StyleValue> attachment,
         NonnullRefPtr<StyleValue> origin,
@@ -433,6 +436,7 @@ private:
         , m_color(color)
         , m_image(image)
         , m_position(position)
+        , m_size(size)
         , m_repeat(repeat)
         , m_attachment(attachment)
         , m_origin(origin)
@@ -442,7 +446,7 @@ private:
     NonnullRefPtr<StyleValue> m_color;
     NonnullRefPtr<StyleValue> m_image;
     NonnullRefPtr<StyleValue> m_position;
-    // FIXME: background-size
+    NonnullRefPtr<StyleValue> m_size;
     NonnullRefPtr<StyleValue> m_repeat;
     NonnullRefPtr<StyleValue> m_attachment;
     NonnullRefPtr<StyleValue> m_origin;