Skip to content

armgit / raea

Public Imported from GitHub
0

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.

A stylesheet's url()s naming the composition's files reach the page as file URLs on Windows (part of #28). An inline <style>'s font URL was written as a raw Windows path, whose backslashes CSS read as escapes and whose C: it read as a URL scheme. One rewriter (runtime/bundle urls.rs) now serves inline <style> text, style props, brand.css and the CSS a bundle imports, writing each file through raea_io_home::src as <img> does. A relative URL starts from the stylesheet's folder (brand/ for brand.css, the edit's folder otherwise), and /media/ from the edit's. Its unit tests check a Windows path on any system: no backslash is left, and the URL is file:///C:/...

shinyobjectz67114ed2ca51
diff --git a/io/home/src/lib.rs b/io/home/src/lib.rs
index 8937836f..53b1976f 100644
--- a/io/home/src/lib.rs
+++ b/io/home/src/lib.rs
@@ -98,7 +98,9 @@ pub fn src(p: &std::path::Path) -> String {
src_on(&p.display().to_string(), cfg!(windows))
}
-fn src_on(s: &str, windows: bool) -> String {
+/// `src` of the path `s` as Windows writes it (`windows`) or as Unix does, whichever system asks (a test of Windows'
+/// form runs on the Mac too)
+pub fn src_on(s: &str, windows: bool) -> String {
if !windows {
return s.to_string();
}
diff --git a/runtime/bundle/src/lib.rs b/runtime/bundle/src/lib.rs
index 17133497..0235784f 100644
--- a/runtime/bundle/src/lib.rs
+++ b/runtime/bundle/src/lib.rs
@@ -15,11 +15,13 @@ mod outside;
mod pid;
mod rewrite;
pub mod styles;
+mod urls;
pub use cache::*;
pub use entry::*;
pub use fresh::*;
pub use outside::*;
pub use rewrite::*;
+pub use urls::*;
pub use raea_core_scene::media_path;
#[cfg(test)]
mod tests;
@@ -39,7 +41,9 @@ pub fn brand_css(dir: &Path, w: u32) -> Option<String> {
let f = here.join("brand").join("brand.css");
if let Ok(css) = std::fs::read_to_string(&f) {
let kept: Vec<&str> = css.lines().filter(|l| !l.trim_start().starts_with("@import")).collect();
- return Some(format!("{}\n:root {{ --brand-scale: {}; }}\n", kept.join("\n"), 2.5 * w as f64 / 1080.0));
+ // its fonts and images as files: /media/ from the edit, the rest from brand/ (urls.rs)
+ let kept = css_file_urls(&kept.join("\n"), dir, &here.join("brand"), |_| ());
+ return Some(format!("{kept}\n:root {{ --brand-scale: {}; }}\n", 2.5 * w as f64 / 1080.0));
}
d = here.parent();
}
@@ -55,7 +59,8 @@ pub fn bundle_css(js: &str) -> Option<String> {
/// A page's own CSS: the space's brand kit (brand_css), then the CSS the bundle `js` imported (bundle_css)
pub fn page_css(dir: &Path, w: u32, js: &Path) -> Option<String> {
- let bundled = std::fs::read_to_string(js).ok().and_then(|t| bundle_css(&t));
+ // its url()s as files from the edit's folder (urls.rs), where the CSS files it imports are found
+ let bundled = std::fs::read_to_string(js).ok().and_then(|t| bundle_css(&t)).map(|c| css_file_urls(&c, dir, dir, |_| ()));
match (brand_css(dir, w), bundled) {
(Some(b), Some(c)) => Some(format!("{b}\n{c}")),
(b, c) => b.or(c),
diff --git a/runtime/bundle/src/urls.rs b/runtime/bundle/src/urls.rs
new file mode 100644
index 00000000..70ed2cca
--- /dev/null
+++ b/runtime/bundle/src/urls.rs
@@ -0,0 +1,86 @@
+//! A stylesheet's `url()`s that name the composition's files, written as what the page reads them by: a path on the Mac
+//! and Linux, a file URL on Windows (raea_io_home::src). Written raw, a Windows path breaks twice in CSS: its
+//! backslashes are escapes (`\A` a newline, `\c` and the hex digits after it a character) and `C:` a URL scheme (#28).
+
+use std::path::Path;
+
+/// `css` with each `url(..)` naming a file (not a URL with a scheme, a data: URI or a `#fragment`) as that file, quoted:
+/// `/media/...` (staticFile's) found from the edit's folder `media` (media_path), anything else from `rel`, the folder
+/// the stylesheet's own relative URLs start from. `missing` hears of each file that isn't there.
+pub fn css_file_urls(css: &str, media: &Path, rel: &Path, mut missing: impl FnMut(&Path)) -> String {
+ if !css.contains("url(") {
+ return css.to_string();
+ }
+ let mut out = String::with_capacity(css.len());
+ let mut rest = css;
+ while let Some(at) = rest.find("url(") {
+ out.push_str(&rest[..at + 4]);
+ rest = &rest[at + 4..];
+ let Some(end) = rest.find(')') else { break };
+ let inner = rest[..end].trim();
+ let quote = inner.chars().next().filter(|c| *c == '"' || *c == '\'');
+ let url = quote.map_or(inner, |q| inner.trim_matches(q));
+ if url.is_empty() || url.contains("://") || url.starts_with("data:") || url.starts_with('#') {
+ out.push_str(&rest[..end]);
+ } else {
+ let file = raea_core_scene::media_path(if url.starts_with("/media/") { media } else { rel }, url);
+ if !file.exists() {
+ missing(&file);
+ }
+ out.push_str(&format!("\"{}\"", src(&file)));
+ }
+ rest = &rest[end..];
+ }
+ out.push_str(rest);
+ out
+}
+
+#[cfg(not(test))]
+fn src(p: &Path) -> String {
+ raea_io_home::src(p)
+}
+
+// a test asks for Windows' form by a path's look, so a Windows path is checked on any system
+#[cfg(test)]
+fn src(p: &Path) -> String {
+ raea_io_home::src_on(&p.display().to_string(), p.display().to_string().as_bytes().get(1) == Some(&b':'))
+}
+
+#[cfg(test)]
+mod tests {
+ use super::css_file_urls;
+ use std::path::Path;
+
+ #[test]
+ fn a_windows_path_is_a_file_url_with_no_backslash() {
+ let dir = Path::new(r"C:\Users\a\AppData\Local\Temp\claude\edit");
+ let css = r#"@font-face { font-family: X; src: url("/media/fonts/x.woff2") format("woff2"); } .a { background: url(bg.png) }"#;
+ let mut gone = Vec::new();
+ let out = css_file_urls(css, dir, dir, |p| gone.push(p.to_path_buf()));
+ assert!(!out.contains('\\'), "{out}");
+ assert!(out.contains(r#"url("file:///C:/Users/a/AppData/Local/Temp/claude/edit/"#), "{out}");
+ assert!(out.contains(r#"fonts/x.woff2") format("woff2")"#) && out.contains(r#"bg.png") }"#), "{out}");
+ assert_eq!(gone.len(), 2);
+ }
+
+ #[test]
+ fn urls_naming_no_file_stay() {
+ let dir = std::env::temp_dir();
+ let css = r#"a { b: url(https://x.io/f.woff2), url('data:font/woff2;base64,AA'), url(#clip), url() }"#;
+ assert_eq!(css_file_urls(css, &dir, &dir, |_| panic!("no file named")), css);
+ assert_eq!(css_file_urls("a { color: red }", &dir, &dir, |_| ()), "a { color: red }");
+ }
+
+ #[test]
+ fn a_relative_url_starts_from_the_stylesheets_folder_and_media_from_the_edits() {
+ let edit = std::env::temp_dir().join(format!("raea-urls-{}", std::process::id()));
+ let brand = edit.join("brand");
+ std::fs::create_dir_all(edit.join("media")).unwrap();
+ std::fs::create_dir_all(&brand).unwrap();
+ std::fs::write(edit.join("media/m.woff2"), b"").unwrap();
+ std::fs::write(brand.join("b.woff2"), b"").unwrap();
+ let out = css_file_urls("url(/media/m.woff2) url(b.woff2)", &edit, &brand, |p| panic!("{p:?} is there"));
+ let _ = std::fs::remove_dir_all(&edit);
+ assert_eq!(out, format!("url(\"{}\") url(\"{}\")", edit.join("media/m.woff2").display(), brand.join("b.woff2").display()));
+ }
+}
diff --git a/runtime/page/src/elements.rs b/runtime/page/src/elements.rs
index 73187a53..78414180 100644
--- a/runtime/page/src/elements.rs
+++ b/runtime/page/src/elements.rs
@@ -151,30 +151,10 @@ pub(crate) fn walk(react: &World, e: Entity, parent: u64, at: At, dir: &Path, ho
out.order.insert(parent, list);
}
-/// A stylesheet's `url()`s of the composition's media (`/media/...`, staticFile's) as the files' paths, which
-/// raea-runtime-js's @font-face and backgrounds read; other text as it is.
+/// 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 {
- if !text.contains("url(") || !text.contains("/media/") {
- return text.to_string();
- }
- let mut out = String::with_capacity(text.len());
- let mut rest = text;
- while let Some(i) = rest.find("url(") {
- out.push_str(&rest[..i + 4]);
- rest = &rest[i + 4..];
- let Some(j) = rest.find(')') else { break };
- let inner = rest[..j].trim();
- let q = inner.chars().next().filter(|c| *c == '"' || *c == '\'');
- let url = q.map_or(inner, |c| inner.trim_matches(c));
- if url.starts_with("/media/") {
- out.push_str(&format!("\"{}\"", raea_runtime_react::scene::media_path(dir, url).display()));
- } else {
- out.push_str(&rest[..j]);
- }
- rest = &rest[j..];
- }
- out.push_str(rest);
- out
+ raea_runtime_react::scene::css_file_urls(text, dir, dir, |_| ())
}
/// A <Video>'s crop (raea-page's `fit_rect`) at source time `at`, from its framing="face" and punchIn (motion.feature,
@@ -256,31 +236,10 @@ pub(crate) fn page_props(kind: &str, props: Option<&Map<String, Value>>, dir: &P
out
}
-/// A CSS value with each `url(..)` that names a media file (not a URL with a scheme, nor a data: URI) turned into
-/// that file's path, as an img's src is (a background or a border-image of staticFile("x.png")).
+/// A CSS value with each `url(..)` that names a media file as that file, as an img's src is (a background or a
+/// border-image of staticFile("x.png")); one that isn't there refuses the render.
fn css_urls(dir: &Path, s: &str) -> String {
- let mut out = String::with_capacity(s.len());
- let mut rest = s;
- while let Some(at) = rest.find("url(") {
- out.push_str(&rest[..at + 4]);
- rest = &rest[at + 4..];
- let Some(end) = rest.find(')') else { break };
- let inner = rest[..end].trim();
- let quote = inner.chars().next().filter(|c| *c == '"' || *c == '\'');
- let path = quote.map_or(inner, |q| inner.trim_matches(q));
- if path.contains("://") || path.starts_with("data:") || path.is_empty() {
- out.push_str(&rest[..end]);
- } else {
- let file = raea_runtime_react::scene::media_path(dir, path);
- if !file.exists() {
- REFUSED.with(|c| c.borrow_mut().push(format!("missing image: {}", file.display())));
- }
- out.push_str(&format!("\"{}\"", raea_io_home::src(&file)));
- }
- rest = &rest[end..];
- }
- out.push_str(rest);
- out
+ 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)]