srctree

Robin Linden parent 2685da7a c2756e5b
layout: Keep a view of the text layouted in the LayoutBox

This allows us to only keep a substr of the real text if we'recollapsing whitespace from the beginning or end of dom node.

inlinesplit
layout/layout.cpp added: 16, removed: 34, total 0
@@ -64,8 +64,8 @@ std::optional<LayoutBox> create_tree(style::StyledNode const &node) {
 
return box;
},
[&node](dom::Text const &) -> std::optional<LayoutBox> {
return LayoutBox{&node, LayoutType::Inline};
[&node](dom::Text const &text) -> std::optional<LayoutBox> {
return LayoutBox{.node = &node, .type = LayoutType::Inline, .layout_text = text.text};
},
};
 
@@ -376,15 +376,7 @@ int get_root_font_size(style::StyledNode const &node) {
} // namespace
 
std::optional<std::string_view> LayoutBox::text() const {
if (node == nullptr) {
return std::nullopt;
}
 
if (auto const *text = std::get_if<dom::Text>(&node->node)) {
return text->text;
}
 
return std::nullopt;
return layout_text;
}
 
std::pair<int, int> LayoutBox::get_border_radius_property(css::PropertyId id) const {
 
layout/layout.h added: 16, removed: 34, total 0
@@ -32,6 +32,7 @@ struct LayoutBox {
LayoutType type;
BoxModel dimensions;
std::vector<LayoutBox> children;
std::optional<std::string_view> layout_text;
[[nodiscard]] bool operator==(LayoutBox const &) const = default;
 
std::optional<std::string_view> text() const;
 
layout/layout_test.cpp added: 16, removed: 34, total 0
@@ -202,8 +202,8 @@ int main() {
.children = {
{&style_root.children[0], LayoutType::Block, {{0, 0, 0, 10}}, {
{nullptr, LayoutType::AnonymousBlock, {{0, 0, 60, 10}}, {
{&style_root.children[0].children[0], LayoutType::Inline, {{0, 0, 25, 10}}, {}},
{&style_root.children[0].children[1], LayoutType::Inline, {{25, 0, 35, 10}}, {}},
{&style_root.children[0].children[0], LayoutType::Inline, {{0, 0, 25, 10}}, {}, "hello"},
{&style_root.children[0].children[1], LayoutType::Inline, {{25, 0, 35, 10}}, {}, "goodbye"},
}},
}},
}
@@ -211,6 +211,9 @@ int main() {
 
auto layout_root = layout::create_layout(style_root, 0);
expect(expected_layout == layout_root);
 
expect_eq(expected_layout.children.at(0).children.at(0).children.at(0).text(), "hello");
expect_eq(expected_layout.children.at(0).children.at(0).children.at(1).text(), "goodbye");
});
 
etest::test("simple width", [] {
@@ -1192,19 +1195,5 @@ int main() {
expect_eq(layout.children.at(0).dimensions.border_box().width, 32);
});
 
etest::test("layout box text accessor", [] {
dom::Node dom = dom::Text{"hello"s};
style::StyledNode style{.node = dom};
layout::LayoutBox layout{.node = &style};
 
expect_eq(layout.text(), "hello");
 
dom = dom::Element{.name = "asdf"};
expect_eq(layout.text(), std::nullopt);
 
layout.node = nullptr;
expect_eq(layout.text(), std::nullopt);
});
 
return etest::run_all_tests();
}
 
render/render_test.cpp added: 16, removed: 34, total 0
@@ -33,7 +33,7 @@ int main() {
.node = &styled,
.type = layout::LayoutType::Inline,
.dimensions = {},
.children = {{&styled.children[0], layout::LayoutType::Inline, {}, {}}},
.children = {{&styled.children[0], layout::LayoutType::Inline, {}, {}, "hello"}},
};
 
gfx::CanvasCommandSaver saver;
@@ -59,7 +59,7 @@ int main() {
auto layout = layout::LayoutBox{
.node = &styled,
.type = layout::LayoutType::Inline,
.children = {{&styled.children[0], layout::LayoutType::Inline}},
.children = {{&styled.children[0], layout::LayoutType::Inline, {}, {}, "hello"}},
};
 
gfx::CanvasCommandSaver saver;
@@ -329,7 +329,7 @@ int main() {
{css::PropertyId::FontFamily, "arial"},
{css::PropertyId::FontSize, "16px"},
}};
auto layout = layout::LayoutBox{.node = &styled};
auto layout = layout::LayoutBox{.node = &styled, .layout_text = "hello"};
 
gfx::CanvasCommandSaver saver;
render::render_layout(saver, layout);