-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: element comparer includes toggle to enforce check of closing tag
- Loading branch information
Showing
8 changed files
with
415 additions
and
455 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
609 changes: 301 additions & 308 deletions
609
src/AngleSharp.Diffing.Tests/Strategies/DiffingStrategyPipelineTest.cs
Large diffs are not rendered by default.
Oops, something went wrong.
41 changes: 0 additions & 41 deletions
41
src/AngleSharp.Diffing.Tests/Strategies/ElementStrategies/ElementClosingComparerTest.cs
This file was deleted.
Oops, something went wrong.
53 changes: 53 additions & 0 deletions
53
src/AngleSharp.Diffing.Tests/Strategies/ElementStrategies/ElementComparerTest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
namespace AngleSharp.Diffing.Strategies.ElementStrategies; | ||
|
||
public class ElementComparerTest : DiffingTestBase | ||
{ | ||
public ElementComparerTest(DiffingTestFixture fixture) : base(fixture) | ||
{ | ||
} | ||
|
||
[Theory(DisplayName = "When control and test nodes have the same type and name and enforceTagClosing is false, the result is Same")] | ||
[InlineData("<p>", "<p />")] | ||
[InlineData("<br>", "<br/>")] | ||
[InlineData("<br>", "<br>")] | ||
[InlineData("<p>", "<p>")] | ||
public void Test001(string controlHtml, string testHtml) | ||
{ | ||
var comparison = ToComparison(controlHtml, testHtml); | ||
new ElementComparer(enforceTagClosing: false) | ||
.Compare(comparison, CompareResult.Unknown) | ||
.ShouldBe(CompareResult.Same); | ||
} | ||
|
||
[Theory(DisplayName = "When control and test nodes have the a different type and name, the result is Different")] | ||
[InlineData("<div>", "<p>", false)] | ||
[InlineData("<div>", "textnode", false)] | ||
[InlineData("<div>", "<!--comment-->", false)] | ||
[InlineData("<!--comment-->", "textnode", false)] | ||
[InlineData("<br>", "<br/>", true)] | ||
[InlineData("<input>", "<input/>", true)] | ||
[InlineData("<div>", "<p>", true)] | ||
[InlineData("<div>", "textnode", true)] | ||
[InlineData("<div>", "<!--comment-->", true)] | ||
[InlineData("<!--comment-->", "textnode", true)] | ||
public void Test002(string controlHtml, string testHtml, bool enforceTagClosing) | ||
{ | ||
var comparison = ToComparison(controlHtml, testHtml); | ||
new ElementComparer(enforceTagClosing) | ||
.Compare(comparison, CompareResult.Unknown) | ||
.ShouldBe(CompareResult.Different); | ||
} | ||
|
||
[Theory(DisplayName = "When unknown node is used in comparison, but node name is equal, the result is Same")] | ||
[InlineData("<svg><path></path></svg>", "<path/>")] | ||
public void HandleUnknownNodeDuringComparison(string controlHtml, string testHtml) | ||
{ | ||
var knownNode = ToNode(controlHtml).FirstChild.ToComparisonSource(0, ComparisonSourceType.Control); | ||
var unknownNode = ToNode(testHtml).ToComparisonSource(0, ComparisonSourceType.Test); | ||
var comparison = new Comparison(knownNode, unknownNode); | ||
|
||
new ElementComparer(enforceTagClosing: false) | ||
.Compare(comparison, CompareResult.Unknown) | ||
.ShouldBe(CompareResult.Same); | ||
} | ||
} |
47 changes: 0 additions & 47 deletions
47
src/AngleSharp.Diffing.Tests/Strategies/NodeStrategies/NodeComparerTest.cs
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 0 additions & 29 deletions
29
src/AngleSharp.Diffing/Strategies/ElementStrategies/ElementClosingComparer.cs
This file was deleted.
Oops, something went wrong.
58 changes: 45 additions & 13 deletions
58
src/AngleSharp.Diffing/Strategies/ElementStrategies/ElementComparer.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,55 @@ | ||
using AngleSharp.Diffing.Core; | ||
using AngleSharp.Dom; | ||
using AngleSharp.Html.Parser.Tokens; | ||
|
||
namespace AngleSharp.Diffing.Strategies.ElementStrategies | ||
namespace AngleSharp.Diffing.Strategies.ElementStrategies; | ||
|
||
/// <summary> | ||
/// Represents the element comparer strategy. | ||
/// </summary> | ||
public class ElementComparer | ||
{ | ||
/// <summary> | ||
/// Represents the element comparer strategy. | ||
/// Gets whether or not the closing style of | ||
/// an element is considered during comparison. | ||
/// </summary> | ||
public bool EnforceTagClosing { get; } | ||
|
||
/// <summary> | ||
/// Creates an instance of the <see cref="ElementComparer"/>. | ||
/// </summary> | ||
/// <param name="enforceTagClosing"> | ||
/// Whether or not the closing style of | ||
/// an element is considered during comparison. | ||
/// </param> | ||
public ElementComparer(bool enforceTagClosing) | ||
{ | ||
EnforceTagClosing = enforceTagClosing; | ||
} | ||
|
||
/// <summary> | ||
/// The element comparer strategy. | ||
/// </summary> | ||
public static class ElementComparer | ||
public CompareResult Compare(in Comparison comparison, CompareResult currentDecision) | ||
{ | ||
/// <summary> | ||
/// The element comparer strategy. | ||
/// </summary> | ||
public static CompareResult Compare(in Comparison comparison, CompareResult currentDecision) | ||
if (currentDecision.IsSameOrSkip()) | ||
return currentDecision; | ||
|
||
var result = comparison.Control.Node.NodeType == NodeType.Element && comparison.AreNodeTypesEqual | ||
? CompareResult.Same | ||
: CompareResult.Different; | ||
|
||
if (EnforceTagClosing && result == CompareResult.Same) | ||
{ | ||
if (currentDecision.IsSameOrSkip()) | ||
return currentDecision; | ||
return comparison.Control.Node.NodeType == NodeType.Element && comparison.AreNodeTypesEqual | ||
? CompareResult.Same | ||
if (comparison.Test.Node is not IElement testElement || testElement.SourceReference is not HtmlTagToken testTag) | ||
throw new InvalidOperationException("No source reference attached to test element, cannot determine element tag closing style."); | ||
|
||
if (comparison.Control.Node is not IElement controlElement || controlElement.SourceReference is not HtmlTagToken controlTag) | ||
throw new InvalidOperationException("No source reference attached to test element, cannot determine element tag closing style."); | ||
|
||
return testTag.IsSelfClosing == controlTag.IsSelfClosing | ||
? result | ||
: CompareResult.Different; | ||
} | ||
|
||
return result; | ||
} | ||
} |