Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions runtime/page/src/bevy_page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
79 changes: 78 additions & 1 deletion runtime/page/src/elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -242,6 +267,58 @@ 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");
}

/// 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)]
mod css_url_tests {
#[test]
Expand Down
3 changes: 3 additions & 0 deletions runtime/page/src/walk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion systems/page-ops/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Loading