From a062a864c693ab6c0c1ab4ff76dee32672deb208 Mon Sep 17 00:00:00 2001 From: Michael Stum Date: Tue, 16 May 2017 20:05:31 -0400 Subject: [PATCH] Potential fix for Issue #12 - pending testing --- src/Simplexcel.TestApp/Program.cs | 5 +++- src/Simplexcel.Tests/BugTests.cs | 28 ++++++++++++++++++++ src/Simplexcel.Tests/Simplexcel.Tests.csproj | 6 ++++- src/Simplexcel/InternalsVisibleTo.cs | 2 ++ src/Simplexcel/XlsxInternal/SharedStrings.cs | 9 ++++--- 5 files changed, 44 insertions(+), 6 deletions(-) create mode 100644 src/Simplexcel.Tests/BugTests.cs create mode 100644 src/Simplexcel/InternalsVisibleTo.cs diff --git a/src/Simplexcel.TestApp/Program.cs b/src/Simplexcel.TestApp/Program.cs index e0b64a1..17856fd 100644 --- a/src/Simplexcel.TestApp/Program.cs +++ b/src/Simplexcel.TestApp/Program.cs @@ -28,7 +28,7 @@ static void Main(string[] args) sheet.Cells[0, 2].Bold = true; sheet.Cells[0, 2].TextColor = Color.Red; - Cell cell = "BIU Big Blue"; + Cell cell = "BIU & Big & Blue"; cell.Bold = true; cell.Underline = true; cell.Italic = true; @@ -55,6 +55,9 @@ static void Main(string[] args) cell2.FontSize = 18; sheet.Cells[0, 2] = cell2; + sheet.Cells[0, 6] = "👪"; + sheet.Cells[0, 7] = "👨‍👩‍👧‍👦"; + wb.Add(sheet); var sheet2 = new Worksheet("Sheet 2"); diff --git a/src/Simplexcel.Tests/BugTests.cs b/src/Simplexcel.Tests/BugTests.cs new file mode 100644 index 0000000..236cf7a --- /dev/null +++ b/src/Simplexcel.Tests/BugTests.cs @@ -0,0 +1,28 @@ +using Simplexcel.XlsxInternal; +using System.Linq; +using Xunit; + +namespace Simplexcel.Tests +{ + public class BugTests + { + /// + /// https://github.com/mstum/Simplexcel/issues/12 + /// + /// SharedStrings _sanitizeRegex makes "&" in CellStrings impossible + /// + [Fact] + public void Issue12_AmpersandInCellValues() + { + var sharedStrings = new SharedStrings(); + sharedStrings.GetStringIndex("Here & Now"); + sharedStrings.GetStringIndex("&"); + var xmlFile = sharedStrings.ToXmlFile(); + + var nodeValues = xmlFile.Content.Descendants(Namespaces.x + "t").Select(x => x.Value).ToList(); + + Assert.Contains("Here & Now", nodeValues); + Assert.Contains("&", nodeValues); + } + } +} diff --git a/src/Simplexcel.Tests/Simplexcel.Tests.csproj b/src/Simplexcel.Tests/Simplexcel.Tests.csproj index b1850e3..33314c8 100644 --- a/src/Simplexcel.Tests/Simplexcel.Tests.csproj +++ b/src/Simplexcel.Tests/Simplexcel.Tests.csproj @@ -1,4 +1,4 @@ - + netcoreapp1.1 @@ -15,4 +15,8 @@ + + + + diff --git a/src/Simplexcel/InternalsVisibleTo.cs b/src/Simplexcel/InternalsVisibleTo.cs new file mode 100644 index 0000000..36c708f --- /dev/null +++ b/src/Simplexcel/InternalsVisibleTo.cs @@ -0,0 +1,2 @@ +using System.Runtime.CompilerServices; +[assembly: InternalsVisibleTo("Simplexcel.Tests")] \ No newline at end of file diff --git a/src/Simplexcel/XlsxInternal/SharedStrings.cs b/src/Simplexcel/XlsxInternal/SharedStrings.cs index 916124e..a95a133 100644 --- a/src/Simplexcel/XlsxInternal/SharedStrings.cs +++ b/src/Simplexcel/XlsxInternal/SharedStrings.cs @@ -13,7 +13,6 @@ namespace Simplexcel.XlsxInternal internal class SharedStrings { private readonly Dictionary _sharedStrings = new Dictionary(StringComparer.Ordinal); - private static Regex _sanitizeRegex = new Regex("[\x00-\x08\x0B\x0C\x0E-\x1F\x26]", RegexOptions.Compiled); /// /// The number of Unique Strings @@ -63,9 +62,11 @@ internal XmlFile ToXmlFile() foreach (var kvp in _sharedStrings.OrderBy(k => k.Value)) { - var str = _sanitizeRegex.Replace(kvp.Key, string.Empty); - var se = new XElement(Namespaces.x + "si", new XElement(Namespaces.x + "t", str)); - sst.Root.Add(se); + var tElem = new XElement(Namespaces.x + "t"); + tElem.Value = kvp.Key; + + var siElem = new XElement(Namespaces.x + "si", tElem); + sst.Root.Add(siElem); } file.Content = sst;