Skip to content

armgit / moli

Public Imported from GitHub
0

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

fix(css): disable presentation hints with author styles

ldm0db38025ff1d0
diff --git a/moli-renderer-v8/src/style_engine/mod.rs b/moli-renderer-v8/src/style_engine/mod.rs
index feee8acb4..fe336fea4 100644
--- a/moli-renderer-v8/src/style_engine/mod.rs
+++ b/moli-renderer-v8/src/style_engine/mod.rs
@@ -182,7 +182,9 @@ impl MoliStyleEngine {
pub(crate) fn new_with_author_styles_disabled(author_styles_disabled: bool) -> Self {
ensure_stylo_browser_compat_prefs();
Self {
- dom_adapter: StyloDomStyleAdapter::new(),
+ dom_adapter: StyloDomStyleAdapter::new_with_author_styles_enabled(
+ !author_styles_disabled,
+ ),
document_worlds: DocumentStyleWorlds::new(),
author_styles_disabled,
owner_stylesheet_source_documents: RefCell::new(HashMap::new()),
diff --git a/moli-renderer-v8/src/style_engine/tests/lifecycle.rs b/moli-renderer-v8/src/style_engine/tests/lifecycle.rs
index 6d32b15ec..ee648ef7e 100644
--- a/moli-renderer-v8/src/style_engine/tests/lifecycle.rs
+++ b/moli-renderer-v8/src/style_engine/tests/lifecycle.rs
@@ -45,6 +45,64 @@ fn disabled_author_styles_use_stylo_author_origin_gate() {
),
Some("rgb(0, 0, 0)".to_owned())
);
+
+ let svg = host
+ .create_element_ns(Some("http://www.w3.org/2000/svg"), "svg:svg")
+ .expect("SVG root");
+ let rect = host
+ .create_element_ns(Some("http://www.w3.org/2000/svg"), "svg:rect")
+ .expect("SVG rect");
+ assert!(host.set_attribute(rect, "display", "none"));
+ assert!(host.set_attribute(rect, "fill", "red"));
+ assert!(host.append_child(svg, rect));
+ assert!(host.append_child(body, svg));
+
+ assert_eq!(
+ engine.computed_style_property_value(
+ &host,
+ &document_url,
+ rect,
+ "display",
+ None,
+ &inputs,
+ None,
+ ),
+ Some("inline".to_owned()),
+ "SVG presentation attributes must follow the author-style policy"
+ );
+ assert_eq!(
+ engine.computed_style_property_value(
+ &host,
+ &document_url,
+ rect,
+ "fill",
+ None,
+ &inputs,
+ None,
+ ),
+ Some("rgb(0, 0, 0)".to_owned())
+ );
+
+ let table = host.create_element("table");
+ let row = host.create_element("tr");
+ let cell = host.create_element("td");
+ assert!(host.set_attribute(table, "cellpadding", "17"));
+ assert!(host.append_child(row, cell));
+ assert!(host.append_child(table, row));
+ assert!(host.append_child(body, table));
+ assert_eq!(
+ engine.computed_style_property_value(
+ &host,
+ &document_url,
+ cell,
+ "padding-left",
+ None,
+ &inputs,
+ None,
+ ),
+ Some("0px".to_owned()),
+ "legacy HTML presentation hints must follow the author-style policy"
+ );
}
#[test]
diff --git a/moli-selector/src/stylo/presentation.rs b/moli-selector/src/stylo/presentation.rs
index 3b26f2267..eb7135d02 100644
--- a/moli-selector/src/stylo/presentation.rs
+++ b/moli-selector/src/stylo/presentation.rs
@@ -107,6 +107,14 @@ impl QueryElement<'_> {
where
V: Push<ApplicableDeclarationBlock>,
{
+ // Stylo collects presentation hints before checking
+ // `AuthorStylesEnabled`, so the adapter must apply the same Document
+ // policy explicitly. Otherwise SVG attributes such as `fill` and
+ // legacy HTML attributes such as `cellpadding` leak into an
+ // author-style-disabled cascade.
+ if !self.author_styles_enabled() {
+ return;
+ }
let element = self.element();
let mut block = PropertyDeclarationBlock::new();
if element.namespace() == SVG_NAMESPACE {
diff --git a/moli-selector/src/stylo/query/element.rs b/moli-selector/src/stylo/query/element.rs
index 196fab497..72afb044f 100644
--- a/moli-selector/src/stylo/query/element.rs
+++ b/moli-selector/src/stylo/query/element.rs
@@ -68,6 +68,11 @@ impl<'a> QueryElement<'a> {
self.shared_lock
}
+ pub(in crate::stylo) fn author_styles_enabled(self) -> bool {
+ self.style_data
+ .is_none_or(StyloElementDataStore::author_styles_enabled)
+ }
+
pub(crate) fn read_quirks_mode(self) -> QuirksMode {
self.host
.owner_document_handle(self.handle)
diff --git a/moli-selector/src/stylo/style_traversal.rs b/moli-selector/src/stylo/style_traversal.rs
index 57248d99c..7034c8610 100644
--- a/moli-selector/src/stylo/style_traversal.rs
+++ b/moli-selector/src/stylo/style_traversal.rs
@@ -58,6 +58,10 @@ pub struct StyloDomStyleAdapter {
/// must not clear an element's data while Stylo holds a borrow to it.
#[derive(Default)]
pub struct StyloElementDataStore {
+ /// Presentation hints are synthesized by the DOM adapter before Stylo's
+ /// author-origin gate. Keep the Document's author-style policy here so
+ /// those declarations follow the same policy as rules and `style=`.
+ author_styles_disabled: bool,
handle_documents: RefCell<HashMap<NodeId, NodeId>>,
documents: RefCell<HashMap<NodeId, StyloElementDocumentDataStore>>,
#[cfg(test)]
@@ -134,11 +138,18 @@ struct SnapshotHandlesReset<'a> {
impl StyloDomStyleAdapter {
pub fn new() -> Self {
+ Self::new_with_author_styles_enabled(true)
+ }
+
+ pub fn new_with_author_styles_enabled(author_styles_enabled: bool) -> Self {
Self {
state: Box::new(StyleDomState {
host: Cell::new(std::ptr::null()),
shared_lock: SharedRwLock::new(),
- element_data: StyloElementDataStore::default(),
+ element_data: StyloElementDataStore {
+ author_styles_disabled: !author_styles_enabled,
+ ..StyloElementDataStore::default()
+ },
atom_cache: QueryAtomCache::default(),
node_data: RefCell::default(),
slotted_node_data: RefCell::default(),
@@ -411,6 +422,10 @@ impl Drop for StyleDomHostBinding<'_> {
}
impl StyloElementDataStore {
+ pub(in crate::stylo) fn author_styles_enabled(&self) -> bool {
+ !self.author_styles_disabled
+ }
+
pub(super) fn note_owner_document_for_host(
&self,
host: &DomHost,
diff --git a/moli/tests/fetch_cli/disable_css.rs b/moli/tests/fetch_cli/disable_css.rs
index 24b7093af..f4fd3c9d1 100644
--- a/moli/tests/fetch_cli/disable_css.rs
+++ b/moli/tests/fetch_cli/disable_css.rs
@@ -56,6 +56,8 @@ const MAIN_PAGE: &str = r#"<!doctype html>
<div id="adopted-target">adopted</div>
<div id="dynamic-target">dynamic</div>
<div id="shadow-host"></div>
+ <svg><rect id="svg-target" display="none" fill="red"></rect></svg>
+ <table cellpadding="17"><tr><td id="legacy-table-target">legacy</td></tr></table>
<iframe id="child" src="/child.html"></iframe>
<iframe id="srcdoc-child" srcdoc="<!doctype html><style>@import url('/srcdoc.css'); #srcdoc-target { display:none; color:rgb(31,32,33) }</style><div id='srcdoc-target' style='display:none'>srcdoc</div><script>document.documentElement.setAttribute('data-script-ran','yes')</script>"></iframe>
<script>
@@ -85,6 +87,8 @@ const DISABLED_READY_SCRIPT: &str = r#"(() => {
const grandchild = child?.getElementById('grandchild')?.contentDocument;
const srcdoc = document.getElementById('srcdoc-child')?.contentDocument;
const shadow = document.getElementById('shadow-host')?.shadowRoot;
+ const svgTarget = document.getElementById('svg-target');
+ const legacyTableTarget = document.getElementById('legacy-table-target');
const targets = [
[window, document.getElementById('main-target'), 'block'],
[window, document.getElementById('adopted-target'), 'block'],
@@ -104,7 +108,13 @@ const DISABLED_READY_SCRIPT: &str = r#"(() => {
const scriptsRan = [document, child, grandchild, srcdoc].every(
targetDocument => targetDocument?.documentElement?.getAttribute('data-script-ran') === 'yes'
);
- if (!authorStylesAreAbsent || !scriptsRan) {
+ const svgStyle = svgTarget && getComputedStyle(svgTarget);
+ const legacyTableStyle = legacyTableTarget && getComputedStyle(legacyTableTarget);
+ const presentationHintsAreAbsent =
+ svgStyle?.display === 'inline' &&
+ svgStyle?.fill === 'rgb(0, 0, 0)' &&
+ legacyTableStyle?.paddingLeft === '0px';
+ if (!authorStylesAreAbsent || !presentationHintsAreAbsent || !scriptsRan) {
return false;
}
document.documentElement.setAttribute('data-disable-css-probe', 'ready');
@@ -271,6 +281,8 @@ fn disables_all_author_style_surfaces_without_disabling_scripts() -> Result<()>
for preserved in [
"id=\"external-link\"",
"id=\"inline-sheet\"",
+ "id=\"svg-target\" display=\"none\" fill=\"red\"",
+ "cellpadding=\"17\"",
"style=\"color: rgb(4, 5, 6); display: none\"",
] {
assert!(