Skip to content

programmatic video rendering framework that is actually fast

fix(svgr): keep ids and references as strings in compile-time trees (#182)

Dmitriy Kovalenkob7fc055f7028
diff --git a/fframes-test-utils/_svgr_snapshots/$string_attributes_that_look_like_colors.snapshot.txt b/fframes-test-utils/_svgr_snapshots/$string_attributes_that_look_like_colors.snapshot.txt
new file mode 100644
index 00000000..5d9711be
--- /dev/null
+++ b/fframes-test-utils/_svgr_snapshots/$string_attributes_that_look_like_colors.snapshot.txt
@@ -0,0 +1,18 @@
+(Runtime)
+<svg width="300" height="100" viewBox="0 0 300 100" xmlns="http://www.w3.org/2000/svg">
+ <defs>
+ <linearGradient id="gold" x1="0" y1="0" x2="1" y2="0" gradientUnits="userSpaceOnUse" gradientTransform="matrix(100 0 0 100 0 0)">
+ <stop offset="0" stop-color="#fff7c2"/>
+ <stop offset="1" stop-color="#e09a00"/>
+ </linearGradient>
+ <linearGradient id="10" x1="0" y1="0" x2="1" y2="0" gradientUnits="userSpaceOnUse" gradientTransform="matrix(100 0 0 100 100 0)">
+ <stop offset="0" stop-color="#00e676"/>
+ <stop offset="1" stop-color="#2979ff"/>
+ </linearGradient>
+ </defs>
+ <path fill="url(#gold)" stroke="none" d="M 0 0 L 100 0 L 100 100 L 0 100 Z"/>
+ <path fill="url(#10)" stroke="none" d="M 100 0 L 200 0 L 200 100 L 100 100 Z"/>
+ <g transform="matrix(1 0 0 1 210 10)">
+ <path fill="#0000ff" stroke="none" d="M 0 0 L 80 0 L 80 80 L 0 80 Z"/>
+ </g>
+</svg>
diff --git a/fframes-test-utils/src/svgr_spec.rs b/fframes-test-utils/src/svgr_spec.rs
index be1cc154..cf721094 100644
--- a/fframes-test-utils/src/svgr_spec.rs
+++ b/fframes-test-utils/src/svgr_spec.rs
@@ -387,3 +387,31 @@ pub fn text_literal_children_next_to_blocks() {
),
);
}
+
+/// Ids and references are strings even when they spell a color, a number or a
+/// hex value (`id="gold"`, `href="#abc"`, `id="10"`): the gradients and the `<use>` must
+/// resolve exactly like in the runtime parser.
+#[test]
+pub fn string_attributes_that_look_like_colors() {
+ assert_compile_time_svgr_eq_runtime(
+ "string_attributes_that_look_like_colors",
+ svgr!(
+ <svg xmlns="http://www.w3.org/2000/svg" width="300" height="100">
+ <defs>
+ <linearGradient id="gold" x1="0" y1="0" x2="1" y2="0">
+ <stop offset="0" stop-color="#fff7c2" />
+ <stop offset="1" stop-color="#e09a00" />
+ </linearGradient>
+ <linearGradient id="10">
+ <stop offset="0" stop-color="#00e676" />
+ <stop offset="1" stop-color="#2979ff" />
+ </linearGradient>
+ <rect id="abc" width="80" height="80" />
+ </defs>
+ <rect x="0" y="0" width="100" height="100" fill="url(#gold)" />
+ <rect x="100" y="0" width="100" height="100" fill="url(#10)" />
+ <use href="#abc" x="210" y="10" fill="blue" />
+ </svg>
+ ),
+ );
+}
diff --git a/svgr-macro/src/nodes_to_svgtree.rs b/svgr-macro/src/nodes_to_svgtree.rs
index 85614546..427cc77c 100644
--- a/svgr-macro/src/nodes_to_svgtree.rs
+++ b/svgr-macro/src/nodes_to_svgtree.rs
@@ -164,6 +164,21 @@ fn points_to_segments(value: &str, element: EId) -> Option<Vec<PathSegment>> {
Some(segments)
}
+/// Attributes usvgr reads a color from. Only these get a static `Color`: any other attribute
+/// keeps its text, so `id="gold"` or `href="#abc"` stay strings instead of silently losing
+/// the element its id or reference.
+fn accepts_color(aid: AId) -> bool {
+ matches!(
+ aid,
+ AId::Fill
+ | AId::Stroke
+ | AId::StopColor
+ | AId::FloodColor
+ | AId::LightingColor
+ | AId::Color
+ )
+}
+
fn inline_attribute_value(value: &str, aid: AId, element: EId) -> TokenStream {
// Special handling for path data - parse at compile time
if aid == AId::D {
@@ -199,7 +214,10 @@ fn inline_attribute_value(value: &str, aid: AId, element: EId) -> TokenStream {
SvgAttributeValue::Float(#float, StringStorage::Borrowed(#value))
}
}
- } else if let Ok(color) = svgtree::svgrtypes::Color::from_str(value) {
+ } else if let Some(color) = accepts_color(aid)
+ .then(|| svgtree::svgrtypes::Color::from_str(value).ok())
+ .flatten()
+ {
quote! {
SvgAttributeValue::Color(#color)
}