Skip to content

Commit ac75f9b

Browse files
committed
Fix url constructor
url, base were being joined in the wrong order. Switch to using URL.stitch if a base is given.
1 parent c80deeb commit ac75f9b

File tree

3 files changed

+36
-8
lines changed

3 files changed

+36
-8
lines changed

src/browser/page.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub const Page = struct {
145145

146146
const file_src = blk: {
147147
if (base) |_base| {
148-
break :blk try URL.stitch(self.arena, specifier, _base);
148+
break :blk try URL.stitch(self.arena, specifier, _base, .{});
149149
} else break :blk specifier;
150150
};
151151

@@ -451,7 +451,7 @@ pub const Page = struct {
451451

452452
// if a base path is given, we resolve src using base.
453453
if (base) |_base| {
454-
res_src = try URL.stitch(arena, src, _base);
454+
res_src = try URL.stitch(arena, src, _base, .{});
455455
}
456456

457457
var origin_url = &self.url;

src/browser/url/url.zig

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,14 @@ pub const URL = struct {
5050
page: *Page,
5151
) !URL {
5252
const arena = page.arena;
53-
const raw = try std.mem.concat(arena, u8, &[_][]const u8{ url, base orelse "" });
53+
var raw: []const u8 = undefined;
54+
if (base) |b| {
55+
raw = try @import("../../url.zig").URL.stitch(arena, url, b, .{
56+
.alloc = .always,
57+
});
58+
} else {
59+
raw = try arena.dupe(u8, url);
60+
}
5461

5562
const uri = std.Uri.parse(raw) catch return error.TypeError;
5663
return init(arena, uri);
@@ -276,4 +283,9 @@ test "Browser.URL" {
276283
.{ "url.searchParams.delete('a')", "undefined" },
277284
.{ "url.searchParams.get('a')", "" },
278285
}, .{});
286+
287+
try runner.testCases(&.{
288+
.{ "var url = new URL('over?9000', 'https://lightpanda.io')", null },
289+
.{ "url.href", "https://lightpanda.io/over?9000" },
290+
}, .{});
279291
}

src/url.zig

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -83,12 +83,28 @@ pub const URL = struct {
8383
return WebApiURL.init(allocator, self.uri);
8484
}
8585

86+
const StitchOpts = struct {
87+
alloc: AllocWhen = .if_needed,
88+
89+
const AllocWhen = enum {
90+
always,
91+
if_needed,
92+
};
93+
};
8694
/// Properly stitches two URL fragments together.
8795
///
8896
/// For URLs with a path, it will replace the last entry with the src.
8997
/// For URLs without a path, it will add src as the path.
90-
pub fn stitch(allocator: Allocator, src: []const u8, base: []const u8) ![]const u8 {
98+
pub fn stitch(
99+
allocator: Allocator,
100+
src: []const u8,
101+
base: []const u8,
102+
opts: StitchOpts,
103+
) ![]const u8 {
91104
if (base.len == 0) {
105+
if (opts.alloc == .always) {
106+
return allocator.dupe(u8, src);
107+
}
92108
return src;
93109
}
94110

@@ -161,7 +177,7 @@ test "URL: Stitching Base & Src URLs (Basic)" {
161177

162178
const base = "https://www.google.com/xyz/abc/123";
163179
const src = "something.js";
164-
const result = try URL.stitch(allocator, src, base);
180+
const result = try URL.stitch(allocator, src, base, .{});
165181
defer allocator.free(result);
166182
try testing.expectString("https://www.google.com/xyz/abc/something.js", result);
167183
}
@@ -171,7 +187,7 @@ test "URL: Stitching Base & Src URLs (Just Ending Slash)" {
171187

172188
const base = "https://www.google.com/";
173189
const src = "something.js";
174-
const result = try URL.stitch(allocator, src, base);
190+
const result = try URL.stitch(allocator, src, base, .{});
175191
defer allocator.free(result);
176192
try testing.expectString("https://www.google.com/something.js", result);
177193
}
@@ -181,7 +197,7 @@ test "URL: Stitching Base & Src URLs (No Ending Slash)" {
181197

182198
const base = "https://www.google.com";
183199
const src = "something.js";
184-
const result = try URL.stitch(allocator, src, base);
200+
const result = try URL.stitch(allocator, src, base, .{});
185201
defer allocator.free(result);
186202
try testing.expectString("https://www.google.com/something.js", result);
187203
}
@@ -191,7 +207,7 @@ test "URL: Stiching Base & Src URLs (Both Local)" {
191207

192208
const base = "./abcdef/123.js";
193209
const src = "something.js";
194-
const result = try URL.stitch(allocator, src, base);
210+
const result = try URL.stitch(allocator, src, base, .{});
195211
defer allocator.free(result);
196212
try testing.expectString("./abcdef/something.js", result);
197213
}

0 commit comments

Comments
 (0)