Bug 486547 part 1. Switch nsTypedSelection to storing nsIRange, not nsIDOMRange, objects. r+sr=smaug

This commit is contained in:
Boris Zbarsky 2009-04-06 13:17:37 -04:00
parent 5670e0305e
commit 081b4b02e7
10 changed files with 482 additions and 588 deletions

View File

@ -42,10 +42,12 @@
class nsINode;
class nsIDOMRange;
class nsIRange;
class nsRange;
#define NS_ICONTENTITERATOR_IID \
{ 0x716a396c, 0xdc4e, 0x4d10, \
{ 0xbd, 0x07, 0x27, 0xee, 0xae, 0x85, 0xe3, 0x86 } }
{ 0x2550078e, 0xae87, 0x4914, \
{ 0xb3, 0x04, 0xe4, 0xd1, 0x46, 0x19, 0x3d, 0x5f } }
class nsIContentIterator : public nsISupports
{
@ -57,8 +59,10 @@ public:
virtual nsresult Init(nsINode* aRoot) = 0;
/* Initializes an iterator for the subtree defined by the range aRange
Subclasses should make sure they implement both of these!
*/
virtual nsresult Init(nsIDOMRange* aRange) = 0;
virtual nsresult Init(nsIRange* aRange) = 0;
/** First will reset the list.
*/

View File

@ -41,11 +41,12 @@
#include "nsISupports.h"
#include "nsCOMPtr.h"
#include "nsINode.h"
#include "nsIDOMRange.h"
// IID for the nsIRange interface
#define NS_IRANGE_IID \
{ 0x267c8c4e, 0x7c97, 0x4a35, \
{ 0xaa, 0x08, 0x55, 0xa5, 0xbe, 0x3a, 0xc5, 0x74 } }
{ 0xbf5c5799, 0xe5b0, 0x49b5, \
{ 0xbd, 0x45, 0x3d, 0x9a, 0x0f, 0xb4, 0x97, 0x89 } }
class nsIRange : public nsISupports {
public:
@ -111,6 +112,12 @@ public:
virtual void Reset() = 0;
// XXXbz we could make these non-virtual if a bunch of nsRange stuff
// became nsIRange stuff... and if no one outside layout needs them.
virtual nsresult SetStart(nsINode* aParent, PRInt32 aOffset) = 0;
virtual nsresult SetEnd(nsINode* aParent, PRInt32 aOffset) = 0;
virtual nsresult CloneRange(nsIRange** aNewRange) = 0;
protected:
nsCOMPtr<nsINode> mRoot;
nsCOMPtr<nsINode> mStartParent;

View File

@ -134,6 +134,7 @@ public:
virtual nsresult Init(nsINode* aRoot);
virtual nsresult Init(nsIDOMRange* aRange);
virtual nsresult Init(nsIRange* aRange);
virtual void First();
@ -312,22 +313,29 @@ nsresult
nsContentIterator::Init(nsIDOMRange* aRange)
{
nsCOMPtr<nsIRange> range = do_QueryInterface(aRange);
NS_ENSURE_TRUE(range, NS_ERROR_NULL_POINTER);
return Init(range);
}
nsresult
nsContentIterator::Init(nsIRange* aRange)
{
NS_ENSURE_ARG_POINTER(aRange);
mIsDone = PR_FALSE;
// get common content parent
mCommonParent = range->GetCommonAncestor();
mCommonParent = aRange->GetCommonAncestor();
NS_ENSURE_TRUE(mCommonParent, NS_ERROR_FAILURE);
// get the start node and offset
PRInt32 startIndx = range->StartOffset();
nsINode* startNode = range->GetStartParent();
PRInt32 startIndx = aRange->StartOffset();
nsINode* startNode = aRange->GetStartParent();
NS_ENSURE_TRUE(startNode, NS_ERROR_FAILURE);
// get the end node and offset
PRInt32 endIndx = range->EndOffset();
nsINode* endNode = range->GetEndParent();
PRInt32 endIndx = aRange->EndOffset();
nsINode* endNode = aRange->GetEndParent();
NS_ENSURE_TRUE(endNode, NS_ERROR_FAILURE);
PRBool startIsData = startNode->IsNodeOfType(nsINode::eDATA_NODE);
@ -1170,6 +1178,7 @@ public:
virtual nsresult Init(nsINode* aRoot);
virtual nsresult Init(nsIDOMRange* aRange);
virtual nsresult Init(nsIRange* aRange);
virtual void Next();
@ -1414,6 +1423,11 @@ nsresult nsContentSubtreeIterator::Init(nsIDOMRange* aRange)
return NS_OK;
}
nsresult nsContentSubtreeIterator::Init(nsIRange* aRange)
{
nsCOMPtr<nsIDOMRange> range = do_QueryInterface(aRange);
return Init(range);
}
/****************************************************************
* nsContentSubtreeIterator overrides of ContentIterator routines

View File

@ -112,11 +112,7 @@ nsRange::CompareNodeToRange(nsINode* aNode, nsIRange* aRange,
// if (RANGE(start) <= NODE(start)) and (RANGE(end) => NODE(end))
// then the Node is contained (completely) by the Range.
nsresult rv;
nsCOMPtr<nsIRange> range = do_QueryInterface(aRange, &rv);
NS_ENSURE_SUCCESS(rv, rv);
if (!range->IsPositioned())
if (!aRange || !aRange->IsPositioned())
return NS_ERROR_UNEXPECTED;
// gather up the dom point info
@ -135,10 +131,10 @@ nsRange::CompareNodeToRange(nsINode* aNode, nsIRange* aRange,
nodeEnd = nodeStart + 1;
}
nsINode* rangeStartParent = range->GetStartParent();
nsINode* rangeEndParent = range->GetEndParent();
PRInt32 rangeStartOffset = range->StartOffset();
PRInt32 rangeEndOffset = range->EndOffset();
nsINode* rangeStartParent = aRange->GetStartParent();
nsINode* rangeEndParent = aRange->GetEndParent();
PRInt32 rangeStartOffset = aRange->StartOffset();
PRInt32 rangeEndOffset = aRange->EndOffset();
// is RANGE(start) <= NODE(start) ?
PRBool disconnected = PR_FALSE;
@ -411,7 +407,7 @@ nsRange::ComparePoint(nsIDOMNode* aParent, PRInt32 aOffset, PRInt16* aResult)
******************************************************/
// Get the length of aNode
static PRInt32 GetNodeLength(nsINode *aNode)
static PRUint32 GetNodeLength(nsINode *aNode)
{
if(aNode->IsNodeOfType(nsINode::eDATA_NODE)) {
return static_cast<nsIContent*>(aNode)->TextLength();
@ -625,24 +621,29 @@ nsresult nsRange::SetStart(nsIDOMNode* aParent, PRInt32 aOffset)
VALIDATE_ACCESS(aParent);
nsCOMPtr<nsINode> parent = do_QueryInterface(aParent);
nsINode* newRoot = IsValidBoundary(parent);
return SetStart(parent, aOffset);
}
nsresult nsRange::SetStart(nsINode* aParent, PRInt32 aOffset)
{
nsINode* newRoot = IsValidBoundary(aParent);
NS_ENSURE_TRUE(newRoot, NS_ERROR_DOM_RANGE_INVALID_NODE_TYPE_ERR);
PRInt32 len = GetNodeLength(parent);
PRInt32 len = GetNodeLength(aParent);
if (aOffset < 0 || aOffset > len)
return NS_ERROR_DOM_INDEX_SIZE_ERR;
// Collapse if not positioned yet, if positioned in another doc or
// if the new start is after end.
if (!mIsPositioned || newRoot != mRoot ||
nsContentUtils::ComparePoints(parent, aOffset,
nsContentUtils::ComparePoints(aParent, aOffset,
mEndParent, mEndOffset) == 1) {
DoSetRange(parent, aOffset, parent, aOffset, newRoot);
DoSetRange(aParent, aOffset, aParent, aOffset, newRoot);
return NS_OK;
}
DoSetRange(parent, aOffset, mEndParent, mEndOffset, mRoot);
DoSetRange(aParent, aOffset, mEndParent, mEndOffset, mRoot);
return NS_OK;
}
@ -678,10 +679,16 @@ nsresult nsRange::SetEnd(nsIDOMNode* aParent, PRInt32 aOffset)
VALIDATE_ACCESS(aParent);
nsCOMPtr<nsINode> parent = do_QueryInterface(aParent);
nsINode* newRoot = IsValidBoundary(parent);
return SetEnd(parent, aOffset);
}
nsresult nsRange::SetEnd(nsINode* aParent, PRInt32 aOffset)
{
nsINode* newRoot = IsValidBoundary(aParent);
NS_ENSURE_TRUE(newRoot, NS_ERROR_DOM_RANGE_INVALID_NODE_TYPE_ERR);
PRInt32 len = GetNodeLength(parent);
PRInt32 len = GetNodeLength(aParent);
if (aOffset < 0 || aOffset > len) {
return NS_ERROR_DOM_INDEX_SIZE_ERR;
}
@ -690,13 +697,13 @@ nsresult nsRange::SetEnd(nsIDOMNode* aParent, PRInt32 aOffset)
// if the new end is before start.
if (!mIsPositioned || newRoot != mRoot ||
nsContentUtils::ComparePoints(mStartParent, mStartOffset,
parent, aOffset) == 1) {
DoSetRange(parent, aOffset, parent, aOffset, newRoot);
aParent, aOffset) == 1) {
DoSetRange(aParent, aOffset, aParent, aOffset, newRoot);
return NS_OK;
}
DoSetRange(mStartParent, mStartOffset, parent, aOffset, mRoot);
DoSetRange(mStartParent, mStartOffset, aParent, aOffset, mRoot);
return NS_OK;
}
@ -1750,7 +1757,7 @@ nsresult nsRange::CloneContents(nsIDOMDocumentFragment** aReturn)
return NS_OK;
}
nsresult nsRange::CloneRange(nsIDOMRange** aReturn)
nsresult nsRange::DoCloneRange(nsRange** aReturn)
{
if(mIsDetached)
return NS_ERROR_DOM_INVALID_STATE_ERR;
@ -1758,18 +1765,38 @@ nsresult nsRange::CloneRange(nsIDOMRange** aReturn)
if (aReturn == 0)
return NS_ERROR_NULL_POINTER;
nsRange* range = new nsRange();
nsRefPtr<nsRange> range = new nsRange();
NS_ENSURE_TRUE(range, NS_ERROR_OUT_OF_MEMORY);
NS_ADDREF(*aReturn = range);
range->SetMaySpanAnonymousSubtrees(mMaySpanAnonymousSubtrees);
range->DoSetRange(mStartParent, mStartOffset, mEndParent, mEndOffset, mRoot);
range.forget(aReturn);
return NS_OK;
}
NS_IMETHODIMP nsRange::CloneRange(nsIDOMRange** aReturn)
{
nsRange* clone;
nsresult rv = DoCloneRange(&clone);
if (NS_SUCCEEDED(rv)) {
*aReturn = clone;
}
return rv;
}
nsresult nsRange::CloneRange(nsIRange** aReturn)
{
nsRange* clone;
nsresult rv = DoCloneRange(&clone);
if (NS_SUCCEEDED(rv)) {
*aReturn = clone;
}
return rv;
}
nsresult nsRange::InsertNode(nsIDOMNode* aN)
{
VALIDATE_ACCESS(aN);
@ -1948,7 +1975,7 @@ nsresult nsRange::ToString(nsAString& aReturn)
nsCOMPtr<nsIContentIterator> iter;
nsresult rv = NS_NewContentIterator(getter_AddRefs(iter));
NS_ENSURE_SUCCESS(rv, rv);
rv = iter->Init(this);
rv = iter->Init(static_cast<nsIRange*>(this));
NS_ENSURE_SUCCESS(rv, rv);
nsString tempString;

View File

@ -95,6 +95,9 @@ public:
// nsIRange interface
virtual nsINode* GetCommonAncestor();
virtual void Reset();
virtual nsresult SetStart(nsINode* aParent, PRInt32 aOffset);
virtual nsresult SetEnd(nsINode* aParent, PRInt32 aOffset);
virtual nsresult CloneRange(nsIRange** aNewRange);
// nsIMutationObserver methods
virtual void CharacterDataChanged(nsIDocument* aDocument,
@ -125,6 +128,11 @@ private:
*/
nsresult CutContents(nsIDOMDocumentFragment** frag);
/**
* Guts of cloning a range. Addrefs the new range.
*/
nsresult DoCloneRange(nsRange** aNewRange);
static nsresult CloneParentsBetween(nsIDOMNode *aAncestor,
nsIDOMNode *aNode,
nsIDOMNode **aClosestAncestor,

View File

@ -514,7 +514,7 @@ nsContentEventHandler::OnQueryTextRect(nsQueryContentEvent* aEvent)
if (NS_FAILED(rv))
return rv;
nsRefPtr<nsRange> range = new nsRange();
nsCOMPtr<nsIRange> range = new nsRange();
if (!range) {
return NS_ERROR_OUT_OF_MEMORY;
}

View File

@ -46,6 +46,7 @@
#include "nsIDOMNode.h"
#include "nsIDOMRange.h"
#include "nsIRange.h"
//------------------------------------------------------------
nsFilteredContentIterator::nsFilteredContentIterator(nsITextServicesFilter* aFilter) :
@ -111,6 +112,13 @@ nsFilteredContentIterator::Init(nsIDOMRange* aRange)
return mIterator->Init(domRange);
}
nsresult
nsFilteredContentIterator::Init(nsIRange* aRange)
{
nsCOMPtr<nsIDOMRange> domRange = do_QueryInterface(aRange);
return Init(domRange);
}
//------------------------------------------------------------
nsresult
nsFilteredContentIterator::SwitchDirections(PRPackedBool aChangeToForward)

View File

@ -62,6 +62,7 @@ public:
/* nsIContentIterator */
virtual nsresult Init(nsINode* aRoot);
virtual nsresult Init(nsIDOMRange* aRange);
virtual nsresult Init(nsIRange* aRange);
virtual void First();
virtual void Last();
virtual void Next();

View File

@ -132,6 +132,11 @@ public:
NS_NOTREACHED("internal error");
return NS_ERROR_NOT_IMPLEMENTED;
}
virtual nsresult Init(nsIRange* aRange)
{
NS_NOTREACHED("internal error");
return NS_ERROR_NOT_IMPLEMENTED;
}
// Not a range because one of the endpoints may be anonymous.
nsresult Init(nsIDOMNode* aStartNode, PRInt32 aStartOffset,
nsIDOMNode* aEndNode, PRInt32 aEndOffset);

File diff suppressed because it is too large Load Diff