mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-29 15:13:28 +00:00
url: first draft
This commit is contained in:
@@ -7,6 +7,7 @@ const HTML = @import("html/html.zig");
|
|||||||
const Events = @import("events/event.zig");
|
const Events = @import("events/event.zig");
|
||||||
const XHR = @import("xhr/xhr.zig");
|
const XHR = @import("xhr/xhr.zig");
|
||||||
const Storage = @import("storage/storage.zig");
|
const Storage = @import("storage/storage.zig");
|
||||||
|
const URL = @import("url/url.zig");
|
||||||
|
|
||||||
pub const HTMLDocument = @import("html/document.zig").HTMLDocument;
|
pub const HTMLDocument = @import("html/document.zig").HTMLDocument;
|
||||||
|
|
||||||
@@ -18,4 +19,5 @@ pub const Interfaces = generate.Tuple(.{
|
|||||||
HTML.Interfaces,
|
HTML.Interfaces,
|
||||||
XHR.Interfaces,
|
XHR.Interfaces,
|
||||||
Storage.Interfaces,
|
Storage.Interfaces,
|
||||||
|
URL.Interfaces,
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -10,6 +10,7 @@ const apiweb = @import("apiweb.zig");
|
|||||||
const Window = @import("html/window.zig").Window;
|
const Window = @import("html/window.zig").Window;
|
||||||
const xhr = @import("xhr/xhr.zig");
|
const xhr = @import("xhr/xhr.zig");
|
||||||
const storage = @import("storage/storage.zig");
|
const storage = @import("storage/storage.zig");
|
||||||
|
const url = @import("url/url.zig");
|
||||||
|
|
||||||
const documentTestExecFn = @import("dom/document.zig").testExecFn;
|
const documentTestExecFn = @import("dom/document.zig").testExecFn;
|
||||||
const HTMLDocumentTestExecFn = @import("html/document.zig").testExecFn;
|
const HTMLDocumentTestExecFn = @import("html/document.zig").testExecFn;
|
||||||
@@ -30,6 +31,7 @@ const EventTestExecFn = @import("events/event.zig").testExecFn;
|
|||||||
const XHRTestExecFn = xhr.testExecFn;
|
const XHRTestExecFn = xhr.testExecFn;
|
||||||
const ProgressEventTestExecFn = @import("xhr/progress_event.zig").testExecFn;
|
const ProgressEventTestExecFn = @import("xhr/progress_event.zig").testExecFn;
|
||||||
const StorageTestExecFn = storage.testExecFn;
|
const StorageTestExecFn = storage.testExecFn;
|
||||||
|
const URLTestExecFn = url.testExecFn;
|
||||||
|
|
||||||
pub const Types = jsruntime.reflect(apiweb.Interfaces);
|
pub const Types = jsruntime.reflect(apiweb.Interfaces);
|
||||||
|
|
||||||
@@ -95,6 +97,7 @@ fn testsAllExecFn(
|
|||||||
ProgressEventTestExecFn,
|
ProgressEventTestExecFn,
|
||||||
ProcessingInstructionTestExecFn,
|
ProcessingInstructionTestExecFn,
|
||||||
StorageTestExecFn,
|
StorageTestExecFn,
|
||||||
|
URLTestExecFn,
|
||||||
};
|
};
|
||||||
|
|
||||||
inline for (testFns) |testFn| {
|
inline for (testFns) |testFn| {
|
||||||
|
|||||||
159
src/url/url.zig
Normal file
159
src/url/url.zig
Normal file
@@ -0,0 +1,159 @@
|
|||||||
|
const std = @import("std");
|
||||||
|
|
||||||
|
const jsruntime = @import("jsruntime");
|
||||||
|
const Case = jsruntime.test_utils.Case;
|
||||||
|
const checkCases = jsruntime.test_utils.checkCases;
|
||||||
|
const generate = @import("../generate.zig");
|
||||||
|
|
||||||
|
pub const Interfaces = generate.Tuple(.{
|
||||||
|
URL,
|
||||||
|
URLSearchParams,
|
||||||
|
});
|
||||||
|
|
||||||
|
// https://url.spec.whatwg.org/#url
|
||||||
|
//
|
||||||
|
// TODO we could avoid many of these getter string allocation in two differents
|
||||||
|
// way:
|
||||||
|
//
|
||||||
|
// 1. We can eventually get the slice of scheme *with* the following char in
|
||||||
|
// the underlying string. But I don't know if it's possible and how to do that.
|
||||||
|
// I mean, if the rawuri contains `https://foo.bar`, uri.scheme is a slice
|
||||||
|
// containing only `https`. I want `https:` so, in theory, I don't need to
|
||||||
|
// allocate data, I should be able to retrieve the scheme + the following `:`
|
||||||
|
// from rawuri.
|
||||||
|
//
|
||||||
|
// 2. The other way would bu to copy the `std.Uri` code to ahve a dedicated
|
||||||
|
// parser including the characters we want for the web API.
|
||||||
|
pub const URL = struct {
|
||||||
|
rawuri: []const u8,
|
||||||
|
uri: std.Uri,
|
||||||
|
|
||||||
|
pub const mem_guarantied = true;
|
||||||
|
|
||||||
|
pub fn constructor(alloc: std.mem.Allocator, url: []const u8, base: ?[]const u8) !URL {
|
||||||
|
const raw = try std.mem.concat(alloc, u8, &[_][]const u8{ url, base orelse "" });
|
||||||
|
errdefer alloc.free(raw);
|
||||||
|
|
||||||
|
const uri = std.Uri.parse(raw) catch {
|
||||||
|
return error.TypeError;
|
||||||
|
};
|
||||||
|
|
||||||
|
return .{
|
||||||
|
.rawuri = raw,
|
||||||
|
.uri = uri,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn deinit(self: *URL, alloc: std.mem.Allocator) void {
|
||||||
|
alloc.free(self.rawuri);
|
||||||
|
}
|
||||||
|
|
||||||
|
// the caller must free the returned string.
|
||||||
|
// TODO return a disposable string
|
||||||
|
// https://github.com/lightpanda-io/jsruntime-lib/issues/195
|
||||||
|
pub fn get_href(self: URL, alloc: std.mem.Allocator) ![]const u8 {
|
||||||
|
var buf = std.ArrayList(u8).init(alloc);
|
||||||
|
defer buf.deinit();
|
||||||
|
|
||||||
|
try self.uri.writeToStream(.{
|
||||||
|
.scheme = true,
|
||||||
|
.authentication = true,
|
||||||
|
.authority = true,
|
||||||
|
.path = true,
|
||||||
|
.query = true,
|
||||||
|
.fragment = true,
|
||||||
|
}, buf.writer());
|
||||||
|
return try buf.toOwnedSlice();
|
||||||
|
}
|
||||||
|
|
||||||
|
// the caller must free the returned string.
|
||||||
|
// TODO return a disposable string
|
||||||
|
// https://github.com/lightpanda-io/jsruntime-lib/issues/195
|
||||||
|
pub fn get_protocol(self: *URL, alloc: std.mem.Allocator) ![]const u8 {
|
||||||
|
return try std.mem.concat(alloc, u8, &[_][]const u8{ self.uri.scheme, ":" });
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_username(self: *URL) []const u8 {
|
||||||
|
return self.uri.user orelse "";
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_password(self: *URL) []const u8 {
|
||||||
|
return self.uri.password orelse "";
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_host(self: *URL) []const u8 {
|
||||||
|
return self.uri.host orelse "";
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_hostname(self: *URL) []const u8 {
|
||||||
|
return self.uri.host orelse "";
|
||||||
|
}
|
||||||
|
|
||||||
|
// the caller must free the returned string.
|
||||||
|
// TODO return a disposable string
|
||||||
|
// https://github.com/lightpanda-io/jsruntime-lib/issues/195
|
||||||
|
pub fn get_port(self: *URL, alloc: std.mem.Allocator) ![]const u8 {
|
||||||
|
if (self.uri.port == null) return try alloc.dupe(u8, "");
|
||||||
|
|
||||||
|
var buf = std.ArrayList(u8).init(alloc);
|
||||||
|
defer buf.deinit();
|
||||||
|
|
||||||
|
try std.fmt.formatInt(self.uri.port.?, 10, .lower, .{}, buf.writer());
|
||||||
|
return try buf.toOwnedSlice();
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn get_pathname(self: *URL) []const u8 {
|
||||||
|
if (self.uri.path.len == 0) return "/";
|
||||||
|
return self.uri.path;
|
||||||
|
}
|
||||||
|
|
||||||
|
// the caller must free the returned string.
|
||||||
|
// TODO return a disposable string
|
||||||
|
// https://github.com/lightpanda-io/jsruntime-lib/issues/195
|
||||||
|
pub fn get_search(self: *URL, alloc: std.mem.Allocator) ![]const u8 {
|
||||||
|
if (self.uri.query == null) return try alloc.dupe(u8, "");
|
||||||
|
|
||||||
|
return try std.mem.concat(alloc, u8, &[_][]const u8{ "?", self.uri.query.? });
|
||||||
|
}
|
||||||
|
|
||||||
|
// the caller must free the returned string.
|
||||||
|
// TODO return a disposable string
|
||||||
|
// https://github.com/lightpanda-io/jsruntime-lib/issues/195
|
||||||
|
pub fn get_hash(self: *URL, alloc: std.mem.Allocator) ![]const u8 {
|
||||||
|
if (self.uri.fragment == null) return try alloc.dupe(u8, "");
|
||||||
|
|
||||||
|
return try std.mem.concat(alloc, u8, &[_][]const u8{ "#", self.uri.fragment.? });
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn _toJSON(self: *URL, alloc: std.mem.Allocator) ![]const u8 {
|
||||||
|
return try self.get_href(alloc);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// https://url.spec.whatwg.org/#interface-urlsearchparams
|
||||||
|
pub const URLSearchParams = struct {
|
||||||
|
pub const mem_guarantied = true;
|
||||||
|
};
|
||||||
|
|
||||||
|
// Tests
|
||||||
|
// -----
|
||||||
|
|
||||||
|
pub fn testExecFn(
|
||||||
|
_: std.mem.Allocator,
|
||||||
|
js_env: *jsruntime.Env,
|
||||||
|
) anyerror!void {
|
||||||
|
var url = [_]Case{
|
||||||
|
.{ .src = "var url = new URL('https://foo.bar/path?query#fragment')", .ex = "undefined" },
|
||||||
|
.{ .src = "url.href", .ex = "https://foo.bar/path?query#fragment" },
|
||||||
|
.{ .src = "url.protocol", .ex = "https:" },
|
||||||
|
.{ .src = "url.username", .ex = "" },
|
||||||
|
.{ .src = "url.password", .ex = "" },
|
||||||
|
.{ .src = "url.host", .ex = "foo.bar" },
|
||||||
|
.{ .src = "url.hostname", .ex = "foo.bar" },
|
||||||
|
.{ .src = "url.port", .ex = "" },
|
||||||
|
.{ .src = "url.pathname", .ex = "/path" },
|
||||||
|
.{ .src = "url.search", .ex = "?query" },
|
||||||
|
.{ .src = "url.hash", .ex = "#fragment" },
|
||||||
|
};
|
||||||
|
try checkCases(js_env, &url);
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user