Skip to content

Commit 893c09b

Browse files
committed
add more whitespace sensitive ISource types knowing about the XML spec
see #39
1 parent aa7ae9f commit 893c09b

9 files changed

+130
-4
lines changed

RELEASE_NOTES.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,9 @@
1212
[#38](https://github.com/xmlunit/xmlunit.net/issues/38). And neither
1313
of the methods could deal with `XmlSignificantWhitespace` at all.
1414

15-
* add `XmlWhitespaceStrippedSource` that only trims characters that
16-
are considered whitespace by the [XML
15+
* add `XmlWhitespaceStrippedSource`, `XmlWhitespaceNormalizedSource`
16+
and `XmlElementContentWhitespaceStrippedSource` that only trim
17+
characters that are considered whitespace by the [XML
1718
Specification](https://www.w3.org/TR/xml11/#NT-S) from textual
1819
content.
1920
Issue [#39](https://github.com/xmlunit/xmlunit.net/issues/39).

src/main/net-core/Input/ElementContentWhitespaceStrippedSource.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,11 @@ namespace Org.XmlUnit.Input {
2222
/// </summary>
2323
/// <remarks>
2424
/// <para>
25+
/// Unlike <see cref="XmlElementContentWhitespaceStrippedSource"/> this class uses
26+
/// Unicode's idea of whitespace rather than the more restricted
27+
/// subset considered whitespace by XML.
28+
/// </para>
29+
/// <para>
2530
/// since XMLUnit 2.6.0
2631
/// </para>
2732
/// </remarks>

src/main/net-core/Input/WhitespaceNormalizedSource.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,20 @@ namespace Org.XmlUnit.Input {
2121
/// all empty text nodes and normalizing the non-empty ones.
2222
/// </summary>
2323
/// <remarks>
24+
/// <para>
2425
/// "normalized" in this context means all whitespace characters
2526
/// are replaced by space characters and consecutive whitespace
2627
/// characters are collapsed.
28+
/// </para>
29+
/// <para>
30+
/// This class is similiar to <see cref="WhitespaceStrippedSource"/>
31+
/// but in addition "normalizes" whitespace.
32+
/// </para>
33+
/// <para>
34+
/// Unlike <see cref="XmlWhitespaceNormalizedSource"/> this class uses
35+
/// Unicode's idea of whitespace rather than the more restricted
36+
/// subset considered whitespace by XML.
37+
/// </para>
2738
/// </remarks>
2839
public class WhitespaceNormalizedSource : DOMSource {
2940
/// <summary>

src/main/net-core/Input/WhitespaceStrippedSource.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ namespace Org.XmlUnit.Input {
2727
/// other text nodes alone you should use
2828
/// ElementContentWhitespaceStrippedSource instead.
2929
/// </para>
30+
/// <para>
31+
/// Unlike <see cref="XmlWhitespaceStrippedSource"/> this class uses
32+
/// Unicode's idea of whitespace rather than the more restricted
33+
/// subset considered whitespace by XML.
34+
/// </para>
3035
/// </remarks>
3136
public class WhitespaceStrippedSource : DOMSource {
3237
/// <summary>
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
This file is licensed to You under the Apache License, Version 2.0
3+
(the "License"); you may not use this file except in compliance with
4+
the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
15+
using Org.XmlUnit.Util;
16+
17+
namespace Org.XmlUnit.Input {
18+
19+
/// <summary>
20+
/// A source that is obtained from a different source by removing all
21+
/// text nodes that only contain whitespace.
22+
/// </summary>
23+
/// <remarks>
24+
/// <para>
25+
/// Unlike <see cref="ElementContentWhitespaceStrippedSource"/>
26+
/// this class uses XML's idea of whitespace rather than the more
27+
/// extensive set considered whitespace by Unicode.
28+
/// </para>
29+
/// <para>
30+
/// since XMLUnit 2.10.0
31+
/// </para>
32+
/// </remarks>
33+
public class XmlElementContentWhitespaceStrippedSource : DOMSource {
34+
/// <summary>
35+
/// Creates a new source that consists of the given source with all
36+
/// text nodes that only contain whitespace stripped.
37+
/// </summary>
38+
/// <param name="originalSource">source with the original content</param>
39+
public XmlElementContentWhitespaceStrippedSource(ISource originalSource) :
40+
base(Nodes.StripXmlElementContentWhitespace(originalSource.ToDocument())) {
41+
SystemId = originalSource.SystemId;
42+
}
43+
}
44+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/*
2+
This file is licensed to You under the Apache License, Version 2.0
3+
(the "License"); you may not use this file except in compliance with
4+
the License. You may obtain a copy of the License at
5+
6+
http://www.apache.org/licenses/LICENSE-2.0
7+
8+
Unless required by applicable law or agreed to in writing, software
9+
distributed under the License is distributed on an "AS IS" BASIS,
10+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
See the License for the specific language governing permissions and
12+
limitations under the License.
13+
*/
14+
15+
using Org.XmlUnit.Util;
16+
17+
namespace Org.XmlUnit.Input {
18+
19+
/// <summary>
20+
/// A source that is obtained from a different source by removing
21+
/// all empty text nodes and normalizing the non-empty ones.
22+
/// </summary>
23+
/// <remarks>
24+
/// <para>
25+
/// "normalized" in this context means all XML whitespace
26+
/// characters are replaced by space characters and
27+
/// consecutive XML whitespace characters are collapsed.
28+
/// </para>
29+
/// <para>
30+
/// This class is similiar to <see cref="XmlWhitespaceStrippedSource"/>
31+
/// but in addition "normalizes" XML whitespace.
32+
/// </para>
33+
/// <para>
34+
/// Unlike <see cref="WhitespaceNormalizedSource"/> this uses XML's idea
35+
/// of whitespace rather than the more extensive set considered
36+
/// whitespace by Unicode.
37+
/// </para>
38+
/// <para>
39+
/// since XMLUnit 2.10.0
40+
/// </para>
41+
/// </remarks>
42+
public class XmlWhitespaceNormalizedSource : DOMSource {
43+
/// <summary>
44+
/// Creates a new Source with the same content as another source normalizing XML whitespace in Text nodes.
45+
/// </summary>
46+
/// <param name="originalSource">source with the original content</param>
47+
public XmlWhitespaceNormalizedSource(ISource originalSource) :
48+
base(Nodes.NormalizeXmlWhitespace(originalSource.ToDocument()))
49+
{
50+
SystemId = originalSource.SystemId;
51+
}
52+
}
53+
}

src/main/net-core/Input/XmlWhitespaceStrippedSource.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ namespace Org.XmlUnit.Input {
3030
/// ElementContentWhitespaceStrippedSource instead.
3131
/// </para>
3232
/// <para>
33+
/// Unlike <see cref="WhitespaceStrippedSource"/> this uses XML's idea
34+
/// of whitespace rather than the more extensive set considered
35+
/// whitespace by Unicode.
36+
/// </para>
37+
/// <para>
3338
/// since XMLUnit 2.10.0
3439
/// </para>
3540
/// </remarks>

src/main/net-core/NetFramework/XMLUnit.Core.NetFramework.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,8 @@
110110
<Compile Include="..\Input\StringSource.cs" />
111111
<Compile Include="..\Input\WhitespaceNormalizedSource.cs" />
112112
<Compile Include="..\Input\WhitespaceStrippedSource.cs" />
113+
<Compile Include="..\Input\XmlElementContentWhitespaceStrippedSource.cs" />
114+
<Compile Include="..\Input\XmlWhitespaceNormalizedSource.cs" />
113115
<Compile Include="..\Input\XmlWhitespaceStrippedSource.cs" />
114116
<Compile Include="..\ISource.cs" />
115117
<Compile Include="..\Transform\Transformation.cs" />

src/main/net-core/Util/Nodes.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public static XmlNode StripXmlWhitespace(XmlNode original) {
130130
/// <para>
131131
/// "normalized" in this context means all whitespace
132132
/// characters are replaced by space characters and
133-
/// consecutive whitespace characaters are collapsed.
133+
/// consecutive whitespace characters are collapsed.
134134
/// </para>
135135
/// <para>
136136
/// This method is similiar to <see cref="StripWhitespace"/>
@@ -159,7 +159,7 @@ public static XmlNode NormalizeWhitespace(XmlNode original) {
159159
/// <para>
160160
/// "normalized" in this context means all XML whitespace
161161
/// characters are replaced by space characters and
162-
/// consecutive XML whitespace characaters are collapsed.
162+
/// consecutive XML whitespace characters are collapsed.
163163
/// </para>
164164
/// <para>
165165
/// This method is similiar to <see cref="StripXmlWhitespace"/>

0 commit comments

Comments
 (0)