Node.JS: Fixed panic when converting brushes to colors

The material components gallery would run `new ui.ListItem({...})` where one of the fields would be a default constructed brush,
for which the "conversion" to a brush in Rust would fail because that case wasn't handled.
This commit is contained in:
Simon Hausmann 2025-06-30 15:59:39 +02:00 committed by Simon Hausmann
parent 1b6b608d41
commit 65d50d0565
3 changed files with 26 additions and 2 deletions

View file

@ -9,6 +9,10 @@ All notable changes to this project are documented in this file.
- Minimum Supported Rust Version (MSRV) is 1.85
### Node.js API
- Fixed panic when attempting to convert brushes to colors.
## [1.12.1] - 2025-06-25
### General

View file

@ -496,6 +496,17 @@ test("get/set brush properties", (t) => {
t.deepEqual(ref_color.blue, 0);
t.deepEqual(ref_color.alpha, 255);
}
// ref is a brush, but setting to a color should not throw, but take the brush's color.
instance!.setProperty("ref-color", ref);
instance_ref = instance!.getProperty("ref-color");
if (t.true(instance_ref instanceof private_api.SlintBrush)) {
const ref_color = (instance_ref as private_api.SlintBrush).color;
t.deepEqual(ref_color.red, ref.color.red);
t.deepEqual(ref_color.green, ref.color.green);
t.deepEqual(ref_color.blue, ref.color.blue);
t.deepEqual(ref_color.alpha, ref.color.alpha);
}
});
test("get/set enum properties", (t) => {

View file

@ -112,8 +112,17 @@ pub fn to_value(env: &Env, unknown: JsUnknown, typ: &Type) -> Result<Value> {
return unknown.coerce_to_string().and_then(string_to_brush)
}
Ok(ValueType::Object) => {
if let Ok(rgb_color) = unknown.coerce_to_object() {
return brush_from_color(rgb_color);
if let Ok(rgb_color_or_brush) = unknown.coerce_to_object() {
if let Some(direct_brush) =
rgb_color_or_brush.get("brush").ok().flatten().and_then(
|maybe_slintbrush| {
env.get_value_external::<Brush>(&maybe_slintbrush).ok()
},
)
{
return Ok(Value::Brush(direct_brush.color().into()));
}
return brush_from_color(rgb_color_or_brush);
}
}
_ => {}