Skip to content

Commit 4219038

Browse files
authored
fix(URL): allow undefined 2nd args and fix pathname return value (#263)
1 parent 8b932a3 commit 4219038

File tree

2 files changed

+83
-56
lines changed

2 files changed

+83
-56
lines changed

NativeScript/runtime/URLImpl.cpp

+23-26
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,16 @@ void URLImpl::Ctor(const v8::FunctionCallbackInfo<v8::Value> &args) {
141141

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

144+
if (result) {
145+
url = result.value();
146+
} else {
147+
isolate->ThrowException(
148+
v8::Exception::TypeError(ToV8String(isolate, "")));
149+
return;
150+
}
151+
} else {
152+
// treat 2nd arg as undefined otherwise.
153+
auto result = ada::parse<ada::url_aggregator>(url_string, nullptr);
144154
if (result) {
145155
url = result.value();
146156
} else {
@@ -149,7 +159,6 @@ void URLImpl::Ctor(const v8::FunctionCallbackInfo<v8::Value> &args) {
149159
return;
150160
}
151161
}
152-
153162
} else {
154163
auto result = ada::parse<ada::url_aggregator>(url_string, nullptr);
155164
if (result) {
@@ -242,7 +251,6 @@ void URLImpl::GetHostName(v8::Local<v8::String> property,
242251
auto value = ptr->GetURL()->get_hostname();
243252

244253
info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));
245-
246254
}
247255

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

273281
info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));
274-
275282
}
276283

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

301308
info.GetReturnValue().Set(ToV8String(isolate, value.c_str()));
302-
303309
}
304310

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

316322
info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));
317-
318323
}
319324

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

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

344-
info.GetReturnValue().Set(ToV8String(isolate, value.data()));
345-
349+
info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));
346350
}
347351

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

372376
info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));
373-
374377
}
375378

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

400403
info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));
401-
402404
}
403405

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

429431
info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));
430-
431432
}
432433

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

458459
info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));
459-
460460
}
461461

462462
void URLImpl::SetUserName(v8::Local<v8::String> property,
@@ -473,39 +473,36 @@ void URLImpl::SetUserName(v8::Local<v8::String> property,
473473
}
474474

475475

476-
void URLImpl::ToString(const v8::FunctionCallbackInfo<v8::Value> &args) {
477-
URLImpl *ptr = GetPointer(args.This());
476+
void URLImpl::ToString(const v8::FunctionCallbackInfo<v8::Value> &info) {
477+
URLImpl *ptr = GetPointer(info.This());
478478
if (ptr == nullptr) {
479-
args.GetReturnValue().SetEmptyString();
479+
info.GetReturnValue().SetEmptyString();
480480
return;
481481
}
482-
auto isolate = args.GetIsolate();
482+
auto isolate = info.GetIsolate();
483483

484484

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

487-
auto ret = ToV8String(isolate, value.data(), (int)value.length());
488-
489-
args.GetReturnValue().Set(ret);
487+
info.GetReturnValue().Set(ToV8String(isolate, value.data(), (int)value.length()));
490488
}
491489

492490

493-
void URLImpl::CanParse(const v8::FunctionCallbackInfo<v8::Value> &args) {
491+
void URLImpl::CanParse(const v8::FunctionCallbackInfo<v8::Value> &info) {
494492
bool value;
495-
auto count = args.Length();
493+
auto count = info.Length();
496494

497-
498-
auto isolate = args.GetIsolate();
495+
auto isolate = info.GetIsolate();
499496
if (count > 1) {
500-
auto url_string = tns::ToString(isolate, args[0].As<v8::String>());
501-
auto base_string = tns::ToString(isolate, args[1].As<v8::String>());
497+
auto url_string = tns::ToString(isolate, info[0].As<v8::String>());
498+
auto base_string = tns::ToString(isolate, info[1].As<v8::String>());
502499
std::string_view base_string_view(base_string.data(), base_string.length());
503500
value = can_parse(url_string, &base_string_view);
504501
} else {
505-
value = can_parse(tns::ToString(isolate, args[0].As<v8::String>()).c_str(), nullptr);
502+
value = can_parse(tns::ToString(isolate, info[0].As<v8::String>()).c_str(), nullptr);
506503
}
507504

508-
args.GetReturnValue().Set(value);
505+
info.GetReturnValue().Set(value);
509506
}
510507

511508

TestRunner/app/tests/URL.js

+60-30
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,61 @@
1-
describe("Test URL ", function () {
2-
3-
it("Test invalid URL parsing", function(){
4-
var exceptionCaught = false;
5-
try {
6-
const url = new URL('');
7-
}catch(e){
8-
exceptionCaught = true;
9-
}
10-
expect(exceptionCaught).toBe(true);
11-
});
12-
13-
it("Test valid URL parsing", function(){
14-
var exceptionCaught = false;
15-
try {
16-
const url = new URL('https://google.com');
17-
}catch(e){
18-
exceptionCaught = true;
19-
}
20-
expect(exceptionCaught).toBe(false);
21-
});
22-
23-
24-
it("Test URL fields", function(){
25-
var exceptionCaught = false;
26-
const url = new URL('https://google.com');
27-
expect(url.protocol).toBe('https:');
28-
expect(url.hostname).toBe('google.com');
29-
});
30-
1+
describe("URL", function () {
2+
it("throws on invalid URL", function () {
3+
var exceptionCaught = false;
4+
try {
5+
const url = new URL("");
6+
} catch (e) {
7+
exceptionCaught = true;
8+
}
9+
expect(exceptionCaught).toBe(true);
10+
});
11+
12+
it("does not throw on valid URL", function () {
13+
var exceptionCaught = false;
14+
try {
15+
const url = new URL("https://google.com");
16+
} catch (e) {
17+
exceptionCaught = true;
18+
}
19+
expect(exceptionCaught).toBe(false);
20+
});
21+
22+
it("parses simple urls", function () {
23+
const url = new URL("https://google.com");
24+
expect(url.protocol).toBe("https:");
25+
expect(url.hostname).toBe("google.com");
26+
expect(url.pathname).toBe("/");
27+
expect(url.port).toBe("");
28+
expect(url.search).toBe("");
29+
expect(url.hash).toBe("");
30+
expect(url.username).toBe("");
31+
expect(url.password).toBe("");
32+
expect(url.origin).toBe("https://google.com");
33+
expect(url.searchParams.size).toBe(0);
34+
});
35+
36+
it("parses with undefined base", function () {
37+
const url = new URL("https://google.com", undefined);
38+
expect(url.protocol).toBe("https:");
39+
expect(url.hostname).toBe("google.com");
40+
});
41+
42+
it("parses with null base", function () {
43+
const url = new URL("https://google.com", null);
44+
expect(url.protocol).toBe("https:");
45+
expect(url.hostname).toBe("google.com");
46+
});
47+
48+
it("parses query strings", function () {
49+
const url = new URL("https://google.com?q=hello");
50+
expect(url.search).toBe("?q=hello");
51+
expect(url.searchParams.get("q")).toBe("hello");
52+
expect(url.pathname).toBe("/");
53+
});
54+
55+
it("parses query strings with pathname", function () {
56+
const url = new URL("https://google.com/some/path?q=hello");
57+
expect(url.search).toBe("?q=hello");
58+
expect(url.searchParams.get("q")).toBe("hello");
59+
expect(url.pathname).toBe("/some/path");
60+
});
3161
});

0 commit comments

Comments
 (0)