summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/typst/src/model/selector.rs22
-rw-r--r--tests/typ/compiler/show-text.typ13
2 files changed, 31 insertions, 4 deletions
diff --git a/crates/typst/src/model/selector.rs b/crates/typst/src/model/selector.rs
index 9723ee4f..12f7fa0e 100644
--- a/crates/typst/src/model/selector.rs
+++ b/crates/typst/src/model/selector.rs
@@ -40,8 +40,22 @@ pub enum Selector {
impl Selector {
/// Define a simple text selector.
- pub fn text(text: &str) -> Self {
- Self::Regex(Regex::new(&regex::escape(text)).unwrap())
+ pub fn text(text: &str) -> StrResult<Self> {
+ if text.is_empty() {
+ bail!("text selector is empty");
+ }
+ Ok(Self::Regex(Regex::new(&regex::escape(text)).unwrap()))
+ }
+
+ /// Define a regex selector.
+ pub fn regex(regex: Regex) -> StrResult<Self> {
+ if regex.as_str().is_empty() {
+ bail!("regex selector is empty");
+ }
+ if regex.is_match("") {
+ bail!("regex matches empty text");
+ }
+ Ok(Self::Regex(regex))
}
/// Define a simple [`Selector::Can`] selector.
@@ -158,8 +172,8 @@ cast! {
.ok_or("only element functions can be used as selectors")?
.select(),
label: Label => Self::Label(label),
- text: EcoString => Self::text(&text),
- regex: Regex => Self::Regex(regex),
+ text: EcoString => Self::text(&text)?,
+ regex: Regex => Self::regex(regex)?,
location: Location => Self::Location(location),
}
diff --git a/tests/typ/compiler/show-text.typ b/tests/typ/compiler/show-text.typ
index e635574c..c279507b 100644
--- a/tests/typ/compiler/show-text.typ
+++ b/tests/typ/compiler/show-text.typ
@@ -26,6 +26,19 @@ AA (8)
Treeworld, the World of worlds, is a world.
---
+// Test there is no crashing on empty strings
+// Error: 1:7-1:9 text selector is empty
+#show "": []
+
+---
+// Error: 1:7-1:16 regex selector is empty
+#show regex(""): [AA]
+
+---
+// Error: 1:7-1:42 regex matches empty text
+#show regex("(VAR_GLOBAL|END_VAR||BOOL)") : []
+
+---
// This is a fun one.
#set par(justify: true)
#show regex("\S"): letter => box(stroke: 1pt, inset: 2pt, upper(letter))