From 98906be0f6499a6b288c446905a7ea2cb0f97dd4 Mon Sep 17 00:00:00 2001 From: nikneym Date: Fri, 26 Sep 2025 15:36:41 +0300 Subject: [PATCH 1/2] parseData: remove iterator variant --- src/browser/netsurf.zig | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/src/browser/netsurf.zig b/src/browser/netsurf.zig index 5cf3ba13..ffa81006 100644 --- a/src/browser/netsurf.zig +++ b/src/browser/netsurf.zig @@ -2500,31 +2500,22 @@ fn parseParams(enc: ?[:0]const u8) c.dom_hubbub_parser_params { } fn parseData(parser: *c.dom_hubbub_parser, reader: anytype) !void { - var err: c.hubbub_error = undefined; - const TI = @typeInfo(@TypeOf(reader)); - if (TI == .pointer and @hasDecl(TI.pointer.child, "next")) { - while (try reader.next()) |data| { - err = c.dom_hubbub_parser_parse_chunk(parser, data.ptr, data.len); - try parserErr(err); - } - } else { - var buffer: [1024]u8 = undefined; - var ln = buffer.len; - while (ln > 0) { - ln = try reader.read(&buffer); - err = c.dom_hubbub_parser_parse_chunk(parser, &buffer, ln); - // TODO handle encoding change error return. - // When the HTML contains a META tag with a different encoding than the - // original one, a c.DOM_HUBBUB_HUBBUB_ERR_ENCODINGCHANGE error is - // returned. - // In this case, we must restart the parsing with the new detected - // encoding. The detected encoding is stored in the document and we can - // get it with documentGetInputEncoding(). - try parserErr(err); - } + var buffer: [1024]u8 = undefined; + var ln = buffer.len; + while (ln > 0) { + ln = try reader.read(&buffer); + const err = c.dom_hubbub_parser_parse_chunk(parser, &buffer, ln); + // TODO handle encoding change error return. + // When the HTML contains a META tag with a different encoding than the + // original one, a c.DOM_HUBBUB_HUBBUB_ERR_ENCODINGCHANGE error is + // returned. + // In this case, we must restart the parsing with the new detected + // encoding. The detected encoding is stored in the document and we can + // get it with documentGetInputEncoding(). + try parserErr(err); } - err = c.dom_hubbub_parser_completed(parser); - try parserErr(err); + const err = c.dom_hubbub_parser_completed(parser); + return parserErr(err); } // documentHTMLClose closes the document. From b9024ab0325608be1d60656a87755d8361dc4cf8 Mon Sep 17 00:00:00 2001 From: nikneym Date: Fri, 26 Sep 2025 15:37:49 +0300 Subject: [PATCH 2/2] set_innerHTML: simpler iteration --- src/browser/dom/element.zig | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-) diff --git a/src/browser/dom/element.zig b/src/browser/dom/element.zig index 90317d9e..1d8f786b 100644 --- a/src/browser/dom/element.zig +++ b/src/browser/dom/element.zig @@ -192,11 +192,9 @@ pub const Element = struct { // a new fragment const clean = try parser.documentCreateDocumentFragment(doc); const children = try parser.nodeGetChildNodes(body); - const ln = parser.nodeListLength(children); - for (0..ln) |_| { - // always index 0, because nodeAppendChild moves the node out of - // the nodeList and into the new tree - const child = parser.nodeListItem(children, 0) orelse continue; + // always index 0, because nodeAppendChild moves the node out of + // the nodeList and into the new tree + while (parser.nodeListItem(children, 0)) |child| { _ = try parser.nodeAppendChild(@ptrCast(@alignCast(clean)), child); } @@ -210,22 +208,18 @@ pub const Element = struct { { // First, copy some of the head element const children = try parser.nodeGetChildNodes(head); - const ln = parser.nodeListLength(children); - for (0..ln) |_| { - // always index 0, because nodeAppendChild moves the node out of - // the nodeList and into the new tree - const child = parser.nodeListItem(children, 0) orelse continue; + // always index 0, because nodeAppendChild moves the node out of + // the nodeList and into the new tree + while (parser.nodeListItem(children, 0)) |child| { _ = try parser.nodeAppendChild(node, child); } } { const children = try parser.nodeGetChildNodes(body); - const ln = parser.nodeListLength(children); - for (0..ln) |_| { - // always index 0, because nodeAppendChild moves the node out of - // the nodeList and into the new tree - const child = parser.nodeListItem(children, 0) orelse continue; + // always index 0, because nodeAppendChild moves the node out of + // the nodeList and into the new tree + while (parser.nodeListItem(children, 0)) |child| { _ = try parser.nodeAppendChild(node, child); } }