@@ -56,10 +56,18 @@ pub const URL = struct {
5656 // the caller must free the returned string.
5757 // TODO return a disposable string
5858 // https://github.com/lightpanda-io/jsruntime-lib/issues/195
59- pub fn get_href (self : URL , alloc : std.mem.Allocator ) ! []const u8 {
59+ pub fn get_href (self : * URL , alloc : std.mem.Allocator ) ! []const u8 {
6060 var buf = std .ArrayList (u8 ).init (alloc );
6161 defer buf .deinit ();
6262
63+ // retrieve the query search from search_params.
64+ const cur = self .uri .query ;
65+ defer self .uri .query = cur ;
66+ var q = std .ArrayList (u8 ).init (alloc );
67+ defer q .deinit ();
68+ try self .search_params .values .encode (q .writer ());
69+ self .uri .query = q .items ;
70+
6371 try self .uri .writeToStream (.{
6472 .scheme = true ,
6573 .authentication = true ,
@@ -116,9 +124,14 @@ pub const URL = struct {
116124 // TODO return a disposable string
117125 // https://github.com/lightpanda-io/jsruntime-lib/issues/195
118126 pub fn get_search (self : * URL , alloc : std.mem.Allocator ) ! []const u8 {
119- if (self .uri .query == null ) return try alloc .dupe (u8 , "" );
127+ if (self .search_params .get_size () == 0 ) return try alloc .dupe (u8 , "" );
128+
129+ var buf : std .ArrayListUnmanaged (u8 ) = .{};
130+ defer buf .deinit (alloc );
120131
121- return try std .mem .concat (alloc , u8 , &[_ ][]const u8 { "?" , self .uri .query .? });
132+ try buf .append (alloc , '?' );
133+ try self .search_params .values .encode (buf .writer (alloc ));
134+ return buf .toOwnedSlice (alloc );
122135 }
123136
124137 // the caller must free the returned string.
@@ -207,12 +220,18 @@ pub fn testExecFn(
207220 try checkCases (js_env , & url );
208221
209222 var qs = [_ ]Case {
210- .{ .src = "var url = new URL('https://foo.bar/path?a=~&b=%7E')" , .ex = "undefined" },
223+ .{ .src = "var url = new URL('https://foo.bar/path?a=~&b=%7E#fragment ')" , .ex = "undefined" },
211224 .{ .src = "url.searchParams.get('a')" , .ex = "~" },
212225 .{ .src = "url.searchParams.get('b')" , .ex = "~" },
213226 .{ .src = "url.searchParams.append('c', 'foo')" , .ex = "undefined" },
214227 .{ .src = "url.searchParams.get('c')" , .ex = "foo" },
215228 .{ .src = "url.searchParams.size" , .ex = "3" },
229+
230+ // search is dynamic
231+ .{ .src = "url.search" , .ex = "?a=%7E&b=%7E&c=foo" },
232+ // href is dynamic
233+ .{ .src = "url.href" , .ex = "https://foo.bar/path?a=%7E&b=%7E&c=foo#fragment" },
234+
216235 .{ .src = "url.searchParams.delete('c', 'foo')" , .ex = "undefined" },
217236 .{ .src = "url.searchParams.get('c')" , .ex = "" },
218237 .{ .src = "url.searchParams.delete('a')" , .ex = "undefined" },
0 commit comments