1998-11-24 21:20:11 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsRange.cpp: Implementation of the nsIDOMRange object.
|
|
|
|
*/
|
1998-11-24 21:24:40 +00:00
|
|
|
|
1998-12-18 02:51:34 +00:00
|
|
|
#include "nsRange.h"
|
|
|
|
|
1998-11-24 21:24:40 +00:00
|
|
|
#include "nsIDOMRange.h"
|
1998-12-01 19:18:52 +00:00
|
|
|
#include "nsIDOMNode.h"
|
1998-12-07 17:53:59 +00:00
|
|
|
#include "nsIDOMDocument.h"
|
1998-12-10 18:58:49 +00:00
|
|
|
#include "nsIDOMDocumentFragment.h"
|
1998-12-03 15:02:37 +00:00
|
|
|
#include "nsIContent.h"
|
1999-01-04 16:48:33 +00:00
|
|
|
#include "nsIDocument.h"
|
1998-12-03 15:02:37 +00:00
|
|
|
#include "nsVoidArray.h"
|
1998-12-09 22:07:00 +00:00
|
|
|
#include "nsIDOMText.h"
|
1999-02-12 05:28:46 +00:00
|
|
|
#include "nsIContentIterator.h"
|
1998-12-18 02:51:34 +00:00
|
|
|
#include "nsIDOMNodeList.h"
|
1999-04-15 20:19:26 +00:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
1999-05-11 20:22:35 +00:00
|
|
|
#include "nsIParser.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsParserCIID.h"
|
1999-06-03 22:38:38 +00:00
|
|
|
#include "nsIHTMLFragmentContentSink.h"
|
|
|
|
// XXX Temporary inclusion to deal with fragment parsing
|
|
|
|
#include "nsHTMLParts.h"
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-12-03 15:02:37 +00:00
|
|
|
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
1999-04-15 20:19:26 +00:00
|
|
|
static NS_DEFINE_IID(kIScriptObjectOwnerIID, NS_ISCRIPTOBJECTOWNER_IID);
|
1999-05-11 20:22:35 +00:00
|
|
|
static NS_DEFINE_IID(kCParserIID, NS_IPARSER_IID);
|
|
|
|
static NS_DEFINE_IID(kCParserCID, NS_PARSER_IID);
|
1999-04-15 20:19:26 +00:00
|
|
|
|
1999-01-04 16:48:33 +00:00
|
|
|
nsVoidArray* nsRange::mStartAncestors = nsnull;
|
|
|
|
nsVoidArray* nsRange::mEndAncestors = nsnull;
|
|
|
|
nsVoidArray* nsRange::mStartAncestorOffsets = nsnull;
|
|
|
|
nsVoidArray* nsRange::mEndAncestorOffsets = nsnull;
|
|
|
|
|
1999-02-12 05:28:46 +00:00
|
|
|
nsresult NS_NewContentIterator(nsIContentIterator** aInstancePtrResult);
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-12-18 02:51:34 +00:00
|
|
|
/******************************************************
|
|
|
|
* non members
|
|
|
|
******************************************************/
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult
|
|
|
|
NS_NewRange(nsIDOMRange** aInstancePtrResult)
|
|
|
|
{
|
|
|
|
nsRange * range = new nsRange();
|
1999-03-03 19:48:57 +00:00
|
|
|
return range->QueryInterface(nsIDOMRange::GetIID(), (void**) aInstancePtrResult);
|
1998-11-25 01:21:42 +00:00
|
|
|
}
|
|
|
|
|
1998-12-18 02:51:34 +00:00
|
|
|
|
|
|
|
// Returns -1 if point1 < point2, 1, if point1 > point2,
|
|
|
|
// 0 if error or if point1 == point2.
|
|
|
|
PRInt32 ComparePoints(nsIDOMNode* aParent1, PRInt32 aOffset1,
|
|
|
|
nsIDOMNode* aParent2, PRInt32 aOffset2)
|
|
|
|
{
|
|
|
|
if (aParent1 == aParent2 && aOffset1 == aOffset2)
|
|
|
|
return 0;
|
|
|
|
nsRange* range = new nsRange;
|
|
|
|
nsresult res = range->SetStart(aParent1, aOffset1);
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-18 02:51:34 +00:00
|
|
|
return 0;
|
|
|
|
res = range->SetEnd(aParent2, aOffset2);
|
|
|
|
delete range;
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
return -1;
|
|
|
|
else
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
// Utility routine to detect if a content node intersects a range
|
|
|
|
PRBool IsNodeIntersectsRange(nsIContent* aNode, nsIDOMRange* aRange)
|
|
|
|
{
|
|
|
|
// create a pair of dom points that expresses location of node:
|
|
|
|
// NODE(start), NODE(end)
|
|
|
|
// Let incoming range be:
|
|
|
|
// {RANGE(start), RANGE(end)}
|
|
|
|
// if (RANGE(start) < NODE(end)) and (RANGE(end) > NODE(start))
|
|
|
|
// then the Node intersect the Range.
|
|
|
|
|
1999-02-19 02:03:23 +00:00
|
|
|
if (!aNode) return PR_FALSE;
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parent, rangeStartParent, rangeEndParent;
|
|
|
|
PRInt32 nodeStart, nodeEnd, rangeStartOffset, rangeEndOffset;
|
|
|
|
|
|
|
|
// gather up the dom point info
|
|
|
|
if (!GetNodeBracketPoints(aNode, &parent, &nodeStart, &nodeEnd))
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(aRange->GetStartParent(getter_AddRefs(rangeStartParent))))
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(aRange->GetStartOffset(&rangeStartOffset)))
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(aRange->GetEndParent(getter_AddRefs(rangeEndParent))))
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(aRange->GetEndOffset(&rangeEndOffset)))
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
|
|
|
// is RANGE(start) < NODE(end) ?
|
|
|
|
PRInt32 comp = ComparePoints(rangeStartParent, rangeStartOffset, parent, nodeEnd);
|
|
|
|
if (comp >= 0)
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE; // range start is after node end
|
1999-02-14 09:14:50 +00:00
|
|
|
|
|
|
|
// is RANGE(end) > NODE(start) ?
|
|
|
|
comp = ComparePoints(rangeEndParent, rangeEndOffset, parent, nodeStart);
|
|
|
|
if (comp <= 0)
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE; // range end is before node start
|
1999-02-14 09:14:50 +00:00
|
|
|
|
|
|
|
// if we got here then the node intersects the range
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_TRUE;
|
1999-02-14 09:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Utility routine to detect if a content node is completely contained in a range
|
1999-02-16 15:43:28 +00:00
|
|
|
// If outNodeBefore is returned true, then the node starts before the range does.
|
|
|
|
// If outNodeAfter is returned true, then the node ends after the range does.
|
|
|
|
// Note that both of the above might be true.
|
|
|
|
// If neither are true, the node is contained inside of the range.
|
|
|
|
// XXX - callers responsibility to ensure node in same doc as range!
|
|
|
|
nsresult CompareNodeToRange(nsIContent* aNode,
|
|
|
|
nsIDOMRange* aRange,
|
|
|
|
PRBool *outNodeBefore,
|
|
|
|
PRBool *outNodeAfter)
|
1999-02-14 09:14:50 +00:00
|
|
|
{
|
|
|
|
// create a pair of dom points that expresses location of node:
|
|
|
|
// NODE(start), NODE(end)
|
|
|
|
// Let incoming range be:
|
|
|
|
// {RANGE(start), RANGE(end)}
|
|
|
|
// if (RANGE(start) <= NODE(start)) and (RANGE(end) => NODE(end))
|
|
|
|
// then the Node is contained (completely) by the Range.
|
|
|
|
|
1999-02-16 15:43:28 +00:00
|
|
|
if (!aNode)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
if (!aRange)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
if (!outNodeBefore)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
if (!outNodeAfter)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
PRBool isPositioned;
|
|
|
|
nsresult err = ((nsRange*)aRange)->GetIsPositioned(&isPositioned);
|
|
|
|
// Why do I have to cast above? Because GetIsPositioned() is
|
|
|
|
// mysteriously missing from the nsIDOMRange interface. dunno why.
|
|
|
|
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(err))
|
1999-02-16 15:43:28 +00:00
|
|
|
return err;
|
|
|
|
|
|
|
|
if (!isPositioned)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parent, rangeStartParent, rangeEndParent;
|
|
|
|
PRInt32 nodeStart, nodeEnd, rangeStartOffset, rangeEndOffset;
|
|
|
|
|
|
|
|
// gather up the dom point info
|
|
|
|
if (!GetNodeBracketPoints(aNode, &parent, &nodeStart, &nodeEnd))
|
1999-02-19 02:32:58 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(aRange->GetStartParent(getter_AddRefs(rangeStartParent))))
|
1999-02-19 02:32:58 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(aRange->GetStartOffset(&rangeStartOffset)))
|
1999-02-19 02:32:58 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(aRange->GetEndParent(getter_AddRefs(rangeEndParent))))
|
1999-02-19 02:32:58 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(aRange->GetEndOffset(&rangeEndOffset)))
|
1999-02-19 02:32:58 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1999-02-19 02:03:23 +00:00
|
|
|
*outNodeBefore = PR_FALSE;
|
|
|
|
*outNodeAfter = PR_FALSE;
|
1999-02-16 15:43:28 +00:00
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
// is RANGE(start) <= NODE(start) ?
|
|
|
|
PRInt32 comp = ComparePoints(rangeStartParent, rangeStartOffset, parent, nodeStart);
|
|
|
|
if (comp > 0)
|
1999-02-19 02:03:23 +00:00
|
|
|
*outNodeBefore = PR_TRUE; // range start is after node start
|
1999-02-14 09:14:50 +00:00
|
|
|
|
|
|
|
// is RANGE(end) >= NODE(end) ?
|
|
|
|
comp = ComparePoints(rangeEndParent, rangeEndOffset, parent, nodeEnd);
|
|
|
|
if (comp < 0)
|
1999-02-19 02:03:23 +00:00
|
|
|
*outNodeAfter = PR_TRUE; // range end is before node end
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1999-02-16 15:43:28 +00:00
|
|
|
return NS_OK;
|
1999-02-14 09:14:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Utility routine to create a pair of dom points to represent
|
|
|
|
// the start and end locations of a single node. Return false
|
|
|
|
// if we dont' succeed.
|
|
|
|
PRBool GetNodeBracketPoints(nsIContent* aNode,
|
|
|
|
nsCOMPtr<nsIDOMNode>* outParent,
|
|
|
|
PRInt32* outStartOffset,
|
|
|
|
PRInt32* outEndOffset)
|
|
|
|
{
|
|
|
|
if (!aNode)
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE;
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!outParent)
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE;
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!outStartOffset)
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE;
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!outEndOffset)
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1999-02-18 23:55:10 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> theDOMNode( do_QueryInterface(aNode) );
|
1999-02-14 09:14:50 +00:00
|
|
|
PRInt32 indx;
|
|
|
|
theDOMNode->GetParentNode(getter_AddRefs(*outParent));
|
|
|
|
|
|
|
|
if (!(*outParent)) // special case for root node
|
|
|
|
{
|
|
|
|
// can't make a parent/offset pair to represent start or
|
|
|
|
// end of the root node, becasue it has no parent.
|
|
|
|
// so instead represent it by (node,0) and (node,numChildren)
|
1999-02-18 23:55:10 +00:00
|
|
|
*outParent = do_QueryInterface(aNode);
|
1999-02-17 02:08:00 +00:00
|
|
|
nsCOMPtr<nsIContent> cN(do_QueryInterface(*outParent));
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!cN)
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE;
|
1999-02-14 09:14:50 +00:00
|
|
|
cN->ChildCount(indx);
|
|
|
|
if (!indx)
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE;
|
1999-02-14 09:14:50 +00:00
|
|
|
*outStartOffset = 0;
|
|
|
|
*outEndOffset = indx;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-02-17 02:08:00 +00:00
|
|
|
nsCOMPtr<nsIContent> cN(do_QueryInterface(*outParent));
|
1999-06-19 20:50:20 +00:00
|
|
|
if (!cN || NS_FAILED(cN->IndexOf(aNode, indx)))
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE;
|
1999-02-14 09:14:50 +00:00
|
|
|
*outStartOffset = indx;
|
|
|
|
*outEndOffset = indx+1;
|
|
|
|
}
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_TRUE;
|
1999-02-14 09:14:50 +00:00
|
|
|
}
|
1998-12-18 02:51:34 +00:00
|
|
|
|
1998-12-03 15:02:37 +00:00
|
|
|
/******************************************************
|
|
|
|
* constructor/destructor
|
|
|
|
******************************************************/
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsRange::nsRange() :
|
|
|
|
mIsPositioned(PR_FALSE),
|
|
|
|
mStartOffset(0),
|
1999-04-15 20:19:26 +00:00
|
|
|
mEndOffset(0),
|
1999-04-27 22:14:17 +00:00
|
|
|
mStartParent(),
|
|
|
|
mEndParent(),
|
1999-04-15 20:19:26 +00:00
|
|
|
mScriptObject(nsnull)
|
1998-12-03 22:59:07 +00:00
|
|
|
{
|
1998-11-25 01:21:42 +00:00
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
1998-12-03 22:59:07 +00:00
|
|
|
nsRange::~nsRange()
|
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
DoSetRange(nsCOMPtr<nsIDOMNode>(),0,nsCOMPtr<nsIDOMNode>(),0);
|
|
|
|
// we want the side effects (releases and list removals)
|
|
|
|
// note that "nsCOMPtr<nsIDOMmNode>()" is the moral equivalent of null
|
1998-11-25 01:21:42 +00:00
|
|
|
}
|
|
|
|
|
1998-12-03 15:02:37 +00:00
|
|
|
/******************************************************
|
|
|
|
* XPCOM cruft
|
|
|
|
******************************************************/
|
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
NS_IMPL_ADDREF(nsRange)
|
|
|
|
NS_IMPL_RELEASE(nsRange)
|
|
|
|
|
1998-12-03 15:02:37 +00:00
|
|
|
nsresult nsRange::QueryInterface(const nsIID& aIID,
|
|
|
|
void** aInstancePtrResult)
|
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
NS_PRECONDITION(aInstancePtrResult, "null pointer");
|
|
|
|
if (!aInstancePtrResult)
|
|
|
|
{
|
1998-12-03 15:02:37 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
1999-02-14 09:14:50 +00:00
|
|
|
if (aIID.Equals(kISupportsIID))
|
|
|
|
{
|
1999-04-15 20:19:26 +00:00
|
|
|
*aInstancePtrResult = (void*)(nsISupports*)(nsIDOMRange *)this;
|
1998-12-03 15:02:37 +00:00
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-03-03 19:48:57 +00:00
|
|
|
if (aIID.Equals(nsIDOMRange::GetIID()))
|
1999-02-14 09:14:50 +00:00
|
|
|
{
|
1998-12-03 15:02:37 +00:00
|
|
|
*aInstancePtrResult = (void*)(nsIDOMRange*)this;
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-04-27 22:16:04 +00:00
|
|
|
if (aIID.Equals(nsIDOMNSRange::GetIID()))
|
|
|
|
{
|
|
|
|
*aInstancePtrResult = (void*)(nsIDOMNSRange*)this;
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-04-15 20:19:26 +00:00
|
|
|
if (aIID.Equals(kIScriptObjectOwnerIID)) {
|
|
|
|
nsIScriptObjectOwner* tmp = this;
|
|
|
|
*aInstancePtrResult = (void*) tmp;
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_NOINTERFACE;
|
1998-12-03 15:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* Private helper routines
|
|
|
|
******************************************************/
|
1998-12-03 22:59:07 +00:00
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
PRBool nsRange::InSameDoc(nsCOMPtr<nsIDOMNode> aNode1, nsCOMPtr<nsIDOMNode> aNode2)
|
1998-12-04 18:21:52 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIContent> cN1;
|
|
|
|
nsCOMPtr<nsIContent> cN2;
|
|
|
|
nsCOMPtr<nsIDocument> doc1;
|
|
|
|
nsCOMPtr<nsIDocument> doc2;
|
1999-01-04 16:48:33 +00:00
|
|
|
|
|
|
|
nsresult res = GetContentFromDOMNode(aNode1, &cN1);
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1999-02-14 09:14:50 +00:00
|
|
|
return PR_FALSE;
|
1999-01-04 16:48:33 +00:00
|
|
|
res = GetContentFromDOMNode(aNode2, &cN2);
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1999-02-14 09:14:50 +00:00
|
|
|
return PR_FALSE;
|
|
|
|
res = cN1->GetDocument(*getter_AddRefs(doc1));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1999-02-14 09:14:50 +00:00
|
|
|
return PR_FALSE;
|
|
|
|
res = cN2->GetDocument(*getter_AddRefs(doc2));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1999-02-14 09:14:50 +00:00
|
|
|
return PR_FALSE;
|
1999-01-04 16:48:33 +00:00
|
|
|
|
|
|
|
// Now compare the two documents: is direct comparison safe?
|
1999-02-14 09:14:50 +00:00
|
|
|
if (doc1 == doc2)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
return PR_FALSE;
|
1998-12-04 18:21:52 +00:00
|
|
|
}
|
1998-12-18 02:51:34 +00:00
|
|
|
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsresult nsRange::AddToListOf(nsCOMPtr<nsIDOMNode> aNode)
|
1998-12-18 02:51:34 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!aNode)
|
1998-12-18 02:51:34 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIContent> cN;
|
1998-12-18 02:51:34 +00:00
|
|
|
|
1999-03-03 19:48:57 +00:00
|
|
|
nsresult res = aNode->QueryInterface(nsIContent::GetIID(), getter_AddRefs(cN));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-18 02:51:34 +00:00
|
|
|
return res;
|
|
|
|
|
1998-12-18 03:29:51 +00:00
|
|
|
res = cN->RangeAdd(*NS_STATIC_CAST(nsIDOMRange*,this));
|
1998-12-18 02:51:34 +00:00
|
|
|
return res;
|
|
|
|
}
|
1998-12-04 18:21:52 +00:00
|
|
|
|
1998-12-18 02:51:34 +00:00
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsresult nsRange::RemoveFromListOf(nsCOMPtr<nsIDOMNode> aNode)
|
1998-12-18 02:51:34 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!aNode)
|
1998-12-18 02:51:34 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIContent> cN;
|
1998-12-18 02:51:34 +00:00
|
|
|
|
1999-03-03 19:48:57 +00:00
|
|
|
nsresult res = aNode->QueryInterface(nsIContent::GetIID(), getter_AddRefs(cN));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-18 02:51:34 +00:00
|
|
|
return res;
|
|
|
|
|
1998-12-18 03:29:51 +00:00
|
|
|
res = cN->RangeRemove(*NS_STATIC_CAST(nsIDOMRange*,this));
|
1998-12-18 02:51:34 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1999-01-04 16:48:33 +00:00
|
|
|
// It's important that all setting of the range start/end points
|
1998-12-18 02:51:34 +00:00
|
|
|
// go through this function, which will do all the right voodoo
|
1999-02-14 09:14:50 +00:00
|
|
|
// for content notification of range ownership.
|
1998-12-18 23:12:29 +00:00
|
|
|
// Calling DoSetRange with either parent argument null will collapse
|
|
|
|
// the range to have both endpoints point to the other node
|
1999-02-14 09:14:50 +00:00
|
|
|
nsresult nsRange::DoSetRange(nsCOMPtr<nsIDOMNode> aStartN, PRInt32 aStartOffset,
|
|
|
|
nsCOMPtr<nsIDOMNode> aEndN, PRInt32 aEndOffset)
|
1998-12-04 18:21:52 +00:00
|
|
|
{
|
1999-01-04 16:48:33 +00:00
|
|
|
//if only one endpoint is null, set it to the other one
|
|
|
|
if (aStartN && !aEndN)
|
|
|
|
{
|
|
|
|
aEndN = aStartN;
|
|
|
|
aEndOffset = aStartOffset;
|
|
|
|
}
|
|
|
|
if (aEndN && !aStartN)
|
|
|
|
{
|
|
|
|
aStartN = aEndN;
|
|
|
|
aStartOffset = aEndOffset;
|
|
|
|
}
|
|
|
|
|
1998-12-04 18:21:52 +00:00
|
|
|
if (mStartParent != aStartN)
|
|
|
|
{
|
1998-12-18 02:51:34 +00:00
|
|
|
if (mStartParent) // if it had a former start node, take it off it's list
|
|
|
|
{
|
|
|
|
RemoveFromListOf(mStartParent);
|
|
|
|
}
|
1998-12-04 18:21:52 +00:00
|
|
|
mStartParent = aStartN;
|
1998-12-18 02:51:34 +00:00
|
|
|
if (mStartParent) // if it has a new start node, put it on it's list
|
|
|
|
{
|
|
|
|
AddToListOf(mStartParent);
|
|
|
|
}
|
1998-12-04 18:21:52 +00:00
|
|
|
}
|
|
|
|
mStartOffset = aStartOffset;
|
1998-12-18 23:12:29 +00:00
|
|
|
|
1998-12-04 18:21:52 +00:00
|
|
|
if (mEndParent != aEndN)
|
|
|
|
{
|
1998-12-18 02:51:34 +00:00
|
|
|
if (mEndParent) // if it had a former end node, take it off it's list
|
|
|
|
{
|
|
|
|
RemoveFromListOf(mEndParent);
|
|
|
|
}
|
1998-12-04 18:21:52 +00:00
|
|
|
mEndParent = aEndN;
|
1998-12-18 02:51:34 +00:00
|
|
|
if (mEndParent) // if it has a new end node, put it on it's list
|
|
|
|
{
|
|
|
|
AddToListOf(mEndParent);
|
|
|
|
}
|
1998-12-04 18:21:52 +00:00
|
|
|
}
|
|
|
|
mEndOffset = aEndOffset;
|
1998-12-18 23:12:29 +00:00
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
if (mStartParent)
|
|
|
|
mIsPositioned = PR_TRUE;
|
|
|
|
else
|
|
|
|
mIsPositioned = PR_FALSE;
|
1999-01-07 21:06:43 +00:00
|
|
|
|
1998-12-18 02:51:34 +00:00
|
|
|
// FIX ME need to handle error cases
|
|
|
|
// (range lists return error, or setting only one endpoint to null)
|
1998-12-04 18:21:52 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
PRBool nsRange::IsIncreasing(nsCOMPtr<nsIDOMNode> aStartN, PRInt32 aStartOffset,
|
|
|
|
nsCOMPtr<nsIDOMNode> aEndN, PRInt32 aEndOffset)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
1998-12-03 22:59:07 +00:00
|
|
|
PRInt32 numStartAncestors = 0;
|
|
|
|
PRInt32 numEndAncestors = 0;
|
|
|
|
PRInt32 commonNodeStartOffset = 0;
|
|
|
|
PRInt32 commonNodeEndOffset = 0;
|
|
|
|
|
|
|
|
// no trivial cases please
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!aStartN || !aEndN)
|
|
|
|
return PR_FALSE;
|
1998-12-03 22:59:07 +00:00
|
|
|
|
|
|
|
// check common case first
|
1998-12-04 18:21:52 +00:00
|
|
|
if (aStartN==aEndN)
|
1998-12-03 22:59:07 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
if (aStartOffset>aEndOffset)
|
|
|
|
return PR_FALSE;
|
|
|
|
else
|
|
|
|
return PR_TRUE;
|
1998-12-03 22:59:07 +00:00
|
|
|
}
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
// XXX - therad safety - need locks around here to end of routine
|
|
|
|
|
1999-01-04 16:48:33 +00:00
|
|
|
// lazy allocation of static arrays
|
|
|
|
if (!mStartAncestors)
|
|
|
|
{
|
|
|
|
mStartAncestors = new nsVoidArray();
|
|
|
|
mStartAncestorOffsets = new nsVoidArray();
|
|
|
|
mEndAncestors = new nsVoidArray();
|
|
|
|
mEndAncestorOffsets = new nsVoidArray();
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX Threading alert - these array structures are shared across all ranges
|
|
|
|
// access to ranges is assumed to be from only one thread. Add monitors (or
|
|
|
|
// stop sharing these) if that changes
|
|
|
|
|
1998-12-14 23:17:52 +00:00
|
|
|
// refresh ancestor data
|
|
|
|
mStartAncestors->Clear();
|
|
|
|
mStartAncestorOffsets->Clear();
|
|
|
|
mEndAncestors->Clear();
|
|
|
|
mEndAncestorOffsets->Clear();
|
|
|
|
|
1998-12-07 16:11:52 +00:00
|
|
|
numStartAncestors = GetAncestorsAndOffsets(aStartN,aStartOffset,mStartAncestors,mStartAncestorOffsets);
|
|
|
|
numEndAncestors = GetAncestorsAndOffsets(aEndN,aEndOffset,mEndAncestors,mEndAncestorOffsets);
|
1998-12-03 22:59:07 +00:00
|
|
|
|
|
|
|
--numStartAncestors; // adjusting for 0-based counting
|
|
|
|
--numEndAncestors;
|
|
|
|
// back through the ancestors, starting from the root, until first non-matching ancestor found
|
|
|
|
while (mStartAncestors->ElementAt(numStartAncestors) == mEndAncestors->ElementAt(numEndAncestors))
|
|
|
|
{
|
|
|
|
--numStartAncestors;
|
|
|
|
--numEndAncestors;
|
1999-02-14 09:14:50 +00:00
|
|
|
if (numStartAncestors<0)
|
|
|
|
break; // this will only happen if one endpoint's node is the common ancestor of the other
|
|
|
|
if (numEndAncestors<0)
|
|
|
|
break;
|
1998-12-03 22:59:07 +00:00
|
|
|
}
|
|
|
|
// now back up one and that's the last common ancestor from the root,
|
|
|
|
// or the first common ancestor from the leaf perspective
|
|
|
|
numStartAncestors++;
|
|
|
|
numEndAncestors++;
|
|
|
|
commonNodeStartOffset = (PRInt32)mStartAncestorOffsets->ElementAt(numStartAncestors);
|
|
|
|
commonNodeEndOffset = (PRInt32)mEndAncestorOffsets->ElementAt(numEndAncestors);
|
|
|
|
|
1999-03-09 19:21:04 +00:00
|
|
|
if (commonNodeStartOffset > commonNodeEndOffset)
|
1999-02-14 09:14:50 +00:00
|
|
|
return PR_FALSE;
|
1999-03-09 19:21:04 +00:00
|
|
|
else if (commonNodeStartOffset < commonNodeEndOffset)
|
1999-02-14 09:14:50 +00:00
|
|
|
return PR_TRUE;
|
1999-03-09 19:21:04 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
// The offsets are equal. This can happen when one endpoint parent is the common parent
|
|
|
|
// of both endpoints. In this case, we compare the depth of the ancestor tree to determine
|
|
|
|
// the ordering.
|
|
|
|
if (numStartAncestors < numEndAncestors)
|
|
|
|
return PR_TRUE;
|
|
|
|
else if (numStartAncestors > numEndAncestors)
|
|
|
|
return PR_FALSE;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// whoa nelly. shouldn't get here.
|
|
|
|
NS_NOTREACHED("nsRange::IsIncreasing");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
1998-12-03 09:51:05 +00:00
|
|
|
}
|
1998-11-26 02:30:44 +00:00
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsresult nsRange::IsPointInRange(nsCOMPtr<nsIDOMNode> aParent, PRInt32 aOffset, PRBool* aResult)
|
1998-12-03 15:02:37 +00:00
|
|
|
{
|
1998-12-07 16:11:52 +00:00
|
|
|
PRInt32 compareResult = 0;
|
|
|
|
nsresult res;
|
|
|
|
res = ComparePointToRange(aParent, aOffset, &compareResult);
|
1999-02-14 09:14:50 +00:00
|
|
|
if (compareResult)
|
|
|
|
*aResult = PR_FALSE;
|
|
|
|
else
|
|
|
|
*aResult = PR_TRUE;
|
1998-12-07 16:11:52 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns -1 if point is before range, 0 if point is in range, 1 if point is after range
|
1999-02-14 09:14:50 +00:00
|
|
|
nsresult nsRange::ComparePointToRange(nsCOMPtr<nsIDOMNode> aParent, PRInt32 aOffset, PRInt32* aResult)
|
1998-12-07 16:11:52 +00:00
|
|
|
{
|
|
|
|
// check arguments
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!aResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1998-12-07 16:11:52 +00:00
|
|
|
|
|
|
|
// no trivial cases please
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!aParent)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1998-12-07 16:11:52 +00:00
|
|
|
|
1999-01-22 08:59:26 +00:00
|
|
|
// our range is in a good state?
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!mIsPositioned)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1998-12-07 16:11:52 +00:00
|
|
|
|
|
|
|
// check common case first
|
|
|
|
if ((aParent == mStartParent) && (aParent == mEndParent))
|
|
|
|
{
|
|
|
|
if (aOffset<mStartOffset)
|
|
|
|
{
|
|
|
|
*aResult = -1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aOffset>mEndOffset)
|
|
|
|
{
|
|
|
|
*aResult = 1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
*aResult = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-01-22 08:59:26 +00:00
|
|
|
// more common cases
|
|
|
|
if ((aParent == mStartParent) && (aOffset == mStartOffset))
|
|
|
|
{
|
|
|
|
*aResult = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if ((aParent == mEndParent) && (aOffset == mEndOffset))
|
|
|
|
{
|
|
|
|
*aResult = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1998-12-07 16:11:52 +00:00
|
|
|
// ok, do it the hard way
|
1999-02-14 09:14:50 +00:00
|
|
|
if (IsIncreasing(aParent,aOffset,mStartParent,mStartOffset))
|
|
|
|
*aResult = -1;
|
|
|
|
else if (IsIncreasing(mEndParent,mEndOffset,aParent,aOffset))
|
|
|
|
*aResult = 1;
|
|
|
|
else
|
|
|
|
*aResult = 0;
|
1998-12-07 16:11:52 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
1998-12-03 15:02:37 +00:00
|
|
|
}
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
PRInt32 nsRange::IndexOf(nsCOMPtr<nsIDOMNode> aChildNode)
|
1998-12-03 15:02:37 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parentNode;
|
|
|
|
nsCOMPtr<nsIContent> contentChild;
|
|
|
|
nsCOMPtr<nsIContent> contentParent;
|
1998-12-05 02:19:01 +00:00
|
|
|
PRInt32 theIndex = nsnull;
|
1998-12-03 15:02:37 +00:00
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!aChildNode)
|
1998-12-03 15:02:37 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// get the parent node
|
1999-02-14 09:14:50 +00:00
|
|
|
nsresult res = aChildNode->GetParentNode(getter_AddRefs(parentNode));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-03 15:02:37 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// convert node and parent to nsIContent, so that we can find the child index
|
1999-03-03 19:48:57 +00:00
|
|
|
res = parentNode->QueryInterface(nsIContent::GetIID(), getter_AddRefs(contentParent));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-03 15:02:37 +00:00
|
|
|
return 0;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1999-03-03 19:48:57 +00:00
|
|
|
res = aChildNode->QueryInterface(nsIContent::GetIID(), getter_AddRefs(contentChild));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-03 15:02:37 +00:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
// finally we get the index
|
1998-12-03 22:59:07 +00:00
|
|
|
res = contentParent->IndexOf(contentChild,theIndex);
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-03 15:02:37 +00:00
|
|
|
return 0;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1998-12-03 15:02:37 +00:00
|
|
|
return theIndex;
|
|
|
|
}
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
PRInt32 nsRange::FillArrayWithAncestors(nsVoidArray* aArray, nsCOMPtr<nsIDOMNode> aNode)
|
1998-12-03 15:02:37 +00:00
|
|
|
{
|
|
|
|
PRInt32 i=0;
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> node(aNode);
|
1999-02-22 19:06:48 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
1998-12-03 15:02:37 +00:00
|
|
|
// callers responsibility to make sure args are non-null and proper type
|
|
|
|
|
|
|
|
// insert node itself
|
|
|
|
aArray->InsertElementAt((void*)node,i);
|
|
|
|
|
|
|
|
// insert all the ancestors
|
|
|
|
// not doing all the inserts at location 0, that would make for lots of memcopys in the voidArray::InsertElementAt implementation
|
1999-02-22 19:06:48 +00:00
|
|
|
node->GetParentNode(getter_AddRefs(parent));
|
|
|
|
while(parent)
|
1998-12-03 15:02:37 +00:00
|
|
|
{
|
1999-02-22 19:06:48 +00:00
|
|
|
node = parent;
|
1998-12-03 15:02:37 +00:00
|
|
|
++i;
|
1999-02-14 09:14:50 +00:00
|
|
|
aArray->InsertElementAt(NS_STATIC_CAST(void*,node),i);
|
1999-02-22 19:06:48 +00:00
|
|
|
node->GetParentNode(getter_AddRefs(parent));
|
1998-12-03 22:59:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
PRInt32 nsRange::GetAncestorsAndOffsets(nsCOMPtr<nsIDOMNode> aNode, PRInt32 aOffset,
|
1998-12-03 22:59:07 +00:00
|
|
|
nsVoidArray* aAncestorNodes, nsVoidArray* aAncestorOffsets)
|
|
|
|
{
|
|
|
|
PRInt32 i=0;
|
|
|
|
PRInt32 nodeOffset;
|
|
|
|
nsresult res;
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIContent> contentNode;
|
|
|
|
nsCOMPtr<nsIContent> contentParent;
|
1998-12-03 22:59:07 +00:00
|
|
|
|
|
|
|
// callers responsibility to make sure args are non-null and proper type
|
|
|
|
|
1999-03-03 19:48:57 +00:00
|
|
|
res = aNode->QueryInterface(nsIContent::GetIID(),getter_AddRefs(contentNode));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-03 22:59:07 +00:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsRange::GetAncestorsAndOffsets");
|
|
|
|
return -1; // poor man's error code
|
|
|
|
}
|
|
|
|
|
|
|
|
// insert node itself
|
|
|
|
aAncestorNodes->InsertElementAt((void*)contentNode,i);
|
|
|
|
aAncestorOffsets->InsertElementAt((void*)aOffset,i);
|
|
|
|
|
|
|
|
// insert all the ancestors
|
|
|
|
// not doing all the inserts at location 0, that would make for lots of memcopys in the voidArray::InsertElementAt implementation
|
1999-02-14 09:14:50 +00:00
|
|
|
contentNode->GetParent(*getter_AddRefs(contentParent));
|
1998-12-03 22:59:07 +00:00
|
|
|
while(contentParent)
|
|
|
|
{
|
|
|
|
contentParent->IndexOf(contentNode, nodeOffset);
|
|
|
|
++i;
|
|
|
|
aAncestorNodes->InsertElementAt((void*)contentParent,i);
|
|
|
|
aAncestorOffsets->InsertElementAt((void*)nodeOffset,i);
|
|
|
|
contentNode = contentParent;
|
1999-02-14 09:14:50 +00:00
|
|
|
contentNode->GetParent(*getter_AddRefs(contentParent));
|
1998-12-03 15:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> nsRange::CommonParent(nsCOMPtr<nsIDOMNode> aNode1, nsCOMPtr<nsIDOMNode> aNode2)
|
1998-12-03 15:02:37 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> theParent;
|
1998-12-03 15:02:37 +00:00
|
|
|
|
|
|
|
// no null nodes please
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!aNode1 || !aNode2)
|
1999-03-09 19:21:04 +00:00
|
|
|
return theParent; // moral equiv of nsnull
|
1998-12-03 15:02:37 +00:00
|
|
|
|
|
|
|
// shortcut for common case - both nodes are the same
|
|
|
|
if (aNode1 == aNode2)
|
|
|
|
{
|
1999-03-09 19:21:04 +00:00
|
|
|
theParent = aNode1; // this forces an AddRef
|
|
|
|
return theParent;
|
1998-12-03 15:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise traverse the tree for the common ancestor
|
|
|
|
// For now, a pretty dumb hack on computing this
|
1999-03-09 19:21:04 +00:00
|
|
|
nsVoidArray array1;
|
|
|
|
nsVoidArray array2;
|
1998-12-03 15:02:37 +00:00
|
|
|
PRInt32 i=0, j=0;
|
|
|
|
|
|
|
|
// get ancestors of each node
|
1999-03-09 19:21:04 +00:00
|
|
|
i = FillArrayWithAncestors(&array1,aNode1);
|
|
|
|
j = FillArrayWithAncestors(&array2,aNode2);
|
1998-12-03 15:02:37 +00:00
|
|
|
|
|
|
|
// sanity test (for now) - FillArrayWithAncestors succeeded
|
|
|
|
if ((i==-1) || (j==-1))
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsRange::CommonParent");
|
1999-03-09 19:21:04 +00:00
|
|
|
return theParent; // moral equiv of nsnull
|
1998-12-03 15:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// sanity test (for now) - the end of each array
|
|
|
|
// should match and be the root
|
1999-03-09 19:21:04 +00:00
|
|
|
if (array1.ElementAt(i) != array2.ElementAt(j))
|
1998-12-03 15:02:37 +00:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsRange::CommonParent");
|
1999-03-09 19:21:04 +00:00
|
|
|
return theParent; // moral equiv of nsnull
|
1998-12-03 15:02:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// back through the ancestors, starting from the root, until
|
|
|
|
// first different ancestor found.
|
1999-03-09 19:21:04 +00:00
|
|
|
while (array1.ElementAt(i) == array2.ElementAt(j))
|
1998-12-03 15:02:37 +00:00
|
|
|
{
|
|
|
|
--i;
|
|
|
|
--j;
|
1999-02-14 09:14:50 +00:00
|
|
|
if (i<0)
|
|
|
|
break; // this will only happen if one endpoint's node is the common ancestor of the other
|
|
|
|
if (j<0)
|
|
|
|
break;
|
1998-12-03 15:02:37 +00:00
|
|
|
}
|
|
|
|
// now back up one and that's the last common ancestor from the root,
|
|
|
|
// or the first common ancestor from the leaf perspective
|
|
|
|
i++;
|
1999-03-09 19:21:04 +00:00
|
|
|
nsIDOMNode *node = NS_STATIC_CAST(nsIDOMNode*, array1.ElementAt(i));
|
|
|
|
theParent = do_QueryInterface(node);
|
1998-12-03 15:02:37 +00:00
|
|
|
return theParent;
|
|
|
|
}
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsresult nsRange::GetDOMNodeFromContent(nsCOMPtr<nsIContent> inContentNode, nsCOMPtr<nsIDOMNode>* outDomNode)
|
1998-12-18 09:28:55 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!outDomNode)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-03-03 19:48:57 +00:00
|
|
|
nsresult res = inContentNode->QueryInterface(nsIDOMNode::GetIID(), getter_AddRefs(*outDomNode));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1999-01-04 16:48:33 +00:00
|
|
|
return res;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsresult nsRange::GetContentFromDOMNode(nsCOMPtr<nsIDOMNode> inDomNode, nsCOMPtr<nsIContent>* outContentNode)
|
1999-01-04 16:48:33 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!outContentNode)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-03-03 19:48:57 +00:00
|
|
|
nsresult res = inDomNode->QueryInterface(nsIContent::GetIID(), getter_AddRefs(*outContentNode));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-18 09:28:55 +00:00
|
|
|
return res;
|
1998-12-30 08:28:16 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsresult nsRange::PopRanges(nsCOMPtr<nsIDOMNode> aDestNode, PRInt32 aOffset, nsCOMPtr<nsIContent> aSourceNode)
|
1998-12-30 08:28:16 +00:00
|
|
|
{
|
|
|
|
// utility routine to pop all the range endpoints inside the content subtree defined by
|
|
|
|
// aSourceNode, into the node/offset represented by aDestNode/aOffset.
|
|
|
|
|
1999-02-12 05:28:46 +00:00
|
|
|
nsCOMPtr<nsIContentIterator> iter;
|
|
|
|
nsresult res = NS_NewContentIterator(getter_AddRefs(iter));
|
|
|
|
iter->Init(aSourceNode);
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIContent> cN;
|
1998-12-30 08:28:16 +00:00
|
|
|
nsVoidArray* theRangeList;
|
|
|
|
|
1999-05-14 06:18:07 +00:00
|
|
|
iter->CurrentNode(getter_AddRefs(cN));
|
|
|
|
while (cN && (NS_COMFALSE == iter->IsDone()))
|
1998-12-30 08:28:16 +00:00
|
|
|
{
|
|
|
|
cN->GetRangeList(theRangeList);
|
|
|
|
if (theRangeList)
|
|
|
|
{
|
|
|
|
nsRange* theRange;
|
1999-04-08 06:00:44 +00:00
|
|
|
PRInt32 theCount = theRangeList->Count();
|
|
|
|
while (theCount)
|
1998-12-30 08:28:16 +00:00
|
|
|
{
|
1999-04-08 06:00:44 +00:00
|
|
|
theRange = NS_STATIC_CAST(nsRange*, (theRangeList->ElementAt(0)));
|
|
|
|
if (theRange)
|
1998-12-30 08:28:16 +00:00
|
|
|
{
|
1999-04-08 06:00:44 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> domNode;
|
|
|
|
res = GetDOMNodeFromContent(cN, &domNode);
|
1999-06-16 21:38:51 +00:00
|
|
|
NS_POSTCONDITION(NS_SUCCEEDED(res), "error updating range list");
|
|
|
|
NS_POSTCONDITION(domNode, "error updating range list");
|
1999-04-08 06:00:44 +00:00
|
|
|
// sanity check - do range and content agree over ownership?
|
|
|
|
res = theRange->ContentOwnsUs(domNode);
|
1999-06-16 21:38:51 +00:00
|
|
|
NS_POSTCONDITION(NS_SUCCEEDED(res), "range and content disagree over range ownership");
|
1999-04-08 06:00:44 +00:00
|
|
|
|
|
|
|
if (theRange->mStartParent == domNode)
|
|
|
|
{
|
|
|
|
// promote start point up to replacement point
|
1999-06-16 21:38:51 +00:00
|
|
|
res = theRange->SetStart(aDestNode, aOffset);
|
|
|
|
NS_POSTCONDITION(NS_SUCCEEDED(res), "nsRange::PopRanges() got error from SetStart()");
|
1999-04-08 06:00:44 +00:00
|
|
|
}
|
|
|
|
if (theRange->mEndParent == domNode)
|
|
|
|
{
|
|
|
|
// promote end point up to replacement point
|
1999-06-16 21:38:51 +00:00
|
|
|
res = theRange->SetEnd(aDestNode, aOffset);
|
|
|
|
NS_POSTCONDITION(NS_SUCCEEDED(res), "nsRange::PopRanges() got error from SetEnd()");
|
1999-04-08 06:00:44 +00:00
|
|
|
}
|
1998-12-30 08:28:16 +00:00
|
|
|
}
|
1999-04-08 06:00:44 +00:00
|
|
|
// must refresh theRangeList - it might have gone away!
|
|
|
|
cN->GetRangeList(theRangeList);
|
|
|
|
if (theRangeList)
|
|
|
|
theCount = theRangeList->Count();
|
|
|
|
else
|
|
|
|
theCount = 0;
|
1998-12-30 08:28:16 +00:00
|
|
|
}
|
|
|
|
}
|
1999-05-10 23:29:01 +00:00
|
|
|
res = iter->Next();
|
|
|
|
if (NS_FAILED(res)) // a little noise here to catch bugs
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsRange::PopRanges() : iterator failed to advance");
|
|
|
|
return res;
|
|
|
|
}
|
1999-05-14 06:18:07 +00:00
|
|
|
iter->CurrentNode(getter_AddRefs(cN));
|
1998-12-30 08:28:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// sanity check routine for content helpers. confirms that given
|
|
|
|
// node owns one or both range endpoints.
|
1999-02-14 09:14:50 +00:00
|
|
|
nsresult nsRange::ContentOwnsUs(nsCOMPtr<nsIDOMNode> domNode)
|
1998-12-30 08:28:16 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(domNode, "null pointer");
|
|
|
|
if ((mStartParent != domNode) && (mEndParent != domNode))
|
1998-12-18 09:28:55 +00:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsRange::ContentOwnsUs");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1998-12-03 15:02:37 +00:00
|
|
|
/******************************************************
|
|
|
|
* public functionality
|
|
|
|
******************************************************/
|
|
|
|
|
1998-12-03 09:51:05 +00:00
|
|
|
nsresult nsRange::GetIsPositioned(PRBool* aIsPositioned)
|
|
|
|
{
|
|
|
|
*aIsPositioned = mIsPositioned;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-12-01 19:15:00 +00:00
|
|
|
nsresult nsRange::GetStartParent(nsIDOMNode** aStartParent)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
|
|
|
if (!mIsPositioned)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (!aStartParent)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-02-14 09:14:50 +00:00
|
|
|
//NS_IF_RELEASE(*aStartParent); don't think we should be doing this
|
1998-12-03 09:51:05 +00:00
|
|
|
*aStartParent = mStartParent;
|
1999-02-14 09:14:50 +00:00
|
|
|
NS_IF_ADDREF(*aStartParent);
|
1998-12-03 09:51:05 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::GetStartOffset(PRInt32* aStartOffset)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
|
|
|
if (!mIsPositioned)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (!aStartOffset)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
*aStartOffset = mStartOffset;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-12-01 19:15:00 +00:00
|
|
|
nsresult nsRange::GetEndParent(nsIDOMNode** aEndParent)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
|
|
|
if (!mIsPositioned)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (!aEndParent)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-02-14 09:14:50 +00:00
|
|
|
//NS_IF_RELEASE(*aEndParent); don't think we should be doing this
|
1998-12-03 09:51:05 +00:00
|
|
|
*aEndParent = mEndParent;
|
1999-02-14 09:14:50 +00:00
|
|
|
NS_IF_ADDREF(*aEndParent);
|
1998-12-03 09:51:05 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::GetEndOffset(PRInt32* aEndOffset)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
|
|
|
if (!mIsPositioned)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (!aEndOffset)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
*aEndOffset = mEndOffset;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::GetIsCollapsed(PRBool* aIsCollapsed)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
|
|
|
if (!mIsPositioned)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (mEndParent == 0 ||
|
|
|
|
(mStartParent == mEndParent && mStartOffset == mEndOffset))
|
|
|
|
*aIsCollapsed = PR_TRUE;
|
|
|
|
else
|
|
|
|
*aIsCollapsed = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::GetCommonParent(nsIDOMNode** aCommonParent)
|
1998-12-03 15:02:37 +00:00
|
|
|
{
|
|
|
|
*aCommonParent = CommonParent(mStartParent,mEndParent);
|
1999-03-09 19:21:04 +00:00
|
|
|
NS_IF_ADDREF(*aCommonParent);
|
1998-12-03 15:02:37 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::SetStart(nsIDOMNode* aParent, PRInt32 aOffset)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
1998-12-04 18:21:52 +00:00
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
if (!aParent) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-03-08 01:20:02 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>theParent( do_QueryInterface(aParent) );
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1998-12-04 18:21:52 +00:00
|
|
|
// must be in same document as endpoint, else
|
|
|
|
// endpoint is collapsed to new start.
|
1999-02-14 09:14:50 +00:00
|
|
|
if (mIsPositioned && !InSameDoc(theParent,mEndParent))
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
res = DoSetRange(theParent,aOffset,theParent,aOffset);
|
1998-12-04 18:21:52 +00:00
|
|
|
return res;
|
1998-12-03 09:51:05 +00:00
|
|
|
}
|
1998-12-04 18:21:52 +00:00
|
|
|
// start must be before end
|
1999-02-14 09:14:50 +00:00
|
|
|
if (mIsPositioned && !IsIncreasing(theParent,aOffset,mEndParent,mEndOffset))
|
1998-12-04 18:21:52 +00:00
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
// if it's in an attribute node, end must be in or descended from same node
|
|
|
|
// (haven't done this one yet)
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
res = DoSetRange(theParent,aOffset,mEndParent,mEndOffset);
|
1998-12-04 18:21:52 +00:00
|
|
|
return res;
|
1998-12-03 09:51:05 +00:00
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-12-10 18:58:49 +00:00
|
|
|
nsresult nsRange::SetStartBefore(nsIDOMNode* aSibling)
|
1998-12-09 18:44:26 +00:00
|
|
|
{
|
1999-03-08 01:20:02 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>theSibling( do_QueryInterface(aSibling) );
|
1999-02-14 09:14:50 +00:00
|
|
|
PRInt32 indx = IndexOf(theSibling);
|
1998-12-14 23:17:52 +00:00
|
|
|
nsIDOMNode *nParent;
|
1999-05-10 23:29:01 +00:00
|
|
|
nsresult res = theSibling->GetParentNode(&nParent);
|
|
|
|
if (NS_FAILED(res)) return res;
|
1998-12-14 23:17:52 +00:00
|
|
|
return SetStart(nParent,indx);
|
1998-12-09 18:44:26 +00:00
|
|
|
}
|
|
|
|
|
1998-12-10 18:58:49 +00:00
|
|
|
nsresult nsRange::SetStartAfter(nsIDOMNode* aSibling)
|
1998-12-09 18:44:26 +00:00
|
|
|
{
|
1999-03-08 01:20:02 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>theSibling( do_QueryInterface(aSibling) );
|
1999-02-14 09:14:50 +00:00
|
|
|
PRInt32 indx = IndexOf(theSibling) + 1;
|
1998-12-14 23:17:52 +00:00
|
|
|
nsIDOMNode *nParent;
|
1999-05-10 23:29:01 +00:00
|
|
|
nsresult res = theSibling->GetParentNode(&nParent);
|
|
|
|
if (NS_FAILED(res)) return res;
|
1998-12-14 23:17:52 +00:00
|
|
|
return SetStart(nParent,indx);
|
1998-12-09 18:44:26 +00:00
|
|
|
}
|
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::SetEnd(nsIDOMNode* aParent, PRInt32 aOffset)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
1998-12-04 18:21:52 +00:00
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
if (!aParent) return NS_ERROR_NULL_POINTER;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1999-03-08 01:20:02 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>theParent( do_QueryInterface(aParent) );
|
1998-12-04 18:21:52 +00:00
|
|
|
|
|
|
|
// must be in same document as startpoint, else
|
|
|
|
// endpoint is collapsed to new end.
|
1999-02-14 09:14:50 +00:00
|
|
|
if (mIsPositioned && !InSameDoc(theParent,mStartParent))
|
1998-12-04 18:21:52 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
res = DoSetRange(theParent,aOffset,theParent,aOffset);
|
1998-12-04 18:21:52 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
// start must be before end
|
1999-02-14 09:14:50 +00:00
|
|
|
if (mIsPositioned && !IsIncreasing(mStartParent,mStartOffset,theParent,aOffset))
|
1998-12-04 18:21:52 +00:00
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
// if it's in an attribute node, start must be in or descended from same node
|
|
|
|
// (haven't done this one yet)
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
res = DoSetRange(mStartParent,mStartOffset,theParent,aOffset);
|
1998-12-04 18:21:52 +00:00
|
|
|
return res;
|
1998-12-03 09:51:05 +00:00
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-12-10 18:58:49 +00:00
|
|
|
nsresult nsRange::SetEndBefore(nsIDOMNode* aSibling)
|
1998-12-09 18:44:26 +00:00
|
|
|
{
|
1999-03-08 01:20:02 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>theSibling( do_QueryInterface(aSibling) );
|
1999-02-14 09:14:50 +00:00
|
|
|
PRInt32 indx = IndexOf(theSibling);
|
1998-12-14 23:17:52 +00:00
|
|
|
nsIDOMNode *nParent;
|
1999-05-10 23:29:01 +00:00
|
|
|
nsresult res = theSibling->GetParentNode(&nParent);
|
|
|
|
if (NS_FAILED(res)) return res;
|
1998-12-14 23:17:52 +00:00
|
|
|
return SetEnd(nParent,indx);
|
1998-12-09 18:44:26 +00:00
|
|
|
}
|
|
|
|
|
1998-12-10 18:58:49 +00:00
|
|
|
nsresult nsRange::SetEndAfter(nsIDOMNode* aSibling)
|
1998-12-09 18:44:26 +00:00
|
|
|
{
|
1999-03-08 01:20:02 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>theSibling( do_QueryInterface(aSibling) );
|
1999-02-14 09:14:50 +00:00
|
|
|
PRInt32 indx = IndexOf(theSibling) + 1;
|
1998-12-14 23:17:52 +00:00
|
|
|
nsIDOMNode *nParent;
|
1999-05-10 23:29:01 +00:00
|
|
|
nsresult res = theSibling->GetParentNode(&nParent);
|
|
|
|
if (NS_FAILED(res)) return res;
|
1998-12-14 23:17:52 +00:00
|
|
|
return SetEnd(nParent,indx);
|
1998-12-09 18:44:26 +00:00
|
|
|
}
|
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::Collapse(PRBool aToStart)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
|
|
|
if (!mIsPositioned)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
if (aToStart)
|
|
|
|
{
|
1998-12-18 02:51:34 +00:00
|
|
|
return DoSetRange(mStartParent,mStartOffset,mStartParent,mStartOffset);
|
1998-12-03 09:51:05 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1998-12-18 02:51:34 +00:00
|
|
|
return DoSetRange(mEndParent,mEndOffset,mEndParent,mEndOffset);
|
1998-12-03 09:51:05 +00:00
|
|
|
}
|
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::Unposition()
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
return DoSetRange(nsCOMPtr<nsIDOMNode>(),0,nsCOMPtr<nsIDOMNode>(),0);
|
|
|
|
// note that "nsCOMPtr<nsIDOMmNode>()" is the moral equivalent of null
|
1998-12-03 09:51:05 +00:00
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::SelectNode(nsIDOMNode* aN)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
1999-03-08 01:20:02 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> theNode( do_QueryInterface(aN) );
|
1999-02-14 09:14:50 +00:00
|
|
|
|
|
|
|
nsresult res = aN->GetParentNode(getter_AddRefs(parent));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1999-02-14 09:14:50 +00:00
|
|
|
PRInt32 indx = IndexOf(theNode);
|
1998-12-18 02:51:34 +00:00
|
|
|
return DoSetRange(parent,indx,parent,indx+1);
|
1998-12-03 09:51:05 +00:00
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::SelectNodeContents(nsIDOMNode* aN)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
1999-03-08 01:20:02 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> theNode( do_QueryInterface(aN) );
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNodeList> aChildNodes;
|
|
|
|
|
|
|
|
nsresult res = aN->GetChildNodes(getter_AddRefs(aChildNodes));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-18 02:51:34 +00:00
|
|
|
return res;
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!aChildNodes)
|
1998-12-18 02:51:34 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
PRUint32 indx;
|
|
|
|
res = aChildNodes->GetLength(&indx);
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-18 02:51:34 +00:00
|
|
|
return res;
|
1999-02-14 09:14:50 +00:00
|
|
|
return DoSetRange(theNode,0,theNode,indx);
|
1998-12-03 09:51:05 +00:00
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::DeleteContents()
|
1998-12-08 02:26:15 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIContent> cStart;
|
|
|
|
nsCOMPtr<nsIContent> cEnd;
|
1998-12-08 02:26:15 +00:00
|
|
|
|
|
|
|
// get the content versions of our endpoints
|
1999-03-03 19:48:57 +00:00
|
|
|
nsresult res = mStartParent->QueryInterface(nsIContent::GetIID(), getter_AddRefs(cStart));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-08 02:26:15 +00:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsRange::DeleteContents");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
1999-03-03 19:48:57 +00:00
|
|
|
res = mEndParent->QueryInterface(nsIContent::GetIID(), getter_AddRefs(cEnd));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-08 02:26:15 +00:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsRange::DeleteContents");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
// effeciency hack for simple case
|
|
|
|
if (cStart == cEnd)
|
|
|
|
{
|
|
|
|
PRBool hasChildren;
|
|
|
|
res = cStart->CanContainChildren(hasChildren);
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-08 02:26:15 +00:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsRange::DeleteContents");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (hasChildren)
|
|
|
|
{
|
|
|
|
PRInt32 i;
|
1998-12-15 23:58:09 +00:00
|
|
|
for (i=mEndOffset; i>=mStartOffset; --i)
|
1998-12-08 02:26:15 +00:00
|
|
|
{
|
|
|
|
res = cStart->RemoveChildAt(i, PR_TRUE);
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-08 02:26:15 +00:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsRange::DeleteContents");
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-02-14 09:14:50 +00:00
|
|
|
else // textnode - offsets refer to data in node
|
1998-12-08 02:26:15 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMText> textNode;
|
1999-03-03 19:48:57 +00:00
|
|
|
res = mStartParent->QueryInterface(nsIDOMText::GetIID(), getter_AddRefs(textNode));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res)) // if it's not a text node, punt
|
1998-12-09 22:07:00 +00:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsRange::DeleteContents");
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
// delete the text
|
1999-01-29 18:57:56 +00:00
|
|
|
res = textNode->DeleteData(mStartOffset, mEndOffset - mStartOffset);
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-09 22:07:00 +00:00
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsRange::DeleteContents");
|
|
|
|
return res;
|
|
|
|
}
|
1999-01-29 18:57:56 +00:00
|
|
|
|
|
|
|
// collapse range endpoints; gravity should do this, but just in case ...
|
|
|
|
mEndOffset = mStartOffset;
|
|
|
|
|
1998-12-09 22:07:00 +00:00
|
|
|
return NS_OK;
|
1998-12-08 02:26:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-12-14 23:17:52 +00:00
|
|
|
/* complex case: cStart != cEnd
|
|
|
|
revisit - there are potential optimizations here and also tradeoffs.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// get start node ancestors
|
|
|
|
nsVoidArray startAncestorList;
|
1999-02-12 05:28:46 +00:00
|
|
|
|
1998-12-14 23:17:52 +00:00
|
|
|
FillArrayWithAncestors(&startAncestorList,mStartParent);
|
1999-02-12 05:28:46 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContentIterator> iter;
|
|
|
|
res = NS_NewContentIterator(getter_AddRefs(iter));
|
1999-05-13 02:43:30 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = iter->Init(this);
|
|
|
|
if (NS_FAILED(res)) return res;
|
1998-12-14 23:17:52 +00:00
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
|
|
|
|
// XXX Note that this chunk is also thread unsafe, since we
|
|
|
|
// aren't ADDREFing nodes as we put them on the delete list
|
|
|
|
// and then releasing them wehn we take them off
|
|
|
|
|
1998-12-14 23:17:52 +00:00
|
|
|
nsVoidArray deleteList;
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIContent> cN;
|
|
|
|
nsCOMPtr<nsIContent> cParent;
|
1998-12-14 23:17:52 +00:00
|
|
|
PRInt32 indx;
|
|
|
|
|
|
|
|
// loop through the content iterator, which returns nodes in the range in
|
|
|
|
// close tag order, and mark for deletion any node that is not an ancestor
|
|
|
|
// of the start node.
|
1999-05-14 06:18:07 +00:00
|
|
|
iter->CurrentNode(getter_AddRefs(cN));
|
|
|
|
while (cN && (NS_COMFALSE == iter->IsDone()))
|
1998-12-14 23:17:52 +00:00
|
|
|
{
|
|
|
|
// if node is not an ancestor of start node, delete it
|
|
|
|
if (mStartAncestors->IndexOf(NS_STATIC_CAST(void*,cN)) == -1)
|
|
|
|
{
|
|
|
|
deleteList.AppendElement(NS_STATIC_CAST(void*,cN));
|
|
|
|
}
|
1999-05-13 02:43:30 +00:00
|
|
|
res = iter->Next();
|
|
|
|
if (NS_FAILED(res)) // a little noise here to catch bugs
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsRange::DeleteContents() : iterator failed to advance");
|
|
|
|
return res;
|
|
|
|
}
|
1999-05-14 06:18:07 +00:00
|
|
|
iter->CurrentNode(getter_AddRefs(cN));
|
1998-12-14 23:17:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// remove the nodes on the delete list
|
|
|
|
while (deleteList.Count())
|
|
|
|
{
|
1999-02-18 23:55:10 +00:00
|
|
|
cN = do_QueryInterface(NS_STATIC_CAST(nsIContent*, deleteList.ElementAt(0)));
|
1999-02-14 09:14:50 +00:00
|
|
|
res = cN->GetParent(*getter_AddRefs(cParent));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1998-12-14 23:17:52 +00:00
|
|
|
res = cParent->IndexOf(cN,indx);
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1998-12-14 23:17:52 +00:00
|
|
|
res = cParent->RemoveChildAt(indx, PR_TRUE);
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1999-02-12 05:28:46 +00:00
|
|
|
deleteList.RemoveElementAt(0);
|
1998-12-14 23:17:52 +00:00
|
|
|
}
|
|
|
|
|
1999-02-12 05:28:46 +00:00
|
|
|
// If mStartParent is a text node, delete the text after start offset
|
|
|
|
nsIDOMText *textNode;
|
1999-03-03 19:48:57 +00:00
|
|
|
res = mStartParent->QueryInterface(nsIDOMText::GetIID(), (void**)&textNode);
|
1999-02-12 05:28:46 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
res = textNode->DeleteData(mStartOffset, 0xFFFFFFFF); // del to end
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1999-02-12 05:28:46 +00:00
|
|
|
return res; // XXX need to switch over to nsCOMPtr to avoid leaks here
|
|
|
|
}
|
|
|
|
|
|
|
|
// If mEndParent is a text node, delete the text before end offset
|
1999-03-03 19:48:57 +00:00
|
|
|
res = mEndParent->QueryInterface(nsIDOMText::GetIID(), (void**)&textNode);
|
1999-02-12 05:28:46 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
res = textNode->DeleteData(0, mEndOffset); // del from start
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1999-02-12 05:28:46 +00:00
|
|
|
return res; // XXX need to switch over to nsCOMPtr to avoid leaks here
|
|
|
|
}
|
|
|
|
|
1999-01-29 18:57:56 +00:00
|
|
|
// Collapse to start point:
|
|
|
|
mEndParent = mStartParent;
|
|
|
|
mEndOffset = mStartOffset;
|
|
|
|
|
1998-12-08 02:26:15 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-12-09 19:21:49 +00:00
|
|
|
nsresult nsRange::CompareEndPoints(PRUint16 how, nsIDOMRange* srcRange,
|
1998-12-10 18:58:49 +00:00
|
|
|
PRInt32* aCmpRet)
|
1998-12-09 18:44:26 +00:00
|
|
|
{
|
1998-12-09 19:21:49 +00:00
|
|
|
nsresult res;
|
1998-12-10 18:58:49 +00:00
|
|
|
if (aCmpRet == 0)
|
1998-12-09 18:44:26 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
if (srcRange == 0)
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> node1;
|
|
|
|
nsCOMPtr<nsIDOMNode> node2;
|
1998-12-09 19:21:49 +00:00
|
|
|
PRInt32 offset1, offset2;
|
1998-12-09 18:44:26 +00:00
|
|
|
switch (how)
|
|
|
|
{
|
|
|
|
case nsIDOMRange::START_TO_START:
|
1998-12-09 19:21:49 +00:00
|
|
|
node1 = mStartParent;
|
|
|
|
offset1 = mStartOffset;
|
1999-02-14 09:14:50 +00:00
|
|
|
res = srcRange->GetStartParent(getter_AddRefs(node2));
|
1998-12-09 19:21:49 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
res = srcRange->GetStartOffset(&offset2);
|
|
|
|
break;
|
1998-12-09 18:44:26 +00:00
|
|
|
case nsIDOMRange::START_TO_END:
|
1998-12-09 19:21:49 +00:00
|
|
|
node1 = mStartParent;
|
|
|
|
offset1 = mStartOffset;
|
1999-02-14 09:14:50 +00:00
|
|
|
res = srcRange->GetEndParent(getter_AddRefs(node2));
|
1998-12-09 19:21:49 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
res = srcRange->GetEndOffset(&offset2);
|
|
|
|
break;
|
1998-12-09 18:44:26 +00:00
|
|
|
case nsIDOMRange::END_TO_START:
|
1998-12-09 19:21:49 +00:00
|
|
|
node1 = mEndParent;
|
|
|
|
offset1 = mEndOffset;
|
1999-02-14 09:14:50 +00:00
|
|
|
res = srcRange->GetStartParent(getter_AddRefs(node2));
|
1998-12-09 19:21:49 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
res = srcRange->GetStartOffset(&offset2);
|
|
|
|
break;
|
1998-12-09 18:44:26 +00:00
|
|
|
case nsIDOMRange::END_TO_END:
|
1998-12-09 19:21:49 +00:00
|
|
|
node1 = mEndParent;
|
|
|
|
offset1 = mEndOffset;
|
1999-02-14 09:14:50 +00:00
|
|
|
res = srcRange->GetEndParent(getter_AddRefs(node2));
|
1998-12-09 19:21:49 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
res = srcRange->GetEndOffset(&offset2);
|
|
|
|
break;
|
|
|
|
|
1998-12-09 18:44:26 +00:00
|
|
|
default: // shouldn't get here
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
1998-12-09 19:21:49 +00:00
|
|
|
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-09 19:21:49 +00:00
|
|
|
return res;
|
|
|
|
|
|
|
|
if ((node1 == node2) && (offset1 == offset2))
|
1998-12-10 18:58:49 +00:00
|
|
|
*aCmpRet = 0;
|
1998-12-09 19:21:49 +00:00
|
|
|
else if (IsIncreasing(node1, offset2, node2, offset2))
|
1998-12-10 18:58:49 +00:00
|
|
|
*aCmpRet = 1;
|
1998-12-09 19:21:49 +00:00
|
|
|
else
|
1998-12-10 18:58:49 +00:00
|
|
|
*aCmpRet = -1;
|
1998-12-09 19:21:49 +00:00
|
|
|
return NS_OK;
|
1998-12-09 18:44:26 +00:00
|
|
|
}
|
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::ExtractContents(nsIDOMDocumentFragment** aReturn)
|
1998-12-09 18:44:26 +00:00
|
|
|
{
|
|
|
|
nsresult res = CloneContents(aReturn);
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-09 18:44:26 +00:00
|
|
|
return res;
|
|
|
|
res = DeleteContents();
|
|
|
|
return res;
|
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-12-15 18:47:44 +00:00
|
|
|
// CloneSibsAndParents(node, dogfrag, leftP)
|
|
|
|
// Assumes that the node passed in is already a clone.
|
|
|
|
// If node == 0, adds directly to doc fragment.
|
|
|
|
// Algorithm:
|
|
|
|
// - Loop from start child until common parent
|
|
|
|
// - clone parent (or use doc frag if parent == common parent)
|
|
|
|
// - add self & left or right right sibs' clones to parent
|
|
|
|
// - recurse to parent
|
|
|
|
//
|
|
|
|
nsresult
|
1999-02-14 09:14:50 +00:00
|
|
|
nsRange::CloneSibsAndParents(nsCOMPtr<nsIDOMNode> parentNode, PRInt32 nodeOffset,
|
|
|
|
nsCOMPtr<nsIDOMNode> clonedNode,
|
|
|
|
nsCOMPtr<nsIDOMNode> commonParent,
|
|
|
|
nsCOMPtr<nsIDOMDocumentFragment> docfrag,
|
1998-12-15 18:47:44 +00:00
|
|
|
PRBool leftP)
|
|
|
|
{
|
|
|
|
nsresult res;
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!docfrag)
|
1998-12-15 18:47:44 +00:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parentClone;
|
|
|
|
if (!parentNode)
|
1998-12-15 18:47:44 +00:00
|
|
|
{
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make clone of parent:
|
1999-02-14 09:14:50 +00:00
|
|
|
if (parentNode == commonParent || !parentNode)
|
1998-12-15 18:47:44 +00:00
|
|
|
{
|
1999-02-17 02:08:00 +00:00
|
|
|
parentClone = do_QueryInterface(docfrag);
|
1998-12-15 18:47:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
res = parentNode->CloneNode(PR_FALSE, getter_AddRefs(parentClone));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-15 18:47:44 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Loop over self and left or right siblings, and add to parent clone:
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> clone;
|
|
|
|
nsCOMPtr<nsIDOMNode> retNode; // To hold the useless return value of insertBefore
|
1998-12-15 18:47:44 +00:00
|
|
|
|
|
|
|
int i = 0; // or 1, depending on which base IndexOf uses
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> tmpNode;
|
|
|
|
res = parentNode->GetFirstChild(getter_AddRefs(tmpNode));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-15 18:47:44 +00:00
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
if (i == nodeOffset)
|
|
|
|
{
|
|
|
|
// If we weren't passed in a clone of node, make one now:
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!clonedNode)
|
1998-12-15 18:47:44 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
res = tmpNode->CloneNode(PR_TRUE, getter_AddRefs(clonedNode));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-15 18:47:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
res = parentClone->InsertBefore(clonedNode, (nsIDOMNode*)0, getter_AddRefs(retNode));
|
1998-12-15 18:47:44 +00:00
|
|
|
}
|
|
|
|
else if ((leftP && (i < nodeOffset))
|
|
|
|
|| (!leftP && (i > nodeOffset)))
|
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
res = tmpNode->CloneNode(PR_TRUE, getter_AddRefs(clone));
|
|
|
|
res = parentClone->InsertBefore(clone, (nsIDOMNode*)0, getter_AddRefs(retNode));
|
1998-12-15 18:47:44 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
res = NS_OK;
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-15 18:47:44 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
++i;
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> tmptmpNode = tmpNode;
|
|
|
|
res = tmpNode->GetNextSibling(getter_AddRefs(tmpNode));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res) || tmpNode == 0)
|
1998-12-15 18:47:44 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res)) // Probably broke out of the loop prematurely
|
1998-12-15 18:47:44 +00:00
|
|
|
return res;
|
|
|
|
|
|
|
|
// Recurse to parent:
|
|
|
|
tmpNode = parentNode;
|
1999-02-14 09:14:50 +00:00
|
|
|
res = tmpNode->GetParentNode(getter_AddRefs(parentNode));
|
1998-12-15 18:47:44 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
PRInt32 index = IndexOf(parentNode);
|
|
|
|
return CloneSibsAndParents(parentNode, index, parentClone,
|
|
|
|
commonParent, docfrag, leftP);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// XXX This is fine for left children but it will include too much
|
|
|
|
// XXX instead of stopping at the left children of the end node.
|
|
|
|
//
|
1998-12-18 02:51:34 +00:00
|
|
|
|
|
|
|
return res;
|
1998-12-15 18:47:44 +00:00
|
|
|
}
|
|
|
|
|
1998-12-09 18:44:26 +00:00
|
|
|
nsresult nsRange::CloneContents(nsIDOMDocumentFragment** aReturn)
|
1998-12-10 18:58:49 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
// XXX Not fully implemented XXX
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
|
|
|
|
#if 0
|
|
|
|
// partial implementation here
|
|
|
|
if (!aReturn)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1998-12-10 18:58:49 +00:00
|
|
|
if (!mIsPositioned)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> commonParent = CommonParent(mStartParent,mEndParent);
|
1998-12-10 18:58:49 +00:00
|
|
|
|
1998-12-15 18:47:44 +00:00
|
|
|
nsresult res;
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMDocument> document;
|
|
|
|
res = mStartParent->GetOwnerDocument(getter_AddRefs(document));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-10 18:58:49 +00:00
|
|
|
return res;
|
|
|
|
|
|
|
|
// Create a new document fragment in the context of this document
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMDocumentFragment> docfrag;
|
|
|
|
res = document->CreateDocumentFragment(getter_AddRefs(docfrag));
|
1998-12-10 18:58:49 +00:00
|
|
|
|
|
|
|
// XXX but who owns this doc frag -- when will it be released?
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
1998-12-15 18:47:44 +00:00
|
|
|
// Loop over the nodes contained in this Range,
|
|
|
|
// from the start and end points, and add them
|
|
|
|
// to the parent doc frag:
|
|
|
|
res = CloneSibsAndParents(mStartParent, mStartOffset, 0,
|
|
|
|
commonParent, docfrag, PR_TRUE);
|
|
|
|
res = CloneSibsAndParents(mEndParent, mEndOffset, 0,
|
|
|
|
commonParent, docfrag, PR_FALSE);
|
|
|
|
|
|
|
|
// XXX Now we need to add the sibs between the two top-level
|
|
|
|
// XXX doc frag cloned elements.
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
return NS_OK;
|
1998-12-10 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
*aReturn = docFrag;
|
1998-12-10 20:15:25 +00:00
|
|
|
return res;
|
1999-02-14 09:14:50 +00:00
|
|
|
#endif
|
1998-12-10 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsRange::Clone(nsIDOMRange** aReturn)
|
|
|
|
{
|
|
|
|
if (aReturn == 0)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult res = NS_NewRange(aReturn);
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-10 18:58:49 +00:00
|
|
|
return res;
|
|
|
|
|
1999-05-26 22:28:04 +00:00
|
|
|
res = (*aReturn)->SetStart(mStartParent, mStartOffset);
|
|
|
|
if (NS_FAILED(res))
|
|
|
|
return res;
|
|
|
|
|
|
|
|
res = (*aReturn)->SetEnd(mEndParent, mEndOffset);
|
|
|
|
return res;
|
1998-12-10 18:58:49 +00:00
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::InsertNode(nsIDOMNode* aN)
|
1998-11-24 21:20:11 +00:00
|
|
|
{ return NS_ERROR_NOT_IMPLEMENTED; }
|
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::SurroundContents(nsIDOMNode* aN)
|
1998-11-24 21:20:11 +00:00
|
|
|
{ return NS_ERROR_NOT_IMPLEMENTED; }
|
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::ToString(nsString& aReturn)
|
1998-12-15 04:42:29 +00:00
|
|
|
{
|
1999-03-09 19:21:04 +00:00
|
|
|
nsCOMPtr<nsIContent> cStart( do_QueryInterface(mStartParent) );
|
|
|
|
nsCOMPtr<nsIContent> cEnd( do_QueryInterface(mEndParent) );
|
1998-12-15 04:42:29 +00:00
|
|
|
|
|
|
|
// clear the string
|
|
|
|
aReturn.Truncate();
|
|
|
|
|
1999-05-11 20:22:35 +00:00
|
|
|
// If we're unpositioned, return the empty string
|
|
|
|
if (!cStart || !cEnd) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-03-09 19:21:04 +00:00
|
|
|
|
1998-12-15 04:42:29 +00:00
|
|
|
// effeciency hack for simple case
|
|
|
|
if (cStart == cEnd)
|
|
|
|
{
|
1999-03-09 19:21:04 +00:00
|
|
|
nsCOMPtr<nsIDOMText> textNode( do_QueryInterface(mStartParent) );
|
|
|
|
|
|
|
|
if (textNode)
|
1998-12-15 04:42:29 +00:00
|
|
|
{
|
1999-03-09 19:21:04 +00:00
|
|
|
// grab the text
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(textNode->SubstringData(mStartOffset,mEndOffset-mStartOffset,aReturn)))
|
1999-03-09 19:21:04 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
return NS_OK;
|
1998-12-15 04:42:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* complex case: cStart != cEnd, or cStart not a text node
|
|
|
|
revisit - there are potential optimizations here and also tradeoffs.
|
|
|
|
*/
|
1999-02-12 05:28:46 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContentIterator> iter;
|
1999-03-09 19:21:04 +00:00
|
|
|
NS_NewContentIterator(getter_AddRefs(iter));
|
1999-02-12 05:28:46 +00:00
|
|
|
iter->Init(this);
|
1998-12-15 04:42:29 +00:00
|
|
|
|
|
|
|
nsString tempString;
|
1999-03-09 19:21:04 +00:00
|
|
|
nsCOMPtr<nsIContent> cN;
|
1998-12-15 04:42:29 +00:00
|
|
|
|
|
|
|
// loop through the content iterator, which returns nodes in the range in
|
|
|
|
// close tag order, and grab the text from any text node
|
1999-05-14 06:18:07 +00:00
|
|
|
iter->CurrentNode(getter_AddRefs(cN));
|
|
|
|
while (cN && (NS_COMFALSE == iter->IsDone()))
|
1998-12-15 04:42:29 +00:00
|
|
|
{
|
1999-03-09 19:21:04 +00:00
|
|
|
nsCOMPtr<nsIDOMText> textNode( do_QueryInterface(cN) );
|
|
|
|
if (textNode) // if it's a text node, get the text
|
1998-12-15 04:42:29 +00:00
|
|
|
{
|
|
|
|
if (cN == cStart) // only include text past start offset
|
|
|
|
{
|
|
|
|
PRUint32 strLength;
|
|
|
|
textNode->GetLength(&strLength);
|
|
|
|
textNode->SubstringData(mStartOffset,strLength-mStartOffset,tempString);
|
|
|
|
aReturn += tempString;
|
|
|
|
}
|
|
|
|
else if (cN == cEnd) // only include text before end offset
|
|
|
|
{
|
|
|
|
textNode->SubstringData(0,mEndOffset,tempString);
|
|
|
|
aReturn += tempString;
|
|
|
|
}
|
|
|
|
else // grab the whole kit-n-kaboodle
|
|
|
|
{
|
|
|
|
textNode->GetData(tempString);
|
|
|
|
aReturn += tempString;
|
|
|
|
}
|
|
|
|
}
|
1999-05-14 06:18:07 +00:00
|
|
|
nsresult res = iter->Next();
|
1999-05-13 02:43:30 +00:00
|
|
|
if (NS_FAILED(res)) // a little noise here to catch bugs
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("nsRange::ToString() : iterator failed to advance");
|
|
|
|
return res;
|
|
|
|
}
|
1999-05-14 06:18:07 +00:00
|
|
|
iter->CurrentNode(getter_AddRefs(cN));
|
1998-12-15 04:42:29 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-12-18 09:28:55 +00:00
|
|
|
|
1998-12-30 08:28:16 +00:00
|
|
|
nsresult nsRange::OwnerGone(nsIContent* aDyingNode)
|
1998-12-18 09:28:55 +00:00
|
|
|
{
|
1999-02-14 09:14:50 +00:00
|
|
|
// nothing for now - should be impossible to getter here
|
|
|
|
// No node should be deleted if it holds a range endpoint,
|
|
|
|
// since the range endpoint addrefs the node.
|
1999-02-19 02:03:23 +00:00
|
|
|
NS_ASSERTION(PR_FALSE,"Deleted content holds a range endpoint");
|
1998-12-30 08:28:16 +00:00
|
|
|
return NS_OK;
|
1998-12-18 09:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult nsRange::OwnerChildInserted(nsIContent* aParentNode, PRInt32 aOffset)
|
|
|
|
{
|
1999-01-03 14:29:54 +00:00
|
|
|
// sanity check - null nodes shouldn't have enclosed ranges
|
|
|
|
if (!aParentNode) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
1999-03-08 01:20:02 +00:00
|
|
|
nsCOMPtr<nsIContent> parent( do_QueryInterface(aParentNode) );
|
1998-12-30 08:28:16 +00:00
|
|
|
// quick return if no range list
|
|
|
|
nsVoidArray *theRangeList;
|
1999-02-14 09:14:50 +00:00
|
|
|
parent->GetRangeList(theRangeList);
|
|
|
|
if (!theRangeList) return NS_OK;
|
1998-12-30 08:28:16 +00:00
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> domNode;
|
1998-12-30 08:28:16 +00:00
|
|
|
nsresult res;
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
res = GetDOMNodeFromContent(parent, &domNode);
|
1999-04-06 19:57:01 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1998-12-30 08:28:16 +00:00
|
|
|
if (!domNode) return NS_ERROR_UNEXPECTED;
|
1998-12-18 09:28:55 +00:00
|
|
|
|
1999-04-15 20:19:26 +00:00
|
|
|
PRInt32 loop = 0;
|
|
|
|
nsRange* theRange;
|
|
|
|
while ((theRange = NS_STATIC_CAST(nsRange*, (theRangeList->ElementAt(loop)))) != nsnull)
|
1998-12-18 09:28:55 +00:00
|
|
|
{
|
1998-12-30 08:28:16 +00:00
|
|
|
// sanity check - do range and content agree over ownership?
|
|
|
|
res = theRange->ContentOwnsUs(domNode);
|
|
|
|
NS_PRECONDITION(NS_SUCCEEDED(res), "range and content disagree over range ownership");
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
if (theRange->mStartParent == domNode)
|
|
|
|
{
|
|
|
|
// if child inserted before start, move start offset right one
|
|
|
|
if (aOffset <= theRange->mStartOffset) theRange->mStartOffset++;
|
|
|
|
}
|
|
|
|
if (theRange->mEndParent == domNode)
|
|
|
|
{
|
|
|
|
// if child inserted before end, move end offset right one
|
|
|
|
if (aOffset <= theRange->mEndOffset) theRange->mEndOffset++;
|
|
|
|
}
|
|
|
|
NS_PRECONDITION(NS_SUCCEEDED(res), "error updating range list");
|
|
|
|
}
|
|
|
|
loop++;
|
1998-12-18 09:28:55 +00:00
|
|
|
}
|
1998-12-30 08:28:16 +00:00
|
|
|
return NS_OK;
|
1998-12-18 09:28:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-12-30 08:28:16 +00:00
|
|
|
nsresult nsRange::OwnerChildRemoved(nsIContent* aParentNode, PRInt32 aOffset, nsIContent* aRemovedNode)
|
1998-12-18 09:28:55 +00:00
|
|
|
{
|
1999-01-03 14:29:54 +00:00
|
|
|
// sanity check - null nodes shouldn't have enclosed ranges
|
|
|
|
if (!aParentNode) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
1999-03-08 01:20:02 +00:00
|
|
|
nsCOMPtr<nsIContent> parent( do_QueryInterface(aParentNode) );
|
|
|
|
nsCOMPtr<nsIContent> removed( do_QueryInterface(aRemovedNode) );
|
1998-12-30 08:28:16 +00:00
|
|
|
// quick return if no range list
|
|
|
|
nsVoidArray *theRangeList;
|
1999-02-14 09:14:50 +00:00
|
|
|
parent->GetRangeList(theRangeList);
|
|
|
|
if (!theRangeList) return NS_OK;
|
1998-12-30 08:28:16 +00:00
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> domNode;
|
1998-12-30 08:28:16 +00:00
|
|
|
nsresult res;
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
res = GetDOMNodeFromContent(parent, &domNode);
|
1999-04-06 19:57:01 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1998-12-30 08:28:16 +00:00
|
|
|
if (!domNode) return NS_ERROR_UNEXPECTED;
|
1998-12-18 09:28:55 +00:00
|
|
|
|
1999-04-15 20:19:26 +00:00
|
|
|
PRInt32 loop = 0;
|
|
|
|
nsRange* theRange;
|
|
|
|
while ((theRange = NS_STATIC_CAST(nsRange*, (theRangeList->ElementAt(loop)))) != nsnull)
|
1998-12-18 09:28:55 +00:00
|
|
|
{
|
1998-12-30 08:28:16 +00:00
|
|
|
// sanity check - do range and content agree over ownership?
|
|
|
|
res = theRange->ContentOwnsUs(domNode);
|
|
|
|
NS_PRECONDITION(NS_SUCCEEDED(res), "range and content disagree over range ownership");
|
|
|
|
if (NS_SUCCEEDED(res))
|
1998-12-18 09:28:55 +00:00
|
|
|
{
|
1998-12-30 08:28:16 +00:00
|
|
|
if (theRange->mStartParent == domNode)
|
|
|
|
{
|
|
|
|
// if child deleted before start, move start offset left one
|
1999-05-26 22:28:04 +00:00
|
|
|
if (aOffset < theRange->mStartOffset) theRange->mStartOffset--;
|
1998-12-30 08:28:16 +00:00
|
|
|
}
|
|
|
|
if (theRange->mEndParent == domNode)
|
|
|
|
{
|
|
|
|
// if child deleted before end, move end offset left one
|
1999-05-26 22:28:04 +00:00
|
|
|
if (aOffset < theRange->mEndOffset)
|
1998-12-30 08:28:16 +00:00
|
|
|
{
|
|
|
|
if (theRange->mEndOffset>0) theRange->mEndOffset--;
|
|
|
|
}
|
|
|
|
}
|
1998-12-18 09:28:55 +00:00
|
|
|
}
|
1998-12-30 08:28:16 +00:00
|
|
|
loop++;
|
1998-12-18 09:28:55 +00:00
|
|
|
}
|
1998-12-30 08:28:16 +00:00
|
|
|
|
|
|
|
// any ranges in the content subtree rooted by aRemovedNode need to
|
|
|
|
// have the enclosed endpoints promoted up to the parentNode/offset
|
1999-02-14 09:14:50 +00:00
|
|
|
res = PopRanges(domNode, aOffset, removed);
|
1998-12-30 08:28:16 +00:00
|
|
|
|
1998-12-18 09:28:55 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1998-12-30 08:28:16 +00:00
|
|
|
nsresult nsRange::OwnerChildReplaced(nsIContent* aParentNode, PRInt32 aOffset, nsIContent* aReplacedNode)
|
1998-12-18 09:28:55 +00:00
|
|
|
{
|
1999-01-03 14:29:54 +00:00
|
|
|
// sanity check - null nodes shouldn't have enclosed ranges
|
|
|
|
if (!aParentNode) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
1998-12-30 08:28:16 +00:00
|
|
|
// don't need to adjust ranges whose endpoints are in this parent,
|
|
|
|
// but we do need to pop out any range endpoints inside the subtree
|
|
|
|
// rooted by aReplacedNode.
|
|
|
|
|
1999-03-08 01:20:02 +00:00
|
|
|
nsCOMPtr<nsIContent> parent( do_QueryInterface(aParentNode) );
|
|
|
|
nsCOMPtr<nsIContent> replaced( do_QueryInterface(aReplacedNode) );
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parentDomNode;
|
1998-12-30 08:28:16 +00:00
|
|
|
nsresult res;
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
res = GetDOMNodeFromContent(parent, &parentDomNode);
|
1999-04-06 19:57:01 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1998-12-30 08:28:16 +00:00
|
|
|
if (!parentDomNode) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
res = PopRanges(parentDomNode, aOffset, replaced);
|
1998-12-30 08:28:16 +00:00
|
|
|
|
|
|
|
return res;
|
1998-12-18 09:28:55 +00:00
|
|
|
}
|
|
|
|
|
1999-01-03 14:29:54 +00:00
|
|
|
|
|
|
|
nsresult nsRange::TextOwnerChanged(nsIContent* aTextNode, PRInt32 aStartChanged, PRInt32 aEndChanged, PRInt32 aReplaceLength)
|
|
|
|
{
|
|
|
|
// sanity check - null nodes shouldn't have enclosed ranges
|
|
|
|
if (!aTextNode) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
1999-03-08 01:20:02 +00:00
|
|
|
nsCOMPtr<nsIContent> textNode( do_QueryInterface(aTextNode) );
|
1999-01-03 14:29:54 +00:00
|
|
|
nsVoidArray *theRangeList;
|
|
|
|
aTextNode->GetRangeList(theRangeList);
|
|
|
|
// the caller already checked to see if there was a range list
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> domNode;
|
1999-01-03 14:29:54 +00:00
|
|
|
nsresult res;
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
res = GetDOMNodeFromContent(textNode, &domNode);
|
1999-04-06 19:57:01 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1999-01-03 14:29:54 +00:00
|
|
|
if (!domNode) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
1999-04-15 20:19:26 +00:00
|
|
|
PRInt32 loop = 0;
|
|
|
|
nsRange* theRange;
|
|
|
|
while ((theRange = NS_STATIC_CAST(nsRange*, (theRangeList->ElementAt(loop)))) != nsnull)
|
1999-01-03 14:29:54 +00:00
|
|
|
{
|
|
|
|
// sanity check - do range and content agree over ownership?
|
|
|
|
res = theRange->ContentOwnsUs(domNode);
|
|
|
|
NS_PRECONDITION(NS_SUCCEEDED(res), "range and content disagree over range ownership");
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
1999-02-19 02:03:23 +00:00
|
|
|
PRBool bStartPointInChangedText = PR_FALSE;
|
1999-01-03 14:29:54 +00:00
|
|
|
|
|
|
|
if (theRange->mStartParent == domNode)
|
|
|
|
{
|
|
|
|
// if range start is inside changed text, position it after change
|
|
|
|
if ((aStartChanged <= theRange->mStartOffset) && (aEndChanged >= theRange->mStartOffset))
|
|
|
|
{
|
|
|
|
theRange->mStartOffset = aStartChanged+aReplaceLength;
|
1999-02-19 02:03:23 +00:00
|
|
|
bStartPointInChangedText = PR_TRUE;
|
1999-01-03 14:29:54 +00:00
|
|
|
}
|
|
|
|
// else if text changed before start, adjust start offset
|
|
|
|
else if (aEndChanged <= theRange->mStartOffset)
|
|
|
|
theRange->mStartOffset += aStartChanged + aReplaceLength - aEndChanged;
|
|
|
|
}
|
|
|
|
if (theRange->mEndParent == domNode)
|
|
|
|
{
|
|
|
|
// if range end is inside changed text, position it before change
|
|
|
|
if ((aStartChanged <= theRange->mEndOffset) && (aEndChanged >= theRange->mEndOffset))
|
|
|
|
{
|
|
|
|
theRange->mEndOffset = aStartChanged;
|
|
|
|
// hack: if BOTH range endpoints were inside the change, then they
|
|
|
|
// both get collapsed to the beginning of the change.
|
|
|
|
if (bStartPointInChangedText) theRange->mStartOffset = aStartChanged;
|
|
|
|
}
|
|
|
|
// else if text changed before end, adjust end offset
|
|
|
|
else if (aEndChanged <= theRange->mEndOffset)
|
|
|
|
theRange->mEndOffset += aStartChanged + aReplaceLength - aEndChanged;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
loop++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-04-15 20:19:26 +00:00
|
|
|
|
1999-04-27 22:16:04 +00:00
|
|
|
|
|
|
|
// nsIDOMNSRange interface
|
|
|
|
NS_IMETHODIMP
|
1999-06-03 22:38:38 +00:00
|
|
|
nsRange::CreateContextualFragment(const nsString& aFragment,
|
|
|
|
nsIDOMDocumentFragment** aReturn)
|
1999-04-27 22:16:04 +00:00
|
|
|
{
|
1999-06-03 22:38:38 +00:00
|
|
|
nsresult result = NS_OK;
|
|
|
|
nsCOMPtr<nsIParser> parser;
|
|
|
|
nsITagStack* tagStack;
|
1999-04-27 22:16:04 +00:00
|
|
|
|
1999-06-03 22:38:38 +00:00
|
|
|
if (!mIsPositioned) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new parser for this entire operation
|
|
|
|
result = nsComponentManager::CreateInstance(kCParserCID,
|
|
|
|
nsnull,
|
|
|
|
kCParserIID,
|
|
|
|
(void **)getter_AddRefs(parser));
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = parser->CreateTagStack(&tagStack);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
nsCOMPtr<nsIContent> content(do_QueryInterface(mStartParent, &result));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
nsCOMPtr<nsIDocument> document;
|
|
|
|
|
|
|
|
result = content->GetDocument(*getter_AddRefs(document));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument(do_QueryInterface(document, &result));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
parent = mStartParent;
|
|
|
|
while (parent &&
|
|
|
|
(parent != domDocument) &&
|
|
|
|
NS_SUCCEEDED(result)) {
|
|
|
|
nsCOMPtr<nsIDOMNode> temp;
|
|
|
|
nsAutoString tagName;
|
|
|
|
PRUnichar* name = nsnull;
|
|
|
|
|
|
|
|
parent->GetNodeName(tagName);
|
|
|
|
// XXX Wish we didn't have to allocate here
|
|
|
|
name = tagName.ToNewUnicode();
|
|
|
|
if (nsnull != name) {
|
|
|
|
tagStack->Push(name);
|
|
|
|
temp = parent;
|
|
|
|
result = temp->GetParentNode(getter_AddRefs(parent));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
nsAutoString contentType;
|
|
|
|
nsIHTMLFragmentContentSink* sink;
|
|
|
|
|
|
|
|
result = NS_NewHTMLFragmentContentSink(&sink);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
parser->SetContentSink(sink);
|
|
|
|
document->GetContentType(contentType);
|
|
|
|
|
|
|
|
result = parser->ParseFragment(aFragment, (void*)0,
|
|
|
|
*tagStack,
|
|
|
|
0, contentType);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
sink->GetFragment(aReturn);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_RELEASE(sink);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX Ick! Delete strings we allocated above.
|
|
|
|
PRUnichar* str = nsnull;
|
|
|
|
str = tagStack->Pop();
|
|
|
|
while (nsnull != str) {
|
|
|
|
delete[] str;
|
|
|
|
str = tagStack->Pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX Double Ick! Deleting something that someone else newed.
|
|
|
|
delete tagStack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
1999-04-27 22:16:04 +00:00
|
|
|
}
|
|
|
|
|
1999-05-11 20:22:35 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::IsValidFragment(const nsString& aFragment, PRBool* aReturn)
|
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
nsCOMPtr<nsIParser> parser;
|
|
|
|
nsITagStack* tagStack;
|
|
|
|
|
|
|
|
if (!mIsPositioned) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new parser for this entire operation
|
|
|
|
result = nsComponentManager::CreateInstance(kCParserCID,
|
|
|
|
nsnull,
|
|
|
|
kCParserIID,
|
|
|
|
(void **)getter_AddRefs(parser));
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = parser->CreateTagStack(&tagStack);
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
nsCOMPtr<nsIContent> content(do_QueryInterface(mStartParent, &result));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
nsCOMPtr<nsIDocument> document;
|
|
|
|
|
|
|
|
result = content->GetDocument(*getter_AddRefs(document));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument(do_QueryInterface(document, &result));
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
parent = mStartParent;
|
|
|
|
while (parent &&
|
|
|
|
(parent != domDocument) &&
|
|
|
|
NS_SUCCEEDED(result)) {
|
|
|
|
nsCOMPtr<nsIDOMNode> temp;
|
|
|
|
nsAutoString tagName;
|
|
|
|
PRUnichar* name = nsnull;
|
|
|
|
|
|
|
|
parent->GetNodeName(tagName);
|
|
|
|
// XXX Wish we didn't have to allocate here
|
|
|
|
name = tagName.ToNewUnicode();
|
|
|
|
if (nsnull != name) {
|
|
|
|
tagStack->Push(name);
|
|
|
|
temp = parent;
|
|
|
|
result = temp->GetParentNode(getter_AddRefs(parent));
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
result = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
nsAutoString contentType;
|
|
|
|
|
|
|
|
document->GetContentType(contentType);
|
|
|
|
*aReturn = parser->IsValidFragment(aFragment,
|
|
|
|
*tagStack,
|
|
|
|
0, contentType);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX Ick! Delete strings we allocated above.
|
|
|
|
PRUnichar* str = nsnull;
|
|
|
|
str = tagStack->Pop();
|
|
|
|
while (nsnull != str) {
|
|
|
|
delete[] str;
|
|
|
|
str = tagStack->Pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX Double Ick! Deleting something that someone else newed.
|
|
|
|
delete tagStack;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-04-15 20:19:26 +00:00
|
|
|
// BEGIN nsIScriptContextOwner interface implementations
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::GetScriptObject(nsIScriptContext *aContext, void** aScriptObject)
|
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
nsIScriptGlobalObject *globalObj = aContext->GetGlobalObject();
|
|
|
|
|
|
|
|
if (nsnull == mScriptObject) {
|
|
|
|
res = NS_NewScriptRange(aContext, (nsISupports *)(nsIDOMRange *)this, globalObj, (void**)&mScriptObject);
|
|
|
|
}
|
|
|
|
*aScriptObject = mScriptObject;
|
|
|
|
|
|
|
|
NS_RELEASE(globalObj);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::SetScriptObject(void *aScriptObject)
|
|
|
|
{
|
|
|
|
mScriptObject = aScriptObject;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// END nsIScriptContextOwner interface implementations
|