Skip to content

Commit 8694b8f

Browse files
committed
Added HtmlDifferenceEngine.Compare(INode controlNode, INode testNode) to make it easier to compare single nodes to each other.
1 parent 6346069 commit 8694b8f

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/AngleSharp.Diffing/Core/HtmlDifferenceEngine.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,29 +16,37 @@ public HtmlDifferenceEngine(IDiffingStrategy diffingStrategy)
1616
_diffingStrategy = diffingStrategy ?? throw new ArgumentNullException(nameof(diffingStrategy));
1717
}
1818

19-
public IEnumerable<IDiff> Compare(INodeList controlNodes, INodeList testNodes)
19+
public IEnumerable<IDiff> Compare(INode controlNode, INode testNode)
20+
{
21+
if (controlNode is null) throw new ArgumentNullException(nameof(controlNode));
22+
if (testNode is null) throw new ArgumentNullException(nameof(testNode));
23+
24+
return Compare(new[] { controlNode }, new[] { testNode });
25+
}
26+
27+
public IEnumerable<IDiff> Compare(IEnumerable<INode> controlNodes, IEnumerable<INode> testNodes)
2028
{
2129
if (controlNodes is null) throw new ArgumentNullException(nameof(controlNodes));
2230
if (testNodes is null) throw new ArgumentNullException(nameof(testNodes));
2331

2432
var controlSources = controlNodes.ToSourceCollection(ComparisonSourceType.Control);
2533
var testSources = testNodes.ToSourceCollection(ComparisonSourceType.Test);
2634

27-
var context = CreateDiffContext(controlNodes, testNodes);
35+
var context = CreateDiffContext(controlSources, testSources);
2836

2937
var diffs = CompareNodeLists(context, controlSources, testSources);
3038
var unmatchedDiffs = context.GetDiffsFromUnmatched();
3139

3240
return diffs.Concat(unmatchedDiffs);
3341
}
3442

35-
private static DiffContext CreateDiffContext(INodeList controlNodes, INodeList testNodes)
43+
private static DiffContext CreateDiffContext(SourceCollection controlNodes, SourceCollection testNodes)
3644
{
3745
IElement? controlRoot = null;
3846
IElement? testRoot = null;
3947

40-
if (controlNodes.Length > 0 && controlNodes[0].GetRoot() is IElement r1) { controlRoot = r1; }
41-
if (testNodes.Length > 0 && testNodes[0].GetRoot() is IElement r2) { testRoot = r2; }
48+
if (controlNodes.Count > 0 && controlNodes.First().Node.GetRoot() is IElement r1) { controlRoot = r1; }
49+
if (testNodes.Count > 0 && testNodes.First().Node.GetRoot() is IElement r2) { testRoot = r2; }
4250

4351
return new DiffContext(controlRoot, testRoot);
4452
}

src/AngleSharp.Diffing/Extensions/NodeListExtensions.cs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using AngleSharp.Dom;
44
using AngleSharp.Diffing.Core;
@@ -7,18 +7,20 @@ namespace AngleSharp.Diffing
77
{
88
public static class AngleSharpDomExtensions
99
{
10-
public static SourceCollection ToSourceCollection(this INodeList nodelist, ComparisonSourceType sourceType, string path = "")
10+
public static SourceCollection ToSourceCollection(this IEnumerable<INode> nodelist, ComparisonSourceType sourceType, string path = "")
1111
{
1212
return new SourceCollection(sourceType, nodelist.ToComparisonSourceList(sourceType, path));
1313
}
1414

15-
public static IEnumerable<ComparisonSource> ToComparisonSourceList(this INodeList nodes, ComparisonSourceType sourceType, string path = "")
15+
public static IEnumerable<ComparisonSource> ToComparisonSourceList(this IEnumerable<INode> nodes, ComparisonSourceType sourceType, string path = "")
1616
{
1717
if (nodes is null) throw new ArgumentNullException(nameof(nodes));
1818

19-
for (int index = 0; index < nodes.Length; index++)
19+
var index = 0;
20+
foreach (var node in nodes)
2021
{
21-
yield return nodes[index].ToComparisonSource(index, sourceType, path);
22+
yield return node.ToComparisonSource(index, sourceType, path);
23+
index += 1;
2224
}
2325
yield break;
2426
}

0 commit comments

Comments
 (0)