Skip to content

Commit

Permalink
fix: xml intention issue on external <code>
Browse files Browse the repository at this point in the history
  • Loading branch information
yufeih committed Nov 24, 2023
1 parent fae6f1d commit f142db4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

namespace Docfx.Common;

public class XHtmlWriter : XmlWriter
class XHtmlWriter : XmlWriter
{
private static HashSet<string> _voidElements;
private string _currentElement;
Expand All @@ -15,7 +15,7 @@ public class XHtmlWriter : XmlWriter

public XHtmlWriter(TextWriter writer)
{
_writer = Create(writer);
_writer = Create(writer, new() { OmitXmlDeclaration = true });
// void element (ref: http://www.w3.org/TR/html-markup/syntax.html)
_voidElements = new HashSet<string> { "area", "base", "br", "col", "command", "embed", "hr", "img", "input", "keygen", "link", "meta", "param", "source", "track", "wbr" };
}
Expand Down
4 changes: 3 additions & 1 deletion src/Docfx.Dotnet/Parsers/XmlComment.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,9 @@ private XmlComment(string xml, XmlCommentParserContext context)
}

// Transform triple slash comment
var doc = XmlCommentTransformer.Transform(xml);
xml = XmlCommentTransformer.Transform(xml);
xml = XDocument.Parse(xml).ToString();
var doc = XDocument.Parse(xml, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);

_context = context;

Expand Down
11 changes: 5 additions & 6 deletions src/Docfx.Dotnet/Parsers/XmlCommentTransformer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Docfx.Dotnet;

internal static class XmlCommentTransformer
static class XmlCommentTransformer
{
private static readonly XslCompiledTransform _transform;

Expand All @@ -25,13 +25,12 @@ static XmlCommentTransformer()
_transform.Load(reader, xsltSettings, new XmlUrlResolver());
}

public static XDocument Transform(string xml)
public static string Transform(string xml)
{
using var ms = new MemoryStream();
using var writer = new XHtmlWriter(new StreamWriter(ms));
using var ms = new StringWriter();
using var writer = new XHtmlWriter(ms);
XDocument doc = XDocument.Parse(xml, LoadOptions.PreserveWhitespace);
_transform.Transform(doc.CreateNavigator(), writer);
ms.Seek(0, SeekOrigin.Begin);
return XDocument.Load(ms, LoadOptions.PreserveWhitespace | LoadOptions.SetLineInfo);
return ms.ToString();
}
}
75 changes: 43 additions & 32 deletions test/Docfx.Dotnet.Tests/XmlCommentUnitTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,35 @@ public void ExternalCodeBlockXaml()
ignoreLineEndingDifferences: true);
}

[Theory]
[InlineData("<example><code source='Example.cs' region='SDK_CustomProcessor' /></example>")]
[InlineData("""
<example>
<code source='Example.cs' region='SDK_CustomProcessor' />
</example>
""")]
public void Issue9462(string input)
{
var commentModel = XmlComment.Parse(input, new()
{
ResolveCode = _ =>
"""
#region SDK_CustomProcessor
using System;
using System.Collections.Generic;
#endregion
"""
});
Assert.Equal(
"""
<pre><code class="lang-cs">using System;
using System.Collections.Generic;</code></pre>
""",
commentModel.Examples.Single(),
ignoreLineEndingDifferences: true);
}

[Fact]
public static void MarkdownCodeBlock()
{
Expand Down Expand Up @@ -338,16 +367,23 @@ Classes in assemblies are by definition complete.
<a href="https://example.org">https://example.org</a>
<a href="https://example.org">example</a>
<p>This is <code class="paramref">ref</code> a sample of exception node</p>
<ul><li>
<pre><code class="lang-c#">public class XmlElement
: XmlLinkedNode</code></pre>
<ol><li>
<ul>
<li>
<pre><code class="lang-c#">public class XmlElement
: XmlLinkedNode</code></pre>
<ol>
<li>
word inside list-&gt;listItem-&gt;list-&gt;listItem-&gt;para.&gt;
the second line.
</li><li>item2 in numbered list</li></ol>
</li><li>item2 in bullet list</li><li>
</li>
<li>item2 in numbered list</li>
</ol>
</li>
<li>item2 in bullet list</li>
<li>
loose text <i>not</i> wrapped in description
</li></ul>
</li>
</ul>
""", remarks, ignoreLineEndingDifferences: true);

var exceptions = commentModel.Exceptions;
Expand Down Expand Up @@ -439,29 +475,4 @@ public void ParseXmlCommentWithoutRootNode()
var commentModel = XmlComment.Parse(input, new());
Assert.Equal("A", commentModel.Summary);
}

[Fact]
public void Issue9462()
{
var input = @"<example><code source=""Example.cs"" region=""SDK_CustomProcessor"" /></example>";
var commentModel = XmlComment.Parse(input, new()
{
ResolveCode = _ =>
"""
#region SDK_CustomProcessor
using System;
using System.Collections.Generic;
#endregion
"""
});
Assert.Equal(
"""
<pre>
<code class="lang-cs">using System;
using System.Collections.Generic;</code>
</pre>
"""
, commentModel.Examples[0]);
}
}

0 comments on commit f142db4

Please sign in to comment.