Skip to content

Lightpanda: the headless browser designed for AI and automation

html: report the real isContentEditable value

a.dmitryukb7a31a93ab77
diff --git a/src/browser/frame/user_input.zig b/src/browser/frame/user_input.zig
index def0c9ccd..d5d5ac5bc 100644
--- a/src/browser/frame/user_input.zig
+++ b/src/browser/frame/user_input.zig
@@ -1166,10 +1166,8 @@ fn outermostEditingHost(target: *Element) ?*Element {
var host: ?*Element = null;
var current: ?*Element = target;
while (current) |el| : (current = el.parentElement()) {
- if (el.getAttributeInterned("contenteditable") == null) {
- continue;
- }
- if (el.isEditingHost() == false) {
+ const editable = el.contentEditableState() orelse continue;
+ if (editable == false) {
break;
}
host = el;
diff --git a/src/browser/tests/document/focus.html b/src/browser/tests/document/focus.html
index ffac23bc9..492c022a6 100644
--- a/src/browser/tests/document/focus.html
+++ b/src/browser/tests/document/focus.html
@@ -447,8 +447,16 @@
plain.focus();
testing.expectEqual(div, document.activeElement);
+ // an invalid value is the inherit state, not an editing host
+ const invalid = document.createElement('div');
+ invalid.setAttribute('contenteditable', 'bogus');
+ document.body.appendChild(invalid);
+ invalid.focus();
+ testing.expectEqual(div, document.activeElement);
+
div.remove();
plain.remove();
+ invalid.remove();
}
</script>
diff --git a/src/browser/tests/element/html/contenteditable.html b/src/browser/tests/element/html/contenteditable.html
index cee1277d5..3be613228 100644
--- a/src/browser/tests/element/html/contenteditable.html
+++ b/src/browser/tests/element/html/contenteditable.html
@@ -1,14 +1,6 @@
<!DOCTYPE html>
<script src="../../testing.js"></script>
-<!--
- Lightpanda has no caret/keyboard editing pipeline, so isContentEditable
- always returns false regardless of the element's effective contenteditable
- state. The spec walk still runs internally to log .not_implemented when the
- spec would have said true; only the return value is asserted here. See
- Html.zig:getIsContentEditable and PR #2310 for context.
--->
-
<div id="own-true" contenteditable="true">own true</div>
<div id="own-empty" contenteditable="">own empty</div>
<div id="own-false" contenteditable="false">own false</div>
@@ -26,16 +18,16 @@
<div id="plain"><span id="child-of-plain">no editing context</span></div>
-<script id="always-false">
+<script id="is-content-editable">
{
- testing.expectEqual(false, document.getElementById('own-true').isContentEditable);
- testing.expectEqual(false, document.getElementById('own-empty').isContentEditable);
+ testing.expectEqual(true, document.getElementById('own-true').isContentEditable);
+ testing.expectEqual(true, document.getElementById('own-empty').isContentEditable);
testing.expectEqual(false, document.getElementById('own-false').isContentEditable);
- testing.expectEqual(false, document.getElementById('own-plaintext').isContentEditable);
- testing.expectEqual(false, document.getElementById('ancestor-true').isContentEditable);
- testing.expectEqual(false, document.getElementById('child-of-true').isContentEditable);
+ testing.expectEqual(true, document.getElementById('own-plaintext').isContentEditable);
+ testing.expectEqual(true, document.getElementById('ancestor-true').isContentEditable);
+ testing.expectEqual(true, document.getElementById('child-of-true').isContentEditable);
testing.expectEqual(false, document.getElementById('ancestor-false').isContentEditable);
- testing.expectEqual(false, document.getElementById('grandchild-rehosted').isContentEditable);
+ testing.expectEqual(true, document.getElementById('grandchild-rehosted').isContentEditable);
testing.expectEqual(false, document.getElementById('plain').isContentEditable);
testing.expectEqual(false, document.getElementById('child-of-plain').isContentEditable);
testing.expectEqual(false, document.body.isContentEditable);
@@ -46,16 +38,52 @@
<script id="dynamic-attribute">
{
const el = document.createElement('div');
+ document.body.appendChild(el);
testing.expectEqual(false, el.isContentEditable);
el.setAttribute('contenteditable', 'true');
- testing.expectEqual(false, el.isContentEditable);
+ testing.expectEqual(true, el.isContentEditable);
el.setAttribute('contenteditable', 'false');
testing.expectEqual(false, el.isContentEditable);
el.removeAttribute('contenteditable');
testing.expectEqual(false, el.isContentEditable);
+
+ const child = document.createElement('span');
+ el.appendChild(child);
+ testing.expectEqual(false, child.isContentEditable);
+ el.setAttribute('contenteditable', 'plaintext-only');
+ testing.expectEqual(true, child.isContentEditable);
+ child.setAttribute('contenteditable', 'false');
+ testing.expectEqual(false, child.isContentEditable);
+ el.remove();
+ }
+</script>
+
+<div id="invalid-alone" contenteditable="bogus">invalid</div>
+<div contenteditable="true">
+ <div id="invalid-in-true" contenteditable="bogus">invalid inherits true</div>
+</div>
+<div contenteditable="false">
+ <div id="invalid-in-false" contenteditable="bogus">invalid inherits false</div>
+</div>
+
+<svg contenteditable="true"><foreignObject><div id="in-svg">in svg</div></foreignObject></svg>
+
+<script id="invalid-value-inherits">
+ {
+ const alone = document.getElementById('invalid-alone');
+ testing.expectEqual('inherit', alone.contentEditable);
+ testing.expectEqual(false, alone.isContentEditable);
+ testing.expectEqual(true, document.getElementById('invalid-in-true').isContentEditable);
+ testing.expectEqual(false, document.getElementById('invalid-in-false').isContentEditable);
+ }
+</script>
+
+<script id="svg-attribute-ignored">
+ {
+ testing.expectEqual(false, document.getElementById('in-svg').isContentEditable);
}
</script>
@@ -71,7 +99,7 @@
const el = document.getElementById('plain');
el.contentEditable = 'TRUE';
testing.expectEqual('true', el.getAttribute('contenteditable'));
- testing.expectEqual(false, el.isContentEditable);
+ testing.expectEqual(true, el.isContentEditable);
el.contentEditable = 'inherit';
testing.expectEqual(false, el.hasAttribute('contenteditable'));
testing.expectError('SyntaxError', () => { el.contentEditable = 'maybe'; });
diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zig
index 0276102d3..1e481d59c 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -1212,19 +1212,35 @@ pub fn isSvgLink(self: *Element) bool {
return self.is(Svg.Graphics.A) != null and self.svgAnchorHref() != null;
}
+// The contenteditable attribute's own state: true for the true and
+// plaintext-only states, false for false, null for inherit (no attribute,
+// or an invalid value). The attribute only applies to HTML elements.
+pub fn contentEditableState(self: *Element) ?bool {
+ if (self.is(Html) == null) {
+ return null;
+ }
+ const value = self.getAttributeInterned("contenteditable") orelse return null;
+ if (value.len == 0 or std.ascii.eqlIgnoreCase(value, "true") or std.ascii.eqlIgnoreCase(value, "plaintext-only")) {
+ return true;
+ }
+ if (std.ascii.eqlIgnoreCase(value, "false")) {
+ return false;
+ }
+ return null;
+}
+
// An editing host takes focus like a form control does.
pub fn isEditingHost(self: *Element) bool {
- const value = self.getAttributeInterned("contenteditable") orelse return false;
- return std.ascii.eqlIgnoreCase(value, "false") == false;
+ return self.contentEditableState() orelse false;
}
// An editing host or a descendant of one: the nearest contenteditable
-// attribute decides.
+// attribute in a true or false state decides.
pub fn isEditable(self: *Element) bool {
var current: ?*Element = self;
while (current) |el| : (current = el.parentElement()) {
- if (el.getAttributeInterned("contenteditable") != null) {
- return el.isEditingHost();
+ if (el.contentEditableState()) |editable| {
+ return editable;
}
}
return false;
diff --git a/src/browser/webapi/element/Html.zig b/src/browser/webapi/element/Html.zig
index 90efdc4f9..13e2e7340 100644
--- a/src/browser/webapi/element/Html.zig
+++ b/src/browser/webapi/element/Html.zig
@@ -102,7 +102,6 @@ pub const Track = @import("html/Track.zig");
pub const UL = @import("html/UL.zig");
pub const Unknown = @import("html/Unknown.zig");
-const log = lp.log;
const TextTransform = StyleManager.TextTransform;
const HtmlElement = @This();
@@ -611,17 +610,8 @@ pub fn setTitle(self: *HtmlElement, value: []const u8, frame: *Frame) !void {
try self.asElement().setAttributeSafe(comptime .wrap("title"), .wrap(value), frame);
}
-// HTML §7.7.5.2 specifies the IDL attribute as true iff the element's effective
-// content editable state is "true" or "plaintext-only". Lightpanda has no
-// caret/keyboard editing pipeline, so a true answer cannot be honored
-// end-to-end — downstream CDP tools (notably Puppeteer's dispatchKeyEvent
-// path) would route into an input pipeline that silently no-ops. Always
-// return false, and log .not_implemented when the spec would have said true
-// so usage surfaces in telemetry rather than silently depending on an
-// 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.
-/// Reflects the attribute only; `isContentEditable` stays false regardless.
+/// Reflects the attribute only; `isContentEditable` gives the state the
+/// element ends up with (see Element.isEditable).
pub fn getContentEditable(self: *HtmlElement) []const u8 {
const raw = self.asElement().getAttributeInterned("contenteditable") orelse return "inherit";
if (raw.len == 0 or std.ascii.eqlIgnoreCase(raw, "true")) return "true";
@@ -644,10 +634,7 @@ pub fn setContentEditable(self: *HtmlElement, value: []const u8, frame: *Frame)
}
pub fn getIsContentEditable(self: *HtmlElement) bool {
- if (self.asElement().isEditable()) {
- log.info(.not_implemented, "IsContentEditable", .{});
- }
- return false;
+ return self.asElement().isEditable();
}
// An event handler content attribute (onclick="...") sets the handler to its
diff --git a/src/server/bidi/http_command.zig b/src/server/bidi/http_command.zig
index c25679088..96dd43a6b 100644
--- a/src/server/bidi/http_command.zig
+++ b/src/server/bidi/http_command.zig
@@ -761,7 +761,7 @@ fn isEditable(element: *Node.Element) bool {
else => {},
}
} else if (element.is(Html.TextArea) == null) {
- return element.isEditingHost();
+ return element.isEditable();
}
return element.isDisabled() == false and element.getAttributeSafe(comptime .wrap("readonly")) == null;
}