Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(dom): complete the default tabindex element set

ldm001da91de89c3
diff --git a/moli-renderer-v8/src/native_bridge/document.rs b/moli-renderer-v8/src/native_bridge/document.rs
index db87be835..cada1e00b 100644
--- a/moli-renderer-v8/src/native_bridge/document.rs
+++ b/moli-renderer-v8/src/native_bridge/document.rs
@@ -297,6 +297,7 @@ pub(in crate::native_bridge::document) use structure::set_document_body_for_nati
pub(crate) const XHTML_NS: &str = "http://www.w3.org/1999/xhtml";
pub(crate) const SVG_NS: &str = "http://www.w3.org/2000/svg";
+pub(crate) const MATHML_NS: &str = "http://www.w3.org/1998/Math/MathML";
#[derive(WebApiFunctionTemplate)]
#[webapi(interface = web_api_interfaces::Document, enumerable, receiver)]
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 71de5f873..4c3ecf46b 100644
--- a/moli-renderer-v8/src/native_bridge/element/global_attributes.rs
+++ b/moli-renderer-v8/src/native_bridge/element/global_attributes.rs
@@ -1,11 +1,11 @@
use crate::document_runtime::DomHandle;
use crate::dom::{forms::InputType, native::Node};
-use crate::native_bridge::document::XHTML_NS;
use crate::util::v8_string;
use crate::webidl;
use super::super::{
JsContextHost,
+ document::{MATHML_NS, SVG_NS, XHTML_NS},
node::{
node_is_element, node_runtime_and_handle_from_object_or_detached,
throw_incompatible_getter_receiver, throw_incompatible_setter_receiver,
@@ -13,6 +13,7 @@ use super::super::{
throw_dom_exception,
};
+use super::details_dialog::main_summary_child;
use super::reflection::{
DomStringReflection, ElementReflectionInterface, NullToEmptyDomStringReflection,
UnsignedLongReflection, UsvStringReflection, remove_reflected_attribute,
@@ -2590,11 +2591,21 @@ fn default_tab_index_for(runtime: &JsContextHost, handle: DomHandle) -> i32 {
let Some(element) = runtime.dom_host().node(handle).and_then(Node::as_element) else {
return -1;
};
- if element.namespace() != "http://www.w3.org/1999/xhtml" {
- return -1;
- }
- match element.local_name() {
- "a" | "button" | "input" | "select" | "textarea" => 0,
+ match (element.namespace(), element.local_name()) {
+ (
+ XHTML_NS,
+ "a" | "area" | "button" | "frame" | "iframe" | "input" | "object" | "select"
+ | "textarea",
+ )
+ | (SVG_NS | MATHML_NS, "a") => 0,
+ (XHTML_NS, "summary")
+ if runtime
+ .dom_host()
+ .parent_node(handle)
+ .is_some_and(|parent| main_summary_child(runtime, parent) == Some(handle)) =>
+ {
+ 0
+ }
_ => -1,
}
}
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document/document_bindings.rs b/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document/document_bindings.rs
index 61a2c52b2..ac9c5d13b 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document/document_bindings.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_elements/live_document/document_bindings.rs
@@ -1195,3 +1195,82 @@ fn geometry_accessors_live_on_owner_prototypes() {
assert_eq!(result, "ok");
}
+
+#[test]
+fn tab_index_getter_uses_the_spec_default_element_set() {
+ let mut vm = new_parsed_test_vm(
+ "https://tab-index-defaults.test/",
+ "<!doctype html><html><head></head><body></body></html>",
+ );
+
+ let result = vm
+ .eval(
+ r#"
+ (() => {
+ const assert = (condition, message) => {
+ if (!condition) throw new Error(message);
+ };
+ const svg = "http://www.w3.org/2000/svg";
+ const mathml = "http://www.w3.org/1998/Math/MathML";
+ const other = "https://tab-index-defaults.test/namespace";
+ const exercise = ownerDocument => {
+ for (const name of [
+ "a", "area", "button", "frame", "iframe", "input", "object", "select", "textarea"
+ ]) {
+ const element = ownerDocument.createElement(name);
+ assert(element.tabIndex === 0, `${name} default`);
+ }
+
+ const div = ownerDocument.createElement("div");
+ assert(div.tabIndex === -1, "ordinary HTML element default");
+
+ const svgAnchor = ownerDocument.createElementNS(svg, "a");
+ const svgGroup = ownerDocument.createElementNS(svg, "g");
+ const mathAnchor = ownerDocument.createElementNS(mathml, "a");
+ const mathRow = ownerDocument.createElementNS(mathml, "mrow");
+ const foreignAnchor = ownerDocument.createElementNS(other, "a");
+ assert(svgAnchor.tabIndex === 0, "SVG a default");
+ assert(svgGroup.tabIndex === -1, "other SVG element default");
+ assert(mathAnchor.tabIndex === 0, "MathML a default");
+ assert(mathRow.tabIndex === -1, "other MathML element default");
+ assert(foreignAnchor.tabIndex === undefined, "unrelated namespace has no mixin");
+
+ const details = ownerDocument.createElement("details");
+ const nonSummary = ownerDocument.createElement("span");
+ const firstSummary = ownerDocument.createElement("summary");
+ const secondSummary = ownerDocument.createElement("summary");
+ details.append(nonSummary, firstSummary, secondSummary);
+ assert(firstSummary.tabIndex === 0, "first summary child default");
+ assert(secondSummary.tabIndex === -1, "later summary child default");
+ assert(ownerDocument.createElement("summary").tabIndex === -1,
+ "orphan summary default");
+
+ firstSummary.remove();
+ assert(secondSummary.tabIndex === 0, "summary default follows tree mutation");
+ details.append(firstSummary);
+ assert(secondSummary.tabIndex === 0 && firstSummary.tabIndex === -1,
+ "first summary is based on current child order");
+
+ const wrapper = ownerDocument.createElement("div");
+ const nestedSummary = ownerDocument.createElement("summary");
+ wrapper.append(nestedSummary);
+ details.prepend(wrapper);
+ assert(nestedSummary.tabIndex === -1, "nested summary is not a details summary");
+
+ secondSummary.setAttribute("tabindex", "-7");
+ assert(secondSummary.tabIndex === -7, "explicit valid tabindex wins");
+ secondSummary.setAttribute("tabindex", "invalid");
+ assert(secondSummary.tabIndex === 0, "invalid tabindex uses element default");
+ div.setAttribute("tabindex", "invalid");
+ assert(div.tabIndex === -1, "invalid tabindex uses ordinary default");
+ };
+
+ exercise(document);
+ return "ok";
+ })()
+ "#,
+ )
+ .expect("tabIndex default getter behavior should evaluate");
+
+ assert_eq!(result, "ok");
+}