Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion Src/CSharpier.Core/Xml/XNodePrinters/Node.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Text.RegularExpressions;
using System.Xml.Linq;
using CSharpier.Core.DocTypes;
using CSharpier.Core.Utilities;
Expand All @@ -6,6 +7,8 @@ namespace CSharpier.Core.Xml.XNodePrinters;

internal static class Node
{
private static readonly Regex NewlineRegex = new(@"\r\n|\n|\r", RegexOptions.Compiled);

internal static Doc Print(XNode xNode, XmlPrintingContext context)
{
if (xNode is XDocument xDocument)
Expand Down Expand Up @@ -57,7 +60,7 @@ internal static Doc Print(XNode xNode, XmlPrintingContext context)

if (xNode is XComment or XProcessingInstruction)
{
return xNode.ToString();
return NewlineRegex.Replace(xNode.ToString(), context.Options.LineEnding);
}

throw new Exception("Need to handle + " + xNode.GetType());
Expand Down
23 changes: 23 additions & 0 deletions Src/CSharpier.Tests/CommandLineFormatterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -879,6 +879,29 @@ public void Should_Support_Config_Path_With_Editor_Config()
context.GetFileContent(fileName).Should().Be("var myVariable =\n someLongValue;\n");
}

[TestCase("\r\n")]
[TestCase("\n")]
public void Format_XML_With_Multiline_Comment_Uses_Consistent_Line_Breaks(string lineBreak)
{
var context = new TestContext();
var content = new StringBuilder();
#pragma warning disable CA1305
// avoiding raw strings because this needs to use specific line breaks
content.Append($"<Root>{lineBreak}");
content.Append($" <Element />{lineBreak}");
content.Append($" <!--{lineBreak}");
content.Append($" SomeText{lineBreak}");
content.Append($" -->{lineBreak}");
content.Append($"</Root>{lineBreak}");
#pragma warning restore CA1305

context.WhenAFileExists("Xml.xml", content.ToString());

Format(context);

context.GetFileContent("Xml.xml").Should().Be(content.ToString());
}

private static FormatResult Format(
TestContext context,
bool skipWrite = false,
Expand Down