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): resolve hidden until-found through native style computation

ldm02fb6b9190a56
diff --git a/moli-renderer-v8/src/detached_css_style.rs b/moli-renderer-v8/src/detached_css_style.rs
index 7a25108f3..543458b22 100644
--- a/moli-renderer-v8/src/detached_css_style.rs
+++ b/moli-renderer-v8/src/detached_css_style.rs
@@ -313,6 +313,7 @@ const LIGHTWEIGHT_STYLE_PROPERTIES: &[&str] = &[
"container-name",
"container-type",
"content",
+ "content-visibility",
"cursor",
"direction",
"display",
diff --git a/moli-renderer-v8/src/native_bridge/element/geometry.rs b/moli-renderer-v8/src/native_bridge/element/geometry.rs
index 60379dc0c..6ba395d52 100644
--- a/moli-renderer-v8/src/native_bridge/element/geometry.rs
+++ b/moli-renderer-v8/src/native_bridge/element/geometry.rs
@@ -25,6 +25,7 @@ pub(in crate::native_bridge) use metrics::{
node_scroll_top_getter_function, node_scroll_top_setter_function,
node_scroll_width_getter_function,
};
+pub(super) use mock::element_has_hidden_attribute;
pub(crate) use mock::{
compute_mock_client_rect, compute_mock_intersection_client_rect,
compute_mock_intersection_scrollport_client_rect,
diff --git a/moli-renderer-v8/src/native_bridge/element/geometry/mock/layout.rs b/moli-renderer-v8/src/native_bridge/element/geometry/mock/layout.rs
index 8b7f70f3e..682f85b42 100644
--- a/moli-renderer-v8/src/native_bridge/element/geometry/mock/layout.rs
+++ b/moli-renderer-v8/src/native_bridge/element/geometry/mock/layout.rs
@@ -160,7 +160,10 @@ fn element_attribute(runtime: &JsContextHost, handle: DomHandle, name: &str) ->
runtime.dom_host().get_attribute(handle, name)
}
-fn element_has_hidden_attribute(runtime: &JsContextHost, handle: DomHandle) -> bool {
+pub(in crate::native_bridge::element) fn element_has_hidden_attribute(
+ runtime: &JsContextHost,
+ handle: DomHandle,
+) -> bool {
element_attribute(runtime, handle, "hidden").is_some()
}
diff --git a/moli-renderer-v8/src/native_bridge/element/geometry/mock/mod.rs b/moli-renderer-v8/src/native_bridge/element/geometry/mock/mod.rs
index 42216997f..f9a09d8e8 100644
--- a/moli-renderer-v8/src/native_bridge/element/geometry/mock/mod.rs
+++ b/moli-renderer-v8/src/native_bridge/element/geometry/mock/mod.rs
@@ -1,6 +1,7 @@
mod layout;
mod queries;
+pub(in crate::native_bridge::element) use layout::element_has_hidden_attribute;
pub(crate) use layout::{
compute_mock_client_rect, compute_mock_intersection_client_rect,
compute_mock_intersection_scrollport_client_rect,
diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse.rs
index b578acafb..209db9ae5 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/parse.rs
@@ -3968,6 +3968,42 @@ mod tests {
}
}
+ #[test]
+ fn content_visibility_cssom_writes_use_stylo_pdb() {
+ for (value, expected) in [
+ ("hidden", "hidden"),
+ ("AUTO", "auto"),
+ ("inherit", "inherit"),
+ ] {
+ assert!(cssom_style_property_write_uses_pdb(
+ "content-visibility",
+ value
+ ));
+ let parsed = parse_style_property_entries_for_cssom_write(
+ "content-visibility",
+ value,
+ true,
+ None,
+ )
+ .expect("content-visibility should parse through Stylo PDB");
+ assert_eq!(parsed.entries.len(), 1);
+ assert_eq!(parsed.entries[0].name, "content-visibility");
+ assert_eq!(parsed.entries[0].value, expected);
+ assert!(parsed.entries[0].priority);
+ assert!(style_entry_is_pdb_safe(&parsed.entries[0]));
+ }
+
+ assert!(
+ parse_style_property_entries_for_cssom_write(
+ "content-visibility",
+ "bogus",
+ false,
+ None,
+ )
+ .is_none()
+ );
+ }
+
#[test]
fn transition_pdb_parser_accepts_dynamic_numeric_longhands() {
let shorthand = parse_style_property_entries_with_base(
diff --git a/moli-renderer-v8/src/native_bridge/element/styles/declaration/values.rs b/moli-renderer-v8/src/native_bridge/element/styles/declaration/values.rs
index beb391bf1..a724da35b 100644
--- a/moli-renderer-v8/src/native_bridge/element/styles/declaration/values.rs
+++ b/moli-renderer-v8/src/native_bridge/element/styles/declaration/values.rs
@@ -12,8 +12,8 @@ use crate::{
},
document_runtime::DomHandle,
native_bridge::element::geometry::{
- ClientRect, observable_bounding_client_rects, observable_used_grid_tracks,
- read_bounding_client_rect,
+ ClientRect, element_has_hidden_attribute, observable_bounding_client_rects,
+ observable_used_grid_tracks, read_bounding_client_rect,
},
style_engine::{
ComputedDisplayKind, ComputedRenderedStyleFacts, FullStyleWorldSnapshot, StyleViewport,
@@ -1383,6 +1383,7 @@ pub(super) fn computed_style_default_value(
"line-height" => "normal".to_owned(),
"link-parameters" => "none".to_owned(),
"content" => "normal".to_owned(),
+ "content-visibility" => "visible".to_owned(),
"background-color" => "rgba(0, 0, 0, 0)".to_owned(),
"background-attachment" => "scroll".to_owned(),
"background-blend-mode" | "mix-blend-mode" => "normal".to_owned(),
@@ -2329,7 +2330,9 @@ fn computed_style_property_value_after_style_update(
if !computed_style_applies(runtime, handle) {
return String::new();
}
- if property == "display" && element_has_hidden_attribute(runtime, handle) {
+ if property == "display"
+ && element_hidden_attribute_state(runtime, handle) == HiddenAttributeState::Hidden
+ {
return "none".to_owned();
}
let inputs = prepared_inputs;
@@ -5108,12 +5111,29 @@ fn css_px_values_equal(left: f64, right: f64) -> bool {
(left - right).abs() < 0.001
}
-fn element_has_hidden_attribute(runtime: &JsContextHost, handle: DomHandle) -> bool {
- runtime
+#[derive(Clone, Copy, Debug, Eq, PartialEq)]
+enum HiddenAttributeState {
+ Missing,
+ Hidden,
+ UntilFound,
+}
+
+fn element_hidden_attribute_state(
+ runtime: &JsContextHost,
+ handle: DomHandle,
+) -> HiddenAttributeState {
+ let value = runtime
.dom_host()
.node(handle)
.and_then(Node::as_element)
- .is_some_and(|element| element.has_attribute("hidden"))
+ .and_then(|element| element.attribute("hidden"));
+ match value {
+ None => HiddenAttributeState::Missing,
+ Some(value) if value.eq_ignore_ascii_case("until-found") => {
+ HiddenAttributeState::UntilFound
+ }
+ Some(_) => HiddenAttributeState::Hidden,
+ }
}
fn resolve_moli_computed_style_value(
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
index f33190fe4..89893b108 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/computed_style.rs
@@ -7027,6 +7027,47 @@ fn computed_display_treats_hidden_attribute_as_none() {
assert_eq!(result, "true|none|inline|0|0");
}
+#[test]
+fn computed_style_distinguishes_hidden_and_until_found_states() {
+ let mut vm = new_storage_test_vm("https://hidden-until-found-computed-style.test/");
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const target = document.createElement('div');
+ (document.body || document.documentElement || document).appendChild(target);
+ const read = () => [
+ getComputedStyle(target).display,
+ getComputedStyle(target).contentVisibility
+ ].join(':');
+ const values = [read()];
+ for (const value of ['', 'asdf', 'until-found', 'UNTIL-FOUND', 'UnTiL-FoUnD', '0']) {
+ target.setAttribute('hidden', value);
+ values.push(read());
+ }
+ target.setAttribute('hidden', 'until-found');
+ target.style.contentVisibility = 'visible';
+ values.push(`${target.style.contentVisibility}/${read()}`);
+ target.style.removeProperty('content-visibility');
+ values.push(read());
+ target.removeAttribute('hidden');
+ target.style.contentVisibility = 'hidden';
+ values.push(`${target.style.contentVisibility}/${read()}`);
+ target.style.contentVisibility = 'bogus';
+ values.push(`${target.style.contentVisibility}/${read()}`);
+ return values.join('|');
+})()
+"#,
+ )
+ .expect("hidden presentation states should affect computed style");
+
+ assert_eq!(
+ result,
+ "block:visible|none:visible|none:visible|block:hidden|block:hidden|block:hidden|none:visible|visible/block:visible|block:hidden|hidden/block:hidden|hidden/block:hidden"
+ );
+}
+
#[test]
fn detached_nested_iframe_window_get_computed_style_uses_iframe_width() {
let mut vm = new_storage_test_vm("https://nested-computed-width.test/");
diff --git a/moli-selector/src/stylo.rs b/moli-selector/src/stylo.rs
index 4b681dec4..f22ed1e4f 100644
--- a/moli-selector/src/stylo.rs
+++ b/moli-selector/src/stylo.rs
@@ -15,6 +15,7 @@ use crate::{
mod atoms;
mod invalidation;
mod presentation;
+mod presentational_hints;
mod query;
mod selector_parse;
mod style_traversal;
diff --git a/moli-selector/src/stylo/presentation.rs b/moli-selector/src/stylo/presentation.rs
index d91a15a86..f8e9e2d3b 100644
--- a/moli-selector/src/stylo/presentation.rs
+++ b/moli-selector/src/stylo/presentation.rs
@@ -26,7 +26,9 @@ use style_traits::{ParsingMode, ToCss};
use crate::dom::native::Element;
-use super::query::QueryElement;
+use super::{
+ presentational_hints::synthesize_hidden_until_found_presentational_hint, query::QueryElement,
+};
const HTML_NAMESPACE: &str = "http://www.w3.org/1999/xhtml";
const SVG_NAMESPACE: &str = "http://www.w3.org/2000/svg";
@@ -162,8 +164,6 @@ impl QueryElement<'_> {
}
} else if element.namespace() == HTML_NAMESPACE {
append_html_table_presentation_declarations(*self, &mut block);
- } else {
- return;
}
if !block.is_empty() {
@@ -173,6 +173,12 @@ impl QueryElement<'_> {
LayerOrder::root(),
));
}
+ synthesize_hidden_until_found_presentational_hint(
+ self.host(),
+ self.handle(),
+ self.shared_lock(),
+ hints,
+ );
}
}
diff --git a/moli-selector/src/stylo/presentational_hints.rs b/moli-selector/src/stylo/presentational_hints.rs
new file mode 100644
index 000000000..3a13ad982
--- /dev/null
+++ b/moli-selector/src/stylo/presentational_hints.rs
@@ -0,0 +1,49 @@
+use selectors::sink::Push;
+use style::{
+ applicable_declarations::ApplicableDeclarationBlock,
+ properties::{
+ Importance, PropertyDeclaration, PropertyDeclarationBlock,
+ longhands::content_visibility::SpecifiedValue as ContentVisibility,
+ },
+ rule_tree::{CascadeLevel, CascadeOrigin},
+ servo_arc::Arc,
+ shared_lock::SharedRwLock,
+ stylesheets::layer_rule::LayerOrder,
+};
+
+use crate::dom::{
+ NodeId,
+ native::{DomHost, Node},
+};
+
+const HTML_NAMESPACE: &str = "http://www.w3.org/1999/xhtml";
+
+pub(super) fn synthesize_hidden_until_found_presentational_hint<V>(
+ host: &DomHost,
+ handle: NodeId,
+ shared_lock: &SharedRwLock,
+ hints: &mut V,
+) where
+ V: Push<ApplicableDeclarationBlock>,
+{
+ let Some(element) = host.node(handle).and_then(Node::as_element) else {
+ return;
+ };
+ if element.namespace() != HTML_NAMESPACE
+ || !element
+ .attribute("hidden")
+ .is_some_and(|value| value.eq_ignore_ascii_case("until-found"))
+ {
+ return;
+ }
+
+ let declarations = PropertyDeclarationBlock::with_one(
+ PropertyDeclaration::ContentVisibility(ContentVisibility::Hidden),
+ Importance::Normal,
+ );
+ hints.push(ApplicableDeclarationBlock::from_declarations(
+ Arc::new(shared_lock.wrap(declarations)),
+ CascadeLevel::new(CascadeOrigin::PresHints),
+ LayerOrder::root(),
+ ));
+}