From a2063cea756b84dbefd454384ac569f4643785eb Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Sat, 21 Nov 2026 15:55:21 -0800 Subject: [PATCH] Scripts: a style change that is put back keeps its style object A removal or rename whose result cannot reach the script is put back, and style objects now follow a command only once it is kept. A failed atomic object-model call puts style objects back with the documents. --- crates/engine/src/script/dom.rs | 17 +++++++--- crates/engine/src/script/dom/text/styles.rs | 35 +++++++++++++++++++++ crates/engine/src/script/mod.rs | 7 +++-- crates/engine/src/tests/script_styles.rs | 29 ++++++++++++++++- 4 files changed, 79 insertions(+), 9 deletions(-) diff --git a/crates/engine/src/script/dom.rs b/crates/engine/src/script/dom.rs index 87a6310ec3..c02d65dfbd 100644 --- a/crates/engine/src/script/dom.rs +++ b/crates/engine/src/script/dom.rs @@ -113,15 +113,22 @@ fn atomically(h: &Host, f: impl FnOnce(&Host) -> std::result::Result, + seen: Vec, +} + +/// The style objects' state now, for [`put_back`] when the savepoint taken with it is restored. +pub(in crate::script) fn mark(h: &Host) -> Mark { + let styles = &h.dom.text.styles; + let slots = styles.slots.borrow().iter().map(|s| (s.name.clone(), s.live)).collect(); + Mark { slots, seen: styles.seen.borrow().clone() } +} + +/// Puts the style objects back as they were at `mark`, after the documents were put back to the +/// savepoint taken with it. A slot added since then stays live only while its document has the +/// style and no older live slot names it. +pub(in crate::script) fn put_back(h: &Host, mark: Mark) { + let styles = &h.dom.text.styles; + let mut slots = styles.slots.borrow_mut(); + let kept = mark.slots.len(); + for (slot, (name, live)) in slots.iter_mut().zip(mark.slots) { + slot.name = name; + slot.live = live; + } + let s = h.session.borrow(); + for i in kept..slots.len() { + let (older, newer) = slots.split_at_mut(i); + let Some(slot) = newer.first_mut() else { break }; + let named = older.iter().any(|o| o.live && o.uid == slot.uid && o.para == slot.para && o.name == slot.name); + slot.live = !named && s.documents().iter().any(|st| st.uid == slot.uid && has_style(st, slot.para, &slot.name)); + } + *styles.seen.borrow_mut() = mark.seen; +} + fn normal(para: bool) -> &'static str { if para { NORMAL_PARA } else { NORMAL_CHAR } } diff --git a/crates/engine/src/script/mod.rs b/crates/engine/src/script/mod.rs index 00d2c50167..e537ab8ef7 100644 --- a/crates/engine/src/script/mod.rs +++ b/crates/engine/src/script/mod.rs @@ -1317,9 +1317,6 @@ fn command_value(host: &Host, id: &str, params: &Value) -> std::result::Result<( let mut s = session.borrow_mut(); step(&mut s, host, &id, ¶ms) }; - if r.is_ok() { - dom::after_command(host); - } // Only commands that exist are timed, so the table holds at most one row per command. if let (Some(started), Some(spec)) = (started, crate::find_command(&id)) { let nanos = started.elapsed().as_nanos(); @@ -1349,6 +1346,10 @@ fn command_value(host: &Host, id: &str, params: &Value) -> std::result::Result<( return Err(HostError::Abort(HISTORY_LOST.to_string())); } } + // Style objects follow only a command that is kept. + if crossed.is_ok() { + dom::after_command(host); + } crossed.map(|data| (data, value)) } diff --git a/crates/engine/src/tests/script_styles.rs b/crates/engine/src/tests/script_styles.rs index 085ecfbbb9..f30e66a7e6 100644 --- a/crates/engine/src/tests/script_styles.rs +++ b/crates/engine/src/tests/script_styles.rs @@ -4,6 +4,7 @@ use serde_json::{Value, json}; use vectorcraft_doc::{TextObject, TextStyleDef}; use crate::cmd::textstyles::{NORMAL_CHAR, NORMAL_PARA}; +use crate::script::TEST_RESULT_BYTES; use crate::*; fn run(s: &mut Session, source: &str) -> Result { @@ -177,6 +178,29 @@ fn a_renamed_style_keeps_its_object_and_a_removed_style_is_stale() { assert_eq!(runs(&s, id)[0].2, 40.0); } +/// A removal or rename whose result cannot cross to the script is put back, and the style's object +/// still names the style after it. +#[test] +fn a_style_change_that_is_put_back_keeps_the_style_object() { + let (mut s, _) = doc_with("Hello"); + s.execute("charStyle.new", &json!({"name": "B", "attrs": {}})).unwrap(); + TEST_RESULT_BYTES.with(|b| b.set(Some(1))); + let r = run( + &mut s, + &format!( + "{DOC} var a = cs.getByName('B');\ + try {{ a.remove(); }} catch (e) {{ vc.log(e.name); }}\ + try {{ vc.run('charStyle.delete', {{name: 'B'}}); }} catch (e) {{ vc.log(e.name); }}\ + try {{ a.name = 'C'; }} catch (e) {{ vc.log(e.name); }}\ + vc.log([a.name, a === cs.getByName('B'), cs.length]);" + ), + ); + TEST_RESULT_BYTES.with(|b| b.set(None)); + let r = r.unwrap(); + assert_eq!(r["log"], json!(["RangeError", "RangeError", "RangeError", ["B", true, 2]]), "{r}"); + assert!(char_names(&s).contains(&"B".to_string()), "{:?}", char_names(&s)); +} + /// A character style styles exactly the range; without clearing overrides the range keeps its /// own underline, and with them it goes back to the Normal style first. #[test] @@ -239,7 +263,10 @@ fn style_attributes_redefine_the_text_in_the_style() { assert_eq!(out, [json!([true, true, true, "CharacterAttributes"]), json!([24, true, true])]); assert_eq!(runs(&s, id), [("ab ".into(), Some("X".into()), 24.0, true), ("cd".into(), Some("X".into()), 9.0, true)]); // Turning auto leading off keeps the leading auto leading gave the style's size. - let out = log(&mut s, &format!("{DOC} var a = cs.getByName('X').characterAttributes; a.autoLeading = false; vc.log([a.autoLeading, Math.round(a.leading * 10)]);")); + let out = log( + &mut s, + &format!("{DOC} var a = cs.getByName('X').characterAttributes; a.autoLeading = false; vc.log([a.autoLeading, Math.round(a.leading * 10)]);"), + ); assert_eq!(out, [json!([false, 288])]); let e = failure(&mut s, &format!("{DOC} cs.getByName('X').characterAttributes.size = 0;")); assert!(e.contains("0.1 to 1296"), "{e}");