Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.
TypeScript 3dagent-skillsai-agentsai-videobevyclaude-codegame-enginegpulinuxmacosmotion-designmotion-graphicsreactrusttypescriptvideo-editingvideo-renderingwgpuwindows
#27: an SVG <text>, <image> and <title> keep their SVG attribute names
James Dimachkie2e6ff46aea29
diff --git a/runtime/page/src/bevy_page.rs b/runtime/page/src/bevy_page.rsindex 563aa10f..15c085b2 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.rsindex 78414180..9c4e7273 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, hoout.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, hol.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.rsindex 2c87b4ae..5f84c389 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 partspub(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 modulepub(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 takediff --git a/systems/page-ops/src/lib.rs b/systems/page-ops/src/lib.rsindex 8c47e5af..0ca04e9b 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.