Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 2 additions & 27 deletions src/browser/polyfill/webcomponents.zig
Original file line number Diff line number Diff line change
Expand Up @@ -37,31 +37,6 @@ pub const pre =
;

const testing = @import("../../testing.zig");
test "Browser.webcomponents" {
var runner = try testing.jsRunner(testing.tracking_allocator, .{ .html = "<div id=main></div>" });
defer runner.deinit();

try @import("polyfill.zig").preload(testing.allocator, runner.page.main_context);

try runner.testCases(&.{
.{
\\ class LightPanda extends HTMLElement {
\\ constructor() {
\\ super();
\\ }
\\ connectedCallback() {
\\ this.append('connected');
\\ }
\\ }
\\ window.customElements.define("lightpanda-test", LightPanda);
\\ const main = document.getElementById('main');
\\ main.appendChild(document.createElement('lightpanda-test'));
,
null,
},

.{ "main.innerHTML", "<lightpanda-test>connected</lightpanda-test>" },
.{ "document.createElement('lightpanda-test').dataset", "[object DataSet]" },
.{ "document.createElement('lightpanda-test.x').dataset", "[object DataSet]" },
}, .{});
test "Browser: Polyfill.WebComponents" {
try testing.htmlRunner("polyfill/webcomponents.html");
}
36 changes: 13 additions & 23 deletions src/browser/storage/storage.zig
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub const Bottle = struct {
return @intCast(self.map.count());
}

pub fn _key(self: *Bottle, idx: u32) ?[]const u8 {
pub fn _key(self: *const Bottle, idx: u32) ?[]const u8 {
if (idx >= self.map.count()) return null;

var it = self.map.valueIterator();
Expand All @@ -142,7 +142,7 @@ pub const Bottle = struct {
unreachable;
}

pub fn _getItem(self: *Bottle, k: []const u8) ?[]const u8 {
pub fn _getItem(self: *const Bottle, k: []const u8) ?[]const u8 {
return self.map.get(k);
}

Expand Down Expand Up @@ -202,35 +202,25 @@ pub const Bottle = struct {
//
// So for now, we won't impement the feature.
}

pub fn named_get(self: *const Bottle, name: []const u8, _: *bool) ?[]const u8 {
return self._getItem(name);
}

pub fn named_set(self: *Bottle, name: []const u8, value: []const u8, _: *bool) !void {
try self._setItem(name, value);
}
};

// Tests
// -----

const testing = @import("../../testing.zig");
test "Browser.Storage.LocalStorage" {
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
defer runner.deinit();

try runner.testCases(&.{
.{ "localStorage.length", "0" },

.{ "localStorage.setItem('foo', 'bar')", "undefined" },
.{ "localStorage.length", "1" },
.{ "localStorage.getItem('foo')", "bar" },
.{ "localStorage.removeItem('foo')", "undefined" },
.{ "localStorage.length", "0" },

// .{ "localStorage['foo'] = 'bar'", "undefined" },
// .{ "localStorage['foo']", "bar" },
// .{ "localStorage.length", "1" },

.{ "localStorage.clear()", "undefined" },
.{ "localStorage.length", "0" },
}, .{});
test "Browser: Storage.LocalStorage" {
try testing.htmlRunner("storage/local_storage.html");
}

test "storage bottle" {
test "Browser: Storage.Bottle" {
var bottle = Bottle.init(std.testing.allocator);
defer bottle.deinit();

Expand Down
161 changes: 4 additions & 157 deletions src/browser/url/url.zig
Original file line number Diff line number Diff line change
Expand Up @@ -501,163 +501,10 @@ const ValueIterable = iterator.Iterable(kv.ValueIterator, "URLSearchParamsValueI
const EntryIterable = iterator.Iterable(kv.EntryIterator, "URLSearchParamsEntryIterator");

const testing = @import("../../testing.zig");
test "Browser.URL" {
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
defer runner.deinit();

try runner.testCases(&.{
.{ "var url = new URL('https://foo.bar/path?query#fragment')", "undefined" },
.{ "url.origin", "https://foo.bar" },
.{ "url.href", "https://foo.bar/path?query#fragment" },
.{ "url.protocol", "https:" },
.{ "url.username", "" },
.{ "url.password", "" },
.{ "url.host", "foo.bar" },
.{ "url.hostname", "foo.bar" },
.{ "url.port", "" },
.{ "url.pathname", "/path" },
.{ "url.search", "?query" },
.{ "url.hash", "#fragment" },
.{ "url.searchParams.get('query')", "" },

.{ "url.search = 'hello=world'", null },
.{ "url.searchParams.size", "1" },
.{ "url.searchParams.get('hello')", "world" },

.{ "url.search = '?over=9000'", null },
.{ "url.searchParams.size", "1" },
.{ "url.searchParams.get('over')", "9000" },

.{ "url.search = ''", null },
.{ "url.searchParams.size", "0" },

.{ " const url2 = new URL(url);", null },
.{ "url2.href", "https://foo.bar/path#fragment" },

.{ " try { new URL(document.createElement('a')); } catch (e) { e }", "TypeError: invalid argument" },

.{ " let a = document.createElement('a');", null },
.{ " a.href = 'https://www.lightpanda.io/over?9000=!!';", null },
.{ " const url3 = new URL(a);", null },
.{ "url3.href", "https://www.lightpanda.io/over?9000=%21%21" },
}, .{});

try runner.testCases(&.{
.{ "var url = new URL('https://foo.bar/path?a=~&b=%7E#fragment')", "undefined" },
.{ "url.searchParams.get('a')", "~" },
.{ "url.searchParams.get('b')", "~" },
.{ "url.searchParams.append('c', 'foo')", "undefined" },
.{ "url.searchParams.get('c')", "foo" },
.{ "url.searchParams.getAll('c').length", "1" },
.{ "url.searchParams.getAll('c')[0]", "foo" },
.{ "url.searchParams.size", "3" },

// search is dynamic
.{ "url.search", "?a=~&b=~&c=foo" },
// href is dynamic
.{ "url.href", "https://foo.bar/path?a=~&b=~&c=foo#fragment" },

.{ "url.searchParams.delete('c', 'foo')", "undefined" },
.{ "url.searchParams.get('c')", "null" },
.{ "url.searchParams.delete('a')", "undefined" },
.{ "url.searchParams.get('a')", "null" },
}, .{});

try runner.testCases(&.{
.{ "var url = new URL('over?9000', 'https://lightpanda.io')", null },
.{ "url.href", "https://lightpanda.io/over?9000" },
}, .{});

try runner.testCases(&.{
.{ "let sk = new URL('sveltekit-internal://')", null },
.{ "sk.protocol", "sveltekit-internal:" },
.{ "sk.host", "" },
.{ "sk.hostname", "" },
.{ "sk.href", "sveltekit-internal://" },
}, .{});
test "Browser: URL" {
try testing.htmlRunner("url/url.html");
}

test "Browser.URLSearchParams" {
var runner = try testing.jsRunner(testing.tracking_allocator, .{});
defer runner.deinit();
try runner.testCases(&.{
.{ "let usp = new URLSearchParams()", null },
.{ "usp.get('a')", "null" },
.{ "usp.has('a')", "false" },
.{ "usp.getAll('a')", "" },
.{ "usp.delete('a')", "undefined" },

.{ "usp.set('a', 1)", "undefined" },
.{ "usp.has('a')", "true" },
.{ "usp.get('a')", "1" },
.{ "usp.getAll('a')", "1" },

.{ "usp.append('a', 2)", "undefined" },
.{ "usp.has('a')", "true" },
.{ "usp.get('a')", "1" },
.{ "usp.getAll('a')", "1,2" },

.{ "usp.append('b', '3')", "undefined" },
.{ "usp.has('a')", "true" },
.{ "usp.get('a')", "1" },
.{ "usp.getAll('a')", "1,2" },
.{ "usp.has('b')", "true" },
.{ "usp.get('b')", "3" },
.{ "usp.getAll('b')", "3" },

.{ "let acc = [];", null },
.{ "for (const key of usp.keys()) { acc.push(key) }; acc;", "a,a,b" },

.{ "acc = [];", null },
.{ "for (const value of usp.values()) { acc.push(value) }; acc;", "1,2,3" },

.{ "acc = [];", null },
.{ "for (const entry of usp.entries()) { acc.push(entry) }; acc;", "a,1,a,2,b,3" },

.{ "acc = [];", null },
.{ "for (const entry of usp) { acc.push(entry) }; acc;", "a,1,a,2,b,3" },

.{ "usp.delete('a')", "undefined" },
.{ "usp.has('a')", "false" },
.{ "usp.has('b')", "true" },

.{ "acc = [];", null },
.{ "for (const key of usp.keys()) { acc.push(key) }; acc;", "b" },

.{ "acc = [];", null },
.{ "for (const value of usp.values()) { acc.push(value) }; acc;", "3" },

.{ "acc = [];", null },
.{ "for (const entry of usp.entries()) { acc.push(entry) }; acc;", "b,3" },

.{ "acc = [];", null },
.{ "for (const entry of usp) { acc.push(entry) }; acc;", "b,3" },
}, .{});

try runner.testCases(&.{
.{ "usp = new URLSearchParams('?hello')", null },
.{ "usp.get('hello')", "" },

.{ "usp = new URLSearchParams('?abc=')", null },
.{ "usp.get('abc')", "" },

.{ "usp = new URLSearchParams('?abc=123&')", null },
.{ "usp.get('abc')", "123" },
.{ "usp.size", "1" },

.{ "var fd = new FormData()", null },
.{ "fd.append('a', '1')", null },
.{ "fd.append('a', '2')", null },
.{ "fd.append('b', '3')", null },
.{ "ups = new URLSearchParams(fd)", null },
.{ "ups.size", "3" },
.{ "ups.getAll('a')", "1,2" },
.{ "ups.getAll('b')", "3" },
.{ "fd.delete('a')", null }, // the two aren't linked, it created a copy
.{ "ups.size", "3" },
.{ "ups = new URLSearchParams({over: 9000, spice: 'flow'})", null },
.{ "ups.size", "2" },
.{ "ups.getAll('over')", "9000" },
.{ "ups.getAll('spice')", "flow" },
}, .{});
test "Browser: URLSearchParams" {
try testing.htmlRunner("url/url_search_params.html");
}
22 changes: 22 additions & 0 deletions src/tests/polyfill/webcomponents.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<script src="../testing.js"></script>

<div id=main></div>

<script id=webcomponents>
class LightPanda extends HTMLElement {
constructor() {
super();
}
connectedCallback() {
this.append('connected');
}
}

window.customElements.define("lightpanda-test", LightPanda);
const main = document.getElementById('main');
main.appendChild(document.createElement('lightpanda-test'));

testing.expectEqual('<lightpanda-test>connected</lightpanda-test>', main.innerHTML)
testing.expectEqual('[object DataSet]', document.createElement('lightpanda-test').dataset.toString());
testing.expectEqual('[object DataSet]', document.createElement('lightpanda-test.x').dataset.toString());
</script>
28 changes: 28 additions & 0 deletions src/tests/storage/local_storage.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<script src="../testing.js"></script>

<script id=localstorage>
testing.expectEqual(0, localStorage.length);
testing.expectEqual(null, localStorage.getItem('foo'));
testing.expectEqual(null, localStorage.key(0));

localStorage.setItem('foo', 'bar');
testing.expectEqual(1, localStorage.length)
testing.expectEqual('bar', localStorage.getItem('foo'));
testing.expectEqual('bar', localStorage.key(0));
testing.expectEqual(null, localStorage.key(1));

localStorage.removeItem('foo');
testing.expectEqual(0, localStorage.length)
testing.expectEqual(null, localStorage.getItem('foo'));

localStorage['foo'] = 'bar';
testing.expectEqual(1, localStorage.length);
testing.expectEqual('bar', localStorage['foo']);

localStorage.setItem('a', '1');
localStorage.setItem('b', '2');
localStorage.setItem('c', '3');
testing.expectEqual(4, localStorage.length)
localStorage.clear();
testing.expectEqual(0, localStorage.length)
</script>
77 changes: 77 additions & 0 deletions src/tests/url/url.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
<script src="../testing.js"></script>
<script id=url>
var url = new URL('https://foo.bar/path?query#fragment');
testing.expectEqual("https://foo.bar", url.origin);
testing.expectEqual("https://foo.bar/path?query#fragment", url.href);
testing.expectEqual("https:", url.protocol);
testing.expectEqual("", url.username);
testing.expectEqual("", url.password);
testing.expectEqual("foo.bar", url.host);
testing.expectEqual("foo.bar", url.hostname);
testing.expectEqual("", url.port);
testing.expectEqual("/path", url.pathname);
testing.expectEqual("?query", url.search);
testing.expectEqual("#fragment", url.hash);
testing.expectEqual("", url.searchParams.get('query'));

url.search = 'hello=world';
testing.expectEqual(1, url.searchParams.size);
testing.expectEqual("world", url.searchParams.get('hello'));

url.search = '?over=9000';
testing.expectEqual(1, url.searchParams.size);
testing.expectEqual("9000", url.searchParams.get('over'));

url.search = '';
testing.expectEqual(0, url.searchParams.size);

const url2 = new URL(url);
testing.expectEqual("https://foo.bar/path#fragment", url2.href);
</script>

<script id="constructor">
testing.expectError("TypeError: invalid argument", () => {
new URL(document.createElement('a'));
});

let a = document.createElement('a');
a.href = 'https://www.lightpanda.io/over?9000=!!';
const url3 = new URL(a);
testing.expectEqual("https://www.lightpanda.io/over?9000=%21%21", url3.href);
</script>

<script id=searchParams>
url = new URL('https://foo.bar/path?a=~&b=%7E#fragment');
testing.expectEqual("~", url.searchParams.get('a'));
testing.expectEqual("~", url.searchParams.get('b'));

url.searchParams.append('c', 'foo');
testing.expectEqual("foo", url.searchParams.get('c'));
testing.expectEqual(1, url.searchParams.getAll('c').length);
testing.expectEqual("foo", url.searchParams.getAll('c')[0]);
testing.expectEqual(3, url.searchParams.size);

// search is dynamic
testing.expectEqual("?a=~&b=~&c=foo", url.search);

// href is dynamic
testing.expectEqual("https://foo.bar/path?a=~&b=~&c=foo#fragment", url.href);

url.searchParams.delete('c', 'foo');
testing.expectEqual(null, url.searchParams.get('c'));
url.searchParams.delete('a');
testing.expectEqual(null, url.searchParams.get('a'));
</script>

<script id=base>
url = new URL('over?9000', 'https://lightpanda.io');
testing.expectEqual("https://lightpanda.io/over?9000", url.href);
</script>

<script id="svelkit">
let sk = new URL('sveltekit-internal://');
testing.expectEqual("sveltekit-internal:", sk.protocol);
testing.expectEqual("", sk.host);
testing.expectEqual("", sk.hostname);
testing.expectEqual("sveltekit-internal://", sk.href);
</script>
Loading
Loading