From 3ef82b55eb3fa595e7cfbfd50fe5c49f632f9176 Mon Sep 17 00:00:00 2001 From: dotCooCoo Date: Mon, 2 Nov 2026 20:15:58 -0800 Subject: [PATCH] Preserve action chains when discarding external references --- crates/optimize/src/external_refs.rs | 13 ++++++---- crates/optimize/tests/optimize.rs | 39 ++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 5 deletions(-) diff --git a/crates/optimize/src/external_refs.rs b/crates/optimize/src/external_refs.rs index 4a61f37e88..3981b81b81 100644 --- a/crates/optimize/src/external_refs.rs +++ b/crates/optimize/src/external_refs.rs @@ -219,7 +219,7 @@ impl Walk { match rest { Some(Kept::Now(v)) => { let mut d = d.clone(); - set_next(&mut d, v); + set_next(doc, &mut d, v); Kept::Now(vec![Object::Dict(d)]) } _ => Kept::Same, @@ -230,13 +230,16 @@ impl Walk { } } -fn set_next(d: &mut Dict, mut v: Vec) { +fn set_next(doc: &mut Document, d: &mut Dict, mut v: Vec) { match v.len() { 0 => { d.remove(b"Next"); } 1 => d.set(b"Next".to_vec(), v.remove(0)), - _ => d.set(b"Next".to_vec(), Object::Array(v)), + _ => { + let next = v.into_iter().map(|a| if matches!(a, Object::Dict(_) | Object::Array(_)) { Object::Ref(doc.add(a)) } else { a }).collect(); + d.set(b"Next".to_vec(), Object::Array(next)); + } } } @@ -278,8 +281,8 @@ fn strip_action(doc: &mut Document, a: &Object, owner: ObjRef) -> Result = std::iter::repeat_n("Named".to_string(), 127).chain(["URI".to_string()]).collect(); + assert_eq!(chain(&reopened, &a), expected); +} + #[test] fn shared_actions_that_multiply_stop_the_optimization_when_one_opens_another_document() { use pdfcraft_optimize::OptimizeError;