diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index facf010415..25269a62e5 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -2140,6 +2140,7 @@ fn text_arg(v: &Data, what: &str) -> std::result::Result { /// `name = value`: a number or a Boolean becomes the text `String(value)` gives. fn item_set_name(h: &Host, a: &[Data]) -> Answer { + this_item(h, a)?; let name = &text_arg(arg(a, 1), "name")?; if name.len() > MAX_TEXT_BYTES { return Err(HostError::Range("the name is too long".into())); @@ -2158,11 +2159,11 @@ fn item_note(h: &Host, a: &[Data]) -> Answer { /// `note = text`: the Attributes panel's note, set with `attributes.set`, which trims it. A number /// or a Boolean becomes the text `String(value)` gives. fn item_set_note(h: &Host, a: &[Data]) -> Answer { + let item = this_item(h, a)?; let note = &text_arg(arg(a, 1), "note")?; if note.len() > MAX_TEXT_BYTES { return Err(HostError::Range("the note is too long".into())); } - let item = this_item(h, a)?; let same = with_doc(h, item.doc, |_, st| st.doc.node(item.id).is_some_and(|n| note_of(n) == note.trim())); if !same { edit_item(h, item, "attributes.set", &json!({"ids": [item.id.0], "note": note}))?; @@ -2196,8 +2197,9 @@ fn item_hidden(h: &Host, a: &[Data]) -> Answer { } fn item_set_hidden(h: &Host, a: &[Data]) -> Answer { + let item = this_item(h, a)?; let hidden = boolean(arg(a, 1), "hidden")?; - refuse_protected_edit(h, this_item(h, a)?)?; + refuse_protected_edit(h, item)?; set_props(h, a, |n| n.visible != hidden, json!({"visible": !hidden})) } @@ -2206,8 +2208,9 @@ fn item_locked(h: &Host, a: &[Data]) -> Answer { } fn item_set_locked(h: &Host, a: &[Data]) -> Answer { + let item = this_item(h, a)?; let locked = boolean(arg(a, 1), "locked")?; - refuse_protected_edit(h, this_item(h, a)?)?; + refuse_protected_edit(h, item)?; set_props(h, a, |n| n.locked == locked, json!({"locked": locked})) } @@ -2216,6 +2219,7 @@ fn item_opacity(h: &Host, a: &[Data]) -> Answer { } fn item_set_opacity(h: &Host, a: &[Data]) -> Answer { + let item = this_item(h, a)?; let v = number(arg(a, 1), "opacity")?; if !(0.0..=100.0).contains(&v) { return Err(HostError::Range("opacity must be from 0 to 100".into())); @@ -2225,7 +2229,6 @@ fn item_set_opacity(h: &Host, a: &[Data]) -> Answer { // replaces the one the graph keeps for its series or kind. // The opacity the item already has may be assigned while its layer is hidden or locked, unless // it would still replace the opacity a graph keeps for the item's series or kind. - let item = this_item(h, a)?; let (now, keeps_other) = with_doc(h, item.doc, |_, st| { let d = &st.doc; // Only the exact number the getter reports counts as the same opacity. @@ -2271,12 +2274,12 @@ fn item_blending_mode(h: &Host, a: &[Data]) -> Answer { /// The mode the object already has may be assigned while its layer is hidden or locked, unless it /// would still replace the blend mode a graph keeps for the item's series or kind. fn item_set_blending_mode(h: &Host, a: &[Data]) -> Answer { + let item = this_item(h, a)?; let mode = match arg(a, 1) { Data::String(s) => s.strip_prefix("BlendModes.").and_then(|m| BLEND_MODES.iter().find(|(name, _)| *name == m)).map(|(_, mode)| *mode), _ => None, }; let mode = mode.ok_or_else(|| HostError::Type("blendingMode needs a BlendModes value".into()))?; - let item = this_item(h, a)?; let (same, keeps_other) = with_doc(h, item.doc, |_, st| { let d = &st.doc; let same = d.node(item.id).is_some_and(|n| n.blend == mode); @@ -2403,16 +2406,19 @@ fn move_to(h: &Host, a: &[Data], x: Option, y: Option) -> Answer { } fn item_set_position(h: &Host, a: &[Data]) -> Answer { + this_item(h, a)?; let p = point(arg(a, 1), "position")?; move_to(h, a, Some(p[0]), Some(p[1])) } fn item_set_left(h: &Host, a: &[Data]) -> Answer { + this_item(h, a)?; let x = coordinate(arg(a, 1), "left")?; move_to(h, a, Some(x), None) } fn item_set_top(h: &Host, a: &[Data]) -> Answer { + this_item(h, a)?; let y = coordinate(arg(a, 1), "top")?; move_to(h, a, None, Some(y)) } @@ -2421,12 +2427,12 @@ fn item_set_top(h: &Host, a: &[Data]) -> Answer { /// or tall, so the page-aligned bounds that `width` and `height` read have that size, rotated /// objects included. fn resize(h: &Host, a: &[Data], width: bool) -> Answer { + let item = this_item(h, a)?; let what = if width { "width" } else { "height" }; let size = number(arg(a, 1), what)?; if size <= 0.0 || size > MAX_SIZE { return Err(HostError::Range(format!("{what} must be more than 0 and at most 3999856 points"))); } - let item = this_item(h, a)?; refuse_protected_edit(h, item)?; selection_ids(h, item.doc, arg(a, 0))?; let (Some(r), _) = item_rect(h, item, false)? else { return Ok(Data::Undefined) }; diff --git a/crates/engine/src/tests/script_dom.rs b/crates/engine/src/tests/script_dom.rs index 03cab27e93..bb333e91d8 100644 --- a/crates/engine/src/tests/script_dom.rs +++ b/crates/engine/src/tests/script_dom.rs @@ -1702,6 +1702,29 @@ fn wrappers_go_stale_after_delete_and_undo() { assert!(e.contains("Stale page item"), "{e}"); } +/// A setter on a removed item reports the stale item before it checks the value it was given. +#[test] +fn setters_on_a_stale_item_report_it_before_a_bad_value() { + let (mut s, _, _) = doc(); + for set in [ + "name = {}", + "note = {}", + "hidden = 'x'", + "locked = 'x'", + "opacity = 500", + "blendingMode = 'x'", + "selected = 'x'", + "position = 'x'", + "left = 'x'", + "top = 'x'", + "width = -1", + "height = -1", + ] { + let e = failure(&mut s, &format!("var i = app.activeDocument.pageItems[0].duplicate(); i.remove(); i.{set};")); + assert!(e.contains("Stale page item"), "{set}: {e}"); + } +} + /// File › Revert loads objects with the ids they were saved with: an object or a layer read before /// the revert does not name the loaded one. #[test]