Skip to content

Commit

Permalink
layout: Support %-values when resolving the width property
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Nov 14, 2023
1 parent 502bc5f commit a437feb
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 1 deletion.
2 changes: 1 addition & 1 deletion layout/layout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ void calculate_width_and_margin(
auto margin_left = box.get_property<css::PropertyId::MarginLeft>();
auto margin_right = box.get_property<css::PropertyId::MarginRight>();
if (auto width = box.get_property<css::PropertyId::Width>(); !width.is_auto()) {
box.dimensions.content.width = width.resolve(font_size, root_font_size);
box.dimensions.content.width = width.resolve(font_size, root_font_size, parent.width);
calculate_left_and_right_margin(box, parent, margin_left, margin_right, font_size, root_font_size);
} else {
if (margin_left != "auto") {
Expand Down
23 changes: 23 additions & 0 deletions layout/layout_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1485,6 +1485,29 @@ int main() {
expect_eq(layout.children.at(0).dimensions.border_box().width, 32);
});

etest::test("% units", [] {
dom::Node dom = dom::Element{"html", {}, {dom::Element{"div"}}};
auto const &div = std::get<dom::Element>(dom).children[0];
style::StyledNode style{
.node{dom},
.properties{{css::PropertyId::Width, "500px"}, {css::PropertyId::Display, "block"}},
.children{
style::StyledNode{
.node{div},
.properties{{css::PropertyId::Width, "50%"}, {css::PropertyId::Display, "block"}},
},
},
};
set_up_parent_ptrs(style);

auto layout = layout::create_layout(style, 1000).value();
expect_eq(layout.children.at(0).dimensions.border_box().width, 250);

style.properties.at(0).second = "10%";
layout = layout::create_layout(style, 1000).value();
expect_eq(layout.children.at(0).dimensions.border_box().width, 50);
});

whitespace_collapsing_tests();

return etest::run_all_tests();
Expand Down

0 comments on commit a437feb

Please sign in to comment.