srctree

Robin Linden parent 2449c470 9918f7f2
layout: Break lines on <br>

inlinesplit
layout/layout.cpp added: 68, removed: 2, total 66
@@ -290,6 +290,16 @@ void Layouter::layout_anonymous_block(LayoutBox &box, geom::Rect const &bounds)
for (std::size_t i = 0; i < box.children.size(); ++i) {
auto *child = &box.children[i];
layout(*child, box.dimensions.content.translated(last_child_end, current_line * font_size.v));
 
// TODO(robinlinden): This needs to get along better with whitespace
// collapsing. A <br> followed by a whitespace will be lead to a leading
// space on the new line.
if (auto const *ele = std::get_if<dom::Element>(&child->node->node); ele != nullptr && ele->name == "br"sv) {
current_line += 1;
last_child_end = 0;
continue;
}
 
// TODO(robinlinden): Handle cases where the text isn't a direct child of the anonymous block.
if (last_child_end + child->dimensions.margin_box().width > bounds.width) {
auto text = child->text();
 
layout/layout_test.cpp added: 68, removed: 2, total 66
@@ -1594,6 +1594,62 @@ int main() {
expect_eq(l, expected);
});
 
etest::test("br", [] {
dom::Node dom = dom::Element{
.name{"html"},
.children{
dom::Text{"hello"},
dom::Element{"br"},
dom::Text{"world"},
},
};
auto const &children = std::get<dom::Element>(dom).children;
 
style::StyledNode style{
.node{dom},
.properties{{css::PropertyId::Display, "block"}, {css::PropertyId::FontSize, "10px"}},
.children{
style::StyledNode{.node{children[0]}},
style::StyledNode{.node{children[1]}},
style::StyledNode{.node{children[2]}},
},
};
set_up_parent_ptrs(style);
 
layout::LayoutBox expected{
.node = &style,
.type = LayoutType::Block,
.dimensions{{0, 0, 25, 20}},
.children{layout::LayoutBox{
.node = nullptr,
.type = LayoutType::AnonymousBlock,
.dimensions{{0, 0, 25, 20}},
.children{
layout::LayoutBox{
.node = &style.children[0],
.type = LayoutType::Inline,
.dimensions{{0, 0, 25, 10}},
.layout_text = "hello"sv,
},
layout::LayoutBox{
.node = &style.children[1],
.type = LayoutType::Inline,
.dimensions{{25, 0, 0, 0}},
},
layout::LayoutBox{
.node = &style.children[2],
.type = LayoutType::Inline,
.dimensions{{0, 10, 25, 10}},
.layout_text = "world"sv,
},
},
}},
};
 
auto l = layout::create_layout(style, 25).value();
expect_eq(l, expected);
});
 
etest::test("display:none on root node", [] {
dom::Node dom = dom::Element{.name{"html"}};
style::StyledNode style{