Lightpanda: the headless browser designed for AI and automation
webapi: window-reflecting handlers target the body's own window
Francis Bouvierdba25389c685
diff --git a/src/browser/tests/frames/support/window_reflecting.html b/src/browser/tests/frames/support/window_reflecting.htmlnew file mode 100644index 000000000..b84a50668--- /dev/null+++ b/src/browser/tests/frames/support/window_reflecting.html@@ -0,0 +1,6 @@+<!DOCTYPE html>+<!-- The onresize attribute exercises the parse-time path: it must land on+ THIS frame's window, not the top frame's. -->+<body onresize="window.__attr_resize = true">+ <p>x</p>+</body>diff --git a/src/browser/tests/frames/window_reflecting_handlers.html b/src/browser/tests/frames/window_reflecting_handlers.htmlnew file mode 100644index 000000000..0150f13d1--- /dev/null+++ b/src/browser/tests/frames/window_reflecting_handlers.html@@ -0,0 +1,53 @@+<!DOCTYPE html>+<head></head>+<body>+<script src="../testing.js"></script>++<!--+The window-reflecting body element event handlers (onblur, onerror, onfocus,+onload, onresize, onscroll) alias the Window of the body's node document. A+same-origin script reaching into another frame (parent setting a handler on+an iframe's body) must target the iframe's window, not the window of the+realm running the assignment.+-->+<iframe id="wrh" src="support/window_reflecting.html"></iframe>++<script id="cross_realm_property_handlers">+ testing.onload(() => {+ const idf = document.getElementById('wrh');+ const ibody = idf.contentDocument.body;+ const iwin = idf.contentWindow;++ const cb = () => {};+ ibody.onblur = cb;+ testing.expectEqual(cb, ibody.onblur);+ testing.expectEqual(cb, iwin.onblur);+ // the parent's window (the caller's realm) must be untouched+ testing.expectTrue(window.onblur == null);+ testing.expectTrue(document.body.onblur == null);++ ibody.onblur = null;+ testing.expectTrue(iwin.onblur == null);+ });+</script>++<script id="cross_realm_attribute_handlers">+ testing.onload(() => {+ const idf = document.getElementById('wrh');+ const ibody = idf.contentDocument.body;+ const iwin = idf.contentWindow;++ // Parse-time: the onresize attribute in the iframe's markup was compiled+ // onto the iframe's window while the parent frame was the active one.+ testing.expectEqual('function', typeof iwin.onresize);+ testing.expectTrue(window.onresize == null);++ // Runtime: setting the content attribute from the parent realm.+ ibody.setAttribute('onscroll', 'window.__parent_set = true');+ testing.expectEqual('function', typeof iwin.onscroll);+ testing.expectTrue(window.onscroll == null);++ ibody.removeAttribute('onscroll');+ testing.expectTrue(iwin.onscroll == null);+ });+</script>diff --git a/src/browser/webapi/element/html/Body.zig b/src/browser/webapi/element/html/Body.zigindex 464ea83be..4101261cd 100644--- a/src/browser/webapi/element/html/Body.zig+++ b/src/browser/webapi/element/html/Body.zig@@ -41,46 +41,54 @@ pub fn asNode(self: *Body) *Node {// Special-case: the "window-reflecting body element event handler set"// (blur, error, focus, load, resize, scroll) are aliases for the Window's// event handlers.-pub fn getOnBlur(_: *Body, frame: *Frame) ?js.Function.Global {- return frame.window._on_blur;++// The aliased Window is the one of the element's node document's frame — not+// the caller's frame, which differs when a same-origin script reaches into+// another frame (e.g. the parent setting iframeDoc.body.onblur).+fn reflectedWindow(self: *Body, frame: *Frame) *Window {+ return self.asElement().ownerFrame(frame).window;+}++pub fn getOnBlur(self: *Body, frame: *Frame) ?js.Function.Global {+ return self.reflectedWindow(frame)._on_blur;}-pub fn setOnBlur(_: *Body, setter: ?Window.FunctionSetter, frame: *Frame) !void {- frame.window._on_blur = Window.getFunctionFromSetter(setter);+pub fn setOnBlur(self: *Body, setter: ?Window.FunctionSetter, frame: *Frame) !void {+ self.reflectedWindow(frame)._on_blur = Window.getFunctionFromSetter(setter);}-pub fn getOnError(_: *Body, frame: *Frame) ?js.Function.Global {- return frame.window._on_error;+pub fn getOnError(self: *Body, frame: *Frame) ?js.Function.Global {+ return self.reflectedWindow(frame)._on_error;}-pub fn setOnError(_: *Body, setter: ?Window.FunctionSetter, frame: *Frame) !void {- frame.window._on_error = Window.getFunctionFromSetter(setter);+pub fn setOnError(self: *Body, setter: ?Window.FunctionSetter, frame: *Frame) !void {+ self.reflectedWindow(frame)._on_error = Window.getFunctionFromSetter(setter);}-pub fn getOnFocus(_: *Body, frame: *Frame) ?js.Function.Global {- return frame.window._on_focus;+pub fn getOnFocus(self: *Body, frame: *Frame) ?js.Function.Global {+ return self.reflectedWindow(frame)._on_focus;}-pub fn setOnFocus(_: *Body, setter: ?Window.FunctionSetter, frame: *Frame) !void {- frame.window._on_focus = Window.getFunctionFromSetter(setter);+pub fn setOnFocus(self: *Body, setter: ?Window.FunctionSetter, frame: *Frame) !void {+ self.reflectedWindow(frame)._on_focus = Window.getFunctionFromSetter(setter);}-pub fn getOnLoad(_: *Body, frame: *Frame) ?js.Function.Global {- return frame.window._on_load;+pub fn getOnLoad(self: *Body, frame: *Frame) ?js.Function.Global {+ return self.reflectedWindow(frame)._on_load;}-pub fn setOnLoad(_: *Body, setter: ?Window.FunctionSetter, frame: *Frame) !void {- frame.window._on_load = Window.getFunctionFromSetter(setter);+pub fn setOnLoad(self: *Body, setter: ?Window.FunctionSetter, frame: *Frame) !void {+ self.reflectedWindow(frame)._on_load = Window.getFunctionFromSetter(setter);}-pub fn getOnResize(_: *Body, frame: *Frame) ?js.Function.Global {- return frame.window._on_resize;+pub fn getOnResize(self: *Body, frame: *Frame) ?js.Function.Global {+ return self.reflectedWindow(frame)._on_resize;}-pub fn setOnResize(_: *Body, setter: ?Window.FunctionSetter, frame: *Frame) !void {- frame.window._on_resize = Window.getFunctionFromSetter(setter);+pub fn setOnResize(self: *Body, setter: ?Window.FunctionSetter, frame: *Frame) !void {+ self.reflectedWindow(frame)._on_resize = Window.getFunctionFromSetter(setter);}-pub fn getOnScroll(_: *Body, frame: *Frame) ?js.Function.Global {- return frame.window._on_scroll;+pub fn getOnScroll(self: *Body, frame: *Frame) ?js.Function.Global {+ return self.reflectedWindow(frame)._on_scroll;}-pub fn setOnScroll(_: *Body, setter: ?Window.FunctionSetter, frame: *Frame) !void {- frame.window._on_scroll = Window.getFunctionFromSetter(setter);+pub fn setOnScroll(self: *Body, setter: ?Window.FunctionSetter, frame: *Frame) !void {+ self.reflectedWindow(frame)._on_scroll = Window.getFunctionFromSetter(setter);}pub const JsApi = struct {@@ -107,18 +115,21 @@ pub const Build = struct {pub fn complete(node: *Node, frame: *Frame) !void {const el = node.as(Element);+ const owner = node.ownerFrame(frame);inline for (window_reflecting_attributes) |attr| {if (el.getAttributeSafe(comptime .wrap(attr))) |value| {- frame.window.setWindowReflectingHandlerFromAttribute(comptime .wrap(attr), value, frame);+ owner.window.setWindowReflectingHandlerFromAttribute(comptime .wrap(attr), value, owner);}}}- pub fn attributeChange(_: *Element, name: String, value: String, frame: *Frame) !void {- frame.window.setWindowReflectingHandlerFromAttribute(name, value.str(), frame);+ pub fn attributeChange(el: *Element, name: String, value: String, frame: *Frame) !void {+ const owner = el.ownerFrame(frame);+ owner.window.setWindowReflectingHandlerFromAttribute(name, value.str(), owner);}- pub fn attributeRemove(_: *Element, name: String, frame: *Frame) !void {- frame.window.setWindowReflectingHandlerFromAttribute(name, null, frame);+ pub fn attributeRemove(el: *Element, name: String, frame: *Frame) !void {+ const owner = el.ownerFrame(frame);+ owner.window.setWindowReflectingHandlerFromAttribute(name, null, owner);}};diff --git a/src/browser/webapi/element/html/FrameSet.zig b/src/browser/webapi/element/html/FrameSet.zigindex bec26401f..2643580a4 100644--- a/src/browser/webapi/element/html/FrameSet.zig+++ b/src/browser/webapi/element/html/FrameSet.zig@@ -23,46 +23,54 @@ pub fn asNode(self: *FrameSet) *Node {// Special-case: the "window-reflecting body element event handler set"// (blur, error, focus, load, resize, scroll) are aliases for the Window's// event handlers, on frameset elements just like on body elements.-pub fn getOnBlur(_: *FrameSet, frame: *Frame) ?js.Function.Global {- return frame.window._on_blur;++// The aliased Window is the one of the element's node document's frame — not+// the caller's frame, which differs when a same-origin script reaches into+// another frame (e.g. the parent setting a handler on a child frameset).+fn reflectedWindow(self: *FrameSet, frame: *Frame) *Window {+ return self.asElement().ownerFrame(frame).window;+}++pub fn getOnBlur(self: *FrameSet, frame: *Frame) ?js.Function.Global {+ return self.reflectedWindow(frame)._on_blur;}-pub fn setOnBlur(_: *FrameSet, setter: ?Window.FunctionSetter, frame: *Frame) !void {- frame.window._on_blur = Window.getFunctionFromSetter(setter);+pub fn setOnBlur(self: *FrameSet, setter: ?Window.FunctionSetter, frame: *Frame) !void {+ self.reflectedWindow(frame)._on_blur = Window.getFunctionFromSetter(setter);}-pub fn getOnError(_: *FrameSet, frame: *Frame) ?js.Function.Global {- return frame.window._on_error;+pub fn getOnError(self: *FrameSet, frame: *Frame) ?js.Function.Global {+ return self.reflectedWindow(frame)._on_error;}-pub fn setOnError(_: *FrameSet, setter: ?Window.FunctionSetter, frame: *Frame) !void {- frame.window._on_error = Window.getFunctionFromSetter(setter);+pub fn setOnError(self: *FrameSet, setter: ?Window.FunctionSetter, frame: *Frame) !void {+ self.reflectedWindow(frame)._on_error = Window.getFunctionFromSetter(setter);}-pub fn getOnFocus(_: *FrameSet, frame: *Frame) ?js.Function.Global {- return frame.window._on_focus;+pub fn getOnFocus(self: *FrameSet, frame: *Frame) ?js.Function.Global {+ return self.reflectedWindow(frame)._on_focus;}-pub fn setOnFocus(_: *FrameSet, setter: ?Window.FunctionSetter, frame: *Frame) !void {- frame.window._on_focus = Window.getFunctionFromSetter(setter);+pub fn setOnFocus(self: *FrameSet, setter: ?Window.FunctionSetter, frame: *Frame) !void {+ self.reflectedWindow(frame)._on_focus = Window.getFunctionFromSetter(setter);}-pub fn getOnLoad(_: *FrameSet, frame: *Frame) ?js.Function.Global {- return frame.window._on_load;+pub fn getOnLoad(self: *FrameSet, frame: *Frame) ?js.Function.Global {+ return self.reflectedWindow(frame)._on_load;}-pub fn setOnLoad(_: *FrameSet, setter: ?Window.FunctionSetter, frame: *Frame) !void {- frame.window._on_load = Window.getFunctionFromSetter(setter);+pub fn setOnLoad(self: *FrameSet, setter: ?Window.FunctionSetter, frame: *Frame) !void {+ self.reflectedWindow(frame)._on_load = Window.getFunctionFromSetter(setter);}-pub fn getOnResize(_: *FrameSet, frame: *Frame) ?js.Function.Global {- return frame.window._on_resize;+pub fn getOnResize(self: *FrameSet, frame: *Frame) ?js.Function.Global {+ return self.reflectedWindow(frame)._on_resize;}-pub fn setOnResize(_: *FrameSet, setter: ?Window.FunctionSetter, frame: *Frame) !void {- frame.window._on_resize = Window.getFunctionFromSetter(setter);+pub fn setOnResize(self: *FrameSet, setter: ?Window.FunctionSetter, frame: *Frame) !void {+ self.reflectedWindow(frame)._on_resize = Window.getFunctionFromSetter(setter);}-pub fn getOnScroll(_: *FrameSet, frame: *Frame) ?js.Function.Global {- return frame.window._on_scroll;+pub fn getOnScroll(self: *FrameSet, frame: *Frame) ?js.Function.Global {+ return self.reflectedWindow(frame)._on_scroll;}-pub fn setOnScroll(_: *FrameSet, setter: ?Window.FunctionSetter, frame: *Frame) !void {- frame.window._on_scroll = Window.getFunctionFromSetter(setter);+pub fn setOnScroll(self: *FrameSet, setter: ?Window.FunctionSetter, frame: *Frame) !void {+ self.reflectedWindow(frame)._on_scroll = Window.getFunctionFromSetter(setter);}pub fn getCols(self: *FrameSet) []const u8 {@@ -108,19 +116,22 @@ pub const Build = struct {pub fn complete(node: *Node, frame: *Frame) !void {const el = node.as(Element);+ const owner = node.ownerFrame(frame);inline for (window_reflecting_attributes) |attr| {if (el.getAttributeSafe(comptime .wrap(attr))) |value| {- frame.window.setWindowReflectingHandlerFromAttribute(comptime .wrap(attr), value, frame);+ owner.window.setWindowReflectingHandlerFromAttribute(comptime .wrap(attr), value, owner);}}}- pub fn attributeChange(_: *Element, name: String, value: String, frame: *Frame) !void {- frame.window.setWindowReflectingHandlerFromAttribute(name, value.str(), frame);+ pub fn attributeChange(el: *Element, name: String, value: String, frame: *Frame) !void {+ const owner = el.ownerFrame(frame);+ owner.window.setWindowReflectingHandlerFromAttribute(name, value.str(), owner);}- pub fn attributeRemove(_: *Element, name: String, frame: *Frame) !void {- frame.window.setWindowReflectingHandlerFromAttribute(name, null, frame);+ pub fn attributeRemove(el: *Element, name: String, frame: *Frame) !void {+ const owner = el.ownerFrame(frame);+ owner.window.setWindowReflectingHandlerFromAttribute(name, null, owner);}};