Skip to content

Commit cdd3135

Browse files
committed
get fetch campire working
1 parent b047cb6 commit cdd3135

File tree

15 files changed

+142
-46
lines changed

15 files changed

+142
-46
lines changed

src/browser/dump.zig

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,14 @@ const std = @import("std");
22
const Node = @import("webapi/Node.zig");
33

44
pub const Opts = struct {
5+
// @ZIGDOM (none of these do anything)
6+
with_base: bool = false,
57
strip_mode: StripMode = .{},
68

7-
const StripMode = struct {
8-
// @ZIGDOM
9+
pub const StripMode = struct {
10+
js: bool = false,
11+
ui: bool = false,
12+
css: bool = false,
913
};
1014
};
1115

src/browser/js/Context.zig

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,6 @@ pub fn throw(self: *Context, err: []const u8) js.Exception {
387387
pub fn zigValueToJs(self: *Context, value: anytype, comptime opts: Caller.CallOpts) !v8.Value {
388388
const isolate = self.isolate;
389389

390-
391390
// Check if it's a "simple" type. This is extracted so that it can be
392391
// reused by other parts of the code. "simple" types only require an
393392
// isolate to create (specifically, they don't our templates array)
@@ -595,7 +594,6 @@ pub fn mapZigInstanceToJs(self: *Context, js_obj_: ?v8.Object, value: anytype) !
595594
};
596595
const JsApi = bridge.Struct(ptr.child).JsApi;
597596

598-
599597
// The TAO contains the pointer to our Zig instance as
600598
// well as any meta data we'll need to use it later.
601599
// See the TaggedAnyOpaque struct for more details.

src/browser/js/Env.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,6 @@ fn generateConstructor(comptime JsApi: type, isolate: v8.Isolate) v8.FunctionTem
311311
return template;
312312
}
313313

314-
315314
// ZIGDOM (HTMLAllCollection I think)
316315
// fn generateUndetectable(comptime Struct: type, template: v8.ObjectTemplate) void {
317316
// const has_js_call_as_function = @hasDecl(Struct, "jsCallAsFunction");

src/browser/js/bridge.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -401,7 +401,6 @@ pub const SubType = enum {
401401
webassemblymemory,
402402
};
403403

404-
405404
pub const JsApis = flattenTypes(&.{
406405
@import("../webapi/AbortController.zig"),
407406
@import("../webapi/AbortSignal.zig"),

src/browser/js/js.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ pub const PersistentPromiseResolver = struct {
106106

107107
pub fn resolve(self: PersistentPromiseResolver, value: anytype) !void {
108108
const context = self.context;
109-
const js_value = try context.zigValueToJs(value);
109+
const js_value = try context.zigValueToJs(value, .{});
110110

111111
// resolver.resolve will return null if the promise isn't pending
112112
const ok = self.resolver.castToPromiseResolver().resolve(context.v8_context, js_value) orelse return;
@@ -117,7 +117,7 @@ pub const PersistentPromiseResolver = struct {
117117

118118
pub fn reject(self: PersistentPromiseResolver, value: anytype) !void {
119119
const context = self.context;
120-
const js_value = try context.zigValueToJs(value);
120+
const js_value = try context.zigValueToJs(value, .{});
121121

122122
// resolver.reject will return null if the promise isn't pending
123123
const ok = self.resolver.castToPromiseResolver().reject(context.v8_context, js_value) orelse return;

src/browser/page.zig

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ _parse_mode: enum { document, fragment },
5858
// even thoug we'll create very few (if any) actual *Attributes.
5959
_attribute_lookup: std.AutoHashMapUnmanaged(usize, *Element.Attribute),
6060

61+
// Same as _atlribute_lookup, but instead of individual attributes, this is for
62+
// the return of elements.attributes.
63+
_attribute_named_node_map_lookup: std.AutoHashMapUnmanaged(usize, *Element.Attribute.NamedNodeMap),
64+
6165
_script_manager: ScriptManager,
6266

6367
_polyfill_loader: polyfill.Loader = .{},
@@ -119,6 +123,7 @@ pub fn deinit(self: *Page) void {
119123
log.debug(.page, "page.deinit", .{ .url = self.url });
120124
}
121125
self.js.deinit();
126+
self._script_manager.deinit();
122127
}
123128

124129
fn reset(self: *Page, comptime initializing: bool) !void {
@@ -144,6 +149,7 @@ fn reset(self: *Page, comptime initializing: bool) !void {
144149
self._parse_state = .pre;
145150
self._load_state = .parsing;
146151
self._attribute_lookup = .empty;
152+
self._attribute_named_node_map_lookup = .empty;
147153
self._event_manager = EventManager.init(self);
148154

149155
self._script_manager = ScriptManager.init(self);
@@ -165,15 +171,15 @@ fn registerBackgroundTasks(self: *Page) !void {
165171
const Browser = @import("Browser.zig");
166172

167173
try self.scheduler.add(self._session.browser, struct {
168-
fn runMicrotasks(ctx: *anyopaque) ?u32 {
174+
fn runMicrotasks(ctx: *anyopaque) !?u32 {
169175
const b: *Browser = @ptrCast(@alignCast(ctx));
170176
b.runMicrotasks();
171177
return 5;
172178
}
173179
}.runMicrotasks, 5, .{ .name = "page.microtasks" });
174180

175181
try self.scheduler.add(self._session.browser, struct {
176-
fn runMessageLoop(ctx: *anyopaque) ?u32 {
182+
fn runMessageLoop(ctx: *anyopaque) !?u32 {
177183
const b: *Browser = @ptrCast(@alignCast(ctx));
178184
b.runMessageLoop();
179185
return 100;
@@ -992,7 +998,7 @@ fn populateElementAttributes(self: *Page, element: *Element, list: anytype) !voi
992998
if (@TypeOf(list) == ?*Element.Attribute.List) {
993999
// from cloneNode
9941000

995-
var existing = list orelse return ;
1001+
var existing = list orelse return;
9961002

9971003
var attributes = try self.arena.create(Element.Attribute.List);
9981004
attributes.* = .{};

src/browser/tests/document/query_selector.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ <h6>Heading 6</h6>
5757
const firstScript = document.querySelector('script');
5858
testing.expectEqual('SCRIPT', firstScript.tagName);
5959

60-
testing.expectEqual(null, document.querySelector('select'));
60+
testing.expectEqual(null, document.querySelector('article'));
6161
testing.expectEqual(null, document.querySelector('another'));
6262
}
6363
</script>

src/browser/webapi/Document.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ pub fn createTreeWalker(_: *const Document, root: *Node, what_to_show: ?u32, fil
170170
return DOMTreeWalker.init(root, show, filter, page);
171171
}
172172

173-
// @ZIGDOM what_to_show tristate (null vs undefined vs value)
173+
// @ZIGDOM what_to_show tristate (null vs undefined vs value)
174174
pub fn createNodeIterator(_: *const Document, root: *Node, what_to_show: ?u32, filter: ?DOMNodeIterator.FilterOpts, page: *Page) !*DOMNodeIterator {
175175
const show = what_to_show orelse NodeFilter.SHOW_ALL;
176176
return DOMNodeIterator.init(root, show, filter, page);

src/browser/webapi/Element.zig

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ pub fn getTagNameLower(self: *const Element) []const u8 {
118118
.script => "script",
119119
.select => "select",
120120
.style => "style",
121-
.text_area => "textara",
121+
.text_area => "textarea",
122122
.title => "title",
123123
.ul => "ul",
124124
.unknown => |e| e._tag_name.str(),
@@ -311,9 +311,14 @@ pub fn getAttributeNames(self: *const Element, page: *Page) ![][]const u8 {
311311
return attributes.getNames(page);
312312
}
313313

314-
pub fn getAttributeNamedNodeMap(self: *Element) Attribute.NamedNodeMap {
315-
const attributes = self._attributes orelse return .{};
316-
return .{ ._list = attributes.*, ._element = self };
314+
pub fn getAttributeNamedNodeMap(self: *Element, page: *Page) !*Attribute.NamedNodeMap {
315+
const gop = try page._attribute_named_node_map_lookup.getOrPut(page.arena, @intFromPtr(self));
316+
if (!gop.found_existing) {
317+
const attributes = try self.getOrCreateAttributeList(page);
318+
const named_node_map = try page._factory.create(Attribute.NamedNodeMap{ ._list = attributes, ._element = self });
319+
gop.value_ptr.* = named_node_map;
320+
}
321+
return gop.value_ptr.*;
317322
}
318323

319324
pub fn getStyle(self: *Element, page: *Page) !*CSSStyleProperties {

src/browser/webapi/element/Attribute.zig

Lines changed: 38 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -326,7 +326,7 @@ fn needsLowerCasing(name: []const u8) bool {
326326
}
327327

328328
pub const NamedNodeMap = struct {
329-
_list: List = .{},
329+
_list: *List,
330330

331331
// Whenever the NamedNodeMap creates an Attribute, it needs to provide the
332332
// "ownerElement".
@@ -418,6 +418,12 @@ pub const InnerIterator = struct {
418418

419419
fn formatAttribute(name: []const u8, value: []const u8, writer: *std.Io.Writer) !void {
420420
try writer.writeAll(name);
421+
422+
// Boolean attributes with empty values are serialized without a value
423+
if (value.len == 0 and boolean_attributes_lookup.has(name)) {
424+
return;
425+
}
426+
421427
try writer.writeByte('=');
422428
if (value.len == 0) {
423429
return writer.writeAll("\"\"");
@@ -433,6 +439,37 @@ fn formatAttribute(name: []const u8, value: []const u8, writer: *std.Io.Writer)
433439
return writer.writeByte('"');
434440
}
435441

442+
const boolean_attributes = [_][]const u8{
443+
"checked",
444+
"disabled",
445+
"required",
446+
"readonly",
447+
"multiple",
448+
"selected",
449+
"autofocus",
450+
"autoplay",
451+
"controls",
452+
"loop",
453+
"muted",
454+
"hidden",
455+
"async",
456+
"defer",
457+
"novalidate",
458+
"formnovalidate",
459+
"ismap",
460+
"reversed",
461+
"default",
462+
"open",
463+
};
464+
465+
const boolean_attributes_lookup = std.StaticStringMap(void).initComptime(blk: {
466+
var entries: [boolean_attributes.len]struct { []const u8, void } = undefined;
467+
for (boolean_attributes, 0..) |attr, i| {
468+
entries[i] = .{ attr, {} };
469+
}
470+
break :blk entries;
471+
});
472+
436473
fn writeEscapedAttributeValue(value: []const u8, first_offset: usize, writer: *std.Io.Writer) !void {
437474
// Write everything before the first special character
438475
try writer.writeAll(value[0..first_offset]);

0 commit comments

Comments
 (0)