Skip to content

Commit 9138a3c

Browse files
committed
MediaQueryLis dummy
1 parent 8b3f36c commit 9138a3c

File tree

10 files changed

+97
-42
lines changed

10 files changed

+97
-42
lines changed

src/browser/Browser.zig

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,3 @@ pub fn runMessageLoop(self: *const Browser) void {
108108
}
109109
self.env.runIdleTasks();
110110
}
111-
112-
const testing = @import("../testing.zig");
113-
test "Browser" {
114-
try testing.htmlRunner("browser.html", .{});
115-
}

src/browser/EventManager.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ pub fn dispatch(self: *EventManager, target: *EventTarget, event: *Event) !void
9292
event._target = target;
9393
switch (target._type) {
9494
.node => |node| try self.dispatchNode(node, event),
95-
.xhr, .window, .abort_signal => {
95+
.xhr, .window, .abort_signal, .media_query_list => {
9696
const list = self.lookup.getPtr(@intFromPtr(target)) orelse return;
9797
try self.dispatchAll(list, target, event);
9898
},

src/browser/js/bridge.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -442,6 +442,7 @@ pub const JsApis = flattenTypes(&.{
442442
@import("../webapi/Crypto.zig"),
443443
@import("../webapi/css/CSSStyleDeclaration.zig"),
444444
@import("../webapi/css/CSSStyleProperties.zig"),
445+
@import("../webapi/css/MediaQueryList.zig"),
445446
@import("../webapi/Document.zig"),
446447
@import("../webapi/HTMLDocument.zig"),
447448
@import("../webapi/KeyValueList.zig"),
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<!DOCTYPE html>
2+
<head>
3+
<script src="../testing.js"></script>
4+
</head>
5+
6+
<body>
7+
</body>
8+
9+
<script id=matchMedia_basic>
10+
{
11+
const mql = window.matchMedia('(min-width: 600px)');
12+
testing.expectEqual('object', typeof mql);
13+
testing.expectEqual('(min-width: 600px)', mql.media);
14+
testing.expectEqual(false, mql.matches);
15+
}
16+
</script>
17+
18+
<script id=matchMedia_different_queries>
19+
{
20+
const mql1 = window.matchMedia('(max-width: 1024px)');
21+
testing.expectEqual('(max-width: 1024px)', mql1.media);
22+
testing.expectEqual(false, mql1.matches);
23+
24+
const mql2 = window.matchMedia('(prefers-color-scheme: dark)');
25+
testing.expectEqual('(prefers-color-scheme: dark)', mql2.media);
26+
testing.expectEqual(false, mql2.matches);
27+
}
28+
</script>
29+
30+
<script id=matchMedia_event_target>
31+
{
32+
const mql = window.matchMedia('(orientation: portrait)');
33+
testing.expectEqual('function', typeof mql.addEventListener);
34+
testing.expectEqual('function', typeof mql.removeEventListener);
35+
testing.expectEqual('function', typeof mql.dispatchEvent);
36+
}
37+
</script>

src/browser/tests/page/meta.html

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,6 @@
3737
testing.expectEqual('string', typeof plainDoc.URL);
3838
testing.expectEqual('string', typeof document.readyState);
3939
testing.expectEqual('string', typeof plainDoc.readyState);
40+
41+
testing.expectEqual("[object Intl.DateTimeFormat]", new Intl.DateTimeFormat().toString());
4042
</script>

src/browser/webapi/EventTarget.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ pub const Type = union(enum) {
1515
window: *@import("Window.zig"),
1616
xhr: *@import("net/XMLHttpRequestEventTarget.zig"),
1717
abort_signal: *@import("AbortSignal.zig"),
18+
media_query_list: *@import("css/MediaQueryList.zig"),
1819
};
1920

2021
pub fn dispatchEvent(self: *EventTarget, event: *Event, page: *Page) !bool {
@@ -62,6 +63,7 @@ pub fn format(self: *EventTarget, writer: *std.Io.Writer) !void {
6263
.window => writer.writeAll("<window>"),
6364
.xhr => writer.writeAll("<XMLHttpRequestEventTarget>"),
6465
.abort_signal => writer.writeAll("<abort_signal>"),
66+
.media_query_list => writer.writeAll("<MediaQueryList>"),
6567
};
6668
}
6769

src/browser/webapi/Location.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub const JsApi = struct {
6565
pub const hash = bridge.accessor(Location.getHash, null, .{});
6666
pub const pathname = bridge.accessor(Location.getPathname, null, .{});
6767
pub const hostname = bridge.accessor(Location.getHostname, null, .{});
68-
pub const host = bridge.accessor(URL.getHost, null, .{});
68+
pub const host = bridge.accessor(Location.getHost, null, .{});
6969
pub const port = bridge.accessor(Location.getPort, null, .{});
7070
pub const origin = bridge.accessor(Location.getOrigin, null, .{});
7171
pub const protocol = bridge.accessor(Location.getProtocol, null, .{});

src/browser/webapi/URL.zig

Lines changed: 0 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -130,41 +130,6 @@ pub fn toString(self: *const URL, page: *const Page) ![:0]const u8 {
130130
return buf.written()[0 .. buf.written().len - 1 :0];
131131
}
132132

133-
fn getUserInfo(self: *const URL) ?[]const u8 {
134-
const raw = self._raw;
135-
const scheme_end = std.mem.indexOf(u8, raw, "://") orelse return null;
136-
const authority_start = scheme_end + 3;
137-
138-
const pos = std.mem.indexOfScalar(u8, raw[authority_start..], '@') orelse return null;
139-
const path_start = std.mem.indexOfScalarPos(u8, raw, authority_start, '/') orelse raw.len;
140-
141-
const full_pos = authority_start + pos;
142-
if (full_pos < path_start) {
143-
return raw[authority_start..full_pos];
144-
}
145-
146-
return null;
147-
}
148-
149-
fn getHost(self: *const URL) []const u8 {
150-
const raw = self._raw;
151-
const scheme_end = std.mem.indexOf(u8, raw, "://") orelse return "";
152-
153-
var authority_start = scheme_end + 3;
154-
if (std.mem.indexOf(u8, raw[authority_start..], "@")) |pos| {
155-
authority_start += pos + 1;
156-
}
157-
158-
const authority = raw[authority_start..];
159-
const path_start = std.mem.indexOfAny(u8, authority, "/?#") orelse return authority;
160-
return authority[0..path_start];
161-
}
162-
163-
const KnownProtocol = enum {
164-
@"http:",
165-
@"https:",
166-
};
167-
168133
pub const JsApi = struct {
169134
pub const bridge = js.Bridge(URL);
170135

src/browser/webapi/Window.zig

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const Location = @import("Location.zig");
1111
const Fetch = @import("net/Fetch.zig");
1212
const EventTarget = @import("EventTarget.zig");
1313
const ErrorEvent = @import("event/ErrorEvent.zig");
14+
const MediaQueryList = @import("css/MediaQueryList.zig");
1415
const storage = @import("storage/storage.zig");
1516

1617
const Window = @This();
@@ -135,6 +136,13 @@ pub fn cancelAnimationFrame(self: *Window, id: u32) void {
135136
sc.removed = true;
136137
}
137138

139+
pub fn matchMedia(_: *const Window, query: []const u8, page: *Page) !*MediaQueryList {
140+
return page._factory.eventTarget(MediaQueryList{
141+
._proto = undefined,
142+
._media = try page.dupeString(query),
143+
});
144+
}
145+
138146
pub fn btoa(_: *const Window, input: []const u8, page: *Page) ![]const u8 {
139147
const encoded_len = std.base64.standard.Encoder.calcSize(input.len);
140148
const encoded = try page.call_arena.alloc(u8, encoded_len);
@@ -266,6 +274,7 @@ pub const JsApi = struct {
266274
pub const clearImmediate = bridge.function(Window.clearImmediate, .{});
267275
pub const requestAnimationFrame = bridge.function(Window.requestAnimationFrame, .{});
268276
pub const cancelAnimationFrame = bridge.function(Window.cancelAnimationFrame, .{});
277+
pub const matchMedia = bridge.function(Window.matchMedia, .{});
269278
pub const btoa = bridge.function(Window.btoa, .{});
270279
pub const atob = bridge.function(Window.atob, .{});
271280
};
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// zlint-disable unused-decls
2+
const std = @import("std");
3+
const js = @import("../../js/js.zig");
4+
const EventTarget = @import("../EventTarget.zig");
5+
6+
const MediaQueryList = @This();
7+
8+
_proto: *EventTarget,
9+
_media: []const u8,
10+
11+
pub fn deinit(self: *MediaQueryList) void {
12+
_ = self;
13+
}
14+
15+
pub fn asEventTarget(self: *MediaQueryList) *EventTarget {
16+
return self._proto;
17+
}
18+
19+
pub fn getMedia(self: *const MediaQueryList) []const u8 {
20+
return self._media;
21+
}
22+
23+
/// Always returns false for dummy implementation
24+
pub fn getMatches(_: *const MediaQueryList) bool {
25+
return false;
26+
}
27+
28+
pub const JsApi = struct {
29+
pub const bridge = js.Bridge(MediaQueryList);
30+
31+
pub const Meta = struct {
32+
pub const name = "MediaQueryList";
33+
pub const prototype_chain = bridge.prototypeChain();
34+
pub var class_index: u16 = 0;
35+
};
36+
37+
pub const media = bridge.accessor(MediaQueryList.getMedia, null, .{});
38+
pub const matches = bridge.accessor(MediaQueryList.getMatches, null, .{});
39+
};
40+
41+
const testing = @import("../../../testing.zig");
42+
test "WebApi: MediaQueryList" {
43+
try testing.htmlRunner("css/media_query_list.html", .{});
44+
}

0 commit comments

Comments
 (0)