Skip to content

Commit

Permalink
Merge pull request #75 from bprucha/if-undefined
Browse files Browse the repository at this point in the history
Add support to conditional check for undefined tags
  • Loading branch information
shps951023 authored Apr 12, 2024
2 parents e6e2248 + aeb12c1 commit f9c4132
Show file tree
Hide file tree
Showing 3 changed files with 6 additions and 1 deletion.
Binary file modified samples/docx/TestIfStatement.docx
Binary file not shown.
5 changes: 4 additions & 1 deletion src/MiniWord/MiniWord.Implment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -526,7 +526,7 @@ private static void ReplaceStatements(OpenXmlElement xmlElement, Dictionary<stri

var statement = paragraphs[ifIndex].InnerText.Split(' ');

var tagValue = tags[statement[1]] ?? "NULL";
var tagValue = tags.ContainsKey(statement[1]) ? tags[statement[1]] ?? "NULL" : "{UNDEFINED}";

var checkStatement = statement.Length == 4 ? EvaluateStatement(tagValue.ToString(), statement[2], statement[3]) : !bool.Parse(tagValue.ToString());

Expand Down Expand Up @@ -557,6 +557,9 @@ private static string EvaluateIfStatement(string text)
var ifEndIndex = text.IndexOf(")if", ifIndex, StringComparison.Ordinal);

var statement = text.Substring(ifIndex + ifStartTag.Length, ifEndIndex - (ifIndex + ifStartTag.Length)).Split(',');

if(Regex.IsMatch(statement[0], "^{{.*}}$"))
statement[0] = "{UNDEFINED}";

var checkStatement = EvaluateStatement(statement[0], statement[1], statement[2]);

Expand Down
2 changes: 2 additions & 0 deletions tests/MiniWordTests/MiniWordTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,8 @@ public void MiniWordIfStatement_FirstIf()
Assert.Contains("Points are greater than 100", docXml);
Assert.Contains("CreateDate is not less than 2021", docXml);
Assert.DoesNotContain("CreateDate is not greater than 2021", docXml);
Assert.Contains("Foo is undefined", docXml);
Assert.Contains("Bar is undefined", docXml);
}

[Fact]
Expand Down

0 comments on commit f9c4132

Please sign in to comment.