Skip to content

Commit 8906c63

Browse files
committed
basic url parsing working
* also reintroduces old `URLSearchParams` implementation since ada prefers its own iterator where we'd like to use our own.
1 parent bfe78d1 commit 8906c63

File tree

4 files changed

+274
-106
lines changed

4 files changed

+274
-106
lines changed

src/browser/html/document.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ pub const HTMLDocument = struct {
4242
// JS funcs
4343
// --------
4444

45-
pub fn get_domain(self: *parser.DocumentHTML, page: *Page) ![]const u8 {
45+
pub fn get_domain(self: *parser.DocumentHTML) ![]const u8 {
4646
// libdom's document_html get_domain always returns null, this is
4747
// the way MDN recommends getting the domain anyways, since document.domain
4848
// is deprecated.
4949
const location = try parser.documentHTMLGetLocation(Location, self) orelse return "";
50-
return location.get_host(page);
50+
return location.get_host();
5151
}
5252

5353
pub fn set_domain(_: *parser.DocumentHTML, _: []const u8) ![]const u8 {

src/browser/html/elements.zig

Lines changed: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -275,54 +275,53 @@ pub const HTMLAnchorElement = struct {
275275
// TODO return a disposable string
276276
pub fn get_origin(self: *parser.Anchor, page: *Page) ![]const u8 {
277277
var u = try url(self, page);
278-
return try u.get_origin(page);
278+
return u.get_origin(page);
279279
}
280280

281281
// TODO return a disposable string
282282
pub fn get_protocol(self: *parser.Anchor, page: *Page) ![]const u8 {
283283
var u = try url(self, page);
284-
return u.get_protocol(page);
284+
return u.get_protocol();
285285
}
286286

287287
pub fn set_protocol(self: *parser.Anchor, v: []const u8, page: *Page) !void {
288288
const arena = page.arena;
289+
_ = arena;
289290
var u = try url(self, page);
290291

291-
u.uri.scheme = v;
292-
const href = try u.toString(arena);
292+
u.set_protocol(v);
293+
const href = try u.get_href(page);
293294
try parser.anchorSetHref(self, href);
294295
}
295296

296-
// TODO return a disposable string
297297
pub fn get_host(self: *parser.Anchor, page: *Page) ![]const u8 {
298298
var u = try url(self, page);
299-
return try u.get_host(page);
299+
return u.get_host();
300300
}
301301

302302
pub fn set_host(self: *parser.Anchor, v: []const u8, page: *Page) !void {
303303
// search : separator
304-
var p: ?u16 = null;
304+
var p: ?[]const u8 = null;
305305
var h: []const u8 = undefined;
306306
for (v, 0..) |c, i| {
307307
if (c == ':') {
308308
h = v[0..i];
309-
p = try std.fmt.parseInt(u16, v[i + 1 ..], 10);
309+
//p = try std.fmt.parseInt(u16, v[i + 1 ..], 10);
310+
p = v[i + 1 ..];
310311
break;
311312
}
312313
}
313314

314-
const arena = page.arena;
315315
var u = try url(self, page);
316316

317-
if (p) |pp| {
318-
u.uri.host = .{ .raw = h };
319-
u.uri.port = pp;
317+
if (p) |port| {
318+
u.set_host(h);
319+
u.set_port(port);
320320
} else {
321-
u.uri.host = .{ .raw = v };
322-
u.uri.port = null;
321+
u.set_host(v);
323322
}
324323

325-
const href = try u.toString(arena);
324+
const href = try u.get_href(page);
326325
try parser.anchorSetHref(self, href);
327326
}
328327

@@ -332,30 +331,26 @@ pub const HTMLAnchorElement = struct {
332331
}
333332

334333
pub fn set_hostname(self: *parser.Anchor, v: []const u8, page: *Page) !void {
335-
const arena = page.arena;
336334
var u = try url(self, page);
337-
u.uri.host = .{ .raw = v };
338-
const href = try u.toString(arena);
335+
u.set_host(v);
336+
const href = try u.get_href(page);
339337
try parser.anchorSetHref(self, href);
340338
}
341339

342340
// TODO return a disposable string
343341
pub fn get_port(self: *parser.Anchor, page: *Page) ![]const u8 {
344342
var u = try url(self, page);
345-
return try u.get_port(page);
343+
return u.get_port();
346344
}
347345

348346
pub fn set_port(self: *parser.Anchor, v: ?[]const u8, page: *Page) !void {
349-
const arena = page.arena;
350347
var u = try url(self, page);
351348

352349
if (v != null and v.?.len > 0) {
353-
u.uri.port = try std.fmt.parseInt(u16, v.?, 10);
354-
} else {
355-
u.uri.port = null;
350+
u.set_host(v.?);
356351
}
357352

358-
const href = try u.toString(arena);
353+
const href = try u.get_href(page);
359354
try parser.anchorSetHref(self, href);
360355
}
361356

@@ -366,37 +361,28 @@ pub const HTMLAnchorElement = struct {
366361
}
367362

368363
pub fn set_username(self: *parser.Anchor, v: ?[]const u8, page: *Page) !void {
369-
const arena = page.arena;
370-
var u = try url(self, page);
364+
if (v) |username| {
365+
var u = try url(self, page);
366+
u.set_username(username);
371367

372-
if (v) |vv| {
373-
u.uri.user = .{ .raw = vv };
374-
} else {
375-
u.uri.user = null;
368+
const href = try u.get_href(page);
369+
try parser.anchorSetHref(self, href);
376370
}
377-
const href = try u.toString(arena);
378-
379-
try parser.anchorSetHref(self, href);
380371
}
381372

382-
// TODO return a disposable string
383373
pub fn get_password(self: *parser.Anchor, page: *Page) ![]const u8 {
384374
var u = try url(self, page);
385-
return try page.arena.dupe(u8, u.get_password());
375+
return u.get_password();
386376
}
387377

388378
pub fn set_password(self: *parser.Anchor, v: ?[]const u8, page: *Page) !void {
389-
const arena = page.arena;
390-
var u = try url(self, page);
379+
if (v) |password| {
380+
var u = try url(self, page);
381+
u.set_password(password);
391382

392-
if (v) |vv| {
393-
u.uri.password = .{ .raw = vv };
394-
} else {
395-
u.uri.password = null;
383+
const href = try u.get_href(page);
384+
try parser.anchorSetHref(self, href);
396385
}
397-
const href = try u.toString(arena);
398-
399-
try parser.anchorSetHref(self, href);
400386
}
401387

402388
// TODO return a disposable string
@@ -406,44 +392,35 @@ pub const HTMLAnchorElement = struct {
406392
}
407393

408394
pub fn set_pathname(self: *parser.Anchor, v: []const u8, page: *Page) !void {
409-
const arena = page.arena;
410395
var u = try url(self, page);
411-
u.uri.path = .{ .raw = v };
412-
const href = try u.toString(arena);
413-
396+
u.set_pathname(v);
397+
const href = try u.get_href(page);
414398
try parser.anchorSetHref(self, href);
415399
}
416400

417401
pub fn get_search(self: *parser.Anchor, page: *Page) ![]const u8 {
418402
var u = try url(self, page);
419-
return try u.get_search(page);
403+
return u.get_search(page);
420404
}
421405

422-
pub fn set_search(self: *parser.Anchor, v: ?[]const u8, page: *Page) !void {
406+
pub fn set_search(self: *parser.Anchor, v: []const u8, page: *Page) !void {
423407
var u = try url(self, page);
424408
try u.set_search(v, page);
425409

426-
const href = try u.toString(page.call_arena);
410+
const href = try u.get_href(page);
427411
try parser.anchorSetHref(self, href);
428412
}
429413

430414
// TODO return a disposable string
431415
pub fn get_hash(self: *parser.Anchor, page: *Page) ![]const u8 {
432416
var u = try url(self, page);
433-
return try u.get_hash(page);
417+
return u.get_hash();
434418
}
435419

436-
pub fn set_hash(self: *parser.Anchor, v: ?[]const u8, page: *Page) !void {
437-
const arena = page.arena;
420+
pub fn set_hash(self: *parser.Anchor, v: []const u8, page: *Page) !void {
438421
var u = try url(self, page);
439-
440-
if (v) |vv| {
441-
u.uri.fragment = .{ .raw = vv };
442-
} else {
443-
u.uri.fragment = null;
444-
}
445-
const href = try u.toString(arena);
446-
422+
u.set_hash(v);
423+
const href = try u.get_href(page);
447424
try parser.anchorSetHref(self, href);
448425
}
449426
};

src/browser/html/location.zig

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,13 @@ pub const Location = struct {
2929
return "";
3030
}
3131

32-
pub fn get_protocol(self: *Location, page: *Page) ![]const u8 {
33-
if (self.url) |*u| return u.get_protocol(page);
32+
pub fn get_protocol(self: *Location) []const u8 {
33+
if (self.url) |*u| return u.get_protocol();
3434
return "";
3535
}
3636

37-
pub fn get_host(self: *Location, page: *Page) ![]const u8 {
38-
if (self.url) |*u| return u.get_host(page);
37+
pub fn get_host(self: *Location) []const u8 {
38+
if (self.url) |*u| return u.get_host();
3939
return "";
4040
}
4141

@@ -44,8 +44,8 @@ pub const Location = struct {
4444
return "";
4545
}
4646

47-
pub fn get_port(self: *Location, page: *Page) ![]const u8 {
48-
if (self.url) |*u| return u.get_port(page);
47+
pub fn get_port(self: *Location) []const u8 {
48+
if (self.url) |*u| return u.get_port();
4949
return "";
5050
}
5151

@@ -59,8 +59,8 @@ pub const Location = struct {
5959
return "";
6060
}
6161

62-
pub fn get_hash(self: *Location, page: *Page) ![]const u8 {
63-
if (self.url) |*u| return u.get_hash(page);
62+
pub fn get_hash(self: *Location) []const u8 {
63+
if (self.url) |*u| return u.get_hash();
6464
return "";
6565
}
6666

@@ -82,7 +82,7 @@ pub const Location = struct {
8282
}
8383

8484
pub fn _toString(self: *Location, page: *Page) ![]const u8 {
85-
return try self.get_href(page);
85+
return self.get_href(page);
8686
}
8787
};
8888

0 commit comments

Comments
 (0)