bug issue with text replace and XML

This commit is contained in:
Xamora 2025-07-10 10:53:14 +02:00
parent f487132af9
commit ecf8ab977a

View file

@ -75,11 +75,11 @@ class DocxEditor {
if (texts.isEmpty) if (texts.isEmpty)
continue; continue;
String original = texts.map((t) { final String original = texts.map((t) {
return t.innerText; return t.innerText;
}).join(); }).join();
String replaced = original.replaceAllMapped(pattern, (match) { final String replaced = original.replaceAllMapped(pattern, (match) {
ReplaceContent rc = replaceMap[match[0]]!; ReplaceContent rc = replaceMap[match[0]]!;
return rc.img == null ? rc.value! : ''; return rc.img == null ? rc.value! : '';
}); });
@ -87,12 +87,10 @@ class DocxEditor {
if (original == replaced) if (original == replaced)
continue; continue;
final firstRun = paragraph.findElements('w:r').first; XmlElement firstRun = paragraph.findElements('w:r').first;
final newRun = firstRun.copy(); for (XmlElement xmlText in paragraph.findAllElements('w:t')) // clear text
newRun.findAllElements('w:t').first.innerText = replaced; // Update with new text xmlText.innerText = '';
firstRun.findAllElements('w:t').first.innerText = replaced;
// replace the first w:r with the new
firstRun.replace(newRun);
} }
return [documentXml, documentXmlRels]; return [documentXml, documentXmlRels];