Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,7 @@ fn addDependencies(b: *Build, mod: *Build.Module, opts: *Build.Step.Options) !vo
try buildMbedtls(b, mod);
try buildNghttp2(b, mod);
try buildCurl(b, mod);
try buildAda(b, mod);

switch (target.result.os.tag) {
.macos => {
Expand Down Expand Up @@ -823,3 +824,27 @@ fn buildCurl(b: *Build, m: *Build.Module) !void {
},
});
}

pub fn buildAda(b: *Build, m: *Build.Module) !void {
const ada_dep = b.dependency("ada-singleheader", .{});
const dep_root = ada_dep.path("");

// Private module that binds ada functions.
const ada_mod = b.createModule(.{
.root_source_file = b.path("vendor/ada/root.zig"),
.target = m.resolved_target,
.optimize = m.optimize,
.link_libcpp = true,
});

// Expose headers; note that "ada.h" is a C++ header so no use here.
ada_mod.addIncludePath(dep_root);

ada_mod.addCSourceFiles(.{
.root = dep_root,
.files = &.{"ada.cpp"},
.flags = &.{"-std=c++20"},
});

m.addImport("ada", ada_mod);
}
4 changes: 4 additions & 0 deletions build.zig.zon
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
.hash = "v8-0.0.0-xddH6_PFAwAqwLMWbF7S0rAZzRbN8zmf2GhLH_ThdMSR",
},
//.v8 = .{ .path = "../zig-v8-fork" }
.@"ada-singleheader" = .{
.url = "https://github.com/ada-url/ada/releases/download/v3.3.0/singleheader.zip",
.hash = "N-V-__8AAPmhFAAw64ALjlzd5YMtzpSrmZ6KymsT84BKfB4s",
},
},
}
4 changes: 2 additions & 2 deletions src/browser/html/document.zig
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ pub const HTMLDocument = struct {
// JS funcs
// --------

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

pub fn set_domain(_: *parser.DocumentHTML, _: []const u8) ![]const u8 {
Expand Down
101 changes: 39 additions & 62 deletions src/browser/html/elements.zig
Original file line number Diff line number Diff line change
Expand Up @@ -275,54 +275,53 @@ pub const HTMLAnchorElement = struct {
// TODO return a disposable string
pub fn get_origin(self: *parser.Anchor, page: *Page) ![]const u8 {
var u = try url(self, page);
return try u.get_origin(page);
return u.get_origin(page);
}

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

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

u.uri.scheme = v;
const href = try u.toString(arena);
u.set_protocol(v);
const href = try u.get_href(page);
try parser.anchorSetHref(self, href);
}

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

pub fn set_host(self: *parser.Anchor, v: []const u8, page: *Page) !void {
// search : separator
var p: ?u16 = null;
var p: ?[]const u8 = null;
var h: []const u8 = undefined;
for (v, 0..) |c, i| {
if (c == ':') {
h = v[0..i];
p = try std.fmt.parseInt(u16, v[i + 1 ..], 10);
//p = try std.fmt.parseInt(u16, v[i + 1 ..], 10);
p = v[i + 1 ..];
break;
}
}

const arena = page.arena;
var u = try url(self, page);

if (p) |pp| {
u.uri.host = .{ .raw = h };
u.uri.port = pp;
if (p) |port| {
u.set_host(h);
u.set_port(port);
} else {
u.uri.host = .{ .raw = v };
u.uri.port = null;
u.set_host(v);
}

const href = try u.toString(arena);
const href = try u.get_href(page);
try parser.anchorSetHref(self, href);
}

Expand All @@ -332,30 +331,26 @@ pub const HTMLAnchorElement = struct {
}

pub fn set_hostname(self: *parser.Anchor, v: []const u8, page: *Page) !void {
const arena = page.arena;
var u = try url(self, page);
u.uri.host = .{ .raw = v };
const href = try u.toString(arena);
u.set_host(v);
const href = try u.get_href(page);
try parser.anchorSetHref(self, href);
}

// TODO return a disposable string
pub fn get_port(self: *parser.Anchor, page: *Page) ![]const u8 {
var u = try url(self, page);
return try u.get_port(page);
return u.get_port();
}

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

if (v != null and v.?.len > 0) {
u.uri.port = try std.fmt.parseInt(u16, v.?, 10);
} else {
u.uri.port = null;
u.set_host(v.?);
}

const href = try u.toString(arena);
const href = try u.get_href(page);
try parser.anchorSetHref(self, href);
}

Expand All @@ -366,37 +361,28 @@ pub const HTMLAnchorElement = struct {
}

pub fn set_username(self: *parser.Anchor, v: ?[]const u8, page: *Page) !void {
const arena = page.arena;
var u = try url(self, page);
if (v) |username| {
var u = try url(self, page);
u.set_username(username);

if (v) |vv| {
u.uri.user = .{ .raw = vv };
} else {
u.uri.user = null;
const href = try u.get_href(page);
try parser.anchorSetHref(self, href);
}
const href = try u.toString(arena);

try parser.anchorSetHref(self, href);
}

// TODO return a disposable string
pub fn get_password(self: *parser.Anchor, page: *Page) ![]const u8 {
var u = try url(self, page);
return try page.arena.dupe(u8, u.get_password());
return u.get_password();
}

pub fn set_password(self: *parser.Anchor, v: ?[]const u8, page: *Page) !void {
const arena = page.arena;
var u = try url(self, page);
if (v) |password| {
var u = try url(self, page);
u.set_password(password);

if (v) |vv| {
u.uri.password = .{ .raw = vv };
} else {
u.uri.password = null;
const href = try u.get_href(page);
try parser.anchorSetHref(self, href);
}
const href = try u.toString(arena);

try parser.anchorSetHref(self, href);
}

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

pub fn set_pathname(self: *parser.Anchor, v: []const u8, page: *Page) !void {
const arena = page.arena;
var u = try url(self, page);
u.uri.path = .{ .raw = v };
const href = try u.toString(arena);

u.set_pathname(v);
const href = try u.get_href(page);
try parser.anchorSetHref(self, href);
}

pub fn get_search(self: *parser.Anchor, page: *Page) ![]const u8 {
var u = try url(self, page);
return try u.get_search(page);
return u.get_search(page);
}

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

const href = try u.toString(page.call_arena);
const href = try u.get_href(page);
try parser.anchorSetHref(self, href);
}

// TODO return a disposable string
pub fn get_hash(self: *parser.Anchor, page: *Page) ![]const u8 {
var u = try url(self, page);
return try u.get_hash(page);
return u.get_hash();
}

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

if (v) |vv| {
u.uri.fragment = .{ .raw = vv };
} else {
u.uri.fragment = null;
}
const href = try u.toString(arena);

u.set_hash(v);
const href = try u.get_href(page);
try parser.anchorSetHref(self, href);
}
};
Expand Down
18 changes: 9 additions & 9 deletions src/browser/html/location.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,13 @@ pub const Location = struct {
return "";
}

pub fn get_protocol(self: *Location, page: *Page) ![]const u8 {
if (self.url) |*u| return u.get_protocol(page);
pub fn get_protocol(self: *Location) []const u8 {
if (self.url) |*u| return u.get_protocol();
return "";
}

pub fn get_host(self: *Location, page: *Page) ![]const u8 {
if (self.url) |*u| return u.get_host(page);
pub fn get_host(self: *Location) []const u8 {
if (self.url) |*u| return u.get_host();
return "";
}

Expand All @@ -44,8 +44,8 @@ pub const Location = struct {
return "";
}

pub fn get_port(self: *Location, page: *Page) ![]const u8 {
if (self.url) |*u| return u.get_port(page);
pub fn get_port(self: *Location) []const u8 {
if (self.url) |*u| return u.get_port();
return "";
}

Expand All @@ -59,8 +59,8 @@ pub const Location = struct {
return "";
}

pub fn get_hash(self: *Location, page: *Page) ![]const u8 {
if (self.url) |*u| return u.get_hash(page);
pub fn get_hash(self: *Location) []const u8 {
if (self.url) |*u| return u.get_hash();
return "";
}

Expand All @@ -82,7 +82,7 @@ pub const Location = struct {
}

pub fn _toString(self: *Location, page: *Page) ![]const u8 {
return try self.get_href(page);
return self.get_href(page);
}
};

Expand Down
Loading