From 9ed8d4d32ff0cb457581f207c7f14f322e312f65 Mon Sep 17 00:00:00 2001 From: Jassiel Ovando Date: Fri, 31 Jul 2026 15:51:19 -0400 Subject: [PATCH 1/2] Add test for handling year-only dates in APA style and update date rendering logic Fixes https://github.com/typst/typst/issues/5488 --- src/csl/mod.rs | 54 ++++++++++++++++++++++++++++++++++++++++ src/csl/rendering/mod.rs | 15 ++++++++++- tests/citeproc-pass.txt | 1 + 3 files changed, 69 insertions(+), 1 deletion(-) diff --git a/src/csl/mod.rs b/src/csl/mod.rs index 6221859e..490c3b4d 100644 --- a/src/csl/mod.rs +++ b/src/csl/mod.rs @@ -3798,6 +3798,60 @@ mod tests { assert_eq!(c1, "Downs (1957)"); assert_eq!(c2, "Brady & Collier (2010)"); } + + #[test] + #[cfg(feature = "archive")] + /// A webpage with only a year (no month/day) as its issued date must not render a dangling delimiter before the (empty) month/day part. + /// + /// See https://github.com/typst/typst/issues/5488 + fn issue_year_only_date_apa() { + let yaml = r#" + nistCVE: + type: Web + author: "NIST" + title: "CVE-2021-44228" + date: "2021" + url: + value: "https://nvd.nist.gov/vuln/detail/CVE-2021-44228" + date: 2024-10-28 + "#; + + let library = from_yaml_str(yaml).unwrap(); + let apa = archive::ArchivedStyle::AmericanPsychologicalAssociation.get(); + let citationberg::Style::Independent(apa) = apa else { unreachable!() }; + let locales = archive::locales(); + + let mut driver = BibliographyDriver::new(); + driver.citation(CitationRequest::new( + vec![CitationItem::with_entry(library.iter().next().unwrap())], + &apa, + None, + &locales, + None, + )); + + let finished = driver.finish(BibliographyRequest { + style: &apa, + locale: None, + locale_files: &locales, + }); + + let mut bib_entry = String::new(); + finished + .bibliography + .unwrap() + .items + .remove(0) + .content + .write_buf(&mut bib_entry, BufWriteFormat::Plain) + .unwrap(); + + assert_eq!( + bib_entry, + "NIST. (2021). CVE-2021-44228. https://nvd.nist.gov/vuln/detail/CVE-2021-44228" + ); + } + #[test] #[cfg(feature = "archive")] /// See https://github.com/typst/hayagriva/issues/48 diff --git a/src/csl/rendering/mod.rs b/src/csl/rendering/mod.rs index a2ed9f06..b3871280 100644 --- a/src/csl/rendering/mod.rs +++ b/src/csl/rendering/mod.rs @@ -792,7 +792,20 @@ impl RenderCsl for citationberg::Date { if !self.will_render(ctx, variable.into()) { (false, UsageInfo::default()) } else { - let has_non_empty_vars = ctx.resolve_date_variable(variable).is_some(); + let base = self.form.and_then(|form| ctx.localized_date(form)); + let parts = self.parts.or(base.and_then(|b| b.parts)).unwrap_or_default(); + + let has_non_empty_vars = + ctx.resolve_date_variable(variable).is_some_and(|date| { + base.unwrap_or(self).date_part.iter().any(|part| match part.name { + DatePartName::Year => true, + DatePartName::Month => { + (parts.has_month() || date.season.is_some()) + && (date.month.is_some() || date.season.is_some()) + } + DatePartName::Day => parts.has_day() && date.day.is_some(), + }) + }); ( has_non_empty_vars, UsageInfo { diff --git a/tests/citeproc-pass.txt b/tests/citeproc-pass.txt index 105bf6cf..d16a7817 100644 --- a/tests/citeproc-pass.txt +++ b/tests/citeproc-pass.txt @@ -188,6 +188,7 @@ fullstyles_ChicagoNoteWithBibliographyWithPublisher group_ComplexNesting group_ShortOutputOnly group_SuppressTermInMacro +group_SuppressTermWhenNoOutputFromPartialDate group_SuppressValueWithEmptySubgroup group_SuppressWithEmptyNestedDateNode integration_CitationSort From 94985abbee95669ad535e3baba2c84da84bf0444 Mon Sep 17 00:00:00 2001 From: Jassiel Ovando Date: Sun, 2 Aug 2026 10:50:21 -0400 Subject: [PATCH 2/2] Update issue link in year-only date test --- src/csl/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/csl/mod.rs b/src/csl/mod.rs index 490c3b4d..44023e90 100644 --- a/src/csl/mod.rs +++ b/src/csl/mod.rs @@ -3803,7 +3803,7 @@ mod tests { #[cfg(feature = "archive")] /// A webpage with only a year (no month/day) as its issued date must not render a dangling delimiter before the (empty) month/day part. /// - /// See https://github.com/typst/typst/issues/5488 + /// See https://github.com/typst/hayagriva/issues/246 fn issue_year_only_date_apa() { let yaml = r#" nistCVE: