Skip to content

Commit

Permalink
Merge pull request #13 from AngleSharp/devel
Browse files Browse the repository at this point in the history
Release 0.14.0
  • Loading branch information
FlorianRappl authored Mar 31, 2020
2 parents 4181aa4 + bee8047 commit 0c03427
Show file tree
Hide file tree
Showing 17 changed files with 151 additions and 87 deletions.
11 changes: 11 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
# 0.14.0

Released on Tuesday, March 31 2020.

- Added more overloads for `ToXml` and `ToMarkup`
- Allow transforming all empty elements to self closing tags
- Serialize self-closing tags correctly (#11)
- Updated the `IMarkupFormatter` implementations
- Added shallow support for source link
- Support more target frameworks

# 0.13.0

Released on Friday, September 6 2019.
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Thanks :heart: to all who contributed to AngleSharp.Xml via issues, pull request
AngleSharp.Xml contains code written by (in order of first pull request / commit):

* [Florian Rappl](https://github.com/FlorianRappl)
* [Konstantin Safonov](https://github.com/kasthack)

Without these awesome people AngleSharp.Xml could not exist. Thanks to everyone for your contributions! :beers:

Expand Down
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
The MIT License (MIT)

Copyright (c) 2013 - 2019 AngleSharp
Copyright (c) 2013 - 2020 AngleSharp

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ This project is supported by the [.NET Foundation](https://dotnetfoundation.org)

The MIT License (MIT)

Copyright (c) 2019 AngleSharp
Copyright (c) 2020 AngleSharp

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

Expand Down
4 changes: 4 additions & 0 deletions build.cake
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@ var projectName = "AngleSharp.Xml";
var solutionName = "AngleSharp.Xml";
var frameworks = new Dictionary<String, String>
{
{ "net46", "net46" },
{ "net461", "net461" },
{ "net472", "net472" },
{ "netstandard1.3", "netstandard1.3" },
{ "netstandard2.0", "netstandard2.0" },
};

#load tools/anglesharp.cake
Expand Down
10 changes: 7 additions & 3 deletions src/AngleSharp.Xml.Tests/AngleSharp.Xml.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="12.0.1" />
<PackageReference Include="NUnit" Version="3.11.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.11.0" />
<PackageReference Include="AngleSharp" Version="0.14.0" />
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
<PackageReference Include="NUnit" Version="3.12.0" />
<PackageReference Include="NUnit3TestAdapter" Version="3.16.1">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Appveyor.TestLogger" Version="2.0.0" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="15.9.0" />
</ItemGroup>
Expand Down
36 changes: 35 additions & 1 deletion src/AngleSharp.Xml.Tests/Various.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
namespace AngleSharp.Xml.Tests
namespace AngleSharp.Xml.Tests
{
using AngleSharp.Dom;
using AngleSharp.Io;
Expand Down Expand Up @@ -63,6 +63,40 @@ public async Task GenerateDocumentFromSvgWithSvgContentType()
Assert.AreEqual("path", document.DocumentElement.FirstElementChild.NodeName);
}

[Test]
public void SelfClosingTagsAreSerializedCorrectlyByDefaultFormatter_Issue11()
{
var parser = new XmlParser();
var xmlDoc = parser.ParseDocument(
@"<Project Sdk=""Microsoft.NET.Sdk"">
<ItemGroup>
<PackageReference Include=""AngleSharp"" Version=""0.12.1"" />
<PackageReference></PackageReference>
</ItemGroup>
</Project>");
var xml = xmlDoc.ToXml();
Assert.AreEqual("<Project Sdk=\"Microsoft.NET.Sdk\">\n <ItemGroup>\n <PackageReference Include=\"AngleSharp\" Version=\"0.12.1\" />\n <PackageReference></PackageReference>\n </ItemGroup>\n </Project>", xml);
}

[Test]
public void EmptyTagsAreSerializedCorrectlyWithStandardFormatterWithOption_Issue11()
{
var parser = new XmlParser();
var xmlDoc = parser.ParseDocument(
@"<Project Sdk=""Microsoft.NET.Sdk"">
<ItemGroup>
<PackageReference Include=""AngleSharp"" Version=""0.12.1"" />
<PackageReference></PackageReference>
</ItemGroup>
</Project>");
var formatter = new XmlMarkupFormatter
{
IsAlwaysSelfClosing = true,
};
var xml = xmlDoc.ToHtml(formatter);
Assert.AreEqual("<Project Sdk=\"Microsoft.NET.Sdk\">\n <ItemGroup>\n <PackageReference Include=\"AngleSharp\" Version=\"0.12.1\" />\n <PackageReference />\n </ItemGroup>\n </Project>", xml);
}

private static Task<IDocument> GenerateDocument(String content, String contentType)
{
var config = Configuration.Default.WithDefaultLoader().WithXml();
Expand Down
4 changes: 2 additions & 2 deletions src/AngleSharp.Xml.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
<requireLicenseAcceptance>false</requireLicenseAcceptance>
<description>Adds a powerful XML and DTD parser to AngleSharp.</description>
<releaseNotes>https://github.com/AngleSharp/AngleSharp.Xml/blob/master/CHANGELOG.md</releaseNotes>
<copyright>Copyright 2016-2019, AngleSharp</copyright>
<copyright>Copyright 2016-2020, AngleSharp</copyright>
<tags>html html5 css css3 dom requester http https xml dtd</tags>
<dependencies>
<dependency id="AngleSharp" version="0.13.0" />
<dependency id="AngleSharp" version="0.14.0" />
</dependencies>
</metadata>
</package>
16 changes: 14 additions & 2 deletions src/AngleSharp.Xml/AngleSharp.Xml.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,26 @@
<PropertyGroup>
<AssemblyName>AngleSharp.Xml</AssemblyName>
<RootNamespace>AngleSharp.Xml</RootNamespace>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">netstandard1.3</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' != 'Windows_NT' ">netstandard1.3;netstandard2.0</TargetFrameworks>
<TargetFrameworks Condition=" '$(OS)' == 'Windows_NT' ">netstandard1.3;netstandard2.0;net46;net461;net472</TargetFrameworks>
<SignAssembly>true</SignAssembly>
<AssemblyOriginatorKeyFile>Key.snk</AssemblyOriginatorKeyFile>
<GenerateDocumentationFile>true</GenerateDocumentationFile>
<LangVersion>7.1</LangVersion>
<RepositoryUrl>https://github.com/AngleSharp/AngleSharp.Xml</RepositoryUrl>
<RepositoryType>git</RepositoryType>
<PublishRepositoryUrl>true</PublishRepositoryUrl>
<EmbedUntrackedSources>true</EmbedUntrackedSources>
<IncludeSymbols>true</IncludeSymbols>
<SymbolPackageFormat>snupkg</SymbolPackageFormat>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AngleSharp" Version="0.13.0" />
<PackageReference Include="Microsoft.SourceLink.GitHub" Version="1.0.0" PrivateAssets="All"/>
</ItemGroup>

<ItemGroup>
<PackageReference Include="AngleSharp" Version="0.14.0" />
</ItemGroup>

<PropertyGroup Condition=" '$(OS)' == 'Windows_NT' ">
Expand Down
70 changes: 16 additions & 54 deletions src/AngleSharp.Xml/AutoSelectedMarkupFormatter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace AngleSharp.Xml
/// AutoSelectedMarkupFormatter class to select the proper MarkupFormatter
/// implementation depending on the used document type.
/// </summary>
public sealed class AutoSelectedMarkupFormatter : IMarkupFormatter
public class AutoSelectedMarkupFormatter : IMarkupFormatter
{
#region Fields

Expand Down Expand Up @@ -60,84 +60,46 @@ private IMarkupFormatter ChildFormatter

#region Methods

/// <summary>
/// Formats an attribute specified by the argument.
/// </summary>
/// <param name="attribute">The attribute to serialize.</param>
/// <returns>The formatted attribute.</returns>
public String Attribute(IAttr attribute) => ChildFormatter.Attribute(attribute);

/// <summary>
/// Formats opening a tag with the given name.
/// </summary>
/// <param name="element">The element to open.</param>
/// <param name="selfClosing">
/// Is the element actually self-closing?
/// </param>
/// <returns>The formatted opening tag.</returns>
public String OpenTag(IElement element, Boolean selfClosing)
/// <inheritdoc />
public virtual String OpenTag(IElement element, Boolean selfClosing)
{
Confirm(element.Owner.Doctype);
return ChildFormatter.OpenTag(element, selfClosing);
}

/// <summary>
/// Formats closing a tag with the given name.
/// </summary>
/// <param name="element">The element to close.</param>
/// <param name="selfClosing">
/// Is the element actually self-closing?
/// </param>
/// <returns>The formatted closing tag.</returns>
public String CloseTag(IElement element, Boolean selfClosing)
/// <inheritdoc />
public virtual String CloseTag(IElement element, Boolean selfClosing)
{
Confirm(element.Owner.Doctype);
return ChildFormatter.CloseTag(element, selfClosing);
}

/// <summary>
/// Formats the given comment.
/// </summary>
/// <param name="comment">The comment to stringify.</param>
/// <returns>The formatted comment.</returns>
public String Comment(IComment comment)
/// <inheritdoc />
public virtual String Comment(IComment comment)
{
Confirm(comment.Owner.Doctype);
return ChildFormatter.Comment(comment);
}

/// <summary>
/// Formats the given doctype using the name, public and system
/// identifiers.
/// </summary>
/// <param name="doctype">The document type to stringify.</param>
/// <returns>The formatted doctype.</returns>
public String Doctype(IDocumentType doctype)
/// <inheritdoc />
public virtual String Doctype(IDocumentType doctype)
{
Confirm(doctype);
return ChildFormatter.Doctype(doctype);
}

/// <summary>
/// Formats the given processing instruction using the target and the
/// data.
/// </summary>
/// <param name="processing">
/// The processing instruction to stringify.
/// </param>
/// <returns>The formatted processing instruction.</returns>
public String Processing(IProcessingInstruction processing)
/// <inheritdoc />
public virtual String Processing(IProcessingInstruction processing)
{
Confirm(processing.Owner.Doctype);
return ChildFormatter.Processing(processing);
}

/// <summary>
/// Formats the given text.
/// </summary>
/// <param name="text">The text to sanatize.</param>
/// <returns>The formatted text.</returns>
public String Text(ICharacterData text) => ChildFormatter.Text(text);
/// <inheritdoc />
public virtual String Text(ICharacterData text) => ChildFormatter.Text(text);

/// <inheritdoc />
public virtual String LiteralText(ICharacterData text) => ChildFormatter.LiteralText(text);

#endregion

Expand Down
5 changes: 1 addition & 4 deletions src/AngleSharp.Xml/Dom/Internal/SvgDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,7 @@ internal SvgDocument(IBrowsingContext context = null)

#region Methods

public override Element CreateElementFrom(String name, String prefix)
{
return _factory.Create(this, name, prefix) as Element;
}
public override Element CreateElementFrom(String name, String prefix, NodeFlags flags = NodeFlags.None) => _factory.Create(this, name, prefix, flags);

public override Node Clone(Document owner, Boolean deep)
{
Expand Down
2 changes: 1 addition & 1 deletion src/AngleSharp.Xml/Dom/Internal/XmlDocument.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ internal XmlDocument(IBrowsingContext context = null)

#region Methods

public override Element CreateElementFrom(String name, String prefix) => new XmlElement(this, name, prefix);
public override Element CreateElementFrom(String name, String prefix, NodeFlags flags = NodeFlags.None) => new XmlElement(this, name, prefix, flags: flags);

public override Node Clone(Document owner, Boolean deep)
{
Expand Down
4 changes: 2 additions & 2 deletions src/AngleSharp.Xml/Dom/Internal/XmlElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ sealed class XmlElement : Element
{
#region ctor

public XmlElement(Document owner, String name, String prefix = null)
: base(owner, name, prefix, null)
public XmlElement(Document owner, String name, String prefix = null, String namespaceUri = null, NodeFlags flags = NodeFlags.None)
: base(owner, name, prefix, namespaceUri, flags)
{
}

Expand Down
18 changes: 18 additions & 0 deletions src/AngleSharp.Xml/MarkupFormatterExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace AngleSharp.Xml
{
using System;
using System.IO;

/// <summary>
/// Extensions for the markup formattable elements.
Expand All @@ -16,6 +17,14 @@ public static class MarkupFormatterExtensions
public static String ToXml(this IMarkupFormattable markup) =>
markup.ToHtml(XmlMarkupFormatter.Instance);

/// <summary>
/// Serializes the object model guided by the XML markup formatter.
/// </summary>
/// <param name="markup">The markup to serialize.</param>
/// <param name="writer">The output target of the serialization.</param>
public static void ToXml(this IMarkupFormattable markup, TextWriter writer) =>
markup.ToHtml(writer, XmlMarkupFormatter.Instance);

/// <summary>
/// Returns the serialization of the object model guided by the
/// auto selected (XML, XHTML, HTML) markup formatter.
Expand All @@ -24,5 +33,14 @@ public static String ToXml(this IMarkupFormattable markup) =>
/// <returns>The source code snippet.</returns>
public static String ToMarkup(this IMarkupFormattable markup) =>
markup.ToHtml(new AutoSelectedMarkupFormatter());

/// <summary>
/// Serializes the object model guided by the auto selected (XML,
/// XHTML, HTML) markup formatter.
/// </summary>
/// <param name="markup">The markup to serialize.</param>
/// <param name="writer">The output target of the serialization.</param>
public static void ToMarkup(this IMarkupFormattable markup, TextWriter writer) =>
markup.ToHtml(writer, new AutoSelectedMarkupFormatter());
}
}
7 changes: 4 additions & 3 deletions src/AngleSharp.Xml/Parser/XmlDomBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,7 @@ private void InBody(XmlToken token)
case XmlTokenType.StartTag:
{
var tagToken = (XmlTagToken)token;
var element = CreateElement(tagToken.Name);
var element = CreateElement(tagToken.Name, tagToken.IsSelfClosing);
CurrentNode.AppendChild(element);

for (var i = 0; i < tagToken.Attributes.Count; i++)
Expand Down Expand Up @@ -402,18 +402,19 @@ private void AfterBody(XmlToken token)

#region Helpers

private Element CreateElement(String name)
private Element CreateElement(String name, Boolean selfClosing)
{
var prefix = default(String);
var colon = name.IndexOf(Symbols.Colon);
var flags = selfClosing ? NodeFlags.SelfClosing : NodeFlags.None;

if (colon > 0 && colon < name.Length - 1)
{
prefix = name.Substring(0, colon);
name = name.Substring(colon + 1);
}

return _document.CreateElementFrom(name, prefix);
return _document.CreateElementFrom(name, prefix, flags);
}

private Attr CreateAttribute(String name, String value)
Expand Down
Loading

0 comments on commit 0c03427

Please sign in to comment.