xhr: return DOM document instead of HTML document

This commit is contained in:
Pierre Tachoire
2024-02-13 09:07:15 +01:00
parent 54a807bb36
commit d58fbe07e3

View File

@@ -66,7 +66,7 @@ pub const XMLHttpRequest = struct {
Text: []const u8, Text: []const u8,
ArrayBuffer: void, ArrayBuffer: void,
Blob: void, Blob: void,
Document: *parser.DocumentHTML, Document: *parser.Document,
JSON: JSONValue, JSON: JSONValue,
}; };
@@ -76,13 +76,16 @@ pub const XMLHttpRequest = struct {
JSON, JSON,
}; };
const ResponseObj = union(ResponseObjTag) { const ResponseObj = union(ResponseObjTag) {
Document: *parser.DocumentHTML, Document: *parser.Document,
Failure: bool, Failure: bool,
JSON: std.json.Parsed(JSONValue), JSON: std.json.Parsed(JSONValue),
fn deinit(self: ResponseObj) void { fn deinit(self: ResponseObj) void {
return switch (self) { return switch (self) {
.Document => |d| parser.documentHTMLClose(d) catch {}, .Document => |d| {
const doc = @as(*parser.DocumentHTML, @ptrCast(d));
parser.documentHTMLClose(doc) catch {};
},
.JSON => |p| p.deinit(), .JSON => |p| p.deinit(),
.Failure => {}, .Failure => {},
}; };
@@ -589,7 +592,9 @@ pub const XMLHttpRequest = struct {
// TODO Set documents URL to xhrs responses URL. // TODO Set documents URL to xhrs responses URL.
// TODO Set documents origin to xhrs relevant settings objects origin. // TODO Set documents origin to xhrs relevant settings objects origin.
self.response_obj = .{ .Document = doc }; self.response_obj = .{
.Document = parser.documentHTMLToDocument(doc),
};
} }
// setResponseObjJSON parses the received bytes as a std.json.Value. // setResponseObjJSON parses the received bytes as a std.json.Value.
@@ -694,7 +699,7 @@ pub fn testExecFn(
.{ .src = "req.getAllResponseHeaders().length > 64", .ex = "true" }, .{ .src = "req.getAllResponseHeaders().length > 64", .ex = "true" },
.{ .src = "req.responseText.length > 64", .ex = "true" }, .{ .src = "req.responseText.length > 64", .ex = "true" },
.{ .src = "req.response", .ex = "" }, .{ .src = "req.response", .ex = "" },
.{ .src = "req.responseXML instanceof HTMLDocument", .ex = "true" }, .{ .src = "req.responseXML instanceof Document", .ex = "true" },
}; };
try checkCases(js_env, &send); try checkCases(js_env, &send);
@@ -709,8 +714,8 @@ pub fn testExecFn(
// So the url has been retrieved. // So the url has been retrieved.
.{ .src = "req2.status", .ex = "200" }, .{ .src = "req2.status", .ex = "200" },
.{ .src = "req2.statusText", .ex = "OK" }, .{ .src = "req2.statusText", .ex = "OK" },
.{ .src = "req2.response instanceof HTMLDocument", .ex = "true" }, .{ .src = "req2.response instanceof Document", .ex = "true" },
.{ .src = "req2.responseXML instanceof HTMLDocument", .ex = "true" }, .{ .src = "req2.responseXML instanceof Document", .ex = "true" },
}; };
try checkCases(js_env, &document); try checkCases(js_env, &document);