Best headless browser for AI agents. Lite, Fast, High-Compatibility. Built in Rust
Rust ai-agentsai-toolsbrowserbrowser-automationcloud-browserheadlessheadless-browserkitesurfplaywrightpuppeteerrustservoweb-crawlerweb-crawlingweb-scraperweb-scraping
fix(svg): map special presentation attributes
ldm09944bfc31f51
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.rsindex 93f16fe13..a009a144f 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@@ -12191,6 +12191,74 @@ fn computed_style_resolves_valid_typed_css_math_and_rejects_invalid_unit_algebra);}+#[test]+fn svg_special_presentation_attributes_follow_element_scopes_and_cascade() {+ let mut vm = new_parsed_test_vm(+ "https://svg-special-presentation-attributes.test/",+ r#"<!doctype html>+ <style>#cascade { transform: scale(3); }</style>+ <svg id="root" width="40" height="20">+ <use id="positioned" x="1" y="2" width="3" height="4"></use>+ <g id="group" transform="translate(200, 0)"></g>+ <symbol id="symbol" transform="translate(100, 0)"></symbol>+ <g id="group-alias" transform="scale(2)" patternTransform="scale(9)"></g>+ <pattern id="pattern" patternTransform="scale(2)" transform="scale(9)"></pattern>+ <linearGradient id="linear" gradientTransform="scale(4)" transform="scale(9)"></linearGradient>+ <radialGradient id="radial" gradientTransform="scale(5)" transform="scale(9)"></radialGradient>+ <g id="irrelevant" x="9" y="10" width="11" height="12"></g>+ <g id="irrelevant-control"></g>+ <path id="path" d="M0,0 L1,1"></path>+ <path id="path-control"></path>+ <animate id="animation" fill="blue"></animate>+ <animate id="animation-control"></animate>+ <svg id="nested" width="1" height="2"></svg>+ <svg id="nested-control"></svg>+ <g id="cascade" transform="scale(2)"></g>+ </svg>"#,+ );++ let result = vm+ .eval(+ r#"+(() => {+ const get = id => document.getElementById(id);+ const value = (id, property) =>+ getComputedStyle(get(id)).getPropertyValue(property);+ const sameProperties = (left, right, properties) =>+ properties.every(property => value(left, property) === value(right, property));++ const values = [+ value('positioned', 'x'),+ value('positioned', 'y'),+ value('positioned', 'width'),+ value('positioned', 'height'),+ value('group', 'transform'),+ value('symbol', 'transform'),+ value('group-alias', 'transform'),+ value('pattern', 'transform'),+ value('linear', 'transform'),+ value('radial', 'transform'),+ sameProperties('irrelevant', 'irrelevant-control', ['x', 'y', 'width', 'height']),+ value('path', 'd') !== value('path-control', 'd'),+ value('animation', 'fill') === value('animation-control', 'fill'),+ sameProperties('nested', 'nested-control', ['width', 'height']),+ value('cascade', 'transform')+ ];++ get('group').setAttribute('transform', 'translate(12, 3)');+ values.push(value('group', 'transform'));+ return JSON.stringify(values);+})()+"#,+ )+ .expect("SVG presentation attributes should evaluate");++ assert_eq!(+ result,+ r#"["1px","2px","3px","4px","matrix(1, 0, 0, 1, 200, 0)","matrix(1, 0, 0, 1, 100, 0)","matrix(2, 0, 0, 2, 0, 0)","matrix(2, 0, 0, 2, 0, 0)","matrix(4, 0, 0, 4, 0, 0)","matrix(5, 0, 0, 5, 0, 0)",true,true,true,true,"matrix(3, 0, 0, 3, 0, 0)","matrix(1, 0, 0, 1, 12, 3)"]"#+ );+}+#[test]fn computed_transform_scale_serializes_as_matrix() {let mut vm = new_parsed_test_vm(diff --git a/moli-selector/src/stylo/presentation.rs b/moli-selector/src/stylo/presentation.rsindex eb7135d02..4b89b2c60 100644--- a/moli-selector/src/stylo/presentation.rs+++ b/moli-selector/src/stylo/presentation.rs@@ -5,6 +5,7 @@// cascade, inheritance, and relative-length resolver own the result instead// of teaching layout about authored attribute strings.+use cssparser::serialize_string;use selectors::{Element as SelectorsElement, sink::Push};use style::{applicable_declarations::ApplicableDeclarationBlock,@@ -30,11 +31,12 @@ use super::query::QueryElement;const HTML_NAMESPACE: &str = "http://www.w3.org/1999/xhtml";const SVG_NAMESPACE: &str = "http://www.w3.org/2000/svg";-// Mirrors Blink's CSSPropertyIdForSVGAttributeName allowlist. These attributes-// participate in the author cascade as presentation hints: author rules and an-// inline style override them, while inheritance observes their parsed CSS-// value. Geometry attributes such as the root SVG width/height are handled-// separately below because they have element-specific SVG parsing rules.+// Mirrors the presentation attributes without element-specific SVG rules.+// These attributes participate in the author cascade as presentation hints:+// author rules and an inline style override them, while inheritance observes+// their parsed CSS value. Geometry and transform attributes are selected by+// `svg_presentation_property_name` because their spelling or applicability+// depends on the element.const SVG_STYLE_PRESENTATION_ATTRIBUTES: &[&str] = &["alignment-baseline","baseline-shift",@@ -96,10 +98,27 @@ const SVG_STYLE_PRESENTATION_ATTRIBUTES: &[&str] = &["writing-mode",];+const SVG_SPECIAL_PRESENTATION_ATTRIBUTES: &[&str] = &[+ "cx",+ "cy",+ "d",+ "gradientTransform",+ "height",+ "patternTransform",+ "r",+ "rx",+ "ry",+ "transform",+ "width",+ "x",+ "y",+];+/// Whether changing an attribute can change an SVG element's computed style/// without any selector dependency on that attribute.pub fn is_svg_presentation_attribute_name(name: &str) -> bool {- matches!(name, "width" | "height") || SVG_STYLE_PRESENTATION_ATTRIBUTES.contains(&name)+ SVG_STYLE_PRESENTATION_ATTRIBUTES.contains(&name)+ || SVG_SPECIAL_PRESENTATION_ATTRIBUTES.contains(&name)}impl QueryElement<'_> {@@ -118,7 +137,11 @@ impl QueryElement<'_> {let element = self.element();let mut block = PropertyDeclarationBlock::new();if element.namespace() == SVG_NAMESPACE {- if element.local_name() == "svg" {+ let is_outermost_svg = element.local_name() == "svg"+ && self+ .parent_element()+ .is_none_or(|parent| parent.element().namespace() != SVG_NAMESPACE);+ if is_outermost_svg {append_root_svg_size_declarations(element, &mut block);}@@ -463,19 +486,31 @@ fn append_svg_style_presentation_declarations(block: &mut PropertyDeclarationBlock,) {for attribute in element.attributes() {- if !attribute.namespace().is_empty()- || !SVG_STYLE_PRESENTATION_ATTRIBUTES.contains(&attribute.local_name())- {+ if !attribute.namespace().is_empty() {continue;}- let Ok(property) = PropertyId::parse_enabled_for_all_content(attribute.local_name()) else {+ let Some(property_name) = svg_presentation_property_name(element, attribute.local_name())+ else {continue;};+ let Ok(property) = PropertyId::parse_enabled_for_all_content(property_name) else {+ continue;+ };+ let mut svg_path_value = String::new();+ let value = if property_name == "d" {+ svg_path_value.push_str("path(");+ serialize_string(attribute.value(), &mut svg_path_value)+ .expect("serializing an SVG path into a String cannot fail");+ svg_path_value.push(')');+ svg_path_value.as_str()+ } else {+ attribute.value()+ };let mut declarations = SourcePropertyDeclaration::default();if parse_one_declaration_into(&mut declarations,property,- attribute.value(),+ value,Origin::Author,url_data,None,@@ -490,6 +525,70 @@ fn append_svg_style_presentation_declarations(}}+fn svg_presentation_property_name<'a>(+ element: &Element,+ attribute_name: &'a str,+) -> Option<&'a str> {+ // The timing `fill` attribute on SVG animation elements must not be+ // reinterpreted as the CSS fill presentation hint. Other presentation+ // attributes remain eligible even though they do not affect rendering.+ if attribute_name == "fill"+ && matches!(+ element.local_name(),+ "animate" | "animateMotion" | "animateTransform" | "set"+ )+ {+ return None;+ }++ if SVG_STYLE_PRESENTATION_ATTRIBUTES.contains(&attribute_name) {+ return Some(attribute_name);+ }++ let local_name = element.local_name();+ let applies = match attribute_name {+ "cx" | "cy" => matches!(local_name, "circle" | "ellipse"),+ "r" => local_name == "circle",+ "rx" | "ry" => matches!(local_name, "ellipse" | "rect"),+ "x" | "y" | "width" | "height" => {+ matches!(local_name, "foreignObject" | "image" | "rect" | "use")+ }+ "d" => local_name == "path",+ "transform" => is_svg_graphics_element(local_name),+ "patternTransform" => local_name == "pattern",+ "gradientTransform" => matches!(local_name, "linearGradient" | "radialGradient"),+ _ => false,+ };+ applies.then_some(match attribute_name {+ "patternTransform" | "gradientTransform" => "transform",+ _ => attribute_name,+ })+}++fn is_svg_graphics_element(local_name: &str) -> bool {+ matches!(+ local_name,+ "a" | "circle"+ | "defs"+ | "ellipse"+ | "foreignObject"+ | "g"+ | "image"+ | "line"+ | "path"+ | "polygon"+ | "polyline"+ | "rect"+ | "svg"+ | "symbol"+ | "switch"+ | "text"+ | "textPath"+ | "tspan"+ | "use"+ )+}+fn append_html_table_cell_padding_declarations(element: QueryElement<'_>,block: &mut PropertyDeclarationBlock,@@ -606,7 +705,7 @@ mod tests {}#[test]- fn svg_paint_attributes_are_classified_as_presentational() {+ fn svg_attributes_are_classified_as_presentational() {for name in ["fill","fill-opacity",@@ -616,6 +715,17 @@ mod tests {"shape-rendering","width","height",+ "x",+ "y",+ "cx",+ "cy",+ "r",+ "rx",+ "ry",+ "d",+ "transform",+ "patternTransform",+ "gradientTransform",] {assert!(is_svg_presentation_attribute_name(name),@@ -623,7 +733,65 @@ mod tests {);}assert!(!is_svg_presentation_attribute_name("viewBox"));- assert!(!is_svg_presentation_attribute_name("d"));+ }++ #[test]+ fn svg_special_presentation_attributes_follow_element_scopes() {+ let element = |local_name: &str| {+ Element::new(+ local_name.to_owned(),+ SVG_NAMESPACE.to_owned(),+ None,+ Vec::new(),+ )+ };++ for local_name in ["foreignObject", "image", "rect", "use"] {+ let element = element(local_name);+ for attribute in ["x", "y", "width", "height"] {+ assert_eq!(+ svg_presentation_property_name(&element, attribute),+ Some(attribute),+ "{attribute} on {local_name}"+ );+ }+ }+ for local_name in ["g", "symbol"] {+ let element = element(local_name);+ assert_eq!(+ svg_presentation_property_name(&element, "transform"),+ Some("transform")+ );+ }+ assert_eq!(+ svg_presentation_property_name(&element("pattern"), "patternTransform"),+ Some("transform")+ );+ assert_eq!(+ svg_presentation_property_name(&element("linearGradient"), "gradientTransform"),+ Some("transform")+ );+ assert_eq!(+ svg_presentation_property_name(&element("radialGradient"), "gradientTransform"),+ Some("transform")+ );++ let group = element("g");+ for attribute in ["x", "y", "width", "height"] {+ assert_eq!(svg_presentation_property_name(&group, attribute), None);+ }+ for local_name in ["pattern", "linearGradient", "radialGradient"] {+ assert_eq!(+ svg_presentation_property_name(&element(local_name), "transform"),+ None+ );+ }+ let animate = element("animate");+ assert_eq!(svg_presentation_property_name(&animate, "fill"), None);+ assert_eq!(+ svg_presentation_property_name(&animate, "stroke"),+ Some("stroke")+ );}#[test]