Skip to content

Commit

Permalink
XmlWriter respects NewLineOnAttributes when writing xmlns (#81822)
Browse files Browse the repository at this point in the history
* XmlWriter respects NewLineOnAttributes when writing xmlns

* formatting
  • Loading branch information
devsko authored Feb 9, 2023
1 parent d0fdb17 commit c00942f
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,18 @@ internal override void WriteStartNamespaceDeclaration(string prefix)

if (_trackTextContent && _inTextContent) { ChangeTextContentMark(false); }

if (_attrEndPos == _bufPos)
{
_bufChars[_bufPos++] = (char)' ';
}

if (prefix.Length == 0)
{
RawText(" xmlns=\"");
RawText("xmlns=\"");
}
else
{
RawText(" xmlns:");
RawText("xmlns:");
RawText(prefix);
_bufChars[_bufPos++] = (char)'=';
_bufChars[_bufPos++] = (char)'"';
Expand Down Expand Up @@ -2037,6 +2042,18 @@ public override void WriteStartAttribute(string? prefix, string localName, strin
base.WriteStartAttribute(prefix, localName, ns);
}

// Same as base class, plus possible indentation.
internal override void WriteStartNamespaceDeclaration(string prefix)
{
// Add indentation
if (_newLineOnAttributes)
{
WriteIndent();
}

base.WriteStartNamespaceDeclaration(prefix);
}

public override void WriteCData(string? text)
{
_mixedContent = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -318,13 +318,18 @@ internal override async Task WriteStartNamespaceDeclarationAsync(string prefix)

if (_trackTextContent && _inTextContent) { ChangeTextContentMark(false); }

if (_attrEndPos == _bufPos)
{
_bufChars[_bufPos++] = (char)' ';
}

if (prefix.Length == 0)
{
await RawTextAsync(" xmlns=\"").ConfigureAwait(false);
await RawTextAsync("xmlns=\"").ConfigureAwait(false);
}
else
{
await RawTextAsync(" xmlns:").ConfigureAwait(false);
await RawTextAsync("xmlns:").ConfigureAwait(false);
await RawTextAsync(prefix).ConfigureAwait(false);
_bufChars[_bufPos++] = (char)'=';
_bufChars[_bufPos++] = (char)'"';
Expand Down Expand Up @@ -1976,6 +1981,19 @@ protected internal override async Task WriteStartAttributeAsync(string? prefix,
await base.WriteStartAttributeAsync(prefix, localName, ns).ConfigureAwait(false);
}

// Same as base class, plus possible indentation.
internal override async Task WriteStartNamespaceDeclarationAsync(string prefix)
{
CheckAsyncCall();
// Add indentation
if (_newLineOnAttributes)
{
await WriteIndentAsync().ConfigureAwait(false);
}

await base.WriteStartNamespaceDeclarationAsync(prefix).ConfigureAwait(false);
}

public override Task WriteCDataAsync(string? text)
{
CheckAsyncCall();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -458,13 +458,18 @@ namespace System.Xml

#><#= SetTextContentMark(3, false) #>

if (_attrEndPos == _bufPos)
{
<#= BufferName #>[_bufPos++] = (<#= BufferType #>)' ';
}

if (prefix.Length == 0)
{
RawText(" xmlns=\"");
RawText("xmlns=\"");
}
else
{
RawText(" xmlns:");
RawText("xmlns:");
RawText(prefix);
<#= BufferName #>[_bufPos++] = (<#= BufferType #>)'=';
<#= BufferName #>[_bufPos++] = (<#= BufferType #>)'"';
Expand Down Expand Up @@ -2090,6 +2095,18 @@ namespace System.Xml
base.WriteStartAttribute(prefix, localName, ns);
}

// Same as base class, plus possible indentation.
internal override void WriteStartNamespaceDeclaration(string prefix)
{
// Add indentation
if (_newLineOnAttributes)
{
WriteIndent();
}

base.WriteStartNamespaceDeclaration(prefix);
}

public override void WriteCData(string text)
{
_mixedContent = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -321,13 +321,18 @@ namespace System.Xml

#><#= SetTextContentMark(3, false) #>

if (_attrEndPos == _bufPos)
{
<#= BufferName #>[_bufPos++] = (<#= BufferType #>)' ';
}

if (prefix.Length == 0)
{
await RawTextAsync(" xmlns=\"").ConfigureAwait(false);
await RawTextAsync("xmlns=\"").ConfigureAwait(false);
}
else
{
await RawTextAsync(" xmlns:").ConfigureAwait(false);
await RawTextAsync("xmlns:").ConfigureAwait(false);
await RawTextAsync(prefix).ConfigureAwait(false);
<#= BufferName #>[_bufPos++] = (<#= BufferType #>)'=';
<#= BufferName #>[_bufPos++] = (<#= BufferType #>)'"';
Expand Down Expand Up @@ -1919,6 +1924,19 @@ namespace System.Xml
await base.WriteStartAttributeAsync(prefix, localName, ns).ConfigureAwait(false);
}

// Same as base class, plus possible indentation.
internal override async Task WriteStartNamespaceDeclarationAsync(string prefix)
{
CheckAsyncCall();
// Add indentation
if (_newLineOnAttributes)
{
await WriteIndentAsync().ConfigureAwait(false);
}

await base.WriteStartNamespaceDeclarationAsync(prefix).ConfigureAwait(false);
}

public override Task WriteCDataAsync(string text)
{
CheckAsyncCall();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -363,13 +363,18 @@ internal override void WriteStartNamespaceDeclaration(string prefix)
{
Debug.Assert(prefix != null);

if (_attrEndPos == _bufPos)
{
_bufBytes[_bufPos++] = (byte)' ';
}

if (prefix.Length == 0)
{
RawText(" xmlns=\"");
RawText("xmlns=\"");
}
else
{
RawText(" xmlns:");
RawText("xmlns:");
RawText(prefix);
_bufBytes[_bufPos++] = (byte)'=';
_bufBytes[_bufPos++] = (byte)'"';
Expand Down Expand Up @@ -1894,6 +1899,18 @@ public override void WriteStartAttribute(string? prefix, string localName, strin
base.WriteStartAttribute(prefix, localName, ns);
}

// Same as base class, plus possible indentation.
internal override void WriteStartNamespaceDeclaration(string prefix)
{
// Add indentation
if (_newLineOnAttributes)
{
WriteIndent();
}

base.WriteStartNamespaceDeclaration(prefix);
}

public override void WriteCData(string? text)
{
_mixedContent = true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -282,13 +282,18 @@ internal override async Task WriteStartNamespaceDeclarationAsync(string prefix)
CheckAsyncCall();
Debug.Assert(prefix != null);

if (_attrEndPos == _bufPos)
{
_bufBytes[_bufPos++] = (byte)' ';
}

if (prefix.Length == 0)
{
await RawTextAsync(" xmlns=\"").ConfigureAwait(false);
await RawTextAsync("xmlns=\"").ConfigureAwait(false);
}
else
{
await RawTextAsync(" xmlns:").ConfigureAwait(false);
await RawTextAsync("xmlns:").ConfigureAwait(false);
await RawTextAsync(prefix).ConfigureAwait(false);
_bufBytes[_bufPos++] = (byte)'=';
_bufBytes[_bufPos++] = (byte)'"';
Expand Down Expand Up @@ -1841,6 +1846,19 @@ protected internal override async Task WriteStartAttributeAsync(string? prefix,
await base.WriteStartAttributeAsync(prefix, localName, ns).ConfigureAwait(false);
}

// Same as base class, plus possible indentation.
internal override async Task WriteStartNamespaceDeclarationAsync(string prefix)
{
CheckAsyncCall();
// Add indentation
if (_newLineOnAttributes)
{
await WriteIndentAsync().ConfigureAwait(false);
}

await base.WriteStartNamespaceDeclarationAsync(prefix).ConfigureAwait(false);
}

public override Task WriteCDataAsync(string? text)
{
CheckAsyncCall();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@
<Compile Include="XmlDocument\XmlProcessingInstructionTests\DataTests.cs" />
<Compile Include="XmlDocument\XmlProcessingInstructionTests\TargetTests.cs" />
<Compile Include="XmlDocument\XmlTextTests\SplitTextTests.cs" />
<Compile Include="XmlReader\Tests\ReadCharsTests.cs" />
<Content Include="XmlDocument\example.xml">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
Expand Down Expand Up @@ -326,6 +325,7 @@
<Compile Include="XmlReader\Tests\AsyncReaderLateInitTests.cs" />
<Compile Include="XmlReader\Tests\DisposeTests.cs" />
<Compile Include="XmlReader\Tests\BaseUriTests.cs" />
<Compile Include="XmlReader\Tests\ReadCharsTests.cs" />
<Compile Include="XmlReader\Tests\ReaderEncodingTests.cs" />
</ItemGroup>
<ItemGroup>
Expand Down Expand Up @@ -437,6 +437,7 @@
<Compile Include="XmlWriter\WriteWithEncoding.cs" />
<Compile Include="XmlWriter\WriteWithEncodingWithFallback.cs" />
<Compile Include="XmlWriter\WriteWithInvalidSurrogate.cs" />
<Compile Include="XmlWriter\WriteWithXmlnsNewLine.cs" />
<Compile Include="XmlWriter\XmlTextWriterTests.cs" />
</ItemGroup>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xunit;

namespace System.Xml.XmlWriterTests
{
public class XmlWriterTests_XmlnsNewLine
{
[Fact]
public static void WriteWithXmlnsNewLine()
{
XmlDocument xml = new();
xml.LoadXml("<svg version=\"1.1\" xmlns=\"http://www.w3.org/2000/svg\" width=\"10\" xmlns:xlink=\"http://www.w3.org/1999/xlink\" height=\"10\"><g /></svg>");

XmlWriterSettings settings = new();
settings.NewLineOnAttributes = true;
settings.NewLineChars = "\n";
settings.Indent = true;
settings.IndentChars = " ";

StringBuilder output = new();
using (XmlWriter writer = XmlWriter.Create(output, settings))
{
xml.Save(writer);
}

Assert.Equal("<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<svg\n version=\"1.1\"\n xmlns=\"http://www.w3.org/2000/svg\"\n width=\"10\"\n xmlns:xlink=\"http://www.w3.org/1999/xlink\"\n height=\"10\">\n <g />\n</svg>", output.ToString());
}
}
}

0 comments on commit c00942f

Please sign in to comment.