* claim: #8358 * Components: a component named like the design is reachable by name (#8358) Document::find_component resolved the design's own name to the root component (id 0) before searching the component list, so a component that shares the design's name was unreachable by name: appearance.assign reported `no component `CurtainRailPlug`` (the same lookup serves every command that takes a component by name). An exact component name (or component id) match now wins over the design-name fallback; "0" and "root" still always mean the design. Regression test: a_component_named_like_the_design_takes_its_appearance_by_name (fails on main with the reported error, passes with the fix).
This commit is contained in:
1 parent
9b7a6ea04d
commit
5221103369
2 files changed
+19
-3
No files matched your search
@@ -3351,12 +3351,13 @@ impl Document {
|
||||
Ok(id)
|
||||
}
|
||||
|
||||
/// A component by id or name (0 / "root" is the design itself).
|
||||
/// A component by id or name (0 / "root" is the design itself). A component that carries the
|
||||
/// design's own name is found by that name: the design is still named by 0 / "root".
|
||||
pub fn find_component(&self, key: &str) -> Option<u64> {
|
||||
if key == "0" || key.eq_ignore_ascii_case("root") || key == self.name {
|
||||
if key == "0" || key.eq_ignore_ascii_case("root") {
|
||||
return Some(0);
|
||||
}
|
||||
self.components.iter().find(|c| c.id.to_string() == key || c.name == key).map(|c| c.id)
|
||||
self.components.iter().find(|c| c.id.to_string() == key || c.name == key).map(|c| c.id).or_else(|| (key == self.name).then_some(0))
|
||||
}
|
||||
|
||||
/// Read a design file's JSON (any format this build knows; older ones are upgraded).
|
||||
|
||||
@@ -183,6 +183,21 @@ fn independent_component_copy_keeps_its_opaque_response_after_source_edits_and_r
|
||||
assert_eq!(reopened.appearances.components[&copied], original);
|
||||
}
|
||||
|
||||
/// A component that carries the design's own name is still reachable by that name (#8358):
|
||||
/// `appearance.assign` resolves the name to the component, not to the design's root row.
|
||||
#[test]
|
||||
fn a_component_named_like_the_design_takes_its_appearance_by_name() {
|
||||
let mut s = Session::default();
|
||||
s.doc_mut().name = "CurtainRailPlug".into();
|
||||
let component = s.execute("component.create", &json!({"name":"CurtainRailPlug","activate":false})).unwrap()["component"].as_u64().unwrap();
|
||||
let assigned =
|
||||
s.execute("appearance.assign", &json!({"components":["CurtainRailPlug"],"color":"#349650","name":"ABS - Green","opacity":1.0})).unwrap();
|
||||
assert_eq!(assigned["components"], json!([component]), "the name resolves to the component, not the design");
|
||||
let look = &s.doc.appearances.components[&component];
|
||||
assert_eq!(look.name, "ABS - Green");
|
||||
assert_eq!(look.color, [0x34, 0x96, 0x50]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn named_opaque_finishes_reach_the_scene_and_survive_undo_and_reopen_without_changing_mass() {
|
||||
let mut s = fixture();
|
||||
|
||||
Reference in new issue
Block a user