From 671b67d43f9b4bbad1076588582823ec92322e3b Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 11 Mar 2024 12:12:41 +0100 Subject: [PATCH] Fix crash with empty raw block (#3619) --- crates/typst/src/text/raw.rs | 8 +++++--- tests/typ/bugs/3601-empty-raw.typ | 7 +++++++ 2 files changed, 12 insertions(+), 3 deletions(-) create mode 100644 tests/typ/bugs/3601-empty-raw.typ diff --git a/crates/typst/src/text/raw.rs b/crates/typst/src/text/raw.rs index 9e81532f..424adb72 100644 --- a/crates/typst/src/text/raw.rs +++ b/crates/typst/src/text/raw.rs @@ -350,14 +350,16 @@ impl Packed { LinkedNode::new(&root), synt::Highlighter::new(theme), &mut |i, _, range, style| { - // Find start of line. + // Find span and start of line. // Note: Dedent is already applied to the text + let span = lines.get(i).map_or_else(Span::detached, |l| l.1); let span_offset = text[..range.start] .rfind('\n') .map_or(0, |i| range.start - (i + 1)); - styled(&text[range], foreground, style, lines[i].1, span_offset) + styled(&text[range], foreground, style, span, span_offset) }, &mut |i, range, line| { + let span = lines.get(i).map_or_else(Span::detached, |l| l.1); seq.push( Packed::new(RawLine::new( (i + 1) as i64, @@ -365,7 +367,7 @@ impl Packed { EcoString::from(&text[range]), Content::sequence(line.drain(..)), )) - .spanned(lines[i].1), + .spanned(span), ); }, ) diff --git a/tests/typ/bugs/3601-empty-raw.typ b/tests/typ/bugs/3601-empty-raw.typ new file mode 100644 index 00000000..3fb39aca --- /dev/null +++ b/tests/typ/bugs/3601-empty-raw.typ @@ -0,0 +1,7 @@ +// Test that empty raw block with `typ` language doesn't cause a crash. +// https://github.com/typst/typst/issues/3601 +// Ref: false + +--- +```typ +```