Skip to content

armgit / moli

Public Imported from GitHub
0

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

refactor(focus): reuse the reflected tabindex integer parser

ldm0c3e8c136c0aa
diff --git a/moli-renderer-v8/src/native_bridge/element/focus.rs b/moli-renderer-v8/src/native_bridge/element/focus.rs
index 4a8c7e42d..98dd04283 100644
--- a/moli-renderer-v8/src/native_bridge/element/focus.rs
+++ b/moli-renderer-v8/src/native_bridge/element/focus.rs
@@ -1,3 +1,4 @@
+use super::global_attributes::parse_tab_index_attribute;
use crate::dom::native::Node;
use super::super::{
@@ -952,7 +953,7 @@ fn style_integer_property(
fn slot_focus_scope_tab_index(runtime: &JsContextHost, handle: DomHandle) -> Option<i32> {
let value = element_attribute(runtime, handle, "tabindex")
- .and_then(|value| parse_sequential_tab_index(&value))
+ .and_then(|value| parse_tab_index_attribute(&value))
.unwrap_or(0);
(value >= 0).then_some(value)
}
@@ -980,7 +981,7 @@ fn sequential_tab_index_without_scroll_descendant_check(
handle: DomHandle,
) -> Option<i32> {
let value = element_attribute(runtime, handle, "tabindex")
- .and_then(|value| parse_sequential_tab_index(&value))
+ .and_then(|value| parse_tab_index_attribute(&value))
.unwrap_or_else(|| default_sequential_tab_index(runtime, handle));
(value >= 0).then_some(value)
}
@@ -1031,7 +1032,7 @@ fn is_interactive_sequential_focusable_descendant(
"a" | "button" | "input" | "select" | "textarea"
);
let has_non_negative_tab_index = element_attribute(runtime, handle, "tabindex")
- .and_then(|value| parse_sequential_tab_index(&value))
+ .and_then(|value| parse_tab_index_attribute(&value))
.is_some_and(|value| value >= 0);
(is_interactive_element || has_non_negative_tab_index)
&& sequential_tab_index_without_scroll_descendant_check(runtime, handle).is_some()
@@ -1039,7 +1040,7 @@ fn is_interactive_sequential_focusable_descendant(
fn explicit_negative_tab_index(runtime: &JsContextHost, handle: DomHandle) -> bool {
element_attribute(runtime, handle, "tabindex")
- .and_then(|value| parse_sequential_tab_index(&value))
+ .and_then(|value| parse_tab_index_attribute(&value))
.is_some_and(|value| value < 0)
}
@@ -1057,28 +1058,6 @@ fn default_sequential_tab_index(runtime: &JsContextHost, handle: DomHandle) -> i
}
}
-fn parse_sequential_tab_index(value: &str) -> Option<i32> {
- let value = value.trim_start();
- let mut chars = value.chars();
- let (sign, rest) = match chars.next() {
- Some('+') => (1_i64, chars.as_str()),
- Some('-') => (-1_i64, chars.as_str()),
- Some(_) => (1_i64, value),
- None => return None,
- };
- let digits = rest
- .chars()
- .take_while(|ch| ch.is_ascii_digit())
- .collect::<String>();
- if digits.is_empty() {
- return None;
- }
- digits
- .parse::<i64>()
- .ok()
- .and_then(|value| i32::try_from(sign * value).ok())
-}
-
fn negative_shadow_scope_tab_target(
runtime: &JsContextHost,
active: DomHandle,
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 0b57d00bf..022e76230 100644
--- a/moli-renderer-v8/src/native_bridge/element/global_attributes.rs
+++ b/moli-renderer-v8/src/native_bridge/element/global_attributes.rs
@@ -2531,7 +2531,7 @@ pub(in crate::native_bridge) fn node_tab_index_setter_function<'s>(
rv.set_undefined();
}
-fn parse_tab_index_attribute(value: &str) -> Option<i32> {
+pub(super) fn parse_tab_index_attribute(value: &str) -> Option<i32> {
let value = value.trim_start_matches(|ch: char| ch.is_ascii_whitespace());
let mut chars = value.chars();
let (sign, rest) = match chars.next() {
@@ -2558,6 +2558,10 @@ mod tests {
#[test]
fn parses_tab_index_attribute_like_html_signed_integer() {
assert_eq!(parse_tab_index_attribute(""), None);
+ assert_eq!(parse_tab_index_attribute("invalid"), None);
+ assert_eq!(parse_tab_index_attribute("\u{b}0"), None);
+ assert_eq!(parse_tab_index_attribute("\u{a0}0"), None);
+ assert_eq!(parse_tab_index_attribute(" \t\n\r\u{c}+0tail"), Some(0));
assert_eq!(parse_tab_index_attribute(" 5abc"), Some(5));
assert_eq!(parse_tab_index_attribute("+5"), Some(5));
assert_eq!(parse_tab_index_attribute("-5"), Some(-5));