forked from BabylonJS/JsRuntimeHost
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathURL.cpp
More file actions
135 lines (112 loc) · 4.19 KB
/
URL.cpp
File metadata and controls
135 lines (112 loc) · 4.19 KB
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
127
128
129
130
131
132
133
134
135
#include "URL.h"
#include <sstream>
namespace Babylon::Polyfills::Internal
{
static constexpr auto JS_URL_CONSTRUCTOR_NAME = "URL";
void URL::Initialize(Napi::Env env)
{
if (env.Global().Get(JS_URL_CONSTRUCTOR_NAME).IsUndefined())
{
Napi::Function func = Napi::ObjectWrap<URL>::DefineClass(
env,
JS_URL_CONSTRUCTOR_NAME,
{
InstanceAccessor("search", &URL::GetSearch, &URL::SetSearch),
InstanceAccessor("href", &URL::GetHref, &URL::SetHref),
InstanceAccessor("origin", &URL::GetOrigin, nullptr),
InstanceAccessor("pathname", &URL::GetPathname, nullptr),
InstanceAccessor("hostname", &URL::GetHostname, nullptr),
InstanceAccessor("searchParams", &URL::GetSearchParams, nullptr),
});
env.Global().Set(JS_URL_CONSTRUCTOR_NAME, func);
}
}
URL& URL::GetFromJavaScript(Napi::Env env)
{
return *URL::Unwrap(JsRuntime::NativeObject::GetFromJavaScript(env).Get(JS_URL_CONSTRUCTOR_NAME).As<Napi::Object>());
}
Napi::Value URL::GetSearch(const Napi::CallbackInfo&)
{
std::string allParams = GetSearchQuery();
return Napi::Value::From(Env(), allParams);
}
void URL::SetSearch(const Napi::CallbackInfo&, const Napi::Value& value)
{
m_search = value.As<Napi::String>();
}
Napi::Value URL::GetHref(const Napi::CallbackInfo&)
{
std::stringstream resultHref;
resultHref << m_origin;
resultHref << m_pathname;
std::string allParams = GetSearchQuery();
resultHref << allParams;
return Napi::Value::From(Env(), resultHref.str());
}
void URL::SetHref(const Napi::CallbackInfo&, const Napi::Value& value)
{
m_href = value.As<Napi::String>();
}
Napi::Value URL::GetOrigin(const Napi::CallbackInfo&)
{
return Napi::Value::From(Env(), m_origin);
}
Napi::Value URL::GetPathname(const Napi::CallbackInfo&)
{
return Napi::Value::From(Env(), m_pathname);
}
Napi::Value URL::GetHostname(const Napi::CallbackInfo&)
{
return Napi::Value::From(Env(), m_hostname);
}
// gets search params from UrlSearchParams class
std::string URL::GetSearchQuery()
{
auto searchParamsObj = URLSearchParams::Unwrap(m_searchParamsReference.Value());
return searchParamsObj->GetAllParams();
}
Napi::Value URL::GetSearchParams(const Napi::CallbackInfo&)
{
return m_searchParamsReference.Value();
}
// TODO current URL constructor is incomplete, it only supports one argument
// and the url parsing is limited, this logic should be moved to UrlLib and use platform
// specific functions to parse the URL and get the parts
URL::URL(const Napi::CallbackInfo& info)
: Napi::ObjectWrap<URL>{info}
{
if (!info.Length())
{
return;
}
// Store Entire URL
m_href = info[0].As<Napi::String>();
// Get Position of ? to store search var
const size_t qIndex = m_href.find_last_of('?');
if (qIndex != std::string::npos)
{
m_search = m_href.substr(qIndex, m_href.size() - qIndex);
}
// get UrlSearchParams object
const Napi::Object searchParams = info.Env().Global().Get(URLSearchParams::JS_URL_SEARCH_PARAMS_CONSTRUCTOR_NAME).As<Napi::Function>().New({Napi::Value::From(info.Env(), m_search)});
m_searchParamsReference = Napi::Persistent(searchParams);
// Get URL Domain
const size_t start = m_href.find("//");
const size_t originStart = m_href.find("h");
const size_t index = m_href.find_first_of("/", start + 2);
m_origin = m_href.substr(originStart, index);
m_hostname = m_origin.substr(start + 2, index);
if (index != std::string::npos)
{
m_pathname = m_href.substr(index, qIndex - index);
}
}
}
namespace Babylon::Polyfills::URL
{
void BABYLON_API Initialize(Napi::Env env)
{
Internal::URL::Initialize(env);
Internal::URLSearchParams::Initialize(env);
}
}