-
-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathHtmlDifferenceEngine.cs
More file actions
196 lines (159 loc) · 6.76 KB
/
HtmlDifferenceEngine.cs
File metadata and controls
196 lines (159 loc) · 6.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
using AngleSharp.Diffing.Core.Diffs;
using AngleSharp.Diffing.Strategies.AttributeStrategies;
namespace AngleSharp.Diffing.Core;
/// <summary>
/// Represents the engine that drives the diffing/comparison of two DOM trees.
/// </summary>
public class HtmlDifferenceEngine
{
private readonly IDiffingStrategy _diffingStrategy;
private readonly SourceCollection _controlSources;
private readonly SourceCollection _testSources;
private DiffContext Context { get; }
/// <summary>
/// Creates a diffing engine which will perform a comparison of the control and test sources, using the provided strategies.
/// </summary>
public HtmlDifferenceEngine(IDiffingStrategy diffingStrategy, SourceCollection controlSources, SourceCollection testSources)
{
_diffingStrategy = diffingStrategy ?? throw new ArgumentNullException(nameof(diffingStrategy));
_controlSources = controlSources ?? throw new ArgumentNullException(nameof(controlSources));
_testSources = testSources ?? throw new ArgumentNullException(nameof(testSources));
Context = new DiffContext(controlSources, testSources);
}
/// <summary>
/// Executes the comparison and returns any differences found.
/// </summary>
public IEnumerable<IDiff> Compare()
{
var diffs = Compare(_controlSources, _testSources);
var unmatchedDiffs = Context.GetDiffsFromUnmatched();
return diffs.Concat(unmatchedDiffs);
}
private IEnumerable<IDiff> Compare(SourceCollection controlSources, SourceCollection testSources)
{
ApplyNodeFilter(controlSources);
ApplyNodeFilter(testSources);
var comparisons = MatchNodes(controlSources, testSources);
var diffs = CompareNodes(comparisons);
return diffs;
}
private void ApplyNodeFilter(SourceCollection sources) => sources.Remove(_diffingStrategy.Filter);
private IEnumerable<Comparison> MatchNodes(SourceCollection controls, SourceCollection tests)
{
foreach (var comparison in _diffingStrategy.Match(Context, controls, tests))
{
UpdateMatchedTracking(comparison);
yield return comparison;
}
UpdateUnmatchedTracking();
yield break;
void UpdateMatchedTracking(in Comparison comparison)
{
controls.MarkAsMatched(comparison.Control);
tests.MarkAsMatched(comparison.Test);
Context.MissingSources.Remove(comparison.Control);
Context.UnexpectedSources.Remove(comparison.Test);
}
void UpdateUnmatchedTracking()
{
Context.MissingSources.AddRange(controls.GetUnmatched());
Context.UnexpectedSources.AddRange(tests.GetUnmatched());
}
}
private IEnumerable<IDiff> CompareNodes(IEnumerable<Comparison> comparisons)
{
return comparisons.SelectMany(comparison => CompareNode(comparison));
}
private IEnumerable<IDiff> CompareNode(in Comparison comparison)
{
if (comparison.Control.Node is IElement)
{
return CompareElement(comparison);
}
var compareRes = _diffingStrategy.Compare(comparison);
if (compareRes.Decision.HasFlag(CompareDecision.Different))
{
IDiff diff = compareRes.Diff ?? new NodeDiff(comparison);
return [diff];
}
return Array.Empty<IDiff>();
}
private List<IDiff> CompareElement(in Comparison comparison)
{
var result = new List<IDiff>();
var compareRes = _diffingStrategy.Compare(comparison);
if (compareRes.Decision.HasFlag(CompareDecision.Different))
{
result.Add(compareRes.Diff ?? new ElementDiff(comparison, ElementDiffKind.Unspecified));
}
if (!compareRes.Decision.HasFlag(CompareDecision.Skip))
{
if (!compareRes.Decision.HasFlag(CompareDecision.SkipAttributes))
result.AddRange(CompareElementAttributes(comparison));
if (!compareRes.Decision.HasFlag(CompareDecision.SkipChildren))
result.AddRange(CompareChildNodes(comparison));
}
return result;
}
private IEnumerable<IDiff> CompareElementAttributes(in Comparison comparison)
{
if (!comparison.Control.Node.HasAttributes() && !comparison.Test.Node.HasAttributes())
return Array.Empty<IDiff>();
var controlAttrs = new SourceMap(comparison.Control);
var testAttrs = new SourceMap(comparison.Test);
ApplyFilterAttributes(controlAttrs);
ApplyFilterAttributes(testAttrs);
var attrComparisons = MatchAttributes(controlAttrs, testAttrs);
return CompareAttributes(attrComparisons);
}
private void ApplyFilterAttributes(SourceMap controlAttrs)
{
controlAttrs.Remove(_diffingStrategy.Filter);
}
private IEnumerable<AttributeComparison> MatchAttributes(SourceMap controls, SourceMap tests)
{
foreach (var comparison in _diffingStrategy.Match(Context, controls, tests))
{
MarkSelectedSourcesAsMatched(comparison);
yield return comparison;
}
UpdateUnmatchedTracking();
yield break;
void MarkSelectedSourcesAsMatched(in AttributeComparison comparison)
{
controls.MarkAsMatched(comparison.Control);
tests.MarkAsMatched(comparison.Test);
Context.MissingAttributeSources.Remove(comparison.Control);
Context.UnexpectedAttributeSources.Remove(comparison.Test);
}
void UpdateUnmatchedTracking()
{
Context.MissingAttributeSources.AddRange(controls.GetUnmatched());
Context.UnexpectedAttributeSources.AddRange(tests.GetUnmatched());
}
}
private IEnumerable<IDiff> CompareChildNodes(in Comparison comparison)
{
if (!comparison.Control.Node.HasChildNodes && !comparison.Test.Node.HasChildNodes)
return Array.Empty<IDiff>();
var ctrlChildNodes = comparison.Control.Node.ChildNodes;
var testChildNodes = comparison.Test.Node.ChildNodes;
var ctrlPath = comparison.Control.Path;
var testPath = comparison.Test.Path;
return Compare(
ctrlChildNodes.ToSourceCollection(ComparisonSourceType.Control, ctrlPath),
testChildNodes.ToSourceCollection(ComparisonSourceType.Test, testPath)
);
}
private IEnumerable<IDiff> CompareAttributes(IEnumerable<AttributeComparison> comparisons)
{
foreach (var comparison in comparisons)
{
var compareRes = _diffingStrategy.Compare(comparison);
if (compareRes.Decision.HasFlag(CompareDecision.Different))
{
yield return compareRes.Diff ?? new AttrDiff(comparison, AttrDiffKind.Unspecified);
}
}
}
}