diff options
author | Linus Groh <mail@linusgroh.de> | 2022-07-12 20:37:43 +0100 |
---|---|---|
committer | Linus Groh <mail@linusgroh.de> | 2022-07-14 00:42:26 +0100 |
commit | 22a627fc1a694b21325aad32cdbb618fe4080253 (patch) | |
tree | f2885af75827c8809ab4b1d12135a9cd9a1ca8fc /Userland/Libraries/LibWeb/HTML | |
parent | 1748362e05a520986039f25968880260067bf5f7 (diff) | |
download | serenity-22a627fc1a694b21325aad32cdbb618fe4080253.zip |
LibWeb: Move Origin into the HTML namespace
Origin is defined in the HTML Standard, and therefore belongs into the
HTML directory and namespace in LibWeb.
https://html.spec.whatwg.org/multipage/origin.html#origin
Diffstat (limited to 'Userland/Libraries/LibWeb/HTML')
4 files changed, 117 insertions, 3 deletions
diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp b/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp index 255a017e39..0b40fc808a 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContextContainer.cpp @@ -9,7 +9,7 @@ #include <LibWeb/DOM/Event.h> #include <LibWeb/HTML/BrowsingContext.h> #include <LibWeb/HTML/BrowsingContextContainer.h> -#include <LibWeb/Origin.h> +#include <LibWeb/HTML/Origin.h> #include <LibWeb/Page/Page.h> namespace Web::HTML { diff --git a/Userland/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp index 852515535c..dd4cfa9b21 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLIFrameElement.cpp @@ -8,8 +8,8 @@ #include <LibWeb/DOM/Event.h> #include <LibWeb/HTML/BrowsingContext.h> #include <LibWeb/HTML/HTMLIFrameElement.h> +#include <LibWeb/HTML/Origin.h> #include <LibWeb/Layout/FrameBox.h> -#include <LibWeb/Origin.h> namespace Web::HTML { diff --git a/Userland/Libraries/LibWeb/HTML/Origin.h b/Userland/Libraries/LibWeb/HTML/Origin.h new file mode 100644 index 0000000000..e08f4d2fa4 --- /dev/null +++ b/Userland/Libraries/LibWeb/HTML/Origin.h @@ -0,0 +1,114 @@ +/* + * Copyright (c) 2020, Andreas Kling <kling@serenityos.org> + * Copyright (c) 2022, Linus Groh <linusg@serenityos.org> + * + * SPDX-License-Identifier: BSD-2-Clause + */ + +#pragma once + +#include <AK/String.h> + +namespace Web::HTML { + +class Origin { +public: + Origin() = default; + Origin(String const& protocol, String const& host, u16 port) + : m_protocol(protocol) + , m_host(host) + , m_port(port) + { + } + + // https://html.spec.whatwg.org/multipage/origin.html#concept-origin-opaque + bool is_opaque() const { return m_protocol.is_null() && m_host.is_null() && m_port == 0; } + + String const& protocol() const { return m_protocol; } + String const& host() const { return m_host; } + u16 port() const { return m_port; } + + // https://html.spec.whatwg.org/multipage/origin.html#same-origin + bool is_same_origin(Origin const& other) const + { + // 1. If A and B are the same opaque origin, then return true. + if (is_opaque() && other.is_opaque()) + return true; + + // 2. If A and B are both tuple origins and their schemes, hosts, and port are identical, then return true. + // 3. Return false. + return protocol() == other.protocol() + && host() == other.host() + && port() == other.port(); + } + + // https://html.spec.whatwg.org/multipage/origin.html#same-origin-domain + bool is_same_origin_domain(Origin const& other) const + { + // 1. If A and B are the same opaque origin, then return true. + if (is_opaque() && other.is_opaque()) + return true; + + // 2. If A and B are both tuple origins, run these substeps: + if (!is_opaque() && !other.is_opaque()) { + // 1. If A and B's schemes are identical, and their domains are identical and non-null, then return true. + // FIXME: Check domains once supported. + if (protocol() == other.protocol()) + return true; + + // 2. Otherwise, if A and B are same origin and their domains are identical and null, then return true. + // FIXME: Check domains once supported. + if (is_same_origin(other)) + return true; + } + + // 3. Return false. + return false; + } + + // https://html.spec.whatwg.org/multipage/origin.html#ascii-serialisation-of-an-origin + String serialize() + { + // 1. If origin is an opaque origin, then return "null" + if (is_opaque()) + return "null"; + + // 2. Otherwise, let result be origin's scheme. + StringBuilder result; + result.append(protocol()); + + // 3. Append "://" to result. + result.append("://"sv); + + // 4. Append origin's host, serialized, to result. + result.append(host()); + + // 5. If origin's port is non-null, append a U+003A COLON character (:), and origin's port, serialized, to result. + if (port() != 0) { + result.append(':'); + result.append(String::number(port())); + } + // 6. Return result + return result.to_string(); + } + + bool operator==(Origin const& other) const { return is_same_origin(other); } + bool operator!=(Origin const& other) const { return !is_same_origin(other); } + +private: + String m_protocol; + String m_host; + u16 m_port { 0 }; +}; + +} + +namespace AK { +template<> +struct Traits<Web::HTML::Origin> : public GenericTraits<Web::HTML::Origin> { + static unsigned hash(Web::HTML::Origin const& origin) + { + return pair_int_hash(origin.protocol().hash(), pair_int_hash(int_hash(origin.port()), origin.host().hash())); + } +}; +} // namespace AK diff --git a/Userland/Libraries/LibWeb/HTML/Scripting/Environments.h b/Userland/Libraries/LibWeb/HTML/Scripting/Environments.h index 8ae9bbca7d..e884af750a 100644 --- a/Userland/Libraries/LibWeb/HTML/Scripting/Environments.h +++ b/Userland/Libraries/LibWeb/HTML/Scripting/Environments.h @@ -14,7 +14,7 @@ #include <LibJS/Runtime/Realm.h> #include <LibWeb/HTML/BrowsingContext.h> #include <LibWeb/HTML/EventLoop/EventLoop.h> -#include <LibWeb/Origin.h> +#include <LibWeb/HTML/Origin.h> namespace Web::HTML { |