srctree

Robin Linden parent 176a5847 53d9de66
layout: Include <img>-alt-texts in the layout

These will be conditionally included once we start loading images.

inlinesplit
layout/layout.cpp added: 77, removed: 3, total 74
@@ -55,7 +55,15 @@ std::optional<LayoutBox> create_tree(style::StyledNode const &node) {
return std::nullopt;
}
 
LayoutBox box{&node, display == style::DisplayValue::Inline ? LayoutType::Inline : LayoutType::Block};
auto type = display == style::DisplayValue::Inline ? LayoutType::Inline : LayoutType::Block;
 
if (auto const &element = std::get<dom::Element>(node.node); element.name == "img"sv) {
if (auto alt = element.attributes.find("alt"sv); alt != element.attributes.end()) {
return LayoutBox{.node = &node, .type = type, .layout_text = std::string_view{alt->second}};
}
}
 
LayoutBox box{&node, type};
 
for (auto const &child : node.children) {
auto child_box = create_tree(child);
 
layout/layout_test.cpp added: 77, removed: 3, total 74
@@ -333,6 +333,71 @@ void whitespace_collapsing_tests() {
});
}
 
void img_tests() {
etest::test("img, no alt or src", [] {
dom::Node dom = dom::Element{"body", {}, {dom::Element{"img"}}};
auto const &body = std::get<dom::Element>(dom);
auto style = style::StyledNode{
.node = dom,
.properties{
{css::PropertyId::Display, "block"},
{css::PropertyId::FontSize, "10px"},
},
.children{
{body.children.at(0), {{css::PropertyId::Display, "block"}}},
},
};
set_up_parent_ptrs(style);
 
auto expected_layout = layout::LayoutBox{
.node = &style,
.type = LayoutType::Block,
.dimensions{{0, 0, 100, 0}},
.children{{
&style.children[0],
LayoutType::Block,
{{0, 0, 100, 0}},
}},
};
 
auto layout_root = layout::create_layout(style, 100);
expect_eq(expected_layout, layout_root);
});
 
etest::test("img, alt, no src", [] {
dom::Node dom = dom::Element{"body", {}, {dom::Element{"img", {{"alt", "hello"}}}}};
auto const &body = std::get<dom::Element>(dom);
auto style = style::StyledNode{
.node = dom,
.properties{
{css::PropertyId::Display, "block"},
{css::PropertyId::FontSize, "10px"},
},
.children{
{body.children.at(0), {{css::PropertyId::Display, "block"}}},
},
};
set_up_parent_ptrs(style);
 
auto expected_layout = layout::LayoutBox{
.node = &style,
.type = LayoutType::Block,
.dimensions{{0, 0, 100, 10}},
.children{{
&style.children[0],
LayoutType::Block,
{{0, 0, 100, 10}},
{},
"hello"sv,
}},
};
 
auto layout_root = layout::create_layout(style, 100);
expect_eq(expected_layout, layout_root);
expect_eq(expected_layout.children.at(0).text(), "hello");
});
}
 
} // namespace
 
// TODO(robinlinden): clang-format doesn't get along well with how I structured
@@ -1557,6 +1622,7 @@ int main() {
});
 
whitespace_collapsing_tests();
img_tests();
 
return etest::run_all_tests();
}