SelectionDetails *details = nsnull; SelectionDetails *newDetails = details;  for (i =0; i<cnt; i++){ nsCOMPtr<nsIDOMRange> range; nsCOMPtr<nsISupports> isupportsindex = dont_AddRef(mRangeArray->ElementAt(i)); range = do_QueryInterface(isupportsindex); if (range){ nsCOMPtr<nsIDOMNode> startNode; nsCOMPtr<nsIDOMNode> endNode; PRInt32 startOffset; PRInt32 endOffset; range->GetStartParent(getter_AddRefs(startNode)); range->GetStartOffset(&startOffset); range->GetEndParent(getter_AddRefs(endNode)); range->GetEndOffset(&endOffset); if (passedInNode == startNode && passedInNode == endNode){ if (startOffset < (aContentOffset + aContentLength)  && endOffset > aContentOffset){ if (!details){ details = new SelectionDetails;  newDetails = details; } else{ newDetails->mNext = new SelectionDetails; newDetails = newDetails->mNext; } if (!newDetails) return NS_ERROR_OUT_OF_MEMORY; newDetails->mNext = nsnull; newDetails->mStart = PR_MAX(0,startOffset - aContentOffset); newDetails->mEnd = PR_MIN(aContentLength, endOffset - aContentOffset); newDetails->mType = aType; } } else if (passedInNode == startNode){ if (startOffset < (aContentOffset + aContentLength)){ if (!details){ details = new SelectionDetails; newDetails = details; } else{ newDetails->mNext = new SelectionDetails; newDetails = newDetails->mNext; } if (!newDetails) return NS_ERROR_OUT_OF_MEMORY; newDetails->mNext = nsnull; newDetails->mStart = PR_MAX(0,startOffset - aContentOffset); newDetails->mEnd = aContentLength; newDetails->mType = aType; } } else if (passedInNode == endNode){ if (endOffset > aContentOffset){ if (!details){ details = new SelectionDetails; newDetails = details; } else{ newDetails->mNext = new SelectionDetails; newDetails = newDetails->mNext; } if (!newDetails) return NS_ERROR_OUT_OF_MEMORY; newDetails->mNext = nsnull; newDetails->mStart = 0; newDetails->mEnd = PR_MIN(aContentLength, endOffset - aContentOffset); newDetails->mType = aType; } } else { if (cnt > 1){  PRInt32 resultnum = ComparePoints(startNode, startOffset ,passedInNode, aContentOffset); if (resultnum > 0) continue; resultnum = ComparePoints(endNode, endOffset, passedInNode, aContentOffset ); if (resultnum <0) continue; } if (!details){ details = new SelectionDetails; newDetails = details; } else{ newDetails->mNext = new SelectionDetails; newDetails = newDetails->mNext; } if (!newDetails) return NS_ERROR_OUT_OF_MEMORY; newDetails->mNext = nsnull; newDetails->mStart = 0; newDetails->mEnd = aContentLength; newDetails->mType = aType; } } else continue;
*aReturnDetails = nsnull; PRInt8 j; for (j = (PRInt8) SELECTION_NORMAL; j < (PRInt8)NUM_SELECTIONTYPES; j++){ if (mDomSelections[j]) mDomSelections[j]->LookUpSelection(aContent, aContentOffset, aContentLength, aReturnDetails, (SelectionType)j, aSlowCheck);
nsDOMSelection::LookUpSelection(nsIContent *aContent, PRInt32 aContentOffset, PRInt32 aContentLength,                           SelectionDetails **aReturnDetails, SelectionType aType){  PRInt32 cnt;  nsresult rv = GetRangeCount(&cnt);  if (NS_FAILED(rv))     return rv;  PRInt32 i;  nsCOMPtr<nsIDOMNode> passedInNode;  passedInNode = do_QueryInterface(aContent);  if (!passedInNode)    return NS_ERROR_FAILURE;  SelectionDetails *details = nsnull;  SelectionDetails *newDetails = details;  for (i =0; i<cnt; i++){    nsCOMPtr<nsIDOMRange> range;    nsCOMPtr<nsISupports> isupportsindex = dont_AddRef(mRangeArray->ElementAt(i));    range = do_QueryInterface(isupportsindex);    if (range){      nsCOMPtr<nsIDOMNode> startNode;      nsCOMPtr<nsIDOMNode> endNode;      PRInt32 startOffset;      PRInt32 endOffset;      range->GetStartParent(getter_AddRefs(startNode));      range->GetStartOffset(&startOffset);      range->GetEndParent(getter_AddRefs(endNode));      range->GetEndOffset(&endOffset);      if (passedInNode == startNode && passedInNode == endNode){        if (startOffset < (aContentOffset + aContentLength)  &&            endOffset > aContentOffset){          if (!details){            details = new SelectionDetails;            newDetails = details;          }          else{            newDetails->mNext = new SelectionDetails;            newDetails = newDetails->mNext;          }          if (!newDetails)            return NS_ERROR_OUT_OF_MEMORY;          newDetails->mNext = nsnull;          newDetails->mStart = PR_MAX(0,startOffset - aContentOffset);          newDetails->mEnd = PR_MIN(aContentLength, endOffset - aContentOffset);          newDetails->mType = aType;        }      }      else if (passedInNode == startNode){        if (startOffset < (aContentOffset + aContentLength)){          if (!details){            details = new SelectionDetails;            newDetails = details;          }          else{            newDetails->mNext = new SelectionDetails;            newDetails = newDetails->mNext;          }          if (!newDetails)            return NS_ERROR_OUT_OF_MEMORY;          newDetails->mNext = nsnull;          newDetails->mStart = PR_MAX(0,startOffset - aContentOffset);          newDetails->mEnd = aContentLength;          newDetails->mType = aType;        }      }      else if (passedInNode == endNode){        if (endOffset > aContentOffset){          if (!details){            details = new SelectionDetails;            newDetails = details;          }          else{            newDetails->mNext = new SelectionDetails;            newDetails = newDetails->mNext;          }          if (!newDetails)            return NS_ERROR_OUT_OF_MEMORY;          newDetails->mNext = nsnull;          newDetails->mStart = 0;          newDetails->mEnd = PR_MIN(aContentLength, endOffset - aContentOffset);          newDetails->mType = aType;        }      }      else {        if (cnt > 1){          //we only have to look at start offset because anything else would have been in the range          PRInt32 resultnum = ComparePoints(startNode, startOffset                                   ,passedInNode, aContentOffset);          if (resultnum > 0)            continue;           resultnum = ComparePoints(endNode, endOffset,                              passedInNode, aContentOffset );          if (resultnum <0)            continue;        }        if (!details){          details = new SelectionDetails;          newDetails = details;        }        else{          newDetails->mNext = new SelectionDetails;          newDetails = newDetails->mNext;        }        if (!newDetails)          return NS_ERROR_OUT_OF_MEMORY;        newDetails->mNext = nsnull;        newDetails->mStart = 0;        newDetails->mEnd = aContentLength;        newDetails->mType = aType;      }    }    else      continue;  }  if (*aReturnDetails && newDetails)    newDetails->mNext = *aReturnDetails;  if (details)    *aReturnDetails = details;  return NS_OK;}