Skip to content

Commit 0ace0e3

Browse files
committed
fix: comparison of node names are case insensitive
when the parser in AngleSharp does not know the element type, it will store the name of the node/element in the NodeName property in upper case. This can lead to false negative results. E.g. if one parses "<svg><path></path></svg>" and selects the `<path />` and want to compare that to a "<path>" element parsed alone, the second will be treated as an unknown element, since it was not parsed in the context of a "<svg>" element. The two elements will have the same name though, just in different cases and should thus be equal.
1 parent 3cbd08f commit 0ace0e3

File tree

2 files changed

+17
-2
lines changed

2 files changed

+17
-2
lines changed

src/AngleSharp.Diffing.Tests/Strategies/NodeStrategies/NodeComparerTest.cs

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
using AngleSharp.Diffing.Core;
1+
using System;
2+
3+
using AngleSharp.Diffing.Core;
24
using AngleSharp.Diffing.Strategies.ElementStrategies;
35

46
using Shouldly;
@@ -30,5 +32,16 @@ public void Test002(string controlHtml, string testHtml)
3032
var comparison = ToComparison(controlHtml, testHtml);
3133
ElementComparer.Compare(comparison, CompareResult.Unknown).ShouldBe(CompareResult.Different);
3234
}
35+
36+
[Theory(DisplayName = "When unknown node is used in comparison, but node name is equal, the result is Same")]
37+
[InlineData("<svg><path></path></svg>", "<path/>")]
38+
public void HandleUnknownNodeDuringComparison(string controlHtml, string testHtml)
39+
{
40+
var knownNode = ToNode(controlHtml).FirstChild.ToComparisonSource(0, ComparisonSourceType.Control);
41+
var unknownNode = ToNode(testHtml).ToComparisonSource(0, ComparisonSourceType.Test);
42+
var comparison = new Comparison(knownNode, unknownNode);
43+
44+
ElementComparer.Compare(comparison, CompareResult.Unknown).ShouldBe(CompareResult.Same);
45+
}
3346
}
3447
}

src/AngleSharp.Diffing/Core/Comparison.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ namespace AngleSharp.Diffing.Core
2525
/// <summary>
2626
/// Gets whether the control and test nodes are of the same type and has the same name.
2727
/// </summary>
28-
public bool AreNodeTypesEqual => Control.Node.NodeType == Test.Node.NodeType && Control.Node.NodeName == Test.Node.NodeName;
28+
public bool AreNodeTypesEqual
29+
=> Control.Node.NodeType == Test.Node.NodeType
30+
&& Control.Node.NodeName.Equals(Test.Node.NodeName, StringComparison.OrdinalIgnoreCase);
2931

3032
/// <summary>
3133
/// Creates a new comparison.

0 commit comments

Comments
 (0)