Merge pull request #9433 from dotCooCoo/fix/script-protected-setters

Scripts: reject protected page-item mutations
This commit is contained in:
BFlat authored and GitHub committed 2026-11-27 00:48:00 +07:00
commit 369e4ea322
4 files changed
+77 -14

No files matched your search

+23 -12
View File
@@ -1599,16 +1599,23 @@ fn item_set_note(h: &Host, a: &[Data]) -> Answer {
Ok(Data::Undefined)
}
/// Refuses a change to the item's appearance, place, stacking order or size while a layer it is in
/// is hidden or locked. Its name and note may still change.
fn refuse_in_closed_layer(h: &Host, item: Item) -> std::result::Result<(), HostError> {
/// Refuses appearance, place, stacking order and size changes in hidden or locked layers and on
/// locked artboards. Names and notes may still change.
fn refuse_protected_edit(h: &Host, item: Item) -> std::result::Result<(), HostError> {
let refused = with_doc(h, item.doc, |_, st| {
let d = &st.doc;
let closed = |i: &NodeId| d.node(*i).is_some_and(|n| matches!(n.kind, NodeKind::Layer { .. }) && (!n.visible || n.locked));
d.ancestry(item.id).is_some_and(|a| a.iter().any(closed)).then(|| d.node(item.id).and_then(typename).unwrap_or("page item"))
let why = if d.ancestry(item.id).is_some_and(|a| a.iter().any(closed)) {
Some("a layer it is in is hidden or locked")
} else if on_locked_artboard(h, st, item.id) {
Some("it is on a locked artboard")
} else {
None
};
why.map(|why| (d.node(item.id).and_then(typename).unwrap_or("page item"), why))
});
match refused {
Some(name) => Err(HostError::Error(format!("Cannot edit {name} {}: a layer it is in is hidden or locked", item.id.0))),
Some((name, why)) => Err(HostError::Error(format!("Cannot edit {name} {}: {why}", item.id.0))),
None => Ok(()),
}
}
@@ -1619,7 +1626,7 @@ fn item_hidden(h: &Host, a: &[Data]) -> Answer {
fn item_set_hidden(h: &Host, a: &[Data]) -> Answer {
let hidden = boolean(arg(a, 1), "hidden")?;
refuse_in_closed_layer(h, this_item(h, a)?)?;
refuse_protected_edit(h, this_item(h, a)?)?;
set_props(h, a, |n| n.visible != hidden, json!({"visible": !hidden}))
}
@@ -1629,7 +1636,7 @@ fn item_locked(h: &Host, a: &[Data]) -> Answer {
fn item_set_locked(h: &Host, a: &[Data]) -> Answer {
let locked = boolean(arg(a, 1), "locked")?;
refuse_in_closed_layer(h, this_item(h, a)?)?;
refuse_protected_edit(h, this_item(h, a)?)?;
set_props(h, a, |n| n.locked == locked, json!({"locked": locked}))
}
@@ -1657,7 +1664,7 @@ fn item_set_opacity(h: &Host, a: &[Data]) -> Answer {
(now, keeps_other)
});
if now.is_none() || keeps_other {
refuse_in_closed_layer(h, item)?;
refuse_protected_edit(h, item)?;
}
set_props(h, a, |_| false, json!({"opacity": now.unwrap_or(v)}))
}
@@ -1741,7 +1748,8 @@ fn item_height(h: &Host, a: &[Data]) -> Answer {
/// stays).
fn move_to(h: &Host, a: &[Data], x: Option<f64>, y: Option<f64>) -> Answer {
let item = this_item(h, a)?;
refuse_in_closed_layer(h, item)?;
refuse_protected_edit(h, item)?;
selection_ids(h, item.doc, arg(a, 0))?;
let (Some(r), f) = item_rect(h, item, false)? else { return Ok(Data::Undefined) };
let c = h.dom.coords.get();
let now = f.to_script(c, Point::new(r.x0, r.y0));
@@ -1778,7 +1786,8 @@ fn resize(h: &Host, a: &[Data], width: bool) -> Answer {
return Err(HostError::Range(format!("{what} must be more than 0 and at most 3999856 points")));
}
let item = this_item(h, a)?;
refuse_in_closed_layer(h, item)?;
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) };
let now = if width { r.width() } else { r.height() };
if now <= 0.0 {
@@ -2024,7 +2033,8 @@ fn item_duplicate(h: &Host, a: &[Data]) -> Answer {
}
/// `zOrder(method)`: one of the four Object › Arrange commands for the item. A path inside a
/// compound path is refused, and so is an object in a hidden or locked layer.
/// compound path is refused. The item must be selectable and outside hidden or locked layers and
/// locked artboards.
fn item_z_order(h: &Host, a: &[Data]) -> Answer {
let id = match arg(a, 1) {
Data::String(s) if s == "ZOrderMethod.BRINGTOFRONT" => "object.arrange.bringToFront",
@@ -2037,7 +2047,8 @@ fn item_z_order(h: &Host, a: &[Data]) -> Answer {
if in_compound(h, item) {
return Err(HostError::Error(COMPOUND_MEMBER_ORDER.into()));
}
refuse_in_closed_layer(h, item)?;
refuse_protected_edit(h, item)?;
selection_ids(h, item.doc, arg(a, 0))?;
let (before, after) = with_doc(h, item.doc, |_, st| {
let at = st.doc.position(item.id);
let count = at.and_then(|(p, ..)| st.doc.children(p).map(Vec::len)).unwrap_or(0);
+48
View File
@@ -1325,6 +1325,54 @@ fn duplicate_refuses_hidden_or_locked_art() {
assert!(order.contains(&a) && order.contains(&b));
}
#[test]
fn setters_refuse_art_on_a_locked_artboard() {
let (mut s, _, _) = doc();
s.edit("Lock Artboard", |d, _| {
d.artboards[0].locked = true;
Ok(())
})
.unwrap();
let (before, steps) = (format!("{:?}", s.active().unwrap().doc.layers), undo_steps(&s));
let out = log(
&mut s,
"var p = app.activeDocument.pageItems[0], refused = [];\
var setters = [function(){p.position=[5,5]},function(){p.left=5},function(){p.top=5},\
function(){p.width=10},function(){p.height=10},function(){p.opacity=50},\
function(){p.hidden=true},function(){p.locked=true},function(){p.zOrder(ZOrderMethod.SENDTOBACK)}];\
for(var i=0;i<setters.length;i++){try{setters[i]();refused.push(false)}catch(e){refused.push(e.name==='Error')}}\
vc.log(refused);",
);
assert_eq!(out, vec![json!(vec![true; 9])]);
assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before);
assert_eq!(undo_steps(&s), steps);
assert_eq!(log(&mut s, "var p=app.activeDocument.pageItems[0];p.opacity=p.opacity;vc.log(p.opacity);"), vec![json!(100)]);
}
#[test]
fn geometry_setters_refuse_locked_guide_paths() {
let (mut s, _, b) = doc();
s.execute("select.set", &json!({"ids": [b.0]})).unwrap();
s.execute("view.guides.make", &json!({})).unwrap();
s.execute("view.guides.lock", &json!({"locked": true})).unwrap();
let (before, steps) = (format!("{:?}", s.active().unwrap().doc.layers), undo_steps(&s));
let out = log(
&mut s,
"var p=app.activeDocument.pageItems[0],refused=[];\
var setters=[function(){p.position=[5,5]},function(){p.left=5},function(){p.top=5},\
function(){p.width=10},function(){p.height=10},function(){p.zOrder(ZOrderMethod.SENDTOBACK)}];\
for(var i=0;i<setters.length;i++){try{setters[i]();refused.push(false)}catch(e){refused.push(e.name==='Error')}}\
vc.log(refused);",
);
assert_eq!(out, vec![json!(vec![true; 6])]);
assert_eq!(format!("{:?}", s.active().unwrap().doc.layers), before);
assert_eq!(undo_steps(&s), steps);
s.execute("view.guides.lock", &json!({"locked": false})).unwrap();
log(&mut s, "var p=app.activeDocument.pageItems[0];p.left=5;p.width=10;");
let bounds = s.active().unwrap().doc.bounds_of(&[b], false).unwrap();
assert!((bounds.x0 - 5.0).abs() < 1e-9 && (bounds.width() - 10.0).abs() < 1e-9);
}
/// Reading `editable` on every member of a group on a locked artboard measures the group once.
#[test]
fn editable_measures_a_group_on_a_locked_artboard_once() {
+4 -2
View File
@@ -2809,12 +2809,14 @@ other files through `$`:
the undo history as they were before the call. `move` on an object that is hidden or locked, inside a hidden or
locked layer or group, or on a locked artboard throws `Cannot move` and moves nothing. `duplicate` on such an
object, with or without a relative object, throws `Cannot duplicate` and makes no copy. Assigning `hidden`,
`locked`, `opacity`, `position`, `left`, `top`, `width` or `height` to an object in a hidden or locked layer
`locked`, `opacity`, `position`, `left`, `top`, `width` or `height` to an object in a hidden or locked layer or on a locked artboard
throws `Cannot edit` and changes nothing, even when the value is the one the object has, and `zOrder()` on such
an object throws `Cannot edit` too. The one exception is `opacity` given the value the object already has, which
changes nothing and does not throw; on a graph's mark or label whose series or kind keeps another opacity, that
value would still change the graph, so it throws `Cannot edit` as well. Its `name` and `note` still change, and an
object that is hidden or locked itself takes all of them (VectorCraft's own rule). `move` with the object itself
object that is hidden or locked itself takes `hidden`, `locked` and `opacity` (VectorCraft's own rule). Position,
size and stacking-order changes also require a selectable object. Other targets excluded by selection filtering,
including guide paths while Lock Guides is on, throw `Cannot select`. `move` with the object itself
as the relative object and `PLACEBEFORE` or `PLACEAFTER` leaves it where it is and adds no undo step. When `move`
or `remove()` takes the last path out of a compound path, the compound path stays, empty, so a script that holds
it can still read it (`remove()` runs `edit.clear` with `keepEmpty: true`). An object with nothing in it, such
+2
View File
@@ -34,6 +34,8 @@
History panel row context menus (2026-11-20, G12): Right-clicking any state offers New Document from State, Step to This State, and Clear History. Creating a document uses the clicked snapshot without stepping the source document; Step to This State uses undo/redo and is disabled on the current row; Clear History removes undo and redo steps without changing artwork and is disabled when history is empty. Headless tests in `crates/ui-egui/src/panels/history.rs` cover opening an earlier row menu, snapshot creation, backward/forward jumps, and clearing history. The snapshot chosen when the menu opens is retained while it stays open; pending tool edits are settled before resolving that snapshot to its new row; a discarded snapshot reports that it is unavailable instead of acting on a neighboring row. Jumps settle pending typing before counting undo/redo steps. Shift+F10 opens the focused row menu; Escape dismisses it and restores row focus. Switching documents dismisses the menu before it can act on another document. Headless tests cover these keyboard and document-switch paths. The list and menu use shared Craft UI controls. Scores and estimates unchanged.
Script appearance and geometry setters reject objects on locked artboards. Assigning the current opacity remains allowed when it changes no graph styling. Position, size and stacking-order setters also reject objects excluded by selection filtering, including locked guide paths. `crates/engine/src/tests/script_dom.rs` covers refused edits, unchanged documents and undo history, and editing a guide after unlocking it.
Variables panel row context menus (2026-11-20, G12): Right-click highlights the clicked variable without changing the canvas selection and offers Select Bound Object, Variable Options…, Unbind Variable, the four Make Dynamic actions, and Delete Variable. Shift+F10 opens the same menu for the focused row; Escape dismisses it. Row actions target that variable; dynamic actions use the canvas selection. Lock and selection checks disable unavailable actions. Headless pointer/accessibility tests cover opening the menu, highlighting, bound-object selection, deletion in one undo step and restoration, and disabled actions. Scores and estimates unchanged.
Actions panel item context menus (2026-11-20): Right-clicking an Action Set row selects it and presents a context menu with New Action…, New Set…, Duplicate Set, Delete Set, and Set Options…. Right-clicking an Action row selects it and presents a context menu with Play, Record, Duplicate, Delete, Action Options…, and Batch…. Duplicate on an action duplicates that action and selects the duplicate; Delete removes it. Headless tests in `crates/ui-egui/src/panels/actions.rs`: `right_clicking_an_action_set_selects_it_and_presents_set_context_options`, `right_clicking_an_action_selects_it_and_presents_action_options`, and `clicking_duplicate_on_an_action_duplicates_that_action_in_the_set`. Scores and estimates unchanged.