Skip to content

armgit / moli

Public Imported from GitHub
0

Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust

fix(svg): expose the owning SVG fragment root

ldm04b11d4429e5c
diff --git a/moli-renderer-v8/src/context_bootstrap/svg_runtime/bindings.rs b/moli-renderer-v8/src/context_bootstrap/svg_runtime/bindings.rs
index ec110a308..2e17dcbc6 100644
--- a/moli-renderer-v8/src/context_bootstrap/svg_runtime/bindings.rs
+++ b/moli-renderer-v8/src/context_bootstrap/svg_runtime/bindings.rs
@@ -737,6 +737,16 @@ struct SvgMatrixTemplateAccessorsDeclaration {
f: (),
}
+#[derive(WebApiFunctionTemplate)]
+#[webapi(interface = web_api_interfaces::SVGElement, enumerable, receiver)]
+struct SvgElementPrototypeAccessorsDeclaration {
+ #[webapi(
+ accessor_property = "ownerSVGElement",
+ getter = svg_element_owner_svg_element_getter
+ )]
+ owner_svg_element: (),
+}
+
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::SVGGraphicsElement, enumerable)]
struct SvgGraphicsElementPrototypeAccessorsDeclaration {
@@ -1045,6 +1055,11 @@ pub(super) fn install_svg_element_accessor_bindings<'s>(
) {
let prototype = template.prototype_template(scope);
match interface_name {
+ "SVGElement" => {
+ SvgElementPrototypeAccessorsDeclaration::initialize_prototype_template(
+ scope, prototype,
+ );
+ }
"SVGGraphicsElement" => {
SvgGraphicsElementPrototypeAccessorsDeclaration::initialize_prototype_template(
scope, prototype,
diff --git a/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs b/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs
index 25810e58d..69746d477 100644
--- a/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs
+++ b/moli-renderer-v8/src/context_bootstrap/svg_runtime/callbacks.rs
@@ -48,6 +48,76 @@ fn require_svg_receiver<'s>(
false
}
+pub(super) fn svg_element_owner_svg_element_getter<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ let Ok((runtime_ptr, handle)) =
+ crate::native_bridge::node_runtime_and_handle_from_object_or_detached(scope, args.this())
+ else {
+ webidl::throw_type_error(
+ scope,
+ "SVGElement.ownerSVGElement called on incompatible receiver.",
+ );
+ return;
+ };
+ let owner_handle = {
+ let runtime = unsafe { &*runtime_ptr };
+ let Some(node) = runtime.dom_host().node(handle) else {
+ rv.set_null();
+ return;
+ };
+ if node.namespace() != Some(crate::native_bridge::document::SVG_NS) {
+ webidl::throw_type_error(
+ scope,
+ "SVGElement.ownerSVGElement called on incompatible receiver.",
+ );
+ return;
+ }
+
+ // Only an outermost <svg> has the fragment-boundary exception.
+ // Other SVG elements still resolve their nearest ancestor <svg>.
+ if node.local_name() == Some("svg")
+ && node
+ .parent_node_id()
+ .and_then(|parent| runtime.dom_host().node(parent))
+ .is_none_or(|parent| {
+ parent.namespace() != Some(crate::native_bridge::document::SVG_NS)
+ || parent.local_name() == Some("foreignObject")
+ })
+ {
+ rv.set_null();
+ return;
+ }
+ let mut current = node.parent_node_id();
+ let mut owner = None;
+ while let Some(candidate) = current {
+ let Some(ancestor) = runtime.dom_host().node(candidate) else {
+ break;
+ };
+ if ancestor.namespace() == Some(crate::native_bridge::document::SVG_NS)
+ && ancestor.local_name() == Some("svg")
+ {
+ owner = Some(candidate);
+ break;
+ }
+ current = ancestor
+ .parent_node_id()
+ .or_else(|| runtime.dom_host().shadow_root_host(candidate));
+ }
+ owner
+ };
+
+ let Some(owner) = owner_handle.and_then(|owner| {
+ crate::native_bridge::document::detached_native_object_for_handle(scope, runtime_ptr, owner)
+ }) else {
+ rv.set_null();
+ return;
+ };
+ rv.set(owner.into());
+}
+
pub(super) fn svg_rect_animated_length_getter<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rs
index cc6917d13..2227becb4 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document.rs
@@ -1264,6 +1264,96 @@ fn svg_specialized_accessors_live_on_owner_prototypes() {
assert_eq!(result, "ok");
}
+#[test]
+fn svg_owner_svg_element_tracks_the_nearest_svg_fragment_root() {
+ let mut vm = new_parsed_test_vm(
+ "https://svg-owner-element.test/",
+ r#"<!doctype html><html><body><div id="container">
+ <svg id="outer">
+ <g><circle id="circle"></circle><svg id="inner"><rect id="rect"></rect></svg></g>
+ <foreignObject><svg id="foreign-svg"><svg id="foreign-inner"></svg></svg></foreignObject>
+ </svg>
+ </div></body></html>"#,
+ );
+
+ let result = vm
+ .eval(
+ r##"
+ (() => {
+ const assert = (condition, message) => {
+ if (!condition) throw new Error(message);
+ };
+ const ns = "http://www.w3.org/2000/svg";
+ const outer = document.querySelector("#outer");
+ const circle = document.querySelector("#circle");
+ const inner = document.querySelector("#inner");
+ const rect = document.querySelector("#rect");
+ const foreignSvg = document.querySelector("#foreign-svg");
+ const foreignInner = document.querySelector("#foreign-inner");
+
+ assert(outer.ownerSVGElement === null, "outer SVG");
+ assert(circle.ownerSVGElement === outer, "descendant of outer SVG");
+ assert(inner.ownerSVGElement === outer, "nested SVG");
+ assert(rect.ownerSVGElement === inner, "descendant of nested SVG");
+ assert(foreignSvg.ownerSVGElement === null, "foreignObject starts a new SVG fragment");
+ assert(foreignInner.ownerSVGElement === foreignSvg, "nested foreignObject SVG");
+
+ const foreignObject = foreignSvg.parentNode;
+ const foreignCircle = foreignObject.appendChild(document.createElementNS(ns, "circle"));
+ const foreignGroup = foreignObject.appendChild(document.createElementNS(ns, "g"));
+ const groupedSvg = foreignGroup.appendChild(document.createElementNS(ns, "svg"));
+ const htmlParent = foreignObject.appendChild(document.createElement("div"));
+ const htmlCircle = htmlParent.appendChild(document.createElementNS(ns, "circle"));
+ const htmlSvg = htmlParent.appendChild(document.createElementNS(ns, "svg"));
+ const shadow = htmlParent.attachShadow({mode: "open"});
+ const shadowCircle = shadow.appendChild(document.createElementNS(ns, "circle"));
+ const shadowSvg = shadow.appendChild(document.createElementNS(ns, "svg"));
+ assert(foreignCircle.ownerSVGElement === outer, "non-root SVG crosses foreignObject");
+ assert(groupedSvg.ownerSVGElement === outer, "only direct foreignObject roots are outermost");
+ assert(htmlCircle.ownerSVGElement === outer, "non-root SVG crosses HTML ancestors");
+ assert(htmlSvg.ownerSVGElement === null, "SVG with HTML parent is outermost");
+ assert(shadowCircle.ownerSVGElement === outer, "non-root SVG resolves through a shadow host");
+ assert(shadowSvg.ownerSVGElement === null, "SVG with a shadow root parent is outermost");
+
+ document.querySelector("#container").remove();
+ assert(circle.ownerSVGElement === outer, "detached outer SVG descendant");
+ assert(inner.ownerSVGElement === outer, "detached nested SVG");
+ assert(rect.ownerSVGElement === inner, "detached nested SVG descendant");
+ assert(htmlCircle.ownerSVGElement === outer, "detached non-root SVG crosses HTML ancestors");
+ assert(shadowCircle.ownerSVGElement === outer, "detached shadow SVG descendant");
+
+ const standalone = document.createElementNS(ns, "ellipse");
+ assert(standalone.ownerSVGElement === null, "standalone SVG element");
+
+ const svgDocument = document.implementation.createDocument(ns, "svg", null);
+ const documentRoot = svgDocument.documentElement;
+ const documentRect = svgDocument.createElementNS(ns, "rect");
+ documentRoot.append(documentRect);
+ assert(documentRoot.ownerSVGElement === null, "SVG document root");
+ assert(documentRect.ownerSVGElement === documentRoot, "SVG document child");
+
+ const descriptor = Object.getOwnPropertyDescriptor(
+ SVGElement.prototype,
+ "ownerSVGElement",
+ );
+ assert(typeof descriptor.get === "function", "prototype getter");
+ assert(descriptor.enumerable && descriptor.configurable, "getter flags");
+ let incompatibleReceiver = false;
+ try {
+ descriptor.get.call(document.body);
+ } catch (error) {
+ incompatibleReceiver = error instanceof TypeError;
+ }
+ assert(incompatibleReceiver, "incompatible receiver");
+ return "ok";
+ })()
+ "##,
+ )
+ .expect("SVG ownerSVGElement probe should evaluate");
+
+ assert_eq!(result, "ok");
+}
+
#[test]
fn svg_geometry_queries_use_computed_paths_live_tree_and_kurbo_bounds() {
let mut vm = new_parsed_test_vm(