Lightpanda: the headless browser designed for AI and automation
Better support for rem font size
Karl Seguin31dd942a1fea
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zigindex 093088377..d1b9477c2 100644--- a/src/browser/StyleManager.zig+++ b/src/browser/StyleManager.zig@@ -1777,13 +1777,34 @@ fn parseInheritedFlag(value: []const u8, comptime on: []const []const u8, comptireturn null;}+// Absolute-size keywords, in pixels as Chrome maps them.+const font_size_keywords = std.StaticStringMapWithEql(f64, std.static_string_map.eqlAsciiIgnoreCase).initComptime(.{+ .{ "xx-small", 9 },+ .{ "x-small", 10 },+ .{ "small", 13 },+ .{ "medium", 16 },+ .{ "large", 18 },+ .{ "x-large", 24 },+ .{ "xx-large", 32 },+ .{ "xxx-large", 48 },+});+fn parseFontSize(self: *StyleManager, raw: []const u8, parent: ?*Element, depth: u8) ?f64 {const value = std.mem.trim(u8, raw, " \t\r\n\x0c");if (inheritsKeyword(value)) {return self.computedFontSizeAt(parent, depth);}- if (std.ascii.eqlIgnoreCase(value, "initial") or std.ascii.eqlIgnoreCase(value, "medium")) {- return 16;+ if (std.ascii.eqlIgnoreCase(value, "initial")) {+ return DEFAULT_FONT_SIZE;+ }+ if (font_size_keywords.get(value)) |size| {+ return size;+ }+ if (std.ascii.eqlIgnoreCase(value, "smaller")) {+ return self.computedFontSizeAt(parent, depth) / 1.2;+ }+ if (std.ascii.eqlIgnoreCase(value, "larger")) {+ return self.computedFontSizeAt(parent, depth) * 1.2;}const parsed = units.parse(value) catch return null;@@ -1791,6 +1812,8 @@ fn parseFontSize(self: *StyleManager, raw: []const u8, parent: ?*Element, depth:.percentage => self.computedFontSizeAt(parent, depth) / 100.0,.em => self.computedFontSizeAt(parent, depth),.ex => self.computedFontSizeAt(parent, depth) / 2.0,+ // The root's own `rem` resolves against the initial size.+ .rem => if (parent == null) DEFAULT_FONT_SIZE else self.computedFontSizeAt(self.frame.document.getDocumentElement(), depth),else => return null,};const size = parsed.value * factor;diff --git a/src/browser/css/units.zig b/src/browser/css/units.zigindex a87cffdb9..ab4e6400d 100644--- a/src/browser/css/units.zig+++ b/src/browser/css/units.zig@@ -30,6 +30,7 @@ pub const Unit = enum {none, // bare number, no suffixpercentage,em,+ rem,ex,px,cm,@@ -52,13 +53,15 @@ pub const Parsed = struct {const WHITESPACE = " \t\r\n\x0c";-// Longer suffixes first: "5grad" must not match the "rad" entry.+// Longer suffixes first: "5grad" must not match the "rad" entry, nor "2rem"+// the "em" one.const suffixes = [_]struct { []const u8, Unit }{.{ "turn", .turn },.{ "grad", .grad },.{ "deg", .deg },.{ "rad", .rad },.{ "%", .percentage },+ .{ "rem", .rem },.{ "em", .em },.{ "ex", .ex },.{ "px", .px },@@ -129,6 +132,7 @@ test "css units: parse" {try testing.expectEqual(Parsed{ .value = 50, .unit = .percentage }, try parse("50%"));try testing.expectEqual(Parsed{ .value = 2.54, .unit = .cm }, try parse("2.54cm"));try testing.expectEqual(Parsed{ .value = 1.5, .unit = .em }, try parse("1.5EM"));+ try testing.expectEqual(Parsed{ .value = 2, .unit = .rem }, try parse("2rem"));try testing.expectEqual(Parsed{ .value = 90, .unit = .deg }, try parse("90deg"));try testing.expectEqual(Parsed{ .value = 100, .unit = .grad }, try parse("100grad"));try testing.expectEqual(Parsed{ .value = 0.5, .unit = .turn }, try parse("0.5turn"));diff --git a/src/browser/tests/element/styles.html b/src/browser/tests/element/styles.htmlindex f6ddc5dc7..b6042d4a4 100644--- a/src/browser/tests/element/styles.html+++ b/src/browser/tests/element/styles.html@@ -294,7 +294,7 @@const cs = window.getComputedStyle(div);const expected = {'font-weight': '400', 'line-height': 'normal', 'letter-spacing': 'normal',- 'word-spacing': '0px', 'text-indent': '0px', 'position': 'static',+ 'word-spacing': '0px', 'text-indent': '0px','top': 'auto', 'right': 'auto', 'bottom': 'auto', 'left': 'auto', 'z-index': 'auto','box-sizing': 'content-box', 'min-width': '0px', 'min-height': '0px','max-width': 'none', 'max-height': 'none', 'flex-grow': '0', 'flex-shrink': '1',@@ -305,7 +305,6 @@for (const [name, value] of Object.entries(expected)) {testing.expectEqual(value, cs.getPropertyValue(name));}- testing.expectEqual('static', cs.position);testing.expectEqual('content-box', cs.boxSizing);testing.expectEqual('1', cs.flexShrink);testing.expectEqual('', div.style.position);@@ -324,12 +323,30 @@testing.expectEqual('30px', getComputedStyle(relative).fontSize);testing.expectEqual('1.5em', relative.style.fontSize);+ const sized = (value) => {+ const el = document.createElement('span');+ el.style.fontSize = value;+ outer.appendChild(el);+ return getComputedStyle(el).fontSize;+ };+ testing.expectEqual('9px', sized('xx-small'));+ testing.expectEqual('18px', sized('LARGE'));+ testing.expectEqual('48px', sized('xxx-large'));+ testing.expectEqual('24px', sized('larger'));+ testing.expectEqual('32px', sized('2rem'));+ const root = document.documentElement;+ root.style.fontSize = '20px';+ testing.expectEqual('30px', sized('1.5rem'));+ root.style.fontSize = '2rem';+ testing.expectEqual('32px', getComputedStyle(root).fontSize);+ testing.expectEqual('48px', sized('1.5rem'));+ root.style.fontSize = '';+const positioned = document.createElement('div');positioned.style.cssText = 'position: absolute; top: 4px';document.body.appendChild(positioned);testing.expectEqual('absolute', getComputedStyle(positioned).position);testing.expectEqual('4px', getComputedStyle(positioned).top);- testing.expectEqual('auto', getComputedStyle(positioned).left);}</script>diff --git a/src/browser/webapi/css/CSSStyleDeclaration.zig b/src/browser/webapi/css/CSSStyleDeclaration.zigindex e2896c60f..9a954a975 100644--- a/src/browser/webapi/css/CSSStyleDeclaration.zig+++ b/src/browser/webapi/css/CSSStyleDeclaration.zig@@ -989,7 +989,6 @@ const initial_values = std.StaticStringMap([]const u8).initComptime(.{.{ "letter-spacing", "normal" },.{ "word-spacing", "0px" },.{ "text-indent", "0px" },- .{ "position", "static" },.{ "top", "auto" },.{ "right", "auto" },.{ "bottom", "auto" },diff --git a/src/browser/webapi/svg/Length.zig b/src/browser/webapi/svg/Length.zigindex 069084090..f6e272aa6 100644--- a/src/browser/webapi/svg/Length.zig+++ b/src/browser/webapi/svg/Length.zig@@ -331,7 +331,7 @@ fn fromShared(unit: units.Unit) ?Unit {.in => .in,.pt => .pt,.pc => .pc,- .vh, .vw, .deg, .rad, .grad, .turn => null,+ .rem, .vh, .vw, .deg, .rad, .grad, .turn => null,};}