1
1
using System ;
2
2
using System . Collections . Generic ;
3
+ using AngleSharp ;
4
+ using AngleSharp . Dom ;
5
+ using AngleSharp . Html . Parser ;
3
6
using Egil . AngleSharp . Diffing . Core ;
7
+ using Egil . AngleSharp . Diffing . Strategies ;
4
8
5
9
namespace Egil . AngleSharp . Diffing
6
10
{
7
11
public class DiffBuilder
8
12
{
13
+ private readonly IBrowsingContext _context ;
14
+ private readonly IHtmlParser _htmlParser ;
15
+ private readonly IDocument _document ;
16
+ private readonly DiffingStrategyPipeline _strategyPipeline ;
17
+
9
18
private string _control = string . Empty ;
10
19
private string _test = string . Empty ;
11
20
@@ -16,6 +25,11 @@ public class DiffBuilder
16
25
private DiffBuilder ( string control )
17
26
{
18
27
Control = control ;
28
+ var config = Configuration . Default . WithCss ( ) ;
29
+ _context = BrowsingContext . New ( config ) ;
30
+ _htmlParser = _context . GetService < IHtmlParser > ( ) ;
31
+ _document = _context . OpenNewAsync ( ) . Result ;
32
+ _strategyPipeline = new DiffingStrategyPipeline ( ) ;
19
33
}
20
34
21
35
public DiffBuilder WithTest ( string test )
@@ -28,26 +42,99 @@ public static DiffBuilder Compare(string control)
28
42
{
29
43
return new DiffBuilder ( control ) ;
30
44
}
31
-
32
- public DiffBuilder WithFilter ( FilterStrategy < ComparisonSource > filterStrategy )
45
+
46
+ /// <summary>
47
+ /// Adds a node filter to the pipeline.
48
+ /// Specialized filters always execute after any generalized filters in the pipeline.
49
+ /// That enables them to correct for the generic filters decision.
50
+ /// </summary>
51
+ /// <param name="filterStrategy"></param>
52
+ /// <param name="isSpecializedFilter">true if <paramref name="filterStrategy"/> is a specialized filter, false if it is a generalized filter</param>
53
+ public DiffBuilder WithFilter ( FilterStrategy < ComparisonSource > filterStrategy , bool isSpecializedFilter = true )
54
+ {
55
+ _strategyPipeline . AddFilter ( filterStrategy , isSpecializedFilter ) ;
56
+ return this ;
57
+ }
58
+
59
+ /// <summary>
60
+ /// Adds an attribute filter to the pipeline.
61
+ /// Specialized filters always execute after any generalized filters in the pipeline.
62
+ /// That enables them to correct for the generic filters decision.
63
+ /// </summary>
64
+ /// <param name="filterStrategy"></param>
65
+ /// <param name="isSpecializedFilter">true if <paramref name="filterStrategy"/> is a specialized filter, false if it is a generalized filter</param>
66
+ public DiffBuilder WithFilter ( FilterStrategy < AttributeComparisonSource > filterStrategy , bool isSpecializedFilter = true )
67
+ {
68
+ _strategyPipeline . AddFilter ( filterStrategy , isSpecializedFilter ) ;
69
+ return this ;
70
+ }
71
+
72
+ /// <summary>
73
+ /// Adds a node matcher to the pipeline.
74
+ /// Specialized matchers always execute before any generalized matchers in the pipeline.
75
+ /// This enables the special matchers to handle special matching cases before the more simple generalized matchers process the rest.
76
+ /// </summary>
77
+ /// <param name="matchStrategy"></param>
78
+ /// <param name="isSpecializedMatcher">true if <paramref name="matchStrategy"/> is a specialized matcher, false if it is a generalized matcher</param>
79
+ public DiffBuilder WithMatcher ( MatchStrategy < SourceCollection , Comparison > matchStrategy , bool isSpecializedMatcher = true )
80
+ {
81
+ _strategyPipeline . AddMatcher ( matchStrategy , isSpecializedMatcher ) ;
82
+ return this ;
83
+ }
84
+
85
+ /// <summary>
86
+ /// Adds an attribute matcher to the pipeline.
87
+ /// Specialized matchers always execute before any generalized matchers in the pipeline.
88
+ /// This enables the special matchers to handle special matching cases before the more simple generalized matchers process the rest.
89
+ /// </summary>
90
+ /// <param name="matchStrategy"></param>
91
+ /// <param name="isSpecializedMatcher">true if <paramref name="matchStrategy"/> is a specialized matcher, false if it is a generalized matcher</param>
92
+ public DiffBuilder WithMatcher ( MatchStrategy < SourceMap , AttributeComparison > matchStrategy , bool isSpecializedMatcher = true )
33
93
{
94
+ _strategyPipeline . AddMatcher ( matchStrategy , isSpecializedMatcher ) ;
34
95
return this ;
35
96
}
36
97
37
- public DiffBuilder WithFilter ( FilterStrategy < AttributeComparisonSource > filterStrategy )
98
+ /// <summary>
99
+ /// Adds a node comparer to the pipeline.
100
+ /// Specialized comparers always execute after any generalized comparers in the pipeline.
101
+ /// That enables them to correct for the generic comparers decision.
102
+ /// </summary>
103
+ /// <param name="compareStrategy"></param>
104
+ /// <param name="isSpecializedComparer">true if <paramref name="compareStrategy"/> is a specialized comparer, false if it is a generalized comparer</param>
105
+ public DiffBuilder WithComparer ( CompareStrategy < Comparison > compareStrategy , bool isSpecializedComparer = true )
38
106
{
39
-
107
+ _strategyPipeline . AddComparer ( compareStrategy , isSpecializedComparer ) ;
108
+ return this ;
109
+ }
110
+
111
+ /// <summary>
112
+ /// Adds a attribute comparer to the pipeline.
113
+ /// Specialized comparers always execute after any generalized comparers in the pipeline.
114
+ /// That enables them to correct for the generic comparers decision.
115
+ /// </summary>
116
+ /// <param name="compareStrategy"></param>
117
+ /// <param name="isSpecializedComparer">true if <paramref name="compareStrategy"/> is a specialized comparer, false if it is a generalized comparer</param>
118
+ public DiffBuilder WithComparer ( CompareStrategy < AttributeComparison > compareStrategy , bool isSpecializedComparer = true )
119
+ {
120
+ _strategyPipeline . AddComparer ( compareStrategy , isSpecializedComparer ) ;
40
121
return this ;
41
122
}
42
123
43
124
public IList < IDiff > Build ( )
44
125
{
45
- //var context = BrowsingContext.New();
46
- //var htmlParser = context.GetService<IHtmlParser>();
47
- //var document = context.OpenNewAsync().Result;
48
- //var control = htmlParser.ParseFragment(Control, document.Body);
49
- //var test = htmlParser.ParseFragment(Test, document.Body);
50
- return Array . Empty < IDiff > ( ) ;
126
+ if ( ! _strategyPipeline . HasMatchers )
127
+ throw new InvalidOperationException ( "No comparer's has been added to the builder. Add at least one and try again." ) ;
128
+ if ( ! _strategyPipeline . HasComparers )
129
+ throw new InvalidOperationException ( "No matcher's has been added to the builder. Add at least one and try again." ) ;
130
+
131
+ return new HtmlDifferenceEngine ( _strategyPipeline , _strategyPipeline , _strategyPipeline )
132
+ . Compare ( Parse ( Control ) , Parse ( Test ) ) ;
133
+ }
134
+
135
+ private INodeList Parse ( string html )
136
+ {
137
+ return _htmlParser . ParseFragment ( html , _document . Body ) ;
51
138
}
52
139
}
53
140
}
0 commit comments