diff --git a/moli-renderer-v8/src/native_bridge/element.rs b/moli-renderer-v8/src/native_bridge/element.rs index 5a26211e8cd..004724169ff 100644 --- a/moli-renderer-v8/src/native_bridge/element.rs +++ b/moli-renderer-v8/src/native_bridge/element.rs @@ -469,6 +469,7 @@ pub(crate) use forms::{ }; use rendered_state::{node_check_visibility_callback, node_current_css_zoom_getter_function}; +use form_controls::SvgAElementRelListPrototypeDeclaration; pub use geometry::ClientRect; #[cfg(feature = "devtools")] pub(crate) use geometry::observable_input_hit_test; diff --git a/moli-renderer-v8/src/native_bridge/element/focus.rs b/moli-renderer-v8/src/native_bridge/element/focus.rs index 3d741730d7e..c605455912e 100644 --- a/moli-renderer-v8/src/native_bridge/element/focus.rs +++ b/moli-renderer-v8/src/native_bridge/element/focus.rs @@ -14,10 +14,7 @@ use super::{ dispatch_popover_show_events, dispatch_public_event, resolved_reflected_element_attribute_handle, }; -use super::{ - element_attribute, element_has_attribute, label_control_handle, - label_receives_programmatic_focus, -}; +use super::{element_attribute, label_control_handle, label_receives_programmatic_focus}; use crate::document_runtime::{DomHandle, EventTargetHandle}; #[cfg(feature = "devtools")] use crate::runtime::RendererDomFocusOutcome; @@ -177,6 +174,58 @@ pub(crate) fn initial_contenteditable_caret( (target, 0) } +fn html_area_has_associated_image_map(runtime: &PageScriptHost, handle: DomHandle) -> bool { + let dom = runtime.dom_host(); + let Some(area) = dom.node(handle).and_then(Node::as_element) else { + return false; + }; + if area.namespace() != document::XHTML_NS + || area.local_name() != "area" + || !area.has_attribute("href") + { + return false; + } + + let mut ancestor = dom.parent_node(handle); + let map_name = loop { + let Some(candidate) = ancestor else { + return false; + }; + let Some(element) = dom.node(candidate).and_then(Node::as_element) else { + ancestor = dom.parent_node(candidate); + continue; + }; + if element.namespace() == document::XHTML_NS && element.local_name() == "map" { + let Some(name) = element.attribute("name").filter(|name| !name.is_empty()) else { + return false; + }; + break name.to_owned(); + } + ancestor = dom.parent_node(candidate); + }; + + let mut stack = dom + .child_handles(runtime.document_handle()) + .collect::>(); + while let Some(candidate) = stack.pop() { + let Some(element) = dom.node(candidate).and_then(Node::as_element) else { + stack.extend(dom.child_handles(candidate)); + continue; + }; + if element.namespace() == document::XHTML_NS + && matches!(element.local_name(), "img" | "object") + && element + .attribute("usemap") + .and_then(|value| value.strip_prefix('#')) + .is_some_and(|name| name == map_name) + { + return true; + } + stack.extend(dom.child_handles(candidate)); + } + false +} + pub(super) fn is_focusable(runtime: &PageScriptHost, handle: DomHandle) -> bool { let dom = runtime.dom_host().dom(); if !dom.node(handle).is_some_and(Node::is_connected) @@ -184,6 +233,14 @@ pub(super) fn is_focusable(runtime: &PageScriptHost, handle: DomHandle) -> bool { return false; } + if dom + .node(handle) + .and_then(Node::as_element) + .is_some_and(|element| element.is_html_element("area")) + && !html_area_has_associated_image_map(runtime, handle) + { + return false; + } match dom.focus_support(handle) { FocusSupport::Blocked => false, FocusSupport::Supported => true, @@ -355,33 +412,6 @@ pub(crate) fn process_post_parse_autofocus( true } -fn focused_chain_requires_async_blur(runtime: &PageScriptHost, active: DomHandle) -> bool { - let mut current = Some(active); - while let Some(handle) = current { - if element_has_attribute(runtime, handle, "inert") - || element_has_attribute(runtime, handle, "hidden") - { - return true; - } - if style_property_value(runtime, handle, StyleMode::Computed, "display") - .eq_ignore_ascii_case("none") - { - return true; - } - current = runtime - .dom_host() - .node(handle) - .and_then(Node::parent_node) - .or_else(|| { - runtime - .dom_host() - .containing_shadow_root(handle) - .and_then(|root| runtime.dom_host().shadow_root_host(root)) - }); - } - false -} - fn blur_if_focused_chain_is_display_none_callback( scope: &mut v8::PinScope<'_, '_>, _args: v8::FunctionCallbackArguments<'_>, @@ -1526,3 +1556,30 @@ pub(in crate::native_bridge) fn node_blur_callback<'s>( } rv.set_undefined(); } + +fn focused_chain_requires_async_blur(runtime: &PageScriptHost, active: DomHandle) -> bool { + let mut current = Some(active); + while let Some(handle) = current { + if super::element_has_attribute(runtime, handle, "inert") + || super::element_has_attribute(runtime, handle, "hidden") + { + return true; + } + if style_property_value(runtime, handle, StyleMode::Computed, "display") + .eq_ignore_ascii_case("none") + { + return true; + } + current = runtime + .dom_host() + .node(handle) + .and_then(Node::parent_node) + .or_else(|| { + runtime + .dom_host() + .containing_shadow_root(handle) + .and_then(|root| runtime.dom_host().shadow_root_host(root)) + }); + } + false +} diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/focus.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/focus.rs index c39de6ad54c..9eaadeadf1c 100644 --- a/moli-renderer-v8/src/script_vm/tests/dom_elements/focus.rs +++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/focus.rs @@ -26,6 +26,59 @@ fn editing_focus_initializes_selection_before_events_and_preserves_existing_rang ); } +#[test] +fn image_map_area_focusability_supports_autofocus() { + let mut vm = new_storage_test_vm("https://area-autofocus.test/"); + + let setup = vm + .eval( + r##" +(() => { + const root = document.documentElement || + document.appendChild(document.createElement('html')); + const body = document.body || root.appendChild(document.createElement('body')); + body.innerHTML = ` + + + + + + + `; + + const rejected = ['outside', 'unreferenced', 'no-href'].map(id => { + const candidate = document.getElementById(id); + candidate.focus(); + return document.activeElement !== candidate; + }); + return rejected.every(Boolean) && document.activeElement === body; +})() +"##, + ) + .expect("image-map focusability fixture should initialize"); + assert_eq!(setup, "true"); + + vm.with_default_context_scope_and_checkpoint_for_test(|scope, runtime_ptr| { + assert!( + crate::native_bridge::element::post_parse_autofocus_is_pending(unsafe { + &*runtime_ptr + }) + ); + assert!(crate::native_bridge::element::process_post_parse_autofocus( + scope, + runtime_ptr + )); + Ok(()) + }) + .expect("image-map area autofocus should run"); + + assert_eq!( + vm.eval("document.activeElement === document.getElementById('target')") + .expect("image-map autofocus result should remain observable"), + "true" + ); +} + #[test] fn editing_focus_uses_native_selection_boundaries_without_author_accessors() { let mut vm = new_storage_html_test_vm("https://example.com/");