11using System ;
22using System . Collections . Generic ;
3+ using AngleSharp ;
4+ using AngleSharp . Dom ;
5+ using AngleSharp . Html . Parser ;
36using Egil . AngleSharp . Diffing . Core ;
7+ using Egil . AngleSharp . Diffing . Strategies ;
48
59namespace Egil . AngleSharp . Diffing
610{
711 public class DiffBuilder
812 {
13+ private readonly IBrowsingContext _context ;
14+ private readonly IHtmlParser _htmlParser ;
15+ private readonly IDocument _document ;
16+ private readonly DiffingStrategyPipeline _strategyPipeline ;
17+
918 private string _control = string . Empty ;
1019 private string _test = string . Empty ;
1120
@@ -16,6 +25,11 @@ public class DiffBuilder
1625 private DiffBuilder ( string control )
1726 {
1827 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 ( ) ;
1933 }
2034
2135 public DiffBuilder WithTest ( string test )
@@ -28,26 +42,99 @@ public static DiffBuilder Compare(string control)
2842 {
2943 return new DiffBuilder ( control ) ;
3044 }
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 )
3393 {
94+ _strategyPipeline . AddMatcher ( matchStrategy , isSpecializedMatcher ) ;
3495 return this ;
3596 }
3697
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 )
38106 {
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 ) ;
40121 return this ;
41122 }
42123
43124 public IList < IDiff > Build ( )
44125 {
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 ) ;
51138 }
52139 }
53140}
0 commit comments