From 395ac65d312ddc67f60b612cfc51933b9d9b6150 Mon Sep 17 00:00:00 2001 From: "anthonyd%netscape.com" Date: Wed, 27 Dec 2000 00:37:01 +0000 Subject: [PATCH] fix for bug#58028 - Range CompareBoundaryPoints returning incorrect values r=jst,kin a=kin --- content/base/src/nsRange.cpp | 62 ++++++++++++----------- editor/base/nsHTMLEditRules.cpp | 4 +- editor/libeditor/html/nsHTMLEditRules.cpp | 4 +- layout/base/src/nsRange.cpp | 62 ++++++++++++----------- 4 files changed, 68 insertions(+), 64 deletions(-) diff --git a/content/base/src/nsRange.cpp b/content/base/src/nsRange.cpp index b27e13bc9d6..baee68c5ad8 100644 --- a/content/base/src/nsRange.cpp +++ b/content/base/src/nsRange.cpp @@ -1341,38 +1341,39 @@ nsresult nsRange::CompareBoundaryPoints(PRUint16 how, nsIDOMRange* srcRange, if (srcRange == 0) return NS_ERROR_INVALID_ARG; - nsCOMPtr node1; - nsCOMPtr node2; - PRInt32 offset1, offset2; + nsCOMPtr boundaryNode; // the Invoking range + nsCOMPtr sourceNode; // the sourceRange + PRInt32 boundaryOffset, sourceOffset; + switch (how) { - case nsIDOMRange::START_TO_START: - node1 = mStartParent; - offset1 = mStartOffset; - res = srcRange->GetStartContainer(getter_AddRefs(node2)); + case nsIDOMRange::START_TO_START: // where is the start point of boundary range + boundaryNode = mStartParent; // relative to the start point of the source range? + boundaryOffset = mStartOffset; + res = srcRange->GetStartContainer(getter_AddRefs(sourceNode)); if (NS_SUCCEEDED(res)) - res = srcRange->GetStartOffset(&offset2); + res = srcRange->GetStartOffset(&sourceOffset); break; - case nsIDOMRange::START_TO_END: - node1 = mStartParent; - offset1 = mStartOffset; - res = srcRange->GetEndContainer(getter_AddRefs(node2)); + case nsIDOMRange::START_TO_END: // where is the end point of the boundary range + boundaryNode = mEndParent; // relative to the start point of source range? + boundaryOffset = mEndOffset; + res = srcRange->GetStartContainer(getter_AddRefs(sourceNode)); if (NS_SUCCEEDED(res)) - res = srcRange->GetEndOffset(&offset2); + res = srcRange->GetStartOffset(&sourceOffset); break; - case nsIDOMRange::END_TO_START: - node1 = mEndParent; - offset1 = mEndOffset; - res = srcRange->GetStartContainer(getter_AddRefs(node2)); + case nsIDOMRange::END_TO_START: // where is the the start point of the boundary range + boundaryNode = mStartParent; // relative to end point of source range? + boundaryOffset = mStartOffset; + res = srcRange->GetEndContainer(getter_AddRefs(sourceNode)); if (NS_SUCCEEDED(res)) - res = srcRange->GetStartOffset(&offset2); + res = srcRange->GetEndOffset(&sourceOffset); break; - case nsIDOMRange::END_TO_END: - node1 = mEndParent; - offset1 = mEndOffset; - res = srcRange->GetEndContainer(getter_AddRefs(node2)); + case nsIDOMRange::END_TO_END: // where is the end point of boundary range + boundaryNode = mEndParent; // relative to the end point of the source range? + boundaryOffset = mEndOffset; + res = srcRange->GetEndContainer(getter_AddRefs(sourceNode)); if (NS_SUCCEEDED(res)) - res = srcRange->GetEndOffset(&offset2); + res = srcRange->GetEndOffset(&sourceOffset); break; default: // shouldn't get here @@ -1382,12 +1383,13 @@ nsresult nsRange::CompareBoundaryPoints(PRUint16 how, nsIDOMRange* srcRange, if (NS_FAILED(res)) return res; - if ((node1 == node2) && (offset1 == offset2)) - *aCmpRet = 0; - else if (IsIncreasing(node1, offset1, node2, offset2)) - *aCmpRet = 1; + if ((boundaryNode == sourceNode) && (boundaryOffset == sourceOffset)) + *aCmpRet = 0;//then the points are equal + else if (IsIncreasing(boundaryNode, boundaryOffset, sourceNode, sourceOffset)) + *aCmpRet = -1;//then boundary point is before source point else - *aCmpRet = -1; + *aCmpRet = 1;//then boundary point is after source point + return NS_OK; } @@ -1539,9 +1541,9 @@ return NS_ERROR_NOT_IMPLEMENTED; // from the start and end points, and add them // to the parent doc frag: res = CloneSibsAndParents(mStartParent, mStartOffset, 0, - commonParent, docfrag, PR_TRUE); + commonParent, docFrag, PR_TRUE); res = CloneSibsAndParents(mEndParent, mEndOffset, 0, - commonParent, docfrag, PR_FALSE); + commonParent, docFrag, PR_FALSE); // XXX Now we need to add the sibs between the two top-level // XXX doc frag cloned elements. diff --git a/editor/base/nsHTMLEditRules.cpp b/editor/base/nsHTMLEditRules.cpp index 50bb7284ff4..bbec3d8e434 100644 --- a/editor/base/nsHTMLEditRules.cpp +++ b/editor/base/nsHTMLEditRules.cpp @@ -5423,7 +5423,7 @@ nsHTMLEditRules::UpdateDocChangeRange(nsIDOMRange *aRange) // compare starts of ranges res = mDocChangeRange->CompareBoundaryPoints(nsIDOMRange::START_TO_START, aRange, &result); if (NS_FAILED(res)) return res; - if (result < 0) // negative result means aRange start is before mDocChangeRange start + if (result > 0) // positive result means mDocChangeRange start is after aRange start { nsCOMPtr startNode; PRInt32 startOffset; @@ -5438,7 +5438,7 @@ nsHTMLEditRules::UpdateDocChangeRange(nsIDOMRange *aRange) // compare ends of ranges res = mDocChangeRange->CompareBoundaryPoints(nsIDOMRange::END_TO_END, aRange, &result); if (NS_FAILED(res)) return res; - if (result > 0) // positive result means aRange end is after mDocChangeRange end + if (result < 0) // negative result means mDocChangeRange end is before aRange end { nsCOMPtr endNode; PRInt32 endOffset; diff --git a/editor/libeditor/html/nsHTMLEditRules.cpp b/editor/libeditor/html/nsHTMLEditRules.cpp index 50bb7284ff4..bbec3d8e434 100644 --- a/editor/libeditor/html/nsHTMLEditRules.cpp +++ b/editor/libeditor/html/nsHTMLEditRules.cpp @@ -5423,7 +5423,7 @@ nsHTMLEditRules::UpdateDocChangeRange(nsIDOMRange *aRange) // compare starts of ranges res = mDocChangeRange->CompareBoundaryPoints(nsIDOMRange::START_TO_START, aRange, &result); if (NS_FAILED(res)) return res; - if (result < 0) // negative result means aRange start is before mDocChangeRange start + if (result > 0) // positive result means mDocChangeRange start is after aRange start { nsCOMPtr startNode; PRInt32 startOffset; @@ -5438,7 +5438,7 @@ nsHTMLEditRules::UpdateDocChangeRange(nsIDOMRange *aRange) // compare ends of ranges res = mDocChangeRange->CompareBoundaryPoints(nsIDOMRange::END_TO_END, aRange, &result); if (NS_FAILED(res)) return res; - if (result > 0) // positive result means aRange end is after mDocChangeRange end + if (result < 0) // negative result means mDocChangeRange end is before aRange end { nsCOMPtr endNode; PRInt32 endOffset; diff --git a/layout/base/src/nsRange.cpp b/layout/base/src/nsRange.cpp index b27e13bc9d6..baee68c5ad8 100644 --- a/layout/base/src/nsRange.cpp +++ b/layout/base/src/nsRange.cpp @@ -1341,38 +1341,39 @@ nsresult nsRange::CompareBoundaryPoints(PRUint16 how, nsIDOMRange* srcRange, if (srcRange == 0) return NS_ERROR_INVALID_ARG; - nsCOMPtr node1; - nsCOMPtr node2; - PRInt32 offset1, offset2; + nsCOMPtr boundaryNode; // the Invoking range + nsCOMPtr sourceNode; // the sourceRange + PRInt32 boundaryOffset, sourceOffset; + switch (how) { - case nsIDOMRange::START_TO_START: - node1 = mStartParent; - offset1 = mStartOffset; - res = srcRange->GetStartContainer(getter_AddRefs(node2)); + case nsIDOMRange::START_TO_START: // where is the start point of boundary range + boundaryNode = mStartParent; // relative to the start point of the source range? + boundaryOffset = mStartOffset; + res = srcRange->GetStartContainer(getter_AddRefs(sourceNode)); if (NS_SUCCEEDED(res)) - res = srcRange->GetStartOffset(&offset2); + res = srcRange->GetStartOffset(&sourceOffset); break; - case nsIDOMRange::START_TO_END: - node1 = mStartParent; - offset1 = mStartOffset; - res = srcRange->GetEndContainer(getter_AddRefs(node2)); + case nsIDOMRange::START_TO_END: // where is the end point of the boundary range + boundaryNode = mEndParent; // relative to the start point of source range? + boundaryOffset = mEndOffset; + res = srcRange->GetStartContainer(getter_AddRefs(sourceNode)); if (NS_SUCCEEDED(res)) - res = srcRange->GetEndOffset(&offset2); + res = srcRange->GetStartOffset(&sourceOffset); break; - case nsIDOMRange::END_TO_START: - node1 = mEndParent; - offset1 = mEndOffset; - res = srcRange->GetStartContainer(getter_AddRefs(node2)); + case nsIDOMRange::END_TO_START: // where is the the start point of the boundary range + boundaryNode = mStartParent; // relative to end point of source range? + boundaryOffset = mStartOffset; + res = srcRange->GetEndContainer(getter_AddRefs(sourceNode)); if (NS_SUCCEEDED(res)) - res = srcRange->GetStartOffset(&offset2); + res = srcRange->GetEndOffset(&sourceOffset); break; - case nsIDOMRange::END_TO_END: - node1 = mEndParent; - offset1 = mEndOffset; - res = srcRange->GetEndContainer(getter_AddRefs(node2)); + case nsIDOMRange::END_TO_END: // where is the end point of boundary range + boundaryNode = mEndParent; // relative to the end point of the source range? + boundaryOffset = mEndOffset; + res = srcRange->GetEndContainer(getter_AddRefs(sourceNode)); if (NS_SUCCEEDED(res)) - res = srcRange->GetEndOffset(&offset2); + res = srcRange->GetEndOffset(&sourceOffset); break; default: // shouldn't get here @@ -1382,12 +1383,13 @@ nsresult nsRange::CompareBoundaryPoints(PRUint16 how, nsIDOMRange* srcRange, if (NS_FAILED(res)) return res; - if ((node1 == node2) && (offset1 == offset2)) - *aCmpRet = 0; - else if (IsIncreasing(node1, offset1, node2, offset2)) - *aCmpRet = 1; + if ((boundaryNode == sourceNode) && (boundaryOffset == sourceOffset)) + *aCmpRet = 0;//then the points are equal + else if (IsIncreasing(boundaryNode, boundaryOffset, sourceNode, sourceOffset)) + *aCmpRet = -1;//then boundary point is before source point else - *aCmpRet = -1; + *aCmpRet = 1;//then boundary point is after source point + return NS_OK; } @@ -1539,9 +1541,9 @@ return NS_ERROR_NOT_IMPLEMENTED; // from the start and end points, and add them // to the parent doc frag: res = CloneSibsAndParents(mStartParent, mStartOffset, 0, - commonParent, docfrag, PR_TRUE); + commonParent, docFrag, PR_TRUE); res = CloneSibsAndParents(mEndParent, mEndOffset, 0, - commonParent, docfrag, PR_FALSE); + commonParent, docFrag, PR_FALSE); // XXX Now we need to add the sibs between the two top-level // XXX doc frag cloned elements.