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 | |
| parent | 04fd0acacab8cf2e82268da9c18ef4bcf37507dc (diff) | |
Disallow empty labels and references (#5776) (#6332)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
Diffstat (limited to 'crates')
| -rw-r--r-- | crates/typst-eval/src/markup.rs | 7 | ||||
| -rw-r--r-- | crates/typst-ide/src/definition.rs | 3 | ||||
| -rw-r--r-- | crates/typst-library/src/foundations/label.rs | 23 | ||||
| -rw-r--r-- | crates/typst-library/src/model/bibliography.rs | 6 | ||||
| -rw-r--r-- | crates/typst-syntax/src/ast.rs | 2 | ||||
| -rw-r--r-- | crates/typst-syntax/src/lexer.rs | 2 |
6 files changed, 31 insertions, 12 deletions
diff --git a/crates/typst-eval/src/markup.rs b/crates/typst-eval/src/markup.rs index 5beefa91..9118ded5 100644 --- a/crates/typst-eval/src/markup.rs +++ b/crates/typst-eval/src/markup.rs @@ -205,7 +205,9 @@ impl Eval for ast::Label<'_> { type Output = Value; fn eval(self, _: &mut Vm) -> SourceResult<Self::Output> { - Ok(Value::Label(Label::new(PicoStr::intern(self.get())))) + Ok(Value::Label( + Label::new(PicoStr::intern(self.get())).expect("unexpected empty label"), + )) } } @@ -213,7 +215,8 @@ impl Eval for ast::Ref<'_> { type Output = Content; fn eval(self, vm: &mut Vm) -> SourceResult<Self::Output> { - let target = Label::new(PicoStr::intern(self.target())); + let target = Label::new(PicoStr::intern(self.target())) + .expect("unexpected empty reference"); let mut elem = RefElem::new(target); if let Some(supplement) = self.supplement() { elem.push_supplement(Smart::Custom(Some(Supplement::Content( diff --git a/crates/typst-ide/src/definition.rs b/crates/typst-ide/src/definition.rs index 69d702b3..ae1ba287 100644 --- a/crates/typst-ide/src/definition.rs +++ b/crates/typst-ide/src/definition.rs @@ -72,7 +72,8 @@ pub fn definition( // Try to jump to the referenced content. DerefTarget::Ref(node) => { - let label = Label::new(PicoStr::intern(node.cast::<ast::Ref>()?.target())); + let label = Label::new(PicoStr::intern(node.cast::<ast::Ref>()?.target())) + .expect("unexpected empty reference"); let selector = Selector::Label(label); let elem = document?.introspector.query_first(&selector)?; return Some(Definition::Span(elem.span())); 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); } diff --git a/crates/typst-syntax/src/ast.rs b/crates/typst-syntax/src/ast.rs index 7b211bfc..547d53cd 100644 --- a/crates/typst-syntax/src/ast.rs +++ b/crates/typst-syntax/src/ast.rs @@ -724,6 +724,8 @@ node! { impl<'a> Ref<'a> { /// Get the target. + /// + /// Will not be empty. pub fn target(self) -> &'a str { self.0 .children() diff --git a/crates/typst-syntax/src/lexer.rs b/crates/typst-syntax/src/lexer.rs index 74f14cfe..82f65cd3 100644 --- a/crates/typst-syntax/src/lexer.rs +++ b/crates/typst-syntax/src/lexer.rs @@ -185,7 +185,7 @@ impl Lexer<'_> { 'h' if self.s.eat_if("ttp://") => self.link(), 'h' if self.s.eat_if("ttps://") => self.link(), '<' if self.s.at(is_id_continue) => self.label(), - '@' => self.ref_marker(), + '@' if self.s.at(is_id_continue) => self.ref_marker(), '.' if self.s.eat_if("..") => SyntaxKind::Shorthand, '-' if self.s.eat_if("--") => SyntaxKind::Shorthand, |
