Scripts: tag commands reject a null name or value

object.tag.add and object.tag.set treated a null name or value as left
out, so an add with a null name made a tag and a set with a null name
still changed the value. A null is now an error that changes nothing.

A test also checks that a Tag whose add the run undid stays stale after
the run adds another tag.
This commit is contained in:
dotCooCoo committed 2026-11-21 14:08:07 -08:00
1 parent bb1da3771d
commit d8fb245db9
3 files changed
+13 -2

No files matched your search

+5 -1
View File
@@ -78,8 +78,12 @@ fn tag_index(d: &Document, id: NodeId, p: &Value, c: &str) -> Result<usize> {
tags_of(n).iter().position(|t| t.id == tag).ok_or_else(|| bad(c, format!("object {} has no tag {tag}", id.0)))
}
/// The text `p` gives for `key` (a tag's name or value), at most [`MAX_TAG_TEXT`] bytes.
/// The text `p` gives for `key` (a tag's name or value), at most [`MAX_TAG_TEXT`] bytes. `None`
/// when `p` leaves it out; a null is an error.
fn tag_text<'a>(p: &'a Value, key: &str, c: &str) -> Result<Option<&'a str>> {
if p.get(key).is_some_and(Value::is_null) {
return Err(bad(c, format!("`{key}` must be a string")));
}
let t = text_param(p, key, c)?;
if t.is_some_and(|t| t.len() > MAX_TAG_TEXT) {
return Err(bad(c, format!("a tag's {key} is longer than 65,536 bytes")));
+7
View File
@@ -167,6 +167,10 @@ fn a_tag_goes_stale_when_it_or_its_item_is_deleted() {
// A tag added after another was deleted does not take the deleted one's place.
let e = failure(&mut s, "var all = app.activeDocument.pageItems[0].tags, t = all[0]; t.remove(); all.add(); t.name;");
assert!(e.contains("Stale tag"), "{e}");
// Nor after the run undoes the add: the undone document's next id does not give the tag's id
// out again.
let e = failure(&mut s, "var all = app.activeDocument.pageItems[0].tags, t = all.add(); vc.run('edit.undo'); all.add().name = 'u'; t.name;");
assert!(e.contains("Stale tag"), "{e}");
// Nor when the deleted tag's id was ahead of the document's next id (a tag pasted from
// another document).
let d = std::sync::Arc::make_mut(&mut s.doc_mut().unwrap().doc);
@@ -188,6 +192,9 @@ fn tag_commands_check_their_params_and_skip_unchanged_edits() {
for (id, p) in [
("object.tag.add", json!({"id": a.0, "name": too_long})),
("object.tag.add", json!({"id": a.0, "value": 3})),
("object.tag.add", json!({"id": a.0, "name": null})),
("object.tag.set", json!({"id": a.0, "tag": tag, "name": null, "value": "updated"})),
("object.tag.set", json!({"id": a.0, "tag": tag, "value": null})),
("object.tag.add", json!({"id": 999_999})),
("object.tag.add", json!({"id": "1"})),
("object.tag.set", json!({"id": a.0, "tag": tag})),
+1 -1
View File
@@ -6824,7 +6824,7 @@ order → `{tags: [{tag, name, value}]}`, where `tag` tells each one apart from
changes its name or value → `{changed}`, `object.tag.remove {id, tag}` deletes it, and `object.tag.removeAll {ids?}`
deletes every tag of the objects in `ids` or the selection → `{changed: objects}`; each edit is one undo step, and an
edit that changes nothing adds none. Names may be empty or repeated. An object has at most 64 tags, each name and
value at most 65,536 bytes; a layer or a ruler guide has none, and a bad value is an error that changes nothing, as is
value at most 65,536 bytes; a layer or a ruler guide has none, and a bad value (a null name or value included) is an error that changes nothing, as is
an add in a document whose ids have reached the top of their range (a damaged or hand-made file). The
native file keeps the tags and their `tag` ids, and opening one keeps the first 64 tags of each object whose name and
value are within the limit, giving a new id to a tag whose id is missing, out of range or repeated. Copies keep their source's tags. Other file formats do not write them.