diff --git a/layout/layout.cpp b/layout/layout.cpp index 43fb1e22..56ea8464 100644 --- a/layout/layout.cpp +++ b/layout/layout.cpp @@ -176,7 +176,7 @@ void calculate_width_and_margin( auto margin_left = box.get_property(); auto margin_right = box.get_property(); if (auto width = box.get_property(); !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") { diff --git a/layout/layout_test.cpp b/layout/layout_test.cpp index 34041639..aa6a1b14 100644 --- a/layout/layout_test.cpp +++ b/layout/layout_test.cpp @@ -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).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();