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