summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibWeb/Bindings/LocationObject.cpp
blob: c27f3ddd98a6a1212b633abe306ca1921194c883 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
 * Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <AK/FlyString.h>
#include <AK/StringBuilder.h>
#include <LibWeb/Bindings/LocationObject.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/DOM/Window.h>

namespace Web {
namespace Bindings {

LocationObject::LocationObject(JS::GlobalObject& global_object)
    : Object(*global_object.object_prototype())
{
}

void LocationObject::initialize(JS::GlobalObject& global_object)
{
    Object::initialize(global_object);
    u8 attr = JS::Attribute::Writable | JS::Attribute::Enumerable;
    define_native_property("href", href_getter, href_setter, attr);
    define_native_property("host", host_getter, nullptr, attr);
    define_native_property("hostname", hostname_getter, nullptr, attr);
    define_native_property("pathname", pathname_getter, nullptr, attr);
    define_native_property("hash", hash_getter, nullptr, attr);
    define_native_property("search", search_getter, nullptr, attr);
    define_native_property("protocol", protocol_getter, nullptr, attr);

    define_native_function("reload", reload, 0, JS::Attribute::Enumerable);
}

LocationObject::~LocationObject()
{
}

JS_DEFINE_NATIVE_GETTER(LocationObject::href_getter)
{
    auto& window = static_cast<WindowObject&>(global_object);
    return JS::js_string(vm, window.impl().document().url().to_string());
}

JS_DEFINE_NATIVE_SETTER(LocationObject::href_setter)
{
    auto& window = static_cast<WindowObject&>(global_object);
    auto new_href = value.to_string(global_object);
    if (vm.exception())
        return;
    auto href_url = window.impl().document().complete_url(new_href);
    if (!href_url.is_valid()) {
        vm.throw_exception<JS::URIError>(global_object, String::formatted("Invalid URL '{}'", new_href));
        return;
    }
    window.impl().did_set_location_href({}, href_url);
}

JS_DEFINE_NATIVE_GETTER(LocationObject::pathname_getter)
{
    auto& window = static_cast<WindowObject&>(global_object);
    return JS::js_string(vm, window.impl().document().url().path());
}

JS_DEFINE_NATIVE_GETTER(LocationObject::hostname_getter)
{
    auto& window = static_cast<WindowObject&>(global_object);
    return JS::js_string(vm, window.impl().document().url().host());
}

JS_DEFINE_NATIVE_GETTER(LocationObject::host_getter)
{
    auto& window = static_cast<WindowObject&>(global_object);
    auto url = window.impl().document().url();
    StringBuilder builder;
    builder.append(url.host());
    builder.append(':');
    builder.appendf("%u", url.port());
    return JS::js_string(vm, builder.to_string());
}

JS_DEFINE_NATIVE_GETTER(LocationObject::hash_getter)
{
    auto& window = static_cast<WindowObject&>(global_object);
    auto fragment = window.impl().document().url().fragment();
    if (!fragment.length())
        return JS::js_string(vm, "");
    StringBuilder builder;
    builder.append('#');
    builder.append(fragment);
    return JS::js_string(vm, builder.to_string());
}

JS_DEFINE_NATIVE_GETTER(LocationObject::search_getter)
{
    auto& window = static_cast<WindowObject&>(global_object);
    auto query = window.impl().document().url().query();
    if (!query.length())
        return JS::js_string(vm, "");
    StringBuilder builder;
    builder.append('?');
    builder.append(query);
    return JS::js_string(vm, builder.to_string());
}

JS_DEFINE_NATIVE_GETTER(LocationObject::protocol_getter)
{
    auto& window = static_cast<WindowObject&>(global_object);
    StringBuilder builder;
    builder.append(window.impl().document().url().protocol());
    builder.append(':');
    return JS::js_string(vm, builder.to_string());
}

JS_DEFINE_NATIVE_FUNCTION(LocationObject::reload)
{
    auto& window = static_cast<WindowObject&>(global_object);
    window.impl().did_call_location_reload({});
    return JS::js_undefined();
}

}

}