Skip to content

Commit 2636d39

Browse files
committed
Whitespace filter done
1 parent c114bc1 commit 2636d39

24 files changed

+241
-410
lines changed

.vscode/launch.json

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

.vscode/tasks.json

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

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ Using the `UseDefaultOptions()` method is equivalent to setting the following op
3030
var diffs = DiffBuilder
3131
.Compare(controlHtml)
3232
.WithTest(testHtml)
33-
.RemoveComments()
33+
.IgnoreComments()
3434
.Whitespace(WhitespaceOption.Normalize)
3535
.IgnoreDiffAttributes()
3636
.Build();
@@ -45,18 +45,18 @@ The library comes with a bunch of options (internally referred to as strategies)
4545

4646
The following section document the current built-in strategies that are available. A later second will describe how to built your own strategies, to get very tight control of the diffing process.
4747

48-
### Remove comments
49-
Enabling this strategy will remove all comment nodes from the comparison. Activate by calling the `RemoveComments()` method on a `DiffBuilder` instance, e.g.:
48+
### Ignore comments
49+
Enabling this strategy will ignore all comment nodes during comparison. Activate by calling the `IgnoreComments()` method on a `DiffBuilder` instance, e.g.:
5050

5151
```csharp
5252
var diffs = DiffBuilder
5353
.Compare(controlHtml)
5454
.WithTest(testHtml)
55-
.RemoveComments()
55+
.IgnoreComments()
5656
.Build();
5757
```
5858

59-
_NOTE: Currently, the remove comment strategy does NOT remove comments from CSS or JavaScript embedded in `<style>` or `<script>` tags.__
59+
_**NOTE**: Currently, the ignore comment strategy does NOT remove comments from CSS or JavaScript embedded in `<style>` or `<script>` tags._
6060

6161
### Whitespace handling
6262
Whitespace can be a source of false-positives when comparing two HTML fragments. Thus, the whitespace handling strategy offer different ways to deal with it during a comparison.

src/Core/IFilterStrategy.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,13 @@ public interface IFilterStrategy
1111
/// </summary>
1212
/// <param name="comparisonSource">A comparison source for the node</param>
1313
/// <returns>true if the node should be part of the comparison, false if the node should be filtered out.</returns>
14-
bool Filter(in ComparisonSource comparisonSource);
14+
FilterDecision Filter(in ComparisonSource comparisonSource);
1515

1616
/// <summary>
1717
/// Decides whether an attribute should be part of the comparison.
1818
/// </summary>
1919
/// <param name="comparisonSource">A comparison source for the attribute</param>
2020
/// <returns>true if the attribute should be part of the comparison, false if the attribute should be filtered out.</returns>
21-
bool Filter(in AttributeComparisonSource attributeComparisonSource);
21+
FilterDecision Filter(in AttributeComparisonSource attributeComparisonSource);
2222
}
2323
}

src/Core/SourceCollection.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
namespace Egil.AngleSharp.Diffing.Core
77
{
8-
public delegate bool SourceCollectionRemovePredicate(in ComparisonSource source);
8+
public delegate FilterDecision SourceCollectionRemovePredicate(in ComparisonSource source);
99

1010
public class SourceCollection : IEnumerable<ComparisonSource>
1111
{
@@ -69,7 +69,7 @@ internal void Remove(SourceCollectionRemovePredicate predicate)
6969
for (int i = 0; i < _sources.Length; i++)
7070
{
7171
var source = _sources[i];
72-
if (!predicate(source))
72+
if (predicate(source) == FilterDecision.Exclude)
7373
{
7474
_status[source.Index] = SOURCE_REMOVED;
7575
Count--;

src/Core/SourceMap.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
namespace Egil.AngleSharp.Diffing.Core
88
{
9-
public delegate bool SourceMapRemovePredicate(in AttributeComparisonSource source);
9+
public delegate FilterDecision SourceMapRemovePredicate(in AttributeComparisonSource source);
1010

1111
[SuppressMessage("Naming", "CA1710:Identifiers should have correct suffix")]
1212
public class SourceMap : IEnumerable<AttributeComparisonSource>
@@ -63,7 +63,7 @@ internal void Remove(SourceMapRemovePredicate predicate)
6363
var removeQueue = new Queue<string>(Count);
6464
foreach (var source in _sources.Values)
6565
{
66-
if (!predicate(source)) removeQueue.Enqueue(source.Attribute.Name);
66+
if (predicate(source) == FilterDecision.Exclude) removeQueue.Enqueue(source.Attribute.Name);
6767
}
6868
foreach (var name in removeQueue)
6969
{

src/DiffingStrategyPipeline.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77

88
namespace Egil.AngleSharp.Diffing
99
{
10-
public delegate bool FilterStrategy<TSource>(in TSource source, bool currentDecision);
10+
11+
public delegate FilterDecision FilterStrategy<TSource>(in TSource source, FilterDecision currentDecision);
1112
public delegate IEnumerable<TComparison> MatchStrategy<in TSources, out TComparison>(DiffContext context, TSources controlSources, TSources testSources);
1213
public delegate CompareResult CompareStrategy<TComparison>(in TComparison comparison, CompareResult currentDecision);
1314

@@ -20,8 +21,8 @@ public class DiffingStrategyPipeline : IFilterStrategy, IMatcherStrategy, ICompa
2021
private readonly List<CompareStrategy<Comparison>> _nodeComparers = new List<CompareStrategy<Comparison>>();
2122
private readonly List<CompareStrategy<AttributeComparison>> _attrComparers = new List<CompareStrategy<AttributeComparison>>();
2223

23-
public bool Filter(in ComparisonSource comparisonSource) => Filter(comparisonSource, _nodeFilters);
24-
public bool Filter(in AttributeComparisonSource attributeComparisonSource) => Filter(attributeComparisonSource, _attrsFilters);
24+
public FilterDecision Filter(in ComparisonSource comparisonSource) => Filter(comparisonSource, _nodeFilters);
25+
public FilterDecision Filter(in AttributeComparisonSource attributeComparisonSource) => Filter(attributeComparisonSource, _attrsFilters);
2526

2627
public IEnumerable<Comparison> Match(DiffContext context, SourceCollection controlSources, SourceCollection testSources)
2728
=> Match(context, controlSources, testSources, _nodeMatchers);
@@ -42,9 +43,9 @@ public CompareResult Compare(in AttributeComparison comparison)
4243
public void AddComparer(CompareStrategy<Comparison> compareStrategy) => _nodeComparers.Add(compareStrategy);
4344
public void AddComparer(CompareStrategy<AttributeComparison> compareStrategy) => _attrComparers.Add(compareStrategy);
4445

45-
private bool Filter<T>(in T source, List<FilterStrategy<T>> filterStrategies)
46+
private FilterDecision Filter<T>(in T source, List<FilterStrategy<T>> filterStrategies)
4647
{
47-
var result = true;
48+
var result = FilterDecision.Keep;
4849
for (int i = 0; i < filterStrategies.Count; i++)
4950
{
5051
result = filterStrategies[i](source, result);

src/Egil.AngleSharp.Diffing.csproj

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,12 @@
44
<TargetFramework>netstandard2.0</TargetFramework>
55
</PropertyGroup>
66

7+
<ItemGroup>
8+
<Compile Remove="Matchers\**" />
9+
<EmbeddedResource Remove="Matchers\**" />
10+
<None Remove="Matchers\**" />
11+
</ItemGroup>
12+
713
<ItemGroup>
814
<PackageReference Include="AngleSharp" Version="$(AngleSharpVersion)" />
915
<PackageReference Include="Microsoft.CodeAnalysis.FxCopAnalyzers" Version="2.9.5">
@@ -12,8 +18,4 @@
1218
</PackageReference>
1319
</ItemGroup>
1420

15-
<ItemGroup>
16-
<Folder Include="Matchers\" />
17-
</ItemGroup>
18-
1921
</Project>

src/FilterDecision.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace Egil.AngleSharp.Diffing
2+
{
3+
public enum FilterDecision
4+
{
5+
Keep = 0,
6+
Exclude
7+
}
8+
9+
public static class FilterDecisionExtensions
10+
{
11+
public static bool IsExclude(this FilterDecision decision) => decision == FilterDecision.Exclude;
12+
public static bool IsKeep(this FilterDecision decision) => decision == FilterDecision.Keep;
13+
}
14+
}

src/Filters/RemoveCommentsNodeFilter.cs

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

0 commit comments

Comments
 (0)