Lightpanda: the headless browser designed for AI and automation
webapi: improve user_input's contenteditable detection
Karl Seguinaf68205d66fa
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zigindex d4366ece4..b536a4f0d 100644--- a/src/browser/frame/user_input.zig+++ b/src/browser/frame/user_input.zig@@ -593,22 +593,18 @@ fn isEditingHost(node: *Node) bool {}fn outermostEditingHost(target: *Element) ?*Element {- var node: ?*Node = target.asNode();- var editable: ?*Node = null;- while (node) |n| : (node = n._parent) {- if (isEditingHost(n)) {- editable = n;- break;+ var host: ?*Element = null;+ var current: ?*Element = target;+ while (current) |el| : (current = el.parentElement()) {+ if (el.getAttributeInterned("contenteditable") == null) {+ continue;}- }- var host = editable orelse return null;- while (host._parent) |p| {- if (!isEditingHost(p)) {+ if (el.isEditingHost() == false) {break;}- host = p;+ host = el;}- return host.is(Element);+ return host;}/// Mousedown default action. A mousedown outside any focusable element movesdiff --git a/src/browser/interactive.zig b/src/browser/interactive.zigindex c6f4e219f..634307cf9 100644--- a/src/browser/interactive.zig+++ b/src/browser/interactive.zig@@ -333,8 +333,8 @@ pub fn classifyInteractivity(if (isInteractiveRole(role)) return .aria;}- // 3. contenteditable (15 bytes, exceeds SSO limit for comptime)- if (el.getAttributeSafe(.wrap("contenteditable"))) |ce| {+ // 3. contenteditable+ if (el.getAttributeInterned("contenteditable")) |ce| {if (ce.len == 0 or std.ascii.eqlIgnoreCase(ce, "true")) return .contenteditable;}diff --git a/src/browser/tests/event/abort_controller.html b/src/browser/tests/event/abort_controller.htmlindex b68dcddb3..0fb4187df 100644--- a/src/browser/tests/event/abort_controller.html+++ b/src/browser/tests/event/abort_controller.html@@ -333,3 +333,13 @@});}</script>++<script id=abortsignal_timeout_enforce_range>+ // [EnforceRange] unsigned long long: no wrapping, out of range throws.+ testing.expectError('TypeError', () => AbortSignal.timeout(-1));+ testing.expectError('TypeError', () => AbortSignal.timeout(NaN));+ testing.expectError('TypeError', () => AbortSignal.timeout(Infinity));+ testing.expectError('TypeError', () => AbortSignal.timeout(2 ** 53));+ testing.expectEqual(false, AbortSignal.timeout(2 ** 53 - 1).aborted);+ testing.expectEqual(false, AbortSignal.timeout(-0.5).aborted);+</script>diff --git a/src/browser/webapi/AbortSignal.zig b/src/browser/webapi/AbortSignal.zigindex 82d1c16d7..9adf1effc 100644--- a/src/browser/webapi/AbortSignal.zig+++ b/src/browser/webapi/AbortSignal.zig@@ -215,7 +215,16 @@ fn createAny(signals_value: js.Value, exec: *const Execution) !*AbortSignal {return result;}-fn createTimeout(delay: u32, exec: *const Execution) !*AbortSignal {+fn createTimeout(milliseconds: f64, exec: *const Execution) !*AbortSignal {+ if (std.math.isFinite(milliseconds) == false) {+ return error.TypeError;+ }+ const truncated = @trunc(milliseconds);+ if (truncated < 0 or truncated > std.math.maxInt(u53)) {+ return error.TypeError;+ }+ const delay: u32 = @intFromFloat(@min(truncated, std.math.maxInt(u32)));+const callback = try exec.arena.create(TimeoutCallback);callback.* = .{.exec = exec,diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zigindex 13469f278..5cab1dc75 100644--- a/src/browser/webapi/Element.zig+++ b/src/browser/webapi/Element.zig@@ -1222,7 +1222,7 @@ pub fn isSvgLink(self: *Element) bool {// An editing host takes focus like a form control does.pub fn isEditingHost(self: *Element) bool {- const value = self.getAttributeSafe(.wrap("contenteditable")) orelse return false;+ const value = self.getAttributeInterned("contenteditable") orelse return false;return std.ascii.eqlIgnoreCase(value, "false") == false;}@@ -1231,7 +1231,7 @@ pub fn isEditingHost(self: *Element) bool {pub fn isEditable(self: *Element) bool {var current: ?*Element = self;while (current) |el| : (current = el.parentElement()) {- if (el.getAttributeSafe(.wrap("contenteditable")) != null) {+ if (el.getAttributeInterned("contenteditable") != null) {return el.isEditingHost();}}diff --git a/src/browser/webapi/element/Html.zig b/src/browser/webapi/element/Html.zigindex 9f551e23b..815586212 100644--- a/src/browser/webapi/element/Html.zig+++ b/src/browser/webapi/element/Html.zig@@ -617,12 +617,9 @@ pub fn setTitle(self: *HtmlElement, value: []const u8, frame: *Frame) !void {// unsupported value. Spec walk per HTML §7.7.5.2 still applies — the nearest// ancestor with `contenteditable` wins; "false" disables. See PR #2310 for// the routing-vs-fail-loud discussion.-//-// "contenteditable" is 15 bytes — past the comptime SSO limit — so the-// String wrap runs at runtime, mirroring the pattern in interactive.zig./// Reflects the attribute only; `isContentEditable` stays false regardless.pub fn getContentEditable(self: *HtmlElement) []const u8 {- const raw = self.asElement().getAttributeSafe(.wrap("contenteditable")) orelse return "inherit";+ const raw = self.asElement().getAttributeInterned("contenteditable") orelse return "inherit";if (raw.len == 0 or std.ascii.eqlIgnoreCase(raw, "true")) return "true";if (std.ascii.eqlIgnoreCase(raw, "false")) return "false";if (std.ascii.eqlIgnoreCase(raw, "plaintext-only")) return "plaintext-only";diff --git a/src/script/Runtime.zig b/src/script/Runtime.zigindex 9794776a3..22ac4793a 100644--- a/src/script/Runtime.zig+++ b/src/script/Runtime.zig@@ -1475,6 +1475,9 @@ test "agent script runtime: mousedown focus follows mouse-focusability rules" {\\ add('label', 'dynLabel', { for: 'dynLabInp' }).textContent = 'lab';\\ add('span', 'dynHostSpan', {}, add('div', 'dynHost', { contenteditable: 'true' })).textContent = 'hs';\\ add('span', 'dynInnerSpan', {}, add('div', 'dynInner', { contenteditable: 'true' }, add('div', 'dynOuter', { contenteditable: 'true' }))).textContent = 'is';+ \\ add('span', 'dynGapSpan', {}, add('p', 'dynGapInner', { contenteditable: 'true' }, add('section', 'dynGapMid', {}, add('div', 'dynGapOuter', { contenteditable: 'true' })))).textContent = 'gs';+ \\ add('span', 'dynIslandSpan', {}, add('p', 'dynIsland', { contenteditable: 'false', tabindex: '0' }, add('div', 'dynIslandHost', { contenteditable: 'true' }))).textContent = 'ls';+ \\ add('span', 'dynReentrySpan', {}, add('b', 'dynReentry', { contenteditable: 'true' }, add('p', 'dynReentryOff', { contenteditable: 'false' }, add('div', 'dynReentryHost', { contenteditable: 'true' })))).textContent = 'rs';\\ const SVG = 'http://www.w3.org/2000/svg';\\ add('rect', 'dynSvgRect', { tabindex: '0', width: '100', height: '40' }, add('svg', 'dynSvg', {}, document.body, SVG), SVG);\\`);@@ -1499,6 +1502,12 @@ test "agent script runtime: mousedown focus follows mouse-focusability rules" {\\expectActive("dynHost", "span inside contenteditable did not focus host");\\page.click("#dynInnerSpan");\\expectActive("dynOuter", "nested contenteditable did not focus the outer host");+ \\page.click("#dynGapSpan");+ \\expectActive("dynGapOuter", "an ancestor without contenteditable split the editable region");+ \\page.click("#dynIslandSpan");+ \\expectActive("dynIsland", "contenteditable=false island did not take its own focus");+ \\page.click("#dynReentrySpan");+ \\expectActive("dynReentry", "contenteditable inside a false island did not focus its own host");\\// An explicit tabindex is focusable on a non-HTML element too.\\page.click("#inp");\\page.click("#dynSvgRect");diff --git a/src/string.zig b/src/string.zigindex 32a953647..9d5dfe782 100644--- a/src/string.zig+++ b/src/string.zig@@ -389,6 +389,7 @@ pub const String = extern struct {},15 => switch (@as(u120, @bitCast(input[0..15].*))) {asUint("text-decoration") => return "text-decoration",+ asUint("contenteditable") => return "contenteditable",asUint("justify-content") => return "justify-content",asUint("aria-labelledby") => return "aria-labelledby",else => {},