use crate::model::Spreadsheet;
#[expect(
clippy::case_sensitive_file_extension_comparisons,
reason = "a part name inside a package, not a path on a filesystem"
)]
pub(super) fn rewrite_series(book: &mut Spreadsheet, rewrite: impl Fn(&str) -> String) {
for part in &mut book.parts {
if !part.path.starts_with("xl/charts/chart") || !part.path.ends_with(".xml") {
continue;
}
let Ok(text) = core::str::from_utf8(&part.data) else {
continue;
};
let out = map_series(text, &rewrite);
if out.as_bytes() != part.data {
part.data = out.into_bytes();
}
}
}
pub(super) fn rewrite_model(book: &mut Spreadsheet, rewrite: impl Fn(&str) -> String) {
for index in 0..book.sheets().len() {
let Some(sheet) = book.sheet_mut(index) else {
continue;
};
for chart in &mut sheet.charts {
let untouched = chart.is_unchanged();
for formula in chart.formulas_mut() {
*formula = rewrite(formula);
}
for markup in chart.markups_mut() {
*markup = map_series(markup, &rewrite);
}
if untouched {
chart.settle();
}
}
for chart in &mut sheet.extended_charts {
let untouched = chart.is_unchanged();
let texts = chart
.title
.iter_mut()
.chain(chart.series.iter_mut().flat_map(|s| &mut s.name));
for text in texts {
if let crate::model::chart::ChartText::Reference { formula, .. } = text {
*formula = rewrite(formula);
}
}
let dimensions = chart.series.iter_mut().flat_map(|s| &mut s.dimensions);
for formula in dimensions.filter_map(|d| d.formula.as_mut()) {
*formula = rewrite(formula);
}
if untouched {
chart.settle();
}
}
}
}
fn map_series(xml: &str, rewrite: &impl Fn(&str) -> String) -> String {
let mut out = String::with_capacity(xml.len());
let mut rest = xml;
while let Some(open) = rest.find('<') {
out.push_str(&rest[..open]);
rest = &rest[open..];
let Some(close) = rest.find('>') else {
break;
};
let tag = &rest[1..close];
out.push_str(&rest[..=close]);
rest = &rest[close + 1..];
let name = tag.split([' ', '/', '\t', '\n']).next().unwrap_or(tag);
let local = name.rsplit(':').next().unwrap_or(name);
if local != "f" || tag.starts_with('/') || tag.ends_with('/') {
continue;
}
let Some(end) = rest.find('<') else {
break;
};
out.push_str(&rewrite(&rest[..end]));
rest = &rest[end..];
}
out.push_str(rest);
out
}
#[cfg(test)]
mod tests {
use super::*;
const CHART: &str = concat!(
r#"<c:chart><c:ser><c:tx><c:strRef><c:f>Sheet1!$B$1</c:f></c:strRef></c:tx>"#,
r#"<c:val><c:numRef><c:f>Sheet1!$B$2:$B$5</c:f><c:numCache>"#,
r#"<c:pt idx="0"><c:v>120</c:v></c:pt></c:numCache></c:numRef></c:val>"#,
r#"</c:ser></c:chart>"#,
);
#[test]
fn only_the_series_text_is_rewritten() {
let out = map_series(CHART, &|f| format!("[{f}]"));
assert!(out.contains("<c:f>[Sheet1!$B$1]</c:f>"), "{out}");
assert!(out.contains("<c:f>[Sheet1!$B$2:$B$5]</c:f>"), "{out}");
assert!(out.contains("<c:v>120</c:v>"), "{out}");
}
#[test]
fn a_default_namespace_is_the_same_element() {
let out = map_series("<chart><f>Sheet1!$A$1</f></chart>", &|f| format!("[{f}]"));
assert_eq!(out, "<chart><f>[Sheet1!$A$1]</f></chart>");
}
#[test]
fn an_empty_element_is_left_alone() {
let out = map_series("<c:f/><c:formatCode>General</c:formatCode>", &|f| {
format!("[{f}]")
});
assert_eq!(out, "<c:f/><c:formatCode>General</c:formatCode>");
}
}