Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

engine: Load stylesheets wherever they appear #1150

Merged
merged 2 commits into from
Jan 11, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions engine/engine.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// SPDX-FileCopyrightText: 2021-2024 Robin Lindén <[email protected]>
// SPDX-FileCopyrightText: 2021-2025 Robin Lindén <[email protected]>
//
// SPDX-License-Identifier: BSD-2-Clause

Expand Down Expand Up @@ -86,6 +86,7 @@ css::MediaQuery::Context to_media_context(Options opts) {

// NOLINTNEXTLINE(performance-unnecessary-value-param): Clang is wrong, the uri is moved below.
tl::expected<std::unique_ptr<PageState>, NavigationError> Engine::navigate(uri::Uri uri, Options opts) {
spdlog::info("Navigating to {}", uri.uri);
auto result = load(std::move(uri));

if (!result.response.has_value()) {
Expand All @@ -108,9 +109,11 @@ tl::expected<std::unique_ptr<PageState>, NavigationError> Engine::navigate(uri::
auto state = std::make_unique<PageState>();
state->uri = std::move(result.uri_after_redirects);
state->response = std::move(result.response.value());
spdlog::info("Parsing HTML");
state->dom = html::parse(state->response.body);
state->stylesheet = css::default_style();

spdlog::info("Parsing inline styles");
state->stylesheet = css::default_style();
for (auto const &style : dom::nodes_by_xpath(state->dom.html(), "/html/head/style"sv)) {
if (style->children.empty()) {
continue;
Expand All @@ -121,7 +124,9 @@ tl::expected<std::unique_ptr<PageState>, NavigationError> Engine::navigate(uri::
state->stylesheet.splice(css::parse(style_content.text));
}

auto head_links = dom::nodes_by_xpath(state->dom.html(), "/html/head/link");
// Stylesheets can appear a bit everywhere:
// https://html.spec.whatwg.org/multipage/semantics.html#allowed-in-the-body
auto head_links = dom::nodes_by_xpath(state->dom.html(), "//link");
std::erase_if(head_links, [](auto const *link) {
return !link->attributes.contains("rel")
|| (link->attributes.contains("rel") && link->attributes.at("rel") != "stylesheet")
Expand Down Expand Up @@ -177,11 +182,13 @@ tl::expected<std::unique_ptr<PageState>, NavigationError> Engine::navigate(uri::
state->layout_width = opts.layout_width;
state->viewport_height = opts.viewport_height;
state->styled = style::style_tree(state->dom.html_node, state->stylesheet, to_media_context(opts));
spdlog::info("Building layout");
state->layout = layout::create_layout(*state->styled,
{state->layout_width, state->viewport_height},
*type_,
get_intrensic_size_for_resource_at_url_);

spdlog::info("Done navigating to {}", state->uri.uri);
return state;
}

Expand Down
9 changes: 8 additions & 1 deletion engine/engine_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ int main() {
.body{"<html><head>"
"<link rel=stylesheet href=one.css />"
"<link rel=stylesheet href=two.css />"
"</head></html>"},
"</head><body>"
"<link rel=stylesheet href=sad.css />"
"</body></html>"},
};
responses["hax://example.com/one.css"s] = Response{
.status_line = {.status_code = 200},
Expand All @@ -173,11 +175,16 @@ int main() {
.status_line = {.status_code = 200},
.body{"p { color: green; }"},
};
responses["hax://example.com/sad.css"s] = Response{
.status_line = {.status_code = 200},
.body{"a { color: red; }"},
};
engine::Engine e{std::make_unique<FakeProtocolHandler>(std::move(responses))};
auto page = e.navigate(uri::Uri::parse("hax://example.com").value()).value();
a.expect(contains(
page->stylesheet.rules, {.selectors{"p"}, .declarations{{css::PropertyId::FontSize, "123em"}}}));
a.expect(contains(page->stylesheet.rules, {.selectors{"p"}, .declarations{{css::PropertyId::Color, "green"}}}));
a.expect(contains(page->stylesheet.rules, {.selectors{"a"}, .declarations{{css::PropertyId::Color, "red"}}}));
});

s.add_test("stylesheet link, unsupported Content-Encoding", [](etest::IActions &a) {
Expand Down