Skip to content

Lightpanda: the headless browser designed for AI and automation

dom: apply text-transform in innerText, resolve it in getComputedStyle

Navid EMAD09493c943c21
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zig
index 0b714641c..33ec9efec 100644
--- a/src/browser/StyleManager.zig
+++ b/src/browser/StyleManager.zig
@@ -42,7 +42,8 @@ const String = lp.String;
const Allocator = std.mem.Allocator;
// Tracks the CSS properties the renderless layout acts on (display, visibility,
-// opacity, pointer-events, overflow, width, height) from author stylesheets.
+// opacity, pointer-events, overflow, width, height) and text-transform, which
+// innerText applies, from author stylesheets.
// Rules are bucketed by their rightmost selector part for fast lookup.
const StyleManager = @This();
@@ -56,6 +57,7 @@ arena: *lp.Arena,
visibility: Group(Visibility) = .{},
geometry: Group(Geometry) = .{},
touch_action: Group(TouchAction) = .{},
+text: Group(Text) = .{},
// Keyed by property name, pruning to what is (hopefully) one or few rules
custom_rules: std.StringHashMapUnmanaged(CustomProperty) = .empty,
@@ -675,6 +677,27 @@ pub fn isHitTestable(self: *StyleManager, el: *Element) bool {
return !p.visibility_hidden and !p.pointer_events_none;
}
+/// Computed text-transform, which inherits: the nearest of `el` and its
+/// ancestors with its own value, else none.
+pub fn textTransform(self: *StyleManager, el: *Element) TextTransform {
+ self.assertOwns(el);
+ var current: ?*Element = el;
+ while (current) |elem| : (current = elem.parentElement()) {
+ const own = self.ownTextTransform(elem);
+ if (own != .inherit) {
+ return own;
+ }
+ }
+ return .none;
+}
+
+/// `el`'s own cascaded text-transform; `.inherit` when it takes its parent's.
+/// For tree walks that already know the parent's value.
+pub fn ownTextTransform(self: *StyleManager, el: *Element) TextTransform {
+ self.rebuildIfDirty() catch return .inherit;
+ return self.text.ownProps(self.arena.allocator(), el, self.frame).text_transform;
+}
+
/// The axes along which `el` is a scroll container: its own computed overflow
/// on that axis is auto, scroll or overlay. No ancestor walk.
pub fn overflowAxes(self: *StyleManager, el: *Element) Element.ScrollAxes {
@@ -1252,6 +1275,51 @@ pub const Display = enum(u2) {
}
};
+/// The case-changing part of `text-transform`. `full-width`, `full-size-kana`
+/// and `math-auto` are accepted but not applied. `.inherit` is both the
+/// `inherit`/`unset` keywords and "nothing declared": text-transform inherits.
+pub const TextTransform = enum(u3) {
+ inherit,
+ none,
+ capitalize,
+ uppercase,
+ lowercase,
+
+ // Null for an invalid value, which drops the declaration.
+ fn parse(value: []const u8) ?TextTransform {
+ const trimmed = std.mem.trim(u8, value, &std.ascii.whitespace);
+ if (std.ascii.eqlIgnoreCase(trimmed, "inherit") or std.ascii.eqlIgnoreCase(trimmed, "unset") or std.ascii.eqlIgnoreCase(trimmed, "revert") or std.ascii.eqlIgnoreCase(trimmed, "revert-layer")) {
+ return .inherit;
+ }
+ if (std.ascii.eqlIgnoreCase(trimmed, "initial") or std.ascii.eqlIgnoreCase(trimmed, "none")) {
+ return .none;
+ }
+
+ var result: TextTransform = .none;
+ var it = std.mem.tokenizeAny(u8, trimmed, &std.ascii.whitespace);
+ while (it.next()) |token| {
+ if (std.ascii.eqlIgnoreCase(token, "uppercase")) {
+ result = .uppercase;
+ } else if (std.ascii.eqlIgnoreCase(token, "lowercase")) {
+ result = .lowercase;
+ } else if (std.ascii.eqlIgnoreCase(token, "capitalize")) {
+ result = .capitalize;
+ } else if (!(std.ascii.eqlIgnoreCase(token, "full-width") or std.ascii.eqlIgnoreCase(token, "full-size-kana") or std.ascii.eqlIgnoreCase(token, "math-auto"))) {
+ return null;
+ }
+ }
+ return result;
+ }
+
+ /// The computed-value keyword, as getComputedStyle reports it.
+ pub fn keyword(self: TextTransform) []const u8 {
+ return switch (self) {
+ .inherit, .none => "none",
+ else => @tagName(self),
+ };
+ }
+};
+
/// A declared width or height. Values that need layout (auto, %, em) are
/// `auto`: they win the cascade but give no size.
pub const Length = packed struct(u34) {
@@ -1474,14 +1542,35 @@ const TouchAction = struct {
};
};
+/// Inherited text properties. Each element stores only its own cascaded
+/// value; `.inherit` means it takes its parent's.
+const Text = struct {
+ const Declared = struct {
+ const names = [_][]const u8{"text-transform"};
+
+ text_transform: ?TextTransform = null,
+
+ fn apply(self: *Declared, name: []const u8, value: []const u8) void {
+ if (std.ascii.eqlIgnoreCase(name, "text-transform")) {
+ self.text_transform = TextTransform.parse(value);
+ }
+ }
+ };
+
+ const Computed = packed struct(u3) {
+ text_transform: TextTransform = .inherit,
+ };
+};
+
/// Every group's share of one declaration block, so a sheet's block is folded
/// once. Field names match the StyleManager's group fields.
const Declarations = struct {
- const names = Visibility.Declared.names ++ Geometry.Declared.names ++ TouchAction.Declared.names;
+ const names = Visibility.Declared.names ++ Geometry.Declared.names ++ TouchAction.Declared.names ++ Text.Declared.names;
visibility: Visibility.Declared = .{},
geometry: Geometry.Declared = .{},
touch_action: TouchAction.Declared = .{},
+ text: Text.Declared = .{},
fn apply(self: *Declarations, name: []const u8, value: []const u8) void {
inline for (group_fields) |field| {
@@ -1907,6 +1996,10 @@ test "StyleManager: custom properties" {
try testing.htmlRunner("css/custom_properties.html", .{});
}
+test "StyleManager: text-transform" {
+ try testing.htmlRunner("css/text_transform.html", .{});
+}
+
test "StyleManager: computeSpecificity: element selector" {
// div -> (0, 0, 1)
const selector = Selector.Selector{
diff --git a/src/browser/tests/css/text_transform.html b/src/browser/tests/css/text_transform.html
new file mode 100644
index 000000000..a43fde58c
--- /dev/null
+++ b/src/browser/tests/css/text_transform.html
@@ -0,0 +1,81 @@
+<!DOCTYPE html>
+<head>
+ <script src="../testing.js"></script>
+</head>
+
+<!--
+ text-transform: computed value (cascade + inheritance) and its effect on
+ innerText. The rendered text collection steps apply text-transform to each
+ text box, so innerText returns the transformed text.
+-->
+
+<style>
+ .upper { text-transform: uppercase; }
+ .lower { text-transform: lowercase; }
+ .cap { text-transform: capitalize; }
+ .reset { text-transform: none; }
+ .combo { text-transform: uppercase full-width; }
+ #id-wins { text-transform: lowercase; }
+ .bogus { text-transform: shout; }
+</style>
+
+<span id=inline style="text-transform: uppercase">unavailable</span>
+<span id=class class=upper>unavailable</span>
+<div class=upper><span id=inherited>unavailable</span></div>
+<div class=upper><span id=reset class=reset>Mixed Case</span></div>
+<span id=lower class=lower>UNAVAILABLE</span>
+<span id=cap class=cap>hello wORLD, it's é-mail 1st</span>
+<span id=id-wins class=upper>Mixed</span>
+<span id=combo class=combo>wide</span>
+<div class=upper><span id=bogus class=bogus>still upper</span></div>
+<div class=upper><span id=inline-inherit style="text-transform: inherit">kept</span></div>
+<div class=upper><span id=inline-initial style="text-transform: initial">plain</span></div>
+<span id=plain>Plain Text</span>
+<div id=mixed>a <b class=upper>bold</b> <i class=cap>two words</i> z</div>
+<div id=nested class=cap><span>one</span><span class=lower>TWO</span> three</div>
+
+<script id=computed>
+ const tt = (id) => getComputedStyle($('#' + id)).textTransform;
+ testing.expectEqual('uppercase', tt('inline'));
+ testing.expectEqual('uppercase', tt('class'));
+ testing.expectEqual('uppercase', tt('inherited'));
+ testing.expectEqual('none', tt('reset'));
+ testing.expectEqual('lowercase', tt('lower'));
+ testing.expectEqual('capitalize', tt('cap'));
+ testing.expectEqual('lowercase', tt('id-wins'));
+ testing.expectEqual('uppercase', tt('combo'));
+ testing.expectEqual('uppercase', tt('bogus'));
+ testing.expectEqual('uppercase', tt('inline-inherit'));
+ testing.expectEqual('none', tt('inline-initial'));
+ testing.expectEqual('none', tt('plain'));
+ testing.expectEqual('uppercase', getComputedStyle($('#class')).getPropertyValue('text-transform'));
+</script>
+
+<script id=innerText>
+ const it = (id) => $('#' + id).innerText;
+ testing.expectEqual('UNAVAILABLE', it('inline'));
+ testing.expectEqual('UNAVAILABLE', it('class'));
+ testing.expectEqual('UNAVAILABLE', it('inherited'));
+ testing.expectEqual('Mixed Case', it('reset'));
+ testing.expectEqual('unavailable', it('lower'));
+ testing.expectEqual("Hello WORLD, It's É-Mail 1st", it('cap'));
+ testing.expectEqual('mixed', it('id-wins'));
+ testing.expectEqual('WIDE', it('combo'));
+ testing.expectEqual('STILL UPPER', it('bogus'));
+ testing.expectEqual('KEPT', it('inline-inherit'));
+ testing.expectEqual('plain', it('inline-initial'));
+ testing.expectEqual('Plain Text', it('plain'));
+ testing.expectEqual('a BOLD Two Words z', it('mixed'));
+ testing.expectEqual('Onetwo Three', it('nested'));
+ // textContent is not rendered text: untouched.
+ testing.expectEqual('unavailable', $('#class').textContent);
+</script>
+
+<script id=dynamic>
+ const el = $('#plain');
+ el.classList.add('upper');
+ testing.expectEqual('PLAIN TEXT', el.innerText);
+ testing.expectEqual('uppercase', getComputedStyle(el).textTransform);
+ el.style.textTransform = 'lowercase';
+ testing.expectEqual('plain text', el.innerText);
+</script>
diff --git a/src/browser/webapi/css/CSSStyleDeclaration.zig b/src/browser/webapi/css/CSSStyleDeclaration.zig
index 02867939a..8da10cd39 100644
--- a/src/browser/webapi/css/CSSStyleDeclaration.zig
+++ b/src/browser/webapi/css/CSSStyleDeclaration.zig
@@ -80,7 +80,7 @@ pub fn getPropertyValue(self: *const CSSStyleDeclaration, property_name: []const
// Computed styles must reflect stylesheet rules, not just the element's
// inline `style=` attribute. Limited to display/visibility — what aria
// tree builders (Playwright ariaSnapshot) consult on every element — and
- // pointer-events.
+ // pointer-events and text-transform, which innerText applies.
if (self._is_computed) {
if (self._element) |element| {
if (element.ownerFrame(frame)) |owner| {
@@ -97,6 +97,9 @@ pub fn getPropertyValue(self: *const CSSStyleDeclaration, property_name: []const
if (style_manager.hasPointerEventsNone(element)) {
return "none";
}
+ } else if (wrapped.eqlSlice("text-transform")) {
+ // Inherited: the nearest declaration up the tree wins.
+ return style_manager.textTransform(element).keyword();
}
}
}
diff --git a/src/browser/webapi/element/Html.zig b/src/browser/webapi/element/Html.zig
index 1167fb76d..b9623e289 100644
--- a/src/browser/webapi/element/Html.zig
+++ b/src/browser/webapi/element/Html.zig
@@ -29,6 +29,7 @@ const Window = @import("../Window.zig");
const Element = @import("../Element.zig");
const EventTarget = @import("../EventTarget.zig");
const ElementInternals = @import("../ElementInternals.zig");
+const TextTransform = @import("../../StyleManager.zig").TextTransform;
const global_event_handlers = @import("../global_event_handlers.zig");
const reflection = @import("reflection.zig");
@@ -346,7 +347,12 @@ pub fn getInnerText(self: *HtmlElement, writer: *std.Io.Writer, frame: *Frame) !
else => {},
}
- var state = InnerTextState{ .writer = writer, .style_manager = style_manager, .preserve = tag == .pre };
+ var state = InnerTextState{
+ .writer = writer,
+ .style_manager = style_manager,
+ .preserve = tag == .pre,
+ .transform = style_manager.textTransform(el),
+ };
try self.collectInnerText(&state);
}
@@ -723,6 +729,13 @@ const InnerTextState = struct {
// preserve whitespace (pre tag, in the future white-space: pre)
preserve: bool = false,
+ // text-transform of the element whose text is being written
+ transform: TextTransform = .none,
+
+ // The last character written continues a word, so `capitalize` leaves
+ // the next letter alone.
+ in_word: bool = false,
+
fn requireBreaks(self: *InnerTextState, n: u8) void {
if (n > self.pending_breaks) {
self.pending_breaks = n;
@@ -742,6 +755,7 @@ const InnerTextState = struct {
// A block boundary trims the whitespace on either side of it.
self.pre_w = false;
self.trim_left = true;
+ self.in_word = false;
}
// Emit a literal separator (table cell tab / row newline).
@@ -751,6 +765,7 @@ const InnerTextState = struct {
self.wrote_any = true;
self.pre_w = false;
self.trim_left = true;
+ self.in_word = false;
}
};
@@ -846,9 +861,18 @@ fn handleChildElement(
state.wrote_any = true;
state.pre_w = false;
state.trim_left = true;
+ state.in_word = false;
return;
}
+ // text-transform inherits, so a child's own value covers its subtree only.
+ const parent_transform = state.transform;
+ defer state.transform = parent_transform;
+ const own_transform = state.style_manager.ownTextTransform(el);
+ if (own_transform != .inherit) {
+ state.transform = own_transform;
+ }
+
switch (innerTextDisplay(he, tag)) {
// Not rendered: skip the subtree entirely (script/style/metadata/...).
.skip => {},
@@ -936,6 +960,28 @@ fn isAllAsciiWhitespace(s: []const u8) bool {
}
fn writeText(c: *Node.CData, state: *InnerTextState) !void {
+ if (state.transform == .none) {
+ try writeRenderedText(c, state);
+ state.in_word = endsInWord(c.getData().str());
+ return;
+ }
+
+ // Every case mapping we apply keeps the UTF-8 length, so the text is
+ // rendered to a scratch buffer and mapped in place.
+ var scratch: std.Io.Writer.Allocating = .init(state.style_manager.frame.local_arena);
+ defer scratch.deinit();
+ const out = state.writer;
+ state.writer = &scratch.writer;
+ const rendered = writeRenderedText(c, state);
+ state.writer = out;
+ try rendered;
+
+ const text = scratch.written();
+ state.in_word = applyTextTransform(text, state.transform, state.in_word);
+ try out.writeAll(text);
+}
+
+fn writeRenderedText(c: *Node.CData, state: *InnerTextState) !void {
const writer = state.writer;
const s = c.getData().str();
@@ -977,6 +1023,100 @@ fn writeText(c: *Node.CData, state: *InnerTextState) !void {
state.trim_left = state.pre_w;
}
+// Maps `text` in place and returns whether it ends inside a word. `in_word`
+// says whether the text before it did, for `capitalize`.
+fn applyTextTransform(text: []u8, transform: TextTransform, in_word: bool) bool {
+ var word = in_word;
+ var i: usize = 0;
+ while (i < text.len) {
+ const len = std.unicode.utf8ByteSequenceLength(text[i]) catch 1;
+ if (i + len > text.len) {
+ break;
+ }
+ const cp = std.unicode.utf8Decode(text[i..][0..len]) catch {
+ word = false;
+ i += 1;
+ continue;
+ };
+ const mapped = switch (transform) {
+ .uppercase => upperCodepoint(cp),
+ .lowercase => lowerCodepoint(cp),
+ .capitalize => if (word) cp else upperCodepoint(cp),
+ .none, .inherit => cp,
+ };
+ if (mapped != cp) {
+ _ = std.unicode.utf8Encode(mapped, text[i..][0..len]) catch unreachable;
+ }
+ word = isWordCodepoint(cp);
+ i += len;
+ }
+ return word;
+}
+
+fn endsInWord(s: []const u8) bool {
+ var start = s.len;
+ while (start > 0) {
+ start -= 1;
+ if (s[start] & 0xC0 != 0x80) {
+ break;
+ }
+ }
+ if (start == s.len) {
+ return false;
+ }
+ const cp = std.unicode.utf8Decode(s[start..]) catch return false;
+ return isWordCodepoint(cp);
+}
+
+// Letters, digits and in-word apostrophes; a letter after one of these does
+// not start a new word for `capitalize` ("it's", "1st").
+fn isWordCodepoint(cp: u21) bool {
+ if (cp < 0x80) {
+ return std.ascii.isAlphanumeric(@intCast(cp)) or cp == '\'';
+ }
+ if (cp == 0x2019) {
+ return true; // right single quotation mark, the typographic apostrophe
+ }
+ return switch (cp) {
+ 0xAA, 0xB5, 0xBA => true,
+ 0x80...0xA9, 0xAB...0xB4, 0xB6...0xB9, 0xBB...0xBF, 0xD7, 0xF7 => false,
+ 0x2000...0x206F => false, // general punctuation
+ else => true,
+ };
+}
+
+// Simple case mappings that keep the UTF-8 length: Latin-1, Latin
+// Extended-A, basic Greek and Cyrillic. Other scripts pass through, as do
+// mappings that change the length (ß -> SS, ı -> I, İ -> i̇).
+fn upperCodepoint(cp: u21) u21 {
+ return switch (cp) {
+ 'a'...'z' => cp - 0x20,
+ 0xE0...0xF6, 0xF8...0xFE => cp - 0x20,
+ 0xFF => 0x178,
+ 0x101...0x12F, 0x133...0x137, 0x14B...0x177 => if (cp % 2 == 1) cp - 1 else cp,
+ 0x13A...0x148, 0x17A...0x17E => if (cp % 2 == 0) cp - 1 else cp,
+ 0x3B1...0x3C1, 0x3C3...0x3C9 => cp - 0x20,
+ 0x3C2 => 0x3A3, // final sigma
+ 0x430...0x44F => cp - 0x20,
+ 0x450...0x45F => cp - 0x50,
+ else => cp,
+ };
+}
+
+fn lowerCodepoint(cp: u21) u21 {
+ return switch (cp) {
+ 'A'...'Z' => cp + 0x20,
+ 0xC0...0xD6, 0xD8...0xDE => cp + 0x20,
+ 0x178 => 0xFF,
+ 0x100...0x12E, 0x132...0x136, 0x14A...0x176 => if (cp % 2 == 0) cp + 1 else cp,
+ 0x139...0x147, 0x179...0x17D => if (cp % 2 == 1) cp + 1 else cp,
+ 0x391...0x3A1, 0x3A3...0x3A9 => cp + 0x20,
+ 0x410...0x42F => cp + 0x20,
+ 0x400...0x40F => cp + 0x50,
+ else => cp,
+ };
+}
+
fn mergeTextNodes(left_node: *Node, right_node: *Node, frame: *Frame) !bool {
const left = left_node.is(Node.CData) orelse return false;
const right = right_node.is(Node.CData) orelse return false;