Skip to content

Lightpanda: the headless browser designed for AI and automation

dom: apply only uppercase and lowercase text-transform in innerText

Navid EMADea6ec0a0eba2
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zig
index 94162f177..bfc1f0e14 100644
--- a/src/browser/StyleManager.zig
+++ b/src/browser/StyleManager.zig
@@ -1284,8 +1284,9 @@ 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
+/// The case-changing part of `text-transform`. innerText applies `uppercase`
+/// and `lowercase`; `capitalize` is a computed value only, and `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,
diff --git a/src/browser/tests/css/text_transform.html b/src/browser/tests/css/text_transform.html
index a43fde58c..9cc1bd074 100644
--- a/src/browser/tests/css/text_transform.html
+++ b/src/browser/tests/css/text_transform.html
@@ -6,7 +6,8 @@
<!--
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.
+ text box, so innerText returns the transformed text. Only uppercase and
+ lowercase are applied; capitalize is reported but leaves the text as is.
-->
<style>
@@ -58,15 +59,15 @@
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("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'));
+ 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>
diff --git a/src/browser/webapi/element/Html.zig b/src/browser/webapi/element/Html.zig
index b9623e289..6691aa425 100644
--- a/src/browser/webapi/element/Html.zig
+++ b/src/browser/webapi/element/Html.zig
@@ -732,10 +732,6 @@ const InnerTextState = struct {
// 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;
@@ -755,7 +751,6 @@ 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).
@@ -765,7 +760,6 @@ const InnerTextState = struct {
self.wrote_any = true;
self.pre_w = false;
self.trim_left = true;
- self.in_word = false;
}
};
@@ -861,7 +855,6 @@ fn handleChildElement(
state.wrote_any = true;
state.pre_w = false;
state.trim_left = true;
- state.in_word = false;
return;
}
@@ -960,10 +953,10 @@ 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;
+ // `capitalize` is recognized (getComputedStyle reports it) but not
+ // applied: it needs word-boundary tracking across text runs.
+ if (state.transform != .uppercase and state.transform != .lowercase) {
+ return writeRenderedText(c, state);
}
// Every case mapping we apply keeps the UTF-8 length, so the text is
@@ -977,7 +970,7 @@ fn writeText(c: *Node.CData, state: *InnerTextState) !void {
try rendered;
const text = scratch.written();
- state.in_word = applyTextTransform(text, state.transform, state.in_word);
+ applyTextTransform(text, state.transform);
try out.writeAll(text);
}
@@ -1023,10 +1016,8 @@ fn writeRenderedText(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;
+// Maps `text` in place for `uppercase` / `lowercase`.
+fn applyTextTransform(text: []u8, transform: TextTransform) void {
var i: usize = 0;
while (i < text.len) {
const len = std.unicode.utf8ByteSequenceLength(text[i]) catch 1;
@@ -1034,55 +1025,19 @@ fn applyTextTransform(text: []u8, transform: TextTransform, in_word: bool) bool
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,
+ else => 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