1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 18:17:34 +00:00

LibWeb: Move url_origin() to URL/URL.{cpp,h}

This commit is contained in:
Linus Groh 2022-10-13 18:25:00 +02:00
parent 93405b4aff
commit c380d2cfdc
3 changed files with 37 additions and 25 deletions

View file

@ -25,6 +25,7 @@
#include <LibWeb/Layout/InitialContainingBlock.h> #include <LibWeb/Layout/InitialContainingBlock.h>
#include <LibWeb/Layout/TextNode.h> #include <LibWeb/Layout/TextNode.h>
#include <LibWeb/Page/Page.h> #include <LibWeb/Page/Page.h>
#include <LibWeb/URL/URL.h>
namespace Web::HTML { namespace Web::HTML {
@ -39,30 +40,6 @@ static bool url_matches_about_blank(AK::URL const& url)
&& url.host().is_null(); && url.host().is_null();
} }
// https://url.spec.whatwg.org/#concept-url-origin
static HTML::Origin url_origin(AK::URL const& url)
{
// FIXME: Move this whole function somewhere better.
if (url.scheme() == "blob"sv) {
// FIXME: Implement
return HTML::Origin {};
}
if (url.scheme().is_one_of("ftp"sv, "http"sv, "https"sv, "ws"sv, "wss"sv)) {
// Return the tuple origin (urls scheme, urls host, urls port, null).
return HTML::Origin(url.scheme(), url.host(), url.port().value_or(0));
}
if (url.scheme() == "file"sv) {
// Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin.
// Note: We must return an origin with the `file://' protocol for `file://' iframes to work from `file://' pages.
return HTML::Origin(url.scheme(), String(), 0);
}
return HTML::Origin {};
}
// https://html.spec.whatwg.org/multipage/browsers.html#determining-the-origin // https://html.spec.whatwg.org/multipage/browsers.html#determining-the-origin
HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optional<AK::URL> url, SandboxingFlagSet sandbox_flags, Optional<HTML::Origin> invocation_origin) HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optional<AK::URL> url, SandboxingFlagSet sandbox_flags, Optional<HTML::Origin> invocation_origin)
{ {
@ -88,7 +65,7 @@ HTML::Origin determine_the_origin(BrowsingContext const& browsing_context, Optio
} }
// 5. Return url's origin. // 5. Return url's origin.
return url_origin(*url); return URL::url_origin(*url);
} }
// https://html.spec.whatwg.org/multipage/browsers.html#creating-a-new-top-level-browsing-context // https://html.spec.whatwg.org/multipage/browsers.html#creating-a-new-top-level-browsing-context

View file

@ -307,4 +307,37 @@ void URL::set_hash(String const& hash)
m_url = move(result_url); m_url = move(result_url);
} }
// https://url.spec.whatwg.org/#concept-url-origin
HTML::Origin url_origin(AK::URL const& url)
{
// FIXME: We should probably have an extended version of AK::URL for LibWeb instead of standalone functions like this.
// The origin of a URL url is the origin returned by running these steps, switching on urls scheme:
// "blob"
if (url.scheme() == "blob"sv) {
// FIXME: Support 'blob://' URLs
return HTML::Origin {};
}
// "ftp"
// "http"
// "https"
// "ws"
// "wss"
if (url.scheme().is_one_of("ftp"sv, "http"sv, "https"sv, "ws"sv, "wss"sv)) {
// Return the tuple origin (urls scheme, urls host, urls port, null).
return HTML::Origin(url.scheme(), url.host(), url.port().value_or(0));
}
// "file"
if (url.scheme() == "file"sv) {
// Unfortunate as it is, this is left as an exercise to the reader. When in doubt, return a new opaque origin.
// Note: We must return an origin with the `file://' protocol for `file://' iframes to work from `file://' pages.
return HTML::Origin(url.scheme(), String(), 0);
}
// Return a new opaque origin.
return HTML::Origin {};
}
} }

View file

@ -71,4 +71,6 @@ private:
JS::NonnullGCPtr<URLSearchParams> m_query; JS::NonnullGCPtr<URLSearchParams> m_query;
}; };
HTML::Origin url_origin(AK::URL const&);
} }