Skip to content

Commit a482d59

Browse files
authored
Merge pull request #739 from lightpanda-io/url_constructor
Fix url constructor
2 parents 12bc540 + 2ef7ea6 commit a482d59

File tree

3 files changed

+34
-8
lines changed

3 files changed

+34
-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

@@ -454,7 +454,7 @@ pub const Page = struct {
454454

455455
// if a base path is given, we resolve src using base.
456456
if (base) |_base| {
457-
res_src = try URL.stitch(arena, src, _base);
457+
res_src = try URL.stitch(arena, src, _base, .{ .alloc = .if_needed });
458458
}
459459

460460
var origin_url = &self.url;

src/browser/url/url.zig

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,12 @@ 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+
} else {
57+
raw = try arena.dupe(u8, url);
58+
}
5459

5560
const uri = std.Uri.parse(raw) catch return error.TypeError;
5661
return init(arena, uri);
@@ -276,4 +281,9 @@ test "Browser.URL" {
276281
.{ "url.searchParams.delete('a')", "undefined" },
277282
.{ "url.searchParams.get('a')", "" },
278283
}, .{});
284+
285+
try runner.testCases(&.{
286+
.{ "var url = new URL('over?9000', 'https://lightpanda.io')", null },
287+
.{ "url.href", "https://lightpanda.io/over?9000" },
288+
}, .{});
279289
}

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 = .always,
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)