1999-03-29 06:21:01 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL") you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "nsHTMLEditRules.h"
|
|
|
|
#include "nsEditor.h"
|
1999-07-19 22:49:21 +00:00
|
|
|
#include "nsHTMLEditor.h"
|
1999-04-21 14:49:58 +00:00
|
|
|
#include "nsTextEditor.h"
|
1999-03-29 06:21:01 +00:00
|
|
|
#include "PlaceholderTxn.h"
|
|
|
|
#include "InsertTextTxn.h"
|
1999-04-21 14:49:58 +00:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIContentIterator.h"
|
1999-03-29 06:21:01 +00:00
|
|
|
#include "nsIDOMNode.h"
|
1999-04-21 14:49:58 +00:00
|
|
|
#include "nsIDOMText.h"
|
1999-03-29 06:21:01 +00:00
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIDOMNodeList.h"
|
|
|
|
#include "nsIDOMSelection.h"
|
|
|
|
#include "nsIDOMRange.h"
|
|
|
|
#include "nsIDOMCharacterData.h"
|
|
|
|
#include "nsIEnumerator.h"
|
1999-04-21 14:49:58 +00:00
|
|
|
#include "nsIStyleContext.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsLayoutCID.h"
|
|
|
|
|
|
|
|
class nsIFrame;
|
1999-03-29 06:21:01 +00:00
|
|
|
|
1999-05-05 23:27:17 +00:00
|
|
|
//const static char* kMOZEditorBogusNodeAttr="MOZ_EDITOR_BOGUS_NODE";
|
|
|
|
//const static char* kMOZEditorBogusNodeValue="TRUE";
|
1999-05-03 22:16:02 +00:00
|
|
|
const unsigned char nbsp = 160;
|
1999-03-29 06:21:01 +00:00
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
static NS_DEFINE_IID(kSubtreeIteratorCID, NS_SUBTREEITERATOR_CID);
|
|
|
|
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
kLonely = 0,
|
|
|
|
kPrevSib = 1,
|
|
|
|
kNextSib = 2,
|
|
|
|
kBothSibs = 3
|
|
|
|
};
|
1999-03-29 06:21:01 +00:00
|
|
|
|
1999-04-05 17:21:59 +00:00
|
|
|
/********************************************************
|
|
|
|
* Constructor/Destructor
|
|
|
|
********************************************************/
|
1999-03-29 06:21:01 +00:00
|
|
|
|
|
|
|
nsHTMLEditRules::nsHTMLEditRules()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsHTMLEditRules::~nsHTMLEditRules()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
1999-04-05 17:21:59 +00:00
|
|
|
|
|
|
|
/********************************************************
|
|
|
|
* Public methods
|
|
|
|
********************************************************/
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-04-12 12:01:32 +00:00
|
|
|
nsHTMLEditRules::WillDoAction(nsIDOMSelection *aSelection,
|
|
|
|
nsRulesInfo *aInfo, PRBool *aCancel)
|
1999-04-05 17:21:59 +00:00
|
|
|
{
|
1999-07-26 18:05:41 +00:00
|
|
|
if (!aInfo || !aCancel)
|
1999-04-05 17:21:59 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-06-08 06:04:51 +00:00
|
|
|
|
|
|
|
*aCancel = PR_FALSE;
|
1999-04-05 17:21:59 +00:00
|
|
|
|
1999-04-12 12:01:32 +00:00
|
|
|
// my kingdom for dynamic cast
|
|
|
|
nsTextRulesInfo *info = NS_STATIC_CAST(nsTextRulesInfo*, aInfo);
|
|
|
|
|
|
|
|
switch (info->action)
|
1999-04-05 17:21:59 +00:00
|
|
|
{
|
1999-04-12 12:01:32 +00:00
|
|
|
case kInsertText:
|
|
|
|
return WillInsertText(aSelection,
|
|
|
|
aCancel,
|
1999-04-26 14:08:52 +00:00
|
|
|
info->placeTxn,
|
1999-04-12 12:01:32 +00:00
|
|
|
info->inString,
|
|
|
|
info->outString,
|
1999-06-08 06:04:51 +00:00
|
|
|
info->typeInState,
|
|
|
|
info->maxLength);
|
1999-04-05 17:21:59 +00:00
|
|
|
case kInsertBreak:
|
|
|
|
return WillInsertBreak(aSelection, aCancel);
|
1999-04-26 14:08:52 +00:00
|
|
|
case kDeleteSelection:
|
1999-05-05 23:27:17 +00:00
|
|
|
return WillDeleteSelection(aSelection, info->collapsedAction, aCancel);
|
1999-06-03 06:01:08 +00:00
|
|
|
case kMakeList:
|
1999-06-16 05:02:43 +00:00
|
|
|
return WillMakeList(aSelection, info->bOrdered, aCancel);
|
1999-06-03 06:01:08 +00:00
|
|
|
case kIndent:
|
|
|
|
return WillIndent(aSelection, aCancel);
|
|
|
|
case kOutdent:
|
|
|
|
return WillOutdent(aSelection, aCancel);
|
|
|
|
case kAlign:
|
1999-06-16 05:02:43 +00:00
|
|
|
return WillAlign(aSelection, info->alignType, aCancel);
|
1999-06-03 06:01:08 +00:00
|
|
|
case kMakeHeader:
|
|
|
|
return WillMakeHeader(aSelection, aCancel);
|
|
|
|
case kMakeAddress:
|
|
|
|
return WillMakeAddress(aSelection, aCancel);
|
|
|
|
case kMakePRE:
|
|
|
|
return WillMakePRE(aSelection, aCancel);
|
1999-04-05 17:21:59 +00:00
|
|
|
}
|
1999-04-12 12:01:32 +00:00
|
|
|
return nsTextEditRules::WillDoAction(aSelection, aInfo, aCancel);
|
1999-04-05 17:21:59 +00:00
|
|
|
}
|
|
|
|
|
1999-05-26 22:44:08 +00:00
|
|
|
|
1999-04-05 17:21:59 +00:00
|
|
|
NS_IMETHODIMP
|
1999-04-12 12:01:32 +00:00
|
|
|
nsHTMLEditRules::DidDoAction(nsIDOMSelection *aSelection,
|
|
|
|
nsRulesInfo *aInfo, nsresult aResult)
|
1999-04-05 17:21:59 +00:00
|
|
|
{
|
1999-05-26 22:44:08 +00:00
|
|
|
// my kingdom for dynamic cast
|
|
|
|
nsTextRulesInfo *info = NS_STATIC_CAST(nsTextRulesInfo*, aInfo);
|
|
|
|
|
|
|
|
switch (info->action)
|
|
|
|
{
|
|
|
|
case kInsertBreak:
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-04-12 12:01:32 +00:00
|
|
|
return nsTextEditRules::DidDoAction(aSelection, aInfo, aResult);
|
1999-04-05 17:21:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/********************************************************
|
1999-04-12 12:01:32 +00:00
|
|
|
* Protected rules methods
|
1999-04-05 17:21:59 +00:00
|
|
|
********************************************************/
|
1999-04-12 12:01:32 +00:00
|
|
|
|
|
|
|
nsresult
|
1999-06-08 06:04:51 +00:00
|
|
|
nsHTMLEditRules::WillInsertText(nsIDOMSelection *aSelection,
|
1999-04-12 12:01:32 +00:00
|
|
|
PRBool *aCancel,
|
|
|
|
PlaceholderTxn **aTxn,
|
|
|
|
const nsString *inString,
|
|
|
|
nsString *outString,
|
1999-06-08 06:04:51 +00:00
|
|
|
TypeInState typeInState,
|
|
|
|
PRInt32 aMaxLength)
|
1999-07-02 04:46:45 +00:00
|
|
|
{ if (!aSelection || !aCancel) { return NS_ERROR_NULL_POINTER; }
|
1999-04-12 12:01:32 +00:00
|
|
|
// initialize out param
|
|
|
|
*aCancel = PR_FALSE;
|
1999-07-02 04:46:45 +00:00
|
|
|
nsresult res;
|
1999-04-12 12:01:32 +00:00
|
|
|
|
1999-07-02 04:46:45 +00:00
|
|
|
char specialChars[] = {'\t',' ',nbsp,'\n',0};
|
1999-04-12 12:01:32 +00:00
|
|
|
|
1999-07-02 04:46:45 +00:00
|
|
|
mEditor->BeginTransaction(); // we have to batch this in case there are returns
|
|
|
|
// Insert Break txns don't auto merge with insert text txns
|
|
|
|
|
|
|
|
// strategy: there are simple cases and harder cases. The harder cases
|
|
|
|
// we handle recursively by breaking them into a series of simple cases.
|
|
|
|
// The simple cases are:
|
|
|
|
// 1) a single space
|
|
|
|
// 2) a single nbsp
|
|
|
|
// 3) a single tab
|
|
|
|
// 4) a single return
|
|
|
|
// 5) a run of chars containing no spaces, nbsp's, tabs, or returns
|
|
|
|
char nbspStr[2] = {nbsp, 0};
|
1999-04-12 12:01:32 +00:00
|
|
|
|
1999-07-02 04:46:45 +00:00
|
|
|
// is it a solo tab?
|
|
|
|
if (*inString == "\t" )
|
|
|
|
{
|
|
|
|
res = InsertTab(aSelection,aCancel,aTxn,outString);
|
|
|
|
}
|
|
|
|
// is it a solo space?
|
|
|
|
else if (*inString == " ")
|
|
|
|
{
|
|
|
|
res = InsertSpace(aSelection,aCancel,aTxn,outString);
|
|
|
|
}
|
|
|
|
// is it a solo nbsp?
|
|
|
|
else if (*inString == nbspStr)
|
|
|
|
{
|
|
|
|
res = InsertSpace(aSelection,aCancel,aTxn,outString);
|
|
|
|
}
|
|
|
|
// is it a solo return?
|
|
|
|
else if (*inString == "\n")
|
|
|
|
{
|
|
|
|
// special case - cancel default handling
|
|
|
|
*aCancel = PR_TRUE;
|
|
|
|
res = mEditor->InsertBreak();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// is it an innocous run of chars? no spaces, nbsps, returns, tabs?
|
|
|
|
PRInt32 pos = inString->FindCharInSet(specialChars);
|
|
|
|
if (pos == -1)
|
|
|
|
{
|
|
|
|
// no special chars, easy case
|
|
|
|
res = nsTextEditRules::WillInsertText(aSelection, aCancel, aTxn, inString, outString, typeInState, aMaxLength);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// else we need to break it up into two parts and recurse
|
|
|
|
*aCancel = PR_TRUE;
|
|
|
|
nsString firstString;
|
1999-07-02 05:13:21 +00:00
|
|
|
nsString restOfString(*inString);
|
1999-07-02 04:46:45 +00:00
|
|
|
// if first char is special, then use just it
|
|
|
|
if (pos == 0) pos = 1;
|
|
|
|
inString->Left(firstString, pos);
|
1999-07-02 05:13:21 +00:00
|
|
|
restOfString.Cut(0, pos);
|
1999-07-02 04:46:45 +00:00
|
|
|
if (NS_SUCCEEDED(mEditor->InsertText(firstString)))
|
|
|
|
{
|
1999-07-02 05:13:21 +00:00
|
|
|
res = mEditor->InsertText(restOfString);
|
1999-07-02 04:46:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mEditor->EndTransaction();
|
|
|
|
return res;
|
1999-04-12 12:01:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
1999-03-29 06:21:01 +00:00
|
|
|
nsHTMLEditRules::WillInsertBreak(nsIDOMSelection *aSelection, PRBool *aCancel)
|
|
|
|
{
|
|
|
|
if (!aSelection || !aCancel) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
// initialize out param
|
|
|
|
*aCancel = PR_FALSE;
|
1999-04-26 14:08:52 +00:00
|
|
|
|
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
// if the selection isn't collapsed, delete it.
|
|
|
|
PRBool bCollapsed;
|
1999-07-18 02:27:19 +00:00
|
|
|
res = aSelection->GetIsCollapsed(&bCollapsed);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!bCollapsed)
|
|
|
|
{
|
1999-05-05 23:27:17 +00:00
|
|
|
res = mEditor->DeleteSelection(nsIEditor::eDoNothing);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
//smart splitting rules
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
PRInt32 offset;
|
|
|
|
PRBool isPRE;
|
|
|
|
|
1999-05-28 21:17:30 +00:00
|
|
|
res = mEditor->GetStartNodeAndOffset(aSelection, &node, &offset);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!node) return NS_ERROR_FAILURE;
|
|
|
|
|
1999-05-28 21:17:30 +00:00
|
|
|
res = mEditor->IsPreformatted(node,&isPRE);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (isPRE)
|
|
|
|
{
|
|
|
|
nsString theString = "\n";
|
|
|
|
*aCancel = PR_TRUE;
|
|
|
|
return mEditor->InsertText(theString);
|
|
|
|
}
|
|
|
|
|
1999-05-28 21:17:30 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> blockParent = mEditor->GetBlockNodeParent(node);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (!blockParent) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// headers: put selection after the header
|
|
|
|
if (IsHeader(blockParent))
|
|
|
|
{
|
|
|
|
res = ReturnInHeader(aSelection, blockParent, node, offset);
|
|
|
|
*aCancel = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// paragraphs: special rules to look for <br>s
|
|
|
|
if (IsParagraph(blockParent))
|
|
|
|
{
|
|
|
|
res = ReturnInParagraph(aSelection, blockParent, node, offset, aCancel);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// list items: special rules to make new list items
|
|
|
|
if (IsListItem(blockParent))
|
|
|
|
{
|
|
|
|
res = ReturnInListItem(aSelection, blockParent, node, offset);
|
|
|
|
*aCancel = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-29 06:21:01 +00:00
|
|
|
return WillInsert(aSelection, aCancel);
|
|
|
|
}
|
|
|
|
|
1999-04-26 14:08:52 +00:00
|
|
|
|
|
|
|
|
1999-04-12 12:01:32 +00:00
|
|
|
nsresult
|
1999-05-05 23:27:17 +00:00
|
|
|
nsHTMLEditRules::WillDeleteSelection(nsIDOMSelection *aSelection, nsIEditor::ECollapsedSelectionAction aAction, PRBool *aCancel)
|
1999-04-26 14:08:52 +00:00
|
|
|
{
|
|
|
|
if (!aSelection || !aCancel) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
// initialize out param
|
|
|
|
*aCancel = PR_FALSE;
|
|
|
|
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
PRBool bCollapsed;
|
1999-07-18 02:27:19 +00:00
|
|
|
res = aSelection->GetIsCollapsed(&bCollapsed);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
PRInt32 offset;
|
|
|
|
|
1999-05-28 21:17:30 +00:00
|
|
|
res = mEditor->GetStartNodeAndOffset(aSelection, &node, &offset);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!node) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
if (bCollapsed)
|
|
|
|
{
|
|
|
|
// easy case, in a text node:
|
1999-05-28 21:17:30 +00:00
|
|
|
if (mEditor->IsTextNode(node))
|
1999-03-29 06:21:01 +00:00
|
|
|
{
|
1999-04-26 14:08:52 +00:00
|
|
|
nsCOMPtr<nsIDOMText> textNode = do_QueryInterface(node);
|
|
|
|
PRUint32 strLength;
|
|
|
|
res = textNode->GetLength(&strLength);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// at beginning of text node and backspaced?
|
1999-05-05 23:27:17 +00:00
|
|
|
if (!offset && (aAction == nsIEditor::eDeleteLeft))
|
1999-04-26 14:08:52 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> priorNode;
|
1999-05-28 21:17:30 +00:00
|
|
|
res = mEditor->GetPriorNode(node, PR_TRUE, getter_AddRefs(priorNode));
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
1999-05-01 19:37:50 +00:00
|
|
|
// if there is no prior node then cancel the deletion
|
|
|
|
if (!priorNode)
|
|
|
|
{
|
|
|
|
*aCancel = PR_TRUE;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
// block parents the same? use default deletion
|
1999-05-28 21:17:30 +00:00
|
|
|
if (mEditor->HasSameBlockNodeParent(node, priorNode)) return NS_OK;
|
1999-06-16 05:02:43 +00:00
|
|
|
|
|
|
|
// deleting across blocks
|
|
|
|
// are the blocks of same type?
|
|
|
|
nsCOMPtr<nsIDOMNode> leftParent = mEditor->GetBlockNodeParent(priorNode);
|
|
|
|
nsCOMPtr<nsIDOMNode> rightParent = mEditor->GetBlockNodeParent(node);
|
|
|
|
nsCOMPtr<nsIAtom> leftAtom = mEditor->GetTag(leftParent);
|
|
|
|
nsCOMPtr<nsIAtom> rightAtom = mEditor->GetTag(rightParent);
|
|
|
|
|
|
|
|
if (leftAtom.get() == rightAtom.get())
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> topParent;
|
|
|
|
leftParent->GetParentNode(getter_AddRefs(topParent));
|
|
|
|
|
|
|
|
if (IsParagraph(leftParent))
|
|
|
|
{
|
|
|
|
// join para's, insert break
|
1999-04-26 14:08:52 +00:00
|
|
|
*aCancel = PR_TRUE;
|
1999-05-28 21:17:30 +00:00
|
|
|
res = mEditor->JoinNodeDeep(leftParent,rightParent,aSelection);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = mEditor->InsertBreak();
|
|
|
|
return res;
|
1999-06-16 05:02:43 +00:00
|
|
|
}
|
|
|
|
if (IsListItem(leftParent) || IsHeader(leftParent))
|
|
|
|
{
|
|
|
|
// join blocks
|
1999-04-26 14:08:52 +00:00
|
|
|
*aCancel = PR_TRUE;
|
1999-05-28 21:17:30 +00:00
|
|
|
res = mEditor->JoinNodeDeep(leftParent,rightParent,aSelection);
|
1999-04-26 14:08:52 +00:00
|
|
|
return res;
|
1999-06-16 05:02:43 +00:00
|
|
|
}
|
|
|
|
}
|
1999-04-26 14:08:52 +00:00
|
|
|
|
|
|
|
// else blocks not same type, bail to default
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
// at end of text node and deleted?
|
1999-06-28 21:26:31 +00:00
|
|
|
if ((offset == (PRInt32)strLength)
|
|
|
|
&& (aAction == nsIEditor::eDeleteRight))
|
1999-03-29 06:21:01 +00:00
|
|
|
{
|
1999-04-26 14:08:52 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> nextNode;
|
1999-05-28 21:17:30 +00:00
|
|
|
res = mEditor->GetNextNode(node, PR_TRUE, getter_AddRefs(nextNode));
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1999-05-01 19:37:50 +00:00
|
|
|
|
|
|
|
// if there is no next node then cancel the deletion
|
|
|
|
if (!nextNode)
|
|
|
|
{
|
|
|
|
*aCancel = PR_TRUE;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
// block parents the same? use default deletion
|
1999-05-28 21:17:30 +00:00
|
|
|
if (mEditor->HasSameBlockNodeParent(node, nextNode)) return NS_OK;
|
1999-06-16 05:02:43 +00:00
|
|
|
|
|
|
|
// deleting across blocks
|
|
|
|
// are the blocks of same type?
|
|
|
|
nsCOMPtr<nsIDOMNode> leftParent = mEditor->GetBlockNodeParent(node);
|
|
|
|
nsCOMPtr<nsIDOMNode> rightParent = mEditor->GetBlockNodeParent(nextNode);
|
|
|
|
nsCOMPtr<nsIAtom> leftAtom = mEditor->GetTag(leftParent);
|
|
|
|
nsCOMPtr<nsIAtom> rightAtom = mEditor->GetTag(rightParent);
|
|
|
|
|
|
|
|
if (leftAtom.get() == rightAtom.get())
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> topParent;
|
|
|
|
leftParent->GetParentNode(getter_AddRefs(topParent));
|
|
|
|
|
|
|
|
if (IsParagraph(leftParent))
|
|
|
|
{
|
|
|
|
// join para's, insert break
|
1999-04-26 14:08:52 +00:00
|
|
|
*aCancel = PR_TRUE;
|
1999-05-28 21:17:30 +00:00
|
|
|
res = mEditor->JoinNodeDeep(leftParent,rightParent,aSelection);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = mEditor->InsertBreak();
|
|
|
|
return res;
|
1999-06-16 05:02:43 +00:00
|
|
|
}
|
|
|
|
if (IsListItem(leftParent) || IsHeader(leftParent))
|
|
|
|
{
|
|
|
|
// join blocks
|
1999-04-26 14:08:52 +00:00
|
|
|
*aCancel = PR_TRUE;
|
1999-05-28 21:17:30 +00:00
|
|
|
res = mEditor->JoinNodeDeep(leftParent,rightParent,aSelection);
|
1999-04-26 14:08:52 +00:00
|
|
|
return res;
|
1999-06-16 05:02:43 +00:00
|
|
|
}
|
|
|
|
}
|
1999-04-26 14:08:52 +00:00
|
|
|
|
|
|
|
// else blocks not same type, bail to default
|
|
|
|
return NS_OK;
|
|
|
|
|
1999-03-29 06:21:01 +00:00
|
|
|
}
|
1999-04-26 14:08:52 +00:00
|
|
|
|
|
|
|
// else do default
|
|
|
|
return NS_OK;
|
1999-03-29 06:21:01 +00:00
|
|
|
}
|
|
|
|
}
|
1999-04-26 14:08:52 +00:00
|
|
|
|
|
|
|
// else we have a non collapsed selection
|
|
|
|
// figure out if the enpoints are in nodes that can be merged
|
|
|
|
nsCOMPtr<nsIDOMNode> endNode;
|
|
|
|
PRInt32 endOffset;
|
1999-05-28 21:17:30 +00:00
|
|
|
res = mEditor->GetEndNodeAndOffset(aSelection, &endNode, &endOffset);
|
1999-07-15 19:13:46 +00:00
|
|
|
if (NS_FAILED(res))
|
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
1999-04-26 14:08:52 +00:00
|
|
|
if (endNode.get() != node.get())
|
|
|
|
{
|
1999-06-16 05:02:43 +00:00
|
|
|
// block parents the same? use default deletion
|
1999-05-28 21:17:30 +00:00
|
|
|
if (mEditor->HasSameBlockNodeParent(node, endNode)) return NS_OK;
|
1999-06-16 05:02:43 +00:00
|
|
|
|
|
|
|
// deleting across blocks
|
|
|
|
// are the blocks of same type?
|
1999-08-04 18:36:19 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> leftParent;
|
|
|
|
nsCOMPtr<nsIDOMNode> rightParent;
|
|
|
|
|
|
|
|
// XXX: Fix for bug #10815: Crash deleting selected text and table.
|
|
|
|
// Make sure leftParent and rightParent are never NULL. This
|
|
|
|
// can happen if we call GetBlockNodeParent() and the node we
|
|
|
|
// pass in is a body node.
|
|
|
|
//
|
|
|
|
// Should we be calling IsBlockNode() instead of IsBody() here?
|
|
|
|
|
|
|
|
if (IsBody(node))
|
|
|
|
leftParent = node;
|
|
|
|
else
|
|
|
|
leftParent = mEditor->GetBlockNodeParent(node);
|
|
|
|
|
|
|
|
if (IsBody(endNode))
|
|
|
|
rightParent = endNode;
|
|
|
|
else
|
|
|
|
rightParent = mEditor->GetBlockNodeParent(endNode);
|
1999-06-16 05:02:43 +00:00
|
|
|
|
|
|
|
// are the blocks siblings?
|
|
|
|
nsCOMPtr<nsIDOMNode> leftBlockParent;
|
|
|
|
nsCOMPtr<nsIDOMNode> rightBlockParent;
|
|
|
|
leftParent->GetParentNode(getter_AddRefs(leftBlockParent));
|
|
|
|
rightParent->GetParentNode(getter_AddRefs(rightBlockParent));
|
|
|
|
// bail to default if blocks aren't siblings
|
|
|
|
if (leftBlockParent.get() != rightBlockParent.get()) return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAtom> leftAtom = mEditor->GetTag(leftParent);
|
|
|
|
nsCOMPtr<nsIAtom> rightAtom = mEditor->GetTag(rightParent);
|
|
|
|
|
|
|
|
if (leftAtom.get() == rightAtom.get())
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> topParent;
|
|
|
|
leftParent->GetParentNode(getter_AddRefs(topParent));
|
|
|
|
|
|
|
|
if (IsParagraph(leftParent))
|
|
|
|
{
|
|
|
|
// first delete the selection
|
1999-04-26 14:08:52 +00:00
|
|
|
*aCancel = PR_TRUE;
|
1999-05-05 23:27:17 +00:00
|
|
|
res = mEditor->nsEditor::DeleteSelection(aAction);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1999-06-16 05:02:43 +00:00
|
|
|
// then join para's, insert break
|
1999-05-28 21:17:30 +00:00
|
|
|
res = mEditor->JoinNodeDeep(leftParent,rightParent,aSelection);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1999-05-04 08:03:07 +00:00
|
|
|
//res = mEditor->InsertBreak();
|
|
|
|
// uhh, no, we don't want to have the <br> on a deleted selction across para's
|
1999-04-26 14:08:52 +00:00
|
|
|
return res;
|
1999-06-16 05:02:43 +00:00
|
|
|
}
|
|
|
|
if (IsListItem(leftParent) || IsHeader(leftParent))
|
|
|
|
{
|
|
|
|
// first delete the selection
|
1999-04-26 14:08:52 +00:00
|
|
|
*aCancel = PR_TRUE;
|
1999-05-05 23:27:17 +00:00
|
|
|
res = mEditor->nsEditor::DeleteSelection(aAction);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1999-06-16 05:02:43 +00:00
|
|
|
// join blocks
|
1999-05-28 21:17:30 +00:00
|
|
|
res = mEditor->JoinNodeDeep(leftParent,rightParent,aSelection);
|
1999-04-26 14:08:52 +00:00
|
|
|
return res;
|
1999-06-16 05:02:43 +00:00
|
|
|
}
|
|
|
|
}
|
1999-04-26 14:08:52 +00:00
|
|
|
|
|
|
|
// else blocks not same type, bail to default
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-04-12 12:01:32 +00:00
|
|
|
|
1999-04-26 14:08:52 +00:00
|
|
|
return res;
|
|
|
|
}
|
1999-04-12 12:01:32 +00:00
|
|
|
|
1999-06-03 06:01:08 +00:00
|
|
|
|
|
|
|
nsresult
|
1999-06-16 05:02:43 +00:00
|
|
|
nsHTMLEditRules::WillMakeList(nsIDOMSelection *aSelection, PRBool aOrdered, PRBool *aCancel)
|
1999-06-03 06:01:08 +00:00
|
|
|
{
|
|
|
|
if (!aSelection || !aCancel) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
// initialize out param
|
1999-06-16 05:02:43 +00:00
|
|
|
*aCancel = PR_TRUE;
|
1999-06-03 06:01:08 +00:00
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
nsAutoSelectionReset selectionResetter(aSelection);
|
1999-07-01 13:42:03 +00:00
|
|
|
nsresult res;
|
1999-06-03 06:01:08 +00:00
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
// convert the selection ranges into "promoted" selection ranges:
|
|
|
|
// this basically just expands the range to include the immediate
|
|
|
|
// block parent, and then further expands to include any ancestors
|
|
|
|
// whose children are all in the range
|
1999-06-16 05:02:43 +00:00
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
nsCOMPtr<nsISupportsArray> arrayOfRanges;
|
|
|
|
res = GetPromotedRanges(aSelection, &arrayOfRanges, kMakeList);
|
1999-06-16 05:02:43 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
// use these ranges to contruct a list of nodes to act on.
|
|
|
|
nsCOMPtr<nsISupportsArray> arrayOfNodes;
|
|
|
|
res = GetNodesForOperation(arrayOfRanges, &arrayOfNodes, kMakeList);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
1999-06-25 09:33:02 +00:00
|
|
|
// if we ended up with any nodes in the list that aren't blocknodes,
|
|
|
|
// find their block parent instead and use that.
|
|
|
|
|
|
|
|
// i started writing this and then the sky fell. there are big questions
|
1999-07-01 13:42:03 +00:00
|
|
|
// about what to do here. i may need to switch from thinking about an array of
|
1999-06-25 09:33:02 +00:00
|
|
|
// nodes to act on to instead think of an array of ranges to act on.
|
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
// Next we remove all the <br>'s in the array. This won't prevent <br>'s
|
|
|
|
// inside of <p>'s from being significant - those <br>'s are not hit by
|
|
|
|
// the subtree iterator, since they are enclosed in a <p>.
|
|
|
|
|
|
|
|
// We also remove all non-editable nodes. Leave them be.
|
|
|
|
|
|
|
|
PRUint32 listCount;
|
|
|
|
PRInt32 i;
|
1999-07-01 13:42:03 +00:00
|
|
|
arrayOfNodes->Count(&listCount);
|
1999-06-16 05:02:43 +00:00
|
|
|
for (i=listCount-1; i>=0; i--)
|
|
|
|
{
|
1999-07-01 13:42:03 +00:00
|
|
|
nsCOMPtr<nsISupports> isupports = (dont_AddRef)(arrayOfNodes->ElementAt(i));
|
|
|
|
nsCOMPtr<nsIDOMNode> testNode( do_QueryInterface(isupports ) );
|
1999-06-16 05:02:43 +00:00
|
|
|
if (IsBreak(testNode))
|
|
|
|
{
|
1999-07-01 13:42:03 +00:00
|
|
|
arrayOfNodes->RemoveElementAt(i);
|
1999-06-16 05:02:43 +00:00
|
|
|
}
|
1999-07-01 18:42:28 +00:00
|
|
|
else if (!mEditor->IsEditable(testNode))
|
1999-06-16 05:02:43 +00:00
|
|
|
{
|
1999-07-01 13:42:03 +00:00
|
|
|
arrayOfNodes->RemoveElementAt(i);
|
1999-06-16 05:02:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-06-25 09:33:02 +00:00
|
|
|
// if there is only one node in the array, and it is a list, div, or blockquote,
|
|
|
|
// then look inside of it until we find what we want to make a list out of.
|
1999-07-01 13:42:03 +00:00
|
|
|
arrayOfNodes->Count(&listCount);
|
1999-06-25 09:33:02 +00:00
|
|
|
if (listCount == 1)
|
|
|
|
{
|
1999-07-01 13:42:03 +00:00
|
|
|
nsCOMPtr<nsISupports> isupports = (dont_AddRef)(arrayOfNodes->ElementAt(0));
|
|
|
|
nsCOMPtr<nsIDOMNode> curNode( do_QueryInterface(isupports) );
|
1999-06-25 09:33:02 +00:00
|
|
|
|
|
|
|
while (IsDiv(curNode) || IsOrderedList(curNode) || IsUnorderedList(curNode) || IsBlockquote(curNode))
|
|
|
|
{
|
|
|
|
// dive as long as there is only one child, and it is a list, div, or blockquote
|
1999-06-25 10:36:54 +00:00
|
|
|
PRUint32 numChildren;
|
1999-07-01 18:42:28 +00:00
|
|
|
res = mEditor->CountEditableChildren(curNode, numChildren);
|
1999-06-25 09:33:02 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (numChildren == 1)
|
|
|
|
{
|
|
|
|
// keep diving
|
|
|
|
nsCOMPtr <nsIDOMNode> tmpNode = nsEditor::GetChildAt(curNode, 0);
|
1999-07-01 13:42:03 +00:00
|
|
|
if (IsDiv(tmpNode) || IsOrderedList(tmpNode) || IsUnorderedList(tmpNode) || IsBlockquote(tmpNode))
|
|
|
|
{
|
|
|
|
// check editablility XXX moose
|
|
|
|
curNode = tmpNode;
|
|
|
|
}
|
|
|
|
else break;
|
1999-06-25 09:33:02 +00:00
|
|
|
}
|
1999-07-01 13:42:03 +00:00
|
|
|
else break;
|
1999-06-25 09:33:02 +00:00
|
|
|
}
|
|
|
|
// we've found innermost list/blockquote/div:
|
|
|
|
// replace the one node in the array with this node
|
1999-07-01 13:42:03 +00:00
|
|
|
isupports = do_QueryInterface(curNode);
|
|
|
|
arrayOfNodes->ReplaceElementAt(isupports, 0);
|
1999-06-25 09:33:02 +00:00
|
|
|
}
|
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
// Next we detect all the transitions in the array, where a transition
|
|
|
|
// means that adjacent nodes in the array don't have the same parent.
|
|
|
|
|
|
|
|
nsVoidArray transitionList;
|
1999-07-01 13:42:03 +00:00
|
|
|
res = MakeTransitionList(arrayOfNodes, &transitionList);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
1999-06-25 09:33:02 +00:00
|
|
|
// Ok, now go through all the nodes and put then in the list,
|
1999-06-16 05:02:43 +00:00
|
|
|
// or whatever is approriate. Wohoo!
|
1999-07-01 13:42:03 +00:00
|
|
|
|
|
|
|
arrayOfNodes->Count(&listCount);
|
1999-06-16 05:02:43 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> curParent;
|
|
|
|
nsCOMPtr<nsIDOMNode> curList;
|
|
|
|
|
1999-06-25 09:33:02 +00:00
|
|
|
for (i=0; i<listCount; i++)
|
1999-06-16 05:02:43 +00:00
|
|
|
{
|
1999-06-25 09:33:02 +00:00
|
|
|
// here's where we actually figure out what to do
|
1999-07-01 13:42:03 +00:00
|
|
|
nsCOMPtr<nsISupports> isupports = (dont_AddRef)(arrayOfNodes->ElementAt(i));
|
|
|
|
nsCOMPtr<nsIDOMNode> curNode( do_QueryInterface(isupports ) );
|
1999-06-25 09:33:02 +00:00
|
|
|
PRInt32 offset;
|
|
|
|
res = nsEditor::GetNodeLocation(curNode, &curParent, &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (transitionList[i] && // transition node
|
|
|
|
((((i+1)<listCount) && transitionList[i+1]) || // and next node is transistion node
|
|
|
|
( i+1 >= listCount))) // or there is no next node
|
1999-06-16 05:02:43 +00:00
|
|
|
{
|
1999-06-25 09:33:02 +00:00
|
|
|
// the parent of this node has no other children on the
|
|
|
|
// list of nodes to make a list out of. So if this node
|
|
|
|
// is a list, change it's list type if needed instead of
|
|
|
|
// reparenting it
|
|
|
|
nsCOMPtr<nsIDOMNode> newBlock;
|
|
|
|
if (IsUnorderedList(curNode))
|
1999-06-16 05:02:43 +00:00
|
|
|
{
|
1999-06-25 09:33:02 +00:00
|
|
|
if (aOrdered)
|
|
|
|
{
|
|
|
|
// make a new ordered list, insert it where the current unordered list is,
|
|
|
|
// and move all the children to the new list, and remove the old list
|
|
|
|
nsAutoString blockType("ol");
|
|
|
|
res = ReplaceContainer(curNode,&newBlock,blockType);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
curList = newBlock;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// do nothing, we are already the right kind of list
|
|
|
|
curList = newBlock;
|
|
|
|
continue;
|
|
|
|
}
|
1999-06-16 05:02:43 +00:00
|
|
|
}
|
1999-06-25 09:33:02 +00:00
|
|
|
else if (IsOrderedList(curNode))
|
1999-06-16 05:02:43 +00:00
|
|
|
{
|
1999-06-25 09:33:02 +00:00
|
|
|
if (!aOrdered)
|
|
|
|
{
|
|
|
|
// make a new unordered list, insert it where the current ordered list is,
|
|
|
|
// and move all the children to the new list, and remove the old list
|
|
|
|
nsAutoString blockType("ul");
|
|
|
|
ReplaceContainer(curNode,&newBlock,blockType);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
curList = newBlock;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// do nothing, we are already the right kind of list
|
|
|
|
curList = newBlock;
|
|
|
|
continue;
|
|
|
|
}
|
1999-06-16 05:02:43 +00:00
|
|
|
}
|
1999-06-25 09:33:02 +00:00
|
|
|
else if (IsDiv(curNode) || IsBlockquote(curNode))
|
1999-06-16 05:02:43 +00:00
|
|
|
{
|
1999-06-25 09:33:02 +00:00
|
|
|
// XXX moose
|
1999-06-16 05:02:43 +00:00
|
|
|
}
|
1999-06-25 09:33:02 +00:00
|
|
|
} // lonely node
|
|
|
|
|
|
|
|
// need to make a list to put things in if we haven't already,
|
|
|
|
// or if this node doesn't go in list we used earlier.
|
|
|
|
if (!curList || transitionList[i])
|
|
|
|
{
|
|
|
|
nsAutoString listType;
|
|
|
|
if (aOrdered) listType = "ol";
|
|
|
|
else listType = "ul";
|
|
|
|
res = mEditor->CreateNode(listType, curParent, offset, getter_AddRefs(curList));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
// curList is now the correct thing to put curNode in
|
1999-06-16 05:02:43 +00:00
|
|
|
}
|
|
|
|
|
1999-06-25 09:33:02 +00:00
|
|
|
// if curNode isn't a list item, we must wrap it in one
|
|
|
|
nsCOMPtr<nsIDOMNode> listItem;
|
|
|
|
if (!IsListItem(curNode))
|
|
|
|
{
|
|
|
|
nsAutoString listItemType = "li";
|
|
|
|
res = InsertContainerAbove(curNode, &listItem, listItemType);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
listItem = curNode;
|
|
|
|
}
|
1999-06-16 05:02:43 +00:00
|
|
|
|
1999-06-25 09:33:02 +00:00
|
|
|
// tuck the listItem into the end of the active list
|
|
|
|
PRUint32 listLen;
|
|
|
|
res = mEditor->GetLengthOfDOMNode(curList, listLen);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = mEditor->DeleteNode(listItem);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = mEditor->InsertNode(listItem, curList, listLen);
|
1999-06-16 05:02:43 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
|
1999-06-03 06:01:08 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::WillIndent(nsIDOMSelection *aSelection, PRBool *aCancel)
|
|
|
|
{
|
|
|
|
if (!aSelection || !aCancel) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
// initialize out param
|
1999-06-16 05:02:43 +00:00
|
|
|
*aCancel = PR_TRUE;
|
1999-06-03 06:01:08 +00:00
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
nsAutoSelectionReset selectionResetter(aSelection);
|
1999-07-01 13:42:03 +00:00
|
|
|
nsresult res;
|
1999-06-16 05:02:43 +00:00
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
// convert the selection ranges into "promoted" selection ranges:
|
|
|
|
// this basically just expands the range to include the immediate
|
|
|
|
// block parent, and then further expands to include any ancestors
|
|
|
|
// whose children are all in the range
|
1999-06-16 05:02:43 +00:00
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
nsCOMPtr<nsISupportsArray> arrayOfRanges;
|
|
|
|
res = GetPromotedRanges(aSelection, &arrayOfRanges, kIndent);
|
1999-06-16 05:02:43 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
// use these ranges to contruct a list of nodes to act on.
|
|
|
|
nsCOMPtr<nsISupportsArray> arrayOfNodes;
|
|
|
|
res = GetNodesForOperation(arrayOfRanges, &arrayOfNodes, kIndent);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
// Next we detect all the transitions in the array, where a transition
|
|
|
|
// means that adjacent nodes in the array don't have the same parent.
|
|
|
|
|
|
|
|
nsVoidArray transitionList;
|
1999-07-01 13:42:03 +00:00
|
|
|
res = MakeTransitionList(arrayOfNodes, &transitionList);
|
|
|
|
if (NS_FAILED(res)) return res;
|
1999-06-16 05:02:43 +00:00
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
// Ok, now go through all the nodes and put them in a blockquote,
|
1999-06-16 05:02:43 +00:00
|
|
|
// or whatever is appropriate. Wohoo!
|
1999-07-01 13:42:03 +00:00
|
|
|
|
|
|
|
PRUint32 listCount;
|
|
|
|
PRInt32 i;
|
|
|
|
arrayOfNodes->Count(&listCount);
|
1999-06-16 05:02:43 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> curParent;
|
|
|
|
nsCOMPtr<nsIDOMNode> curQuote;
|
|
|
|
for (i=0; i<listCount; i++)
|
|
|
|
{
|
|
|
|
// here's where we actually figure out what to do
|
1999-07-01 13:42:03 +00:00
|
|
|
nsCOMPtr<nsISupports> isupports = (dont_AddRef)(arrayOfNodes->ElementAt(i));
|
|
|
|
nsCOMPtr<nsIDOMNode> curNode( do_QueryInterface(isupports ) );
|
1999-06-16 05:02:43 +00:00
|
|
|
PRInt32 offset;
|
|
|
|
res = nsEditor::GetNodeLocation(curNode, &curParent, &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// need to make a blockquote to put things in if we haven't already,
|
|
|
|
// or if this node doesn't go in list we used earlier.
|
|
|
|
if (!curQuote || transitionList[i])
|
|
|
|
{
|
|
|
|
nsAutoString quoteType("blockquote");
|
1999-07-19 22:49:21 +00:00
|
|
|
if (mEditor->CanContainTag(curParent,quoteType))
|
|
|
|
{
|
|
|
|
res = mEditor->CreateNode(quoteType, curParent, offset, getter_AddRefs(curQuote));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
// curQuote is now the correct thing to put curNode in
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
printf("trying to put a blockquote in a bad place\n");
|
|
|
|
}
|
1999-06-16 05:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// tuck the node into the end of the active blockquote
|
|
|
|
PRUint32 listLen;
|
|
|
|
res = mEditor->GetLengthOfDOMNode(curQuote, listLen);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = mEditor->DeleteNode(curNode);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = mEditor->InsertNode(curNode, curQuote, listLen);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
|
1999-06-03 06:01:08 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::WillOutdent(nsIDOMSelection *aSelection, PRBool *aCancel)
|
|
|
|
{
|
|
|
|
if (!aSelection || !aCancel) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
// initialize out param
|
1999-06-16 05:02:43 +00:00
|
|
|
*aCancel = PR_TRUE;
|
1999-06-03 06:01:08 +00:00
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
nsAutoSelectionReset selectionResetter(aSelection);
|
1999-06-03 06:01:08 +00:00
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
// convert the selection ranges into "promoted" selection ranges:
|
|
|
|
// this basically just expands the range to include the immediate
|
|
|
|
// block parent, and then further expands to include any ancestors
|
|
|
|
// whose children are all in the range
|
1999-06-16 05:02:43 +00:00
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
nsCOMPtr<nsISupportsArray> arrayOfRanges;
|
|
|
|
res = GetPromotedRanges(aSelection, &arrayOfRanges, kOutdent);
|
1999-06-16 05:02:43 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
// use these ranges to contruct a list of nodes to act on.
|
|
|
|
nsCOMPtr<nsISupportsArray> arrayOfNodes;
|
|
|
|
res = GetNodesForOperation(arrayOfRanges, &arrayOfNodes, kOutdent);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
// Next we detect all the transitions in the array, where a transition
|
|
|
|
// means that adjacent nodes in the array don't have the same parent.
|
|
|
|
|
|
|
|
nsVoidArray transitionList;
|
1999-07-01 13:42:03 +00:00
|
|
|
res = MakeTransitionList(arrayOfNodes, &transitionList);
|
|
|
|
if (NS_FAILED(res)) return res;
|
1999-06-16 05:02:43 +00:00
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
// Ok, now go through all the nodes and remove a level of blockquoting,
|
1999-06-16 05:02:43 +00:00
|
|
|
// or whatever is appropriate. Wohoo!
|
1999-07-01 13:42:03 +00:00
|
|
|
|
|
|
|
PRUint32 listCount;
|
|
|
|
PRInt32 i;
|
|
|
|
arrayOfNodes->Count(&listCount);
|
1999-06-16 05:02:43 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> curParent;
|
|
|
|
for (i=0; i<listCount; i++)
|
|
|
|
{
|
|
|
|
// here's where we actually figure out what to do
|
1999-07-01 13:42:03 +00:00
|
|
|
nsCOMPtr<nsISupports> isupports = (dont_AddRef)(arrayOfNodes->ElementAt(i));
|
|
|
|
nsCOMPtr<nsIDOMNode> curNode( do_QueryInterface(isupports ) );
|
1999-06-16 05:02:43 +00:00
|
|
|
PRInt32 offset;
|
|
|
|
res = nsEditor::GetNodeLocation(curNode, &curParent, &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (transitionList[i])
|
|
|
|
{
|
1999-06-25 09:33:02 +00:00
|
|
|
// look for a blockquote somewhere above us and remove it.
|
1999-06-16 05:02:43 +00:00
|
|
|
// this is a hack until i think about outdent for real.
|
|
|
|
nsCOMPtr<nsIDOMNode> n = curNode;
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
while (!IsBody(n))
|
|
|
|
{
|
|
|
|
if (IsBlockquote(n))
|
|
|
|
{
|
1999-06-25 09:33:02 +00:00
|
|
|
RemoveContainer(n);
|
1999-06-16 05:02:43 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
n->GetParentNode(getter_AddRefs(tmp));
|
|
|
|
n = tmp;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-06-03 06:01:08 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
1999-06-16 05:02:43 +00:00
|
|
|
nsHTMLEditRules::WillAlign(nsIDOMSelection *aSelection, const nsString *alignType, PRBool *aCancel)
|
1999-06-03 06:01:08 +00:00
|
|
|
{
|
|
|
|
if (!aSelection || !aCancel) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
// initialize out param
|
1999-06-16 05:02:43 +00:00
|
|
|
*aCancel = PR_TRUE;
|
1999-06-03 06:01:08 +00:00
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
nsAutoSelectionReset selectionResetter(aSelection);
|
1999-06-03 06:01:08 +00:00
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
// convert the selection ranges into "promoted" selection ranges:
|
|
|
|
// this basically just expands the range to include the immediate
|
|
|
|
// block parent, and then further expands to include any ancestors
|
|
|
|
// whose children are all in the range
|
1999-06-16 05:02:43 +00:00
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
nsCOMPtr<nsISupportsArray> arrayOfRanges;
|
|
|
|
res = GetPromotedRanges(aSelection, &arrayOfRanges, kAlign);
|
1999-06-16 05:02:43 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
// use these ranges to contruct a list of nodes to act on.
|
|
|
|
nsCOMPtr<nsISupportsArray> arrayOfNodes;
|
|
|
|
res = GetNodesForOperation(arrayOfRanges, &arrayOfNodes, kAlign);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
// Next we detect all the transitions in the array, where a transition
|
|
|
|
// means that adjacent nodes in the array don't have the same parent.
|
|
|
|
|
|
|
|
nsVoidArray transitionList;
|
1999-07-01 13:42:03 +00:00
|
|
|
res = MakeTransitionList(arrayOfNodes, &transitionList);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// Ok, now go through all the nodes and give them an align attrib or put them in a div,
|
1999-06-16 05:02:43 +00:00
|
|
|
// or whatever is appropriate. Wohoo!
|
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
PRUint32 listCount;
|
|
|
|
PRInt32 i;
|
|
|
|
arrayOfNodes->Count(&listCount);
|
1999-06-16 05:02:43 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> curParent;
|
|
|
|
nsCOMPtr<nsIDOMNode> curDiv;
|
|
|
|
for (i=0; i<listCount; i++)
|
|
|
|
{
|
|
|
|
// here's where we actually figure out what to do
|
1999-07-01 13:42:03 +00:00
|
|
|
nsCOMPtr<nsISupports> isupports = (dont_AddRef)(arrayOfNodes->ElementAt(i));
|
|
|
|
nsCOMPtr<nsIDOMNode> curNode( do_QueryInterface(isupports ) );
|
1999-06-16 05:02:43 +00:00
|
|
|
PRInt32 offset;
|
|
|
|
res = nsEditor::GetNodeLocation(curNode, &curParent, &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
1999-06-25 09:33:02 +00:00
|
|
|
// if it's a div, don't nest it, just set the alignment
|
|
|
|
if (IsDiv(curNode))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMElement> divElem = do_QueryInterface(curNode);
|
|
|
|
nsAutoString attr("align");
|
|
|
|
res = mEditor->SetAttribute(divElem, attr, *alignType);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
// clear out curDiv so that we don't put nodes after this one into it
|
|
|
|
curDiv = 0;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
// need to make a div to put things in if we haven't already,
|
|
|
|
// or if this node doesn't go in div we used earlier.
|
|
|
|
if (!curDiv || transitionList[i])
|
|
|
|
{
|
|
|
|
nsAutoString divType("div");
|
|
|
|
res = mEditor->CreateNode(divType, curParent, offset, getter_AddRefs(curDiv));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
// set up the alignment on the div
|
|
|
|
nsCOMPtr<nsIDOMElement> divElem = do_QueryInterface(curDiv);
|
|
|
|
nsAutoString attr("align");
|
|
|
|
res = mEditor->SetAttribute(divElem, attr, *alignType);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
// curDiv is now the correct thing to put curNode in
|
|
|
|
}
|
|
|
|
|
|
|
|
// tuck the node into the end of the active div
|
|
|
|
PRUint32 listLen;
|
|
|
|
res = mEditor->GetLengthOfDOMNode(curDiv, listLen);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = mEditor->DeleteNode(curNode);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = mEditor->InsertNode(curNode, curDiv, listLen);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
|
1999-06-03 06:01:08 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::WillMakeHeader(nsIDOMSelection *aSelection, PRBool *aCancel)
|
|
|
|
{
|
|
|
|
if (!aSelection || !aCancel) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
// initialize out param
|
|
|
|
*aCancel = PR_FALSE;
|
|
|
|
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::WillMakeAddress(nsIDOMSelection *aSelection, PRBool *aCancel)
|
|
|
|
{
|
|
|
|
if (!aSelection || !aCancel) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
// initialize out param
|
|
|
|
*aCancel = PR_FALSE;
|
|
|
|
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::WillMakePRE(nsIDOMSelection *aSelection, PRBool *aCancel)
|
|
|
|
{
|
|
|
|
if (!aSelection || !aCancel) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
// initialize out param
|
|
|
|
*aCancel = PR_FALSE;
|
|
|
|
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-12 12:01:32 +00:00
|
|
|
/********************************************************
|
|
|
|
* helper methods
|
|
|
|
********************************************************/
|
|
|
|
|
1999-04-26 14:08:52 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsHeader: true if node an html header
|
|
|
|
//
|
|
|
|
PRBool
|
|
|
|
nsHTMLEditRules::IsHeader(nsIDOMNode *node)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(node, "null parent passed to nsHTMLEditRules::IsHeader");
|
1999-05-28 21:17:30 +00:00
|
|
|
nsAutoString tag;
|
|
|
|
nsEditor::GetTagString(node,tag);
|
|
|
|
if ( (tag == "h1") ||
|
|
|
|
(tag == "h2") ||
|
|
|
|
(tag == "h3") ||
|
|
|
|
(tag == "h4") ||
|
|
|
|
(tag == "h5") ||
|
|
|
|
(tag == "h6") )
|
1999-04-26 14:08:52 +00:00
|
|
|
{
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsParagraph: true if node an html paragraph
|
|
|
|
//
|
|
|
|
PRBool
|
|
|
|
nsHTMLEditRules::IsParagraph(nsIDOMNode *node)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(node, "null parent passed to nsHTMLEditRules::IsParagraph");
|
1999-05-28 21:17:30 +00:00
|
|
|
nsAutoString tag;
|
|
|
|
nsEditor::GetTagString(node,tag);
|
|
|
|
if (tag == "p")
|
1999-04-26 14:08:52 +00:00
|
|
|
{
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsListItem: true if node an html list item
|
|
|
|
//
|
|
|
|
PRBool
|
|
|
|
nsHTMLEditRules::IsListItem(nsIDOMNode *node)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(node, "null parent passed to nsHTMLEditRules::IsListItem");
|
1999-05-28 21:17:30 +00:00
|
|
|
nsAutoString tag;
|
|
|
|
nsEditor::GetTagString(node,tag);
|
|
|
|
if (tag == "li")
|
1999-04-26 14:08:52 +00:00
|
|
|
{
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsOrderedList: true if node an html orderd list
|
|
|
|
//
|
|
|
|
PRBool
|
|
|
|
nsHTMLEditRules::IsOrderedList(nsIDOMNode *node)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(node, "null parent passed to nsHTMLEditRules::IsOrderedList");
|
|
|
|
nsAutoString tag;
|
|
|
|
nsEditor::GetTagString(node,tag);
|
|
|
|
if (tag == "ol")
|
|
|
|
{
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsUnorderedList: true if node an html orderd list
|
|
|
|
//
|
|
|
|
PRBool
|
|
|
|
nsHTMLEditRules::IsUnorderedList(nsIDOMNode *node)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(node, "null parent passed to nsHTMLEditRules::IsUnorderedList");
|
|
|
|
nsAutoString tag;
|
|
|
|
nsEditor::GetTagString(node,tag);
|
|
|
|
if (tag == "ul")
|
|
|
|
{
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-26 14:08:52 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
1999-05-28 21:17:30 +00:00
|
|
|
// IsBreak: true if node an html break node
|
1999-04-26 14:08:52 +00:00
|
|
|
//
|
|
|
|
PRBool
|
|
|
|
nsHTMLEditRules::IsBreak(nsIDOMNode *node)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(node, "null parent passed to nsHTMLEditRules::IsBreak");
|
1999-05-28 21:17:30 +00:00
|
|
|
nsAutoString tag;
|
|
|
|
nsEditor::GetTagString(node,tag);
|
|
|
|
if (tag == "br")
|
1999-04-26 14:08:52 +00:00
|
|
|
{
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-04-21 14:49:58 +00:00
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsBody: true if node an html body node
|
|
|
|
//
|
|
|
|
PRBool
|
|
|
|
nsHTMLEditRules::IsBody(nsIDOMNode *node)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(node, "null parent passed to nsHTMLEditRules::IsBody");
|
|
|
|
nsAutoString tag;
|
|
|
|
nsEditor::GetTagString(node,tag);
|
|
|
|
if (tag == "body")
|
|
|
|
{
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsBlockquote: true if node an html blockquote node
|
|
|
|
//
|
|
|
|
PRBool
|
|
|
|
nsHTMLEditRules::IsBlockquote(nsIDOMNode *node)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(node, "null parent passed to nsHTMLEditRules::IsBlockquote");
|
|
|
|
nsAutoString tag;
|
|
|
|
nsEditor::GetTagString(node,tag);
|
|
|
|
if (tag == "blockquote")
|
|
|
|
{
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-25 09:33:02 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsDiv: true if node an html div node
|
|
|
|
//
|
|
|
|
PRBool
|
|
|
|
nsHTMLEditRules::IsDiv(nsIDOMNode *node)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(node, "null parent passed to nsHTMLEditRules::IsDiv");
|
|
|
|
nsAutoString tag;
|
|
|
|
nsEditor::GetTagString(node,tag);
|
|
|
|
if (tag == "div")
|
|
|
|
{
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-21 14:49:58 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetTabAsNBSPs: stuff the right number of nbsp's into outString
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::GetTabAsNBSPs(nsString *outString)
|
|
|
|
{
|
|
|
|
if (!outString) return NS_ERROR_NULL_POINTER;
|
|
|
|
// XXX - this should get the right number from prefs
|
|
|
|
*outString += nbsp; *outString += nbsp; *outString += nbsp; *outString += nbsp;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetTabAsNBSPsAndSpace: stuff the right number of nbsp's followed by a
|
|
|
|
// space into outString
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::GetTabAsNBSPsAndSpace(nsString *outString)
|
|
|
|
{
|
|
|
|
if (!outString) return NS_ERROR_NULL_POINTER;
|
|
|
|
// XXX - this should get the right number from prefs
|
|
|
|
*outString += nbsp; *outString += nbsp; *outString += nbsp; *outString += ' ';
|
1999-04-13 13:35:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsFirstNode: Are we the first edittable node in our parent?
|
|
|
|
//
|
|
|
|
PRBool
|
|
|
|
nsHTMLEditRules::IsFirstNode(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
PRInt32 offset, j=0;
|
|
|
|
nsEditor::GetNodeLocation(aNode, &parent, &offset);
|
|
|
|
if (!offset) // easy case, we are first dom child
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
// ok, so there are earlier children. But are they editable???
|
|
|
|
nsCOMPtr<nsIDOMNodeList>childList;
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
parent->GetChildNodes(getter_AddRefs(childList));
|
|
|
|
while (j < offset)
|
|
|
|
{
|
|
|
|
childList->Item(j, getter_AddRefs(child));
|
1999-07-01 18:42:28 +00:00
|
|
|
if (mEditor->IsEditable(child))
|
1999-06-16 05:02:43 +00:00
|
|
|
return PR_FALSE;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsLastNode: Are we the first edittable node in our parent?
|
|
|
|
//
|
|
|
|
PRBool
|
|
|
|
nsHTMLEditRules::IsLastNode(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
PRInt32 offset, j;
|
|
|
|
PRUint32 numChildren;
|
|
|
|
nsEditor::GetNodeLocation(aNode, &parent, &offset);
|
|
|
|
nsEditor::GetLengthOfDOMNode(parent, numChildren);
|
|
|
|
if (offset+1 == numChildren) // easy case, we are last dom child
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
// ok, so there are later children. But are they editable???
|
|
|
|
j = offset+1;
|
|
|
|
nsCOMPtr<nsIDOMNodeList>childList;
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
parent->GetChildNodes(getter_AddRefs(childList));
|
|
|
|
while (j < numChildren)
|
|
|
|
{
|
|
|
|
childList->Item(j, getter_AddRefs(child));
|
1999-07-01 18:42:28 +00:00
|
|
|
if (mEditor->IsEditable(child))
|
1999-06-16 05:02:43 +00:00
|
|
|
return PR_FALSE;
|
|
|
|
j++;
|
|
|
|
}
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetPromotedPoint: figure out where a start or end point for a block
|
|
|
|
// operation really is
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::GetPromotedPoint(RulesEndpoint aWhere, nsIDOMNode *aNode, PRInt32 aOffset,
|
|
|
|
PRInt32 actionID, nsCOMPtr<nsIDOMNode> *outNode, PRInt32 *outOffset)
|
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
nsCOMPtr<nsIDOMNode> node = aNode;
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = aNode;
|
|
|
|
PRInt32 offset = aOffset;
|
|
|
|
|
|
|
|
if (aWhere == kStart)
|
|
|
|
{
|
|
|
|
// some special casing for text nodes
|
|
|
|
if (nsEditor::IsTextNode(aNode))
|
|
|
|
{
|
|
|
|
nsEditor::GetNodeLocation(aNode, &parent, &offset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node = nsEditor::GetChildAt(parent,offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// finding the real start for this point. look up the tree for as long as we are the
|
|
|
|
// first node in the container, and as long as we haven't hit the body node.
|
|
|
|
while ((IsFirstNode(node)) && (!IsBody(parent)))
|
|
|
|
{
|
|
|
|
node = parent;
|
|
|
|
res = nsEditor::GetNodeLocation(node, &parent, &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
*outNode = parent;
|
|
|
|
*outOffset = offset;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aWhere == kEnd)
|
|
|
|
{
|
|
|
|
// some special casing for text nodes
|
|
|
|
if (nsEditor::IsTextNode(aNode))
|
|
|
|
{
|
|
|
|
nsEditor::GetNodeLocation(aNode, &parent, &offset);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node = nsEditor::GetChildAt(parent,offset);
|
|
|
|
}
|
1999-08-04 18:36:19 +00:00
|
|
|
|
|
|
|
if (node)
|
|
|
|
offset++; // since this is going to be used for a range _endpoint_, we want to be after the node
|
|
|
|
else
|
|
|
|
node = parent;
|
1999-06-16 05:02:43 +00:00
|
|
|
|
|
|
|
// finding the real end for this point. look up the tree for as long as we are the
|
|
|
|
// last node in the container, and as long as we haven't hit the body node.
|
|
|
|
while ((IsLastNode(node)) && (!IsBody(parent)))
|
|
|
|
{
|
|
|
|
node = parent;
|
|
|
|
res = nsEditor::GetNodeLocation(node, &parent, &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
*outNode = parent;
|
|
|
|
*outOffset = offset;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1999-07-01 13:42:03 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetPromotedRanges: run all the selection range endpoint through
|
|
|
|
// GetPromotedPoint()
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::GetPromotedRanges(nsIDOMSelection *inSelection,
|
|
|
|
nsCOMPtr<nsISupportsArray> *outArrayOfRanges,
|
|
|
|
PRInt32 inOperationType)
|
|
|
|
{
|
|
|
|
if (!inSelection || !outArrayOfRanges) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult res = NS_NewISupportsArray(getter_AddRefs(*outArrayOfRanges));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
PRInt32 rangeCount;
|
1999-07-18 02:27:19 +00:00
|
|
|
res = inSelection->GetRangeCount(&rangeCount);
|
1999-07-01 13:42:03 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
PRInt32 i;
|
|
|
|
nsCOMPtr<nsIDOMRange> selectionRange;
|
|
|
|
nsCOMPtr<nsIDOMNode> startNode;
|
|
|
|
nsCOMPtr<nsIDOMNode> endNode;
|
|
|
|
PRInt32 startOffset, endOffset;
|
|
|
|
|
|
|
|
for (i = 0; i < rangeCount; i++)
|
|
|
|
{
|
1999-07-18 02:27:19 +00:00
|
|
|
res = inSelection->GetRangeAt(i, getter_AddRefs(selectionRange));
|
1999-07-01 13:42:03 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = selectionRange->GetStartParent(getter_AddRefs(startNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = selectionRange->GetStartOffset(&startOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = selectionRange->GetEndParent(getter_AddRefs(endNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = selectionRange->GetEndOffset(&endOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// make a new adjusted range to represent the appropriate block content
|
|
|
|
// this is tricky. the basic idea is to push out the range endpoints
|
|
|
|
// to truly enclose the blocks that we will affect
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> opStartNode;
|
|
|
|
nsCOMPtr<nsIDOMNode> opEndNode;
|
|
|
|
PRInt32 opStartOffset, opEndOffset;
|
|
|
|
nsCOMPtr<nsIDOMRange> opRange;
|
|
|
|
|
|
|
|
res = GetPromotedPoint( kStart, startNode, startOffset, inOperationType, &opStartNode, &opStartOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = GetPromotedPoint( kEnd, endNode, endOffset, inOperationType, &opEndNode, &opEndOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = selectionRange->Clone(getter_AddRefs(opRange));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
opRange->SetStart(opStartNode, opStartOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
opRange->SetEnd(opEndNode, opEndOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// stuff new opRange into nsISupportsArray
|
|
|
|
nsCOMPtr<nsISupports> isupports = do_QueryInterface(opRange);
|
|
|
|
(*outArrayOfRanges)->AppendElement(isupports);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetNodesForOperation: run through the ranges in the array and construct
|
|
|
|
// a new array of nodes to be acted on.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::GetNodesForOperation(nsISupportsArray *inArrayOfRanges,
|
|
|
|
nsCOMPtr<nsISupportsArray> *outArrayOfNodes,
|
|
|
|
PRInt32 inOperationType)
|
|
|
|
{
|
|
|
|
if (!inArrayOfRanges || !outArrayOfNodes) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult res = NS_NewISupportsArray(getter_AddRefs(*outArrayOfNodes));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
PRUint32 rangeCount;
|
|
|
|
res = inArrayOfRanges->Count(&rangeCount);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
PRInt32 i;
|
|
|
|
nsCOMPtr<nsIDOMRange> opRange;
|
|
|
|
nsCOMPtr<nsISupports> isupports;
|
|
|
|
nsCOMPtr<nsIContentIterator> iter;
|
|
|
|
|
|
|
|
for (i = 0; i < rangeCount; i++)
|
|
|
|
{
|
|
|
|
isupports = (dont_AddRef)(inArrayOfRanges->ElementAt(i));
|
|
|
|
opRange = do_QueryInterface(isupports);
|
|
|
|
res = nsComponentManager::CreateInstance(kSubtreeIteratorCID,
|
|
|
|
nsnull,
|
|
|
|
nsIContentIterator::GetIID(),
|
|
|
|
getter_AddRefs(iter));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = iter->Init(opRange);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
while (NS_COMFALSE == iter->IsDone())
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
res = iter->CurrentNode(getter_AddRefs(content));
|
|
|
|
node = do_QueryInterface(content);
|
|
|
|
if ((NS_SUCCEEDED(res)) && node)
|
|
|
|
{
|
|
|
|
isupports = do_QueryInterface(node);
|
|
|
|
(*outArrayOfNodes)->AppendElement(isupports);
|
|
|
|
}
|
|
|
|
res = iter->Next();
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// MakeTransitionList: detect all the transitions in the array, where a
|
|
|
|
// transition means that adjacent nodes in the array
|
|
|
|
// don't have the same parent.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::MakeTransitionList(nsISupportsArray *inArrayOfNodes,
|
|
|
|
nsVoidArray *inTransitionArray)
|
|
|
|
{
|
|
|
|
if (!inArrayOfNodes || !inTransitionArray) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
PRUint32 listCount;
|
|
|
|
PRInt32 i;
|
|
|
|
inArrayOfNodes->Count(&listCount);
|
|
|
|
nsVoidArray transitionList;
|
|
|
|
nsCOMPtr<nsIDOMNode> prevElementParent;
|
|
|
|
nsCOMPtr<nsIDOMNode> curElementParent;
|
|
|
|
|
|
|
|
for (i=0; i<listCount; i++)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> isupports = (dont_AddRef)(inArrayOfNodes->ElementAt(i));
|
|
|
|
nsCOMPtr<nsIDOMNode> transNode( do_QueryInterface(isupports ) );
|
|
|
|
transNode->GetParentNode(getter_AddRefs(curElementParent));
|
|
|
|
if (curElementParent != prevElementParent)
|
|
|
|
{
|
|
|
|
inTransitionArray->InsertElementAt((void*)PR_TRUE,i); // different parents: transition point
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
inTransitionArray->InsertElementAt((void*)PR_FALSE,i); // same parents: these nodes grew up together
|
|
|
|
}
|
|
|
|
prevElementParent = curElementParent;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-06-16 05:02:43 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// ReplaceContainer: replace inNode with a new node (outNode) which is contructed
|
|
|
|
// to be of type aNodeType. Put inNodes children into outNode.
|
|
|
|
// Callers responsibility to make sure inNode's children can
|
|
|
|
// go in outNode.
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::ReplaceContainer(nsIDOMNode *inNode,
|
|
|
|
nsCOMPtr<nsIDOMNode> *outNode,
|
|
|
|
nsString &aNodeType)
|
|
|
|
{
|
|
|
|
if (!inNode || !outNode)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
PRInt32 offset;
|
|
|
|
res = nsEditor::GetNodeLocation(inNode, &parent, &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = mEditor->CreateNode(aNodeType, parent, offset, getter_AddRefs(*outNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
PRBool bHasMoreChildren;
|
|
|
|
inNode->HasChildNodes(&bHasMoreChildren);
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
offset = 0;
|
|
|
|
while (bHasMoreChildren)
|
|
|
|
{
|
|
|
|
inNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
res = mEditor->DeleteNode(child);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = mEditor->InsertNode(child, *outNode, offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
inNode->HasChildNodes(&bHasMoreChildren);
|
|
|
|
offset++;
|
|
|
|
}
|
1999-06-25 09:33:02 +00:00
|
|
|
res = mEditor->DeleteNode(inNode);
|
|
|
|
return res;
|
1999-06-16 05:02:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
1999-06-25 09:33:02 +00:00
|
|
|
// RemoveContainer: remove inNode, reparenting it's children into their
|
|
|
|
// the parent of inNode
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::RemoveContainer(nsIDOMNode *inNode)
|
|
|
|
{
|
|
|
|
if (!inNode)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
if (IsBody(inNode))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
PRInt32 offset;
|
|
|
|
res = nsEditor::GetNodeLocation(inNode, &parent, &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
PRBool bHasMoreChildren;
|
|
|
|
inNode->HasChildNodes(&bHasMoreChildren);
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
while (bHasMoreChildren)
|
|
|
|
{
|
|
|
|
inNode->GetLastChild(getter_AddRefs(child));
|
|
|
|
res = mEditor->DeleteNode(child);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = mEditor->InsertNode(child, parent, offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
inNode->HasChildNodes(&bHasMoreChildren);
|
|
|
|
}
|
|
|
|
res = mEditor->DeleteNode(inNode);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// InsertContainerAbove: insert a new parent for inNode, returned in outNode,
|
1999-06-16 05:02:43 +00:00
|
|
|
// which is contructed to be of type aNodeType. outNode becomes
|
|
|
|
// a child of inNode's earlier parent.
|
|
|
|
// Callers responsibility to make sure inNode's can be child
|
|
|
|
// of outNode, and outNode can be child of old parent.
|
|
|
|
nsresult
|
1999-06-25 09:33:02 +00:00
|
|
|
nsHTMLEditRules::InsertContainerAbove(nsIDOMNode *inNode,
|
1999-06-16 05:02:43 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> *outNode,
|
|
|
|
nsString &aNodeType)
|
|
|
|
{
|
|
|
|
if (!inNode || !outNode)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
PRInt32 offset;
|
|
|
|
res = nsEditor::GetNodeLocation(inNode, &parent, &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// make new parent, outNode
|
|
|
|
res = mEditor->CreateNode(aNodeType, parent, offset, getter_AddRefs(*outNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// put inNode in new parent, outNode
|
|
|
|
res = mEditor->DeleteNode(inNode);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = mEditor->InsertNode(inNode, *outNode, 0);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-04-13 13:35:08 +00:00
|
|
|
|
1999-06-25 09:33:02 +00:00
|
|
|
|
|
|
|
|
1999-04-13 13:35:08 +00:00
|
|
|
/********************************************************
|
1999-06-16 05:02:43 +00:00
|
|
|
* main implementation methods
|
1999-04-13 13:35:08 +00:00
|
|
|
********************************************************/
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// InsertTab: top level logic for determining how to insert a tab
|
|
|
|
//
|
1999-04-12 12:01:32 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::InsertTab(nsIDOMSelection *aSelection,
|
|
|
|
PRBool *aCancel,
|
|
|
|
PlaceholderTxn **aTxn,
|
|
|
|
nsString *outString)
|
|
|
|
{
|
1999-04-21 14:49:58 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parentNode;
|
1999-04-13 13:35:08 +00:00
|
|
|
PRInt32 offset;
|
1999-04-21 14:49:58 +00:00
|
|
|
PRBool isPRE;
|
|
|
|
PRBool isNextWhiteSpace;
|
|
|
|
PRBool isPrevWhiteSpace;
|
1999-04-13 13:35:08 +00:00
|
|
|
|
1999-05-28 21:17:30 +00:00
|
|
|
nsresult result = mEditor->GetStartNodeAndOffset(aSelection, &parentNode, &offset);
|
1999-04-21 14:49:58 +00:00
|
|
|
if (NS_FAILED(result)) return result;
|
1999-04-13 13:35:08 +00:00
|
|
|
|
1999-04-21 14:49:58 +00:00
|
|
|
if (!parentNode) return NS_ERROR_FAILURE;
|
1999-04-13 13:35:08 +00:00
|
|
|
|
1999-05-28 21:17:30 +00:00
|
|
|
result = mEditor->IsPreformatted(parentNode,&isPRE);
|
1999-04-21 14:49:58 +00:00
|
|
|
if (NS_FAILED(result)) return result;
|
1999-04-13 13:35:08 +00:00
|
|
|
|
1999-04-12 12:01:32 +00:00
|
|
|
if (isPRE)
|
|
|
|
{
|
1999-04-21 14:49:58 +00:00
|
|
|
*outString += '\t';
|
|
|
|
return NS_OK;
|
1999-04-12 12:01:32 +00:00
|
|
|
}
|
1999-04-21 14:49:58 +00:00
|
|
|
|
1999-05-28 21:17:30 +00:00
|
|
|
result = mEditor->IsNextCharWhitespace(parentNode, offset, &isNextWhiteSpace);
|
1999-04-21 14:49:58 +00:00
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
|
1999-05-28 21:17:30 +00:00
|
|
|
result = mEditor->IsPrevCharWhitespace(parentNode, offset, &isPrevWhiteSpace);
|
1999-04-21 14:49:58 +00:00
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
|
|
|
|
if (isPrevWhiteSpace)
|
1999-04-12 12:01:32 +00:00
|
|
|
{
|
1999-04-21 14:49:58 +00:00
|
|
|
// prev character is a whitespace; Need to
|
|
|
|
// insert nbsp's BEFORE the space
|
|
|
|
|
|
|
|
// XXX for now put tab in wrong place
|
|
|
|
if (isNextWhiteSpace)
|
|
|
|
{
|
|
|
|
GetTabAsNBSPs(outString);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
GetTabAsNBSPsAndSpace(outString);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isNextWhiteSpace)
|
|
|
|
{
|
|
|
|
// character after us is ws. insert nbsps
|
|
|
|
GetTabAsNBSPs(outString);
|
|
|
|
return NS_OK;
|
1999-04-12 12:01:32 +00:00
|
|
|
}
|
1999-04-21 14:49:58 +00:00
|
|
|
|
|
|
|
// else we are in middle of a word; use n-1 nbsp's plus a space
|
|
|
|
GetTabAsNBSPsAndSpace(outString);
|
|
|
|
|
1999-04-12 12:01:32 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-13 13:35:08 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// InsertSpace: top level logic for determining how to insert a space
|
|
|
|
//
|
1999-04-12 12:01:32 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::InsertSpace(nsIDOMSelection *aSelection,
|
|
|
|
PRBool *aCancel,
|
|
|
|
PlaceholderTxn **aTxn,
|
|
|
|
nsString *outString)
|
|
|
|
{
|
1999-04-13 13:35:08 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parentNode;
|
|
|
|
PRInt32 offset;
|
1999-04-21 14:49:58 +00:00
|
|
|
PRBool isPRE;
|
|
|
|
PRBool isNextWhiteSpace;
|
|
|
|
PRBool isPrevWhiteSpace;
|
1999-04-13 13:35:08 +00:00
|
|
|
|
1999-05-28 21:17:30 +00:00
|
|
|
nsresult result = mEditor->GetStartNodeAndOffset(aSelection, &parentNode, &offset);
|
1999-04-21 14:49:58 +00:00
|
|
|
if (NS_FAILED(result)) return result;
|
1999-04-13 13:35:08 +00:00
|
|
|
|
1999-04-21 14:49:58 +00:00
|
|
|
if (!parentNode) return NS_ERROR_FAILURE;
|
1999-04-13 13:35:08 +00:00
|
|
|
|
1999-05-28 21:17:30 +00:00
|
|
|
result = mEditor->IsPreformatted(parentNode,&isPRE);
|
1999-04-21 14:49:58 +00:00
|
|
|
if (NS_FAILED(result)) return result;
|
1999-04-13 13:35:08 +00:00
|
|
|
|
1999-04-21 14:49:58 +00:00
|
|
|
if (isPRE)
|
1999-04-13 13:35:08 +00:00
|
|
|
{
|
1999-04-21 14:49:58 +00:00
|
|
|
*outString += " ";
|
1999-04-13 13:35:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-05-28 21:17:30 +00:00
|
|
|
result = mEditor->IsNextCharWhitespace(parentNode, offset, &isNextWhiteSpace);
|
1999-04-21 14:49:58 +00:00
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
|
1999-05-28 21:17:30 +00:00
|
|
|
result = mEditor->IsPrevCharWhitespace(parentNode, offset, &isPrevWhiteSpace);
|
1999-04-21 14:49:58 +00:00
|
|
|
if (NS_FAILED(result)) return result;
|
1999-04-13 13:35:08 +00:00
|
|
|
|
1999-04-21 14:49:58 +00:00
|
|
|
if (isPrevWhiteSpace)
|
1999-04-13 13:35:08 +00:00
|
|
|
{
|
1999-04-21 14:49:58 +00:00
|
|
|
// prev character is a whitespace; Need to
|
1999-04-13 13:35:08 +00:00
|
|
|
// insert nbsp BEFORE the space
|
|
|
|
|
1999-04-21 14:49:58 +00:00
|
|
|
// XXX for now put in wrong place
|
|
|
|
if (isNextWhiteSpace)
|
|
|
|
{
|
|
|
|
*outString += nbsp;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
*outString += nbsp;
|
1999-04-13 13:35:08 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-04-21 14:49:58 +00:00
|
|
|
if (isNextWhiteSpace)
|
|
|
|
{
|
|
|
|
// character after us is ws. insert nbsp
|
|
|
|
*outString += nbsp;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-04-13 13:35:08 +00:00
|
|
|
// else just a space
|
|
|
|
*outString += " ";
|
|
|
|
|
1999-04-12 12:01:32 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-26 14:08:52 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// ReturnInHeader: do the right thing for returns pressed in headers
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::ReturnInHeader(nsIDOMSelection *aSelection,
|
|
|
|
nsIDOMNode *aHeader,
|
|
|
|
nsIDOMNode *aTextNode,
|
|
|
|
PRInt32 aOffset)
|
|
|
|
{
|
|
|
|
if (!aSelection || !aHeader || !aTextNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> leftNode;
|
|
|
|
nsCOMPtr<nsIDOMNode> textNode = do_QueryInterface(aTextNode); // to hold a ref across the delete call
|
|
|
|
// split the node
|
|
|
|
nsresult res = mEditor->SplitNode(aTextNode, aOffset, getter_AddRefs(leftNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// move the right node outside of the header, via deletion/insertion
|
|
|
|
// delete the right node
|
|
|
|
res = mEditor->DeleteNode(textNode);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// insert the right node
|
|
|
|
nsCOMPtr<nsIDOMNode> p;
|
|
|
|
aHeader->GetParentNode(getter_AddRefs(p));
|
1999-05-28 21:17:30 +00:00
|
|
|
PRInt32 indx = mEditor->GetIndexOf(p,aHeader);
|
1999-04-26 14:08:52 +00:00
|
|
|
res = mEditor->InsertNode(textNode,p,indx+1);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// merge text node with like sibling, if any
|
|
|
|
nsCOMPtr<nsIDOMNode> sibling;
|
|
|
|
textNode->GetNextSibling(getter_AddRefs(sibling));
|
1999-06-16 23:40:16 +00:00
|
|
|
if (sibling && mEditor->IsTextNode(sibling) && mEditor->IsEditable(sibling))
|
1999-04-26 14:08:52 +00:00
|
|
|
{
|
|
|
|
res = mEditor->JoinNodes(textNode,sibling,p);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
textNode = sibling; // sibling was the node kept by the join; remember it in "textNode"
|
|
|
|
}
|
|
|
|
|
|
|
|
// position selection before inserted node
|
1999-07-18 02:27:19 +00:00
|
|
|
res = aSelection->Collapse(textNode,0);
|
1999-04-26 14:08:52 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// ReturnInParagraph: do the right thing for returns pressed in paragraphs
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::ReturnInParagraph(nsIDOMSelection *aSelection,
|
|
|
|
nsIDOMNode *aHeader,
|
|
|
|
nsIDOMNode *aNode,
|
|
|
|
PRInt32 aOffset,
|
|
|
|
PRBool *aCancel)
|
|
|
|
{
|
|
|
|
if (!aSelection || !aHeader || !aNode || !aCancel) return NS_ERROR_NULL_POINTER;
|
|
|
|
*aCancel = PR_FALSE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> sibling;
|
|
|
|
nsresult res = NS_OK;
|
1999-07-03 13:27:13 +00:00
|
|
|
|
|
|
|
//bombs away - nuking this for now...
|
|
|
|
#if 0
|
|
|
|
|
1999-04-26 14:08:52 +00:00
|
|
|
// easy case, in a text node:
|
1999-05-28 21:17:30 +00:00
|
|
|
if (mEditor->IsTextNode(aNode))
|
1999-04-26 14:08:52 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMText> textNode = do_QueryInterface(aNode);
|
|
|
|
PRUint32 strLength;
|
|
|
|
res = textNode->GetLength(&strLength);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// at beginning of text node?
|
|
|
|
if (!aOffset)
|
|
|
|
{
|
|
|
|
// is there a BR prior to it?
|
|
|
|
aNode->GetPreviousSibling(getter_AddRefs(sibling));
|
|
|
|
if (!sibling)
|
|
|
|
{
|
|
|
|
// no previous sib, so
|
|
|
|
// just fall out to default of inserting a BR
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
if (IsBreak(sibling))
|
|
|
|
{
|
|
|
|
*aCancel = PR_TRUE;
|
|
|
|
// get rid of the break
|
|
|
|
res = mEditor->DeleteNode(sibling);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
// split the paragraph
|
1999-05-28 21:17:30 +00:00
|
|
|
res = mEditor->SplitNodeDeep( aHeader, aNode, aOffset);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
// position selection inside textnode
|
|
|
|
res = aSelection->Collapse(aNode,0);
|
|
|
|
}
|
|
|
|
// else just fall out to default of inserting a BR
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
// at end of text node?
|
|
|
|
if (aOffset == strLength)
|
|
|
|
{
|
|
|
|
// is there a BR after to it?
|
|
|
|
res = aNode->GetNextSibling(getter_AddRefs(sibling));
|
|
|
|
if (!sibling)
|
|
|
|
{
|
|
|
|
// no next sib, so
|
|
|
|
// just fall out to default of inserting a BR
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
if (IsBreak(sibling))
|
|
|
|
{
|
|
|
|
*aCancel = PR_TRUE;
|
|
|
|
// get rid of the break
|
|
|
|
res = mEditor->DeleteNode(sibling);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
// split the paragraph
|
1999-05-28 21:17:30 +00:00
|
|
|
res = mEditor->SplitNodeDeep( aHeader, aNode, aOffset);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
// position selection inside textnode
|
|
|
|
res = aSelection->Collapse(aNode,0);
|
|
|
|
}
|
|
|
|
// else just fall out to default of inserting a BR
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
// inside text node
|
|
|
|
// just fall out to default of inserting a BR
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// not in a text node. are we next to BR's?
|
1999-07-03 13:27:13 +00:00
|
|
|
// XXX
|
|
|
|
#endif
|
1999-04-26 14:08:52 +00:00
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// ReturnInListItem: do the right thing for returns pressed in list items
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditRules::ReturnInListItem(nsIDOMSelection *aSelection,
|
|
|
|
nsIDOMNode *aListItem,
|
|
|
|
nsIDOMNode *aNode,
|
|
|
|
PRInt32 aOffset)
|
|
|
|
{
|
|
|
|
if (!aSelection || !aListItem || !aNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-05-28 21:17:30 +00:00
|
|
|
nsresult res = mEditor->SplitNodeDeep( aListItem, aNode, aOffset);
|
1999-04-26 14:08:52 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1999-07-18 02:27:19 +00:00
|
|
|
res = aSelection->Collapse(aNode,0);
|
1999-04-26 14:08:52 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|