Skip to content

Commit 709f09e

Browse files
committed
Diff Engine done.
1 parent 35ca968 commit 709f09e

26 files changed

+757
-303
lines changed

src/CompareResult.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Egil.AngleSharp.Diffing
2+
{
3+
public enum CompareResult
4+
{
5+
Same,
6+
Different
7+
}
8+
}

src/Comparisons/AttributeComparisonSource.cs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using AngleSharp.Dom;
3+
using System.Diagnostics.CodeAnalysis;
34

45
namespace Egil.AngleSharp.Diffing.Comparisons
56
{
@@ -9,14 +10,24 @@ namespace Egil.AngleSharp.Diffing.Comparisons
910

1011
public IComparisonSource<IElement> ElementSource { get; }
1112

13+
public string Path { get; }
14+
15+
public ComparisonSourceType SourceType { get; }
16+
17+
[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Path should be in lower case")]
1218
public AttributeComparisonSource(IAttr attribute, in IComparisonSource<IElement> elementSource)
1319
{
20+
if (attribute is null) throw new ArgumentNullException(nameof(attribute));
21+
if (elementSource is null) throw new ArgumentNullException(nameof(elementSource));
22+
1423
Attribute = attribute;
1524
ElementSource = elementSource;
25+
SourceType = elementSource.SourceType;
26+
Path = $"{elementSource.Path}[{attribute.Name.ToLowerInvariant()}]";
1627
}
1728

1829
#region Equals and HashCode
19-
public bool Equals(AttributeComparisonSource other) => Attribute == other.Attribute && ElementSource == other.ElementSource;
30+
public bool Equals(AttributeComparisonSource other) => Attribute == other.Attribute && ElementSource == other.ElementSource && Path.Equals(other.Path, StringComparison.Ordinal);
2031
public override int GetHashCode() => (Attribute, ElementSource).GetHashCode();
2132
public override bool Equals(object obj) => obj is AttributeComparisonSource other && Equals(other);
2233
public static bool operator ==(AttributeComparisonSource left, AttributeComparisonSource right) => left.Equals(right);

src/Comparisons/Comparison.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
using System;
22
using AngleSharp.Dom;
3-
using Egil.AngleSharp.Diffing.Comparisons;
43

5-
namespace Egil.AngleSharp.Diffing
4+
namespace Egil.AngleSharp.Diffing.Comparisons
65
{
76
/// <summary>
87
/// Represent a comparison between two nodes.

src/Comparisons/ComparisonFactory.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
using System;
2-
using AngleSharp.Dom;
1+
using AngleSharp.Dom;
32
using Egil.AngleSharp.Diffing.Comparisons;
43

54
namespace Egil.AngleSharp.Diffing
@@ -8,8 +7,8 @@ public static class ComparisonFactory
87
{
98
public static IComparison<INode> Create(in IComparisonSource<INode> control, in IComparisonSource<INode> test)
109
{
11-
if (control is null) throw new ArgumentNullException(nameof(control));
12-
if (test is null) throw new ArgumentNullException(nameof(test));
10+
if (control is null) throw new System.ArgumentNullException(nameof(control));
11+
if (test is null) throw new System.ArgumentNullException(nameof(test));
1312

1413
return control switch
1514
{

src/Comparisons/ComparisonSource.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using AngleSharp.Dom;
3+
using System.Diagnostics.CodeAnalysis;
34

45
namespace Egil.AngleSharp.Diffing.Comparisons
56
{
@@ -13,11 +14,15 @@ namespace Egil.AngleSharp.Diffing.Comparisons
1314

1415
public ComparisonSourceType SourceType { get; }
1516

17+
[SuppressMessage("Globalization", "CA1308:Normalize strings to uppercase", Justification = "Path should be in lower case")]
1618
public ComparisonSource(TNode node, int index, string path, ComparisonSourceType sourceType)
1719
{
1820
Node = node;
1921
Index = index;
20-
Path = path;
22+
Path = string.IsNullOrEmpty(path)
23+
? $"{Node.NodeName.ToLowerInvariant()}({Index})"
24+
: $"{path} > {Node.NodeName.ToLowerInvariant()}({Index})";
25+
2126
SourceType = sourceType;
2227
}
2328

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,19 @@
11
namespace Egil.AngleSharp.Diffing.Comparisons
22
{
3+
/// <summary>
4+
/// Represents a comparison between two attributes,
5+
/// that have been matched up for comparison.
6+
/// </summary>
37
public interface IAttributeComparison
48
{
9+
/// <summary>
10+
/// Gets the control <see cref="IAttributeComparisonSource"/>, that represents the expected attribute.
11+
/// </summary>
512
IAttributeComparisonSource Control { get; }
13+
14+
/// <summary>
15+
/// Gets the test <see cref="IAttributeComparisonSource"/>, which should be compared against <see cref="Control"/>.
16+
/// </summary>
617
IAttributeComparisonSource Test { get; }
718
}
819
}

src/Comparisons/IAttributeComparisonSource.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,29 @@
22

33
namespace Egil.AngleSharp.Diffing.Comparisons
44
{
5+
/// <summary>
6+
/// A input source for an attribute comparison.
7+
/// </summary>
58
public interface IAttributeComparisonSource
69
{
10+
/// <summary>
11+
/// Gets the attribute which should take part in a comparison.
12+
/// </summary>
713
IAttr Attribute { get; }
14+
15+
/// <summary>
16+
/// Gets the source of the element which the attribute belongs to.
17+
/// </summary>
818
IComparisonSource<IElement> ElementSource { get; }
19+
20+
/// <summary>
21+
/// Gets an CSS-selector like representation of the path to the attribute in the DOM-tree.
22+
/// </summary>
23+
string Path { get; }
24+
25+
/// <summary>
26+
/// Gets the source type, i.e. if this is a <see cref="ComparisonSourceType.Control"/> or <see cref="ComparisonSourceType.Test"/> source.
27+
/// </summary>
28+
ComparisonSourceType SourceType { get; }
929
}
1030
}

src/Comparisons/IComparison.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,20 @@
66

77
namespace Egil.AngleSharp.Diffing.Comparisons
88
{
9+
/// <summary>
10+
/// Represents a single comparison between a control node and a test node.
11+
/// </summary>
12+
/// <typeparam name="TNode"></typeparam>
913
public interface IComparison<out TNode> where TNode : INode
1014
{
15+
/// <summary>
16+
/// Gets the Control node source, which should be used as the baseline to compare the <see cref="Test"/> node source with.
17+
/// </summary>
1118
IComparisonSource<TNode> Control { get; }
1219

20+
/// <summary>
21+
/// Gets the Test node source, which should be compare to the <see cref="Control"/> node source.
22+
/// </summary>
1323
IComparisonSource<TNode> Test { get; }
1424
}
1525
}

src/Comparisons/IComparisonSource.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,30 @@
22

33
namespace Egil.AngleSharp.Diffing.Comparisons
44
{
5+
/// <summary>
6+
/// A input source for an node comparison.
7+
/// </summary>
8+
/// <typeparam name="TNode">The type of the node</typeparam>
59
public interface IComparisonSource<out TNode> where TNode : INode
610
{
11+
/// <summary>
12+
/// Gets the node which should take part in a comparison.
13+
/// </summary>
714
TNode Node { get; }
15+
16+
/// <summary>
17+
/// Gets the index of the node in the DOM-tree, relative to its siblings.
18+
/// </summary>
819
int Index { get; }
20+
21+
/// <summary>
22+
/// Gets an CSS-selector like representation of the path to the node in the DOM-tree.
23+
/// </summary>
924
string Path { get; }
25+
26+
/// <summary>
27+
/// Gets the source type, i.e. if this is a <see cref="ComparisonSourceType.Control"/> or <see cref="ComparisonSourceType.Test"/> source.
28+
/// </summary>
1029
ComparisonSourceType SourceType { get; }
1130
}
1231
}

src/Diffs/AttrDiff.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
using System;
2+
using System.Diagnostics;
3+
using Egil.AngleSharp.Diffing.Comparisons;
4+
5+
namespace Egil.AngleSharp.Diffing
6+
{
7+
[DebuggerDisplay("Diff={Target} {Result} Control={Control.Node.NodeName}[{Control.Index}] Test={Test.Node.NodeName}[{Test.Index}]")]
8+
public readonly struct AttrDiff : IDiff, IEquatable<AttrDiff>
9+
{
10+
public IAttributeComparisonSource Control { get; }
11+
12+
public IAttributeComparisonSource Test { get; }
13+
14+
public DiffResult Result { get; }
15+
16+
public DiffTarget Target { get; }
17+
18+
internal AttrDiff(in IAttributeComparison comparison)
19+
{
20+
if (comparison is null) throw new ArgumentNullException(nameof(comparison));
21+
Control = comparison.Control;
22+
Test = comparison.Test;
23+
Result = DiffResult.Different;
24+
Target = DiffTarget.Attribute;
25+
}
26+
27+
public bool Equals(AttrDiff other) => Control.Equals(other.Control) && Test.Equals(other.Test) && Result == other.Result && Target == other.Target;
28+
public override bool Equals(object obj) => obj is AttrDiff other && Equals(other);
29+
public override int GetHashCode() => (Control, Test, Result, Target).GetHashCode();
30+
public static bool operator ==(AttrDiff left, AttrDiff right) => left.Equals(right);
31+
public static bool operator !=(AttrDiff left, AttrDiff right) => !(left == right);
32+
}
33+
}

0 commit comments

Comments
 (0)