Sfoglia il codice sorgente

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

Linus Groh 2 anni fa
parent
commit
c380d2cfdc

+ 2 - 25
Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp

@@ -25,6 +25,7 @@
 #include <LibWeb/Layout/InitialContainingBlock.h>
 #include <LibWeb/Layout/TextNode.h>
 #include <LibWeb/Page/Page.h>
+#include <LibWeb/URL/URL.h>
 
 namespace Web::HTML {
 
@@ -39,30 +40,6 @@ static bool url_matches_about_blank(AK::URL const& url)
         && 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 (url’s scheme, url’s host, url’s 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
 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.
-    return url_origin(*url);
+    return URL::url_origin(*url);
 }
 
 // https://html.spec.whatwg.org/multipage/browsers.html#creating-a-new-top-level-browsing-context

+ 33 - 0
Userland/Libraries/LibWeb/URL/URL.cpp

@@ -307,4 +307,37 @@ void URL::set_hash(String const& hash)
         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 url’s 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 (url’s scheme, url’s host, url’s 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 {};
+}
+
 }

+ 2 - 0
Userland/Libraries/LibWeb/URL/URL.h

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