fix(dom): complete tabIndex defaults

This commit is contained in:
ldm0
2026-11-24 14:28:14 +08:00
parent 985b1c5f43
commit eabc726f89
3 changed files with 142 additions and 31 deletions
@@ -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;
@@ -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::<Vec<_>>();
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
}
@@ -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 = `
<area id="outside" href="#" autofocus>
<map name="unused"><area id="unreferenced" href="#" autofocus></map>
<map name="active">
<area id="no-href" autofocus>
<area id="target" href="#" autofocus>
</map>
<img usemap="#active">`;
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/");