Skip to content

Commit e254b0c

Browse files
committed
Add :ignore filter approach
1 parent 5f19c08 commit e254b0c

File tree

4 files changed

+32
-8
lines changed

4 files changed

+32
-8
lines changed

src/AngleSharp.Diffing.Tests/DiffBuilderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public void Test008(string controlHtml, string testHtml, DiffResult expectedDiff
141141
.WithTest(testHtml)
142142
.WithOptions(a => a // Most important thing to note here is we do not have a ignore attribute comparer
143143
.AddSearchingNodeMatcher()
144-
.AddAttributeNameMatcher()
144+
.AddMatcher(AttributeNameMatcher.Match, StrategyType.Generalized)
145145
.AddElementComparer(enforceTagClosing: false)
146146
.AddMatcher(PostfixedAttributeMatcher.Match, StrategyType.Specialized)
147147
.AddComparer(AttributeComparer.Compare, StrategyType.Generalized)

src/AngleSharp.Diffing/Core/HtmlDifferenceEngine.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -161,13 +161,7 @@ void MarkSelectedSourcesAsMatched(in AttributeComparison comparison)
161161

162162
void UpdateUnmatchedTracking()
163163
{
164-
// Filter out unmatched :ignore attributes, they were meant to be ignored after all
165-
// https://github.com/AngleSharp/AngleSharp.Diffing/issues/48
166-
var controlsUnmatched = controls
167-
.GetUnmatched()
168-
.Where(c => !IgnoreAttributeComparer.IsIgnoreAttribute(c.Attribute));
169-
170-
Context.MissingAttributeSources.AddRange(controlsUnmatched);
164+
Context.MissingAttributeSources.AddRange(controls.GetUnmatched());
171165
Context.UnexpectedAttributeSources.AddRange(tests.GetUnmatched());
172166
}
173167
}

src/AngleSharp.Diffing/Strategies/AttributeStrategies/DiffingStrategyPipelineBuilderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public static IDiffingStrategyCollection IgnoreDiffAttributes(this IDiffingStrat
2323
public static IDiffingStrategyCollection AddAttributeNameMatcher(this IDiffingStrategyCollection builder)
2424
{
2525
builder.AddMatcher(AttributeNameMatcher.Match, StrategyType.Generalized);
26+
builder.AddMatcher(IgnoreAttributeMatcher.Match, StrategyType.Generalized);
2627
return builder;
2728
}
2829

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace AngleSharp.Diffing.Strategies.AttributeStrategies;
2+
3+
/// <summary>
4+
/// Ignore Attribute matcher strategy.
5+
/// </summary>
6+
public static class IgnoreAttributeMatcher
7+
{
8+
private const string DIFF_IGNORE_POSTFIX = ":ignore";
9+
10+
/// <summary>
11+
/// Attribute name matcher strategy.
12+
/// </summary>
13+
public static IEnumerable<AttributeComparison> Match(IDiffContext context, SourceMap controlSources, SourceMap testSources)
14+
{
15+
if (controlSources is null)
16+
throw new ArgumentNullException(nameof(controlSources));
17+
if (testSources is null)
18+
throw new ArgumentNullException(nameof(testSources));
19+
20+
foreach (var control in controlSources.GetUnmatched())
21+
{
22+
// An unmatched :ignore attribute can just be matched with itself if it isn't
23+
// matched with a "test" attribute of the same name already.
24+
// this means an ignored attribute is ignored even if it does not appear in the test html.
25+
if (control.Attribute.Name.EndsWith(DIFF_IGNORE_POSTFIX, StringComparison.OrdinalIgnoreCase))
26+
yield return new AttributeComparison(control, control);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)