use super::agent::REVS;
use super::ooxml::{Element, Node};
pub(crate) fn resolve(element: &Element, accept: bool) -> Element {
let mut out = element.clone();
resolve_in(&mut out, accept);
fn box_text(element: &mut Element, in_box: bool) {
let in_box = in_box || element.is("txbxContent");
if in_box && element.is("delText") {
element.name = "w:t".to_string();
}
for child in &mut element.children {
if let Node::Element(child) = child {
box_text(child, in_box);
}
}
}
box_text(&mut out, false);
out
}
fn removed(local: &str, accept: bool) -> bool {
if accept {
matches!(local, "del" | "moveFrom" | "cellDel")
} else {
matches!(local, "ins" | "moveTo" | "cellIns")
}
}
fn kept(local: &str, accept: bool) -> bool {
if accept {
matches!(local, "ins" | "moveTo")
} else {
matches!(local, "del" | "moveFrom")
}
}
fn is_marker(local: &str) -> bool {
matches!(
local,
"ins" | "del" | "moveTo" | "moveFrom" | "cellIns" | "cellDel" | "cellMerge"
) || local.ends_with("PrChange")
}
fn marked_removed(properties: Option<&Element>, accept: bool) -> bool {
properties.is_some_and(|p| p.elements().any(|m| removed(m.local(), accept)))
}
fn resolve_in(element: &mut Element, accept: bool) {
let properties = element.local().ends_with("Pr");
if properties && !accept {
restore_earlier(element);
}
let mut children = Vec::with_capacity(element.children.len());
for child in std::mem::take(&mut element.children) {
let Node::Element(mut child) = child else {
children.push(child);
continue;
};
let local = child.local().to_string();
if properties && is_marker(&local) {
continue;
}
let gone = match local.as_str() {
"tr" => marked_removed(child.child("trPr"), accept),
"tc" => marked_removed(child.child("tcPr"), accept),
_ => removed(&local, accept),
};
if gone {
if !matches!(local.as_str(), "tr" | "tc") {
range_ends(&child, &mut children);
}
continue;
}
if local == "p" && marked_removed(child.path(&["pPr", "rPr"]), accept) {
child.attrs.push((BREAK_GONE.to_string(), String::new()));
}
resolve_in(&mut child, accept);
if kept(&local, accept) {
children.extend(child.children);
} else {
children.push(Node::Element(child));
}
}
element.children = join_paragraphs(children);
}
fn range_ends(element: &Element, out: &mut Vec<Node>) {
for child in element.elements() {
match child.local() {
"commentRangeStart" | "commentRangeEnd" => out.push(Node::Element(child.clone())),
_ => range_ends(child, out),
}
}
}
fn restore_earlier(properties: &mut Element) {
let change = format!("{}Change", properties.local());
let Some(change) = properties.child(&change) else {
return;
};
let earlier = change
.elements()
.find(|e| e.local() == properties.local())
.map(|e| e.children.clone())
.unwrap_or_default();
let current = std::mem::take(&mut properties.children);
properties.children = earlier;
properties.children.extend(
current.into_iter().filter(
|node| matches!(node, Node::Element(e) if matches!(e.local(), "rPr" | "sectPr")),
),
);
}
fn join_paragraphs(children: Vec<Node>) -> Vec<Node> {
let mut out: Vec<Node> = Vec::with_capacity(children.len());
let mut joining: Option<usize> = None;
for node in children {
let mut p = match node {
Node::Element(p) if p.is("p") => p,
Node::Element(other) => {
if !is_empty_marker(other.local()) {
joining = None;
}
out.push(Node::Element(other));
continue;
}
text => {
out.push(text);
continue;
}
};
let gone = p.attrs.iter().any(|(name, _)| name == BREAK_GONE);
p.attrs.retain(|(name, _)| name != BREAK_GONE);
let at = match joining.and_then(|at| match &mut out[at] {
Node::Element(before) => Some((at, before)),
Node::Text(_) => None,
}) {
Some((at, before)) => {
if let Some(later) = p.attr(REVS).map(str::to_string) {
match before.attrs.iter_mut().find(|(name, _)| name == REVS) {
Some((_, revs)) => {
revs.push(' ');
revs.push_str(&later);
}
None => before.attrs.push((REVS.to_string(), later)),
}
}
let (properties, rest): (Vec<Node>, Vec<Node>) = p
.children
.into_iter()
.partition(|n| matches!(n, Node::Element(e) if e.is("pPr")));
let content = std::mem::take(&mut before.children)
.into_iter()
.filter(|n| !matches!(n, Node::Element(e) if e.is("pPr")));
before.children = properties.into_iter().chain(content).chain(rest).collect();
at
}
None => {
out.push(Node::Element(p));
out.len() - 1
}
};
joining = gone.then_some(at);
}
out
}
fn is_empty_marker(local: &str) -> bool {
local.ends_with("RangeStart")
|| local.ends_with("RangeEnd")
|| matches!(
local,
"bookmarkStart" | "bookmarkEnd" | "permStart" | "permEnd" | "proofErr"
)
}
const BREAK_GONE: &str = "anymd:break-gone";