summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb
diff options
context:
space:
mode:
authorSam Atkins <atkinssj@serenityos.org>2022-01-19 11:20:40 +0000
committerAndreas Kling <kling@serenityos.org>2022-01-20 00:04:10 +0100
commit784ba2ec427cf1ad4101b3dfc170f50c49887a25 (patch)
tree799042e7c3bafd49b16a3f0a1fb5b777eeb39532 /Userland/Libraries/LibWeb
parent0162ca912b58d5c7fbe8f8c75768d4819689a0a8 (diff)
downloadserenity-784ba2ec427cf1ad4101b3dfc170f50c49887a25.zip
LibWeb: Convert background-position to LengthPercentage
Not much needed changing this time, hurrah! :^)
Diffstat (limited to 'Userland/Libraries/LibWeb')
-rw-r--r--Userland/Libraries/LibWeb/CSS/ComputedValues.h4
-rw-r--r--Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp33
-rw-r--r--Userland/Libraries/LibWeb/CSS/StyleValue.h12
-rw-r--r--Userland/Libraries/LibWeb/Painting/BackgroundPainting.cpp8
4 files changed, 36 insertions, 21 deletions
diff --git a/Userland/Libraries/LibWeb/CSS/ComputedValues.h b/Userland/Libraries/LibWeb/CSS/ComputedValues.h
index 5d9df0c6a9..a39b1f0703 100644
--- a/Userland/Libraries/LibWeb/CSS/ComputedValues.h
+++ b/Userland/Libraries/LibWeb/CSS/ComputedValues.h
@@ -45,9 +45,9 @@ struct BackgroundLayerData {
CSS::BackgroundBox origin { CSS::BackgroundBox::PaddingBox };
CSS::BackgroundBox clip { CSS::BackgroundBox::BorderBox };
CSS::PositionEdge position_edge_x { CSS::PositionEdge::Left };
- CSS::Length position_offset_x { CSS::Length::make_px(0) };
+ CSS::LengthPercentage position_offset_x { CSS::Length::make_px(0) };
CSS::PositionEdge position_edge_y { CSS::PositionEdge::Top };
- CSS::Length position_offset_y { CSS::Length::make_px(0) };
+ CSS::LengthPercentage position_offset_y { CSS::Length::make_px(0) };
CSS::BackgroundSize size_type { CSS::BackgroundSize::LengthPercentage };
CSS::LengthPercentage size_x { CSS::Length::make_auto() };
CSS::LengthPercentage size_y { CSS::Length::make_auto() };
diff --git a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
index 3b7e93a13e..b7dfe9c57c 100644
--- a/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
+++ b/Userland/Libraries/LibWeb/CSS/Parser/Parser.cpp
@@ -2763,12 +2763,12 @@ RefPtr<StyleValue> Parser::parse_single_background_position_value(TokenStream<St
}
};
- auto zero_offset = Length::make_px(0);
- auto center_offset = Length { 50, Length::Type::Percentage };
+ LengthPercentage zero_offset = Length::make_px(0);
+ LengthPercentage center_offset = Percentage { 50 };
struct EdgeOffset {
PositionEdge edge;
- Length offset;
+ LengthPercentage offset;
bool edge_provided;
bool offset_provided;
};
@@ -2790,6 +2790,17 @@ RefPtr<StyleValue> Parser::parse_single_background_position_value(TokenStream<St
tokens.next_token();
auto value = maybe_value.release_nonnull();
+ if (value->is_percentage()) {
+ if (!horizontal.has_value()) {
+ horizontal = EdgeOffset { PositionEdge::Left, value->as_percentage().percentage(), false, true };
+ } else if (!vertical.has_value()) {
+ vertical = EdgeOffset { PositionEdge::Top, value->as_percentage().percentage(), false, true };
+ } else {
+ return error();
+ }
+ continue;
+ }
+
if (value->has_length()) {
if (!horizontal.has_value()) {
horizontal = EdgeOffset { PositionEdge::Left, value->to_length(), false, true };
@@ -2804,24 +2815,24 @@ RefPtr<StyleValue> Parser::parse_single_background_position_value(TokenStream<St
if (value->has_identifier()) {
auto identifier = value->to_identifier();
if (is_horizontal(identifier)) {
- Length offset = zero_offset;
+ LengthPercentage offset = zero_offset;
bool offset_provided = false;
if (tokens.has_next_token()) {
- auto maybe_offset = parse_length(tokens.peek_token());
- if (maybe_offset.has_value()) {
- offset = maybe_offset.value();
+ auto maybe_offset = parse_dimension(tokens.peek_token());
+ if (maybe_offset.has_value() && maybe_offset.value().is_length_percentage()) {
+ offset = maybe_offset.value().length_percentage();
offset_provided = true;
tokens.next_token();
}
}
horizontal = EdgeOffset { *to_edge(identifier), offset, true, offset_provided };
} else if (is_vertical(identifier)) {
- Length offset = zero_offset;
+ LengthPercentage offset = zero_offset;
bool offset_provided = false;
if (tokens.has_next_token()) {
- auto maybe_offset = parse_length(tokens.peek_token());
- if (maybe_offset.has_value()) {
- offset = maybe_offset.value();
+ auto maybe_offset = parse_dimension(tokens.peek_token());
+ if (maybe_offset.has_value() && maybe_offset.value().is_length_percentage()) {
+ offset = maybe_offset.value().length_percentage();
offset_provided = true;
tokens.next_token();
}
diff --git a/Userland/Libraries/LibWeb/CSS/StyleValue.h b/Userland/Libraries/LibWeb/CSS/StyleValue.h
index 32b7344494..93ef5f60c8 100644
--- a/Userland/Libraries/LibWeb/CSS/StyleValue.h
+++ b/Userland/Libraries/LibWeb/CSS/StyleValue.h
@@ -1215,21 +1215,21 @@ private:
class PositionStyleValue final : public StyleValue {
public:
- static NonnullRefPtr<PositionStyleValue> create(PositionEdge edge_x, Length const& offset_x, PositionEdge edge_y, Length const& offset_y)
+ static NonnullRefPtr<PositionStyleValue> create(PositionEdge edge_x, LengthPercentage const& offset_x, PositionEdge edge_y, LengthPercentage const& offset_y)
{
return adopt_ref(*new PositionStyleValue(edge_x, offset_x, edge_y, offset_y));
}
virtual ~PositionStyleValue() override { }
PositionEdge edge_x() const { return m_edge_x; }
- Length const& offset_x() const { return m_offset_x; }
+ LengthPercentage const& offset_x() const { return m_offset_x; }
PositionEdge edge_y() const { return m_edge_y; }
- Length const& offset_y() const { return m_offset_y; }
+ LengthPercentage const& offset_y() const { return m_offset_y; }
virtual String to_string() const override;
private:
- PositionStyleValue(PositionEdge edge_x, Length const& offset_x, PositionEdge edge_y, Length const& offset_y)
+ PositionStyleValue(PositionEdge edge_x, LengthPercentage const& offset_x, PositionEdge edge_y, LengthPercentage const& offset_y)
: StyleValue(Type::Position)
, m_edge_x(edge_x)
, m_offset_x(offset_x)
@@ -1239,9 +1239,9 @@ private:
}
PositionEdge m_edge_x;
- Length m_offset_x;
+ LengthPercentage m_offset_x;
PositionEdge m_edge_y;
- Length m_offset_y;
+ LengthPercentage m_offset_y;
};
class StringStyleValue : public StyleValue {
diff --git a/Userland/Libraries/LibWeb/Painting/BackgroundPainting.cpp b/Userland/Libraries/LibWeb/Painting/BackgroundPainting.cpp
index 9e4afb5db1..cd325aaef6 100644
--- a/Userland/Libraries/LibWeb/Painting/BackgroundPainting.cpp
+++ b/Userland/Libraries/LibWeb/Painting/BackgroundPainting.cpp
@@ -143,14 +143,18 @@ void paint_background(PaintContext& context, Layout::NodeWithStyleAndBoxModelMet
int space_y = background_positioning_area.height() - image_rect.height();
// Position
- int offset_x = layer.position_offset_x.resolved_or_zero(layout_node, space_x).to_px(layout_node);
+ int offset_x = layer.position_offset_x.resolved(CSS::Length::make_px(space_x))
+ .resolved_or_zero(layout_node, space_x)
+ .to_px(layout_node);
if (layer.position_edge_x == CSS::PositionEdge::Right) {
image_rect.set_right_without_resize(background_positioning_area.right() - offset_x);
} else {
image_rect.set_left(background_positioning_area.left() + offset_x);
}
- int offset_y = layer.position_offset_y.resolved_or_zero(layout_node, space_y).to_px(layout_node);
+ int offset_y = layer.position_offset_y.resolved(CSS::Length::make_px(space_y))
+ .resolved_or_zero(layout_node, space_y)
+ .to_px(layout_node);
if (layer.position_edge_y == CSS::PositionEdge::Bottom) {
image_rect.set_bottom_without_resize(background_positioning_area.bottom() - offset_y);
} else {