Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(html): reveal details for fragment navigation

ldm0bac28518ff4d
diff --git a/moli-renderer-v8/src/native_bridge/element.rs b/moli-renderer-v8/src/native_bridge/element.rs
index 2121bd02a..e3cee4d6d 100644
--- a/moli-renderer-v8/src/native_bridge/element.rs
+++ b/moli-renderer-v8/src/native_bridge/element.rs
@@ -245,10 +245,11 @@ pub(super) use content::{
};
pub(super) use dataset::{build_dom_string_map_wrapper_template, node_dataset_getter_function};
use details_dialog::{
- close_dialog_element, details_open_getter_function, details_open_setter_function,
- dialog_close_callback, dialog_open_getter_function, dialog_open_setter_function,
- dialog_return_value_getter_function, dialog_return_value_setter_function, dialog_show_callback,
- dialog_show_modal_callback, perform_summary_click_default_action,
+ close_dialog_element, closed_details_ancestors_to_reveal, details_open_getter_function,
+ details_open_setter_function, dialog_close_callback, dialog_open_getter_function,
+ dialog_open_setter_function, dialog_return_value_getter_function,
+ dialog_return_value_setter_function, dialog_show_callback, dialog_show_modal_callback,
+ perform_summary_click_default_action,
};
pub(crate) use details_dialog::{
queue_details_toggle_event_for_attribute_change, queue_parser_details_toggle_event,
diff --git a/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs b/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs
index 7f84666df..9a3d0242d 100644
--- a/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs
+++ b/moli-renderer-v8/src/native_bridge/element/activation/default_action.rs
@@ -24,15 +24,16 @@ use super::super::super::JsContextHost;
use super::super::forms::{FormAssociatedResetCallbackTiming, reset_form_default_action};
use super::super::{
NodePublicEventDispatchOutcome, TextEditInputType, cache_input_files_from_selected_files,
- construct_click_event_with_detail_and_modifiers, construct_command_event,
- construct_input_event, construct_simple_event, contenteditable_editing_host,
- dispatch_beforeinput, dispatch_popover_toggle_events, dispatch_public_event, element_attribute,
- element_has_attribute, form_associated_form_owner, is_disabled_form_control, is_focusable,
- is_valid_submit_button, label_activation_control_handle,
- perform_popover_invoker_default_action, perform_summary_click_default_action,
- replace_text_control_selection, resolve_url_like_attribute,
- resolved_reflected_element_attribute_handle, scroll_node_into_view_at_start,
- submit_form_with_submit_event, update_focus,
+ closed_details_ancestors_to_reveal, construct_click_event_with_detail_and_modifiers,
+ construct_command_event, construct_input_event, construct_simple_event,
+ contenteditable_editing_host, dispatch_beforeinput, dispatch_popover_toggle_events,
+ dispatch_public_event, element_attribute, element_has_attribute, form_associated_form_owner,
+ is_disabled_form_control, is_focusable, is_valid_submit_button,
+ label_activation_control_handle, perform_popover_invoker_default_action,
+ perform_summary_click_default_action, replace_text_control_selection,
+ resolve_url_like_attribute, resolved_reflected_element_attribute_handle,
+ scroll_node_into_view_at_start, set_reflected_boolean_attribute, submit_form_with_submit_event,
+ update_focus,
};
use super::targets::{
SpecialBrowsingContextTarget, named_iframe_target_handle_for_navigation,
@@ -1942,7 +1943,7 @@ fn anchor_click_default_action(
&& let Ok(url) = url::Url::parse(&resolved)
&& same_document_fragment_target(runtime.host_document().url(), &url)
{
- if let Err(error) = scroll_to_document_fragment_target(scope, runtime_ptr, runtime, &url) {
+ if let Err(error) = scroll_to_document_fragment_target(scope, runtime_ptr, &url) {
throw_activation_layout_error(scope, "scrolling to fragment", error);
return None;
}
@@ -2160,7 +2161,6 @@ pub(crate) fn scroll_to_url_fragment_or_top(
runtime_ptr: *mut JsContextHost,
target_url: &str,
) -> Result<(), moli_layout::LayoutError> {
- let runtime = unsafe { &*runtime_ptr };
let Ok(target_url) = url::Url::parse(target_url) else {
return Ok(());
};
@@ -2168,7 +2168,7 @@ pub(crate) fn scroll_to_url_fragment_or_top(
crate::window_host::scroll_window_to(scope, runtime_ptr, 0.0, 0.0);
return Ok(());
}
- if !scroll_to_document_fragment_target(scope, runtime_ptr, runtime, &target_url)? {
+ if !scroll_to_document_fragment_target(scope, runtime_ptr, &target_url)? {
crate::window_host::scroll_window_to(scope, runtime_ptr, 0.0, 0.0);
}
Ok(())
@@ -2177,7 +2177,6 @@ pub(crate) fn scroll_to_url_fragment_or_top(
fn scroll_to_document_fragment_target(
scope: &mut v8::PinScope<'_, '_>,
runtime_ptr: *mut JsContextHost,
- runtime: &JsContextHost,
target_url: &url::Url,
) -> Result<bool, moli_layout::LayoutError> {
let Some(fragment) = target_url.fragment() else {
@@ -2186,13 +2185,31 @@ fn scroll_to_document_fragment_target(
let fragment = percent_encoding::percent_decode_str(fragment)
.decode_utf8_lossy()
.into_owned();
- let Some(target) = document_tree_fragment_target(runtime, &fragment) else {
+ let Some(target) = document_tree_fragment_target(unsafe { &*runtime_ptr }, &fragment) else {
return Ok(false);
};
+ reveal_details_ancestors_for_fragment_target(scope, runtime_ptr, target);
let _ = scroll_node_into_view_at_start(scope, runtime_ptr, target)?;
Ok(true)
}
+fn reveal_details_ancestors_for_fragment_target(
+ scope: &mut v8::PinScope<'_, '_>,
+ runtime_ptr: *mut JsContextHost,
+ target: DomHandle,
+) {
+ let ancestors = closed_details_ancestors_to_reveal(unsafe { &*runtime_ptr }, target);
+ for details in ancestors {
+ let runtime = unsafe { &*runtime_ptr };
+ if !runtime.dom_host().is_connected(details)
+ || element_has_attribute(runtime, details, "open")
+ {
+ return;
+ }
+ set_reflected_boolean_attribute(scope, runtime_ptr, details, "open", true);
+ }
+}
+
fn document_tree_fragment_target(runtime: &JsContextHost, fragment: &str) -> Option<DomHandle> {
let mut stack = Vec::new();
let mut current = Some(runtime.dom_host().document_handle());
diff --git a/moli-renderer-v8/src/native_bridge/element/details_dialog.rs b/moli-renderer-v8/src/native_bridge/element/details_dialog.rs
index 45a400067..50cda3e40 100644
--- a/moli-renderer-v8/src/native_bridge/element/details_dialog.rs
+++ b/moli-renderer-v8/src/native_bridge/element/details_dialog.rs
@@ -139,6 +139,54 @@ pub(super) fn node_is_hidden_by_closed_details(runtime: &JsContextHost, handle:
false
}
+pub(super) fn closed_details_ancestors_to_reveal(
+ runtime: &JsContextHost,
+ target: DomHandle,
+) -> Vec<DomHandle> {
+ let mut ancestors = Vec::new();
+ let mut branch = target;
+ while let Some(parent) = details_reveal_flat_tree_parent(runtime, branch) {
+ if runtime.dom_host().is_html_element_named(parent, "details")
+ && !element_has_attribute(runtime, parent, "open")
+ && main_summary_child(runtime, parent) != Some(branch)
+ {
+ ancestors.push(parent);
+ }
+ branch = parent;
+ }
+ ancestors
+}
+
+fn details_reveal_flat_tree_parent(
+ runtime: &JsContextHost,
+ handle: DomHandle,
+) -> Option<DomHandle> {
+ if let Some(slot) = runtime.dom_host().assigned_slot_for_node(handle) {
+ return Some(slot);
+ }
+ let parent = runtime.dom_host().parent_node(handle)?;
+ if runtime.dom_host().is_shadow_root(parent) {
+ return runtime.dom_host().shadow_root_host(parent);
+ }
+ if runtime.dom_host().is_html_element_named(parent, "slot")
+ && !runtime
+ .dom_host()
+ .assigned_nodes_for_slot_with_options(parent, false)
+ .is_empty()
+ {
+ return None;
+ }
+ if runtime.dom_host().shadow_root_handle(parent).is_some()
+ && runtime
+ .dom_host()
+ .node(handle)
+ .is_some_and(|node| node.is_element() || node.is_text())
+ {
+ return None;
+ }
+ Some(parent)
+}
+
fn main_summary_details_handle(runtime: &JsContextHost, summary: DomHandle) -> Option<DomHandle> {
let summary_element = runtime
.dom_host()
diff --git a/moli-renderer-v8/src/script_vm/tests/browser_api/details.rs b/moli-renderer-v8/src/script_vm/tests/browser_api/details.rs
index f0ed83e1c..489f1304c 100644
--- a/moli-renderer-v8/src/script_vm/tests/browser_api/details.rs
+++ b/moli-renderer-v8/src/script_vm/tests/browser_api/details.rs
@@ -170,3 +170,56 @@ fn details_name_groups_enforce_exclusivity_within_each_tree_root() {
r#"{"parserStates":[1,0],"directOpenStates":[0,1],"insertionStates":[0,0,1],"renameStates":[1,0],"nestedInsertionStates":[1,0],"treeScopeStates":[1,0,1],"emptyNameStates":[1,1],"detachedParserStates":[1,0]}"#
);
}
+
+#[test]
+fn fragment_navigation_reveals_closed_details_ancestors_but_not_their_summary() {
+ let mut vm = new_parsed_test_vm(
+ "https://details-fragment-reveal.test/",
+ r##"
+ <!doctype html>
+ <html>
+ <body>
+ <a id="reveal-link" href="#target"></a>
+ <details id="outer">
+ <summary>Outer summary</summary>
+ <details id="inner">
+ <summary id="inner-summary">Inner summary</summary>
+ <div id="target">Target</div>
+ </details>
+ </details>
+ </body>
+ </html>
+ "##,
+ );
+
+ let result = vm
+ .eval(
+ r##"
+(() => {
+ const outer = document.getElementById("outer");
+ const inner = document.getElementById("inner");
+ document.getElementById("reveal-link").click();
+ const targetState = [outer.open, inner.open, location.hash];
+
+ outer.open = false;
+ inner.open = false;
+ location.hash = "";
+ const summaryLink = document.createElement("a");
+ summaryLink.href = "#inner-summary";
+ document.body.appendChild(summaryLink);
+ summaryLink.click();
+
+ return JSON.stringify({
+ targetState,
+ summaryState: [outer.open, inner.open, location.hash]
+ });
+})()
+"##,
+ )
+ .expect("details fragment ancestor reveal should evaluate");
+
+ assert_eq!(
+ result,
+ r##"{"targetState":[true,true,"#target"],"summaryState":[true,false,"#inner-summary"]}"##
+ );
+}