srctree

Robin Linden parent f475557f 37f7267f
style: Give the style tree root a stable address

This will be needed when allowing traversal from child to parent in thestyle tree.

inlinesplit
browser/engine.h added: 22, removed: 12, total 10
@@ -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
 
@@ -12,6 +12,7 @@
#include "uri/uri.h"
 
#include <functional>
#include <memory>
#include <optional>
#include <utility>
 
@@ -45,7 +46,7 @@ private:
uri::Uri uri_{};
protocol::Response response_{};
dom::Document dom_{};
std::optional<style::StyledNode> styled_{};
std::unique_ptr<style::StyledNode> styled_{};
std::optional<layout::LayoutBox> layout_{};
 
void on_navigation_success();
 
style/style.cpp added: 22, removed: 12, total 10
@@ -44,12 +44,13 @@ std::vector<std::pair<std::string, std::string>> matching_rules(
return matched_rules;
}
 
StyledNode style_tree(std::reference_wrapper<dom::Node const> root, std::vector<css::Rule> const &stylesheet) {
namespace {
StyledNode style_tree_impl(std::reference_wrapper<dom::Node const> root, std::vector<css::Rule> const &stylesheet) {
std::vector<StyledNode> children{};
 
if (auto const *element = std::get_if<dom::Element>(&root.get())) {
for (auto const &child : element->children) {
children.push_back(style_tree(child, stylesheet));
children.push_back(style_tree_impl(child, stylesheet));
}
}
 
@@ -63,5 +64,11 @@ StyledNode style_tree(std::reference_wrapper<dom::Node const> root, std::vector<
.children = std::move(children),
};
}
} // namespace
 
std::unique_ptr<StyledNode> style_tree(
std::reference_wrapper<dom::Node const> root, std::vector<css::Rule> const &stylesheet) {
return std::make_unique<StyledNode>(style_tree_impl(root, stylesheet));
}
 
} // namespace style
 
style/style.h added: 22, removed: 12, total 10
@@ -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,6 +10,7 @@
#include "style/styled_node.h"
 
#include <functional>
#include <memory>
#include <string>
#include <string_view>
#include <utility>
@@ -22,7 +23,8 @@ bool is_match(dom::Element const &element, std::string_view selector);
std::vector<std::pair<std::string, std::string>> matching_rules(
dom::Element const &element, std::vector<css::Rule> const &stylesheet);
 
StyledNode style_tree(std::reference_wrapper<dom::Node const> root, std::vector<css::Rule> const &stylesheet);
std::unique_ptr<StyledNode> style_tree(
std::reference_wrapper<dom::Node const> root, std::vector<css::Rule> const &stylesheet);
 
} // namespace style
 
 
style/style_test.cpp added: 22, removed: 12, total 10
@@ -113,7 +113,7 @@ int main() {
},
};
 
expect(style::style_tree(root, {}) == expected);
expect(*style::style_tree(root, {}) == expected);
});
 
etest::test("style_tree: style is applied", [] {
@@ -155,7 +155,7 @@ int main() {
},
};
 
expect(style::style_tree(root, stylesheet) == expected);
expect(*style::style_tree(root, stylesheet) == expected);
});
 
return etest::run_all_tests();