Skip to content

Commit 7808d12

Browse files
committed
add URL.canParse static method
1 parent 1015fc0 commit 7808d12

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

src/browser/url/url.zig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ pub const URL = struct {
132132
return ada.clearHash(self.internal);
133133
}
134134

135+
/// Returns a boolean indicating whether or not an absolute URL,
136+
/// or a relative URL combined with a base URL, are parsable and valid.
137+
pub fn static_canParse(url: ConstructorArg, maybe_base: ?ConstructorArg, page: *Page) !bool {
138+
const url_str = try url.toString(page);
139+
140+
if (maybe_base) |base| {
141+
return ada.canParseWithBase(url_str, try base.toString(page));
142+
}
143+
144+
return ada.canParse(url_str);
145+
}
146+
135147
/// Alias to get_href.
136148
pub fn _toString(self: *const URL, page: *Page) ![]const u8 {
137149
return self.get_href(page);

vendor/ada/root.zig

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,14 @@ pub fn parseWithBase(input: []const u8, base: []const u8) ParseError!URL {
3333
return url;
3434
}
3535

36+
pub inline fn canParse(input: []const u8) bool {
37+
return c.ada_can_parse(input.ptr, input.len);
38+
}
39+
40+
pub inline fn canParseWithBase(input: []const u8, base: []const u8) bool {
41+
return c.ada_can_parse_with_base(input.ptr, input.len, base.ptr, base.len);
42+
}
43+
3644
pub inline fn getComponents(url: URL) *const URLComponents {
3745
return c.ada_get_components(url);
3846
}

0 commit comments

Comments
 (0)