srctree

Robin Linden parent 31ad50c6 d3b35650
layout: Add a method for getting properties directly from a layout node

inlinesplit
layout/layout.cpp added: 33, removed: 4, total 29
@@ -307,6 +307,14 @@ void print_box(LayoutBox const &box, std::ostream &os, uint8_t depth = 0) {
 
} // namespace
 
std::optional<std::string_view> LayoutBox::get_property(std::string_view property) const {
if (!node) {
return std::nullopt;
}
 
return style::get_property(*node, property);
}
 
LayoutBox create_layout(style::StyledNode const &node, int width) {
auto tree = create_tree(node);
layout(*tree, {0, 0, width, 0});
 
layout/layout.h added: 33, removed: 4, total 29
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021 Robin Lindén <dev@robinlinden.eu>
// SPDX-FileCopyrightText: 2021-2022 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
@@ -10,7 +10,9 @@
#include "geom/geom.h"
#include "style/styled_node.h"
 
#include <optional>
#include <string>
#include <string_view>
#include <vector>
 
namespace layout {
@@ -27,6 +29,8 @@ struct LayoutBox {
BoxModel dimensions;
std::vector<LayoutBox> children;
[[nodiscard]] bool operator==(LayoutBox const &) const = default;
 
std::optional<std::string_view> get_property(std::string_view) const;
};
 
LayoutBox create_layout(style::StyledNode const &node, int width);
 
layout/layout_test.cpp added: 33, removed: 4, total 29
@@ -921,5 +921,22 @@ int main() {
expect_eq(to_string(layout::create_layout(style_root, 0)), expected);
});
 
// clang-format on
etest::test("get_property", [] {
dom::Node dom_root = dom::Element{.name{"html"}, .attributes{}, .children{}};
auto style_root = style::StyledNode{.node = dom_root, .properties = {{"color", "green"}}, .children{}};
 
auto layout = layout::create_layout(style_root, 0);
expect_eq(layout.get_property("color"), "green");
expect_eq(layout.get_property("background-color"), std::nullopt);
});
 
etest::test("get_property, no backing style node", [] {
auto layout = layout::LayoutBox{
.node = nullptr,
};
expect_eq(layout.get_property("color"), std::nullopt);
});
 
return etest::run_all_tests();
}