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): honor computed content visibility in innerText

ldm01b05e14af2a7
diff --git a/moli-renderer-v8/src/css_style.rs b/moli-renderer-v8/src/css_style.rs
index f784c751c..cbf667911 100644
--- a/moli-renderer-v8/src/css_style.rs
+++ b/moli-renderer-v8/src/css_style.rs
@@ -131,28 +131,6 @@ impl CssInlineStyleDeclarationState {
self.entries.clone()
}
- /// Returns the winning value for an already-canonicalized longhand without
- /// cloning the declaration list or canonicalizing every stored name.
- ///
- /// This intentionally does not expand shorthands. It is for renderer-only
- /// typed facts whose declarations are stored in the side projection
- /// because the Servo-flavored Stylo build does not expose that longhand.
- pub(crate) fn canonical_longhand_value(&self, property: &str) -> Option<&str> {
- let mut normal = None;
- let mut important = None;
- for entry in &self.entries {
- if entry.name != property {
- continue;
- }
- if entry.priority {
- important = Some(entry.value.as_str());
- } else {
- normal = Some(entry.value.as_str());
- }
- }
- important.or(normal)
- }
-
pub(crate) fn property_names(&self) -> Vec<String> {
if self.side_entries.is_empty() && self.entries.is_empty() && !self.block.is_empty() {
return stylo_declaration_block_property_names(&self.block);
@@ -1258,8 +1236,7 @@ pub(crate) fn parse_css_declaration_list(style_text: &str) -> Vec<CssStyleEntry>
}
/// Returns the winning declaration for one property inside a declaration
-/// list. This is also usable for valid properties that the Servo flavor of
-/// Stylo does not expose, such as `content-visibility`.
+/// list, without requiring a computed style.
pub(crate) fn css_declaration_list_property_value(
style_text: &str,
property: &str,
@@ -1281,39 +1258,6 @@ pub(crate) fn css_declaration_list_property_value(
winner.map(|entry| entry.value)
}
-/// Returns one exact, already-canonicalized longhand from raw declaration text
-/// without routing through the general CSSOM property-name canonicalizer.
-///
-/// `cssparser` has already decoded the declaration name, so an ASCII
-/// case-insensitive comparison covers CSS property-name matching, including
-/// escaped identifiers. This is the uncached fallback for renderer-only typed
-/// facts; live elements normally use `CssInlineStyleDeclarationState`.
-pub(crate) fn css_declaration_list_canonical_longhand_value(
- style_text: &str,
- property: &str,
-) -> Option<String> {
- let mut normal = None;
- let mut important = None;
- for declaration in parse_declaration_list(
- style_text,
- DeclarationParseOptions {
- canonicalize_property_name: false,
- unescape_value_semicolons: true,
- preserve_empty_values: true,
- },
- ) {
- if !declaration.name.eq_ignore_ascii_case(property) {
- continue;
- }
- if declaration.important {
- important = Some(declaration.value);
- } else {
- normal = Some(declaration.value);
- }
- }
- important.or(normal)
-}
-
pub(crate) fn resolve_css_url_function(value: &str, base_url: &url::Url) -> String {
let value = value.trim();
let Some(raw_url) = value
@@ -2713,39 +2657,6 @@ mod tests {
assert!(entries[0].priority);
}
- #[test]
- fn canonical_longhand_fast_path_preserves_css_priority_and_identifier_matching() {
- let raw = r#"
- CONTENT-VISIBILITY: auto !important;
- content\2d visibility: visible;
- content-visibility: hidden;
- "#;
- assert_eq!(
- css_declaration_list_canonical_longhand_value(raw, "content-visibility").as_deref(),
- Some("auto")
- );
-
- let state = CssInlineStyleDeclarationState {
- entries: vec![
- CssStyleEntry {
- name: "content-visibility".to_owned(),
- value: "hidden".to_owned(),
- priority: true,
- },
- CssStyleEntry {
- name: "content-visibility".to_owned(),
- value: "visible".to_owned(),
- priority: false,
- },
- ],
- ..Default::default()
- };
- assert_eq!(
- state.canonical_longhand_value("content-visibility"),
- Some("hidden")
- );
- }
-
#[test]
fn css_declaration_list_parser_normalizes_zero_lengths_for_cssom() {
let entries = parse_css_declaration_list("width:0; height: 0; top: 0; left: 0;");
diff --git a/moli-renderer-v8/src/native_bridge/element/content.rs b/moli-renderer-v8/src/native_bridge/element/content.rs
index 15fceba82..94c004ec8 100644
--- a/moli-renderer-v8/src/native_bridge/element/content.rs
+++ b/moli-renderer-v8/src/native_bridge/element/content.rs
@@ -17,7 +17,7 @@ use super::{
html_element_getter_receiver, html_element_setter_receiver, observable_sources_with_fragments,
property_string_value,
rendered_state::{
- ElementBoxState, ElementContentVisibility, ElementRenderedState, ElementRenderedStyle,
+ ElementBoxState, ElementRenderedState, ElementRenderedStyle,
rendered_child_participates_in_flat_tree,
},
styles::StyleObservation,
@@ -636,10 +636,7 @@ fn append_inner_text(
let style = task
.prepared_style
.unwrap_or_else(|| ElementRenderedStyle::read_in_scope(style_scope, task.handle));
- if style.display == ComputedDisplayKind::None
- || (style.content_visibility == ElementContentVisibility::Hidden
- && style.content_visibility_applicable)
- {
+ if style.display == ComputedDisplayKind::None || style.hides_contents {
continue;
}
diff --git a/moli-renderer-v8/src/native_bridge/element/rendered_state.rs b/moli-renderer-v8/src/native_bridge/element/rendered_state.rs
index 6a4b48255..ea51f0b6b 100644
--- a/moli-renderer-v8/src/native_bridge/element/rendered_state.rs
+++ b/moli-renderer-v8/src/native_bridge/element/rendered_state.rs
@@ -27,20 +27,12 @@ pub(super) enum ElementBoxState {
NoBox,
}
-#[derive(Debug, Clone, Copy, PartialEq, Eq)]
-pub(super) enum ElementContentVisibility {
- Visible,
- Hidden,
- Auto,
-}
-
#[derive(Debug, Clone, Copy, PartialEq)]
pub(super) struct ElementRenderedStyle {
pub(super) display: ComputedDisplayKind,
effective_zoom: f32,
pub(super) visibility_visible: bool,
- pub(super) content_visibility: ElementContentVisibility,
- pub(super) content_visibility_applicable: bool,
+ pub(super) hides_contents: bool,
pub(super) opacity_zero: bool,
pub(super) text_transform: ComputedTextTransformKind,
pub(super) white_space_collapse: ComputedWhiteSpaceCollapseKind,
@@ -49,19 +41,16 @@ pub(super) struct ElementRenderedStyle {
impl ElementRenderedStyle {
pub(super) fn read_in_scope(scope: &mut StyleObservation<'_>, handle: DomHandle) -> Self {
- let runtime = scope.runtime();
let style = scope.read(handle);
let effective_zoom = style
.computed_values()
.map_or(1.0, |values| values.effective_zoom.value());
- let content_visibility = inline_content_visibility(runtime, handle);
if let Some(facts) = style.rendered_style_facts() {
return Self {
display: facts.display,
effective_zoom,
visibility_visible: facts.visibility_visible,
- content_visibility,
- content_visibility_applicable: facts.content_visibility_applicable,
+ hides_contents: facts.hides_contents,
opacity_zero: facts.opacity_zero,
text_transform: facts.text_transform,
white_space_collapse: facts.white_space_collapse,
@@ -74,8 +63,8 @@ impl ElementRenderedStyle {
display,
effective_zoom,
visibility_visible: style.property("visibility") == "visible",
- content_visibility,
- content_visibility_applicable: fallback_content_visibility_applicable(&display_value),
+ hides_contents: fallback_content_visibility_applicable(&display_value)
+ && style.property("content-visibility") == "hidden",
opacity_zero: computed_opacity_is_zero(&style.property("opacity")),
text_transform: computed_text_transform_kind(&style.property("text-transform")),
white_space_collapse: computed_white_space_collapse_kind(
@@ -106,44 +95,6 @@ fn computed_display_kind(value: &str) -> ComputedDisplayKind {
}
}
-fn inline_content_visibility(
- runtime: &JsContextHost,
- handle: DomHandle,
-) -> ElementContentVisibility {
- if runtime.element_inline_style_csp_state(handle)
- == crate::style_engine::InlineStyleCspState::BlockedAttribute
- {
- return ElementContentVisibility::Visible;
- }
-
- let value = runtime
- .element_inline_style_declaration_state(handle)
- .and_then(|state| state.canonical_longhand_value("content-visibility"));
- let fallback_value;
- let value = match value {
- Some(value) => Some(value),
- None => {
- fallback_value = runtime
- .dom_host()
- .node(handle)
- .and_then(Node::as_element)
- .and_then(|element| element.attribute("style"))
- .and_then(|style| {
- crate::css_style::css_declaration_list_canonical_longhand_value(
- style,
- "content-visibility",
- )
- });
- fallback_value.as_deref()
- }
- };
- match value.map(str::trim) {
- Some(value) if value.eq_ignore_ascii_case("hidden") => ElementContentVisibility::Hidden,
- Some(value) if value.eq_ignore_ascii_case("auto") => ElementContentVisibility::Auto,
- _ => ElementContentVisibility::Visible,
- }
-}
-
fn fallback_content_visibility_applicable(display: &str) -> bool {
!matches!(
display,
@@ -268,11 +219,7 @@ impl ElementRenderedState {
let Some(style) = entry.style.as_ref() else {
continue;
};
- if style.display == ComputedDisplayKind::None
- || (index != 0
- && style.content_visibility == ElementContentVisibility::Hidden
- && style.content_visibility_applicable)
- {
+ if style.display == ComputedDisplayKind::None || (index != 0 && style.hides_contents) {
return ElementBoxState::NoBox;
}
}
@@ -301,9 +248,7 @@ impl ElementRenderedState {
if style.display == ComputedDisplayKind::None {
return false;
}
- if style.content_visibility == ElementContentVisibility::Hidden
- && style.content_visibility_applicable
- {
+ if style.hides_contents {
return true;
}
}
diff --git a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/rendered_text.rs b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/rendered_text.rs
index 9ab6de0b3..2ab5d6801 100644
--- a/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/rendered_text.rs
+++ b/moli-renderer-v8/src/script_vm/tests/dom_xhr/dom/rendered_text.rs
@@ -423,6 +423,81 @@ fn check_visibility_and_inner_text_use_computed_rendered_state() {
);
}
+#[test]
+fn inner_text_observes_until_found_content_visibility_and_reveal() {
+ let mut vm = new_rendered_test_vm(
+ "https://inner-text-until-found.test/",
+ r#"<!doctype html><html><body><div id=h hidden="until-found">SECRET-UNTIL</div><div id=b hidden>SECRET-BOOL</div><p>visible</p></body></html>"#,
+ );
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const h = document.getElementById('h');
+ const b = document.getElementById('b');
+ const read = () => document.body.innerText;
+ const values = [h.hidden, getComputedStyle(h).display,
+ getComputedStyle(h).contentVisibility, getComputedStyle(b).display,
+ read(), h.innerText, b.innerText];
+ h.hidden = false;
+ values.push(read());
+ h.hidden = 'until-found';
+ values.push(read());
+ h.style.contentVisibility = 'visible';
+ values.push(read());
+ h.style.removeProperty('content-visibility');
+ values.push(read());
+ return JSON.stringify(values);
+})()
+"#,
+ )
+ .expect("innerText must follow until-found hiding and synchronous reveal");
+
+ assert_eq!(
+ result,
+ r#"["until-found","block","hidden","none","visible","","SECRET-BOOL","SECRET-UNTIL\n\nvisible","visible","SECRET-UNTIL\n\nvisible","visible"]"#,
+ );
+}
+
+#[test]
+fn inner_text_uses_cascaded_content_visibility() {
+ let mut vm = new_rendered_test_vm(
+ "https://inner-text-cascaded-content-visibility.test/",
+ r#"<!doctype html><html><head><style>
+ .locked { content-visibility: hidden; }
+ .shown { content-visibility: visible !important; }
+ </style></head><body><div id=target class=locked>secret <span id=child>child</span></div><p>visible</p></body></html>"#,
+ );
+
+ let result = vm
+ .eval(
+ r#"
+(() => {
+ const target = document.getElementById('target');
+ const child = document.getElementById('child');
+ const read = () => [document.body.innerText, target.innerText, child.innerText, child.checkVisibility()];
+ const values = [read()];
+ target.className = 'shown';
+ target.style.contentVisibility = 'hidden';
+ values.push(read());
+ target.className = 'locked';
+ values.push(read());
+ target.className = '';
+ target.style.removeProperty('content-visibility');
+ values.push(read());
+ return JSON.stringify(values);
+})()
+"#,
+ )
+ .expect("rendered text must follow the content-visibility cascade");
+
+ assert_eq!(
+ result,
+ r#"[["visible","","",false],["secret child\n\nvisible","secret child","child",true],["visible","","",false],["secret child\n\nvisible","secret child","child",true]]"#,
+ );
+}
+
#[test]
fn content_visibility_only_locks_chromium_eligible_boxes() {
let mut vm = new_rendered_test_vm(
diff --git a/moli-renderer-v8/src/style_engine/computed.rs b/moli-renderer-v8/src/style_engine/computed.rs
index 4d27f9b3f..be22111f4 100644
--- a/moli-renderer-v8/src/style_engine/computed.rs
+++ b/moli-renderer-v8/src/style_engine/computed.rs
@@ -145,7 +145,7 @@ impl StyloAnonymousBoxKind {
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub(crate) struct ComputedRenderedStyleFacts {
pub(crate) display: ComputedDisplayKind,
- pub(crate) content_visibility_applicable: bool,
+ pub(crate) hides_contents: bool,
pub(crate) visibility_visible: bool,
pub(crate) opacity_zero: bool,
pub(crate) text_transform: ComputedTextTransformKind,
@@ -186,7 +186,8 @@ impl StyloComputedStyleSnapshot {
pub(crate) fn rendered_style_facts(&self) -> ComputedRenderedStyleFacts {
let display = self.primary.clone_display();
- let content_visibility_applicable = content_visibility_applies(display);
+ let hides_contents = content_visibility_applies(display)
+ && self.primary.clone_content_visibility() == ContentVisibility::Hidden;
let display = if display.is_none() {
ComputedDisplayKind::None
} else if display.is_contents() {
@@ -252,7 +253,7 @@ impl StyloComputedStyleSnapshot {
};
ComputedRenderedStyleFacts {
display,
- content_visibility_applicable,
+ hides_contents,
visibility_visible: self.primary.clone_visibility() == ComputedVisibility::Visible,
opacity_zero: self.primary.clone_opacity() == 0.0,
text_transform,