Skip to content

Commit 89c6547

Browse files
committed
.
1 parent 9716e7c commit 89c6547

File tree

4 files changed

+61
-33
lines changed

4 files changed

+61
-33
lines changed

docs/verify-xml.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public Task XmlIgnoreMember() =>
6666
VerifyXml(xml)
6767
.IgnoreMember("node");
6868
```
69-
<sup><a href='/src/Verify.Tests/XmlTests.cs#L30-L37' title='Snippet source file'>snippet source</a> | <a href='#snippet-XmlIgnoreMember' title='Start of snippet'>anchor</a></sup>
69+
<sup><a href='/src/Verify.Tests/XmlTests.cs#L46-L53' title='Snippet source file'>snippet source</a> | <a href='#snippet-XmlIgnoreMember' title='Start of snippet'>anchor</a></sup>
7070
<!-- endSnippet -->
7171

7272
Will produce
@@ -92,7 +92,7 @@ public Task XmlScrubMember() =>
9292
VerifyXml(xml)
9393
.ScrubMember("node");
9494
```
95-
<sup><a href='/src/Verify.Tests/XmlTests.cs#L39-L46' title='Snippet source file'>snippet source</a> | <a href='#snippet-XmlScrubMember' title='Start of snippet'>anchor</a></sup>
95+
<sup><a href='/src/Verify.Tests/XmlTests.cs#L55-L62' title='Snippet source file'>snippet source</a> | <a href='#snippet-XmlScrubMember' title='Start of snippet'>anchor</a></sup>
9696
<!-- endSnippet -->
9797

9898
Will produce
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+


src/Verify.Tests/XmlTests.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,22 @@ public Task NoDeclaration() =>
2727
</body>
2828
""");
2929

30+
[Fact]
31+
public Task CData() =>
32+
VerifyXml(
33+
"""
34+
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
35+
<person><![CDATA[name is John Doe]]></person>
36+
""");
37+
38+
[Fact]
39+
public Task CDataMix() =>
40+
VerifyXml(
41+
"""
42+
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
43+
<person><![CDATA[name is John Doe]]>value</person>
44+
""");
45+
3046
#region XmlIgnoreMember
3147

3248
[Fact]

src/Verify/Verifier/InnerVerifier_Xml.cs

Lines changed: 42 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -63,52 +63,31 @@ Task<VerifyResult> VerifyXml(XContainer? target)
6363
}
6464

6565
var serialization = settings.serialization;
66-
var nodes = target
66+
var elements = target
6767
.Descendants()
6868
.ToList();
69-
foreach (var node in nodes)
69+
foreach (var element in elements)
7070
{
71-
if (serialization.TryGetScrubOrIgnoreByName(node.Name.LocalName, out var scrubOrIgnore))
71+
if (serialization.TryGetScrubOrIgnoreByName(element.Name.LocalName, out var scrubOrIgnore))
7272
{
7373
if (scrubOrIgnore == ScrubOrIgnore.Ignore)
7474
{
75-
node.Remove();
75+
element.Remove();
7676
}
7777
else
7878
{
79-
node.Value = "Scrubbed";
79+
element.Value = "Scrubbed";
8080
}
8181

8282
continue;
8383
}
8484

85-
foreach (var attribute in node
86-
.Attributes()
87-
.ToList())
88-
{
89-
if (serialization.TryGetScrubOrIgnoreByName(attribute.Name.LocalName, out scrubOrIgnore))
90-
{
91-
if (scrubOrIgnore == ScrubOrIgnore.Ignore)
92-
{
93-
attribute.Remove();
94-
}
95-
else
96-
{
97-
attribute.Value = "Scrubbed";
98-
}
99-
100-
continue;
101-
}
102-
103-
attribute.Value = ConvertValue(serialization, attribute.Value);
104-
}
105-
106-
if (node.IsEmpty || node.HasElements)
107-
{
108-
continue;
109-
}
85+
ScrubAttributes(element, serialization);
86+
}
11087

111-
node.Value = ConvertValue(serialization, node.Value);
88+
foreach (var node in target.DescendantNodes())
89+
{
90+
Debug.WriteLine(node);
11291
}
11392

11493
return VerifyString(target.ToString(), "xml");
@@ -124,4 +103,36 @@ string ConvertValue(SerializationSettings serialization, string value)
124103

125104
return ApplyScrubbers.ApplyForPropertyValue(span, settings, counter).ToString();
126105
}
106+
107+
void ScrubAttributes(XElement node, SerializationSettings serialization)
108+
{
109+
foreach (var attribute in node
110+
.Attributes()
111+
.ToList())
112+
{
113+
if (serialization.TryGetScrubOrIgnoreByName(attribute.Name.LocalName, out var scrubOrIgnore))
114+
{
115+
if (scrubOrIgnore == ScrubOrIgnore.Ignore)
116+
{
117+
attribute.Remove();
118+
}
119+
else
120+
{
121+
attribute.Value = "Scrubbed";
122+
}
123+
124+
continue;
125+
}
126+
127+
var span = attribute.Value.AsSpan();
128+
if (serialization.TryConvertString(counter, span, out var result))
129+
{
130+
attribute.Value = result;
131+
}
132+
else
133+
{
134+
attribute.Value = ApplyScrubbers.ApplyForPropertyValue(span, settings, counter).ToString();
135+
}
136+
}
137+
}
127138
}

0 commit comments

Comments
 (0)