mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-25 09:00:22 +00:00
LibHTML+AK: Move URL completion from Document to AK::URL
Completing a relative URL based on a base URL seems like generally useful functionality.
This commit is contained in:
parent
47326042c5
commit
0405ab91aa
Notes:
sideshowbarker
2024-07-19 11:08:38 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/0405ab91aaf
3 changed files with 31 additions and 23 deletions
28
AK/URL.cpp
28
AK/URL.cpp
|
@ -1,3 +1,4 @@
|
||||||
|
#include <AK/FileSystemPath.h>
|
||||||
#include <AK/StringBuilder.h>
|
#include <AK/StringBuilder.h>
|
||||||
#include <AK/URL.h>
|
#include <AK/URL.h>
|
||||||
|
|
||||||
|
@ -146,4 +147,31 @@ String URL::to_string() const
|
||||||
return builder.to_string();
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
2
AK/URL.h
2
AK/URL.h
|
@ -31,6 +31,8 @@ public:
|
||||||
|
|
||||||
String to_string() const;
|
String to_string() const;
|
||||||
|
|
||||||
|
URL complete_url(const String&) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool parse(const StringView&);
|
bool parse(const StringView&);
|
||||||
|
|
||||||
|
|
|
@ -156,29 +156,7 @@ RefPtr<GraphicsBitmap> Document::background_image() const
|
||||||
|
|
||||||
URL Document::complete_url(const String& string) const
|
URL Document::complete_url(const String& string) const
|
||||||
{
|
{
|
||||||
URL url(string);
|
return m_url.complete_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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Document::force_layout()
|
void Document::force_layout()
|
||||||
|
|
Loading…
Reference in a new issue