From 2e6ff46aea29dacbd452bcc6ba224fb4c51cf3c6 Mon Sep 17 00:00:00 2001 From: James Dimachkie Date: Sat, 7 Nov 2026 16:53:04 -0800 Subject: [PATCH 1/2] #27: an SVG , and keep their SVG attribute names The composition walk never said `$ns: "svg"` for the three SVG tags HTML also has, so inside an <svg> they became HTML elements and their camelCase attributes were lowercased (textAnchor to textanchor, fontSize to fontsize), which usvg doesn't know. The walk now tracks whether it is inside an <svg> and not back in HTML under a <foreignObject> (IN_SVG, beside IN_3D), and gives those three `$ns: "svg"` there, as sdk/ui's host does. An update in ops.rs also treats a node as SVG's when its element is in SVG's namespace, so an animated fontSize keeps `font-size` on every frame, on sdk/ui's path too. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --- runtime/page/src/bevy_page.rs | 1 + runtime/page/src/elements.rs | 50 ++++++++++++++++++++++++++++++++++- runtime/page/src/walk.rs | 3 +++ systems/page-ops/src/lib.rs | 41 +++++++++++++++++++++++++++- 4 files changed, 93 insertions(+), 2 deletions(-) diff --git a/runtime/page/src/bevy_page.rs b/runtime/page/src/bevy_page.rs index 563aa10ff..15c085b2a 100644 --- a/runtime/page/src/bevy_page.rs +++ b/runtime/page/src/bevy_page.rs @@ -57,6 +57,7 @@ impl BevyPage { SCENE_SLOT.with(|c| c.set(None)); VIEW_HOLDER.with(|c| c.set(None)); IN_3D.with(|c| c.set(0)); + IN_SVG.with(|c| c.set(false)); APP_SCREENS.with(|c| c.borrow_mut().clear()); TEXTURE_SLOTS.with(|c| c.borrow_mut().clear()); REFUSED.with(|c| c.borrow_mut().clear()); diff --git a/runtime/page/src/elements.rs b/runtime/page/src/elements.rs index 784141801..9c4e7273c 100644 --- a/runtime/page/src/elements.rs +++ b/runtime/page/src/elements.rs @@ -134,7 +134,14 @@ pub(crate) fn walk(react: &World, e: Entity, parent: u64, at: At, dir: &Path, ho out.put(Want { id, parent, kind: kind.to_string(), props: page, text: None, path }, None); i } else { - out.element(id, parent, at, tail, kind, props, None, || Want { id, parent, kind: kind.to_string(), props: page_props(kind, props, dir), text: None, path: String::new() }) + let svg = svg_owned(kind, IN_SVG.with(|c| c.get())); + out.element(id, parent, at, tail, kind, props, None, || { + let mut page = page_props(kind, props, dir); + if svg { + page.insert("$ns".into(), Value::from("svg")); + } + Want { id, parent, kind: kind.to_string(), props: page, text: None, path: String::new() } + }) }; list.push(id); // an element's own string: a text child of its own @@ -145,12 +152,30 @@ pub(crate) fn walk(react: &World, e: Entity, parent: u64, at: At, dir: &Path, ho l.push(tid); out.order.insert(id, l); } else { + let was = IN_SVG.with(|c| c.replace(svg_inside(kind, c.get()))); walk(react, k, id, At::In(me), dir, holders, out, videos, screens); + IN_SVG.with(|c| c.set(was)); } } out.order.insert(parent, list); } +/// Whether an element's children are inside an <svg>: from an <svg> down, until a <foreignObject> holds HTML again. +pub(crate) fn svg_inside(kind: &str, inside: bool) -> bool { + match kind { + "svg" => true, + "foreignObject" => false, + _ => inside, + } +} + +/// Whether an element is SVG's although HTML has its tag too: a `text`, `image` or `title` inside an <svg>. `in_svg` +/// (raea-sys-page) leaves those three out as ambiguous, so the walk says so with `$ns`, as sdk/ui's host does; without +/// it a composition's `textAnchor` and `fontSize` were lowercased as HTML attributes, which usvg doesn't know. +pub(crate) fn svg_owned(kind: &str, inside: bool) -> bool { + inside && matches!(kind, "text" | "image" | "title") +} + /// A stylesheet's `url()`s that name the composition's files as what raea-runtime-js's @font-face and backgrounds read /// (raea-runtime-bundle's css_file_urls: a file URL on Windows, #28), relative ones from the edit's folder. pub(crate) fn media_urls(text: &str, dir: &Path) -> String { @@ -242,6 +267,29 @@ fn css_urls(dir: &Path, s: &str) -> String { raea_runtime_react::scene::css_file_urls(s, dir, dir, |f| REFUSED.with(|c| c.borrow_mut().push(format!("missing image: {}", f.display())))) } +#[cfg(test)] +mod svg_namespace_tests { + use super::{svg_inside, svg_owned}; + + /// a text, image or title is SVG's only inside an <svg>, and not under its <foreignObject>, which holds HTML + #[test] + fn text_image_and_title_are_svgs_only_inside_an_svg() { + let mut inside = false; + for kind in ["div", "svg", "g"] { + inside = svg_inside(kind, inside); + } + assert!(inside); + for kind in ["text", "image", "title"] { + assert!(svg_owned(kind, inside), "{kind} inside an <svg>"); + assert!(!svg_owned(kind, false), "{kind} in HTML"); + } + assert!(!svg_owned("rect", inside), "a rect is SVG's anyway (in_svg)"); + let html = svg_inside("foreignObject", inside); + assert!(!svg_owned("title", html), "a title under a <foreignObject> is HTML's"); + assert!(svg_inside("svg", html), "an <svg> in that HTML starts SVG again"); + } +} + #[cfg(test)] mod css_url_tests { #[test] diff --git a/runtime/page/src/walk.rs b/runtime/page/src/walk.rs index 2c87b4ae8..5f84c3899 100644 --- a/runtime/page/src/walk.rs +++ b/runtime/page/src/walk.rs @@ -242,6 +242,9 @@ thread_local! { pub(crate) static VIEW_HOLDER: std::cell::Cell<Option<u64>> = const { std::cell::Cell::new(None) }; /// how deep the walk is inside 3D parts pub(crate) static IN_3D: std::cell::Cell<u32> = const { std::cell::Cell::new(0) }; + /// whether the walk is inside an <svg> (not back in HTML under its <foreignObject>): a `text`, `image` or `title` + /// there is SVG's, as sdk/ui's host says with `$ns`, so its attributes keep their SVG names (svg_owned) + pub(crate) static IN_SVG: std::cell::Cell<bool> = const { std::cell::Cell::new(false) }; /// the app screens (`<Screen app>`) the last walk met: each one's name, size and module pub(crate) static APP_SCREENS: std::cell::RefCell<Vec<(String, u32, u32, String)>> = const { std::cell::RefCell::new(Vec::new()) }; /// why the last walk's page can't be rendered as written (an <img> of a file that isn't there, `page_props`; a take diff --git a/systems/page-ops/src/lib.rs b/systems/page-ops/src/lib.rs index 8c47e5af6..0ca04e9b1 100644 --- a/systems/page-ops/src/lib.rs +++ b/systems/page-ops/src/lib.rs @@ -233,7 +233,8 @@ pub fn apply(doc: &mut BaseDocument, nodes: &mut Nodes, ops: Vec<Op>) { styles.push(p); } } - let svg = in_svg(kind); + // (a text, image or title is SVG's when it was made in SVG's namespace: `$ns` at its create) + let svg = in_svg(kind) || m.element_name(n).is_some_and(|q| q.ns == ns!(svg)); // a textarea's value is its text too: Blitz makes its editor from its text (construct.rs) let area = (kind == "editableText" && m.element_name(n).is_some_and(|q| &*q.local == "textarea")).then(|| set.as_ref()?.get("value")?.as_str().map(str::to_string)).flatten(); let kept = nodes.props.entry(id).or_default(); @@ -415,6 +416,44 @@ mod tests { assert_eq!(attr_name("strokeWidth", true).as_deref(), Some("stroke-width")); assert_eq!(attr_name("viewBox", true).as_deref(), Some("viewBox")); } + + /// An SVG <text> made in SVG's namespace (`$ns`) keeps SVG's attribute names when React changes them, as an animated + /// `fontSize` does every frame: `font-size`, never the lowercased `fontsize` usvg doesn't know. + #[test] + fn an_svg_texts_attributes_keep_their_svg_names_when_they_change() { + use blitz_dom::DocumentConfig; + use serde_json::json; + let mut doc = BaseDocument::new(DocumentConfig::default()); + let body = { + let mut m = doc.mutate(); + let root = m.doc.root_node().id; + let html = m.create_element(super::attrs::name("html", false), vec![]); + m.append_children(root, &[html]); + let body = m.create_element(super::attrs::name("body", false), vec![]); + m.append_children(html, &[body]); + body + }; + let mut nodes = Nodes::new(body); + let props = |v: Value| v.as_object().unwrap().clone(); + apply(&mut doc, &mut nodes, vec![ + Op::Create { id: 1, kind: "svg".into(), props: props(json!({"width": 640, "height": 360})), text: None }, + Op::Create { id: 2, kind: "text".into(), props: props(json!({"$ns": "svg", "x": 320, "y": 190, "fontSize": 48, "textAnchor": "middle"})), text: Some("Middle".into()) }, + Op::Append { parent: 0, child: 1 }, + Op::Append { parent: 1, child: 2 }, + ]); + let text = nodes.by_id[&2]; + let attrs = |doc: &BaseDocument| -> Vec<(String, String)> { + let el = doc.get_node(text).unwrap().element_data().unwrap(); + el.attrs().iter().map(|a| (a.name.local.to_string(), a.value.to_string())).collect() + }; + let has = |a: &[(String, String)], k: &str, v: &str| a.iter().any(|(n, x)| n == k && x == v); + let made = attrs(&doc); + assert!(has(&made, "font-size", "48") && has(&made, "text-anchor", "middle"), "{made:?}"); + apply(&mut doc, &mut nodes, vec![Op::Update { id: 2, set: Some(props(json!({"fontSize": 60, "textAnchor": "end"}))), unset: None, text: None }]); + let changed = attrs(&doc); + assert!(has(&changed, "font-size", "60") && has(&changed, "text-anchor", "end"), "{changed:?}"); + assert!(!changed.iter().any(|(n, _)| n == "fontsize" || n == "textanchor"), "lowercased as HTML: {changed:?}"); + } } /// A menu's items (`menuItem`: a `menuitem` attribute), in document order. From 07908a430d30094c4264d642e5d1eee45a5c6b3a Mon Sep 17 00:00:00 2001 From: James Dimachkie <jamesdimachkie@gmail.com> Date: Sat, 7 Nov 2026 17:32:56 -0800 Subject: [PATCH 2/2] #27: the regression test moves to raea-runtime-page, through the frame diff raea-sys-page is at its 2000-line limit (tools/size.sh), so the test of an SVG <text>'s attribute names leaves ops.rs. In raea-runtime-page it takes the path a composition's frames take: the walk's Wants (the <text> with `$ns`), then Tree::diff, whose second frame sends only the changed fontSize and textAnchor, then ops::apply. It fails without the ops.rs change: the update adds fontsize and textanchor beside the stale font-size. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> --- runtime/page/src/elements.rs | 29 +++++++++++++++++++++++++++ systems/page-ops/src/lib.rs | 38 ------------------------------------ 2 files changed, 29 insertions(+), 38 deletions(-) diff --git a/runtime/page/src/elements.rs b/runtime/page/src/elements.rs index 9c4e7273c..eb7f8a3ee 100644 --- a/runtime/page/src/elements.rs +++ b/runtime/page/src/elements.rs @@ -288,6 +288,35 @@ mod svg_namespace_tests { assert!(!svg_owned("title", html), "a title under a <foreignObject> is HTML's"); assert!(svg_inside("svg", html), "an <svg> in that HTML starts SVG again"); } + + /// An SVG <text> keeps SVG's attribute names from frame to frame, as an animated `fontSize` changes them: the walk + /// gives it `$ns`, and a later frame's diff sends only what changed, so the page tells it's SVG's by its namespace + #[test] + fn an_svg_texts_attributes_keep_their_svg_names_from_frame_to_frame() { + use bevy::prelude::{Entity, UVec2}; + use raea_core_wire::tree::{Tree, Want}; + use serde_json::{Value, json}; + let mut docs = raea_sys_page::Docs::default(); + let doc = docs.open(Entity::PLACEHOLDER, raea_sys_page::Page { size: UVec2::new(640, 360), scale: 1.0 }); + let mut nodes = raea_sys_page::ops::Nodes::new(doc.root_node().id); + let (mut tree, order) = (Tree::default(), std::collections::HashMap::from([(0, vec![1]), (1, vec![2]), (2, vec![3])])); + let want = |id, parent, kind: &str, props: Value, text: Option<&str>| Want { id, parent, kind: kind.into(), props: props.as_object().unwrap().clone(), text: text.map(Into::into), path: String::new() }; + let mut attrs_at = |size: u32, anchor: &str| { + let frame = [ + want(1, 0, "svg", json!({"width": 640, "height": 360}), None), + want(2, 1, "text", json!({"$ns": "svg", "x": 320, "y": 190, "fontSize": size, "textAnchor": anchor}), None), + want(3, 2, "text", json!({}), Some("Middle")), + ]; + raea_sys_page::ops::apply(doc, &mut nodes, tree.diff(&frame, &order).0); + let el = doc.get_node(nodes.by_id[&2]).unwrap().element_data().unwrap(); + el.attrs().iter().map(|a| format!("{}={}", a.name.local, a.value)).collect::<Vec<_>>() + }; + let first = attrs_at(48, "middle"); + assert!(first.contains(&"font-size=48".into()) && first.contains(&"text-anchor=middle".into()), "{first:?}"); + let next = attrs_at(60, "end"); + assert!(next.contains(&"font-size=60".into()) && next.contains(&"text-anchor=end".into()), "{next:?}"); + assert!(!next.iter().any(|a| a.starts_with("fontsize=") || a.starts_with("textanchor=")), "lowercased as HTML: {next:?}"); + } } #[cfg(test)] diff --git a/systems/page-ops/src/lib.rs b/systems/page-ops/src/lib.rs index 0ca04e9b1..d65d4debc 100644 --- a/systems/page-ops/src/lib.rs +++ b/systems/page-ops/src/lib.rs @@ -416,44 +416,6 @@ mod tests { assert_eq!(attr_name("strokeWidth", true).as_deref(), Some("stroke-width")); assert_eq!(attr_name("viewBox", true).as_deref(), Some("viewBox")); } - - /// An SVG <text> made in SVG's namespace (`$ns`) keeps SVG's attribute names when React changes them, as an animated - /// `fontSize` does every frame: `font-size`, never the lowercased `fontsize` usvg doesn't know. - #[test] - fn an_svg_texts_attributes_keep_their_svg_names_when_they_change() { - use blitz_dom::DocumentConfig; - use serde_json::json; - let mut doc = BaseDocument::new(DocumentConfig::default()); - let body = { - let mut m = doc.mutate(); - let root = m.doc.root_node().id; - let html = m.create_element(super::attrs::name("html", false), vec![]); - m.append_children(root, &[html]); - let body = m.create_element(super::attrs::name("body", false), vec![]); - m.append_children(html, &[body]); - body - }; - let mut nodes = Nodes::new(body); - let props = |v: Value| v.as_object().unwrap().clone(); - apply(&mut doc, &mut nodes, vec![ - Op::Create { id: 1, kind: "svg".into(), props: props(json!({"width": 640, "height": 360})), text: None }, - Op::Create { id: 2, kind: "text".into(), props: props(json!({"$ns": "svg", "x": 320, "y": 190, "fontSize": 48, "textAnchor": "middle"})), text: Some("Middle".into()) }, - Op::Append { parent: 0, child: 1 }, - Op::Append { parent: 1, child: 2 }, - ]); - let text = nodes.by_id[&2]; - let attrs = |doc: &BaseDocument| -> Vec<(String, String)> { - let el = doc.get_node(text).unwrap().element_data().unwrap(); - el.attrs().iter().map(|a| (a.name.local.to_string(), a.value.to_string())).collect() - }; - let has = |a: &[(String, String)], k: &str, v: &str| a.iter().any(|(n, x)| n == k && x == v); - let made = attrs(&doc); - assert!(has(&made, "font-size", "48") && has(&made, "text-anchor", "middle"), "{made:?}"); - apply(&mut doc, &mut nodes, vec![Op::Update { id: 2, set: Some(props(json!({"fontSize": 60, "textAnchor": "end"}))), unset: None, text: None }]); - let changed = attrs(&doc); - assert!(has(&changed, "font-size", "60") && has(&changed, "text-anchor", "end"), "{changed:?}"); - assert!(!changed.iter().any(|(n, _)| n == "fontsize" || n == "textanchor"), "lowercased as HTML: {changed:?}"); - } } /// A menu's items (`menuItem`: a `menuitem` attribute), in document order.