From 0405ab91aaf22424b0ae28cba120b6b817a1afe0 Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Mon, 18 Nov 2019 22:04:39 +0100 Subject: [PATCH] LibHTML+AK: Move URL completion from Document to AK::URL Completing a relative URL based on a base URL seems like generally useful functionality. --- AK/URL.cpp | 28 ++++++++++++++++++++++++++++ AK/URL.h | 2 ++ Libraries/LibHTML/DOM/Document.cpp | 24 +----------------------- 3 files changed, 31 insertions(+), 23 deletions(-) diff --git a/AK/URL.cpp b/AK/URL.cpp index a1109363021..79557cea321 100644 --- a/AK/URL.cpp +++ b/AK/URL.cpp @@ -1,3 +1,4 @@ +#include #include #include @@ -146,4 +147,31 @@ String URL::to_string() const return builder.to_string(); } +URL URL::complete_url(const String& string) const +{ + URL url(string); + if (url.is_valid()) + return url; + + FileSystemPath fspath(path()); + StringBuilder builder; + builder.append('/'); + + bool document_url_ends_in_slash = path()[path().length() - 1] == '/'; + + for (int i = 0; i < fspath.parts().size(); ++i) { + if (i == fspath.parts().size() - 1 && !document_url_ends_in_slash) + break; + builder.append(fspath.parts()[i]); + builder.append('/'); + } + builder.append(string); + auto built = builder.to_string(); + fspath = FileSystemPath(built); + + url = *this; + url.set_path(fspath.string()); + return url; +} + } diff --git a/AK/URL.h b/AK/URL.h index ec249be0a51..57af992cbed 100644 --- a/AK/URL.h +++ b/AK/URL.h @@ -31,6 +31,8 @@ public: String to_string() const; + URL complete_url(const String&) const; + private: bool parse(const StringView&); diff --git a/Libraries/LibHTML/DOM/Document.cpp b/Libraries/LibHTML/DOM/Document.cpp index 5276c1804b4..cdba807e777 100644 --- a/Libraries/LibHTML/DOM/Document.cpp +++ b/Libraries/LibHTML/DOM/Document.cpp @@ -156,29 +156,7 @@ RefPtr Document::background_image() const URL Document::complete_url(const String& string) const { - URL url(string); - if (url.is_valid()) - return url; - - FileSystemPath fspath(m_url.path()); - StringBuilder builder; - builder.append('/'); - - bool document_url_ends_in_slash = m_url.path()[m_url.path().length() - 1] == '/'; - - for (int i = 0; i < fspath.parts().size(); ++i) { - if (i == fspath.parts().size() - 1 && !document_url_ends_in_slash) - break; - builder.append(fspath.parts()[i]); - builder.append('/'); - } - builder.append(string); - auto built = builder.to_string(); - fspath = FileSystemPath(built); - - url = m_url; - url.set_path(fspath.string()); - return url; + return m_url.complete_url(string); } void Document::force_layout()