Skip to content

Commit

Permalink
fix(URL): allow undefined 2nd args (#1826)
Browse files Browse the repository at this point in the history
  • Loading branch information
rigor789 authored Oct 15, 2024
1 parent 27eec84 commit 2bab8f5
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 52 deletions.
87 changes: 59 additions & 28 deletions test-app/app/src/main/assets/app/tests/testURLImpl.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,62 @@
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);
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("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("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("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');

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");
});
});

45 changes: 21 additions & 24 deletions test-app/runtime/src/main/cpp/URLImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,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(v8::String::Empty(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 @@ -139,7 +149,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 All @@ -149,7 +158,6 @@ void URLImpl::Ctor(const v8::FunctionCallbackInfo<v8::Value> &args) {
v8::Exception::TypeError(v8::String::Empty(isolate)));
return;
}

}

auto ret = args.This();
Expand All @@ -162,7 +170,6 @@ void URLImpl::Ctor(const v8::FunctionCallbackInfo<v8::Value> &args) {
urlImpl->BindFinalizer(isolate, ret);

args.GetReturnValue().Set(ret);

}


Expand All @@ -178,7 +185,6 @@ void URLImpl::GetHash(v8::Local<v8::String> property,
auto value = ptr->GetURL()->get_hash();
info.GetReturnValue().Set(
ArgConverter::ConvertToV8String(isolate, value.data(), value.length()));

}

void URLImpl::SetHash(v8::Local<v8::String> property,
Expand Down Expand Up @@ -235,7 +241,6 @@ void URLImpl::GetHostName(v8::Local<v8::String> property,

info.GetReturnValue().Set(
ArgConverter::ConvertToV8String(isolate, value.data(), value.length()));

}

void URLImpl::SetHostName(v8::Local<v8::String> property,
Expand Down Expand Up @@ -265,7 +270,6 @@ void URLImpl::GetHref(v8::Local<v8::String> property,

info.GetReturnValue().Set(
ArgConverter::ConvertToV8String(isolate, value.data(), value.length()));

}

void URLImpl::SetHref(v8::Local<v8::String> property,
Expand Down Expand Up @@ -294,7 +298,6 @@ void URLImpl::GetOrigin(v8::Local<v8::String> property,

info.GetReturnValue().Set(
ArgConverter::ConvertToV8String(isolate, value.data(), value.length()));

}

void URLImpl::GetPassword(v8::Local<v8::String> property,
Expand All @@ -310,7 +313,6 @@ void URLImpl::GetPassword(v8::Local<v8::String> property,

info.GetReturnValue().Set(
ArgConverter::ConvertToV8String(isolate, value.data(), value.length()));

}

void URLImpl::SetPassword(v8::Local<v8::String> property,
Expand Down Expand Up @@ -339,7 +341,6 @@ void URLImpl::GetPathName(v8::Local<v8::String> property,

info.GetReturnValue().Set(
ArgConverter::ConvertToV8String(isolate, value.data(), value.length()));

}

void URLImpl::SetPathName(v8::Local<v8::String> property,
Expand Down Expand Up @@ -368,7 +369,6 @@ void URLImpl::GetPort(v8::Local<v8::String> property,

info.GetReturnValue().Set(
ArgConverter::ConvertToV8String(isolate, value.data(), value.length()));

}

void URLImpl::SetPort(v8::Local<v8::String> property,
Expand Down Expand Up @@ -397,7 +397,6 @@ void URLImpl::GetProtocol(v8::Local<v8::String> property,

info.GetReturnValue().Set(
ArgConverter::ConvertToV8String(isolate, value.data(), value.length()));

}

void URLImpl::SetProtocol(v8::Local<v8::String> property,
Expand Down Expand Up @@ -427,7 +426,6 @@ void URLImpl::GetSearch(v8::Local<v8::String> property,

info.GetReturnValue().Set(
ArgConverter::ConvertToV8String(isolate, value.data(), value.length()));

}

void URLImpl::SetSearch(v8::Local<v8::String> property,
Expand Down Expand Up @@ -457,7 +455,6 @@ void URLImpl::GetUserName(v8::Local<v8::String> property,

info.GetReturnValue().Set(
ArgConverter::ConvertToV8String(isolate, value.data(), value.length()));

}

void URLImpl::SetUserName(v8::Local<v8::String> property,
Expand All @@ -474,36 +471,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 = ArgConverter::ConvertToV8String(isolate, value.data(), value.length());

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


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();


if (count > 1) {
auto url_string = ArgConverter::ConvertToString(args[0].As<v8::String>());
auto base_string = ArgConverter::ConvertToString(args[1].As<v8::String>());
auto url_string = ArgConverter::ConvertToString(info[0].As<v8::String>());
auto base_string = ArgConverter::ConvertToString(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(ArgConverter::ConvertToString(args[0].As<v8::String>()).c_str(), nullptr);
value = can_parse(ArgConverter::ConvertToString(info[0].As<v8::String>()).c_str(), nullptr);
}

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

0 comments on commit 2bab8f5

Please sign in to comment.