From ce7c70c39aeb890fde09edd170c569318a8d9a67 Mon Sep 17 00:00:00 2001 From: Robin Linden Date: Fri, 10 Jan 2025 00:50:52 +0100 Subject: [PATCH 1/2] engine: Add more progress logging This is helpful for easily seeing what phases are taking more time than they should. This happens to be most of them right now, but we'll get there. --- engine/engine.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/engine/engine.cpp b/engine/engine.cpp index 327d4c79..f0675209 100644 --- a/engine/engine.cpp +++ b/engine/engine.cpp @@ -1,4 +1,4 @@ -// SPDX-FileCopyrightText: 2021-2024 Robin Lindén +// SPDX-FileCopyrightText: 2021-2025 Robin Lindén // // SPDX-License-Identifier: BSD-2-Clause @@ -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, 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()) { @@ -108,9 +109,11 @@ tl::expected, NavigationError> Engine::navigate(uri:: auto state = std::make_unique(); 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; @@ -177,11 +180,13 @@ tl::expected, 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; } From c83bb777936f5f824f6b3e3cbda0ac4f85d8a6a7 Mon Sep 17 00:00:00 2001 From: Robin Linden Date: Fri, 10 Jan 2025 01:11:14 +0100 Subject: [PATCH 2/2] engine: Load stylesheets wherever they appear --- engine/engine.cpp | 4 +++- engine/engine_test.cpp | 9 ++++++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/engine/engine.cpp b/engine/engine.cpp index f0675209..f4fb5c94 100644 --- a/engine/engine.cpp +++ b/engine/engine.cpp @@ -124,7 +124,9 @@ tl::expected, 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") diff --git a/engine/engine_test.cpp b/engine/engine_test.cpp index bdfaf584..b84307c7 100644 --- a/engine/engine_test.cpp +++ b/engine/engine_test.cpp @@ -163,7 +163,9 @@ int main() { .body{"" "" "" - ""}, + "" + "" + ""}, }; responses["hax://example.com/one.css"s] = Response{ .status_line = {.status_code = 200}, @@ -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(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) {