1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 11:58:12 +00:00

Browser: Use LibWebView to decide what parts of a URL to highlight

This commit is contained in:
Timothy Flynn 2023-10-17 13:17:23 -04:00 committed by Andreas Kling
parent 55092dd164
commit 192aa0838a

View file

@ -8,6 +8,7 @@
#include <Applications/Browser/URLBox.h> #include <Applications/Browser/URLBox.h>
#include <LibGfx/Palette.h> #include <LibGfx/Palette.h>
#include <LibGfx/TextAttributes.h> #include <LibGfx/TextAttributes.h>
#include <LibWebView/URL.h>
namespace Browser { namespace Browser {
@ -53,45 +54,33 @@ void URLBox::mousedown_event(GUI::MouseEvent& event)
void URLBox::highlight_url() void URLBox::highlight_url()
{ {
auto url = URL::create_with_url_or_path(text());
Vector<GUI::TextDocumentSpan> spans; Vector<GUI::TextDocumentSpan> spans;
if (url.is_valid() && !is_focused()) { if (auto url_parts = WebView::break_url_into_parts(text()); url_parts.has_value()) {
if (url.scheme() == "http" || url.scheme() == "https" || url.scheme() == "gemini") { Gfx::TextAttributes dark_attributes;
auto serialized_host = url.serialized_host().release_value_but_fixme_should_propagate_errors().to_deprecated_string(); dark_attributes.color = palette().color(Gfx::ColorRole::PlaceholderText);
auto host_start = url.scheme().bytes_as_string_view().length() + 3;
auto host_length = serialized_host.length();
// FIXME: Maybe add a generator to use https://publicsuffix.org/list/public_suffix_list.dat Gfx::TextAttributes highlight_attributes;
// for now just highlight the whole host highlight_attributes.color = palette().color(Gfx::ColorRole::BaseText);
Gfx::TextAttributes default_format;
default_format.color = palette().color(Gfx::ColorRole::PlaceholderText);
spans.append({ spans.append({
{ { 0, 0 }, { 0, host_start } }, { { 0, 0 }, { 0, url_parts->scheme_and_subdomain.length() } },
default_format, dark_attributes,
});
Gfx::TextAttributes host_format;
host_format.color = palette().color(Gfx::ColorRole::BaseText);
spans.append({
{ { 0, host_start }, { 0, host_start + host_length } },
host_format,
}); });
spans.append({ spans.append({
{ { 0, host_start + host_length }, { 0, text().length() } }, { { 0, url_parts->scheme_and_subdomain.length() }, { 0, url_parts->scheme_and_subdomain.length() + url_parts->effective_tld_plus_one.length() } },
default_format, highlight_attributes,
}); });
} else if (url.scheme() == "file") {
Gfx::TextAttributes scheme_format;
scheme_format.color = palette().color(Gfx::ColorRole::PlaceholderText);
spans.append({ spans.append({
{ { 0, 0 }, { 0, url.scheme().bytes_as_string_view().length() + 3 } }, {
scheme_format, { 0, url_parts->scheme_and_subdomain.length() + url_parts->effective_tld_plus_one.length() },
{ 0, url_parts->scheme_and_subdomain.length() + url_parts->effective_tld_plus_one.length() + url_parts->remainder.length() },
},
dark_attributes,
}); });
} }
}
document().set_spans(0, move(spans)); document().set_spans(0, move(spans));
update(); update();