mirror of
https://github.com/lightpanda-io/browser.git
synced 2025-10-28 22:53:28 +00:00
URL stitch avoid double /
This commit is contained in:
@@ -219,7 +219,7 @@ pub const URL = struct {
|
||||
}
|
||||
|
||||
pub fn _toJSON(self: *URL, page: *Page) ![]const u8 {
|
||||
return try self.get_href(page);
|
||||
return self.get_href(page);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
20
src/url.zig
20
src/url.zig
@@ -125,16 +125,16 @@ pub const URL = struct {
|
||||
}
|
||||
};
|
||||
|
||||
const normalized_src = if (src[0] == '/') src[1..] else src;
|
||||
|
||||
if (std.mem.lastIndexOfScalar(u8, base[protocol_end..], '/')) |index| {
|
||||
const last_slash_pos = index + protocol_end;
|
||||
if (last_slash_pos == base.len - 1) {
|
||||
return std.fmt.allocPrint(allocator, "{s}{s}", .{ base, src });
|
||||
} else {
|
||||
return std.fmt.allocPrint(allocator, "{s}/{s}", .{ base[0..last_slash_pos], src });
|
||||
return std.fmt.allocPrint(allocator, "{s}{s}", .{ base, normalized_src });
|
||||
}
|
||||
} else {
|
||||
return std.fmt.allocPrint(allocator, "{s}/{s}", .{ base, src });
|
||||
return std.fmt.allocPrint(allocator, "{s}/{s}", .{ base[0..last_slash_pos], normalized_src });
|
||||
}
|
||||
return std.fmt.allocPrint(allocator, "{s}/{s}", .{ base, normalized_src });
|
||||
}
|
||||
|
||||
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)" {
|
||||
try testing.expectString("https://www.google.com/something.js", result);
|
||||
}
|
||||
|
||||
test "URL: Stitching Base & Src URLs with leading slash" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
const base = "https://www.google.com/";
|
||||
const src = "/something.js";
|
||||
const result = try URL.stitch(allocator, src, base, .{});
|
||||
defer allocator.free(result);
|
||||
try testing.expectString("https://www.google.com/something.js", result);
|
||||
}
|
||||
|
||||
test "URL: Stitching Base & Src URLs (No Ending Slash)" {
|
||||
const allocator = testing.allocator;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user