A date with no field to keep takes the deck's remembered format

My previous commit withheld the format from `set_date` whenever the caller had
not named one, so a field the slide already had would not be rewritten. That is
right where there is a field. A placeholder showing a fixed date holds plain
text and no date field at all, so there was no format to preserve and the
withheld format left `date_kind("")` to build a `datetime1`: a slide taken from
`datetime11` to fixed text and back came away on the short date (noted in
review).

The deck's format now always reaches `set_date`, and `format_given` is what
decides whether it may rewrite an existing field. With no field, there is
nothing to preserve and the remembered format is what a new one takes.

`a_fixed_date_turned_automatic_again_comes_back_on_the_remembered_format` walks
that transition with no `dateFormat` in the call, which is what the dialog
sends, and goes on to assert the guard still holds where a field does exist. It
is a test of its own rather than more of `the_automatic_date_takes_the_format_it_is_given`,
because setting a format for one slide also moves the deck's global one, which
that test's later assertions depend on.
This commit is contained in:
Hamza committed 2026-11-02 15:57:14 +05:00
1 parent 372c0e6c44
commit 370b38e69a
1 file changed
+41 -8
+41 -8
View File
@@ -176,12 +176,10 @@ pub(super) fn header_footer(s: &mut Session, p: &Value) -> Result<Value> {
}
(Some(pos), true) if k == PhType::Date && (date_given || format_given) => {
if let Some(sh) = sl.shapes.get_mut(pos) {
// Only a format the caller asked for. The dialog always sends
// `dateText`, so an Apply with no format selection reached here and
// rewrote a slide's own `datetime11` field to the deck's global
// format, which the caller never mentioned (noted in review). No
// format means the field keeps the one it has.
set_date(sh, &hf.date_text, if format_given { &hf.date_format } else { "" });
// The deck's format always travels; whether the caller asked for it
// is what decides if it may rewrite a field the slide already has
// (see `set_date`).
set_date(sh, &hf.date_text, &hf.date_format, format_given);
}
}
_ => {}
@@ -205,8 +203,19 @@ fn date_now(format: &str) -> String {
}
/// Point a slide's date placeholder at a fixed date (its own plain text) or, for an empty
/// `fixed`, at an automatic date field (one it already has keeps its format).
fn set_date(sh: &mut Shape, fixed: &str, format: &str) {
/// `fixed`, at an automatic date field.
///
/// `format_given` is whether the caller named a format. The dialog always sends `dateText`, so an
/// Apply with no format selection reached here carrying the deck's global format and rewrote a
/// slide's own `datetime11` field to it -- a format the caller never mentioned. A field the slide
/// already has therefore keeps its own format unless one was asked for.
///
/// That holds only where there *is* a field to keep. A placeholder showing a fixed date has no
/// date field and no format of its own, so clearing `dateText` without naming a format used to
/// build a `datetime1`: a slide that went from `datetime11` to fixed text and back came away on
/// the short date. With nothing to preserve, the deck's remembered format is the right one to
/// take (noted in review).
fn set_date(sh: &mut Shape, fixed: &str, format: &str, format_given: bool) {
let runs = || sh.text.iter().flat_map(|t| &t.paragraphs).flat_map(|p| &p.runs);
let field_now = runs().find_map(|r| match &r.kind {
RunKind::Field { field } if deckcraft_text::datetime::is_date_field(field) => Some(field.clone()),
@@ -214,6 +223,7 @@ fn set_date(sh: &mut Shape, fixed: &str, format: &str) {
});
let has_field = field_now.is_some();
let props = runs().next().map(|r| r.props.clone()).unwrap_or_default();
let format = if format_given || !has_field { format } else { "" };
let want = date_kind(format);
let (text, kind) = match fixed {
// Automatic: keep the field, but follow a format that has changed.
@@ -329,6 +339,29 @@ mod header_footer_date_tests {
assert_eq!(kinds(&s), vec![Some("datetime2".to_string()); 2], "a format the caller asked for still applies");
}
/// A fixed date is plain text, so the placeholder has no date field and no format of its
/// own. Clearing `dateText` without naming a format therefore has nothing to preserve, and
/// built a `datetime1`: a slide that went from `datetime11` to fixed text and back came away
/// on the short date (noted in review).
#[test]
fn a_fixed_date_turned_automatic_again_comes_back_on_the_remembered_format() {
let mut s = Session::with_new();
run_automatic(&mut s, json!({"date": true, "dateFormat": "datetime11", "all": true}), "datetime11");
s.execute("design.headerFooter", &json!({"date": true, "dateText": "4 July 1776", "all": true})).unwrap();
assert_eq!(dates(&s), vec![("4 July 1776".to_string(), None)], "fixed: plain text, no field");
// No `dateFormat` in this call, which is what the dialog sends on an Apply.
run_automatic(&mut s, json!({"date": true, "dateText": "", "all": true}), "datetime11");
// And the guard this must not undo: where there *is* a field, an Apply naming no format
// still leaves the slide's own format alone, although the deck's format now reaches
// `set_date` either way.
s.execute("design.headerFooter", &json!({"date": true, "dateFormat": "datetime5", "all": false})).unwrap();
s.execute("design.headerFooter", &json!({"date": true, "dateText": "", "all": true})).unwrap();
assert_eq!(dates(&s)[0].1.as_deref(), Some("datetime5"), "the slide's own format survives");
}
/// Header & Footer's automatic date takes the format it is given. `HeaderFooter::date_format`
/// existed for this and was never read: the placeholder was always `datetime1`, and a
/// `dateFormat` parameter was accepted and silently dropped.