diff --git a/AK/URL.cpp b/AK/URL.cpp index a110936302..79557cea32 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 ec249be0a5..57af992cbe 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 5276c1804b..cdba807e77 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()