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 | |
| parent | 04fd0acacab8cf2e82268da9c18ef4bcf37507dc (diff) | |
Disallow empty labels and references (#5776) (#6332)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
| -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 | ||||
| -rw-r--r-- | tests/ref/ref-to-empty-label-not-possible.png | bin | 0 -> 182 bytes | |||
| -rw-r--r-- | tests/suite/foundations/label.typ | 4 | ||||
| -rw-r--r-- | tests/suite/model/bibliography.typ | 8 | ||||
| -rw-r--r-- | tests/suite/model/ref.typ | 11 |
10 files changed, 54 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, diff --git a/tests/ref/ref-to-empty-label-not-possible.png b/tests/ref/ref-to-empty-label-not-possible.png Binary files differnew file mode 100644 index 00000000..774b7958 --- /dev/null +++ b/tests/ref/ref-to-empty-label-not-possible.png diff --git a/tests/suite/foundations/label.typ b/tests/suite/foundations/label.typ index 3b84c2d7..6eb2a9fd 100644 --- a/tests/suite/foundations/label.typ +++ b/tests/suite/foundations/label.typ @@ -92,3 +92,7 @@ _Visible_ --- label-non-existent-error --- // Error: 5-10 sequence does not have field "label" #[].label + +--- label-empty --- +// Error: 23-32 label name must not be empty += Something to label #label("") diff --git a/tests/suite/model/bibliography.typ b/tests/suite/model/bibliography.typ index 23576c15..6a0c3e3c 100644 --- a/tests/suite/model/bibliography.typ +++ b/tests/suite/model/bibliography.typ @@ -75,6 +75,14 @@ Now we have multiple bibliographies containing @glacier-melt @keshav2007read // Error: 2-62 CSL style "Alphanumeric" is not suitable for bibliographies #bibliography("/assets/bib/works.bib", style: "alphanumeric") +--- bibliography-empty-key --- +#let src = ```yaml +"": + type: Book +``` +// Error: 15-30 bibliography contains entry with empty key +#bibliography(bytes(src.text)) + --- issue-4618-bibliography-set-heading-level --- // Test that the bibliography block's heading is set to 2 by the show rule, // and therefore should be rendered like a level-2 heading. Notably, this diff --git a/tests/suite/model/ref.typ b/tests/suite/model/ref.typ index 87b1c409..d48072ed 100644 --- a/tests/suite/model/ref.typ +++ b/tests/suite/model/ref.typ @@ -86,3 +86,14 @@ Text seen on #ref(<text>, form: "page", supplement: "Page"). // Test reference with non-whitespace before it. #figure[] <1> #test([(#ref(<1>))], [(@1)]) + +--- ref-to-empty-label-not-possible --- +// @ without any following label should just produce the symbol in the output +// and not produce a reference to a label with an empty name. +@ + +--- ref-function-empty-label --- +// using ref() should also not be possible +// Error: 6-7 unexpected less-than operator +// Error: 7-8 unexpected greater-than operator +#ref(<>) |
