diff --git a/crates/docx/src/chart_spec.rs b/crates/docx/src/chart_spec.rs index 1950b15bf..167be6b0d 100644 --- a/crates/docx/src/chart_spec.rs +++ b/crates/docx/src/chart_spec.rs @@ -677,6 +677,21 @@ mod tests { assert_eq!(spec_of(&parse(&plain)), Some(plain)); } + #[test] + fn an_axis_format_is_limited_in_characters_not_bytes() { + use craft_wordmodel::chart::MAX_FMT; + let cjk = format!("0\"{}\"", "\u{5143}".repeat(90)); + let ascii = format!("0\"{}\"", "x".repeat(90)); + for fmt in [cjk, ascii] { + assert!(fmt.chars().count() <= MAX_FMT); + let mut spec = ChartSpec::sample(ChartType::Column); + spec.val_axis.fmt = Some(fmt.clone()); + let spec = spec.sanitized(); + assert_eq!(spec.val_axis.fmt.as_deref(), Some(fmt.as_str())); + assert_eq!(spec_of(&parse(&spec)).as_ref(), Some(&spec), "{} chars", fmt.chars().count()); + } + } + #[test] fn a_chart_with_more_than_the_model_stays_read_only() { let spec = ChartSpec::sample(ChartType::Column); diff --git a/crates/docx/src/read/chart/parse.rs b/crates/docx/src/read/chart/parse.rs index 45586509b..6b20879c6 100644 --- a/crates/docx/src/read/chart/parse.rs +++ b/crates/docx/src/read/chart/parse.rs @@ -311,7 +311,7 @@ pub(super) fn axis(a: Option<&El>) -> Axis { .child("c:numFmt") .filter(|f| f.attr("sourceLinked") != Some("1")) .and_then(|f| f.attr("formatCode")) - .filter(|f| f.len() <= MAX_FORMAT_CODE) + .filter(|f| f.chars().count() <= craft_wordmodel::chart::MAX_FMT) .map(str::to_string), title: a.child("c:title").and_then(title_text), labels: a.child("c:tickLblPos").and_then(|p| p.attr("val")) != Some("none"), diff --git a/crates/docx/src/read/chart/tests.rs b/crates/docx/src/read/chart/tests.rs index 6e8bcfd82..8b26f40c4 100644 --- a/crates/docx/src/read/chart/tests.rs +++ b/crates/docx/src/read/chart/tests.rs @@ -386,9 +386,13 @@ fn axis_format_code_length_is_capped() { let x = format!(r#""#); axis(Some(&crate::xml::parse(x.as_bytes()).unwrap())).fmt }; - let ok = format!("{}0", "x".repeat(254)); + use craft_wordmodel::chart::MAX_FMT; + // The limit is the model's, counted in characters (a CJK character is one). + let ok = format!("{}0", "x".repeat(MAX_FMT - 1)); assert_eq!(fmt(&ok), Some(ok.clone())); - assert_eq!(fmt(&format!("{}0", "x".repeat(300))), None); + let cjk = format!("{}0", "\u{5143}".repeat(MAX_FMT - 1)); + assert_eq!(fmt(&cjk), Some(cjk.clone())); + assert_eq!(fmt(&format!("{}0", "x".repeat(MAX_FMT))), None); } #[test]