Make nsSelection::ToString(); do the right thing, now it uses the document encoder to create the string representation of the selection. r=mjudge&akkana (before I did a string API change).

This commit is contained in:
jst%netscape.com 2000-04-28 13:58:08 +00:00
parent 4ad7ab823c
commit c29c060230
3 changed files with 87 additions and 120 deletions

View File

@ -45,6 +45,7 @@
#include "nsIDOMSelectionListener.h" #include "nsIDOMSelectionListener.h"
#include "nsIContentIterator.h" #include "nsIContentIterator.h"
#include "nsIDocumentEncoder.h"
#include "nsIDOMText.h" #include "nsIDOMText.h"
@ -80,9 +81,10 @@ static NS_DEFINE_IID(kCGenSubtreeIteratorCID, NS_GENERATEDSUBTREEITERATOR_CID);
static NS_DEFINE_IID(kCSubtreeIteratorCID, NS_SUBTREEITERATOR_CID); static NS_DEFINE_IID(kCSubtreeIteratorCID, NS_SUBTREEITERATOR_CID);
//PROTOTYPES //PROTOTYPES
#if OLD_SELECTION
static nsCOMPtr<nsIAtom> GetTag(nsIDOMNode *aNode); static nsCOMPtr<nsIAtom> GetTag(nsIDOMNode *aNode);
static nsresult ParentOffset(nsIDOMNode *aNode, nsIDOMNode **aParent, PRInt32 *aChildOffset); static nsresult ParentOffset(nsIDOMNode *aNode, nsIDOMNode **aParent, PRInt32 *aChildOffset);
#endif
#ifdef PRINT_RANGE #ifdef PRINT_RANGE
static void printRange(nsIDOMRange *aDomRange); static void printRange(nsIDOMRange *aDomRange);
@ -907,7 +909,7 @@ nsSelection::GetRootForContentSubtree(nsIContent *aContent, nsIContent **aParent
// as a child of it's parent. In this case, the anonymous content would // as a child of it's parent. In this case, the anonymous content would
// be considered the root of the subtree. // be considered the root of the subtree.
nsresult result; nsresult result = NS_OK;
if (!aContent || !aParent) if (!aContent || !aParent)
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
@ -1269,7 +1271,7 @@ nsSelection::MoveCaret(PRUint32 aKeycode, PRBool aContinue, nsSelectionAmount aA
PRInt32 offsetused = 0; PRInt32 offsetused = 0;
PRBool isCollapsed; PRBool isCollapsed;
nscoord desiredX; //we must keep this around and revalidate it when its just UP/DOWN nscoord desiredX = 0; //we must keep this around and revalidate it when its just UP/DOWN
PRInt8 index = GetIndexFromSelectionType(nsISelectionController::SELECTION_NORMAL); PRInt8 index = GetIndexFromSelectionType(nsISelectionController::SELECTION_NORMAL);
result = mDomSelections[index]->GetIsCollapsed(&isCollapsed); result = mDomSelections[index]->GetIsCollapsed(&isCollapsed);
@ -1456,48 +1458,35 @@ nsSelection::HandleKeyEvent(nsIPresContext* aPresContext, nsGUIEvent *aGuiEvent)
NS_IMETHODIMP NS_IMETHODIMP
nsDOMSelection::ToString(nsString& aReturn) nsDOMSelection::ToString(nsString& aReturn)
{ {
PRInt32 cnt; nsresult rv = NS_OK;
GetRangeCount(&cnt);
aReturn.AssignWithConversion("nsSelection: ");
aReturn.AppendInt(cnt);
aReturn.AppendWithConversion(" items\n");
// Get an iterator nsCOMPtr<nsIDocumentEncoder> encoder;
nsSelectionIterator iter(this); rv = nsComponentManager::CreateInstance(NS_DOC_ENCODER_PROGID_BASE "text/plain",
nsresult res = iter.First(); nsnull,
if (!NS_SUCCEEDED(res)) NS_GET_IID(nsIDocumentEncoder),
{ getter_AddRefs(encoder));
aReturn.AppendWithConversion(" Can't get an iterator\n"); NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIPresShell> shell;
rv = GetPresShell(getter_AddRefs(shell));
if (NS_FAILED(rv) || !shell) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
while (iter.IsDone()) nsCOMPtr<nsIDocument> doc;
{ rv = shell->GetDocument(getter_AddRefs(doc));
nsCOMPtr<nsIDOMRange> range; NS_ENSURE_SUCCESS(rv, rv);
res = iter.CurrentItem(NS_STATIC_CAST(nsIDOMRange**, getter_AddRefs(range)));
if (!NS_SUCCEEDED(res))
{
aReturn.AppendWithConversion(" OOPS\n");
return NS_ERROR_FAILURE;
}
nsString rangeStr;
if (NS_SUCCEEDED(range->ToString(rangeStr)))
aReturn += rangeStr;
iter.Next();
}
aReturn.AppendWithConversion("Anchor is "); rv = encoder->Init(doc, NS_ConvertASCIItoUCS2("text/plain"),
PRInt32 theInt = (PRInt32)(nsIDOMNode*)FetchAnchorNode(); nsIDocumentEncoder::OutputFormatted ||
aReturn.AppendInt(theInt); nsIDocumentEncoder::OutputSelectionOnly);
aReturn.AppendWithConversion(", "); NS_ENSURE_SUCCESS(rv, rv);
aReturn.AppendInt(FetchAnchorOffset());
aReturn.AppendWithConversion("Focus is");
aReturn.AppendInt((long)(nsIDOMNode*)FetchFocusNode(), 16);
aReturn.AppendWithConversion(", ");
aReturn.AppendInt(FetchFocusOffset());
aReturn.AppendWithConversion("\n ... end of selection\n");
return NS_OK; encoder->SetSelection(this);
rv = encoder->EncodeToString(aReturn);
return rv;
} }
NS_IMETHODIMP NS_IMETHODIMP
@ -3690,7 +3679,7 @@ nsDOMSelection::RemoveRange(nsIDOMRange* aRange)
{ {
if (!aRange) if (!aRange)
return NS_ERROR_INVALID_ARG; return NS_ERROR_INVALID_ARG;
nsresult result = RemoveItem(aRange); RemoveItem(aRange);
nsCOMPtr<nsIPresContext> presContext; nsCOMPtr<nsIPresContext> presContext;
GetPresContext(getter_AddRefs(presContext)); GetPresContext(getter_AddRefs(presContext));

View File

@ -45,6 +45,7 @@
#include "nsIDOMSelectionListener.h" #include "nsIDOMSelectionListener.h"
#include "nsIContentIterator.h" #include "nsIContentIterator.h"
#include "nsIDocumentEncoder.h"
#include "nsIDOMText.h" #include "nsIDOMText.h"
@ -80,9 +81,10 @@ static NS_DEFINE_IID(kCGenSubtreeIteratorCID, NS_GENERATEDSUBTREEITERATOR_CID);
static NS_DEFINE_IID(kCSubtreeIteratorCID, NS_SUBTREEITERATOR_CID); static NS_DEFINE_IID(kCSubtreeIteratorCID, NS_SUBTREEITERATOR_CID);
//PROTOTYPES //PROTOTYPES
#if OLD_SELECTION
static nsCOMPtr<nsIAtom> GetTag(nsIDOMNode *aNode); static nsCOMPtr<nsIAtom> GetTag(nsIDOMNode *aNode);
static nsresult ParentOffset(nsIDOMNode *aNode, nsIDOMNode **aParent, PRInt32 *aChildOffset); static nsresult ParentOffset(nsIDOMNode *aNode, nsIDOMNode **aParent, PRInt32 *aChildOffset);
#endif
#ifdef PRINT_RANGE #ifdef PRINT_RANGE
static void printRange(nsIDOMRange *aDomRange); static void printRange(nsIDOMRange *aDomRange);
@ -907,7 +909,7 @@ nsSelection::GetRootForContentSubtree(nsIContent *aContent, nsIContent **aParent
// as a child of it's parent. In this case, the anonymous content would // as a child of it's parent. In this case, the anonymous content would
// be considered the root of the subtree. // be considered the root of the subtree.
nsresult result; nsresult result = NS_OK;
if (!aContent || !aParent) if (!aContent || !aParent)
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
@ -1269,7 +1271,7 @@ nsSelection::MoveCaret(PRUint32 aKeycode, PRBool aContinue, nsSelectionAmount aA
PRInt32 offsetused = 0; PRInt32 offsetused = 0;
PRBool isCollapsed; PRBool isCollapsed;
nscoord desiredX; //we must keep this around and revalidate it when its just UP/DOWN nscoord desiredX = 0; //we must keep this around and revalidate it when its just UP/DOWN
PRInt8 index = GetIndexFromSelectionType(nsISelectionController::SELECTION_NORMAL); PRInt8 index = GetIndexFromSelectionType(nsISelectionController::SELECTION_NORMAL);
result = mDomSelections[index]->GetIsCollapsed(&isCollapsed); result = mDomSelections[index]->GetIsCollapsed(&isCollapsed);
@ -1456,48 +1458,35 @@ nsSelection::HandleKeyEvent(nsIPresContext* aPresContext, nsGUIEvent *aGuiEvent)
NS_IMETHODIMP NS_IMETHODIMP
nsDOMSelection::ToString(nsString& aReturn) nsDOMSelection::ToString(nsString& aReturn)
{ {
PRInt32 cnt; nsresult rv = NS_OK;
GetRangeCount(&cnt);
aReturn.AssignWithConversion("nsSelection: ");
aReturn.AppendInt(cnt);
aReturn.AppendWithConversion(" items\n");
// Get an iterator nsCOMPtr<nsIDocumentEncoder> encoder;
nsSelectionIterator iter(this); rv = nsComponentManager::CreateInstance(NS_DOC_ENCODER_PROGID_BASE "text/plain",
nsresult res = iter.First(); nsnull,
if (!NS_SUCCEEDED(res)) NS_GET_IID(nsIDocumentEncoder),
{ getter_AddRefs(encoder));
aReturn.AppendWithConversion(" Can't get an iterator\n"); NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIPresShell> shell;
rv = GetPresShell(getter_AddRefs(shell));
if (NS_FAILED(rv) || !shell) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
while (iter.IsDone()) nsCOMPtr<nsIDocument> doc;
{ rv = shell->GetDocument(getter_AddRefs(doc));
nsCOMPtr<nsIDOMRange> range; NS_ENSURE_SUCCESS(rv, rv);
res = iter.CurrentItem(NS_STATIC_CAST(nsIDOMRange**, getter_AddRefs(range)));
if (!NS_SUCCEEDED(res))
{
aReturn.AppendWithConversion(" OOPS\n");
return NS_ERROR_FAILURE;
}
nsString rangeStr;
if (NS_SUCCEEDED(range->ToString(rangeStr)))
aReturn += rangeStr;
iter.Next();
}
aReturn.AppendWithConversion("Anchor is "); rv = encoder->Init(doc, NS_ConvertASCIItoUCS2("text/plain"),
PRInt32 theInt = (PRInt32)(nsIDOMNode*)FetchAnchorNode(); nsIDocumentEncoder::OutputFormatted ||
aReturn.AppendInt(theInt); nsIDocumentEncoder::OutputSelectionOnly);
aReturn.AppendWithConversion(", "); NS_ENSURE_SUCCESS(rv, rv);
aReturn.AppendInt(FetchAnchorOffset());
aReturn.AppendWithConversion("Focus is");
aReturn.AppendInt((long)(nsIDOMNode*)FetchFocusNode(), 16);
aReturn.AppendWithConversion(", ");
aReturn.AppendInt(FetchFocusOffset());
aReturn.AppendWithConversion("\n ... end of selection\n");
return NS_OK; encoder->SetSelection(this);
rv = encoder->EncodeToString(aReturn);
return rv;
} }
NS_IMETHODIMP NS_IMETHODIMP
@ -3690,7 +3679,7 @@ nsDOMSelection::RemoveRange(nsIDOMRange* aRange)
{ {
if (!aRange) if (!aRange)
return NS_ERROR_INVALID_ARG; return NS_ERROR_INVALID_ARG;
nsresult result = RemoveItem(aRange); RemoveItem(aRange);
nsCOMPtr<nsIPresContext> presContext; nsCOMPtr<nsIPresContext> presContext;
GetPresContext(getter_AddRefs(presContext)); GetPresContext(getter_AddRefs(presContext));

View File

@ -45,6 +45,7 @@
#include "nsIDOMSelectionListener.h" #include "nsIDOMSelectionListener.h"
#include "nsIContentIterator.h" #include "nsIContentIterator.h"
#include "nsIDocumentEncoder.h"
#include "nsIDOMText.h" #include "nsIDOMText.h"
@ -80,9 +81,10 @@ static NS_DEFINE_IID(kCGenSubtreeIteratorCID, NS_GENERATEDSUBTREEITERATOR_CID);
static NS_DEFINE_IID(kCSubtreeIteratorCID, NS_SUBTREEITERATOR_CID); static NS_DEFINE_IID(kCSubtreeIteratorCID, NS_SUBTREEITERATOR_CID);
//PROTOTYPES //PROTOTYPES
#if OLD_SELECTION
static nsCOMPtr<nsIAtom> GetTag(nsIDOMNode *aNode); static nsCOMPtr<nsIAtom> GetTag(nsIDOMNode *aNode);
static nsresult ParentOffset(nsIDOMNode *aNode, nsIDOMNode **aParent, PRInt32 *aChildOffset); static nsresult ParentOffset(nsIDOMNode *aNode, nsIDOMNode **aParent, PRInt32 *aChildOffset);
#endif
#ifdef PRINT_RANGE #ifdef PRINT_RANGE
static void printRange(nsIDOMRange *aDomRange); static void printRange(nsIDOMRange *aDomRange);
@ -907,7 +909,7 @@ nsSelection::GetRootForContentSubtree(nsIContent *aContent, nsIContent **aParent
// as a child of it's parent. In this case, the anonymous content would // as a child of it's parent. In this case, the anonymous content would
// be considered the root of the subtree. // be considered the root of the subtree.
nsresult result; nsresult result = NS_OK;
if (!aContent || !aParent) if (!aContent || !aParent)
return NS_ERROR_NULL_POINTER; return NS_ERROR_NULL_POINTER;
@ -1269,7 +1271,7 @@ nsSelection::MoveCaret(PRUint32 aKeycode, PRBool aContinue, nsSelectionAmount aA
PRInt32 offsetused = 0; PRInt32 offsetused = 0;
PRBool isCollapsed; PRBool isCollapsed;
nscoord desiredX; //we must keep this around and revalidate it when its just UP/DOWN nscoord desiredX = 0; //we must keep this around and revalidate it when its just UP/DOWN
PRInt8 index = GetIndexFromSelectionType(nsISelectionController::SELECTION_NORMAL); PRInt8 index = GetIndexFromSelectionType(nsISelectionController::SELECTION_NORMAL);
result = mDomSelections[index]->GetIsCollapsed(&isCollapsed); result = mDomSelections[index]->GetIsCollapsed(&isCollapsed);
@ -1456,48 +1458,35 @@ nsSelection::HandleKeyEvent(nsIPresContext* aPresContext, nsGUIEvent *aGuiEvent)
NS_IMETHODIMP NS_IMETHODIMP
nsDOMSelection::ToString(nsString& aReturn) nsDOMSelection::ToString(nsString& aReturn)
{ {
PRInt32 cnt; nsresult rv = NS_OK;
GetRangeCount(&cnt);
aReturn.AssignWithConversion("nsSelection: ");
aReturn.AppendInt(cnt);
aReturn.AppendWithConversion(" items\n");
// Get an iterator nsCOMPtr<nsIDocumentEncoder> encoder;
nsSelectionIterator iter(this); rv = nsComponentManager::CreateInstance(NS_DOC_ENCODER_PROGID_BASE "text/plain",
nsresult res = iter.First(); nsnull,
if (!NS_SUCCEEDED(res)) NS_GET_IID(nsIDocumentEncoder),
{ getter_AddRefs(encoder));
aReturn.AppendWithConversion(" Can't get an iterator\n"); NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIPresShell> shell;
rv = GetPresShell(getter_AddRefs(shell));
if (NS_FAILED(rv) || !shell) {
return NS_ERROR_FAILURE; return NS_ERROR_FAILURE;
} }
while (iter.IsDone()) nsCOMPtr<nsIDocument> doc;
{ rv = shell->GetDocument(getter_AddRefs(doc));
nsCOMPtr<nsIDOMRange> range; NS_ENSURE_SUCCESS(rv, rv);
res = iter.CurrentItem(NS_STATIC_CAST(nsIDOMRange**, getter_AddRefs(range)));
if (!NS_SUCCEEDED(res))
{
aReturn.AppendWithConversion(" OOPS\n");
return NS_ERROR_FAILURE;
}
nsString rangeStr;
if (NS_SUCCEEDED(range->ToString(rangeStr)))
aReturn += rangeStr;
iter.Next();
}
aReturn.AppendWithConversion("Anchor is "); rv = encoder->Init(doc, NS_ConvertASCIItoUCS2("text/plain"),
PRInt32 theInt = (PRInt32)(nsIDOMNode*)FetchAnchorNode(); nsIDocumentEncoder::OutputFormatted ||
aReturn.AppendInt(theInt); nsIDocumentEncoder::OutputSelectionOnly);
aReturn.AppendWithConversion(", "); NS_ENSURE_SUCCESS(rv, rv);
aReturn.AppendInt(FetchAnchorOffset());
aReturn.AppendWithConversion("Focus is");
aReturn.AppendInt((long)(nsIDOMNode*)FetchFocusNode(), 16);
aReturn.AppendWithConversion(", ");
aReturn.AppendInt(FetchFocusOffset());
aReturn.AppendWithConversion("\n ... end of selection\n");
return NS_OK; encoder->SetSelection(this);
rv = encoder->EncodeToString(aReturn);
return rv;
} }
NS_IMETHODIMP NS_IMETHODIMP
@ -3690,7 +3679,7 @@ nsDOMSelection::RemoveRange(nsIDOMRange* aRange)
{ {
if (!aRange) if (!aRange)
return NS_ERROR_INVALID_ARG; return NS_ERROR_INVALID_ARG;
nsresult result = RemoveItem(aRange); RemoveItem(aRange);
nsCOMPtr<nsIPresContext> presContext; nsCOMPtr<nsIPresContext> presContext;
GetPresContext(getter_AddRefs(presContext)); GetPresContext(getter_AddRefs(presContext));