-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add a new XmlWhitespaceStrippedSource that honors XML's idea of white…
- Loading branch information
Showing
8 changed files
with
166 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
/* | ||
This file is licensed to You under the Apache License, Version 2.0 | ||
(the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
using Org.XmlUnit.Util; | ||
|
||
namespace Org.XmlUnit.Input { | ||
|
||
/// <summary> | ||
/// A source that is obtained from a different source by removing | ||
/// all empty text nodes and removing all characters XML considers | ||
/// whitespace at the start and end of the text content of the | ||
/// non-empty ones. <see href="https://www.w3.org/TR/xml11/#NT-S"/> | ||
/// </summary> | ||
/// <remarks> | ||
/// <para> | ||
/// If you only want to remove text nodes consisting solely of | ||
/// whitespace (AKA element content whitespace) but leave all | ||
/// other text nodes alone you should use | ||
/// ElementContentWhitespaceStrippedSource instead. | ||
/// </para> | ||
/// </remarks> | ||
public class XmlWhitespaceStrippedSource : DOMSource { | ||
/// <summary> | ||
/// Creates a new Source with the same content as another source trimming whitespace from Text nodes. | ||
/// </summary> | ||
/// <param name="originalSource">source with the original content</param> | ||
public XmlWhitespaceStrippedSource(ISource originalSource) : | ||
base(Nodes.StripXmlWhitespace(originalSource.ToDocument())) { | ||
SystemId = originalSource.SystemId; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
src/tests/net-core/Input/XmlWhitespaceStrippedSourceTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
/* | ||
This file is licensed to You under the Apache License, Version 2.0 | ||
(the "License"); you may not use this file except in compliance with | ||
the License. You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
using System.Xml; | ||
using Org.XmlUnit.Util; | ||
using NUnit.Framework; | ||
|
||
namespace Org.XmlUnit.Input { | ||
|
||
[TestFixture] | ||
public class XmlWhitespaceStrippedSourceTest { | ||
|
||
[Test] | ||
public void XmlWhitespaceIsStrippedProperly() { | ||
XmlWhitespaceIsStrippedProperly(new XmlDocument()); | ||
} | ||
|
||
[Test] | ||
public void XmlWhitespaceIsStrippedProperlyEvenWithPreserveWhitespaceDoc() { | ||
XmlDocument testDoc = new XmlDocument(); | ||
testDoc.PreserveWhitespace = true; | ||
XmlWhitespaceIsStrippedProperly(testDoc); | ||
} | ||
|
||
private void XmlWhitespaceIsStrippedProperly(XmlDocument testDoc) { | ||
string testXml = "<a>\n <b>\n Test Value\u00a0\n </b>\n</a>"; | ||
testDoc.LoadXml(testXml); | ||
XmlWhitespaceStrippedSource s = new XmlWhitespaceStrippedSource(new DOMSource(testDoc)); | ||
XmlNode root = s.Node; | ||
Assert.AreEqual(1, root.ChildNodes.Count); | ||
XmlNode a = root.FirstChild; | ||
Assert.AreEqual(1, a.ChildNodes.Count); | ||
XmlNode b = a.FirstChild; | ||
Assert.AreEqual(1, b.ChildNodes.Count); | ||
Assert.AreEqual("Test Value\u00a0", b.FirstChild.Value); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters