Skip to content

Commit abf81f8

Browse files
committed
fix(XMLDiff): previously implemented handling of in-line comments for defining position of added elements in diff
1 parent ea88515 commit abf81f8

File tree

1 file changed

+11
-73
lines changed

1 file changed

+11
-73
lines changed

XMLDiff/Program.cs

Lines changed: 11 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -720,7 +720,11 @@ private static bool CompareElements(
720720
{
721721
string xpath = GenerateXPath(originalChildren[i - 1], pathOptions);
722722
string pos = "after";
723-
if (lastRemovedOrReplaced == i - 1 || NumericIdsPattern.IsMatch(xpath))
723+
if (
724+
lastRemovedOrReplaced == i - 1
725+
|| NumericIdsPattern.IsMatch(xpath)
726+
|| IsElementPrecededByPosBeforeComment(modifiedChildren[j])
727+
)
724728
{
725729
string xpathBefore = GenerateXPath(originalChild, pathOptions);
726730
if (!NumericIdsPattern.IsMatch(xpathBefore))
@@ -744,43 +748,10 @@ private static bool CompareElements(
744748
for (int l = j; l < k; l++)
745749
{
746750
var addedChild = modifiedChildren[l];
747-
748-
// Check if this specific element should use pos=before
749-
if (IsElementPrecededByPosBeforeComment(addedChild))
750-
{
751-
// Create a separate add operation with pos=before for this element
752-
string posBeforeXpath = GenerateXPath(addedChild, pathOptions);
753-
if (!NumericIdsPattern.IsMatch(posBeforeXpath))
754-
{
755-
XElement posBeforeAddOp = new XElement("add", new XAttribute("sel", posBeforeXpath), new XAttribute("pos", "before"));
756-
posBeforeAddOp.Add(addedChild);
757-
DiffRootAddOperation(diffRoot, posBeforeAddOp);
758-
Logger.Info(
759-
$"[Operation Add] Element '{GetElementInfo(addedChild)}' with pos=before to parent '{GetElementInfo(originalElem)}'."
760-
);
761-
}
762-
else
763-
{
764-
// Fall back to the regular add operation if XPath contains numeric IDs
765-
addOp.Add(addedChild);
766-
Logger.Info(
767-
$"[Operation Add] Element '{GetElementInfo(addedChild)}' to parent '{GetElementInfo(originalElem)}' (pos=before comment found but using regular add due to numeric XPath)."
768-
);
769-
}
770-
}
771-
else
772-
{
773-
// Use the regular add operation
774-
addOp.Add(addedChild);
775-
Logger.Info($"[Operation Add] Element '{GetElementInfo(addedChild)}' to parent '{GetElementInfo(originalElem)}'.");
776-
}
777-
}
778-
779-
// Only add the regular addOp if it has children
780-
if (addOp.HasElements)
781-
{
782-
DiffRootAddOperation(diffRoot, addOp);
751+
addOp.Add(addedChild);
752+
Logger.Info($"[Operation Add] Element '{GetElementInfo(addedChild)}' to parent '{GetElementInfo(originalElem)}'.");
783753
}
754+
DiffRootAddOperation(diffRoot, addOp);
784755
j = k;
785756
foundMatch = true;
786757
break;
@@ -834,44 +805,11 @@ private static bool CompareElements(
834805
while (j < modifiedChildren.Count)
835806
{
836807
var addedChild = modifiedChildren[j];
837-
838-
// Check if this specific element should use pos=before
839-
if (IsElementPrecededByPosBeforeComment(addedChild))
840-
{
841-
// Create a separate add operation with pos=before for this element
842-
string posBeforeXpath = GenerateXPath(addedChild, pathOptions);
843-
if (!NumericIdsPattern.IsMatch(posBeforeXpath))
844-
{
845-
XElement posBeforeAddOp = new XElement("add", new XAttribute("sel", posBeforeXpath), new XAttribute("pos", "before"));
846-
posBeforeAddOp.Add(addedChild);
847-
DiffRootAddOperation(diffRoot, posBeforeAddOp);
848-
Logger.Info(
849-
$"[Operation add] Element '{GetElementInfo(addedChild)}' with pos=before to parent '{GetElementInfo(originalElem)}'."
850-
);
851-
}
852-
else
853-
{
854-
// Fall back to the regular add operation if XPath contains numeric IDs
855-
addOp.Add(addedChild);
856-
Logger.Info(
857-
$"[Operation add] Element '{GetElementInfo(addedChild)}' to parent '{GetElementInfo(originalElem)}' (pos=before comment found but using regular add due to numeric XPath)."
858-
);
859-
}
860-
}
861-
else
862-
{
863-
// Use the regular add operation
864-
addOp.Add(addedChild);
865-
Logger.Info($"[Operation add] Element '{GetElementInfo(addedChild)}' to parent '{GetElementInfo(originalElem)}'.");
866-
}
808+
addOp.Add(addedChild);
809+
Logger.Info($"[Operation add] Element '{GetElementInfo(addedChild)}' to parent '{GetElementInfo(originalElem)}'.");
867810
j++;
868811
}
869-
870-
// Only add the regular addOp if it has children
871-
if (addOp.HasElements)
872-
{
873-
DiffRootAddOperation(diffRoot, addOp);
874-
}
812+
DiffRootAddOperation(diffRoot, addOp);
875813
}
876814
if (checkOnly)
877815
{

0 commit comments

Comments
 (0)