Skip to content

Commit 0a27948

Browse files
committed
Preserve line break pre for OSX/Windows
1 parent 042efa1 commit 0a27948

File tree

5 files changed

+53
-7
lines changed

5 files changed

+53
-7
lines changed

examples/Demo/Program.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,15 @@ static async Task Main(string[] args)
1717
const string filename = "test.docx";
1818
string html = ResourceHelper.GetString("Resources.CompleteRunTest.html");
1919
if (File.Exists(filename)) File.Delete(filename);
20+
const string preformattedText = @"
21+
^__^
22+
(oo)\_______
23+
(__)\ )\/\
24+
||----w |
25+
|| ||";
2026

27+
html = @$"<pre role='img' aria-label='ASCII COW'>
28+
{preformattedText}</pre>";
2129
using (MemoryStream generatedDocument = new MemoryStream())
2230
{
2331
// Uncomment and comment the second using() to open an existing template document

src/Html2OpenXml/Expressions/ParsingContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ sealed class ParsingContext(HtmlConverter converter, MainDocumentPart mainPart)
3434
private Dictionary<string, object?> propertyBag = [];
3535

3636
/// <summary>Whether the text content should preserve the line breaks.</summary>
37-
public bool PreverseLinebreaks { get; set; }
37+
public bool PreserveLinebreaks { get; set; }
3838

3939
/// <summary>Whether the text content should collapse the whitespaces.</summary>
4040
public bool CollapseWhitespaces { get; set; } = true;

src/Html2OpenXml/Expressions/PreElementExpression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ public override IEnumerable<OpenXmlElement> Interpret(ParsingContext context)
2626
{
2727
ComposeStyles(context);
2828
var childContext = context.CreateChild(this);
29-
childContext.PreverseLinebreaks = true;
29+
childContext.PreserveLinebreaks = true;
3030
childContext.CollapseWhitespaces = false;
3131
var childElements = Interpret(childContext, node.ChildNodes);
3232

src/Html2OpenXml/Expressions/TextExpression.cs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public override IEnumerable<OpenXmlElement> Interpret (ParsingContext context)
3131
string text = node.TextContent.Normalize();
3232
if (text.Trim().Length == 0) return [];
3333

34-
if (!context.PreverseLinebreaks)
34+
if (!context.PreserveLinebreaks)
3535
text = text.CollapseLineBreaks();
3636
if (context.CollapseWhitespaces && text[0].IsWhiteSpaceCharacter() &&
3737
node.PreviousSibling is IHtmlImageElement)
@@ -41,9 +41,36 @@ public override IEnumerable<OpenXmlElement> Interpret (ParsingContext context)
4141
else if (context.CollapseWhitespaces)
4242
text = text.CollapseAndStrip();
4343

44-
Run run = new(
45-
new Text(text)
46-
);
44+
if (!context.PreserveLinebreaks)
45+
return [new Run(new Text(text))];
46+
47+
var run = new Run();
48+
char[] chars = text.ToCharArray();
49+
int shift = 0, c = 0;
50+
bool wasCR = false; // avoid adding 2 breaks for \r\n
51+
for ( ; c < chars.Length ; c++)
52+
{
53+
if (!chars[c].IsLineBreak())
54+
{
55+
wasCR = false;
56+
continue;
57+
}
58+
59+
if (wasCR) continue;
60+
wasCR = chars[c] == Symbols.CarriageReturn;
61+
62+
if (c > 1)
63+
{
64+
run.Append(new Text(new string(chars, shift, c - shift))
65+
{ Space = SpaceProcessingModeValues.Preserve });
66+
run.Append(new Break());
67+
}
68+
shift = c + 1;
69+
}
70+
71+
if (c > shift)
72+
run.Append(new Text(new string(chars, shift, c - shift))
73+
{ Space = SpaceProcessingModeValues.Preserve });
4774

4875
return [run];
4976
}

test/HtmlToOpenXml.Tests/TableTests.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,11 +387,22 @@ public void ParsePreAsTable()
387387
var cell = cells.First();
388388
Assert.Multiple(() =>
389389
{
390-
Assert.That(cell.InnerText, Is.EqualTo(preformattedText));
391390
Assert.That(cell.TableCellProperties?.TableCellBorders.ChildElements.Count(), Is.EqualTo(4));
392391
Assert.That(cell.TableCellProperties?.TableCellBorders.ChildElements, Has.All.InstanceOf<BorderType>());
393392
Assert.That(cell.TableCellProperties?.TableCellBorders.Elements<BorderType>().All(b => b.Val.Value == BorderValues.Single), Is.True);
394393
});
394+
395+
var run = cell.GetFirstChild<Paragraph>()?.GetFirstChild<Run>();
396+
Assert.Multiple(() =>
397+
{
398+
var odds = run.ChildElements.Where((item, index) => index % 2 != 0);
399+
Assert.That(odds, Has.All.TypeOf<Break>());
400+
Assert.That(run.ChildElements.ElementAt(0).InnerText, Is.EqualTo(" ^__^"));
401+
Assert.That(run.ChildElements.ElementAt(2).InnerText, Is.EqualTo(" (oo)\\_______"));
402+
Assert.That(run.ChildElements.ElementAt(4).InnerText, Is.EqualTo(" (__)\\ )\\/\\"));
403+
Assert.That(run.ChildElements.ElementAt(6).InnerText, Is.EqualTo(" ||----w |"));
404+
Assert.That(run.ChildElements.ElementAt(8).InnerText, Is.EqualTo(" || ||"));
405+
});
395406
}
396407

397408
[Test]

0 commit comments

Comments
 (0)