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): load image href resources

ldm066fd71f48296
diff --git a/moli-dom/src/native/element/mod.rs b/moli-dom/src/native/element/mod.rs
index a8a3f6543..b8d2e67fc 100644
--- a/moli-dom/src/native/element/mod.rs
+++ b/moli-dom/src/native/element/mod.rs
@@ -240,6 +240,14 @@ impl Element {
self.namespace() == "http://www.w3.org/1999/xhtml" && self.local_name() == local_name
}
+ pub fn is_svg_element(&self, local_name: &str) -> bool {
+ self.namespace() == "http://www.w3.org/2000/svg" && self.local_name() == local_name
+ }
+
+ pub fn is_image_resource_element(&self) -> bool {
+ self.is_html_element("img") || self.is_svg_element("image")
+ }
+
pub fn is_inline_style_element(&self) -> bool {
self.local_name() == "style"
&& matches!(
diff --git a/moli-renderer-v8/src/document_runtime/dom_facade.rs b/moli-renderer-v8/src/document_runtime/dom_facade.rs
index 08d1e4e74..c46b8917d 100644
--- a/moli-renderer-v8/src/document_runtime/dom_facade.rs
+++ b/moli-renderer-v8/src/document_runtime/dom_facade.rs
@@ -800,7 +800,12 @@ impl DocumentRuntime {
let mut stack = vec![root];
let mut images = Vec::new();
while let Some(handle) = stack.pop() {
- if self.dom_host.is_html_element_named(handle, "img") {
+ if self
+ .dom_host
+ .node(handle)
+ .and_then(Node::as_element)
+ .is_some_and(|element| element.is_image_resource_element())
+ {
images.push(handle);
}
if let Some(shadow_root) = self.dom_host.shadow_root_handle(handle) {
diff --git a/moli-renderer-v8/src/document_runtime/lifecycle.rs b/moli-renderer-v8/src/document_runtime/lifecycle.rs
index 9efb963de..cfe7535c4 100644
--- a/moli-renderer-v8/src/document_runtime/lifecycle.rs
+++ b/moli-renderer-v8/src/document_runtime/lifecycle.rs
@@ -513,7 +513,7 @@ impl DocumentRuntime {
let Some(element) = node.as_element() else {
continue;
};
- if !element.is_html_element("img") || element.image_load_dispatched() {
+ if !element.is_image_resource_element() || element.image_load_dispatched() {
continue;
}
native_bridge::element::queue_image_load_event_if_needed_with_initiator(
diff --git a/moli-renderer-v8/src/document_runtime/mutation_commands/tree/resources.rs b/moli-renderer-v8/src/document_runtime/mutation_commands/tree/resources.rs
index 9e9128d38..806b2d0c1 100644
--- a/moli-renderer-v8/src/document_runtime/mutation_commands/tree/resources.rs
+++ b/moli-renderer-v8/src/document_runtime/mutation_commands/tree/resources.rs
@@ -87,7 +87,12 @@ impl DocumentRuntime {
if let [root] = roots
&& self.dom_host.first_child(*root).is_none()
{
- if self.dom_host.is_html_element_named(*root, "img") {
+ if self
+ .dom_host
+ .node(*root)
+ .and_then(Node::as_element)
+ .is_some_and(|element| element.is_image_resource_element())
+ {
crate::native_bridge::element::queue_image_load_event_if_needed_with_initiator(
scope,
host_ptr,
@@ -104,7 +109,7 @@ impl DocumentRuntime {
.dom_host
.node(handle)
.and_then(Node::as_element)
- .is_some_and(|element| element.is_html_element("img"))
+ .is_some_and(|element| element.is_image_resource_element())
{
images.push(handle);
}
@@ -127,9 +132,11 @@ impl DocumentRuntime {
plan.node_count += 1;
if let Some(element) = self.dom_host.node(handle).and_then(Node::as_element) {
plan.details.observe_element(handle, element);
+ if element.is_image_resource_element() {
+ plan.may_have_images = true;
+ }
match element.local_name() {
"img" => {
- plan.may_have_images = true;
plan.may_have_image_relevant_picture_source = true;
}
"source" => {
@@ -293,7 +300,12 @@ impl DocumentRuntime {
let mut handles = Vec::new();
self.collect_subtree_handles_preorder(removal_plan.root, &mut handles);
for handle in handles {
- if self.dom_host.is_html_element_named(handle, "img") {
+ if self
+ .dom_host
+ .node(handle)
+ .and_then(Node::as_element)
+ .is_some_and(|element| element.is_image_resource_element())
+ {
crate::native_bridge::element::queue_image_load_event_if_needed(
scope, host_ptr, handle,
);
diff --git a/moli-renderer-v8/src/native_bridge/context_host/child_documents/lifecycle.rs b/moli-renderer-v8/src/native_bridge/context_host/child_documents/lifecycle.rs
index 22360a5e8..64b4c0b06 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/child_documents/lifecycle.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/child_documents/lifecycle.rs
@@ -121,7 +121,7 @@ impl JsContextHost {
.dom_host()
.node(*handle)
.and_then(crate::dom::native::Node::as_element)
- .is_some_and(|element| element.is_html_element("img"))
+ .is_some_and(|element| element.is_image_resource_element())
})
.collect::<Vec<_>>();
let host_ptr = self as *mut JsContextHost;
diff --git a/moli-renderer-v8/src/native_bridge/context_host/image_loads.rs b/moli-renderer-v8/src/native_bridge/context_host/image_loads.rs
index 71ee35c6b..e9b5e1a28 100644
--- a/moli-renderer-v8/src/native_bridge/context_host/image_loads.rs
+++ b/moli-renderer-v8/src/native_bridge/context_host/image_loads.rs
@@ -37,7 +37,7 @@ impl JsContextHost {
.node(image_handle)
.filter(|node| {
node.as_element()
- .is_some_and(|element| element.is_html_element("img"))
+ .is_some_and(|element| element.is_image_resource_element())
})
.and_then(Node::owner_document)?;
let is_main_document = owner_document_handle == self.document_handle();
diff --git a/moli-renderer-v8/src/native_bridge/element/images/src.rs b/moli-renderer-v8/src/native_bridge/element/images/src.rs
index 96c964965..521b832f1 100644
--- a/moli-renderer-v8/src/native_bridge/element/images/src.rs
+++ b/moli-renderer-v8/src/native_bridge/element/images/src.rs
@@ -282,6 +282,17 @@ fn image_selected_source_candidate(
) -> Option<SelectedImageSource> {
let node = runtime.dom_host().node(handle)?;
let element = node.as_element()?;
+ if element.is_svg_element("image") {
+ return element
+ .attribute("href")
+ .or_else(|| element.attribute("xlink:href"))
+ .map(str::trim)
+ .filter(|href| !href.is_empty())
+ .map(|href| SelectedImageSource {
+ url: href.to_owned(),
+ density: 1.0,
+ });
+ }
if !element.is_html_element("img") {
return None;
}
@@ -454,6 +465,16 @@ pub(crate) fn plan_image_attribute_mutation(
targets: reloads_image.then_some(handle).into_iter().collect(),
};
}
+ if element.is_svg_element("image") {
+ let reloads_image = name.eq_ignore_ascii_case("href")
+ || (name.eq_ignore_ascii_case("crossorigin")
+ && ImageRequestCorsMode::from_cross_origin_attribute(
+ element.attribute("crossorigin"),
+ ) != ImageRequestCorsMode::from_cross_origin_attribute(next_value));
+ return ImageAttributeMutationPlan {
+ targets: reloads_image.then_some(handle).into_iter().collect(),
+ };
+ }
if !element.is_html_element("source")
|| (!name.eq_ignore_ascii_case("srcset")
&& !name.eq_ignore_ascii_case("sizes")
diff --git a/moli-renderer-v8/src/network_host/image.rs b/moli-renderer-v8/src/network_host/image.rs
index f85457d49..c1e0f11bb 100644
--- a/moli-renderer-v8/src/network_host/image.rs
+++ b/moli-renderer-v8/src/network_host/image.rs
@@ -199,7 +199,7 @@ pub(crate) fn start_image_element_resource_fetch(
.dom_host()
.node(image_handle)
.and_then(crate::dom::native::Node::as_element)
- .filter(|element| element.is_html_element("img"))
+ .filter(|element| element.is_image_resource_element())
.ok_or_else(|| "image element is unavailable".to_owned())?;
let request_initiator_type = pending.request_initiator_type();
let (request_mode, credentials_mode) = image_cross_origin_request_modes(element);