diff options
| author | Said A. <47973576+Daaiid@users.noreply.github.com> | 2025-06-26 11:18:51 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-26 09:18:51 +0000 |
| commit | 5dd5771df03a666fe17930b0b071b06266e5937f (patch) | |
| tree | 68a79a843f7b813aea70733b75b7df45a938ee0d /crates/typst-library | |
| parent | 04fd0acacab8cf2e82268da9c18ef4bcf37507dc (diff) | |
Disallow empty labels and references (#5776) (#6332)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
Diffstat (limited to 'crates/typst-library')
| -rw-r--r-- | crates/typst-library/src/foundations/label.rs | 23 | ||||
| -rw-r--r-- | crates/typst-library/src/model/bibliography.rs | 6 |
2 files changed, 21 insertions, 8 deletions
diff --git a/crates/typst-library/src/foundations/label.rs b/crates/typst-library/src/foundations/label.rs index 3b9b010c..b1ac58bf 100644 --- a/crates/typst-library/src/foundations/label.rs +++ b/crates/typst-library/src/foundations/label.rs @@ -1,7 +1,8 @@ use ecow::{eco_format, EcoString}; use typst_utils::{PicoStr, ResolvedPicoStr}; -use crate::foundations::{func, scope, ty, Repr, Str}; +use crate::diag::StrResult; +use crate::foundations::{bail, func, scope, ty, Repr, Str}; /// A label for an element. /// @@ -27,7 +28,8 @@ use crate::foundations::{func, scope, ty, Repr, Str}; /// # Syntax /// This function also has dedicated syntax: You can create a label by enclosing /// its name in angle brackets. This works both in markup and code. A label's -/// name can contain letters, numbers, `_`, `-`, `:`, and `.`. +/// name can contain letters, numbers, `_`, `-`, `:`, and `.`. A label cannot +/// be empty. /// /// Note that there is a syntactical difference when using the dedicated syntax /// for this function. In the code below, the `[<a>]` terminates the heading and @@ -50,8 +52,11 @@ pub struct Label(PicoStr); impl Label { /// Creates a label from an interned string. - pub fn new(name: PicoStr) -> Self { - Self(name) + /// + /// Returns `None` if the given string is empty. + pub fn new(name: PicoStr) -> Option<Self> { + const EMPTY: PicoStr = PicoStr::constant(""); + (name != EMPTY).then_some(Self(name)) } /// Resolves the label to a string. @@ -70,10 +75,14 @@ impl Label { /// Creates a label from a string. #[func(constructor)] pub fn construct( - /// The name of the label. + /// The name of the label. Must not be empty. name: Str, - ) -> Label { - Self(PicoStr::intern(name.as_str())) + ) -> StrResult<Label> { + if name.is_empty() { + bail!("label name must not be empty"); + } + + Ok(Self(PicoStr::intern(name.as_str()))) } } diff --git a/crates/typst-library/src/model/bibliography.rs b/crates/typst-library/src/model/bibliography.rs index e1a07359..f56f5813 100644 --- a/crates/typst-library/src/model/bibliography.rs +++ b/crates/typst-library/src/model/bibliography.rs @@ -321,7 +321,11 @@ impl Bibliography { for d in data.iter() { let library = decode_library(d)?; for entry in library { - match map.entry(Label::new(PicoStr::intern(entry.key()))) { + let label = Label::new(PicoStr::intern(entry.key())) + .ok_or("bibliography contains entry with empty key") + .at(d.source.span)?; + + match map.entry(label) { indexmap::map::Entry::Vacant(vacant) => { vacant.insert(entry); } |
