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): reflect fetch priority hints

ldm0fcac5fef2437
diff --git a/moli-renderer-v8/src/native_bridge/element.rs b/moli-renderer-v8/src/native_bridge/element.rs
index 357da7fc3..1fd0d41a1 100644
--- a/moli-renderer-v8/src/native_bridge/element.rs
+++ b/moli-renderer-v8/src/native_bridge/element.rs
@@ -408,6 +408,7 @@ pub(super) use geometry::{
node_scroll_top_getter_function, node_scroll_top_setter_function,
node_scroll_width_getter_function,
};
+use global_attributes::canonical_fetch_priority_value;
pub(super) use global_attributes::{
anchor_target_getter_function, anchor_target_setter_function, area_no_href_setter_function,
area_target_getter_function, area_target_setter_function, base_target_getter_function,
@@ -2732,6 +2733,114 @@ fn svg_script_type_setter_function<'s>(
rv.set_undefined();
}
+fn svg_fetch_priority_receiver<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ receiver: v8::Local<'s, v8::Object>,
+ interface: &'static str,
+ local_name: &'static str,
+ setter: bool,
+) -> Option<(*mut JsContextHost, DomHandle)> {
+ let Ok((runtime_ptr, handle)) =
+ node_runtime_and_handle_from_object_or_detached(scope, receiver)
+ else {
+ if setter {
+ throw_incompatible_setter_receiver(scope, interface, "fetchPriority");
+ } else {
+ throw_incompatible_getter_receiver(scope, interface, "fetchPriority");
+ }
+ return None;
+ };
+ let is_expected_element = unsafe { &*runtime_ptr }
+ .dom_host()
+ .node(handle)
+ .and_then(crate::dom::native::Node::as_element)
+ .is_some_and(|element| element.is_svg_element(local_name));
+ if !is_expected_element {
+ if setter {
+ throw_incompatible_setter_receiver(scope, interface, "fetchPriority");
+ } else {
+ throw_incompatible_getter_receiver(scope, interface, "fetchPriority");
+ }
+ return None;
+ }
+ Some((runtime_ptr, handle))
+}
+
+fn svg_fetch_priority_getter_function<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'s, v8::Value>,
+ interface: &'static str,
+ local_name: &'static str,
+) {
+ let Some((runtime_ptr, handle)) =
+ svg_fetch_priority_receiver(scope, args.this(), interface, local_name, false)
+ else {
+ return;
+ };
+ let raw = unsafe { &*runtime_ptr }
+ .dom_host()
+ .get_attribute(handle, "fetchpriority")
+ .unwrap_or_default();
+ let Some(value) = v8_string(scope, canonical_fetch_priority_value(&raw)) else {
+ rv.set_empty_string();
+ return;
+ };
+ rv.set(value.into());
+}
+
+fn svg_fetch_priority_setter_function<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ mut rv: v8::ReturnValue<'_, v8::Value>,
+ interface: &'static str,
+ local_name: &'static str,
+) {
+ let Some((runtime_ptr, handle)) =
+ svg_fetch_priority_receiver(scope, args.this(), interface, local_name, true)
+ else {
+ return;
+ };
+ let Some(value) = property_dom_string_value(scope, args.get(0), interface, "fetchPriority")
+ else {
+ return;
+ };
+ set_reflected_attribute(scope, runtime_ptr, handle, "fetchpriority", &value);
+ rv.set_undefined();
+}
+
+fn svg_image_fetch_priority_getter_function<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ svg_fetch_priority_getter_function(scope, args, rv, "SVGImageElement", "image");
+}
+
+fn svg_image_fetch_priority_setter_function<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ rv: v8::ReturnValue<'_, v8::Value>,
+) {
+ svg_fetch_priority_setter_function(scope, args, rv, "SVGImageElement", "image");
+}
+
+fn svg_script_fetch_priority_getter_function<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ rv: v8::ReturnValue<'s, v8::Value>,
+) {
+ svg_fetch_priority_getter_function(scope, args, rv, "SVGScriptElement", "script");
+}
+
+fn svg_script_fetch_priority_setter_function<'s>(
+ scope: &mut v8::PinScope<'s, '_>,
+ args: v8::FunctionCallbackArguments<'s>,
+ rv: v8::ReturnValue<'_, v8::Value>,
+) {
+ svg_fetch_priority_setter_function(scope, args, rv, "SVGScriptElement", "script");
+}
+
fn node_nonce_getter_function<'s>(
scope: &mut v8::PinScope<'s, '_>,
args: v8::FunctionCallbackArguments<'s>,
@@ -3499,6 +3608,12 @@ struct HtmlScriptElementPrototypeDeclaration {
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::SVGScriptElement, enumerable)]
struct SvgScriptElementPrototypeDeclaration {
+ #[webapi(
+ accessor_property = "fetchPriority",
+ getter = svg_script_fetch_priority_getter_function,
+ setter = svg_script_fetch_priority_setter_function
+ )]
+ fetch_priority: (),
#[webapi(
accessor_property = "type",
getter = script_type_getter_function,
@@ -3513,6 +3628,17 @@ struct SvgScriptElementPrototypeDeclaration {
r#async: (),
}
+#[derive(WebApiFunctionTemplate)]
+#[webapi(interface = web_api_interfaces::SVGImageElement, enumerable)]
+struct SvgImageElementPrototypeDeclaration {
+ #[webapi(
+ accessor_property = "fetchPriority",
+ getter = svg_image_fetch_priority_getter_function,
+ setter = svg_image_fetch_priority_setter_function
+ )]
+ fetch_priority: (),
+}
+
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::HTMLAnchorElement, enumerable)]
struct HtmlAnchorElementUrlPrototypeDeclaration {
@@ -7071,6 +7197,7 @@ pub(crate) fn install_element_template_bindings<'s>(
"HTMLMarqueeElement" => install!(HtmlMarqueeElementLegacyPrototypeDeclaration),
"HTMLScriptElement" => install!(HtmlScriptElementPrototypeDeclaration),
"SVGScriptElement" => install!(SvgScriptElementPrototypeDeclaration),
+ "SVGImageElement" => install!(SvgImageElementPrototypeDeclaration),
"HTMLStyleElement" => install!(HtmlStyleElementPrototypeDeclaration),
"SVGStyleElement" => install!(SvgStyleElementPrototypeDeclaration),
"HTMLTableElement" => install!(HtmlTableElementPrototypeDeclaration),
diff --git a/moli-renderer-v8/src/native_bridge/element/global_attributes.rs b/moli-renderer-v8/src/native_bridge/element/global_attributes.rs
index 545dad98e..0b57d00bf 100644
--- a/moli-renderer-v8/src/native_bridge/element/global_attributes.rs
+++ b/moli-renderer-v8/src/native_bridge/element/global_attributes.rs
@@ -1672,7 +1672,7 @@ pub(in crate::native_bridge) fn canonical_preload_value(raw: &str) -> &'static s
}
}
-fn canonical_fetch_priority_value(raw: &str) -> &'static str {
+pub(super) fn canonical_fetch_priority_value(raw: &str) -> &'static str {
if raw.eq_ignore_ascii_case("low") {
"low"
} else if raw.eq_ignore_ascii_case("high") {
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 ad892814c..cc6917d13 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
@@ -4886,10 +4886,13 @@ fn fetch_priority_reflection_is_shared_by_supported_elements() {
return error instanceof TypeError;
}
};
+ const svgNamespace = "http://www.w3.org/2000/svg";
const cases = [
[HTMLImageElement.prototype, document.createElement("img"), "img"],
[HTMLLinkElement.prototype, document.createElement("link"), "link"],
- [HTMLScriptElement.prototype, document.createElement("script"), "script"]
+ [HTMLScriptElement.prototype, document.createElement("script"), "script"],
+ [SVGImageElement.prototype, document.createElementNS(svgNamespace, "image"), "svg image"],
+ [SVGScriptElement.prototype, document.createElementNS(svgNamespace, "script"), "svg script"]
];
for (const [prototype, element, label] of cases) {