diff --git a/crates/typst/src/introspection/counter.rs b/crates/typst/src/introspection/counter.rs index 2aefb68ec..b9462e3dd 100644 --- a/crates/typst/src/introspection/counter.rs +++ b/crates/typst/src/introspection/counter.rs @@ -685,14 +685,12 @@ impl CounterState { pub fn step(&mut self, level: NonZeroUsize, by: usize) { let level = level.get(); - if self.0.len() >= level { - self.0[level - 1] = self.0[level - 1].saturating_add(by); - self.0.truncate(level); + while self.0.len() < level { + self.0.push(0); } - while self.0.len() < level { - self.0.push(1); - } + self.0[level - 1] = self.0[level - 1].saturating_add(by); + self.0.truncate(level); } /// Get the first number of the state. diff --git a/tests/ref/outline.png b/tests/ref/outline.png index 71dd6e1a9..c0d22969b 100644 Binary files a/tests/ref/outline.png and b/tests/ref/outline.png differ diff --git a/tests/suite/introspection/counter.typ b/tests/suite/introspection/counter.typ index be17d7e06..0d2be6e2e 100644 --- a/tests/suite/introspection/counter.typ +++ b/tests/suite/introspection/counter.typ @@ -104,3 +104,15 @@ At Beta, it was #context { #block(foo()) #block(foo()) #foo() + +--- issue-4626-counter-depth-skip --- +// When we step and skip a level, the levels should be filled with zeros, not +// with ones. +#let c = counter("c") +#context test(c.get(), (0,)) +#c.step(level: 4) +#context test(c.get(), (0, 0, 0, 1)) +#c.step(level: 1) +#context test(c.get(), (1,)) +#c.step(level: 3) +#context test(c.get(), (1, 0, 1))