Skip to content

Commit 3ea8d0b

Browse files
Merge pull request #824 from lightpanda-io/dom-non-html
create a DOM tree for non-html files
2 parents c52d33e + 7896d27 commit 3ea8d0b

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

src/browser/mime.zig

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ pub const Mime = struct {
3535
text_html,
3636
text_javascript,
3737
text_plain,
38+
text_css,
39+
application_json,
3840
unknown,
3941
other,
4042
};
@@ -44,6 +46,8 @@ pub const Mime = struct {
4446
text_html: void,
4547
text_javascript: void,
4648
text_plain: void,
49+
text_css: void,
50+
application_json: void,
4751
unknown: void,
4852
other: struct { type: []const u8, sub_type: []const u8 },
4953
};
@@ -174,18 +178,22 @@ pub const Mime = struct {
174178
if (std.meta.stringToEnum(enum {
175179
@"text/xml",
176180
@"text/html",
181+
@"text/css",
182+
@"text/plain",
177183

178184
@"text/javascript",
179185
@"application/javascript",
180186
@"application/x-javascript",
181187

182-
@"text/plain",
188+
@"application/json",
183189
}, type_name)) |known_type| {
184190
const ct: ContentType = switch (known_type) {
185191
.@"text/xml" => .{ .text_xml = {} },
186192
.@"text/html" => .{ .text_html = {} },
187193
.@"text/javascript", .@"application/javascript", .@"application/x-javascript" => .{ .text_javascript = {} },
188194
.@"text/plain" => .{ .text_plain = {} },
195+
.@"text/css" => .{ .text_css = {} },
196+
.@"application/json" => .{ .application_json = {} },
189197
};
190198
return .{ ct, attribute_start };
191199
}
@@ -351,6 +359,9 @@ test "Mime: parse common" {
351359
try expect(.{ .content_type = .{ .text_javascript = {} } }, "text/javascript");
352360
try expect(.{ .content_type = .{ .text_javascript = {} } }, "Application/JavaScript");
353361
try expect(.{ .content_type = .{ .text_javascript = {} } }, "application/x-javascript");
362+
363+
try expect(.{ .content_type = .{ .application_json = {} } }, "application/json");
364+
try expect(.{ .content_type = .{ .text_css = {} } }, "text/css");
354365
}
355366

356367
test "Mime: parse uncommon" {

src/browser/page.zig

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -238,17 +238,33 @@ pub const Page = struct {
238238
.reason = opts.reason,
239239
});
240240

241-
if (!mime.isHTML()) {
241+
if (mime.isHTML()) {
242+
// the page is an HTML, load it as it.
243+
try self.loadHTMLDoc(&response, mime.charset orelse "utf-8");
244+
} else {
245+
// the page isn't an HTML
242246
var arr: std.ArrayListUnmanaged(u8) = .{};
243247
while (try response.next()) |data| {
244248
try arr.appendSlice(arena, try arena.dupe(u8, data));
245249
}
246250
// save the body into the page.
247251
self.raw_data = arr.items;
248-
return;
249-
}
250252

251-
try self.loadHTMLDoc(&response, mime.charset orelse "utf-8");
253+
// construct a pseudo HTML containing the response body.
254+
var buf: std.ArrayListUnmanaged(u8) = .{};
255+
256+
switch (mime.content_type) {
257+
.application_json, .text_plain, .text_javascript, .text_css => {
258+
try buf.appendSlice(arena, "<html><head><meta charset=\"utf-8\"></head><body><pre>");
259+
try buf.appendSlice(arena, self.raw_data.?);
260+
try buf.appendSlice(arena, "</pre></body></html>\n");
261+
},
262+
// In other cases, we prefer to not integrate the content into the HTML document page iself.
263+
else => {},
264+
}
265+
var fbs = std.io.fixedBufferStream(buf.items);
266+
try self.loadHTMLDoc(fbs.reader(), mime.charset orelse "utf-8");
267+
}
252268
}
253269

254270
try self.processHTMLDoc();

0 commit comments

Comments
 (0)