Skip to content

Commit f3fd82c

Browse files
committed
Ignore unmatched :ignore attributes
1 parent 5694c9b commit f3fd82c

File tree

4 files changed

+28
-3
lines changed

4 files changed

+28
-3
lines changed

src/AngleSharp.Diffing.Tests/DiffBuilderTest.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,4 +116,17 @@ public void Test006(string control, string test)
116116

117117
diffs.ShouldBeEmpty();
118118
}
119+
120+
[Theory(DisplayName = "When a control element has 'class:ignore', elements with and without class should return empty diffs")]
121+
[InlineData("<div class=\"ian-fleming\"></div>")]
122+
[InlineData("<div class=\"\"></div>")]
123+
[InlineData("<div class></div>")]
124+
[InlineData("<div></div>")]
125+
public void Test007(string testHtml)
126+
{
127+
var controlHtml = "<div class:ignore></div>";
128+
var diffs = DiffBuilder.Compare(controlHtml).WithTest(testHtml).Build();
129+
Assert.Empty(diffs);
130+
}
131+
119132
}

src/AngleSharp.Diffing/Core/HtmlDifferenceEngine.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using AngleSharp.Diffing.Core.Diffs;
2+
using AngleSharp.Diffing.Strategies.AttributeStrategies;
23

34
namespace AngleSharp.Diffing.Core;
45

@@ -160,7 +161,13 @@ void MarkSelectedSourcesAsMatched(in AttributeComparison comparison)
160161

161162
void UpdateUnmatchedTracking()
162163
{
163-
Context.MissingAttributeSources.AddRange(controls.GetUnmatched());
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);
164171
Context.UnexpectedAttributeSources.AddRange(tests.GetUnmatched());
165172
}
166173
}

src/AngleSharp.Diffing/Core/SourceMap.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ public IEnumerable<AttributeComparisonSource> GetUnmatched()
7070
{
7171
foreach (var source in _sources.Values)
7272
{
73-
if (!_matched.Contains(source.Attribute.Name))
73+
if (IsUnmatched(source.Attribute.Name))
7474
yield return source;
7575
}
7676
}

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,13 @@ public static CompareResult Compare(in AttributeComparison comparison, CompareRe
1515
if (currentDecision.IsSameOrSkip)
1616
return currentDecision;
1717

18-
return comparison.Control.Attribute.Name.EndsWith(DIFF_IGNORE_POSTFIX, StringComparison.OrdinalIgnoreCase)
18+
return IsIgnoreAttribute(comparison.Control.Attribute)
1919
? CompareResult.Same
2020
: currentDecision;
2121
}
22+
23+
public static bool IsIgnoreAttribute(IAttr source)
24+
{
25+
return source.Name.EndsWith(DIFF_IGNORE_POSTFIX, StringComparison.OrdinalIgnoreCase);
26+
}
2227
}

0 commit comments

Comments
 (0)