Skip to content
Merged
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
12 changes: 12 additions & 0 deletions src/browser/url/url.zig
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ pub const URL = struct {
return ada.clearHash(self.internal);
}

/// Returns a boolean indicating whether or not an absolute URL,
/// or a relative URL combined with a base URL, are parsable and valid.
pub fn static_canParse(url: ConstructorArg, maybe_base: ?ConstructorArg, page: *Page) !bool {
const url_str = try url.toString(page);

if (maybe_base) |base| {
return ada.canParseWithBase(url_str, try base.toString(page));
}

return ada.canParse(url_str);
}

/// Alias to get_href.
pub fn _toString(self: *const URL, page: *Page) ![]const u8 {
return self.get_href(page);
Expand Down
8 changes: 8 additions & 0 deletions src/tests/url/url.html
Original file line number Diff line number Diff line change
Expand Up @@ -99,3 +99,11 @@
_ = new URL("://foo.bar/path?query#fragment");
});
</script>

<script id=URL.canParse>
testing.expectEqual(true, URL.canParse("https://lightpanda.io"));
testing.expectEqual(false, URL.canParse("://lightpanda.io"));

testing.expectEqual(true, URL.canParse("/home", "https://lightpanda.io"));
testing.expectEqual(false, URL.canParse("lightpanda.io", "https"));
</script>
8 changes: 8 additions & 0 deletions vendor/ada/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,14 @@ pub fn parseWithBase(input: []const u8, base: []const u8) ParseError!URL {
return url;
}

pub inline fn canParse(input: []const u8) bool {
return c.ada_can_parse(input.ptr, input.len);
}

pub inline fn canParseWithBase(input: []const u8, base: []const u8) bool {
return c.ada_can_parse_with_base(input.ptr, input.len, base.ptr, base.len);
}

pub inline fn getComponents(url: URL) *const URLComponents {
return c.ada_get_components(url);
}
Expand Down