Skip to content

Commit ad19bdd

Browse files
authored
Merge pull request #85 from wangx036/MIf
Modify:Remove all elements between @if~@endif, not paragraph
2 parents 4ef9cee + 1ae2d9e commit ad19bdd

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

src/MiniWord/MiniWord.Implment.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -510,6 +510,7 @@ private static void ReplaceText(OpenXmlElement xmlElement, WordprocessingDocumen
510510

511511
private static void ReplaceStatements(OpenXmlElement xmlElement, Dictionary<string, object> tags)
512512
{
513+
var descendants = xmlElement.Descendants().ToList();
513514
var paragraphs = xmlElement.Descendants<Paragraph>().ToList();
514515

515516
while (paragraphs.Any(s => s.InnerText.Contains("@if")))
@@ -525,10 +526,14 @@ private static void ReplaceStatements(OpenXmlElement xmlElement, Dictionary<stri
525526

526527
if (!checkStatement)
527528
{
528-
for (int i = ifIndex + 1; i <= endIfFinalIndex - 1; i++)
529+
var paragraphIfIndex = descendants.FindIndex(a => a == paragraphs[ifIndex]);
530+
var paragraphEndIfIndex = descendants.FindIndex(a => a == paragraphs[endIfFinalIndex]);
531+
532+
for (int i = paragraphIfIndex + 1; i <= paragraphEndIfIndex - 1; i++)
529533
{
530-
paragraphs[i].Remove();
534+
descendants[i].Remove();
531535
}
536+
532537
}
533538

534539
paragraphs[ifIndex].Remove();

0 commit comments

Comments
 (0)