Skip to content

Commit b35107a

Browse files
committed
URL stitch avoid double /
1 parent 1090ff0 commit b35107a

File tree

2 files changed

+16
-6
lines changed

2 files changed

+16
-6
lines changed

src/browser/url/url.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ pub const URL = struct {
219219
}
220220

221221
pub fn _toJSON(self: *URL, page: *Page) ![]const u8 {
222-
return try self.get_href(page);
222+
return self.get_href(page);
223223
}
224224
};
225225

src/url.zig

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -125,16 +125,16 @@ pub const URL = struct {
125125
}
126126
};
127127

128+
const normalized_src = if (src[0] == '/') src[1..] else src;
129+
128130
if (std.mem.lastIndexOfScalar(u8, base[protocol_end..], '/')) |index| {
129131
const last_slash_pos = index + protocol_end;
130132
if (last_slash_pos == base.len - 1) {
131-
return std.fmt.allocPrint(allocator, "{s}{s}", .{ base, src });
132-
} else {
133-
return std.fmt.allocPrint(allocator, "{s}/{s}", .{ base[0..last_slash_pos], src });
133+
return std.fmt.allocPrint(allocator, "{s}{s}", .{ base, normalized_src });
134134
}
135-
} else {
136-
return std.fmt.allocPrint(allocator, "{s}/{s}", .{ base, src });
135+
return std.fmt.allocPrint(allocator, "{s}/{s}", .{ base[0..last_slash_pos], normalized_src });
137136
}
137+
return std.fmt.allocPrint(allocator, "{s}/{s}", .{ base, normalized_src });
138138
}
139139

140140
pub fn concatQueryString(arena: Allocator, url: []const u8, query_string: []const u8) ![]const u8 {
@@ -233,6 +233,16 @@ test "URL: Stitching Base & Src URLs (Just Ending Slash)" {
233233
try testing.expectString("https://www.google.com/something.js", result);
234234
}
235235

236+
test "URL: Stitching Base & Src URLs with leading slash" {
237+
const allocator = testing.allocator;
238+
239+
const base = "https://www.google.com/";
240+
const src = "/something.js";
241+
const result = try URL.stitch(allocator, src, base, .{});
242+
defer allocator.free(result);
243+
try testing.expectString("https://www.google.com/something.js", result);
244+
}
245+
236246
test "URL: Stitching Base & Src URLs (No Ending Slash)" {
237247
const allocator = testing.allocator;
238248

0 commit comments

Comments
 (0)