Skip to content

Commit 35ca968

Browse files
committed
Refactored 2nd time. Better diff output.
1 parent 0940e86 commit 35ca968

36 files changed

+830
-470
lines changed

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,26 @@
11
Steps in finding differences:
22

3-
- NodeSelector/NodeFilter - filters away nodes that should not be part of the comparison
3+
```
4+
var diffs : []
5+
var currentPath : string
6+
var controlNodes : []
7+
var testNodes : []
8+
foreach (ctrlNode, index) in controlNodes
9+
if(shouldSkip(ctrlNode)) continue;
10+
11+
ctrlSource = {ctrlNode, index, currentPath}
12+
testNode = nodeMatcher(ctrlSource)
13+
14+
15+
testSource =
16+
17+
18+
nodeMatcher() ->
19+
```
20+
21+
22+
23+
- NodeFilter - filters away nodes that should not be part of the comparison
424
- Inline filter(ignorer)
525
- NodeMatcher - matches a control-node with a test-node for comparison
626
- Compare nodes by:

src/Comparison.cs

Lines changed: 0 additions & 27 deletions
This file was deleted.

src/ComparisonSource.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using System;
2+
3+
namespace Egil.AngleSharp.Diffing.Comparisons
4+
{
5+
public readonly struct AttributeComparison : IEquatable<AttributeComparison>, IAttributeComparison
6+
{
7+
public IAttributeComparisonSource Control { get; }
8+
public IAttributeComparisonSource Test { get; }
9+
10+
public AttributeComparison(IAttributeComparisonSource control, IAttributeComparisonSource test)
11+
{
12+
Control = control;
13+
Test = test;
14+
}
15+
16+
#region Equals and HashCode
17+
public bool Equals(AttributeComparison other) => Control == other.Control && Test == other.Test;
18+
public override bool Equals(object obj) => obj is AttributeComparison other && Equals(other);
19+
public override int GetHashCode() => (Control, Test).GetHashCode();
20+
public static bool operator ==(AttributeComparison left, AttributeComparison right) => left.Equals(right);
21+
public static bool operator !=(AttributeComparison left, AttributeComparison right) => !(left == right);
22+
#endregion
23+
}
24+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using AngleSharp.Dom;
3+
4+
namespace Egil.AngleSharp.Diffing.Comparisons
5+
{
6+
public readonly struct AttributeComparisonSource : IEquatable<AttributeComparisonSource>, IAttributeComparisonSource
7+
{
8+
public IAttr Attribute { get; }
9+
10+
public IComparisonSource<IElement> ElementSource { get; }
11+
12+
public AttributeComparisonSource(IAttr attribute, in IComparisonSource<IElement> elementSource)
13+
{
14+
Attribute = attribute;
15+
ElementSource = elementSource;
16+
}
17+
18+
#region Equals and HashCode
19+
public bool Equals(AttributeComparisonSource other) => Attribute == other.Attribute && ElementSource == other.ElementSource;
20+
public override int GetHashCode() => (Attribute, ElementSource).GetHashCode();
21+
public override bool Equals(object obj) => obj is AttributeComparisonSource other && Equals(other);
22+
public static bool operator ==(AttributeComparisonSource left, AttributeComparisonSource right) => left.Equals(right);
23+
public static bool operator !=(AttributeComparisonSource left, AttributeComparisonSource right) => !(left == right);
24+
#endregion
25+
}
26+
}

src/Comparisons/Comparison.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using AngleSharp.Dom;
3+
using Egil.AngleSharp.Diffing.Comparisons;
4+
5+
namespace Egil.AngleSharp.Diffing
6+
{
7+
/// <summary>
8+
/// Represent a comparison between two nodes.
9+
/// Create using <see cref="ComparisonFactory.Create(in IComparisonSource{INode}, in IComparisonSource{INode})"/>.
10+
/// </summary>
11+
/// <typeparam name="TNode"></typeparam>
12+
public readonly struct Comparison<TNode> : IEquatable<Comparison<TNode>>, IComparison<TNode>
13+
where TNode : INode
14+
{
15+
public IComparisonSource<TNode> Control { get; }
16+
public IComparisonSource<TNode> Test { get; }
17+
18+
internal Comparison(in IComparisonSource<TNode> control, in IComparisonSource<TNode> test)
19+
{
20+
Control = control;
21+
Test = test;
22+
}
23+
24+
#region Equals and HashCode
25+
public bool Equals(Comparison<TNode> other) => Control == other.Control && Test == other.Test;
26+
public override bool Equals(object obj) => obj is Comparison<TNode> other && Equals(other);
27+
public override int GetHashCode() => (Control, Test).GetHashCode();
28+
public static bool operator ==(Comparison<TNode> left, Comparison<TNode> right) => left.Equals(right);
29+
public static bool operator !=(Comparison<TNode> left, Comparison<TNode> right) => !(left == right);
30+
#endregion
31+
}
32+
}

src/Comparisons/ComparisonFactory.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
using System;
2+
using AngleSharp.Dom;
3+
using Egil.AngleSharp.Diffing.Comparisons;
4+
5+
namespace Egil.AngleSharp.Diffing
6+
{
7+
public static class ComparisonFactory
8+
{
9+
public static IComparison<INode> Create(in IComparisonSource<INode> control, in IComparisonSource<INode> test)
10+
{
11+
if (control is null) throw new ArgumentNullException(nameof(control));
12+
if (test is null) throw new ArgumentNullException(nameof(test));
13+
14+
return control switch
15+
{
16+
IComparisonSource<IElement> elmCtrl when (test is IComparisonSource<IElement> elmTest)
17+
=> new Comparison<IElement>(in elmCtrl, in elmTest),
18+
IComparisonSource<IComment> cmtCtrl when (test is IComparisonSource<IComment> cmtTest)
19+
=> new Comparison<IComment>(in cmtCtrl, in cmtTest),
20+
IComparisonSource<IText> textCtrl when (test is IComparisonSource<IText> textTest)
21+
=> new Comparison<IText>(in textCtrl, in textTest),
22+
_ => new Comparison<INode>(in control, in test)
23+
};
24+
}
25+
}
26+
}

src/Comparisons/ComparisonSource.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
using System;
2+
using AngleSharp.Dom;
3+
4+
namespace Egil.AngleSharp.Diffing.Comparisons
5+
{
6+
public readonly struct ComparisonSource<TNode> : IEquatable<ComparisonSource<TNode>>, IComparisonSource<TNode> where TNode : INode
7+
{
8+
public TNode Node { get; }
9+
10+
public int Index { get; }
11+
12+
public string Path { get; }
13+
14+
public ComparisonSourceType SourceType { get; }
15+
16+
public ComparisonSource(TNode node, int index, string path, ComparisonSourceType sourceType)
17+
{
18+
Node = node;
19+
Index = index;
20+
Path = path;
21+
SourceType = sourceType;
22+
}
23+
24+
#region Equals and HashCode
25+
public bool Equals(ComparisonSource<TNode> other) => Node.Equals(other.Node) && Index == other.Index && Path.Equals(other.Path, StringComparison.Ordinal) && SourceType == other.SourceType;
26+
public override int GetHashCode() => (Node, Index, Path, SourceType).GetHashCode();
27+
public override bool Equals(object obj) => obj is ComparisonSource<TNode> other && Equals(other);
28+
public static bool operator ==(ComparisonSource<TNode> left, ComparisonSource<TNode> right) => left.Equals(right);
29+
public static bool operator !=(ComparisonSource<TNode> left, ComparisonSource<TNode> right) => !(left == right);
30+
#endregion
31+
}
32+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
namespace Egil.AngleSharp.Diffing.Comparisons
2+
{
3+
public interface IAttributeComparison
4+
{
5+
IAttributeComparisonSource Control { get; }
6+
IAttributeComparisonSource Test { get; }
7+
}
8+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using AngleSharp.Dom;
2+
3+
namespace Egil.AngleSharp.Diffing.Comparisons
4+
{
5+
public interface IAttributeComparisonSource
6+
{
7+
IAttr Attribute { get; }
8+
IComparisonSource<IElement> ElementSource { get; }
9+
}
10+
}

0 commit comments

Comments
 (0)