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/HTML/WorkerLocation.cpp
Line
Count
Source
1
/*
2
 * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
3
 *
4
 * SPDX-License-Identifier: BSD-2-Clause
5
 */
6
7
#include <LibURL/Parser.h>
8
#include <LibWeb/Bindings/WorkerLocationPrototype.h>
9
#include <LibWeb/HTML/WorkerGlobalScope.h>
10
#include <LibWeb/HTML/WorkerLocation.h>
11
12
namespace Web::HTML {
13
14
JS_DEFINE_ALLOCATOR(WorkerLocation);
15
16
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-href
17
WebIDL::ExceptionOr<String> WorkerLocation::href() const
18
0
{
19
0
    auto& vm = realm().vm();
20
    // The href getter steps are to return this's WorkerGlobalScope object's url, serialized.
21
0
    return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_global_scope->url().serialize()));
22
0
}
23
24
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-origin
25
WebIDL::ExceptionOr<String> WorkerLocation::origin() const
26
0
{
27
0
    auto& vm = realm().vm();
28
    // The origin getter steps are to return the serialization of this's WorkerGlobalScope object's url's origin.
29
0
    return TRY_OR_THROW_OOM(vm, String::from_byte_string(m_global_scope->url().origin().serialize()));
30
0
}
31
32
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-protocol
33
WebIDL::ExceptionOr<String> WorkerLocation::protocol() const
34
0
{
35
0
    auto& vm = realm().vm();
36
    // The protocol getter steps are to return this's WorkerGlobalScope object's url's scheme, followed by ":".
37
0
    return TRY_OR_THROW_OOM(vm, String::formatted("{}:", m_global_scope->url().scheme()));
38
0
}
39
40
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-host
41
WebIDL::ExceptionOr<String> WorkerLocation::host() const
42
0
{
43
0
    auto& vm = realm().vm();
44
45
    // The host getter steps are:
46
    // 1. Let url be this's WorkerGlobalScope object's url.
47
0
    auto const& url = m_global_scope->url();
48
49
    // 2. If url's host is null, return the empty string.
50
0
    if (url.host().has<Empty>())
51
0
        return String {};
52
53
    // 3. If url's port is null, return url's host, serialized.
54
0
    if (!url.port().has_value())
55
0
        return TRY_OR_THROW_OOM(vm, url.serialized_host());
56
57
    // 4. Return url's host, serialized, followed by ":" and url's port, serialized.
58
0
    return TRY_OR_THROW_OOM(vm, String::formatted("{}:{}", TRY_OR_THROW_OOM(vm, url.serialized_host()), url.port().value()));
59
0
}
60
61
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-hostname
62
WebIDL::ExceptionOr<String> WorkerLocation::hostname() const
63
0
{
64
0
    auto& vm = realm().vm();
65
66
    // The hostname getter steps are:
67
    // 1. Let host be this's WorkerGlobalScope object's url's host.
68
0
    auto const& host = m_global_scope->url().host();
69
70
    // 2. If host is null, return the empty string.
71
0
    if (host.has<Empty>())
72
0
        return String {};
73
74
    // 3. Return host, serialized.
75
0
    return TRY_OR_THROW_OOM(vm, URL::Parser::serialize_host(host));
76
0
}
77
78
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-port
79
WebIDL::ExceptionOr<String> WorkerLocation::port() const
80
0
{
81
    // The port getter steps are:
82
    // 1. Let port be this's WorkerGlobalScope object's url's port.
83
0
    auto const& port = m_global_scope->url().port();
84
85
    // 2. If port is null, return the empty string.
86
0
    if (!port.has_value())
87
0
        return String {};
88
    // 3. Return port, serialized.
89
0
    return String::number(port.value());
90
0
}
91
92
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-pathname
93
String WorkerLocation::pathname() const
94
0
{
95
    // The pathname getter steps are to return the result of URL path serializing this's WorkerGlobalScope object's url.
96
0
    return m_global_scope->url().serialize_path();
97
0
}
98
99
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-search
100
WebIDL::ExceptionOr<String> WorkerLocation::search() const
101
0
{
102
0
    auto& vm = realm().vm();
103
104
    // The search getter steps are:
105
    // 1. Let query be this's WorkerGlobalScope object's url's query.
106
0
    auto const& query = m_global_scope->url().query();
107
108
    // 2. If query is either null or the empty string, return the empty string.
109
0
    if (!query.has_value() || query->is_empty())
110
0
        return String {};
111
112
    // 3. Return "?", followed by query.
113
0
    return TRY_OR_THROW_OOM(vm, String::formatted("?{}", *query));
114
0
}
115
116
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerlocation-hash
117
WebIDL::ExceptionOr<String> WorkerLocation::hash() const
118
0
{
119
0
    auto& vm = realm().vm();
120
121
    // The hash getter steps are:
122
    // 1. Let fragment be this's WorkerGlobalScope object's url's fragment.
123
0
    auto const& fragment = m_global_scope->url().fragment();
124
125
    // 2. If fragment is either null or the empty string, return the empty string.
126
0
    if (!fragment.has_value() || fragment->is_empty())
127
0
        return String {};
128
129
    // 3. Return "#", followed by fragment.
130
0
    return TRY_OR_THROW_OOM(vm, String::formatted("#{}", *fragment));
131
0
}
132
133
WorkerLocation::WorkerLocation(WorkerGlobalScope& global_scope)
134
0
    : PlatformObject(global_scope.realm())
135
0
    , m_global_scope(global_scope)
136
0
{
137
    // FIXME: Set prototype once we can get to worker scope prototypes.
138
0
}
139
140
0
WorkerLocation::~WorkerLocation() = default;
141
142
void WorkerLocation::initialize(JS::Realm& realm)
143
0
{
144
0
    Base::initialize(realm);
145
0
    WEB_SET_PROTOTYPE_FOR_INTERFACE(WorkerLocation);
146
0
}
147
148
void WorkerLocation::visit_edges(Cell::Visitor& visitor)
149
0
{
150
0
    Base::visit_edges(visitor);
151
0
    visitor.visit(m_global_scope);
152
0
}
153
154
}