1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 16:07:47 +00:00

LibWeb: Convert background-position to LengthPercentage

Not much needed changing this time, hurrah! :^)
This commit is contained in:
Sam Atkins 2022-01-19 11:20:40 +00:00 committed by Andreas Kling
parent 0162ca912b
commit 784ba2ec42
4 changed files with 36 additions and 21 deletions

View file

@ -45,9 +45,9 @@ struct BackgroundLayerData {
CSS::BackgroundBox origin { CSS::BackgroundBox::PaddingBox }; CSS::BackgroundBox origin { CSS::BackgroundBox::PaddingBox };
CSS::BackgroundBox clip { CSS::BackgroundBox::BorderBox }; CSS::BackgroundBox clip { CSS::BackgroundBox::BorderBox };
CSS::PositionEdge position_edge_x { CSS::PositionEdge::Left }; 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::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::BackgroundSize size_type { CSS::BackgroundSize::LengthPercentage };
CSS::LengthPercentage size_x { CSS::Length::make_auto() }; CSS::LengthPercentage size_x { CSS::Length::make_auto() };
CSS::LengthPercentage size_y { CSS::Length::make_auto() }; CSS::LengthPercentage size_y { CSS::Length::make_auto() };

View file

@ -2763,12 +2763,12 @@ RefPtr<StyleValue> Parser::parse_single_background_position_value(TokenStream<St
} }
}; };
auto zero_offset = Length::make_px(0); LengthPercentage zero_offset = Length::make_px(0);
auto center_offset = Length { 50, Length::Type::Percentage }; LengthPercentage center_offset = Percentage { 50 };
struct EdgeOffset { struct EdgeOffset {
PositionEdge edge; PositionEdge edge;
Length offset; LengthPercentage offset;
bool edge_provided; bool edge_provided;
bool offset_provided; bool offset_provided;
}; };
@ -2790,6 +2790,17 @@ RefPtr<StyleValue> Parser::parse_single_background_position_value(TokenStream<St
tokens.next_token(); tokens.next_token();
auto value = maybe_value.release_nonnull(); 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 (value->has_length()) {
if (!horizontal.has_value()) { if (!horizontal.has_value()) {
horizontal = EdgeOffset { PositionEdge::Left, value->to_length(), false, true }; 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()) { if (value->has_identifier()) {
auto identifier = value->to_identifier(); auto identifier = value->to_identifier();
if (is_horizontal(identifier)) { if (is_horizontal(identifier)) {
Length offset = zero_offset; LengthPercentage offset = zero_offset;
bool offset_provided = false; bool offset_provided = false;
if (tokens.has_next_token()) { if (tokens.has_next_token()) {
auto maybe_offset = parse_length(tokens.peek_token()); auto maybe_offset = parse_dimension(tokens.peek_token());
if (maybe_offset.has_value()) { if (maybe_offset.has_value() && maybe_offset.value().is_length_percentage()) {
offset = maybe_offset.value(); offset = maybe_offset.value().length_percentage();
offset_provided = true; offset_provided = true;
tokens.next_token(); tokens.next_token();
} }
} }
horizontal = EdgeOffset { *to_edge(identifier), offset, true, offset_provided }; horizontal = EdgeOffset { *to_edge(identifier), offset, true, offset_provided };
} else if (is_vertical(identifier)) { } else if (is_vertical(identifier)) {
Length offset = zero_offset; LengthPercentage offset = zero_offset;
bool offset_provided = false; bool offset_provided = false;
if (tokens.has_next_token()) { if (tokens.has_next_token()) {
auto maybe_offset = parse_length(tokens.peek_token()); auto maybe_offset = parse_dimension(tokens.peek_token());
if (maybe_offset.has_value()) { if (maybe_offset.has_value() && maybe_offset.value().is_length_percentage()) {
offset = maybe_offset.value(); offset = maybe_offset.value().length_percentage();
offset_provided = true; offset_provided = true;
tokens.next_token(); tokens.next_token();
} }

View file

@ -1215,21 +1215,21 @@ private:
class PositionStyleValue final : public StyleValue { class PositionStyleValue final : public StyleValue {
public: 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)); return adopt_ref(*new PositionStyleValue(edge_x, offset_x, edge_y, offset_y));
} }
virtual ~PositionStyleValue() override { } virtual ~PositionStyleValue() override { }
PositionEdge edge_x() const { return m_edge_x; } 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; } 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; virtual String to_string() const override;
private: 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) : StyleValue(Type::Position)
, m_edge_x(edge_x) , m_edge_x(edge_x)
, m_offset_x(offset_x) , m_offset_x(offset_x)
@ -1239,9 +1239,9 @@ private:
} }
PositionEdge m_edge_x; PositionEdge m_edge_x;
Length m_offset_x; LengthPercentage m_offset_x;
PositionEdge m_edge_y; PositionEdge m_edge_y;
Length m_offset_y; LengthPercentage m_offset_y;
}; };
class StringStyleValue : public StyleValue { class StringStyleValue : public StyleValue {

View file

@ -143,14 +143,18 @@ void paint_background(PaintContext& context, Layout::NodeWithStyleAndBoxModelMet
int space_y = background_positioning_area.height() - image_rect.height(); int space_y = background_positioning_area.height() - image_rect.height();
// Position // 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) { if (layer.position_edge_x == CSS::PositionEdge::Right) {
image_rect.set_right_without_resize(background_positioning_area.right() - offset_x); image_rect.set_right_without_resize(background_positioning_area.right() - offset_x);
} else { } else {
image_rect.set_left(background_positioning_area.left() + offset_x); 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) { if (layer.position_edge_y == CSS::PositionEdge::Bottom) {
image_rect.set_bottom_without_resize(background_positioning_area.bottom() - offset_y); image_rect.set_bottom_without_resize(background_positioning_area.bottom() - offset_y);
} else { } else {