2001-09-25 01:32:19 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1998-11-24 21:20:11 +00:00
|
|
|
*
|
2001-09-25 01:32:19 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
1998-11-24 21:20:11 +00:00
|
|
|
*
|
2001-09-25 01:32:19 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1998-11-24 21:20:11 +00:00
|
|
|
*
|
1999-11-06 03:40:37 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-25 01:32:19 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:40:37 +00:00
|
|
|
*
|
2001-09-25 01:32:19 +00:00
|
|
|
* Contributor(s):
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1998-11-24 21:20:11 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* nsRange.cpp: Implementation of the nsIDOMRange object.
|
|
|
|
*/
|
1998-11-24 21:24:40 +00:00
|
|
|
|
2001-08-14 07:59:59 +00:00
|
|
|
#include "nscore.h"
|
1998-12-18 02:51:34 +00:00
|
|
|
#include "nsRange.h"
|
|
|
|
|
2001-09-29 08:28:41 +00:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
1998-12-01 19:18:52 +00:00
|
|
|
#include "nsIDOMNode.h"
|
1998-12-07 17:53:59 +00:00
|
|
|
#include "nsIDOMDocument.h"
|
2001-09-24 22:43:23 +00:00
|
|
|
#include "nsIDOMNSDocument.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"
|
2000-01-27 02:51:51 +00:00
|
|
|
#include "nsDOMError.h"
|
1999-02-12 05:28:46 +00:00
|
|
|
#include "nsIContentIterator.h"
|
1998-12-18 02:51:34 +00:00
|
|
|
#include "nsIDOMNodeList.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"
|
2001-07-16 02:40:48 +00:00
|
|
|
#include "nsIEnumerator.h"
|
2003-08-22 03:06:53 +00:00
|
|
|
#include "nsScriptSecurityManager.h"
|
2001-07-09 23:02:53 +00:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIScriptContext.h"
|
2001-11-16 04:48:30 +00:00
|
|
|
#include "nsIHTMLDocument.h"
|
2002-05-15 18:55:21 +00:00
|
|
|
#include "nsCRT.h"
|
2001-07-09 23:02:53 +00:00
|
|
|
|
|
|
|
#include "nsIJSContextStack.h"
|
1999-06-03 22:38:38 +00:00
|
|
|
// XXX Temporary inclusion to deal with fragment parsing
|
|
|
|
#include "nsHTMLParts.h"
|
1998-11-24 21:20:11 +00:00
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 16:46:42 +00:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
|
1998-12-03 15:02:37 +00:00
|
|
|
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
2001-03-10 21:02:12 +00:00
|
|
|
static NS_DEFINE_CID(kCParserCID, NS_PARSER_CID);
|
1999-04-15 20:19:26 +00:00
|
|
|
|
1999-07-03 11:14:08 +00:00
|
|
|
PRMonitor* nsRange::mMonitor = nsnull;
|
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);
|
2002-03-18 14:38:11 +00:00
|
|
|
nsresult NS_NewContentSubtreeIterator(nsIContentIterator** aInstancePtrResult);
|
1999-07-03 11:14:08 +00:00
|
|
|
|
2001-04-28 12:02:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark utility functions (some exposed through nsRangeUtils, below
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
1999-07-03 11:14:08 +00:00
|
|
|
/******************************************************
|
|
|
|
* stack based utilty class for managing monitor
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
class nsAutoRangeLock
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
nsAutoRangeLock() { nsRange::Lock(); }
|
|
|
|
~nsAutoRangeLock() { nsRange::Unlock(); }
|
|
|
|
};
|
|
|
|
|
2003-02-04 04:19:05 +00:00
|
|
|
// NS_ERROR_DOM_NOT_OBJECT_ERR is not the correct one to throw, but spec doesn't say
|
|
|
|
// what is
|
|
|
|
#define VALIDATE_ACCESS(node_) \
|
|
|
|
PR_BEGIN_MACRO \
|
|
|
|
if (!node_) { \
|
|
|
|
return NS_ERROR_DOM_NOT_OBJECT_ERR; \
|
|
|
|
} \
|
|
|
|
if (!nsContentUtils::CanCallerAccess(node_)) { \
|
|
|
|
return NS_ERROR_DOM_SECURITY_ERR; \
|
|
|
|
} \
|
|
|
|
if (IsDetached()) { \
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR; \
|
|
|
|
} \
|
|
|
|
PR_END_MACRO
|
|
|
|
|
|
|
|
|
1999-07-03 11:14:08 +00:00
|
|
|
|
1998-12-18 02:51:34 +00:00
|
|
|
// Returns -1 if point1 < point2, 1, if point1 > point2,
|
2001-04-28 12:02:14 +00:00
|
|
|
// 0 if error or if point1 == point2.
|
1998-12-18 02:51:34 +00:00
|
|
|
PRInt32 ComparePoints(nsIDOMNode* aParent1, PRInt32 aOffset1,
|
|
|
|
nsIDOMNode* aParent2, PRInt32 aOffset2)
|
|
|
|
{
|
|
|
|
if (aParent1 == aParent2 && aOffset1 == aOffset2)
|
|
|
|
return 0;
|
1999-07-01 08:39:24 +00:00
|
|
|
nsIDOMRange* range;
|
|
|
|
if (NS_FAILED(NS_NewRange(&range)))
|
|
|
|
return 0;
|
1998-12-18 02:51:34 +00:00
|
|
|
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);
|
1999-07-01 08:39:24 +00:00
|
|
|
NS_RELEASE(range);
|
1998-12-18 02:51:34 +00:00
|
|
|
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
|
2000-12-09 04:46:08 +00:00
|
|
|
if (!GetNodeBracketPoints(aNode, address_of(parent), &nodeStart, &nodeEnd))
|
1999-02-19 02:03:23 +00:00
|
|
|
return PR_FALSE;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
2000-08-24 03:54:30 +00:00
|
|
|
if (NS_FAILED(aRange->GetStartContainer(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
|
|
|
|
2000-08-24 03:54:30 +00:00
|
|
|
if (NS_FAILED(aRange->GetEndContainer(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.
|
2001-04-28 12:02:14 +00:00
|
|
|
// XXX - callers responsibility to ensure node in same doc as range!
|
2003-01-14 23:05:52 +00:00
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
|
|
|
nsRange::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
|
2000-12-09 04:46:08 +00:00
|
|
|
if (!GetNodeBracketPoints(aNode, address_of(parent), &nodeStart, &nodeEnd))
|
1999-02-19 02:32:58 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
2000-08-24 03:54:30 +00:00
|
|
|
if (NS_FAILED(aRange->GetStartContainer(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
|
|
|
|
2000-08-24 03:54:30 +00:00
|
|
|
if (NS_FAILED(aRange->GetEndContainer(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
|
|
|
theDOMNode->GetParentNode(getter_AddRefs(*outParent));
|
2003-09-27 04:18:26 +00:00
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
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;
|
2003-09-27 04:18:26 +00:00
|
|
|
PRUint32 indx = cN->GetChildCount();
|
|
|
|
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-06-20 00:12:59 +00:00
|
|
|
*outStartOffset = nsRange::IndexOf(theDOMNode);
|
|
|
|
*outEndOffset = *outStartOffset+1;
|
1999-02-14 09:14:50 +00:00
|
|
|
}
|
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
|
|
|
|
2001-04-28 12:02:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark class nsRangeUtils
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* non members
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewRangeUtils(nsIRangeUtils** aResult)
|
|
|
|
{
|
2002-12-11 14:24:49 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
2001-04-28 12:02:14 +00:00
|
|
|
|
|
|
|
nsRangeUtils* rangeUtil = new nsRangeUtils();
|
2002-12-11 14:24:49 +00:00
|
|
|
if (!rangeUtil) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CallQueryInterface(rangeUtil, aResult);
|
2001-04-28 12:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* constructor/destructor
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
nsRangeUtils::nsRangeUtils()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRangeUtils::~nsRangeUtils()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* nsISupports
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF(nsRangeUtils)
|
|
|
|
NS_IMPL_RELEASE(nsRangeUtils)
|
|
|
|
|
|
|
|
nsresult nsRangeUtils::QueryInterface(const nsIID& aIID,
|
|
|
|
void** aInstancePtrResult)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aInstancePtrResult, "null pointer");
|
|
|
|
if (!aInstancePtrResult)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
if (aIID.Equals(kISupportsIID))
|
|
|
|
{
|
|
|
|
*aInstancePtrResult = (void*)(nsISupports*)(nsIRangeUtils *)this;
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aIID.Equals(NS_GET_IID(nsIRangeUtils)))
|
|
|
|
{
|
|
|
|
*aInstancePtrResult = (void*)(nsIRangeUtils*)this;
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* nsIRangeUtils methods
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(PRInt32)
|
|
|
|
nsRangeUtils::ComparePoints(nsIDOMNode* aParent1, PRInt32 aOffset1,
|
|
|
|
nsIDOMNode* aParent2, PRInt32 aOffset2)
|
|
|
|
{
|
|
|
|
return ::ComparePoints(aParent1, aOffset1, aParent2, aOffset2);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(PRBool)
|
|
|
|
nsRangeUtils::IsNodeIntersectsRange(nsIContent* aNode, nsIDOMRange* aRange)
|
|
|
|
{
|
|
|
|
return ::IsNodeIntersectsRange( aNode, aRange);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-01-14 23:05:52 +00:00
|
|
|
nsRangeUtils::CompareNodeToRange(nsIContent* aNode, nsIDOMRange* aRange,
|
|
|
|
PRBool *outNodeBefore, PRBool *outNodeAfter)
|
2001-04-28 12:02:14 +00:00
|
|
|
{
|
2003-01-14 23:05:52 +00:00
|
|
|
return nsRange::CompareNodeToRange(aNode, aRange, outNodeBefore,
|
|
|
|
outNodeAfter);
|
2001-04-28 12:02:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark class nsRange
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* non members
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewRange(nsIDOMRange** aResult)
|
|
|
|
{
|
2002-12-11 14:24:49 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aResult);
|
2001-04-28 12:02:14 +00:00
|
|
|
|
|
|
|
nsRange * range = new nsRange();
|
2002-12-11 14:24:49 +00:00
|
|
|
if (!range) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CallQueryInterface(range, aResult);
|
2001-04-28 12:02:14 +00:00
|
|
|
}
|
2002-12-11 14:24:49 +00:00
|
|
|
|
1998-12-03 15:02:37 +00:00
|
|
|
/******************************************************
|
|
|
|
* constructor/destructor
|
|
|
|
******************************************************/
|
1999-10-08 20:41:19 +00:00
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsRange::nsRange() :
|
|
|
|
mIsPositioned(PR_FALSE),
|
2001-01-09 21:44:35 +00:00
|
|
|
mIsDetached(PR_FALSE),
|
1999-02-14 09:14:50 +00:00
|
|
|
mStartOffset(0),
|
1999-04-15 20:19:26 +00:00
|
|
|
mEndOffset(0),
|
1999-04-27 22:14:17 +00:00
|
|
|
mStartParent(),
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 16:46:42 +00:00
|
|
|
mEndParent()
|
1998-12-03 22:59:07 +00:00
|
|
|
{
|
1998-11-25 01:21:42 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2000-10-29 21:38:52 +00:00
|
|
|
// for layout module destructor
|
|
|
|
void nsRange::Shutdown()
|
|
|
|
{
|
|
|
|
if (mMonitor) {
|
|
|
|
PR_DestroyMonitor(mMonitor);
|
|
|
|
mMonitor = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
delete mStartAncestors;
|
|
|
|
mStartAncestors = nsnull;
|
|
|
|
|
|
|
|
delete mEndAncestors;
|
|
|
|
mEndAncestors = nsnull;
|
|
|
|
|
|
|
|
delete mStartAncestorOffsets;
|
|
|
|
mStartAncestorOffsets = nsnull;
|
|
|
|
|
|
|
|
delete mEndAncestorOffsets;
|
|
|
|
mEndAncestorOffsets = nsnull;
|
|
|
|
}
|
|
|
|
|
1998-12-03 15:02:37 +00:00
|
|
|
/******************************************************
|
1999-10-08 20:41:19 +00:00
|
|
|
* nsISupports
|
1998-12-03 15:02:37 +00:00
|
|
|
******************************************************/
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 16:46:42 +00:00
|
|
|
|
|
|
|
|
|
|
|
// QueryInterface implementation for nsRange
|
|
|
|
NS_INTERFACE_MAP_BEGIN(nsRange)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMRange)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIDOMNSRange)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMRange)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_CONTENT_CLASSINFO(Range)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
NS_IMPL_ADDREF(nsRange)
|
|
|
|
NS_IMPL_RELEASE(nsRange)
|
|
|
|
|
1998-12-03 15:02:37 +00:00
|
|
|
|
2000-06-07 22:57:36 +00:00
|
|
|
/********************************************************
|
|
|
|
* Utilities for comparing points: API from nsIDOMNSRange
|
|
|
|
********************************************************/
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::IsPointInRange(nsIDOMNode* aParent, PRInt32 aOffset, PRBool* aResult)
|
|
|
|
{
|
|
|
|
PRInt16 compareResult = 0;
|
|
|
|
nsresult res;
|
|
|
|
res = ComparePoint(aParent, aOffset, &compareResult);
|
|
|
|
if (compareResult)
|
|
|
|
*aResult = PR_FALSE;
|
|
|
|
else
|
|
|
|
*aResult = PR_TRUE;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// returns -1 if point is before range, 0 if point is in range,
|
|
|
|
// 1 if point is after range.
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::ComparePoint(nsIDOMNode* aParent, PRInt32 aOffset, PRInt16* aResult)
|
|
|
|
{
|
|
|
|
// check arguments
|
|
|
|
if (!aResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// no trivial cases please
|
|
|
|
if (!aParent)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// our range is in a good state?
|
|
|
|
if (!mIsPositioned)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
// check common case first
|
|
|
|
if ((aParent == mStartParent.get()) && (aParent == mEndParent.get()))
|
|
|
|
{
|
|
|
|
if (aOffset<mStartOffset)
|
|
|
|
{
|
|
|
|
*aResult = -1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (aOffset>mEndOffset)
|
|
|
|
{
|
|
|
|
*aResult = 1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
*aResult = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// more common cases
|
|
|
|
if ((aParent == mStartParent.get()) && (aOffset == mStartOffset))
|
|
|
|
{
|
|
|
|
*aResult = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if ((aParent == mEndParent.get()) && (aOffset == mEndOffset))
|
|
|
|
{
|
|
|
|
*aResult = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// ok, do it the hard way
|
|
|
|
if (IsIncreasing(aParent,aOffset,mStartParent,mStartOffset))
|
|
|
|
*aResult = -1;
|
|
|
|
else if (IsIncreasing(mEndParent,mEndOffset,aParent,aOffset))
|
|
|
|
*aResult = 1;
|
|
|
|
else
|
|
|
|
*aResult = 0;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::IntersectsNode(nsIDOMNode* aNode, PRBool* aReturn)
|
|
|
|
{
|
|
|
|
if (!aReturn)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
nsCOMPtr<nsIContent> content (do_QueryInterface(aNode));
|
|
|
|
if (!content)
|
|
|
|
{
|
|
|
|
*aReturn = 0;
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
*aReturn = IsNodeIntersectsRange(content, this);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// HOW does the node intersect the range?
|
|
|
|
NS_IMETHODIMP
|
massive landing of joki changes.
Relevant nsbeta3+ bugs 43309, 44503, 2634, 2504,5981, 24698, 25758, 33577,
36062, 36217, 41191, 41491, 42356, 42829, 43016
r=saari (joki code). also been tested by heikki and bryner
2000-08-08 21:31:05 +00:00
|
|
|
nsRange::CompareNode(nsIDOMNode* aNode, PRUint16* aReturn)
|
2000-06-07 22:57:36 +00:00
|
|
|
{
|
|
|
|
if (!aReturn)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
*aReturn = 0;
|
|
|
|
PRBool nodeBefore, nodeAfter;
|
|
|
|
nsCOMPtr<nsIContent> content (do_QueryInterface(aNode));
|
|
|
|
if (!content)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
nsresult res = CompareNodeToRange(content, this, &nodeBefore, &nodeAfter);
|
|
|
|
if (NS_FAILED(res))
|
|
|
|
return res;
|
|
|
|
|
|
|
|
// nodeBefore -> range start after node start, i.e. node starts before range.
|
|
|
|
// nodeAfter -> range end before node end, i.e. node ends after range.
|
|
|
|
// But I know that I get nodeBefore && !nodeAfter when the node is
|
|
|
|
// entirely inside the selection! This doesn't make sense.
|
|
|
|
if (nodeBefore && !nodeAfter)
|
|
|
|
*aReturn = nsIDOMNSRange::NODE_BEFORE; // May or may not intersect
|
|
|
|
else if (!nodeBefore && nodeAfter)
|
|
|
|
*aReturn = nsIDOMNSRange::NODE_AFTER; // May or may not intersect
|
|
|
|
else if (nodeBefore && nodeAfter)
|
|
|
|
*aReturn = nsIDOMNSRange::NODE_BEFORE_AND_AFTER; // definitely intersects
|
|
|
|
else
|
|
|
|
*aReturn = nsIDOMNSRange::NODE_INSIDE; // definitely intersects
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-01-09 21:44:35 +00:00
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsRange::NSDetach()
|
|
|
|
{
|
|
|
|
return DoSetRange(nsnull,0,nsnull,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1998-12-03 15:02:37 +00:00
|
|
|
/******************************************************
|
|
|
|
* Private helper routines
|
|
|
|
******************************************************/
|
1998-12-03 22:59:07 +00:00
|
|
|
|
1999-06-20 23:02:48 +00:00
|
|
|
nsresult nsRange::AddToListOf(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;
|
|
|
|
|
2002-12-11 14:24:49 +00:00
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsIContent> cN = do_QueryInterface(aNode, &res);
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-18 02:51:34 +00:00
|
|
|
return res;
|
|
|
|
|
2001-12-11 09:03:38 +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-06-20 23:02:48 +00:00
|
|
|
nsresult nsRange::RemoveFromListOf(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;
|
|
|
|
|
2002-12-11 14:24:49 +00:00
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsIContent> cN = do_QueryInterface(aNode, &res);
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res))
|
1998-12-18 02:51:34 +00:00
|
|
|
return res;
|
|
|
|
|
2001-12-11 09:03:38 +00:00
|
|
|
res = cN->RangeRemove(NS_STATIC_CAST(nsIDOMRange*,this));
|
1998-12-18 02:51:34 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-08-23 18:02:45 +00:00
|
|
|
|
|
|
|
// Get the length of aNode
|
|
|
|
PRInt32 nsRange::GetNodeLength(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
if (!aNode)
|
|
|
|
return 0;
|
2003-09-27 04:18:26 +00:00
|
|
|
|
2002-08-23 18:02:45 +00:00
|
|
|
PRUint16 nodeType;
|
2003-09-27 04:18:26 +00:00
|
|
|
PRInt32 len = -1;
|
|
|
|
|
2002-08-23 18:02:45 +00:00
|
|
|
aNode->GetNodeType(&nodeType);
|
|
|
|
if( (nodeType == nsIDOMNode::CDATA_SECTION_NODE) ||
|
|
|
|
(nodeType == nsIDOMNode::TEXT_NODE) )
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMText> textText = do_QueryInterface(aNode);
|
|
|
|
if (textText)
|
2003-09-27 04:18:26 +00:00
|
|
|
textText->GetLength((PRUint32 *)&len);
|
2002-08-23 18:02:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNodeList> childList;
|
|
|
|
nsresult res = aNode->GetChildNodes(getter_AddRefs(childList));
|
|
|
|
if (NS_SUCCEEDED(res) && childList)
|
2003-09-27 04:18:26 +00:00
|
|
|
childList->GetLength((PRUint32 *)&len);
|
2002-08-23 18:02:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
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-06-20 23:02:48 +00:00
|
|
|
nsresult nsRange::DoSetRange(nsIDOMNode* aStartN, PRInt32 aStartOffset,
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1999-08-06 12:14:39 +00:00
|
|
|
if (mStartParent && (mStartParent.get() != aStartN) && (mStartParent.get() != aEndN))
|
1999-08-06 10:33:09 +00:00
|
|
|
{
|
|
|
|
// if old start parent no longer involved, remove range from that
|
|
|
|
// node's range list.
|
|
|
|
RemoveFromListOf(mStartParent);
|
|
|
|
}
|
1999-08-06 12:14:39 +00:00
|
|
|
if (mEndParent && (mEndParent.get() != aStartN) && (mEndParent.get() != aEndN))
|
1999-08-06 10:33:09 +00:00
|
|
|
{
|
|
|
|
// if old end parent no longer involved, remove range from that
|
|
|
|
// node's range list.
|
|
|
|
RemoveFromListOf(mEndParent);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-20 23:32:22 +00:00
|
|
|
if (mStartParent.get() != aStartN)
|
1998-12-04 18:21:52 +00:00
|
|
|
{
|
1999-06-20 23:32:22 +00:00
|
|
|
mStartParent = do_QueryInterface(aStartN);
|
1998-12-18 02:51:34 +00:00
|
|
|
if (mStartParent) // if it has a new start node, put it on it's list
|
|
|
|
{
|
1999-08-06 10:33:09 +00:00
|
|
|
AddToListOf(mStartParent); // AddToList() detects duplication for us
|
1998-12-18 02:51:34 +00:00
|
|
|
}
|
1998-12-04 18:21:52 +00:00
|
|
|
}
|
|
|
|
mStartOffset = aStartOffset;
|
1998-12-18 23:12:29 +00:00
|
|
|
|
1999-06-20 23:32:22 +00:00
|
|
|
if (mEndParent.get() != aEndN)
|
1998-12-04 18:21:52 +00:00
|
|
|
{
|
1999-06-20 23:32:22 +00:00
|
|
|
mEndParent = do_QueryInterface(aEndN);
|
1998-12-18 02:51:34 +00:00
|
|
|
if (mEndParent) // if it has a new end node, put it on it's list
|
|
|
|
{
|
1999-08-06 10:33:09 +00:00
|
|
|
AddToListOf(mEndParent); // AddToList() detects duplication for us
|
1998-12-18 02:51:34 +00:00
|
|
|
}
|
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-06-20 23:02:48 +00:00
|
|
|
PRBool nsRange::IsIncreasing(nsIDOMNode* aStartN, PRInt32 aStartOffset,
|
|
|
|
nsIDOMNode* aEndN, PRInt32 aEndOffset)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
2002-06-25 20:03:06 +00:00
|
|
|
PRInt32 startIdx = 0;
|
|
|
|
PRInt32 endIdx = 0;
|
1998-12-03 22:59:07 +00:00
|
|
|
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-07-03 11:14:08 +00:00
|
|
|
// thread safety - need locks around here to end of routine to protect use of static members
|
|
|
|
nsAutoRangeLock lock;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
1999-01-04 16:48:33 +00:00
|
|
|
// lazy allocation of static arrays
|
|
|
|
if (!mStartAncestors)
|
|
|
|
{
|
2001-09-06 19:16:03 +00:00
|
|
|
mStartAncestors = new nsAutoVoidArray();
|
1999-07-25 08:41:42 +00:00
|
|
|
if (!mStartAncestors) return NS_ERROR_OUT_OF_MEMORY;
|
2001-09-06 19:16:03 +00:00
|
|
|
mStartAncestorOffsets = new nsAutoVoidArray();
|
1999-07-25 08:41:42 +00:00
|
|
|
if (!mStartAncestorOffsets) return NS_ERROR_OUT_OF_MEMORY;
|
2001-09-06 19:16:03 +00:00
|
|
|
mEndAncestors = new nsAutoVoidArray();
|
1999-07-25 08:41:42 +00:00
|
|
|
if (!mEndAncestors) return NS_ERROR_OUT_OF_MEMORY;
|
2001-09-06 19:16:03 +00:00
|
|
|
mEndAncestorOffsets = new nsAutoVoidArray();
|
1999-07-25 08:41:42 +00:00
|
|
|
if (!mEndAncestorOffsets) return NS_ERROR_OUT_OF_MEMORY;
|
1999-01-04 16:48:33 +00:00
|
|
|
}
|
|
|
|
|
1998-12-14 23:17:52 +00:00
|
|
|
// refresh ancestor data
|
|
|
|
mStartAncestors->Clear();
|
|
|
|
mStartAncestorOffsets->Clear();
|
|
|
|
mEndAncestors->Clear();
|
|
|
|
mEndAncestorOffsets->Clear();
|
|
|
|
|
2002-06-25 20:03:06 +00:00
|
|
|
nsContentUtils::GetAncestorsAndOffsets(aStartN, aStartOffset,
|
|
|
|
mStartAncestors, mStartAncestorOffsets);
|
|
|
|
|
|
|
|
nsContentUtils::GetAncestorsAndOffsets(aEndN, aEndOffset,
|
|
|
|
mEndAncestors, mEndAncestorOffsets);
|
|
|
|
|
|
|
|
// Get the number of ancestors, adjusting for zero-based counting.
|
|
|
|
startIdx = mStartAncestors->Count() - 1;
|
|
|
|
endIdx = mEndAncestors->Count() - 1;
|
|
|
|
|
2002-09-11 01:58:50 +00:00
|
|
|
// Ensure that we actually have ancestors to iterate through
|
|
|
|
if (startIdx < 0) {
|
|
|
|
if (startIdx < endIdx) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
if (endIdx < 0) {
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
1998-12-03 22:59:07 +00:00
|
|
|
// back through the ancestors, starting from the root, until first non-matching ancestor found
|
2002-06-25 20:03:06 +00:00
|
|
|
while (startIdx >= 0 && endIdx >= 0 &&
|
|
|
|
mStartAncestors->ElementAt(startIdx) == mEndAncestors->ElementAt(endIdx))
|
1998-12-03 22:59:07 +00:00
|
|
|
{
|
2002-06-25 20:03:06 +00:00
|
|
|
--startIdx;
|
|
|
|
--endIdx;
|
2001-12-21 01:10:07 +00:00
|
|
|
// numStartAncestors will only be <0 if one endpoint's node is the
|
|
|
|
// common ancestor of the other
|
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
|
2002-06-25 20:03:06 +00:00
|
|
|
++startIdx;
|
|
|
|
++endIdx;
|
2001-12-21 01:10:07 +00:00
|
|
|
// both indexes are now >= 0
|
2002-06-25 20:03:06 +00:00
|
|
|
commonNodeStartOffset = NS_PTR_TO_INT32(mStartAncestorOffsets->ElementAt(startIdx));
|
|
|
|
commonNodeEndOffset = NS_PTR_TO_INT32(mEndAncestorOffsets->ElementAt(endIdx));
|
|
|
|
|
|
|
|
if (commonNodeStartOffset > commonNodeEndOffset) {
|
1999-02-14 09:14:50 +00:00
|
|
|
return PR_FALSE;
|
2002-06-25 20:03:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (commonNodeStartOffset < commonNodeEndOffset) {
|
1999-02-14 09:14:50 +00:00
|
|
|
return PR_TRUE;
|
1999-03-09 19:21:04 +00:00
|
|
|
}
|
2002-06-25 20:03:06 +00:00
|
|
|
|
|
|
|
// 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 (startIdx == endIdx) {
|
|
|
|
// whoa nelly. this shouldn't happen.
|
|
|
|
NS_NOTREACHED("nsRange::IsIncreasing");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (startIdx < endIdx) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return PR_FALSE;
|
1998-12-03 09:51:05 +00:00
|
|
|
}
|
1998-11-26 02:30:44 +00:00
|
|
|
|
1999-06-20 00:12:59 +00:00
|
|
|
PRInt32 nsRange::IndexOf(nsIDOMNode* aChildNode)
|
1998-12-03 15:02:37 +00:00
|
|
|
{
|
2003-09-27 04:18:26 +00:00
|
|
|
// convert node to nsIContent, so that we can find the child index
|
1998-12-03 15:02:37 +00:00
|
|
|
|
2003-09-27 04:18:26 +00:00
|
|
|
nsCOMPtr<nsIContent> contentChild = do_QueryInterface(aChildNode);
|
|
|
|
if (!contentChild)
|
1998-12-03 15:02:37 +00:00
|
|
|
return 0;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
2003-09-27 04:18:26 +00:00
|
|
|
nsIContent *parent = contentChild->GetParent();
|
|
|
|
|
|
|
|
if (!parent)
|
1998-12-03 15:02:37 +00:00
|
|
|
return 0;
|
1999-02-14 09:14:50 +00:00
|
|
|
|
2003-09-27 04:18:26 +00:00
|
|
|
// finally we get the index
|
|
|
|
return parent->IndexOf(contentChild);
|
1998-12-03 15:02:37 +00:00
|
|
|
}
|
|
|
|
|
1999-06-20 23:02:48 +00:00
|
|
|
nsresult nsRange::PopRanges(nsIDOMNode* aDestNode, PRInt32 aOffset, 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));
|
1999-10-28 03:16:48 +00:00
|
|
|
while (cN && (NS_ENUMERATOR_FALSE == iter->IsDone()))
|
1998-12-30 08:28:16 +00:00
|
|
|
{
|
2003-06-13 20:10:01 +00:00
|
|
|
cN->GetRangeList(&theRangeList);
|
1998-12-30 08:28:16 +00:00
|
|
|
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
|
|
|
{
|
2002-07-10 05:10:09 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> domNode(do_QueryInterface(cN));
|
1999-06-16 21:38:51 +00:00
|
|
|
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-07-25 05:31:05 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
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-07-25 05:31:05 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
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!
|
2003-06-13 20:10:01 +00:00
|
|
|
cN->GetRangeList(&theRangeList);
|
1999-04-08 06:00:44 +00:00
|
|
|
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-06-20 23:02:48 +00:00
|
|
|
nsresult nsRange::ContentOwnsUs(nsIDOMNode* domNode)
|
1998-12-30 08:28:16 +00:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(domNode, "null pointer");
|
1999-06-20 23:32:22 +00:00
|
|
|
if ((mStartParent.get() != domNode) && (mEndParent.get() != 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
|
|
|
|
2000-08-24 03:54:30 +00:00
|
|
|
nsresult nsRange::GetStartContainer(nsIDOMNode** aStartParent)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
|
|
|
if (!mIsPositioned)
|
2001-01-07 15:11:50 +00:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1998-12-03 09:51:05 +00:00
|
|
|
if (!aStartParent)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2003-04-15 01:09:09 +00:00
|
|
|
|
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)
|
2001-01-07 15:11:50 +00:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1998-12-03 09:51:05 +00:00
|
|
|
if (!aStartOffset)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
*aStartOffset = mStartOffset;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
2000-08-24 03:54:30 +00:00
|
|
|
nsresult nsRange::GetEndContainer(nsIDOMNode** aEndParent)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
|
|
|
if (!mIsPositioned)
|
2001-01-07 15:11:50 +00:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1998-12-03 09:51:05 +00:00
|
|
|
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)
|
2001-01-07 15:11:50 +00:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1998-12-03 09:51:05 +00:00
|
|
|
if (!aEndOffset)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
*aEndOffset = mEndOffset;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
2000-08-24 03:54:30 +00:00
|
|
|
nsresult nsRange::GetCollapsed(PRBool* aIsCollapsed)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
2001-01-09 21:44:35 +00:00
|
|
|
if(IsDetached())
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
1998-12-03 09:51:05 +00:00
|
|
|
if (!mIsPositioned)
|
2001-01-07 15:11:50 +00:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2001-01-09 21:44:35 +00:00
|
|
|
|
1998-12-03 09:51:05 +00:00
|
|
|
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
|
|
|
|
2000-08-24 03:54:30 +00:00
|
|
|
nsresult nsRange::GetCommonAncestorContainer(nsIDOMNode** aCommonParent)
|
1998-12-03 15:02:37 +00:00
|
|
|
{
|
2001-01-09 21:44:35 +00:00
|
|
|
if(IsDetached())
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
|
2002-06-25 20:03:06 +00:00
|
|
|
return nsContentUtils::GetCommonAncestor(mStartParent, mEndParent, aCommonParent);
|
1998-12-03 15:02:37 +00:00
|
|
|
}
|
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
|
|
|
{
|
2003-02-04 04:19:05 +00:00
|
|
|
VALIDATE_ACCESS(aParent);
|
|
|
|
|
2002-08-23 18:02:45 +00:00
|
|
|
PRInt32 len = GetNodeLength(aParent);
|
|
|
|
if ( (aOffset < 0) || (len < 0) || (aOffset > len) )
|
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
|
2002-06-25 20:03:06 +00:00
|
|
|
if (mIsPositioned) {
|
|
|
|
// if not in the same document as the endpoint,
|
|
|
|
// collapse the endpoint to the new start.
|
|
|
|
if (!nsContentUtils::InSameDoc(aParent, mEndParent)) {
|
|
|
|
return DoSetRange(aParent, aOffset, aParent, aOffset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the start must be before the end
|
|
|
|
if (!IsIncreasing(aParent, aOffset, mEndParent, mEndOffset)) {
|
|
|
|
return NS_ERROR_ILLEGAL_VALUE;
|
|
|
|
}
|
1998-12-03 09:51:05 +00:00
|
|
|
}
|
2002-06-25 20:03:06 +00:00
|
|
|
|
1998-12-04 18:21:52 +00:00
|
|
|
// if it's in an attribute node, end must be in or descended from same node
|
2002-06-25 20:03:06 +00:00
|
|
|
// XXX write me!
|
|
|
|
|
|
|
|
return DoSetRange(aParent, aOffset, mEndParent, mEndOffset);
|
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
|
|
|
{
|
2003-02-04 04:19:05 +00:00
|
|
|
VALIDATE_ACCESS(aSibling);
|
|
|
|
|
2000-05-24 23:01:09 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> nParent;
|
|
|
|
nsresult res = aSibling->GetParentNode(getter_AddRefs(nParent));
|
|
|
|
if (NS_FAILED(res) || !nParent) return NS_ERROR_DOM_RANGE_INVALID_NODE_TYPE_ERR;
|
|
|
|
PRInt32 indx = IndexOf(aSibling);
|
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
|
|
|
{
|
2003-02-04 04:19:05 +00:00
|
|
|
VALIDATE_ACCESS(aSibling);
|
2001-01-09 21:44:35 +00:00
|
|
|
|
2000-05-24 23:01:09 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> nParent;
|
|
|
|
nsresult res = aSibling->GetParentNode(getter_AddRefs(nParent));
|
|
|
|
if (NS_FAILED(res) || !nParent) return NS_ERROR_DOM_RANGE_INVALID_NODE_TYPE_ERR;
|
|
|
|
PRInt32 indx = IndexOf(aSibling) + 1;
|
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
|
|
|
{
|
2003-02-04 04:19:05 +00:00
|
|
|
VALIDATE_ACCESS(aParent);
|
|
|
|
|
2002-08-23 18:02:45 +00:00
|
|
|
PRInt32 len = GetNodeLength(aParent);
|
|
|
|
if ( (aOffset < 0) || (len < 0) || (aOffset > len) )
|
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
|
1998-12-04 18:21:52 +00:00
|
|
|
nsresult res;
|
|
|
|
|
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.
|
2002-06-25 20:03:06 +00:00
|
|
|
if (mIsPositioned && !nsContentUtils::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
|
|
|
{
|
2003-02-04 04:19:05 +00:00
|
|
|
VALIDATE_ACCESS(aSibling);
|
|
|
|
|
2000-05-24 23:01:09 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> nParent;
|
|
|
|
nsresult res = aSibling->GetParentNode(getter_AddRefs(nParent));
|
|
|
|
if (NS_FAILED(res) || !nParent) return NS_ERROR_DOM_RANGE_INVALID_NODE_TYPE_ERR;
|
|
|
|
PRInt32 indx = IndexOf(aSibling);
|
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
|
|
|
{
|
2003-02-04 04:19:05 +00:00
|
|
|
VALIDATE_ACCESS(aSibling);
|
|
|
|
|
2000-05-24 23:01:09 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> nParent;
|
|
|
|
nsresult res = aSibling->GetParentNode(getter_AddRefs(nParent));
|
|
|
|
if (NS_FAILED(res) || !nParent) return NS_ERROR_DOM_RANGE_INVALID_NODE_TYPE_ERR;
|
|
|
|
PRInt32 indx = IndexOf(aSibling) + 1;
|
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
|
|
|
{
|
2001-01-09 21:44:35 +00:00
|
|
|
if(IsDetached())
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
1998-12-03 09:51:05 +00:00
|
|
|
if (!mIsPositioned)
|
2001-01-07 15:11:50 +00:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1998-12-03 09:51:05 +00:00
|
|
|
|
|
|
|
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::SelectNode(nsIDOMNode* aN)
|
1998-12-03 09:51:05 +00:00
|
|
|
{
|
2003-02-04 04:19:05 +00:00
|
|
|
VALIDATE_ACCESS(aN);
|
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
2001-03-23 09:29:51 +00:00
|
|
|
PRInt32 start, end;
|
2000-05-11 01:44:44 +00:00
|
|
|
|
|
|
|
PRUint16 type = 0;
|
|
|
|
aN->GetNodeType(&type);
|
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case nsIDOMNode::ATTRIBUTE_NODE :
|
|
|
|
case nsIDOMNode::ENTITY_NODE :
|
|
|
|
case nsIDOMNode::DOCUMENT_NODE :
|
|
|
|
case nsIDOMNode::DOCUMENT_FRAGMENT_NODE :
|
|
|
|
case nsIDOMNode::NOTATION_NODE :
|
|
|
|
return NS_ERROR_DOM_RANGE_INVALID_NODE_TYPE_ERR;
|
|
|
|
}
|
|
|
|
|
2001-03-23 09:29:51 +00:00
|
|
|
nsresult res;
|
|
|
|
res = aN->GetParentNode(getter_AddRefs(parent));
|
|
|
|
if(NS_SUCCEEDED(res) && parent)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMDocument> doc(do_QueryInterface(parent));
|
|
|
|
if(doc)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent>content(do_QueryInterface(aN));
|
|
|
|
if(!content)
|
|
|
|
return NS_ERROR_DOM_RANGE_INVALID_NODE_TYPE_ERR;
|
|
|
|
parent = aN;//parent is now equal to the node you passed in
|
|
|
|
// which is the root. start is zero, end is the number of children
|
|
|
|
start = 0;
|
2003-09-27 04:18:26 +00:00
|
|
|
end = content->GetChildCount();
|
2001-03-23 09:29:51 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
start = IndexOf(aN);
|
|
|
|
end = start + 1;
|
|
|
|
}
|
|
|
|
return DoSetRange(parent,start,parent,end);
|
|
|
|
}
|
|
|
|
return NS_ERROR_DOM_RANGE_INVALID_NODE_TYPE_ERR;
|
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
|
|
|
{
|
2003-02-04 04:19:05 +00:00
|
|
|
VALIDATE_ACCESS(aN);
|
|
|
|
|
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
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark class RangeSubtreeIterator
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
2001-01-09 21:44:35 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
// The Subtree Content Iterator only returns subtrees that are
|
|
|
|
// completely within a given range. It doesn't return a CharacterData
|
|
|
|
// node that contains either the start or end point of the range.
|
|
|
|
// We need an iterator that will also include these start/end points
|
|
|
|
// so that our methods/algorithms aren't cluttered with special
|
|
|
|
// case code that tries to include these points while iterating.
|
|
|
|
//
|
|
|
|
// The RangeSubtreeIterator class mimics the nsIContentIterator
|
|
|
|
// methods we need, so should the Content Iterator support the
|
|
|
|
// start/end points in the future, we can switchover relatively
|
|
|
|
// easy.
|
|
|
|
|
|
|
|
class RangeSubtreeIterator
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
enum RangeSubtreeIterState { eDone=0,
|
|
|
|
eUseStartCData,
|
|
|
|
eUseIterator,
|
|
|
|
eUseEndCData };
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentIterator> mIter;
|
|
|
|
RangeSubtreeIterState mIterState;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> mStartCData;
|
|
|
|
nsCOMPtr<nsIDOMNode> mEndCData;
|
|
|
|
|
|
|
|
public:
|
|
|
|
|
|
|
|
RangeSubtreeIterator() : mIterState(eDone) {}
|
|
|
|
~RangeSubtreeIterator() {}
|
|
|
|
|
|
|
|
nsresult Init(nsIDOMRange *aRange)
|
1998-12-08 02:26:15 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aRange);
|
|
|
|
|
|
|
|
mIterState = eDone;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
|
|
|
|
// Grab the start point of the range and QI it to
|
|
|
|
// a CharacterData pointer. If it is CharacterData store
|
|
|
|
// a pointer to the node.
|
|
|
|
|
|
|
|
nsresult res = aRange->GetStartContainer(getter_AddRefs(node));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!node) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> cData = do_QueryInterface(node);
|
|
|
|
if (cData)
|
|
|
|
mStartCData = node;
|
|
|
|
|
|
|
|
// Grab the end point of the range and QI it to
|
|
|
|
// a CharacterData pointer. If it is CharacterData store
|
|
|
|
// a pointer to the node.
|
|
|
|
|
|
|
|
res = aRange->GetEndContainer(getter_AddRefs(node));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!node) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
cData = do_QueryInterface(node);
|
|
|
|
if (cData)
|
|
|
|
mEndCData = node;
|
|
|
|
|
|
|
|
if (mStartCData && mStartCData == mEndCData)
|
|
|
|
{
|
|
|
|
// The range starts and stops in the same CharacterData
|
|
|
|
// node. Null out the end pointer so we only visit the
|
|
|
|
// node once!
|
|
|
|
|
|
|
|
mEndCData = nsnull;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Now create a Content Subtree Iterator to be used
|
|
|
|
// for the subtrees between the end points!
|
|
|
|
|
|
|
|
res = NS_NewContentSubtreeIterator(getter_AddRefs(mIter));
|
|
|
|
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!mIter) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
res = mIter->Init(aRange);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (mIter->IsDone() != NS_ENUMERATOR_FALSE)
|
|
|
|
{
|
|
|
|
// The subtree iterator thinks there's nothing
|
|
|
|
// to iterate over, so just free it up so we
|
|
|
|
// don't accidentally call into it.
|
|
|
|
|
|
|
|
mIter = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the iterator by calling First().
|
|
|
|
// Note that we are ignoring the return value on purpose!
|
|
|
|
|
|
|
|
(void)First();
|
|
|
|
|
|
|
|
return NS_OK;
|
1998-12-08 02:26:15 +00:00
|
|
|
}
|
2002-03-18 14:38:11 +00:00
|
|
|
|
|
|
|
nsresult CurrentNode(nsIDOMNode **aNode)
|
1998-12-08 02:26:15 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aNode);
|
|
|
|
|
|
|
|
*aNode = nsnull;
|
|
|
|
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
if (mIterState == eUseStartCData && mStartCData)
|
|
|
|
*aNode = mStartCData;
|
|
|
|
else if (mIterState == eUseEndCData && mEndCData)
|
|
|
|
*aNode = mEndCData;
|
|
|
|
else if (mIterState == eUseIterator && mIter)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
res = mIter->CurrentNode(getter_AddRefs(content));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!content) return NS_ERROR_FAILURE;
|
|
|
|
nsCOMPtr<nsIDOMNode> node(do_QueryInterface(content));
|
|
|
|
if (!node) return NS_ERROR_FAILURE;
|
|
|
|
*aNode = node;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
res = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
NS_IF_ADDREF(*aNode);
|
|
|
|
|
|
|
|
return res;
|
1998-12-08 02:26:15 +00:00
|
|
|
}
|
2002-03-18 14:38:11 +00:00
|
|
|
|
|
|
|
nsresult First()
|
1998-12-08 02:26:15 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
if (mStartCData)
|
|
|
|
mIterState = eUseStartCData;
|
|
|
|
else if (mIter)
|
1998-12-08 02:26:15 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
res = mIter->First();
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
mIterState = eUseIterator;
|
1998-12-08 02:26:15 +00:00
|
|
|
}
|
2002-03-18 14:38:11 +00:00
|
|
|
else if (mEndCData)
|
|
|
|
mIterState = eUseEndCData;
|
|
|
|
else
|
|
|
|
res = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Last()
|
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
if (mEndCData)
|
|
|
|
mIterState = eUseEndCData;
|
|
|
|
else if (mIter)
|
1998-12-08 02:26:15 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
res = mIter->Last();
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
mIterState = eUseIterator;
|
1998-12-08 02:26:15 +00:00
|
|
|
}
|
2002-03-18 14:38:11 +00:00
|
|
|
else if (mStartCData)
|
|
|
|
mIterState = eUseStartCData;
|
|
|
|
else
|
|
|
|
res = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Next()
|
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
if (mIterState == eUseStartCData)
|
1998-12-08 02:26:15 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
if (mIter)
|
1998-12-09 22:07:00 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
res = mIter->First();
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
mIterState = eUseIterator;
|
1998-12-09 22:07:00 +00:00
|
|
|
}
|
2002-03-18 14:38:11 +00:00
|
|
|
else if (mEndCData)
|
|
|
|
mIterState = eUseEndCData;
|
|
|
|
else
|
|
|
|
mIterState = eDone;
|
|
|
|
}
|
|
|
|
else if (mIterState == eUseIterator)
|
|
|
|
{
|
|
|
|
res = mIter->Next();
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (mIter->IsDone() != NS_ENUMERATOR_FALSE)
|
1998-12-09 22:07:00 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
if (mEndCData)
|
|
|
|
mIterState = eUseEndCData;
|
|
|
|
else
|
|
|
|
mIterState = eDone;
|
1998-12-09 22:07:00 +00:00
|
|
|
}
|
1998-12-08 02:26:15 +00:00
|
|
|
}
|
2002-03-18 14:38:11 +00:00
|
|
|
else if (mIterState == eUseEndCData)
|
|
|
|
mIterState = eDone;
|
|
|
|
else
|
|
|
|
res = NS_ERROR_FAILURE;
|
1998-12-14 23:17:52 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Prev()
|
1998-12-14 23:17:52 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
if (mIterState == eUseEndCData)
|
1998-12-14 23:17:52 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
if (mIter)
|
|
|
|
{
|
|
|
|
res = mIter->Last();
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
mIterState = eUseIterator;
|
|
|
|
}
|
|
|
|
else if (mStartCData)
|
|
|
|
mIterState = eUseStartCData;
|
|
|
|
else
|
|
|
|
mIterState = eDone;
|
1998-12-14 23:17:52 +00:00
|
|
|
}
|
2002-03-18 14:38:11 +00:00
|
|
|
else if (mIterState == eUseIterator)
|
1999-05-13 02:43:30 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
res = mIter->Prev();
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (mIter->IsDone() != NS_ENUMERATOR_FALSE)
|
|
|
|
{
|
|
|
|
if (mStartCData)
|
|
|
|
mIterState = eUseStartCData;
|
|
|
|
else
|
|
|
|
mIterState = eDone;
|
|
|
|
}
|
1999-05-13 02:43:30 +00:00
|
|
|
}
|
2002-03-18 14:38:11 +00:00
|
|
|
else if (mIterState == eUseStartCData)
|
|
|
|
mIterState = eDone;
|
|
|
|
else
|
|
|
|
res = NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return res;
|
1998-12-14 23:17:52 +00:00
|
|
|
}
|
2002-03-18 14:38:11 +00:00
|
|
|
|
|
|
|
PRBool IsDone()
|
1998-12-14 23:17:52 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
return mIterState == eDone;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// CollapseRangeAfterDelete() is a utiltiy method that is used by
|
|
|
|
// DeleteContents() and ExtractContents() to collapse the range
|
|
|
|
// in the correct place, under the range's root container (the
|
|
|
|
// range end points common container) as outlined by the Range spec:
|
|
|
|
//
|
|
|
|
// http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113/ranges.html
|
|
|
|
// The assumption made by this method is that the delete or extract
|
|
|
|
// has been done already, and left the range in a state where there is
|
|
|
|
// no content between the 2 end points.
|
|
|
|
|
|
|
|
nsresult nsRange::CollapseRangeAfterDelete(nsIDOMRange *aRange)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRange);
|
|
|
|
|
|
|
|
// Check if range gravity took care of collapsing the range for us!
|
|
|
|
|
|
|
|
PRBool isCollapsed = PR_FALSE;
|
|
|
|
nsresult res = aRange->GetCollapsed(&isCollapsed);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (isCollapsed)
|
|
|
|
{
|
|
|
|
// aRange is collapsed so there's nothing for us to do.
|
|
|
|
//
|
|
|
|
// There are 2 possible scenarios here:
|
|
|
|
//
|
|
|
|
// 1. aRange could've been collapsed prior to the delete/extract,
|
|
|
|
// which would've resulted in nothing being removed, so aRange
|
|
|
|
// is already where it should be.
|
|
|
|
//
|
|
|
|
// 2. Prior to the delete/extract, aRange's start and end were in
|
|
|
|
// the same container which would mean everything between them
|
|
|
|
// was removed, causing range gravity to collapse the range.
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// aRange isn't collapsed so figure out the appropriate place to collapse!
|
|
|
|
// First get both end points and their common ancestor.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> commonAncestor;
|
|
|
|
res = aRange->GetCommonAncestorContainer(getter_AddRefs(commonAncestor));
|
|
|
|
if(NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> startContainer, endContainer;
|
|
|
|
|
|
|
|
res = aRange->GetStartContainer(getter_AddRefs(startContainer));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
res = aRange->GetEndContainer(getter_AddRefs(endContainer));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// Collapse to one of the end points if they are already in the
|
|
|
|
// commonAncestor. This should work ok since this method is called
|
|
|
|
// immediately after a delete or extract that leaves no content
|
|
|
|
// between the 2 end points!
|
|
|
|
|
|
|
|
if (startContainer == commonAncestor)
|
|
|
|
return aRange->Collapse(PR_TRUE);
|
|
|
|
if (endContainer == commonAncestor)
|
|
|
|
return aRange->Collapse(PR_FALSE);
|
|
|
|
|
|
|
|
// End points are at differing levels. We want to collapse to the
|
|
|
|
// point that is between the 2 subtrees that contain each point,
|
|
|
|
// under the common ancestor.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> nodeToSelect(startContainer), parent;
|
|
|
|
|
|
|
|
while (nodeToSelect)
|
|
|
|
{
|
|
|
|
nsresult res = nodeToSelect->GetParentNode(getter_AddRefs(parent));
|
1999-05-10 23:29:01 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2002-03-18 14:38:11 +00:00
|
|
|
|
|
|
|
if (parent == commonAncestor)
|
|
|
|
break; // We found the nodeToSelect!
|
|
|
|
|
|
|
|
nodeToSelect = parent;
|
1998-12-14 23:17:52 +00:00
|
|
|
}
|
2002-03-18 14:38:11 +00:00
|
|
|
|
|
|
|
if (!nodeToSelect)
|
|
|
|
return NS_ERROR_FAILURE; // This should never happen!
|
|
|
|
|
|
|
|
res = aRange->SelectNode(nodeToSelect);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
return aRange->Collapse(PR_FALSE);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult nsRange::DeleteContents()
|
|
|
|
{
|
|
|
|
if(IsDetached())
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
|
|
|
|
// Save the range end points locally to avoid interference
|
|
|
|
// of Range gravity during our edits!
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> startContainer(mStartParent);
|
|
|
|
PRInt32 startOffset = mStartOffset;
|
|
|
|
nsCOMPtr<nsIDOMNode> endContainer(mEndParent);
|
|
|
|
PRInt32 endOffset = mEndOffset;
|
|
|
|
|
|
|
|
// Create and initialize a subtree iterator that will give
|
|
|
|
// us all the subtrees within the range.
|
|
|
|
|
|
|
|
RangeSubtreeIterator iter;
|
|
|
|
|
|
|
|
nsresult res = iter.Init(this);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (iter.IsDone())
|
1999-02-12 05:28:46 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
// There's nothing for us to delete.
|
|
|
|
return CollapseRangeAfterDelete(this);
|
1999-02-12 05:28:46 +00:00
|
|
|
}
|
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
// We delete backwards to avoid iterator problems!
|
|
|
|
|
|
|
|
res = iter.Last();
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
res = iter.CurrentNode(getter_AddRefs(node));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!node) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
PRBool handled = PR_FALSE;
|
|
|
|
|
|
|
|
// With the exception of text nodes that contain one of the range
|
|
|
|
// end points, the subtree iterator should only give us back subtrees
|
|
|
|
// that are completely contained between the range's end points.
|
|
|
|
|
|
|
|
while (node)
|
1999-02-12 05:28:46 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
// Before we delete anything, advance the iterator to the
|
|
|
|
// next subtree.
|
|
|
|
|
|
|
|
res = iter.Prev();
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
handled = PR_FALSE;
|
|
|
|
|
|
|
|
// If it's CharacterData, make sure we might need to delete
|
|
|
|
// part of the data, instead of removing the whole node.
|
|
|
|
//
|
|
|
|
// XXX_kin: We need to also handle ProcessingInstruction
|
|
|
|
// XXX_kin: according to the spec.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> charData(do_QueryInterface(node));
|
|
|
|
|
|
|
|
if (charData)
|
|
|
|
{
|
|
|
|
PRUint32 dataLength = 0;
|
|
|
|
|
|
|
|
if (node == startContainer)
|
|
|
|
{
|
|
|
|
if (node == endContainer)
|
|
|
|
{
|
|
|
|
// This range is completely contained within a single text node.
|
|
|
|
// Delete the data between startOffset and endOffset.
|
|
|
|
|
|
|
|
if (endOffset > startOffset)
|
|
|
|
{
|
|
|
|
res = charData->DeleteData(startOffset, endOffset - startOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
handled = PR_TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Delete everything after startOffset.
|
|
|
|
|
|
|
|
res = charData->GetLength(&dataLength);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (dataLength > (PRUint32)startOffset)
|
|
|
|
{
|
|
|
|
res = charData->DeleteData(startOffset, dataLength - startOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
handled = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (node == endContainer)
|
|
|
|
{
|
|
|
|
// Delete the data between 0 and endOffset.
|
|
|
|
|
|
|
|
if (endOffset > 0)
|
|
|
|
{
|
|
|
|
res = charData->DeleteData(0, endOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
handled = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!handled)
|
|
|
|
{
|
|
|
|
// node was not handled above, so it must be completely contained
|
|
|
|
// within the range. Just remove it from the tree!
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> parent, tmpNode;
|
|
|
|
|
|
|
|
res = node->GetParentNode(getter_AddRefs(parent));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
res = parent->RemoveChild(node, getter_AddRefs(tmpNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iter.IsDone())
|
|
|
|
break; // We must be done!
|
|
|
|
|
|
|
|
res = iter.CurrentNode(getter_AddRefs(node));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!node) return NS_ERROR_FAILURE;
|
1999-02-12 05:28:46 +00:00
|
|
|
}
|
1999-01-29 18:57:56 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
// XXX_kin: At this point we should be checking for the case
|
|
|
|
// XXX_kin: where we have 2 adjacent text nodes left, each
|
|
|
|
// XXX_kin: containing one of the range end points. The spec
|
|
|
|
// XXX_kin: says the 2 nodes should be merged in that case,
|
|
|
|
// XXX_kin: and to use Normalize() to do the merging, but
|
|
|
|
// XXX_kin: calling Normalize() on the common parent to accomplish
|
|
|
|
// XXX_kin: this might also normalize nodes that are outside the
|
|
|
|
// XXX_kin: range but under the common parent. Need to verify
|
|
|
|
// XXX_kin: with the range commitee members that this was the
|
|
|
|
// XXX_kin: desired behavior. For now we don't merge anything!
|
|
|
|
|
|
|
|
return CollapseRangeAfterDelete(this);
|
1998-12-08 02:26:15 +00:00
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
2003-01-14 23:05:52 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::CompareBoundaryPoints(PRUint16 how, nsIDOMRange* srcRange,
|
|
|
|
PRInt16* aCmpRet)
|
1998-12-09 18:44:26 +00:00
|
|
|
{
|
2001-01-09 21:44:35 +00:00
|
|
|
if(IsDetached())
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
|
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;
|
|
|
|
|
2000-12-27 00:37:01 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> boundaryNode; // the Invoking range
|
|
|
|
nsCOMPtr<nsIDOMNode> sourceNode; // the sourceRange
|
|
|
|
PRInt32 boundaryOffset, sourceOffset;
|
|
|
|
|
1998-12-09 18:44:26 +00:00
|
|
|
switch (how)
|
|
|
|
{
|
2000-12-27 00:37:01 +00:00
|
|
|
case nsIDOMRange::START_TO_START: // where is the start point of boundary range
|
|
|
|
boundaryNode = mStartParent; // relative to the start point of the source range?
|
|
|
|
boundaryOffset = mStartOffset;
|
|
|
|
res = srcRange->GetStartContainer(getter_AddRefs(sourceNode));
|
1998-12-09 19:21:49 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
2000-12-27 00:37:01 +00:00
|
|
|
res = srcRange->GetStartOffset(&sourceOffset);
|
1998-12-09 19:21:49 +00:00
|
|
|
break;
|
2000-12-27 00:37:01 +00:00
|
|
|
case nsIDOMRange::START_TO_END: // where is the end point of the boundary range
|
|
|
|
boundaryNode = mEndParent; // relative to the start point of source range?
|
|
|
|
boundaryOffset = mEndOffset;
|
|
|
|
res = srcRange->GetStartContainer(getter_AddRefs(sourceNode));
|
1998-12-09 19:21:49 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
2000-12-27 00:37:01 +00:00
|
|
|
res = srcRange->GetStartOffset(&sourceOffset);
|
1998-12-09 19:21:49 +00:00
|
|
|
break;
|
2000-12-27 00:37:01 +00:00
|
|
|
case nsIDOMRange::END_TO_START: // where is the the start point of the boundary range
|
|
|
|
boundaryNode = mStartParent; // relative to end point of source range?
|
|
|
|
boundaryOffset = mStartOffset;
|
|
|
|
res = srcRange->GetEndContainer(getter_AddRefs(sourceNode));
|
1998-12-09 19:21:49 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
2000-12-27 00:37:01 +00:00
|
|
|
res = srcRange->GetEndOffset(&sourceOffset);
|
1998-12-09 19:21:49 +00:00
|
|
|
break;
|
2000-12-27 00:37:01 +00:00
|
|
|
case nsIDOMRange::END_TO_END: // where is the end point of boundary range
|
|
|
|
boundaryNode = mEndParent; // relative to the end point of the source range?
|
|
|
|
boundaryOffset = mEndOffset;
|
|
|
|
res = srcRange->GetEndContainer(getter_AddRefs(sourceNode));
|
1998-12-09 19:21:49 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
2000-12-27 00:37:01 +00:00
|
|
|
res = srcRange->GetEndOffset(&sourceOffset);
|
1998-12-09 19:21:49 +00:00
|
|
|
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;
|
|
|
|
|
2000-12-27 00:37:01 +00:00
|
|
|
if ((boundaryNode == sourceNode) && (boundaryOffset == sourceOffset))
|
|
|
|
*aCmpRet = 0;//then the points are equal
|
|
|
|
else if (IsIncreasing(boundaryNode, boundaryOffset, sourceNode, sourceOffset))
|
|
|
|
*aCmpRet = -1;//then boundary point is before source point
|
1998-12-09 19:21:49 +00:00
|
|
|
else
|
2000-12-27 00:37:01 +00:00
|
|
|
*aCmpRet = 1;//then boundary point is after source point
|
|
|
|
|
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
|
|
|
{
|
2001-01-09 21:44:35 +00:00
|
|
|
if(IsDetached())
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
// XXX_kin: The spec says that nodes that are completely in the
|
|
|
|
// XXX_kin: range should be moved into the document fragment, not
|
|
|
|
// XXX_kin: copied. This method will have to be rewritten using
|
|
|
|
// XXX_kin: DeleteContents() as a template, with the charData cloning
|
|
|
|
// XXX_kin: code from CloneContents() merged in.
|
|
|
|
|
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
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
nsresult nsRange::CloneParentsBetween(nsIDOMNode *aAncestor,
|
|
|
|
nsIDOMNode *aNode,
|
|
|
|
nsIDOMNode **aClosestAncestor,
|
|
|
|
nsIDOMNode **aFarthestAncestor)
|
1998-12-15 18:47:44 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
NS_ENSURE_ARG_POINTER((aAncestor && aNode && aClosestAncestor && aFarthestAncestor));
|
1998-12-15 18:47:44 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
*aClosestAncestor = nsnull;
|
|
|
|
*aFarthestAncestor = nsnull;
|
1998-12-15 18:47:44 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
if (aAncestor == aNode)
|
|
|
|
return NS_OK;
|
1998-12-15 18:47:44 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parent, firstParent, lastParent;
|
1998-12-15 18:47:44 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
nsresult res = aNode->GetParentNode(getter_AddRefs(parent));
|
1998-12-15 18:47:44 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
while(parent && parent != aAncestor)
|
1998-12-15 18:47:44 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> clone, tmpNode;
|
1998-12-15 18:47:44 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
res = parent->CloneNode(PR_FALSE, getter_AddRefs(clone));
|
1998-12-15 18:47:44 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!clone) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
if (! firstParent)
|
|
|
|
firstParent = lastParent = clone;
|
|
|
|
else
|
1998-12-15 18:47:44 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
res = clone->AppendChild(lastParent, getter_AddRefs(tmpNode));
|
|
|
|
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
lastParent = clone;
|
1998-12-15 18:47:44 +00:00
|
|
|
}
|
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
tmpNode = parent;
|
|
|
|
res = tmpNode->GetParentNode(getter_AddRefs(parent));
|
1998-12-15 18:47:44 +00:00
|
|
|
}
|
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
*aClosestAncestor = firstParent;
|
|
|
|
NS_IF_ADDREF(*aClosestAncestor);
|
1998-12-15 18:47:44 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
*aFarthestAncestor = lastParent;
|
|
|
|
NS_IF_ADDREF(*aFarthestAncestor);
|
1998-12-15 18:47:44 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
return NS_OK;
|
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
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
if (IsDetached())
|
2001-01-09 21:44:35 +00:00
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
1998-12-10 18:58:49 +00:00
|
|
|
|
1998-12-15 18:47:44 +00:00
|
|
|
nsresult res;
|
2001-05-18 21:05:51 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> commonAncestor;
|
|
|
|
res = GetCommonAncestorContainer(getter_AddRefs(commonAncestor));
|
2002-03-18 14:38:11 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2001-05-18 21:05:51 +00:00
|
|
|
|
1999-02-14 09:14:50 +00:00
|
|
|
nsCOMPtr<nsIDOMDocument> document;
|
|
|
|
res = mStartParent->GetOwnerDocument(getter_AddRefs(document));
|
2001-05-18 21:05:51 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1998-12-10 18:58:49 +00:00
|
|
|
|
2001-07-21 02:53:43 +00:00
|
|
|
// Create a new document fragment in the context of this document,
|
|
|
|
// which might be null
|
2002-03-18 14:38:11 +00:00
|
|
|
|
2001-05-18 21:05:51 +00:00
|
|
|
nsCOMPtr<nsIDOMDocumentFragment> clonedFrag;
|
2001-07-21 02:53:43 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc(do_QueryInterface(document));
|
|
|
|
|
|
|
|
res = NS_NewDocumentFragment(getter_AddRefs(clonedFrag), doc);
|
2001-05-18 21:05:51 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> commonCloneAncestor(do_QueryInterface(clonedFrag));
|
|
|
|
if (!commonCloneAncestor) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Create and initialize a subtree iterator that will give
|
|
|
|
// us all the subtrees within the range.
|
|
|
|
|
|
|
|
RangeSubtreeIterator iter;
|
|
|
|
|
|
|
|
res = iter.Init(this);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (iter.IsDone())
|
1998-12-10 18:58:49 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
// There's nothing to add to the doc frag, we must be done!
|
|
|
|
|
2001-05-18 21:05:51 +00:00
|
|
|
*aReturn = clonedFrag;
|
2002-03-18 14:38:11 +00:00
|
|
|
NS_IF_ADDREF(*aReturn);
|
2001-05-18 21:05:51 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-12-15 18:47:44 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
res = iter.First();
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
res = iter.CurrentNode(getter_AddRefs(node));
|
2001-05-18 21:05:51 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2002-03-18 14:38:11 +00:00
|
|
|
if (!node) return NS_ERROR_FAILURE;
|
|
|
|
|
1998-12-15 18:47:44 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
// With the exception of text nodes that contain one of the range
|
|
|
|
// end points, the subtree iterator should only give us back subtrees
|
|
|
|
// that are completely contained between the range's end points.
|
|
|
|
//
|
|
|
|
// Unfortunately these subtrees don't contain the parent hierarchy/context
|
|
|
|
// that the Range spec requires us to return. This loop clones the
|
|
|
|
// parent hierarchy, adds a cloned version of the subtree, to it, then
|
|
|
|
// correctly places this new subtree into the doc fragment.
|
2001-06-26 20:45:18 +00:00
|
|
|
|
2002-03-18 14:38:11 +00:00
|
|
|
while (node)
|
2001-05-18 21:05:51 +00:00
|
|
|
{
|
2002-03-18 14:38:11 +00:00
|
|
|
// Clone the current subtree!
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> clone;
|
|
|
|
res = node->CloneNode(PR_TRUE, getter_AddRefs(clone));
|
|
|
|
|
2001-05-18 21:05:51 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2002-03-18 14:38:11 +00:00
|
|
|
if (!clone) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// If it's CharacterData, make sure we only clone what
|
|
|
|
// is in the range.
|
|
|
|
//
|
|
|
|
// XXX_kin: We need to also handle ProcessingInstruction
|
|
|
|
// XXX_kin: according to the spec.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> charData(do_QueryInterface(clone));
|
|
|
|
|
|
|
|
if (charData)
|
|
|
|
{
|
|
|
|
if (node == mEndParent)
|
|
|
|
{
|
|
|
|
// We only need the data before mEndOffset, so get rid of any
|
|
|
|
// data after it.
|
|
|
|
|
|
|
|
PRUint32 dataLength = 0;
|
|
|
|
res = charData->GetLength(&dataLength);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (dataLength > (PRUint32)mEndOffset)
|
|
|
|
{
|
|
|
|
res = charData->DeleteData(mEndOffset, dataLength - mEndOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (node == mStartParent)
|
|
|
|
{
|
|
|
|
// We don't need any data before mStartOffset, so just
|
|
|
|
// delete it!
|
|
|
|
|
|
|
|
if (mStartOffset > 0)
|
|
|
|
{
|
|
|
|
res = charData->DeleteData(0, mStartOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone the parent hierarchy between commonAncestor and node.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> closestAncestor, farthestAncestor;
|
|
|
|
|
|
|
|
res = CloneParentsBetween(commonAncestor, node,
|
|
|
|
getter_AddRefs(closestAncestor),
|
|
|
|
getter_AddRefs(farthestAncestor));
|
|
|
|
|
2001-05-18 21:05:51 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2002-03-18 14:38:11 +00:00
|
|
|
|
|
|
|
// Hook the parent hierarchy/context of the subtree into the clone tree.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> tmpNode;
|
|
|
|
|
|
|
|
if (farthestAncestor)
|
|
|
|
{
|
|
|
|
res = commonCloneAncestor->AppendChild(farthestAncestor,
|
|
|
|
getter_AddRefs(tmpNode));
|
|
|
|
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Place the cloned subtree into the cloned doc frag tree!
|
|
|
|
|
|
|
|
if (closestAncestor)
|
|
|
|
{
|
|
|
|
// Append the subtree under closestAncestor since it is the
|
|
|
|
// immediate parent of the subtree.
|
|
|
|
|
|
|
|
res = closestAncestor->AppendChild(clone, getter_AddRefs(tmpNode));
|
|
|
|
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If we get here, there is no missing parent hierarchy between
|
|
|
|
// commonAncestor and node, so just append clone to commonCloneAncestor.
|
|
|
|
|
|
|
|
res = commonCloneAncestor->AppendChild(clone, getter_AddRefs(tmpNode));
|
|
|
|
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the next subtree to be processed. The idea here is to setup
|
|
|
|
// the parameters for the next iteration of the loop.
|
|
|
|
|
|
|
|
res = iter.Next();
|
|
|
|
|
|
|
|
if (iter.IsDone())
|
|
|
|
break; // We must be done!
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> nextNode;
|
|
|
|
res = iter.CurrentNode(getter_AddRefs(nextNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!nextNode) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Get node and nextNode's common parent.
|
2002-06-25 20:03:06 +00:00
|
|
|
nsContentUtils::GetCommonAncestor(node, nextNode, getter_AddRefs(commonAncestor));
|
2002-03-18 14:38:11 +00:00
|
|
|
|
|
|
|
if (!commonAncestor)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Find the equivalent of commonAncestor in the cloned tree!
|
|
|
|
|
|
|
|
while (node && node != commonAncestor)
|
|
|
|
{
|
|
|
|
tmpNode = node;
|
|
|
|
res = tmpNode->GetParentNode(getter_AddRefs(node));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!node) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
tmpNode = clone;
|
|
|
|
res = tmpNode->GetParentNode(getter_AddRefs(clone));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!node) return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
commonCloneAncestor = clone;
|
|
|
|
node = nextNode;
|
2001-05-18 21:05:51 +00:00
|
|
|
}
|
2001-06-26 20:45:18 +00:00
|
|
|
|
2001-05-18 21:05:51 +00:00
|
|
|
*aReturn = clonedFrag;
|
2002-03-18 14:38:11 +00:00
|
|
|
NS_IF_ADDREF(*aReturn);
|
|
|
|
|
2001-05-18 21:05:51 +00:00
|
|
|
return NS_OK;
|
1998-12-10 18:58:49 +00:00
|
|
|
}
|
|
|
|
|
2000-08-24 03:54:30 +00:00
|
|
|
nsresult nsRange::CloneRange(nsIDOMRange** aReturn)
|
1998-12-10 18:58:49 +00:00
|
|
|
{
|
2001-01-09 21:44:35 +00:00
|
|
|
if(IsDetached())
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
|
1998-12-10 18:58:49 +00:00
|
|
|
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)
|
2001-01-09 21:44:35 +00:00
|
|
|
{
|
2003-02-04 04:19:05 +00:00
|
|
|
VALIDATE_ACCESS(aN);
|
|
|
|
|
2001-05-18 21:05:51 +00:00
|
|
|
nsresult res;
|
|
|
|
PRInt32 tStartOffset;
|
|
|
|
this->GetStartOffset(&tStartOffset);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> tStartContainer;
|
|
|
|
res = this->GetStartContainer(getter_AddRefs(tStartContainer));
|
|
|
|
if(NS_FAILED(res)) return res;
|
2003-04-26 01:14:42 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMText> startTextNode(do_QueryInterface(tStartContainer));
|
|
|
|
if (startTextNode)
|
2001-05-18 21:05:51 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tSCParentNode;
|
|
|
|
res = tStartContainer->GetParentNode(getter_AddRefs(tSCParentNode));
|
|
|
|
if(NS_FAILED(res)) return res;
|
|
|
|
|
2003-04-26 01:14:42 +00:00
|
|
|
nsCOMPtr<nsIDOMText> secondPart;
|
|
|
|
res = startTextNode->SplitText(tStartOffset, getter_AddRefs(secondPart));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
2001-05-18 21:05:51 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> tResultNode;
|
2003-04-26 01:14:42 +00:00
|
|
|
return tSCParentNode->InsertBefore(aN, secondPart, getter_AddRefs(tResultNode));
|
2001-05-18 21:05:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNodeList>tChildList;
|
|
|
|
res = tStartContainer->GetChildNodes(getter_AddRefs(tChildList));
|
|
|
|
if(NS_FAILED(res)) return res;
|
|
|
|
PRUint32 tChildListLength;
|
|
|
|
res = tChildList->GetLength(&tChildListLength);
|
|
|
|
if(NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// find the insertion point in the DOM and insert the Node
|
2003-04-26 01:14:42 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>tChildNode;
|
|
|
|
res = tChildList->Item(tStartOffset, getter_AddRefs(tChildNode));
|
|
|
|
if(NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> tResultNode;
|
|
|
|
return tStartContainer->InsertBefore(aN, tChildNode, getter_AddRefs(tResultNode));
|
2001-01-09 21:44:35 +00:00
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
1998-11-25 01:21:42 +00:00
|
|
|
nsresult nsRange::SurroundContents(nsIDOMNode* aN)
|
2001-01-09 21:44:35 +00:00
|
|
|
{
|
2003-02-04 04:19:05 +00:00
|
|
|
VALIDATE_ACCESS(aN);
|
|
|
|
|
2001-05-18 21:05:51 +00:00
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
//get start offset, and start container
|
|
|
|
PRInt32 tStartOffset;
|
|
|
|
this->GetStartOffset(&tStartOffset);
|
|
|
|
nsCOMPtr<nsIDOMNode> tStartContainer;
|
|
|
|
res = GetStartContainer(getter_AddRefs(tStartContainer));
|
|
|
|
if(NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
//get end offset, and end container
|
|
|
|
PRInt32 tEndOffset;
|
|
|
|
this->GetEndOffset(&tEndOffset);
|
|
|
|
nsCOMPtr<nsIDOMNode> tEndContainer;
|
|
|
|
res = GetEndContainer(getter_AddRefs(tEndContainer));
|
|
|
|
if(NS_FAILED(res)) return res;
|
|
|
|
|
2001-06-15 18:15:09 +00:00
|
|
|
//prep start
|
2001-05-18 21:05:51 +00:00
|
|
|
PRUint16 tStartNodeType;
|
|
|
|
tStartContainer->GetNodeType(&tStartNodeType);
|
|
|
|
if( (nsIDOMNode::CDATA_SECTION_NODE == tStartNodeType) ||
|
|
|
|
(nsIDOMNode::TEXT_NODE == tStartNodeType) )
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMText> tStartContainerText = do_QueryInterface(tStartContainer);
|
|
|
|
nsCOMPtr<nsIDOMText> tTempText;
|
|
|
|
res = tStartContainerText->SplitText(tStartOffset, getter_AddRefs(tTempText));
|
|
|
|
if(NS_FAILED(res)) return res;
|
|
|
|
tStartOffset = 0;
|
|
|
|
tStartContainer = do_QueryInterface(tTempText);
|
|
|
|
}
|
|
|
|
|
2001-06-15 18:15:09 +00:00
|
|
|
//prep end
|
2001-05-18 21:05:51 +00:00
|
|
|
PRUint16 tEndNodeType;
|
|
|
|
tEndContainer->GetNodeType(&tEndNodeType);
|
|
|
|
if( (nsIDOMNode::CDATA_SECTION_NODE == tEndNodeType) ||
|
|
|
|
(nsIDOMNode::TEXT_NODE == tEndNodeType) )
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMText> tEndContainerText = do_QueryInterface(tEndContainer);
|
|
|
|
nsCOMPtr<nsIDOMText> tTempText;
|
|
|
|
res = tEndContainerText->SplitText(tEndOffset, getter_AddRefs(tTempText));
|
|
|
|
if(NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
tEndContainer = do_QueryInterface(tTempText);
|
|
|
|
}
|
|
|
|
|
2001-06-15 18:15:09 +00:00
|
|
|
//get ancestor info
|
2001-05-18 21:05:51 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> tAncestorContainer;
|
|
|
|
this->GetCommonAncestorContainer(getter_AddRefs(tAncestorContainer));
|
|
|
|
|
|
|
|
PRUint16 tCommonAncestorType;
|
|
|
|
tAncestorContainer->GetNodeType(&tCommonAncestorType);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode>tempNode;
|
|
|
|
nsCOMPtr<nsIDOMNode>tRangeContentsNode;
|
|
|
|
nsCOMPtr<nsIDOMDocument> document;
|
|
|
|
res = mStartParent->GetOwnerDocument(getter_AddRefs(document));
|
|
|
|
if (NS_FAILED(res)) return res;
|
2001-06-15 18:15:09 +00:00
|
|
|
|
2001-07-21 02:53:43 +00:00
|
|
|
// Create a new document fragment in the context of this document,
|
|
|
|
// which might be null
|
2001-05-18 21:05:51 +00:00
|
|
|
nsCOMPtr<nsIDOMDocumentFragment> docfrag;
|
2001-07-21 02:53:43 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc(do_QueryInterface(document));
|
|
|
|
|
|
|
|
res = NS_NewDocumentFragment(getter_AddRefs(docfrag), doc);
|
2001-05-18 21:05:51 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2001-06-15 18:15:09 +00:00
|
|
|
|
|
|
|
res = this->ExtractContents(getter_AddRefs(docfrag));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
2001-05-18 21:05:51 +00:00
|
|
|
tRangeContentsNode = do_QueryInterface(docfrag);
|
|
|
|
aN->AppendChild(tRangeContentsNode, getter_AddRefs(tempNode));
|
|
|
|
|
|
|
|
if( (nsIDOMNode::CDATA_SECTION_NODE == tCommonAncestorType) ||
|
|
|
|
(nsIDOMNode::TEXT_NODE == tCommonAncestorType) )
|
2001-06-15 18:15:09 +00:00
|
|
|
{//easy stuff here
|
2001-05-18 21:05:51 +00:00
|
|
|
this->InsertNode(aN);
|
|
|
|
}
|
|
|
|
else
|
2001-06-15 18:15:09 +00:00
|
|
|
{//hard stuff here
|
2001-05-18 21:05:51 +00:00
|
|
|
nsCOMPtr<nsIDOMNodeList>tChildList;
|
|
|
|
res = tAncestorContainer->GetChildNodes(getter_AddRefs(tChildList));
|
|
|
|
PRUint32 i,tNumChildren;
|
|
|
|
tChildList->GetLength(&tNumChildren);
|
|
|
|
|
|
|
|
PRBool tFound = PR_FALSE;
|
|
|
|
PRInt16 tResult;
|
|
|
|
for(i = 0; (i < tNumChildren && !tFound); i++)
|
|
|
|
{
|
|
|
|
ComparePoint(tAncestorContainer, i, &tResult);
|
|
|
|
if(tResult == 0)
|
|
|
|
{
|
2001-05-18 22:14:12 +00:00
|
|
|
tFound = PR_TRUE;
|
2001-05-18 21:05:51 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if(tFound)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tChild;
|
2003-04-02 03:19:50 +00:00
|
|
|
tChildList->Item(i, getter_AddRefs(tChild));
|
2001-05-18 21:05:51 +00:00
|
|
|
tAncestorContainer->InsertBefore(aN, tChild, getter_AddRefs(tempNode));
|
|
|
|
}
|
|
|
|
else // there is an error this may need to be updated later
|
|
|
|
this->InsertNode(aN);
|
|
|
|
|
|
|
|
// re-define the range so that it contains the same content as it did before
|
2002-08-23 18:02:45 +00:00
|
|
|
tEndOffset = GetNodeLength(tEndContainer);
|
|
|
|
if (tEndOffset == -1) // failure code
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2001-05-18 21:05:51 +00:00
|
|
|
this->DoSetRange(tStartContainer, 0, tEndContainer, tEndOffset);
|
|
|
|
}
|
2001-06-15 18:15:09 +00:00
|
|
|
this->SelectNode(aN);
|
|
|
|
return NS_OK;
|
2001-01-09 21:44:35 +00:00
|
|
|
}
|
1998-11-24 21:20:11 +00:00
|
|
|
|
2002-03-23 23:54:46 +00:00
|
|
|
nsresult nsRange::ToString(nsAString& aReturn)
|
1998-12-15 04:42:29 +00:00
|
|
|
{
|
2001-01-09 21:44:35 +00:00
|
|
|
if(IsDetached())
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
|
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-08-19 22:20:57 +00:00
|
|
|
|
1999-09-29 21:52:10 +00:00
|
|
|
#ifdef DEBUG_range
|
2000-10-28 22:17:53 +00:00
|
|
|
printf("Range dump: -----------------------\n");
|
1999-08-19 22:20:57 +00:00
|
|
|
#endif /* DEBUG */
|
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-09-29 21:52:10 +00:00
|
|
|
#ifdef DEBUG_range
|
1999-08-19 22:20:57 +00:00
|
|
|
// If debug, dump it:
|
|
|
|
nsCOMPtr<nsIContent> cN (do_QueryInterface(mStartParent));
|
|
|
|
if (cN) cN->List(stdout);
|
2000-10-28 22:17:53 +00:00
|
|
|
printf("End Range dump: -----------------------\n");
|
1999-08-19 22:20:57 +00:00
|
|
|
#endif /* DEBUG */
|
|
|
|
|
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));
|
1999-10-28 03:16:48 +00:00
|
|
|
while (cN && (NS_ENUMERATOR_FALSE == iter->IsDone()))
|
1998-12-15 04:42:29 +00:00
|
|
|
{
|
1999-09-29 21:52:10 +00:00
|
|
|
#ifdef DEBUG_range
|
1999-08-19 22:20:57 +00:00
|
|
|
// If debug, dump it:
|
|
|
|
cN->List(stdout);
|
|
|
|
#endif /* DEBUG */
|
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
|
|
|
}
|
1999-08-19 22:20:57 +00:00
|
|
|
|
1999-09-29 21:52:10 +00:00
|
|
|
#ifdef DEBUG_range
|
2000-10-28 22:17:53 +00:00
|
|
|
printf("End Range dump: -----------------------\n");
|
1999-08-19 22:20:57 +00:00
|
|
|
#endif /* DEBUG */
|
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
|
|
|
|
2000-08-24 03:54:30 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsRange::Detach()
|
|
|
|
{
|
2001-01-09 21:44:35 +00:00
|
|
|
if(IsDetached())
|
|
|
|
return NS_ERROR_DOM_INVALID_STATE_ERR;
|
|
|
|
mIsDetached = PR_TRUE;
|
2000-08-24 03:54:30 +00:00
|
|
|
return DoSetRange(nsnull,0,nsnull,0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
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;
|
2003-06-13 20:10:01 +00:00
|
|
|
parent->GetRangeList(&theRangeList);
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!theRangeList) return NS_OK;
|
2002-07-10 05:10:09 +00:00
|
|
|
|
1998-12-30 08:28:16 +00:00
|
|
|
nsresult res;
|
2002-07-10 05:10:09 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> domNode(do_QueryInterface(parent));
|
1998-12-30 08:28:16 +00:00
|
|
|
if (!domNode) return NS_ERROR_UNEXPECTED;
|
1998-12-18 09:28:55 +00:00
|
|
|
|
2001-12-21 01:10:07 +00:00
|
|
|
|
|
|
|
PRInt32 count = theRangeList->Count();
|
|
|
|
for (PRInt32 loop = 0; loop < count; loop++)
|
1998-12-18 09:28:55 +00:00
|
|
|
{
|
2001-12-21 01:10:07 +00:00
|
|
|
nsRange* theRange = NS_STATIC_CAST(nsRange*, (theRangeList->ElementAt(loop)));
|
|
|
|
NS_ASSERTION(theRange, "oops, no range");
|
|
|
|
|
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
|
2002-03-14 01:07:09 +00:00
|
|
|
if (aOffset < theRange->mStartOffset) theRange->mStartOffset++;
|
1998-12-30 08:28:16 +00:00
|
|
|
}
|
|
|
|
if (theRange->mEndParent == domNode)
|
|
|
|
{
|
|
|
|
// if child inserted before end, move end offset right one
|
2002-03-14 01:07:09 +00:00
|
|
|
if (aOffset < theRange->mEndOffset) theRange->mEndOffset++;
|
1998-12-30 08:28:16 +00:00
|
|
|
}
|
|
|
|
NS_PRECONDITION(NS_SUCCEEDED(res), "error updating range list");
|
|
|
|
}
|
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) );
|
1999-09-08 05:27:44 +00:00
|
|
|
|
|
|
|
// any ranges in the content subtree rooted by aRemovedNode need to
|
|
|
|
// have the enclosed endpoints promoted up to the parentNode/offset
|
2002-07-10 05:10:09 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> domNode(do_QueryInterface(parent));
|
1999-09-08 05:27:44 +00:00
|
|
|
if (!domNode) return NS_ERROR_UNEXPECTED;
|
2002-07-10 05:10:09 +00:00
|
|
|
nsresult res = PopRanges(domNode, aOffset, removed);
|
1999-09-08 05:27:44 +00:00
|
|
|
|
1998-12-30 08:28:16 +00:00
|
|
|
// quick return if no range list
|
|
|
|
nsVoidArray *theRangeList;
|
2003-06-13 20:10:01 +00:00
|
|
|
parent->GetRangeList(&theRangeList);
|
1999-02-14 09:14:50 +00:00
|
|
|
if (!theRangeList) return NS_OK;
|
1998-12-30 08:28:16 +00:00
|
|
|
|
2001-12-21 01:10:07 +00:00
|
|
|
PRInt32 count = theRangeList->Count();
|
|
|
|
for (PRInt32 loop = 0; loop < count; loop++)
|
1998-12-18 09:28:55 +00:00
|
|
|
{
|
2001-12-21 01:10:07 +00:00
|
|
|
nsRange* theRange = NS_STATIC_CAST(nsRange*, (theRangeList->ElementAt(loop)));
|
|
|
|
NS_ASSERTION(theRange, "oops, no range");
|
|
|
|
|
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
|
|
|
|
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) );
|
2002-07-10 05:10:09 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parentDomNode( do_QueryInterface(parent) );
|
|
|
|
|
1998-12-30 08:28:16 +00:00
|
|
|
if (!parentDomNode) return NS_ERROR_UNEXPECTED;
|
2002-07-10 05:10:09 +00:00
|
|
|
|
|
|
|
return PopRanges(parentDomNode, aOffset, replaced);
|
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;
|
2003-06-13 20:10:01 +00:00
|
|
|
aTextNode->GetRangeList(&theRangeList);
|
1999-01-03 14:29:54 +00:00
|
|
|
// the caller already checked to see if there was a range list
|
|
|
|
|
2002-07-10 05:10:09 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> domNode(do_QueryInterface(textNode));
|
1999-01-03 14:29:54 +00:00
|
|
|
if (!domNode) return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2001-12-21 01:10:07 +00:00
|
|
|
PRInt32 count = theRangeList->Count();
|
|
|
|
for (PRInt32 loop = 0; loop < count; loop++)
|
1999-01-03 14:29:54 +00:00
|
|
|
{
|
2001-12-21 01:10:07 +00:00
|
|
|
nsRange* theRange = NS_STATIC_CAST(nsRange*, (theRangeList->ElementAt(loop)));
|
|
|
|
NS_ASSERTION(theRange, "oops, no range");
|
|
|
|
|
1999-01-03 14:29:54 +00:00
|
|
|
// sanity check - do range and content agree over ownership?
|
2002-07-10 05:10:09 +00:00
|
|
|
nsresult res = theRange->ContentOwnsUs(domNode);
|
1999-01-03 14:29:54 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-04-15 20:19:26 +00:00
|
|
|
|
1999-04-27 22:16:04 +00:00
|
|
|
|
|
|
|
// nsIDOMNSRange interface
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 23:54:46 +00:00
|
|
|
nsRange::CreateContextualFragment(const nsAString& aFragment,
|
1999-06-03 22:38:38 +00:00
|
|
|
nsIDOMDocumentFragment** aReturn)
|
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
|
2002-12-11 14:24:49 +00:00
|
|
|
nsresult result;
|
|
|
|
nsCOMPtr<nsIParser> parser = do_CreateInstance(kCParserCID, &result);
|
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2001-10-19 20:48:18 +00:00
|
|
|
|
2002-12-11 14:24:49 +00:00
|
|
|
nsCOMPtr<nsIDocument> document;
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDocument;
|
|
|
|
|
2003-04-15 01:09:09 +00:00
|
|
|
result = mStartParent->GetOwnerDocument(getter_AddRefs(domDocument));
|
|
|
|
if (domDocument && NS_SUCCEEDED(result)) {
|
|
|
|
document = do_QueryInterface(domDocument, &result);
|
2002-12-13 08:41:42 +00:00
|
|
|
}
|
2002-12-11 14:24:49 +00:00
|
|
|
|
|
|
|
nsVoidArray tagStack;
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = mStartParent;
|
|
|
|
while (parent &&
|
|
|
|
(parent != domDocument) &&
|
|
|
|
NS_SUCCEEDED(result)) {
|
|
|
|
PRUint16 nodeType;
|
|
|
|
|
|
|
|
parent->GetNodeType(&nodeType);
|
|
|
|
if (nsIDOMNode::ELEMENT_NODE == nodeType) {
|
|
|
|
nsAutoString tagName;
|
|
|
|
parent->GetNodeName(tagName);
|
|
|
|
// XXX Wish we didn't have to allocate here
|
|
|
|
PRUnichar* name = ToNewUnicode(tagName);
|
|
|
|
if (name) {
|
|
|
|
tagStack.AppendElement(name);
|
|
|
|
nsCOMPtr<nsIDOMNode> temp = parent;
|
|
|
|
result = temp->GetParentNode(getter_AddRefs(parent));
|
2001-10-19 20:48:18 +00:00
|
|
|
}
|
2002-12-11 14:24:49 +00:00
|
|
|
else {
|
|
|
|
result = NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nsCOMPtr<nsIDOMNode> temp = parent;
|
|
|
|
result = temp->GetParentNode(getter_AddRefs(parent));
|
|
|
|
}
|
|
|
|
}
|
1999-06-03 22:38:38 +00:00
|
|
|
|
2002-12-11 14:24:49 +00:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
nsCAutoString contentType;
|
|
|
|
nsCOMPtr<nsIHTMLFragmentContentSink> sink;
|
|
|
|
|
|
|
|
result = NS_NewHTMLFragmentContentSink(getter_AddRefs(sink));
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
2003-04-15 01:09:09 +00:00
|
|
|
sink->SetTargetDocument(document);
|
2002-12-11 14:24:49 +00:00
|
|
|
parser->SetContentSink(sink);
|
|
|
|
nsCOMPtr<nsIDOMNSDocument> domnsDocument(do_QueryInterface(document));
|
|
|
|
if (domnsDocument) {
|
|
|
|
nsAutoString buf;
|
|
|
|
domnsDocument->GetContentType(buf);
|
|
|
|
CopyUCS2toASCII(buf, contentType);
|
2001-10-19 20:48:18 +00:00
|
|
|
}
|
2002-12-11 14:24:49 +00:00
|
|
|
else {
|
|
|
|
// Who're we kidding. This only works for html.
|
|
|
|
contentType = NS_LITERAL_CSTRING("text/html");
|
|
|
|
}
|
|
|
|
|
|
|
|
// If there's no JS or system JS running,
|
|
|
|
// push the current document's context on the JS context stack
|
|
|
|
// so that event handlers in the fragment do not get
|
|
|
|
// compiled with the system principal.
|
|
|
|
nsCOMPtr<nsIJSContextStack> ContextStack;
|
|
|
|
nsCOMPtr<nsIScriptSecurityManager> secMan;
|
|
|
|
secMan = do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &result);
|
|
|
|
if (document && NS_SUCCEEDED(result)) {
|
|
|
|
nsCOMPtr<nsIPrincipal> sysPrin;
|
|
|
|
nsCOMPtr<nsIPrincipal> subjectPrin;
|
|
|
|
|
|
|
|
// Just to compare, not to use!
|
|
|
|
result = secMan->GetSystemPrincipal(getter_AddRefs(sysPrin));
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
result = secMan->GetSubjectPrincipal(getter_AddRefs(subjectPrin));
|
|
|
|
// If there's no subject principal, there's no JS running, so we're in system code.
|
|
|
|
// (just in case...null subject principal will probably never happen)
|
|
|
|
if (NS_SUCCEEDED(result) &&
|
|
|
|
(!subjectPrin || sysPrin.get() == subjectPrin.get())) {
|
|
|
|
nsCOMPtr<nsIScriptGlobalObject> globalObj;
|
|
|
|
result = document->GetScriptGlobalObject(getter_AddRefs(globalObj));
|
|
|
|
|
|
|
|
nsCOMPtr<nsIScriptContext> scriptContext;
|
|
|
|
if (NS_SUCCEEDED(result) && globalObj) {
|
|
|
|
result = globalObj->GetContext(getter_AddRefs(scriptContext));
|
2001-10-19 20:48:18 +00:00
|
|
|
}
|
2000-01-27 02:51:51 +00:00
|
|
|
|
2002-12-11 14:24:49 +00:00
|
|
|
JSContext* cx = nsnull;
|
|
|
|
if (NS_SUCCEEDED(result) && scriptContext) {
|
|
|
|
cx = (JSContext*)scriptContext->GetNativeContext();
|
1999-06-03 22:38:38 +00:00
|
|
|
}
|
2002-12-11 14:24:49 +00:00
|
|
|
|
|
|
|
if(cx) {
|
|
|
|
ContextStack = do_GetService("@mozilla.org/js/xpc/ContextStack;1", &result);
|
|
|
|
if(NS_SUCCEEDED(result)) {
|
|
|
|
result = ContextStack->Push(cx);
|
2002-06-25 21:16:17 +00:00
|
|
|
}
|
2001-11-16 04:48:30 +00:00
|
|
|
}
|
2002-12-11 14:24:49 +00:00
|
|
|
}
|
|
|
|
}
|
2001-10-19 20:48:18 +00:00
|
|
|
|
2002-12-11 14:24:49 +00:00
|
|
|
nsDTDMode mode = eDTDMode_autodetect;
|
2003-04-15 01:09:09 +00:00
|
|
|
nsCOMPtr<nsIHTMLDocument> htmlDoc(do_QueryInterface(domDocument));
|
2002-12-11 14:24:49 +00:00
|
|
|
if (htmlDoc) {
|
|
|
|
nsCompatibility compatMode;
|
|
|
|
htmlDoc->GetCompatibilityMode(compatMode);
|
|
|
|
switch (compatMode) {
|
|
|
|
case eCompatibility_NavQuirks:
|
|
|
|
mode = eDTDMode_quirks;
|
|
|
|
break;
|
|
|
|
case eCompatibility_AlmostStandards:
|
|
|
|
mode = eDTDMode_almost_standards;
|
|
|
|
break;
|
|
|
|
case eCompatibility_FullStandards:
|
|
|
|
mode = eDTDMode_full_standards;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_NOTREACHED("unknown mode");
|
|
|
|
break;
|
1999-06-03 22:38:38 +00:00
|
|
|
}
|
|
|
|
}
|
2002-12-11 14:24:49 +00:00
|
|
|
result = parser->ParseFragment(aFragment, (void*)0,
|
|
|
|
tagStack,
|
|
|
|
0, contentType, mode);
|
|
|
|
|
|
|
|
if (ContextStack) {
|
|
|
|
JSContext *notused;
|
|
|
|
ContextStack->Pop(¬used);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
sink->GetFragment(aReturn);
|
1999-06-03 22:38:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-11 14:24:49 +00:00
|
|
|
// XXX Ick! Delete strings we allocated above.
|
|
|
|
PRInt32 count = tagStack.Count();
|
|
|
|
for (PRInt32 i = 0; i < count; i++) {
|
|
|
|
PRUnichar* str = (PRUnichar*)tagStack.ElementAt(i);
|
|
|
|
if (str) {
|
|
|
|
nsCRT::free(str);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-06-03 22:38:38 +00:00
|
|
|
return result;
|
1999-04-27 22:16:04 +00:00
|
|
|
}
|
|
|
|
|
2000-02-10 04:35:51 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::GetHasGeneratedBefore(PRBool *aBool)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aBool);
|
|
|
|
*aBool = mBeforeGenContent;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::GetHasGeneratedAfter(PRBool *aBool)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aBool);
|
|
|
|
*aBool = mAfterGenContent;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::SetHasGeneratedBefore(PRBool aBool)
|
|
|
|
{
|
|
|
|
mBeforeGenContent = aBool;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::SetHasGeneratedAfter(PRBool aBool)
|
|
|
|
{
|
|
|
|
mAfterGenContent = aBool;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::SetBeforeAndAfter(PRBool aBefore, PRBool aAfter)
|
|
|
|
{
|
|
|
|
mBeforeGenContent = aBefore;
|
|
|
|
mBeforeGenContent = aAfter;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-07-03 11:14:08 +00:00
|
|
|
nsresult
|
|
|
|
nsRange::Lock()
|
|
|
|
{
|
|
|
|
if (!mMonitor)
|
|
|
|
mMonitor = ::PR_NewMonitor();
|
|
|
|
|
|
|
|
if (mMonitor)
|
|
|
|
PR_EnterMonitor(mMonitor);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsRange::Unlock()
|
|
|
|
{
|
|
|
|
if (mMonitor)
|
|
|
|
PR_ExitMonitor(mMonitor);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|