Skip to content

Commit

Permalink
fix(URL): allow undefined 2nd args and fix pathname return value (#263)
Browse files Browse the repository at this point in the history
  • Loading branch information
rigor789 authored Dec 9, 2024
1 parent 8b932a3 commit 4219038
Show file tree
Hide file tree
Showing 2 changed files with 83 additions and 56 deletions.
49 changes: 23 additions & 26 deletions NativeScript/runtime/URLImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,16 @@ void URLImpl::Ctor(const v8::FunctionCallbackInfo<v8::Value> &args) {

auto result = ada::parse<ada::url_aggregator>(url_string, &base_url.value());

if (result) {
url = result.value();
} else {
isolate->ThrowException(
v8::Exception::TypeError(ToV8String(isolate, "")));
return;
}
} else {
// treat 2nd arg as undefined otherwise.
auto result = ada::parse<ada::url_aggregator>(url_string, nullptr);
if (result) {
url = result.value();
} else {
Expand All @@ -149,7 +159,6 @@ void URLImpl::Ctor(const v8::FunctionCallbackInfo<v8::Value> &args) {
return;
}
}

} else {
auto result = ada::parse<ada::url_aggregator>(url_string, nullptr);
if (result) {
Expand Down Expand Up @@ -242,7 +251,6 @@ void URLImpl::GetHostName(v8::Local<v8::String> property,
auto value = ptr->GetURL()->get_hostname();

info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));

}

void URLImpl::SetHostName(v8::Local<v8::String> property,
Expand Down Expand Up @@ -271,7 +279,6 @@ void URLImpl::GetHref(v8::Local<v8::String> property,
auto value = ptr->GetURL()->get_href();

info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));

}

void URLImpl::SetHref(v8::Local<v8::String> property,
Expand Down Expand Up @@ -299,7 +306,6 @@ void URLImpl::GetOrigin(v8::Local<v8::String> property,
auto value = ptr->GetURL()->get_origin();

info.GetReturnValue().Set(ToV8String(isolate, value.c_str()));

}

void URLImpl::GetPassword(v8::Local<v8::String> property,
Expand All @@ -314,7 +320,6 @@ void URLImpl::GetPassword(v8::Local<v8::String> property,
auto value = ptr->GetURL()->get_password();

info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));

}

void URLImpl::SetPassword(v8::Local<v8::String> property,
Expand All @@ -341,8 +346,7 @@ void URLImpl::GetPathName(v8::Local<v8::String> property,

auto value = ptr->GetURL()->get_pathname();

info.GetReturnValue().Set(ToV8String(isolate, value.data()));

info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));
}

void URLImpl::SetPathName(v8::Local<v8::String> property,
Expand Down Expand Up @@ -370,7 +374,6 @@ void URLImpl::GetPort(v8::Local<v8::String> property,
auto value = ptr->GetURL()->get_port();

info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));

}

void URLImpl::SetPort(v8::Local<v8::String> property,
Expand Down Expand Up @@ -398,7 +401,6 @@ void URLImpl::GetProtocol(v8::Local<v8::String> property,
auto value = ptr->GetURL()->get_protocol();

info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));

}

void URLImpl::SetProtocol(v8::Local<v8::String> property,
Expand Down Expand Up @@ -427,7 +429,6 @@ void URLImpl::GetSearch(v8::Local<v8::String> property,
auto value = ptr->GetURL()->get_search();

info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));

}

void URLImpl::SetSearch(v8::Local<v8::String> property,
Expand Down Expand Up @@ -456,7 +457,6 @@ void URLImpl::GetUserName(v8::Local<v8::String> property,
auto value = ptr->GetURL()->get_username();

info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));

}

void URLImpl::SetUserName(v8::Local<v8::String> property,
Expand All @@ -473,39 +473,36 @@ void URLImpl::SetUserName(v8::Local<v8::String> property,
}


void URLImpl::ToString(const v8::FunctionCallbackInfo<v8::Value> &args) {
URLImpl *ptr = GetPointer(args.This());
void URLImpl::ToString(const v8::FunctionCallbackInfo<v8::Value> &info) {
URLImpl *ptr = GetPointer(info.This());
if (ptr == nullptr) {
args.GetReturnValue().SetEmptyString();
info.GetReturnValue().SetEmptyString();
return;
}
auto isolate = args.GetIsolate();
auto isolate = info.GetIsolate();


auto value = ptr->GetURL()->get_href();

auto ret = ToV8String(isolate, value.data(), (int)value.length());

args.GetReturnValue().Set(ret);
info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));
}


void URLImpl::CanParse(const v8::FunctionCallbackInfo<v8::Value> &args) {
void URLImpl::CanParse(const v8::FunctionCallbackInfo<v8::Value> &info) {
bool value;
auto count = args.Length();
auto count = info.Length();


auto isolate = args.GetIsolate();
auto isolate = info.GetIsolate();
if (count > 1) {
auto url_string = tns::ToString(isolate, args[0].As<v8::String>());
auto base_string = tns::ToString(isolate, args[1].As<v8::String>());
auto url_string = tns::ToString(isolate, info[0].As<v8::String>());
auto base_string = tns::ToString(isolate, info[1].As<v8::String>());
std::string_view base_string_view(base_string.data(), base_string.length());
value = can_parse(url_string, &base_string_view);
} else {
value = can_parse(tns::ToString(isolate, args[0].As<v8::String>()).c_str(), nullptr);
value = can_parse(tns::ToString(isolate, info[0].As<v8::String>()).c_str(), nullptr);
}

args.GetReturnValue().Set(value);
info.GetReturnValue().Set(value);
}


Expand Down
90 changes: 60 additions & 30 deletions TestRunner/app/tests/URL.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,61 @@
describe("Test URL ", function () {

it("Test invalid URL parsing", function(){
var exceptionCaught = false;
try {
const url = new URL('');
}catch(e){
exceptionCaught = true;
}
expect(exceptionCaught).toBe(true);
});

it("Test valid URL parsing", function(){
var exceptionCaught = false;
try {
const url = new URL('https://google.com');
}catch(e){
exceptionCaught = true;
}
expect(exceptionCaught).toBe(false);
});


it("Test URL fields", function(){
var exceptionCaught = false;
const url = new URL('https://google.com');
expect(url.protocol).toBe('https:');
expect(url.hostname).toBe('google.com');
});

describe("URL", function () {
it("throws on invalid URL", function () {
var exceptionCaught = false;
try {
const url = new URL("");
} catch (e) {
exceptionCaught = true;
}
expect(exceptionCaught).toBe(true);
});

it("does not throw on valid URL", function () {
var exceptionCaught = false;
try {
const url = new URL("https://google.com");
} catch (e) {
exceptionCaught = true;
}
expect(exceptionCaught).toBe(false);
});

it("parses simple urls", function () {
const url = new URL("https://google.com");
expect(url.protocol).toBe("https:");
expect(url.hostname).toBe("google.com");
expect(url.pathname).toBe("/");
expect(url.port).toBe("");
expect(url.search).toBe("");
expect(url.hash).toBe("");
expect(url.username).toBe("");
expect(url.password).toBe("");
expect(url.origin).toBe("https://google.com");
expect(url.searchParams.size).toBe(0);
});

it("parses with undefined base", function () {
const url = new URL("https://google.com", undefined);
expect(url.protocol).toBe("https:");
expect(url.hostname).toBe("google.com");
});

it("parses with null base", function () {
const url = new URL("https://google.com", null);
expect(url.protocol).toBe("https:");
expect(url.hostname).toBe("google.com");
});

it("parses query strings", function () {
const url = new URL("https://google.com?q=hello");
expect(url.search).toBe("?q=hello");
expect(url.searchParams.get("q")).toBe("hello");
expect(url.pathname).toBe("/");
});

it("parses query strings with pathname", function () {
const url = new URL("https://google.com/some/path?q=hello");
expect(url.search).toBe("?q=hello");
expect(url.searchParams.get("q")).toBe("hello");
expect(url.pathname).toBe("/some/path");
});
});

0 comments on commit 4219038

Please sign in to comment.