Skip to content

Commit

Permalink
If there are no implicit elements, do not clone
Browse files Browse the repository at this point in the history
This fixes unit test failures that arose because cloning the XmlDocument
caused a spurious `\n` character to be inserted after the XML declaration
tag in the saved output.
  • Loading branch information
rainersigwald committed Dec 1, 2016
1 parent cad9a09 commit 1d821a8
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion src/XMakeBuildEngine/Construction/ProjectRootElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ public class ProjectRootElement : ProjectElementContainer
/// </summary>
private BuildEventContext _buildEventContext;

/// <summary>
/// Xpath expression that will find any element with the implicit attribute
/// </summary>
private static readonly string ImplicitAttributeXpath = $"//*[@{XMakeAttributes.@implicit}]";

/// <summary>
/// Initialize a ProjectRootElement instance from a XmlReader.
/// May throw InvalidProjectFileException.
Expand Down Expand Up @@ -1781,15 +1786,21 @@ public void Save(Encoding saveEncoding)

private XmlDocument RemoveImplicits()
{
if (XmlDocument.SelectSingleNode(ImplicitAttributeXpath) == null)
{
return XmlDocument;
}

var xmlWithNoImplicits = (XmlDocument) XmlDocument.CloneNode(deep: true);

var implicitElements =
xmlWithNoImplicits.SelectNodes($"//*[@{XMakeAttributes.@implicit}]");
xmlWithNoImplicits.SelectNodes(ImplicitAttributeXpath);

foreach (XmlNode implicitElement in implicitElements)
{
implicitElement.ParentNode.RemoveChild(implicitElement);
}

return xmlWithNoImplicits;
}

Expand Down

0 comments on commit 1d821a8

Please sign in to comment.