-
-
Notifications
You must be signed in to change notification settings - Fork 8
Description
New Feature Proposal
Description
The path
property inside the HtmlDiffer.compare()
Diff nodes does not work for TextNode
nodes.
Background
We are using this library to check the difference between 2 HTML elements and add style to those elements to show to users the changes between the 2. Since the nodes inside the diffs returned by HtmlDiffer
don't refer to the original HtmlDocument
passed to the compare method, I need to use the path
property to traverse the original document in order to add style (A red strikethrough) to the nodes marked by diff as MissingNodeDiff
.
The path
property works fine for all HtmlElement
nodes as it returns the correct index inside the list returned by Children. However, since TextNode are not present inside the Children
property, I must use ChildNodes
to access all of my TextNode
. In this case, the path does not return the correct index for all of my TextNode
.
Example
Here is the path returned by the diff node. I want to access the text(8) element inside the p(0) node. Accessing the p(0) element is not problem.
Here is what is returned by the Children
property inside the p(0) node. We can see that no text nodes are present.
Here is what is returned by the ChildNodes
property inside the p(0) node. We can see that there are text nodes however, although there is a TextNode
at index 8, it is not the correct node. The correct node should be at index 10 (I compared the content of the node returned by diff and the content of all nodes inside ChildNodes
).
Suggestion
I would suggest changing the current implementation of ComparisonSource.GetPathIndex() to:
private static int GetPathIndex(INode node)
{
var result = 0;
var parent = node.Parent;
if (parent is not null)
{
var childNodes = parent.ChildNodes;
for (int index = 0; index < childNodes.Length; index++)
{
if (ReferenceEquals(childNodes[index], node))
return index;
}
}
throw new InvalidOperationException("Unexpected node tree state. The node was not found in its parents child nodes collection.");
}
Also, not sure how it should be implemented, but the path
property is a little hard to work with as it requires extracting the indexes from the path string with a regex.