Coverage Report

Created: 2025-12-18 07:52

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/serenity/Userland/Libraries/LibWeb/DOMURL/DOMURL.h
Line
Count
Source
1
/*
2
 * Copyright (c) 2021, Idan Horowitz <idan.horowitz@serenityos.org>
3
 * Copyright (c) 2021, the SerenityOS developers.
4
 * Copyright (c) 2023, networkException <networkexception@serenityos.org>
5
 * Copyright (c) 2024, Shannon Booth <shannon@serenityos.org>
6
 *
7
 * SPDX-License-Identifier: BSD-2-Clause
8
 */
9
10
#pragma once
11
12
#include <LibURL/URL.h>
13
#include <LibWeb/Bindings/PlatformObject.h>
14
#include <LibWeb/DOMURL/URLSearchParams.h>
15
#include <LibWeb/WebIDL/ExceptionOr.h>
16
17
namespace Web::DOMURL {
18
19
class DOMURL : public Bindings::PlatformObject {
20
    WEB_PLATFORM_OBJECT(DOMURL, Bindings::PlatformObject);
21
    JS_DECLARE_ALLOCATOR(DOMURL);
22
23
public:
24
    [[nodiscard]] static JS::NonnullGCPtr<DOMURL> create(JS::Realm&, URL::URL, JS::NonnullGCPtr<URLSearchParams> query);
25
    static WebIDL::ExceptionOr<JS::NonnullGCPtr<DOMURL>> construct_impl(JS::Realm&, String const& url, Optional<String> const& base = {});
26
27
    virtual ~DOMURL() override;
28
29
    static WebIDL::ExceptionOr<String> create_object_url(JS::VM&, JS::NonnullGCPtr<FileAPI::Blob> object);
30
    static WebIDL::ExceptionOr<void> revoke_object_url(JS::VM&, StringView url);
31
32
    static JS::GCPtr<DOMURL> parse_for_bindings(JS::VM&, String const& url, Optional<String> const& base = {});
33
    static bool can_parse(JS::VM&, String const& url, Optional<String> const& base = {});
34
35
    WebIDL::ExceptionOr<String> href() const;
36
    WebIDL::ExceptionOr<void> set_href(String const&);
37
38
    WebIDL::ExceptionOr<String> origin() const;
39
40
    WebIDL::ExceptionOr<String> protocol() const;
41
    WebIDL::ExceptionOr<void> set_protocol(String const&);
42
43
    String const& username() const;
44
    void set_username(String const&);
45
46
    String const& password() const;
47
    void set_password(String const&);
48
49
    WebIDL::ExceptionOr<String> host() const;
50
    void set_host(String const&);
51
52
    WebIDL::ExceptionOr<String> hostname() const;
53
    void set_hostname(String const&);
54
55
    WebIDL::ExceptionOr<String> port() const;
56
    void set_port(String const&);
57
58
    String pathname() const;
59
    void set_pathname(String const&);
60
61
0
    Optional<String> const& fragment() const { return m_url.fragment(); }
62
63
0
    ByteString path_segment_at_index(size_t index) const { return m_url.path_segment_at_index(index); }
64
65
0
    void set_paths(Vector<ByteString> const& paths) { return m_url.set_paths(paths); }
66
67
    // FIXME: Reimplement this to meet the definition in https://url.spec.whatwg.org/#url-opaque-path once we modernize URL to meet the spec.
68
0
    bool cannot_be_a_base_url() const { return m_url.cannot_be_a_base_url(); }
69
70
    WebIDL::ExceptionOr<String> search() const;
71
    void set_search(String const&);
72
73
    JS::NonnullGCPtr<URLSearchParams const> search_params() const;
74
75
    WebIDL::ExceptionOr<String> hash() const;
76
    void set_hash(String const&);
77
78
    WebIDL::ExceptionOr<String> to_json() const;
79
80
0
    Optional<String> const& query() const { return m_url.query(); }
81
0
    void set_query(Badge<URLSearchParams>, Optional<String> query) { m_url.set_query(move(query)); }
82
83
private:
84
    DOMURL(JS::Realm&, URL::URL, JS::NonnullGCPtr<URLSearchParams> query);
85
86
    static JS::NonnullGCPtr<DOMURL> initialize_a_url(JS::Realm&, URL::URL const&);
87
88
    virtual void initialize(JS::Realm&) override;
89
    virtual void visit_edges(Cell::Visitor&) override;
90
91
    URL::URL m_url;
92
    JS::NonnullGCPtr<URLSearchParams> m_query;
93
};
94
95
bool host_is_domain(URL::Host const&);
96
97
// https://url.spec.whatwg.org/#potentially-strip-trailing-spaces-from-an-opaque-path
98
void strip_trailing_spaces_from_an_opaque_path(DOMURL& url);
99
100
// https://url.spec.whatwg.org/#concept-url-parser
101
URL::URL parse(StringView input, Optional<URL::URL> const& base_url = {}, Optional<StringView> encoding = {});
102
103
}