Skip to content

Lightpanda: the headless browser designed for AI and automation

use element.ownerFrame to access StyleManager

Karl Seguin4b06c248e70a
diff --git a/src/browser/StyleManager.zig b/src/browser/StyleManager.zig
index b054de4f3..6dc83ead9 100644
--- a/src/browser/StyleManager.zig
+++ b/src/browser/StyleManager.zig
@@ -1191,6 +1191,8 @@ const CheckVisibilityOptions = struct {
// its field max, so a real rule can never pack to all-ones.
const INLINE_PRIORITY: u64 = std.math.maxInt(u64);
+// `frame` is the StyleManager's frame, which callers guarantee is el's owner
+// frame (el.ownerFrame) — the map where the materialized style lives.
fn getInlineStyleProperty(el: *Element, property_name: String, frame: *Frame) ?*CSSStyleProperty {
const style = frame._element_styles.get(el) orelse blk: {
// No JS-set style object and no style attribute -> nothing inline to read.
diff --git a/src/browser/tests/frames/cross_realm_attributes.html b/src/browser/tests/frames/cross_realm_attributes.html
index d06e5d45f..ac38acf20 100644
--- a/src/browser/tests/frames/cross_realm_attributes.html
+++ b/src/browser/tests/frames/cross_realm_attributes.html
@@ -80,4 +80,47 @@ reads of iframe data-* returned null (WPT encoding/*).
});
}
</script>
+
+<script id="cross_realm_style_resync">
+ testing.onload(() => {
+ const idf = document.getElementById('idf');
+ const iwin = idf.contentWindow;
+ const el = idf.contentDocument.getElementById('s1');
+
+ // el.style materialized from the parent realm must live in the iframe's
+ // (owner) map: a style attribute write from either realm resyncs it.
+ el.style.color = 'red';
+ testing.expectEqual('color: red;', iwin.readAttr('style'));
+
+ iwin.setAttr('style', 'color: blue');
+ testing.expectEqual('blue', el.style.color);
+
+ el.setAttribute('style', 'color: green');
+ testing.expectEqual('green', el.style.color);
+
+ // and a subsequent parent-side write must not resurrect the stale list
+ el.style.margin = '1px';
+ testing.expectEqual('color: green; margin: 1px;', iwin.readAttr('style'));
+
+ iwin.removeAttr('style');
+ testing.expectEqual('', el.style.cssText);
+ testing.expectEqual(false, iwin.checkHidden());
+
+ // inline display:none set from the parent is seen by the iframe realm's
+ // visibility check (same map, same StyleManager walk)
+ el.style.display = 'none';
+ testing.expectEqual(true, iwin.checkHidden());
+ iwin.removeAttr('style');
+ testing.expectEqual(true, el.checkVisibility());
+
+ // a rule from the iframe's own stylesheet must apply, even when the
+ // check is made from the parent realm (owner frame's StyleManager)
+ el.className = 'iframe-hidden';
+ testing.expectEqual(false, el.checkVisibility());
+ testing.expectEqual('none', iwin.getComputedStyle(el).display);
+ testing.expectEqual('none', getComputedStyle(el).display);
+ el.className = '';
+ testing.expectEqual(true, el.checkVisibility());
+ });
+</script>
</body>
diff --git a/src/browser/tests/frames/support/cross_realm_attributes.html b/src/browser/tests/frames/support/cross_realm_attributes.html
index 756e5c0d4..0629b8766 100644
--- a/src/browser/tests/frames/support/cross_realm_attributes.html
+++ b/src/browser/tests/frames/support/cross_realm_attributes.html
@@ -1,4 +1,5 @@
<!DOCTYPE html>
+<head><style>.iframe-hidden { display: none }</style></head>
<body>
<span id="s1" data-cp="5F" data-bytes="A1B2" data-cycle="A1B2" nonstandardattr="ns1">x</span>
<script>
@@ -12,6 +13,9 @@
function removeAttr(name) {
document.getElementById('s1').removeAttribute(name);
}
+ function checkHidden() {
+ return !document.getElementById('s1').checkVisibility();
+ }
window.addEventListener('message', (e) => {
if (e.data !== 'cycle-attr') return;
diff --git a/src/browser/webapi/Element.zig b/src/browser/webapi/Element.zig
index 6bb7ee479..5eab95796 100644
--- a/src/browser/webapi/Element.zig
+++ b/src/browser/webapi/Element.zig
@@ -970,16 +970,20 @@ pub fn getAttributeNamedNodeMap(self: *Element, frame: *Frame) !*Attribute.Named
return gop.value_ptr.*;
}
+// The materialized style lives in the map of the element's own frame, not
+// the caller's: attributeChange (which resyncs it) is dispatched on the owner
+// frame, and a same-origin script can reach an element in another frame.
pub fn getOrCreateStyle(self: *Element, frame: *Frame) !*CSSStyleProperties {
- const gop = try frame._element_styles.getOrPut(frame.arena, self);
+ const owner = self.ownerFrame(frame);
+ const gop = try owner._element_styles.getOrPut(owner.arena, self);
if (!gop.found_existing) {
- gop.value_ptr.* = try CSSStyleProperties.init(self, false, frame);
+ gop.value_ptr.* = try CSSStyleProperties.init(self, false, owner);
}
return gop.value_ptr.*;
}
fn getStyle(self: *Element, frame: *Frame) ?*CSSStyleProperties {
- return frame._element_styles.get(self);
+ return self.ownerFrame(frame)._element_styles.get(self);
}
pub fn setStyle(self: *Element, value: []const u8, frame: *Frame) !void {
@@ -1299,12 +1303,15 @@ pub const VisibilityCache = StyleManager.VisibilityCache;
/// Cache for pointer-events checks - re-exported from StyleManager for convenience.
pub const PointerEventsCache = StyleManager.PointerEventsCache;
+// Style checks go through the StyleManager of the element's own frame, not
+// the caller's: its stylesheets and materialized inline styles are per-frame,
+// and a same-origin script can reach an element in another frame.
pub fn hasPointerEventsNone(self: *Element, cache: ?*PointerEventsCache, frame: *Frame) bool {
- return frame._style_manager.hasPointerEventsNone(self, cache);
+ return self.ownerFrame(frame)._style_manager.hasPointerEventsNone(self, cache);
}
pub fn checkVisibilityCached(self: *Element, cache: ?*VisibilityCache, frame: *Frame) bool {
- return !frame._style_manager.isHidden(self, cache, .{});
+ return !self.ownerFrame(frame)._style_manager.isHidden(self, cache, .{});
}
const CheckVisibilityOpts = struct {
@@ -1315,7 +1322,7 @@ const CheckVisibilityOpts = struct {
};
pub fn checkVisibility(self: *Element, opts_: ?CheckVisibilityOpts, frame: *Frame) bool {
const opts = opts_ orelse CheckVisibilityOpts{};
- return !frame._style_manager.isHidden(self, null, .{
+ return !self.ownerFrame(frame)._style_manager.isHidden(self, null, .{
.check_opacity = opts.checkOpacity or opts.opacityProperty,
.check_visibility = opts.visibilityProperty or opts.checkVisibilityCSS,
});
diff --git a/src/browser/webapi/css/CSSStyleDeclaration.zig b/src/browser/webapi/css/CSSStyleDeclaration.zig
index a78bd3b5d..c06e8ebbd 100644
--- a/src/browser/webapi/css/CSSStyleDeclaration.zig
+++ b/src/browser/webapi/css/CSSStyleDeclaration.zig
@@ -82,10 +82,11 @@ pub fn getPropertyValue(self: *const CSSStyleDeclaration, property_name: []const
// tree builders (Playwright ariaSnapshot) consult on every element.
if (self._is_computed) {
if (self._element) |element| {
+ const style_manager = &element.ownerFrame(frame)._style_manager;
if (wrapped.eql(comptime .wrap("display"))) {
- if (frame._style_manager.hasDisplayNone(element)) return "none";
+ if (style_manager.hasDisplayNone(element)) return "none";
} else if (wrapped.eql(comptime .wrap("visibility"))) {
- if (frame._style_manager.hasVisibilityHiddenInherited(element)) return "hidden";
+ if (style_manager.hasVisibilityHiddenInherited(element)) return "hidden";
}
}
}
@@ -96,7 +97,7 @@ pub fn getPropertyValue(self: *const CSSStyleDeclaration, property_name: []const
if (self._element) |element| {
// Resolve inline `style=` declarations through the element's
// parsed inline style, so computed values match `el.style`.
- if (frame._style_manager.inlineStyleValue(element, wrapped)) |value| {
+ if (element.ownerFrame(frame)._style_manager.inlineStyleValue(element, wrapped)) |value| {
return value;
}
diff --git a/src/browser/webapi/element/svg/TextContent.zig b/src/browser/webapi/element/svg/TextContent.zig
index 4f8683fda..8ab165993 100644
--- a/src/browser/webapi/element/svg/TextContent.zig
+++ b/src/browser/webapi/element/svg/TextContent.zig
@@ -88,7 +88,8 @@ fn text(self: *TextContent, frame: *Frame) []const u8 {
}
fn fontSize(self: *TextContent, frame: *Frame) f64 {
- return frame._style_manager.computedFontSize(self.asElement());
+ const element = self.asElement();
+ return element.ownerFrame(frame)._style_manager.computedFontSize(element);
}
fn getTextLength(self: *TextContent, frame: *Frame) !*AnimatedLength {
diff --git a/src/browser/webapi/svg/Length.zig b/src/browser/webapi/svg/Length.zig
index 0aec09250..2ccb43e9f 100644
--- a/src/browser/webapi/svg/Length.zig
+++ b/src/browser/webapi/svg/Length.zig
@@ -290,15 +290,16 @@ fn pageViewportDimension(direction: Direction, frame: *Frame) f64 {
fn resolveParsedLength(parsed: Parsed, element: *Element, direction: Direction, frame: *Frame, depth: u8) f64 {
const factor = switch (parsed.unit) {
.percentage => ancestorViewportDimensionAt(element, direction, frame, depth) / 100.0,
- .em => frame._style_manager.computedFontSize(element),
- .ex => frame._style_manager.computedFontSize(element) / 2.0,
+ .em => element.ownerFrame(frame)._style_manager.computedFontSize(element),
+ .ex => element.ownerFrame(frame)._style_manager.computedFontSize(element) / 2.0,
else => units.absoluteLengthFactor(toShared(parsed.unit)).?,
};
return parsed.value * factor;
}
fn fontSize(self: *const Length, frame: *Frame) f64 {
- return frame._style_manager.computedFontSize(self._element);
+ const element = self._element orelse return frame._style_manager.computedFontSize(null);
+ return element.ownerFrame(frame)._style_manager.computedFontSize(element);
}
const Parsed = struct {