Lightpanda: the headless browser designed for AI and automation
Move localtime to datetime module
Karl Seguinab357b354ea4
diff --git a/src/browser/webapi/Document.zig b/src/browser/webapi/Document.zigindex 4e803a4c1..cc050d7ac 100644--- a/src/browser/webapi/Document.zig+++ b/src/browser/webapi/Document.zig@@ -183,25 +183,23 @@ pub fn getCompatMode(self: *const Document) []const u8 {// document.lastModified: the response's Last-Modified header in local time,// "MM/DD/YYYY hh:mm:ss", defaulting to the current time.pub fn getLastModified(self: *const Document, frame: *Frame) ![]const u8 {- var timestamp: i64 = std.time.timestamp();- if (self._frame) |doc_frame| {- if (doc_frame.document == self) {- for (doc_frame._http_headers.items) |header| {+ const dt = @import("../../datetime.zig");++ const timestamp = blk: {+ if (self._frame) |owner| {+ for (owner._http_headers.items) |header| {if (std.ascii.eqlIgnoreCase(header.name, "last-modified")) {- const DateTime = @import("../../datetime.zig").DateTime;- if (DateTime.parse(header.value, .rfc822)) |dt| {- timestamp = dt.unix(.seconds);+ if (dt.DateTime.parse(header.value, .rfc822)) |parsed| {+ break :blk parsed.unix(.seconds);} else |_| {}break;}}}- }+ break :blk std.time.timestamp();+ };- var tm: LibcTm = undefined;- if (localtime_r(×tamp, &tm) == null) {- return error.InvalidArgument;- }+ const tm = try dt.localTime(timestamp);return std.fmt.allocPrint(frame.local_arena, "{d:0>2}/{d:0>2}/{d} {d:0>2}:{d:0>2}:{d:0>2}", .{@as(u32, @intCast(tm.tm_mon + 1)),@as(u32, @intCast(tm.tm_mday)),@@ -212,21 +210,6 @@ pub fn getLastModified(self: *const Document, frame: *Frame) ![]const u8 {});}-const LibcTm = extern struct {- tm_sec: c_int,- tm_min: c_int,- tm_hour: c_int,- tm_mday: c_int,- tm_mon: c_int,- tm_year: c_int,- tm_wday: c_int,- tm_yday: c_int,- tm_isdst: c_int,- tm_gmtoff: c_long,- tm_zone: ?[*:0]const u8,-};-extern "c" fn localtime_r(timep: *const i64, result: *LibcTm) ?*LibcTm;-pub fn getCharset(self: *const Document) []const u8 {if (self._charset) |charset| {return charset;diff --git a/src/browser/webapi/HTMLDocument.zig b/src/browser/webapi/HTMLDocument.zigindex 0fdeeedc7..be47ff982 100644--- a/src/browser/webapi/HTMLDocument.zig+++ b/src/browser/webapi/HTMLDocument.zig@@ -49,8 +49,8 @@ pub fn asEventTarget(self: *HTMLDocument) *@import("EventTarget.zig") {// "blah:head" is an HTMLUnknownElement but still qualifies).pub fn getHead(self: *HTMLDocument) ?*Element {const doc_el = self._proto.getDocumentElement() orelse return null;- var child = doc_el.asNode().firstChild();- while (child) |node| : (child = node.nextSibling()) {+ var it = doc_el.asNode().childrenIterator();+ while (it.next()) |node| {const el = node.is(Element) orelse continue;if (el._namespace == .html and std.mem.eql(u8, el.getLocalName(), "head")) {return el;diff --git a/src/browser/webapi/element/Attribute.zig b/src/browser/webapi/element/Attribute.zigindex cd74b52b7..983c6aa5f 100644--- a/src/browser/webapi/element/Attribute.zig+++ b/src/browser/webapi/element/Attribute.zig@@ -529,15 +529,7 @@ fn normalizeNameForLookup(name: String, frame: *Frame) !String {return .wrap(normalized);}-pub fn normalizeNameForLookupAlloc(allocator: Allocator, name: String) !String {- if (!needsLowerCasing(name.str())) {- return name.dupe(allocator);- }- const normalized = try std.ascii.allocLowerString(allocator, name.str());- return .wrap(normalized);-}--fn needsLowerCasing(name: []const u8) bool {+pub fn needsLowerCasing(name: []const u8) bool {var remaining = name;if (comptime std.simd.suggestVectorLength(u8)) |vector_len| {while (remaining.len > vector_len) {@@ -557,6 +549,11 @@ fn needsLowerCasing(name: []const u8) bool {return false;}+pub fn normalizeNameForLookupAlloc(allocator: Allocator, name: String) !String {+ const normalized = try std.ascii.allocLowerString(allocator, name.str());+ return .wrap(normalized);+}+pub const NamedNodeMap = struct {// Whenever the NamedNodeMap creates an Attribute, it needs to provide the// "ownerElement". The attribute list is the element's.diff --git a/src/browser/webapi/selector/Parser.zig b/src/browser/webapi/selector/Parser.zigindex b66ea8c71..79e7abb7a 100644--- a/src/browser/webapi/selector/Parser.zig+++ b/src/browser/webapi/selector/Parser.zig@@ -17,6 +17,7 @@// along with this program. If not, see <https://www.gnu.org/licenses/>.const std = @import("std");+const lp = @import("lightpanda");const Node = @import("../Node.zig");const Attribute = @import("../element/Attribute.zig");@@ -159,7 +160,7 @@ pub fn parseList(arena: Allocator, input: []const u8) ParseError![]const Selectoreturn selectors.items;}-pub fn parse(arena: Allocator, input: []const u8) ParseError!Selector.Selector {+fn parse(arena: Allocator, input: []const u8) ParseError!Selector.Selector {var parser = Parser{ .input = input };var segments: std.ArrayList(Segment) = .empty;var current_compound: std.ArrayList(Part) = .empty;@@ -961,12 +962,16 @@ fn attribute(self: *Parser, arena: Allocator) !Selector.Attribute {self.input = self.input[1..];}+ // Kept as-written in original_name for the case-sensitive match on+ // foreign elements; like the rest of the AST, it borrows the input.const attr_name = try self.attributeName(arena);- // As written, for the case-sensitive match on foreign elements.- const original_name = try arena.dupe(u8, attr_name);// Normalize the name to lowercase for fast matching (consistent with Attribute.normalizeNameForLookup)- const name = try Attribute.normalizeNameForLookupAlloc(arena, .wrap(attr_name));+ const name = if (Attribute.needsLowerCasing(attr_name))+ try Attribute.normalizeNameForLookupAlloc(arena, .wrap(attr_name))+ else+ lp.String.wrap(attr_name);+var case_insensitive = false;_ = self.skipSpaces();@@ -976,7 +981,12 @@ fn attribute(self: *Parser, arena: Allocator) !Selector.Attribute {if (self.peek() == ']') {self.input = self.input[1..];}- return .{ .name = name, .original_name = .wrap(original_name), .matcher = .presence, .case_insensitive = case_insensitive };+ return .{+ .name = name,+ .matcher = .presence,+ .original_name = .wrap(attr_name),+ .case_insensitive = case_insensitive,+ };}const matcher_type = try self.attributeMatcher();@@ -1014,7 +1024,12 @@ fn attribute(self: *Parser, arena: Allocator) !Selector.Attribute {.presence => unreachable,};- return .{ .name = name, .original_name = .wrap(original_name), .matcher = matcher, .case_insensitive = case_insensitive };+ return .{+ .name = name,+ .matcher = matcher,+ .original_name = .wrap(attr_name),+ .case_insensitive = case_insensitive,+ };}fn attributeName(self: *Parser, arena: Allocator) ![]const u8 {diff --git a/src/datetime.zig b/src/datetime.zigindex 49332cdd1..6b0757756 100644--- a/src/datetime.zig+++ b/src/datetime.zig@@ -621,6 +621,29 @@ fn paddingTwoDigits(value: usize) [2]u8 {return digits[value * 2 ..][0..2].*;}+pub fn localTime(ts: i64) !LibcTm {+ var tm: LibcTm = undefined;+ if (localtime_r(&ts, &tm) == null) {+ return error.InvalidArgument;+ }+ return tm;+}++const LibcTm = extern struct {+ tm_sec: c_int,+ tm_min: c_int,+ tm_hour: c_int,+ tm_mday: c_int,+ tm_mon: c_int,+ tm_year: c_int,+ tm_wday: c_int,+ tm_yday: c_int,+ tm_isdst: c_int,+ tm_gmtoff: c_long,+ tm_zone: ?[*:0]const u8,+};+extern "c" fn localtime_r(timep: *const i64, result: *LibcTm) ?*LibcTm;+const Parser = struct {input: []const u8,pos: usize,