1999-03-01 22:54:47 +03: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 "nsTextEditor.h"
|
|
|
|
#include "nsHTMLEditor.h"
|
1999-03-29 12:02:05 +04:00
|
|
|
#include "nsHTMLEditRules.h"
|
1999-03-01 22:54:47 +03:00
|
|
|
#include "nsEditorEventListeners.h"
|
1999-05-07 23:23:17 +04:00
|
|
|
#include "nsInsertHTMLTxn.h"
|
1999-04-08 04:46:10 +04:00
|
|
|
#include "nsIDOMNodeList.h"
|
1999-05-06 03:27:17 +04:00
|
|
|
#include "nsIDOMNSRange.h"
|
1999-03-01 22:54:47 +03:00
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDOMEventReceiver.h"
|
|
|
|
#include "nsIDOMKeyListener.h"
|
|
|
|
#include "nsIDOMMouseListener.h"
|
1999-03-29 12:02:05 +04:00
|
|
|
#include "nsIDOMSelection.h"
|
1999-03-30 02:01:26 +04:00
|
|
|
#include "nsIDOMHTMLAnchorElement.h"
|
|
|
|
#include "nsIDOMHTMLImageElement.h"
|
1999-04-20 21:47:12 +04:00
|
|
|
#include "nsIEnumerator.h"
|
1999-04-21 22:53:55 +04:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIContentIterator.h"
|
1999-03-01 22:54:47 +03:00
|
|
|
#include "nsEditorCID.h"
|
1999-04-21 22:53:55 +04:00
|
|
|
#include "nsLayoutCID.h"
|
|
|
|
#include "nsIDOMRange.h"
|
1999-05-01 02:40:18 +04:00
|
|
|
#include "nsISupportsArray.h"
|
|
|
|
#include "nsVoidArray.h"
|
1999-05-14 00:59:08 +04:00
|
|
|
#include "nsFileSpec.h"
|
1999-03-09 12:44:27 +03:00
|
|
|
#include "nsIComponentManager.h"
|
1999-03-01 22:54:47 +03:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
|
|
|
|
|
|
|
|
static NS_DEFINE_IID(kIDOMEventReceiverIID, NS_IDOMEVENTRECEIVER_IID);
|
|
|
|
static NS_DEFINE_IID(kIDOMMouseListenerIID, NS_IDOMMOUSELISTENER_IID);
|
|
|
|
static NS_DEFINE_IID(kIDOMKeyListenerIID, NS_IDOMKEYLISTENER_IID);
|
|
|
|
|
1999-05-07 23:23:17 +04:00
|
|
|
static NS_DEFINE_IID(kInsertHTMLTxnIID, NS_INSERT_HTML_TXN_IID);
|
|
|
|
|
1999-03-01 22:54:47 +03:00
|
|
|
static NS_DEFINE_CID(kEditorCID, NS_EDITOR_CID);
|
|
|
|
static NS_DEFINE_IID(kIEditorIID, NS_IEDITOR_IID);
|
|
|
|
static NS_DEFINE_IID(kISupportsIID, NS_ISUPPORTS_IID);
|
|
|
|
|
|
|
|
static NS_DEFINE_IID(kITextEditorIID, NS_ITEXTEDITOR_IID);
|
|
|
|
static NS_DEFINE_CID(kTextEditorCID, NS_TEXTEDITOR_CID);
|
|
|
|
static NS_DEFINE_IID(kIHTMLEditorIID, NS_IHTMLEDITOR_IID);
|
|
|
|
static NS_DEFINE_CID(kHTMLEditorCID, NS_HTMLEDITOR_CID);
|
1999-04-21 22:53:55 +04:00
|
|
|
static NS_DEFINE_CID(kCContentIteratorCID, NS_CONTENTITERATOR_CID);
|
|
|
|
static NS_DEFINE_IID(kIContentIteratorIID, NS_ICONTENTITERTOR_IID);
|
1999-03-01 22:54:47 +03:00
|
|
|
|
1999-04-20 21:47:12 +04:00
|
|
|
#ifdef NS_DEBUG
|
1999-05-05 08:51:54 +04:00
|
|
|
static PRBool gNoisy = PR_FALSE;
|
1999-04-20 21:47:12 +04:00
|
|
|
#else
|
|
|
|
static const PRBool gNoisy = PR_FALSE;
|
|
|
|
#endif
|
1999-03-01 22:54:47 +03:00
|
|
|
|
|
|
|
|
|
|
|
nsHTMLEditor::nsHTMLEditor()
|
|
|
|
{
|
1999-03-06 00:05:35 +03:00
|
|
|
// Done in nsEditor
|
|
|
|
// NS_INIT_REFCNT();
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsHTMLEditor::~nsHTMLEditor()
|
|
|
|
{
|
|
|
|
//the autopointers will clear themselves up.
|
1999-03-06 00:05:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Adds appropriate AddRef, Release, and QueryInterface methods for derived class
|
|
|
|
//NS_IMPL_ISUPPORTS_INHERITED(nsHTMLEditor, nsTextEditor, nsIHTMLEditor)
|
|
|
|
|
|
|
|
//NS_IMPL_ADDREF_INHERITED(Class, Super)
|
|
|
|
NS_IMETHODIMP_(nsrefcnt) nsHTMLEditor::AddRef(void)
|
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::AddRef();
|
1999-03-06 00:05:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//NS_IMPL_RELEASE_INHERITED(Class, Super)
|
|
|
|
NS_IMETHODIMP_(nsrefcnt) nsHTMLEditor::Release(void)
|
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::Release();
|
1999-03-06 00:05:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//NS_IMPL_QUERY_INTERFACE_INHERITED(Class, Super, AdditionalInterface)
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::QueryInterface(REFNSIID aIID, void** aInstancePtr)
|
|
|
|
{
|
|
|
|
if (!aInstancePtr) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if (aIID.Equals(nsIHTMLEditor::GetIID())) {
|
|
|
|
*aInstancePtr = NS_STATIC_CAST(nsIHTMLEditor*, this);
|
|
|
|
NS_ADDREF_THIS();
|
|
|
|
return NS_OK;
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::QueryInterface(aIID, aInstancePtr);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-06 00:05:35 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::Init(nsIDOMDocument *aDoc, nsIPresShell *aPresShell)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(nsnull!=aDoc && nsnull!=aPresShell, "bad arg");
|
|
|
|
nsresult result=NS_ERROR_NULL_POINTER;
|
|
|
|
if ((nsnull!=aDoc) && (nsnull!=aPresShell))
|
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::Init(aDoc, aPresShell);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-03-29 12:02:05 +04:00
|
|
|
void nsHTMLEditor::InitRules()
|
|
|
|
{
|
|
|
|
// instantiate the rules for this text editor
|
|
|
|
// XXX: we should be told which set of rules to instantiate
|
|
|
|
mRules = new nsHTMLEditRules();
|
|
|
|
mRules->Init(this);
|
|
|
|
}
|
|
|
|
|
1999-04-15 10:06:33 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::SetTextProperty(nsIAtom *aProperty,
|
|
|
|
const nsString *aAttribute,
|
|
|
|
const nsString *aValue)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-04-15 10:06:33 +04:00
|
|
|
return nsTextEditor::SetTextProperty(aProperty, aAttribute, aValue);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-04-15 10:06:33 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::GetTextProperty(nsIAtom *aProperty,
|
|
|
|
const nsString *aAttribute,
|
|
|
|
const nsString *aValue,
|
|
|
|
PRBool &aFirst, PRBool &aAny, PRBool &aAll)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-04-15 10:06:33 +04:00
|
|
|
return nsTextEditor::GetTextProperty(aProperty, aAttribute, aValue, aFirst, aAny, aAll);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-04-15 10:06:33 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::RemoveTextProperty(nsIAtom *aProperty, const nsString *aAttribute)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-04-15 10:06:33 +04:00
|
|
|
return nsTextEditor::RemoveTextProperty(aProperty, aAttribute);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-05-06 03:27:17 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::DeleteSelection(nsIEditor::ECollapsedSelectionAction aAction)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-05-06 03:27:17 +04:00
|
|
|
return nsTextEditor::DeleteSelection(aAction);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::InsertText(const nsString& aStringToInsert)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::InsertText(aStringToInsert);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-10 22:49:18 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::InsertBreak()
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-29 12:02:05 +04:00
|
|
|
nsresult result;
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMSelection> selection;
|
|
|
|
PRBool cancel= PR_FALSE;
|
|
|
|
|
|
|
|
result = nsEditor::BeginTransaction();
|
|
|
|
if (NS_FAILED(result)) { return result; }
|
|
|
|
|
|
|
|
// pre-process
|
|
|
|
nsEditor::GetSelection(getter_AddRefs(selection));
|
1999-04-12 16:01:32 +04:00
|
|
|
nsTextRulesInfo ruleInfo(nsHTMLEditRules::kInsertBreak);
|
|
|
|
result = mRules->WillDoAction(selection, &ruleInfo, &cancel);
|
1999-03-29 12:02:05 +04:00
|
|
|
if ((PR_FALSE==cancel) && (NS_SUCCEEDED(result)))
|
|
|
|
{
|
|
|
|
// create the new BR node
|
|
|
|
nsCOMPtr<nsIDOMNode> newNode;
|
|
|
|
nsAutoString tag("BR");
|
|
|
|
result = nsEditor::DeleteSelectionAndCreateNode(tag, getter_AddRefs(newNode));
|
|
|
|
if (NS_SUCCEEDED(result) && newNode)
|
|
|
|
{
|
|
|
|
// set the selection to the new node
|
|
|
|
nsCOMPtr<nsIDOMNode>parent;
|
|
|
|
result = newNode->GetParentNode(getter_AddRefs(parent));
|
|
|
|
if (NS_SUCCEEDED(result) && parent)
|
|
|
|
{
|
|
|
|
PRInt32 offsetInParent=-1; // we use the -1 as a marker to see if we need to compute this or not
|
|
|
|
nsCOMPtr<nsIDOMNode>nextNode;
|
|
|
|
newNode->GetNextSibling(getter_AddRefs(nextNode));
|
|
|
|
if (nextNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMCharacterData>nextTextNode;
|
|
|
|
nextTextNode = do_QueryInterface(nextNode);
|
|
|
|
if (!nextTextNode) {
|
|
|
|
nextNode = do_QueryInterface(newNode);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
offsetInParent=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
nextNode = do_QueryInterface(newNode);
|
|
|
|
}
|
|
|
|
result = nsEditor::GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
if (-1==offsetInParent)
|
|
|
|
{
|
|
|
|
nextNode->GetParentNode(getter_AddRefs(parent));
|
1999-05-05 08:05:19 +04:00
|
|
|
result = GetChildOffset(nextNode, parent, offsetInParent);
|
1999-03-29 12:02:05 +04:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
selection->Collapse(parent, offsetInParent+1); // +1 to insert just after the break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
selection->Collapse(nextNode, offsetInParent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// post-process, always called if WillInsertBreak didn't return cancel==PR_TRUE
|
1999-04-12 16:01:32 +04:00
|
|
|
result = mRules->DidDoAction(selection, &ruleInfo, result);
|
1999-03-29 12:02:05 +04:00
|
|
|
}
|
|
|
|
nsresult endTxnResult = nsEditor::EndTransaction(); // don't return this result!
|
|
|
|
NS_ASSERTION ((NS_SUCCEEDED(endTxnResult)), "bad end transaction result");
|
|
|
|
|
|
|
|
// XXXX: Horrible hack! We are doing this because
|
|
|
|
// of an error in Gecko which is not rendering the
|
|
|
|
// document after a change via the DOM - gpk 2/13/99
|
|
|
|
// BEGIN HACK!!!
|
|
|
|
HACKForceRedraw();
|
|
|
|
// END HACK
|
|
|
|
|
|
|
|
return result;
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-05-06 03:29:18 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::GetParagraphFormat(nsString& aParagraphFormat)
|
|
|
|
{
|
|
|
|
nsresult result = NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::SetParagraphFormat(const nsString& aParagraphFormat)
|
|
|
|
{
|
|
|
|
nsresult result = NS_ERROR_NOT_INITIALIZED;
|
|
|
|
//Kinda sad to waste memory just to force lower case
|
|
|
|
nsAutoString tag = aParagraphFormat;
|
|
|
|
tag.ToLowerCase();
|
|
|
|
if (tag == "normal" || tag == "p") {
|
|
|
|
result = RemoveParagraphStyle();
|
|
|
|
} else {
|
|
|
|
result = ReplaceBlockParent(tag);
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXXX: Horrible hack! We are doing this because
|
|
|
|
// of an error in Gecko which is not rendering the
|
|
|
|
// document after a change via the DOM - gpk 2/13/99
|
|
|
|
// BEGIN HACK!!!
|
|
|
|
HACKForceRedraw();
|
|
|
|
// END HACK
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-03-01 22:54:47 +03:00
|
|
|
// Methods shared with the base editor.
|
|
|
|
// Note: We could call each of these via nsTextEditor -- is that better?
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::EnableUndo(PRBool aEnable)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::EnableUndo(aEnable);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::Undo(PRUint32 aCount)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::Undo(aCount);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::CanUndo(PRBool &aIsEnabled, PRBool &aCanUndo)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::CanUndo(aIsEnabled, aCanUndo);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::Redo(PRUint32 aCount)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::Redo(aCount);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::CanRedo(PRBool &aIsEnabled, PRBool &aCanRedo)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::CanRedo(aIsEnabled, aCanRedo);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::BeginTransaction()
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::BeginTransaction();
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::EndTransaction()
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::EndTransaction();
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::MoveSelectionUp(nsIAtom *aIncrement, PRBool aExtendSelection)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::MoveSelectionUp(aIncrement, aExtendSelection);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::MoveSelectionDown(nsIAtom *aIncrement, PRBool aExtendSelection)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::MoveSelectionDown(aIncrement, aExtendSelection);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::MoveSelectionNext(nsIAtom *aIncrement, PRBool aExtendSelection)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::MoveSelectionNext(aIncrement, aExtendSelection);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::MoveSelectionPrevious(nsIAtom *aIncrement, PRBool aExtendSelection)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::MoveSelectionPrevious(aIncrement, aExtendSelection);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::SelectNext(nsIAtom *aIncrement, PRBool aExtendSelection)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::SelectNext(aIncrement, aExtendSelection);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::SelectPrevious(nsIAtom *aIncrement, PRBool aExtendSelection)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::SelectPrevious(aIncrement, aExtendSelection);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-11 22:33:37 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::SelectAll()
|
|
|
|
{
|
|
|
|
return nsTextEditor::SelectAll();
|
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::ScrollUp(nsIAtom *aIncrement)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::ScrollUp(aIncrement);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::ScrollDown(nsIAtom *aIncrement)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::ScrollDown(aIncrement);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-02 08:30:53 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::ScrollIntoView(PRBool aScrollToBegin)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::ScrollIntoView(aScrollToBegin);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-05-07 09:02:35 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::Save()
|
|
|
|
{
|
|
|
|
return nsTextEditor::Save();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::SaveAs(PRBool aSavingCopy)
|
|
|
|
{
|
|
|
|
return nsTextEditor::SaveAs(aSavingCopy);
|
|
|
|
}
|
|
|
|
|
1999-03-11 00:29:41 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::Cut()
|
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::Cut();
|
1999-03-11 00:29:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::Copy()
|
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::Copy();
|
1999-03-11 00:29:41 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::Paste()
|
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::Paste();
|
1999-03-11 00:29:41 +03:00
|
|
|
}
|
|
|
|
|
1999-03-30 02:01:26 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::Insert(nsString& aInputString)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-05-07 23:23:17 +04:00
|
|
|
nsresult res;
|
|
|
|
|
1999-05-06 03:27:17 +04:00
|
|
|
nsEditor::BeginTransaction();
|
|
|
|
|
|
|
|
nsEditor::DeleteSelection(nsIEditor::eDoNothing);
|
|
|
|
|
1999-05-07 23:23:17 +04:00
|
|
|
// Make the transaction for insert html:
|
|
|
|
nsInsertHTMLTxn* insertHTMLTxn = 0;
|
|
|
|
res = TransactionFactory::GetNewTransaction(kInsertHTMLTxnIID,
|
|
|
|
(EditTxn **)&insertHTMLTxn);
|
|
|
|
if (NS_SUCCEEDED(res))
|
1999-05-06 03:27:17 +04:00
|
|
|
{
|
1999-05-07 23:23:17 +04:00
|
|
|
res = insertHTMLTxn->Init(aInputString, this);
|
1999-05-06 03:27:17 +04:00
|
|
|
if (NS_SUCCEEDED(res))
|
1999-05-07 23:23:17 +04:00
|
|
|
res = Do(insertHTMLTxn);
|
|
|
|
|
|
|
|
// XXX How is it that we don't have to release the transaction?
|
1999-05-06 03:27:17 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsEditor::EndTransaction();
|
|
|
|
|
|
|
|
if (!NS_SUCCEEDED(res))
|
|
|
|
printf("Couldn't insert html: error was %d\n", res);
|
1999-05-07 23:23:17 +04:00
|
|
|
|
1999-05-06 03:27:17 +04:00
|
|
|
return res;
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-06 23:32:48 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::OutputText(nsString& aOutputString)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::OutputText(aOutputString);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-03-06 23:32:48 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::OutputHTML(nsString& aOutputString)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-03-11 01:41:18 +03:00
|
|
|
return nsTextEditor::OutputHTML(aOutputString);
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
1999-05-03 22:48:39 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::OutputText(nsIOutputStream* aOutputStream, nsString* aCharsetOverride)
|
|
|
|
{
|
|
|
|
return nsTextEditor::OutputText(aOutputStream,aCharsetOverride);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::OutputHTML(nsIOutputStream* aOutputStream,nsString* aCharsetOverride)
|
|
|
|
{
|
|
|
|
return nsTextEditor::OutputHTML(aOutputStream,aCharsetOverride);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-04-21 22:53:55 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::CopyAttributes(nsIDOMNode *aDestNode, nsIDOMNode *aSourceNode)
|
|
|
|
{
|
|
|
|
return nsTextEditor::CopyAttributes(aDestNode, aSourceNode);
|
|
|
|
}
|
|
|
|
|
1999-03-01 22:54:47 +03:00
|
|
|
//================================================================
|
|
|
|
// HTML Editor methods
|
|
|
|
//
|
|
|
|
// Note: Table Editing methods are implemented in EditTable.cpp
|
|
|
|
//
|
1999-03-30 02:01:26 +04:00
|
|
|
|
1999-05-01 02:40:18 +04:00
|
|
|
// get the paragraph style(s) for the selection
|
1999-04-20 21:47:12 +04:00
|
|
|
NS_IMETHODIMP
|
1999-05-01 02:40:18 +04:00
|
|
|
nsHTMLEditor::GetParagraphStyle(nsStringArray *aTagList)
|
1999-04-20 21:47:12 +04:00
|
|
|
{
|
1999-05-01 02:40:18 +04:00
|
|
|
if (gNoisy) { printf("---------- nsHTMLEditor::GetParagraphStyle ----------\n"); }
|
|
|
|
if (!aTagList) { return NS_ERROR_NULL_POINTER; }
|
1999-04-20 21:47:12 +04:00
|
|
|
|
1999-05-01 02:40:18 +04:00
|
|
|
nsresult result;
|
|
|
|
nsCOMPtr<nsIDOMSelection>selection;
|
|
|
|
result = nsEditor::GetSelection(getter_AddRefs(selection));
|
|
|
|
if ((NS_SUCCEEDED(result)) && selection)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEnumerator> enumerator;
|
|
|
|
enumerator = do_QueryInterface(selection);
|
|
|
|
if (enumerator)
|
|
|
|
{
|
|
|
|
enumerator->First();
|
|
|
|
nsISupports *currentItem;
|
|
|
|
result = enumerator->CurrentItem(¤tItem);
|
|
|
|
if ((NS_SUCCEEDED(result)) && (nsnull!=currentItem))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMRange> range( do_QueryInterface(currentItem) );
|
1999-05-05 08:05:19 +04:00
|
|
|
// scan the range for all the independent block content blockSections
|
1999-05-01 02:40:18 +04:00
|
|
|
// and get the block parent of each
|
1999-05-05 08:05:19 +04:00
|
|
|
nsISupportsArray *blockSections;
|
|
|
|
result = NS_NewISupportsArray(&blockSections);
|
|
|
|
if ((NS_SUCCEEDED(result)) && blockSections)
|
1999-05-01 02:40:18 +04:00
|
|
|
{
|
1999-05-05 08:05:19 +04:00
|
|
|
result = GetBlockSectionsForRange(range, blockSections);
|
1999-05-01 02:40:18 +04:00
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
nsIDOMRange *subRange;
|
1999-05-05 08:05:19 +04:00
|
|
|
subRange = (nsIDOMRange *)(blockSections->ElementAt(0));
|
1999-05-01 02:40:18 +04:00
|
|
|
while (subRange && (NS_SUCCEEDED(result)))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode>startParent;
|
|
|
|
result = subRange->GetStartParent(getter_AddRefs(startParent));
|
|
|
|
if (NS_SUCCEEDED(result) && startParent)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMElement> blockParent;
|
|
|
|
result = GetBlockParent(startParent, getter_AddRefs(blockParent));
|
|
|
|
if (NS_SUCCEEDED(result) && blockParent)
|
|
|
|
{
|
|
|
|
nsAutoString blockParentTag;
|
|
|
|
blockParent->GetTagName(blockParentTag);
|
1999-05-05 08:05:19 +04:00
|
|
|
PRBool isRoot;
|
|
|
|
IsRootTag(blockParentTag, isRoot);
|
|
|
|
if ((PR_FALSE==isRoot) && (-1==aTagList->IndexOf(blockParentTag))) {
|
1999-05-01 02:40:18 +04:00
|
|
|
aTagList->AppendString(blockParentTag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_RELEASE(subRange);
|
1999-05-05 08:05:19 +04:00
|
|
|
blockSections->RemoveElementAt(0);
|
|
|
|
subRange = (nsIDOMRange *)(blockSections->ElementAt(0));
|
1999-05-01 02:40:18 +04:00
|
|
|
}
|
|
|
|
}
|
1999-05-05 08:05:19 +04:00
|
|
|
NS_RELEASE(blockSections);
|
1999-05-01 02:40:18 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// use this when block parents are to be added regardless of current state
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::AddBlockParent(nsString& aParentTag)
|
|
|
|
{
|
1999-04-20 21:47:12 +04:00
|
|
|
if (gNoisy)
|
|
|
|
{
|
|
|
|
char *tag = aParentTag.ToNewCString();
|
|
|
|
printf("---------- nsHTMLEditor::AddBlockParent %s ----------\n", tag);
|
|
|
|
delete [] tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult result=NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsCOMPtr<nsIDOMSelection>selection;
|
|
|
|
result = nsEditor::GetSelection(getter_AddRefs(selection));
|
|
|
|
if ((NS_SUCCEEDED(result)) && selection)
|
|
|
|
{
|
|
|
|
// set the block parent for all selected ranges
|
|
|
|
nsEditor::BeginTransaction();
|
|
|
|
nsCOMPtr<nsIEnumerator> enumerator;
|
|
|
|
enumerator = do_QueryInterface(selection);
|
|
|
|
if (enumerator)
|
|
|
|
{
|
|
|
|
enumerator->First();
|
|
|
|
nsISupports *currentItem;
|
|
|
|
result = enumerator->CurrentItem(¤tItem);
|
|
|
|
if ((NS_SUCCEEDED(result)) && (nsnull!=currentItem))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMRange> range( do_QueryInterface(currentItem) );
|
1999-05-05 08:05:19 +04:00
|
|
|
// scan the range for all the independent block content blockSections
|
1999-05-01 02:40:18 +04:00
|
|
|
// and apply the transformation to them
|
|
|
|
result = ReParentContentOfRange(range, aParentTag, eInsertParent);
|
1999-04-20 21:47:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
nsEditor::EndTransaction();
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{ // set the selection
|
|
|
|
// XXX: can't do anything until I can create ranges
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gNoisy) {printf("Finished nsHTMLEditor::AddBlockParent with this content:\n"); DebugDumpContent(); } // DEBUG
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-05-01 02:40:18 +04:00
|
|
|
// use this when a paragraph type is being transformed from one type to another
|
1999-04-20 21:47:12 +04:00
|
|
|
NS_IMETHODIMP
|
1999-05-01 02:40:18 +04:00
|
|
|
nsHTMLEditor::ReplaceBlockParent(nsString& aParentTag)
|
|
|
|
{
|
|
|
|
|
|
|
|
if (gNoisy)
|
|
|
|
{
|
|
|
|
char *tag = aParentTag.ToNewCString();
|
|
|
|
printf("---------- nsHTMLEditor::ReplaceBlockParent %s ----------\n", tag);
|
|
|
|
delete [] tag;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult result=NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsCOMPtr<nsIDOMSelection>selection;
|
|
|
|
result = nsEditor::GetSelection(getter_AddRefs(selection));
|
|
|
|
if ((NS_SUCCEEDED(result)) && selection)
|
|
|
|
{
|
|
|
|
// set the block parent for all selected ranges
|
|
|
|
nsEditor::BeginTransaction();
|
|
|
|
nsCOMPtr<nsIEnumerator> enumerator;
|
|
|
|
enumerator = do_QueryInterface(selection);
|
|
|
|
if (enumerator)
|
|
|
|
{
|
|
|
|
enumerator->First();
|
|
|
|
nsISupports *currentItem;
|
|
|
|
result = enumerator->CurrentItem(¤tItem);
|
|
|
|
if ((NS_SUCCEEDED(result)) && (nsnull!=currentItem))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMRange> range( do_QueryInterface(currentItem) );
|
1999-05-05 08:05:19 +04:00
|
|
|
// scan the range for all the independent block content blockSections
|
1999-05-01 02:40:18 +04:00
|
|
|
// and apply the transformation to them
|
|
|
|
result = ReParentContentOfRange(range, aParentTag, eReplaceParent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nsEditor::EndTransaction();
|
|
|
|
}
|
|
|
|
if (gNoisy) {printf("Finished nsHTMLEditor::AddBlockParent with this content:\n"); DebugDumpContent(); } // DEBUG
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::ReParentContentOfNode(nsIDOMNode *aNode,
|
|
|
|
nsString &aParentTag,
|
|
|
|
BlockTransformationType aTransformation)
|
1999-04-20 21:47:12 +04:00
|
|
|
{
|
|
|
|
if (!aNode) { return NS_ERROR_NULL_POINTER; }
|
1999-05-05 08:05:19 +04:00
|
|
|
if (gNoisy)
|
|
|
|
{
|
|
|
|
char *tag = aParentTag.ToNewCString();
|
|
|
|
printf("---------- ReParentContentOfNode(%p,%s,%d) -----------\n", aNode, tag, aTransformation);
|
|
|
|
delete [] tag;
|
|
|
|
}
|
1999-04-21 08:08:43 +04:00
|
|
|
// find the current block parent, or just use aNode if it is a block node
|
1999-04-20 21:47:12 +04:00
|
|
|
nsCOMPtr<nsIDOMElement>blockParentElement;
|
1999-04-21 08:08:43 +04:00
|
|
|
nsCOMPtr<nsIDOMNode>nodeToReParent; // this is the node we'll operate on, by default it's aNode
|
|
|
|
nsresult result = aNode->QueryInterface(nsIDOMNode::GetIID(), getter_AddRefs(nodeToReParent));
|
|
|
|
PRBool nodeIsInline;
|
|
|
|
PRBool nodeIsBlock=PR_FALSE;
|
|
|
|
nsTextEditor::IsNodeInline(aNode, nodeIsInline);
|
|
|
|
if (PR_FALSE==nodeIsInline)
|
|
|
|
{
|
|
|
|
nsresult QIResult;
|
|
|
|
nsCOMPtr<nsIDOMCharacterData>nodeAsText;
|
|
|
|
QIResult = aNode->QueryInterface(nsIDOMCharacterData::GetIID(), getter_AddRefs(nodeAsText));
|
|
|
|
if (NS_FAILED(QIResult) || !nodeAsText) {
|
|
|
|
nodeIsBlock=PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
1999-05-01 02:40:18 +04:00
|
|
|
// if aNode is the block parent, then the node to reparent is one of its children
|
1999-04-21 08:08:43 +04:00
|
|
|
if (PR_TRUE==nodeIsBlock)
|
|
|
|
{
|
|
|
|
result = aNode->QueryInterface(nsIDOMNode::GetIID(), getter_AddRefs(blockParentElement));
|
|
|
|
if (NS_SUCCEEDED(result) && blockParentElement) {
|
|
|
|
result = aNode->GetFirstChild(getter_AddRefs(nodeToReParent));
|
|
|
|
}
|
|
|
|
}
|
1999-05-01 02:40:18 +04:00
|
|
|
else { // we just need to get the block parent of aNode
|
1999-04-21 08:08:43 +04:00
|
|
|
result = nsTextEditor::GetBlockParent(aNode, getter_AddRefs(blockParentElement));
|
|
|
|
}
|
|
|
|
// at this point, we must have a good result, a node to reparent, and a block parent
|
|
|
|
if (!nodeToReParent) { return NS_ERROR_UNEXPECTED;}
|
|
|
|
if (!blockParentElement) { return NS_ERROR_NULL_POINTER;}
|
|
|
|
if (NS_SUCCEEDED(result))
|
1999-04-20 21:47:12 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> newParentNode;
|
|
|
|
nsCOMPtr<nsIDOMNode> blockParentNode = do_QueryInterface(blockParentElement);
|
1999-04-21 08:08:43 +04:00
|
|
|
// we need to treat nodes directly inside the body differently
|
1999-04-20 21:47:12 +04:00
|
|
|
nsAutoString parentTag;
|
|
|
|
blockParentElement->GetTagName(parentTag);
|
1999-05-01 02:40:18 +04:00
|
|
|
PRBool isRoot;
|
|
|
|
IsRootTag(parentTag, isRoot);
|
|
|
|
if (PR_TRUE==isRoot)
|
1999-04-20 21:47:12 +04:00
|
|
|
{
|
1999-05-01 02:40:18 +04:00
|
|
|
// if nodeToReParent is a text node, we have <ROOT>Text.
|
|
|
|
// re-parent Text into a new <aTag> at the offset of Text in <ROOT>
|
|
|
|
// so we end up with <ROOT><aTag>Text
|
|
|
|
// ignore aTransformation, replaces act like inserts
|
1999-04-21 08:08:43 +04:00
|
|
|
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(nodeToReParent);
|
1999-04-20 21:47:12 +04:00
|
|
|
if (nodeAsText)
|
|
|
|
{
|
1999-04-21 08:08:43 +04:00
|
|
|
result = ReParentBlockContent(nodeToReParent, aParentTag, blockParentNode, parentTag,
|
1999-05-01 02:40:18 +04:00
|
|
|
aTransformation, getter_AddRefs(newParentNode));
|
1999-04-20 21:47:12 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{ // this is the case of an insertion point between 2 non-text objects
|
1999-05-01 02:40:18 +04:00
|
|
|
// XXX: how to you know it's an insertion point???
|
1999-04-20 21:47:12 +04:00
|
|
|
PRInt32 offsetInParent=0;
|
1999-05-05 08:05:19 +04:00
|
|
|
result = GetChildOffset(nodeToReParent, blockParentNode, offsetInParent);
|
1999-04-20 21:47:12 +04:00
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "bad result from GetChildOffset");
|
|
|
|
// otherwise, just create the block parent at the selection
|
|
|
|
result = nsTextEditor::CreateNode(aParentTag, blockParentNode, offsetInParent,
|
|
|
|
getter_AddRefs(newParentNode));
|
1999-05-01 02:40:18 +04:00
|
|
|
// XXX: need to move some of the children of blockParentNode into the newParentNode?
|
|
|
|
// XXX: need to create a bogus text node inside this new block?
|
1999-04-20 21:47:12 +04:00
|
|
|
// that means, I need to generalize bogus node handling
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
1999-05-01 02:40:18 +04:00
|
|
|
{ // the block parent is not a ROOT,
|
|
|
|
// for the selected block content, transform blockParentNode
|
|
|
|
if (((eReplaceParent==aTransformation) && (PR_FALSE==parentTag.EqualsIgnoreCase(aParentTag))) ||
|
|
|
|
(eInsertParent==aTransformation))
|
1999-04-20 21:47:12 +04:00
|
|
|
{
|
1999-05-01 02:40:18 +04:00
|
|
|
if (gNoisy) { DebugDumpContent(); } // DEBUG
|
1999-04-21 08:08:43 +04:00
|
|
|
result = ReParentBlockContent(nodeToReParent, aParentTag, blockParentNode, parentTag,
|
1999-05-01 02:40:18 +04:00
|
|
|
aTransformation, getter_AddRefs(newParentNode));
|
|
|
|
if ((NS_SUCCEEDED(result)) && (newParentNode) && (eReplaceParent==aTransformation))
|
1999-04-20 21:47:12 +04:00
|
|
|
{
|
|
|
|
PRBool hasChildren;
|
|
|
|
blockParentNode->HasChildNodes(&hasChildren);
|
|
|
|
if (PR_FALSE==hasChildren)
|
|
|
|
{
|
|
|
|
result = nsEditor::DeleteNode(blockParentNode);
|
|
|
|
if (gNoisy)
|
|
|
|
{
|
|
|
|
printf("deleted old block parent node %p\n", blockParentNode.get());
|
|
|
|
DebugDumpContent(); // DEBUG
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else { // otherwise, it's a no-op
|
|
|
|
if (gNoisy) { printf("AddBlockParent is a no-op for this collapsed selection.\n"); }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::ReParentBlockContent(nsIDOMNode *aNode,
|
|
|
|
nsString &aParentTag,
|
|
|
|
nsIDOMNode *aBlockParentNode,
|
|
|
|
nsString &aBlockParentTag,
|
1999-05-05 08:05:19 +04:00
|
|
|
BlockTransformationType aTransformation,
|
1999-04-20 21:47:12 +04:00
|
|
|
nsIDOMNode **aNewParentNode)
|
|
|
|
{
|
|
|
|
if (!aNode || !aBlockParentNode || !aNewParentNode) { return NS_ERROR_NULL_POINTER; }
|
1999-05-05 08:05:19 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> blockParentNode = do_QueryInterface(aBlockParentNode);
|
|
|
|
PRBool removeBlockParent = PR_FALSE;
|
|
|
|
PRBool removeBreakBefore = PR_FALSE;
|
|
|
|
PRBool removeBreakAfter = PR_FALSE;
|
1999-04-20 21:47:12 +04:00
|
|
|
nsCOMPtr<nsIDOMNode>ancestor;
|
|
|
|
nsresult result = aNode->GetParentNode(getter_AddRefs(ancestor));
|
|
|
|
nsCOMPtr<nsIDOMNode>previousAncestor = do_QueryInterface(aNode);
|
|
|
|
while (NS_SUCCEEDED(result) && ancestor)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMElement>ancestorElement = do_QueryInterface(ancestor);
|
|
|
|
nsAutoString ancestorTag;
|
|
|
|
ancestorElement->GetTagName(ancestorTag);
|
|
|
|
if (ancestorTag.EqualsIgnoreCase(aBlockParentTag))
|
|
|
|
{
|
|
|
|
break; // previousAncestor will contain the node to operate on
|
|
|
|
}
|
|
|
|
previousAncestor = do_QueryInterface(ancestor);
|
|
|
|
result = ancestorElement->GetParentNode(getter_AddRefs(ancestor));
|
|
|
|
}
|
|
|
|
// now, previousAncestor is the node we are operating on
|
|
|
|
nsCOMPtr<nsIDOMNode>leftNode, rightNode;
|
1999-05-05 08:05:19 +04:00
|
|
|
result = GetBlockSection(previousAncestor,
|
|
|
|
getter_AddRefs(leftNode),
|
|
|
|
getter_AddRefs(rightNode));
|
1999-04-20 21:47:12 +04:00
|
|
|
if ((NS_SUCCEEDED(result)) && leftNode && rightNode)
|
|
|
|
{
|
1999-05-05 08:05:19 +04:00
|
|
|
// determine some state for managing <BR>s around the new block
|
|
|
|
PRBool isSubordinateBlock = PR_FALSE; // if true, the content is already in a subordinate block
|
|
|
|
PRBool isRootBlock = PR_FALSE; // if true, the content is in a root block
|
|
|
|
nsCOMPtr<nsIDOMElement>blockParentElement = do_QueryInterface(blockParentNode);
|
|
|
|
if (blockParentElement)
|
|
|
|
{
|
|
|
|
nsAutoString blockParentTag;
|
|
|
|
blockParentElement->GetTagName(blockParentTag);
|
|
|
|
IsSubordinateBlock(blockParentTag, isSubordinateBlock);
|
|
|
|
IsRootTag(blockParentTag, isRootBlock);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PR_TRUE==isRootBlock)
|
|
|
|
{ // we're creating a block element where a block element did not previously exist
|
|
|
|
removeBreakBefore = PR_TRUE;
|
|
|
|
removeBreakAfter = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// apply the transformation
|
1999-04-20 21:47:12 +04:00
|
|
|
PRInt32 offsetInParent=0;
|
1999-05-05 08:05:19 +04:00
|
|
|
if (eInsertParent==aTransformation || PR_TRUE==isRootBlock)
|
1999-04-20 21:47:12 +04:00
|
|
|
{
|
1999-05-05 08:05:19 +04:00
|
|
|
result = GetChildOffset(leftNode, blockParentNode, offsetInParent);
|
1999-04-20 21:47:12 +04:00
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "bad result from GetChildOffset");
|
1999-05-05 08:05:19 +04:00
|
|
|
result = nsTextEditor::CreateNode(aParentTag, blockParentNode, offsetInParent, aNewParentNode);
|
|
|
|
if (gNoisy) { printf("created a node in blockParentNode at offset %d\n", offsetInParent); }
|
1999-04-20 21:47:12 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> grandParent;
|
1999-05-05 08:05:19 +04:00
|
|
|
result = blockParentNode->GetParentNode(getter_AddRefs(grandParent));
|
1999-04-20 21:47:12 +04:00
|
|
|
if ((NS_SUCCEEDED(result)) && grandParent)
|
|
|
|
{
|
1999-04-21 08:08:43 +04:00
|
|
|
nsCOMPtr<nsIDOMNode>firstChildNode, lastChildNode;
|
1999-05-05 08:05:19 +04:00
|
|
|
blockParentNode->GetFirstChild(getter_AddRefs(firstChildNode));
|
|
|
|
blockParentNode->GetLastChild(getter_AddRefs(lastChildNode));
|
1999-04-21 08:08:43 +04:00
|
|
|
if (firstChildNode==leftNode && lastChildNode==rightNode)
|
|
|
|
{
|
1999-05-05 08:05:19 +04:00
|
|
|
result = GetChildOffset(blockParentNode, grandParent, offsetInParent);
|
1999-04-21 08:08:43 +04:00
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "bad result from GetChildOffset");
|
|
|
|
result = nsTextEditor::CreateNode(aParentTag, grandParent, offsetInParent, aNewParentNode);
|
|
|
|
if (gNoisy) { printf("created a node in grandParent at offset %d\n", offsetInParent); }
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
1999-05-05 08:05:19 +04:00
|
|
|
// We're in the case where the content of blockParentNode is separated by <BR>'s,
|
|
|
|
// creating multiple block content ranges.
|
|
|
|
// Split blockParentNode around the blockContent
|
|
|
|
if (gNoisy) { printf("splitting a node because of <BR>s\n"); }
|
|
|
|
nsCOMPtr<nsIDOMNode> newLeftNode;
|
|
|
|
if (firstChildNode!=leftNode)
|
|
|
|
{
|
|
|
|
result = GetChildOffset(leftNode, blockParentNode, offsetInParent);
|
|
|
|
if (gNoisy) { printf("splitting left at %d\n", offsetInParent); }
|
|
|
|
result = SplitNode(blockParentNode, offsetInParent, getter_AddRefs(newLeftNode));
|
|
|
|
// after this split, blockParentNode still contains leftNode and rightNode
|
|
|
|
}
|
|
|
|
if (lastChildNode!=rightNode)
|
|
|
|
{
|
|
|
|
result = GetChildOffset(rightNode, blockParentNode, offsetInParent);
|
|
|
|
offsetInParent++;
|
|
|
|
if (gNoisy) { printf("splitting right at %d\n", offsetInParent); }
|
|
|
|
result = SplitNode(blockParentNode, offsetInParent, getter_AddRefs(newLeftNode));
|
|
|
|
blockParentNode = do_QueryInterface(newLeftNode);
|
|
|
|
}
|
|
|
|
result = GetChildOffset(leftNode, blockParentNode, offsetInParent);
|
1999-04-21 08:08:43 +04:00
|
|
|
NS_ASSERTION((NS_SUCCEEDED(result)), "bad result from GetChildOffset");
|
1999-05-05 08:05:19 +04:00
|
|
|
result = nsTextEditor::CreateNode(aParentTag, blockParentNode, offsetInParent, aNewParentNode);
|
|
|
|
if (gNoisy) { printf("created a node in blockParentNode at offset %d\n", offsetInParent); }
|
|
|
|
// what we need to do here is remove the existing block parent when we're all done.
|
|
|
|
removeBlockParent = PR_TRUE;
|
1999-04-21 08:08:43 +04:00
|
|
|
}
|
1999-04-20 21:47:12 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((NS_SUCCEEDED(result)) && *aNewParentNode)
|
1999-05-05 08:05:19 +04:00
|
|
|
{ // move all the children/contents of blockParentNode to aNewParentNode
|
1999-04-20 21:47:12 +04:00
|
|
|
nsCOMPtr<nsIDOMNode>childNode = do_QueryInterface(rightNode);
|
|
|
|
nsCOMPtr<nsIDOMNode>previousSiblingNode;
|
|
|
|
while (NS_SUCCEEDED(result) && childNode)
|
|
|
|
{
|
|
|
|
childNode->GetPreviousSibling(getter_AddRefs(previousSiblingNode));
|
|
|
|
// explicitly delete of childNode from it's current parent
|
|
|
|
// can't just rely on DOM semantics of InsertNode doing the delete implicitly, doesn't undo!
|
|
|
|
result = nsEditor::DeleteNode(childNode);
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
result = nsEditor::InsertNode(childNode, *aNewParentNode, 0);
|
|
|
|
if (gNoisy)
|
|
|
|
{
|
|
|
|
printf("re-parented sibling node %p\n", childNode.get());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (childNode==leftNode || rightNode==leftNode) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
childNode = do_QueryInterface(previousSiblingNode);
|
|
|
|
} // end while loop
|
|
|
|
}
|
1999-05-05 08:05:19 +04:00
|
|
|
// clean up the surrounding content to maintain vertical whitespace
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
// if the prior node is a <BR> and we did something to change vertical whitespacing, delete the <BR>
|
|
|
|
nsCOMPtr<nsIDOMNode> brNode;
|
1999-05-05 08:51:54 +04:00
|
|
|
result = GetPriorNode(leftNode, PR_TRUE, getter_AddRefs(brNode));
|
1999-05-05 08:05:19 +04:00
|
|
|
if (NS_SUCCEEDED(result) && brNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> brContent = do_QueryInterface(brNode);
|
|
|
|
if (brContent)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAtom> brContentTag;
|
|
|
|
brContent->GetTag(*getter_AddRefs(brContentTag));
|
1999-05-05 09:56:58 +04:00
|
|
|
if (nsIEditProperty::br==brContentTag.get()) {
|
1999-05-05 08:05:19 +04:00
|
|
|
result = DeleteNode(brNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the next node is a <BR> and we did something to change vertical whitespacing, delete the <BR>
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
1999-05-05 08:51:54 +04:00
|
|
|
result = GetNextNode(rightNode, PR_TRUE, getter_AddRefs(brNode));
|
1999-05-05 08:05:19 +04:00
|
|
|
if (NS_SUCCEEDED(result) && brNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> brContent = do_QueryInterface(brNode);
|
|
|
|
if (brContent)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAtom> brContentTag;
|
|
|
|
brContent->GetTag(*getter_AddRefs(brContentTag));
|
1999-05-05 09:56:58 +04:00
|
|
|
if (nsIEditProperty::br==brContentTag.get()) {
|
1999-05-05 08:05:19 +04:00
|
|
|
result = DeleteNode(brNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((NS_SUCCEEDED(result)) && (PR_TRUE==removeBlockParent))
|
|
|
|
{ // we determined we need to remove the previous block parent. Do it!
|
|
|
|
// go through list backwards so deletes don't interfere with the iteration
|
|
|
|
nsCOMPtr<nsIDOMNodeList> childNodes;
|
|
|
|
result = blockParentNode->GetChildNodes(getter_AddRefs(childNodes));
|
|
|
|
if ((NS_SUCCEEDED(result)) && (childNodes))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode>grandParent;
|
|
|
|
blockParentNode->GetParentNode(getter_AddRefs(grandParent));
|
|
|
|
PRInt32 offsetInParent;
|
|
|
|
result = GetChildOffset(blockParentNode, grandParent, offsetInParent);
|
|
|
|
PRUint32 childCount;
|
|
|
|
childNodes->GetLength(&childCount);
|
|
|
|
PRInt32 i=childCount-1;
|
|
|
|
for ( ; ((NS_SUCCEEDED(result)) && (0<=i)); i--)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> childNode;
|
|
|
|
result = childNodes->Item(i, getter_AddRefs(childNode));
|
|
|
|
if ((NS_SUCCEEDED(result)) && (childNode))
|
|
|
|
{
|
|
|
|
result = nsTextEditor::DeleteNode(childNode);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = nsTextEditor::InsertNode(childNode, grandParent, offsetInParent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (gNoisy) { printf("removing the old block parent %p\n", blockParentNode.get()); }
|
|
|
|
result = nsTextEditor::DeleteNode(blockParentNode);
|
|
|
|
}
|
|
|
|
}
|
1999-04-20 21:47:12 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-05-01 02:40:18 +04:00
|
|
|
nsHTMLEditor::ReParentContentOfRange(nsIDOMRange *aRange,
|
|
|
|
nsString &aParentTag,
|
|
|
|
BlockTransformationType aTranformation)
|
1999-04-20 21:47:12 +04:00
|
|
|
{
|
1999-05-01 02:40:18 +04:00
|
|
|
if (!aRange) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
nsresult result;
|
1999-05-05 08:05:19 +04:00
|
|
|
nsISupportsArray *blockSections;
|
|
|
|
result = NS_NewISupportsArray(&blockSections);
|
|
|
|
if ((NS_SUCCEEDED(result)) && blockSections)
|
1999-05-01 02:40:18 +04:00
|
|
|
{
|
1999-05-05 08:05:19 +04:00
|
|
|
result = GetBlockSectionsForRange(aRange, blockSections);
|
1999-05-01 02:40:18 +04:00
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
nsIDOMRange *subRange;
|
1999-05-05 08:05:19 +04:00
|
|
|
subRange = (nsIDOMRange *)(blockSections->ElementAt(0));
|
1999-05-01 02:40:18 +04:00
|
|
|
while (subRange && (NS_SUCCEEDED(result)))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode>startParent;
|
|
|
|
result = subRange->GetStartParent(getter_AddRefs(startParent));
|
|
|
|
if (NS_SUCCEEDED(result) && startParent)
|
|
|
|
{
|
1999-05-05 08:05:19 +04:00
|
|
|
if (gNoisy) { printf("ReParentContentOfRange calling ReParentContentOfNode\n"); }
|
1999-05-01 02:40:18 +04:00
|
|
|
result = ReParentContentOfNode(startParent, aParentTag, aTranformation);
|
|
|
|
}
|
|
|
|
NS_RELEASE(subRange);
|
1999-05-05 08:05:19 +04:00
|
|
|
blockSections->RemoveElementAt(0);
|
|
|
|
subRange = (nsIDOMRange *)(blockSections->ElementAt(0));
|
1999-05-01 02:40:18 +04:00
|
|
|
}
|
|
|
|
}
|
1999-05-05 08:05:19 +04:00
|
|
|
NS_RELEASE(blockSections);
|
1999-05-01 02:40:18 +04:00
|
|
|
}
|
1999-04-20 21:47:12 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-04-22 01:01:59 +04:00
|
|
|
// this should probably get moved into rules? is this an app-level choice, or
|
|
|
|
// a restriction of HTML itself?
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::CanContainBlock(nsString &aBlockChild, nsString &aBlockParent, PRBool &aCanContain)
|
|
|
|
{
|
|
|
|
if (aBlockParent.EqualsIgnoreCase("body") ||
|
|
|
|
aBlockParent.EqualsIgnoreCase("td") ||
|
|
|
|
aBlockParent.EqualsIgnoreCase("th") ||
|
|
|
|
aBlockParent.EqualsIgnoreCase("blockquote") )
|
|
|
|
{
|
|
|
|
aCanContain = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
aCanContain = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-04-20 21:47:12 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
1999-05-05 08:05:19 +04:00
|
|
|
nsHTMLEditor::RemoveParagraphStyle()
|
1999-04-20 21:47:12 +04:00
|
|
|
{
|
1999-04-22 18:45:48 +04:00
|
|
|
if (gNoisy) {
|
1999-05-05 08:05:19 +04:00
|
|
|
printf("---------- nsHTMLEditor::RemoveParagraphStyle ----------\n");
|
1999-04-22 18:45:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult result=NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsCOMPtr<nsIDOMSelection>selection;
|
|
|
|
result = nsEditor::GetSelection(getter_AddRefs(selection));
|
|
|
|
if ((NS_SUCCEEDED(result)) && selection)
|
|
|
|
{
|
|
|
|
nsEditor::BeginTransaction();
|
|
|
|
nsCOMPtr<nsIEnumerator> enumerator;
|
|
|
|
enumerator = do_QueryInterface(selection);
|
|
|
|
if (enumerator)
|
|
|
|
{
|
|
|
|
enumerator->First();
|
|
|
|
nsISupports *currentItem;
|
|
|
|
result = enumerator->CurrentItem(¤tItem);
|
|
|
|
if ((NS_SUCCEEDED(result)) && (nsnull!=currentItem))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMRange> range( do_QueryInterface(currentItem) );
|
1999-05-05 08:05:19 +04:00
|
|
|
result = RemoveParagraphStyleFromRange(range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nsEditor::EndTransaction();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::RemoveParagraphStyleFromRange(nsIDOMRange *aRange)
|
|
|
|
{
|
|
|
|
if (!aRange) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
nsresult result;
|
|
|
|
nsISupportsArray *blockSections;
|
|
|
|
result = NS_NewISupportsArray(&blockSections);
|
|
|
|
if ((NS_SUCCEEDED(result)) && blockSections)
|
|
|
|
{
|
|
|
|
result = GetBlockSectionsForRange(aRange, blockSections);
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
nsIDOMRange *subRange;
|
|
|
|
subRange = (nsIDOMRange *)(blockSections->ElementAt(0));
|
|
|
|
while (subRange && (NS_SUCCEEDED(result)))
|
|
|
|
{
|
|
|
|
result = RemoveParagraphStyleFromBlockContent(subRange);
|
|
|
|
NS_RELEASE(subRange);
|
|
|
|
blockSections->RemoveElementAt(0);
|
|
|
|
subRange = (nsIDOMRange *)(blockSections->ElementAt(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_RELEASE(blockSections);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::RemoveParagraphStyleFromBlockContent(nsIDOMRange *aRange)
|
|
|
|
{
|
|
|
|
if (!aRange) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
nsresult result;
|
|
|
|
nsCOMPtr<nsIDOMNode>startParent;
|
|
|
|
aRange->GetStartParent(getter_AddRefs(startParent));
|
|
|
|
nsCOMPtr<nsIDOMElement>blockParentElement;
|
|
|
|
result = nsTextEditor::GetBlockParent(startParent, getter_AddRefs(blockParentElement));
|
|
|
|
while ((NS_SUCCEEDED(result)) && blockParentElement)
|
|
|
|
{
|
|
|
|
nsAutoString blockParentTag;
|
|
|
|
blockParentElement->GetTagName(blockParentTag);
|
|
|
|
PRBool isSubordinateBlock;
|
|
|
|
IsSubordinateBlock(blockParentTag, isSubordinateBlock);
|
|
|
|
if (PR_FALSE==isSubordinateBlock) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// go through list backwards so deletes don't interfere with the iteration
|
|
|
|
nsCOMPtr<nsIDOMNodeList> childNodes;
|
|
|
|
result = blockParentElement->GetChildNodes(getter_AddRefs(childNodes));
|
|
|
|
if ((NS_SUCCEEDED(result)) && (childNodes))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode>grandParent;
|
|
|
|
blockParentElement->GetParentNode(getter_AddRefs(grandParent));
|
|
|
|
PRInt32 offsetInParent;
|
|
|
|
result = GetChildOffset(blockParentElement, grandParent, offsetInParent);
|
|
|
|
PRUint32 childCount;
|
|
|
|
childNodes->GetLength(&childCount);
|
|
|
|
PRInt32 i=childCount-1;
|
|
|
|
for ( ; ((NS_SUCCEEDED(result)) && (0<=i)); i--)
|
1999-04-22 18:45:48 +04:00
|
|
|
{
|
1999-05-05 08:05:19 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> childNode;
|
|
|
|
result = childNodes->Item(i, getter_AddRefs(childNode));
|
|
|
|
if ((NS_SUCCEEDED(result)) && (childNode))
|
|
|
|
{
|
|
|
|
result = nsTextEditor::DeleteNode(childNode);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = nsTextEditor::InsertNode(childNode, grandParent, offsetInParent);
|
1999-04-22 18:45:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-05-05 08:05:19 +04:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = nsTextEditor::DeleteNode(blockParentElement);
|
|
|
|
}
|
1999-04-22 18:45:48 +04:00
|
|
|
}
|
|
|
|
}
|
1999-05-05 08:05:19 +04:00
|
|
|
result = nsTextEditor::GetBlockParent(startParent, getter_AddRefs(blockParentElement));
|
1999-04-22 18:45:48 +04:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-05-05 08:05:19 +04:00
|
|
|
|
1999-04-22 18:45:48 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::RemoveParent(const nsString &aParentTag)
|
|
|
|
{
|
|
|
|
if (gNoisy) {
|
1999-05-06 03:29:18 +04:00
|
|
|
printf("---------- nsHTMLEditor::RemoveParent ----------\n");
|
1999-04-22 18:45:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult result=NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsCOMPtr<nsIDOMSelection>selection;
|
|
|
|
result = nsEditor::GetSelection(getter_AddRefs(selection));
|
|
|
|
if ((NS_SUCCEEDED(result)) && selection)
|
|
|
|
{
|
|
|
|
nsEditor::BeginTransaction();
|
|
|
|
nsCOMPtr<nsIEnumerator> enumerator;
|
|
|
|
enumerator = do_QueryInterface(selection);
|
|
|
|
if (enumerator)
|
|
|
|
{
|
|
|
|
enumerator->First();
|
|
|
|
nsISupports *currentItem;
|
|
|
|
result = enumerator->CurrentItem(¤tItem);
|
|
|
|
if ((NS_SUCCEEDED(result)) && (nsnull!=currentItem))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMRange> range( do_QueryInterface(currentItem) );
|
1999-05-05 08:05:19 +04:00
|
|
|
result = RemoveParentFromRange(aParentTag, range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nsEditor::EndTransaction();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::RemoveParentFromRange(const nsString &aParentTag, nsIDOMRange *aRange)
|
|
|
|
{
|
|
|
|
if (!aRange) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
nsresult result;
|
|
|
|
nsISupportsArray *blockSections;
|
|
|
|
result = NS_NewISupportsArray(&blockSections);
|
|
|
|
if ((NS_SUCCEEDED(result)) && blockSections)
|
|
|
|
{
|
|
|
|
result = GetBlockSectionsForRange(aRange, blockSections);
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
nsIDOMRange *subRange;
|
|
|
|
subRange = (nsIDOMRange *)(blockSections->ElementAt(0));
|
|
|
|
while (subRange && (NS_SUCCEEDED(result)))
|
|
|
|
{
|
|
|
|
result = RemoveParentFromBlockContent(aParentTag, subRange);
|
|
|
|
NS_RELEASE(subRange);
|
|
|
|
blockSections->RemoveElementAt(0);
|
|
|
|
subRange = (nsIDOMRange *)(blockSections->ElementAt(0));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
NS_RELEASE(blockSections);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::RemoveParentFromBlockContent(const nsString &aParentTag, nsIDOMRange *aRange)
|
|
|
|
{
|
|
|
|
if (!aRange) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
nsresult result;
|
|
|
|
nsCOMPtr<nsIDOMNode>startParent;
|
|
|
|
result = aRange->GetStartParent(getter_AddRefs(startParent));
|
|
|
|
if ((NS_SUCCEEDED(result)) && startParent)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode>parentNode;
|
|
|
|
nsCOMPtr<nsIDOMElement>parentElement;
|
|
|
|
result = startParent->GetParentNode(getter_AddRefs(parentNode));
|
|
|
|
while ((NS_SUCCEEDED(result)) && parentNode)
|
|
|
|
{
|
|
|
|
parentElement = do_QueryInterface(parentNode);
|
|
|
|
nsAutoString parentTag;
|
|
|
|
parentElement->GetTagName(parentTag);
|
|
|
|
PRBool isRoot;
|
|
|
|
IsRootTag(parentTag, isRoot);
|
|
|
|
if (aParentTag.EqualsIgnoreCase(parentTag))
|
|
|
|
{
|
|
|
|
// go through list backwards so deletes don't interfere with the iteration
|
|
|
|
nsCOMPtr<nsIDOMNodeList> childNodes;
|
|
|
|
result = parentElement->GetChildNodes(getter_AddRefs(childNodes));
|
|
|
|
if ((NS_SUCCEEDED(result)) && (childNodes))
|
1999-04-22 18:45:48 +04:00
|
|
|
{
|
1999-05-05 08:05:19 +04:00
|
|
|
nsCOMPtr<nsIDOMNode>grandParent;
|
|
|
|
parentElement->GetParentNode(getter_AddRefs(grandParent));
|
|
|
|
PRInt32 offsetInParent;
|
|
|
|
result = GetChildOffset(parentElement, grandParent, offsetInParent);
|
|
|
|
PRUint32 childCount;
|
|
|
|
childNodes->GetLength(&childCount);
|
|
|
|
PRInt32 i=childCount-1;
|
|
|
|
for ( ; ((NS_SUCCEEDED(result)) && (0<=i)); i--)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> childNode;
|
|
|
|
result = childNodes->Item(i, getter_AddRefs(childNode));
|
|
|
|
if ((NS_SUCCEEDED(result)) && (childNode))
|
1999-04-22 18:45:48 +04:00
|
|
|
{
|
1999-05-05 08:05:19 +04:00
|
|
|
result = nsTextEditor::DeleteNode(childNode);
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = nsTextEditor::InsertNode(childNode, grandParent, offsetInParent);
|
1999-04-22 18:45:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-05-05 08:05:19 +04:00
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
result = nsTextEditor::DeleteNode(parentElement);
|
|
|
|
}
|
1999-04-22 18:45:48 +04:00
|
|
|
}
|
1999-05-05 08:05:19 +04:00
|
|
|
break;
|
1999-04-22 18:45:48 +04:00
|
|
|
}
|
1999-05-05 08:05:19 +04:00
|
|
|
else if (PR_TRUE==isRoot) { // hit a local root node, terminate loop
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
result = parentElement->GetParentNode(getter_AddRefs(parentNode));
|
1999-04-22 18:45:48 +04:00
|
|
|
}
|
|
|
|
}
|
1999-04-20 21:47:12 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-03-30 02:01:26 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::InsertLink(nsString& aURL)
|
|
|
|
{
|
|
|
|
nsresult res = nsEditor::BeginTransaction();
|
|
|
|
|
|
|
|
// Find out if the selection is collapsed:
|
|
|
|
nsCOMPtr<nsIDOMSelection> selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (!NS_SUCCEEDED(res) || !selection)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_akkana
|
|
|
|
printf("Can't get selection!");
|
|
|
|
#endif
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
PRBool isCollapsed;
|
1999-04-13 05:33:32 +04:00
|
|
|
res = selection->GetIsCollapsed(&isCollapsed);
|
1999-03-30 02:01:26 +04:00
|
|
|
if (!NS_SUCCEEDED(res))
|
|
|
|
isCollapsed = PR_TRUE;
|
|
|
|
|
|
|
|
// Temporary: we need to save the contents of the selection,
|
|
|
|
// then insert them back in as the child of the newly created
|
|
|
|
// anchor node in order to put the link around the selection.
|
|
|
|
// This will require copying the selection into a document fragment,
|
|
|
|
// then splicing the document fragment back into the tree after the
|
|
|
|
// new anchor node has been put in place. As a temporary solution,
|
|
|
|
// Copy/Paste does this for us in the text case
|
|
|
|
// (and eventually in all cases).
|
|
|
|
if (!isCollapsed)
|
|
|
|
(void)Copy();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> newNode;
|
|
|
|
nsAutoString tag("A");
|
|
|
|
res = nsEditor::DeleteSelectionAndCreateNode(tag, getter_AddRefs(newNode));
|
|
|
|
if (!NS_SUCCEEDED(res) || !newNode)
|
|
|
|
return res;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMHTMLAnchorElement> anchor (do_QueryInterface(newNode));
|
|
|
|
if (!anchor)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_akkana
|
|
|
|
printf("Not an anchor element\n");
|
|
|
|
#endif
|
|
|
|
return NS_NOINTERFACE;
|
|
|
|
}
|
|
|
|
|
|
|
|
res = anchor->SetHref(aURL);
|
|
|
|
if (!NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_akkana
|
|
|
|
printf("SetHref failed");
|
|
|
|
#endif
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the selection to the node we just inserted:
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (!NS_SUCCEEDED(res) || !selection)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_akkana
|
|
|
|
printf("Can't get selection!");
|
|
|
|
#endif
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
res = selection->Collapse(newNode, 0);
|
|
|
|
if (!NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_akkana
|
|
|
|
printf("Couldn't collapse");
|
|
|
|
#endif
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we weren't collapsed, paste the old selection back in under the link:
|
|
|
|
if (!isCollapsed)
|
|
|
|
(void)Paste();
|
|
|
|
// Otherwise (we were collapsed) insert some bogus text in
|
|
|
|
// so the link will be visible
|
|
|
|
else
|
|
|
|
{
|
|
|
|
nsString link("[***]");
|
|
|
|
(void) InsertText(link); // ignore return value -- we don't care
|
|
|
|
}
|
|
|
|
|
|
|
|
nsEditor::EndTransaction(); // don't return this result!
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::InsertImage(nsString& aURL,
|
|
|
|
nsString& aWidth, nsString& aHeight,
|
|
|
|
nsString& aHspace, nsString& aVspace,
|
|
|
|
nsString& aBorder,
|
|
|
|
nsString& aAlt,
|
|
|
|
nsString& aAlignment)
|
|
|
|
{
|
1999-04-06 23:49:33 +04:00
|
|
|
nsresult res;
|
1999-03-30 02:01:26 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> newNode;
|
1999-04-30 22:03:39 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument>doc;
|
|
|
|
res = GetDocument(getter_AddRefs(doc));
|
|
|
|
if (NS_SUCCEEDED(res))
|
1999-04-21 22:53:55 +04:00
|
|
|
{
|
1999-04-30 22:03:39 +04:00
|
|
|
nsAutoString tag("IMG");
|
|
|
|
nsCOMPtr<nsIDOMElement>newElement;
|
|
|
|
res = doc->CreateElement(tag, getter_AddRefs(newElement));
|
|
|
|
if (NS_SUCCEEDED(res) && newElement)
|
|
|
|
{
|
|
|
|
newNode = do_QueryInterface(newElement);
|
|
|
|
nsCOMPtr<nsIDOMHTMLImageElement> image (do_QueryInterface(newNode));
|
|
|
|
// Set all the attributes now, before we insert into the tree:
|
|
|
|
if (image)
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(res = image->SetSrc(aURL)))
|
|
|
|
if (NS_SUCCEEDED(res = image->SetWidth(aWidth)))
|
|
|
|
if (NS_SUCCEEDED(res = image->SetHeight(aHeight)))
|
|
|
|
if (NS_SUCCEEDED(res = image->SetAlt(aAlt)))
|
|
|
|
if (NS_SUCCEEDED(res = image->SetBorder(aBorder)))
|
|
|
|
if (NS_SUCCEEDED(res = image->SetAlign(aAlignment)))
|
|
|
|
if (NS_SUCCEEDED(res = image->SetHspace(aHspace)))
|
|
|
|
if (NS_SUCCEEDED(res = image->SetVspace(aVspace)))
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
1999-04-21 22:53:55 +04:00
|
|
|
}
|
1999-04-08 04:46:10 +04:00
|
|
|
|
1999-04-30 22:03:39 +04:00
|
|
|
// If any of these failed, then don't insert the new node into the tree
|
|
|
|
if (!NS_SUCCEEDED(res))
|
1999-04-06 23:49:33 +04:00
|
|
|
{
|
1999-04-21 22:53:55 +04:00
|
|
|
#ifdef DEBUG_akkana
|
1999-04-30 22:03:39 +04:00
|
|
|
printf("Some failure creating the new image node\n");
|
1999-04-21 22:53:55 +04:00
|
|
|
#endif
|
1999-04-30 22:03:39 +04:00
|
|
|
return res;
|
1999-04-21 22:53:55 +04:00
|
|
|
}
|
|
|
|
|
1999-04-30 22:03:39 +04:00
|
|
|
//
|
|
|
|
// Now we're ready to insert the new image node:
|
|
|
|
// Starting now, don't return without ending the transaction!
|
|
|
|
//
|
|
|
|
(void)nsEditor::BeginTransaction();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> parentNode;
|
|
|
|
PRInt32 offsetOfNewNode;
|
|
|
|
res = nsEditor::DeleteSelectionAndPrepareToCreateNode(parentNode,
|
|
|
|
offsetOfNewNode);
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
// and insert it into the right place in the tree:
|
|
|
|
res = InsertNode(newNode, parentNode, offsetOfNewNode);
|
|
|
|
}
|
1999-04-21 22:53:55 +04:00
|
|
|
|
|
|
|
(void)nsEditor::EndTransaction(); // don't return this result!
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This should replace InsertLink and InsertImage once it is working
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetSelectedElement(const nsString& aTagName, nsIDOMElement** aReturn)
|
|
|
|
{
|
|
|
|
if (!aReturn )
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*aReturn = nsnull;
|
|
|
|
|
|
|
|
nsAutoString TagName = aTagName;
|
|
|
|
TagName.ToLowerCase();
|
|
|
|
|
|
|
|
//Note that this doesn't need to go through the transaction system
|
|
|
|
|
|
|
|
nsresult result=NS_ERROR_NOT_INITIALIZED;
|
|
|
|
PRBool first=PR_TRUE;
|
|
|
|
nsCOMPtr<nsIDOMSelection>selection;
|
|
|
|
result = nsEditor::GetSelection(getter_AddRefs(selection));
|
|
|
|
if (!NS_SUCCEEDED(result) || !selection)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
PRBool isCollapsed;
|
|
|
|
selection->GetIsCollapsed(&isCollapsed);
|
|
|
|
nsCOMPtr<nsIDOMElement> selectedElement;
|
|
|
|
PRBool bNodeFound = PR_FALSE;
|
|
|
|
|
|
|
|
// Don't bother to examine selection if it is collapsed
|
|
|
|
if (!isCollapsed)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIEnumerator> enumerator;
|
|
|
|
enumerator = do_QueryInterface(selection);
|
|
|
|
if (enumerator)
|
1999-04-08 04:46:10 +04:00
|
|
|
{
|
1999-04-21 22:53:55 +04:00
|
|
|
enumerator->First();
|
|
|
|
nsISupports *currentItem;
|
|
|
|
result = enumerator->CurrentItem(¤tItem);
|
|
|
|
if ((NS_SUCCEEDED(result)) && currentItem)
|
1999-04-08 04:46:10 +04:00
|
|
|
{
|
1999-04-21 22:53:55 +04:00
|
|
|
nsCOMPtr<nsIDOMRange> range( do_QueryInterface(currentItem) );
|
|
|
|
nsCOMPtr<nsIContentIterator> iter;
|
|
|
|
result = nsComponentManager::CreateInstance(kCContentIteratorCID, nsnull,
|
|
|
|
kIContentIteratorIID,
|
|
|
|
getter_AddRefs(iter));
|
|
|
|
if ((NS_SUCCEEDED(result)) && iter)
|
|
|
|
{
|
|
|
|
iter->Init(range);
|
|
|
|
// loop through the content iterator for each content node
|
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
result = iter->CurrentNode(getter_AddRefs(content));
|
|
|
|
PRBool bOtherNodeTypeFound = PR_FALSE;
|
|
|
|
|
|
|
|
while (NS_COMFALSE == iter->IsDone())
|
|
|
|
{
|
|
|
|
// Query interface to cast nsIContent to nsIDOMNode
|
|
|
|
// then get tagType to compare to aTagName
|
|
|
|
// Clone node of each desired type and append it to the aDomFrag
|
|
|
|
selectedElement = do_QueryInterface(content);
|
|
|
|
if (selectedElement)
|
|
|
|
{
|
|
|
|
// If we already found a node, then we have another element,
|
|
|
|
// so don't return an element
|
|
|
|
if (bNodeFound)
|
|
|
|
{
|
|
|
|
bNodeFound;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString domTagName;
|
|
|
|
selectedElement->GetNodeName(domTagName);
|
1999-05-08 02:26:23 +04:00
|
|
|
domTagName.ToLowerCase();
|
1999-04-21 22:53:55 +04:00
|
|
|
|
|
|
|
// The "A" tag is a pain,
|
|
|
|
// used for both link(href is set) and "Named Anchor"
|
1999-05-08 02:26:23 +04:00
|
|
|
if (aTagName == "href" || (aTagName == "anchor"))
|
1999-04-21 22:53:55 +04:00
|
|
|
{
|
|
|
|
// We could use GetAttribute, but might as well use anchor element directly
|
|
|
|
nsCOMPtr<nsIDOMHTMLAnchorElement> anchor = do_QueryInterface(selectedElement);
|
|
|
|
if (anchor)
|
|
|
|
{
|
|
|
|
nsString tmpText;
|
1999-05-08 02:26:23 +04:00
|
|
|
if( aTagName == "href")
|
1999-04-21 22:53:55 +04:00
|
|
|
{
|
1999-04-22 10:19:44 +04:00
|
|
|
if (NS_SUCCEEDED(anchor->GetHref(tmpText)) && tmpText.GetUnicode() && tmpText.Length() != 0)
|
1999-04-21 22:53:55 +04:00
|
|
|
bNodeFound = PR_TRUE;
|
1999-05-08 02:26:23 +04:00
|
|
|
} else if (aTagName == "anchor")
|
1999-04-21 22:53:55 +04:00
|
|
|
{
|
1999-04-22 10:19:44 +04:00
|
|
|
if (NS_SUCCEEDED(anchor->GetName(tmpText)) && tmpText.GetUnicode() && tmpText.Length() != 0)
|
1999-04-21 22:53:55 +04:00
|
|
|
bNodeFound = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (aTagName == domTagName) { // All other tag names are handled here
|
|
|
|
bNodeFound = PR_TRUE;
|
|
|
|
}
|
|
|
|
if (!bNodeFound)
|
|
|
|
{
|
|
|
|
// Check if node we have is really part of the selection???
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
1999-04-27 05:10:10 +04:00
|
|
|
|
|
|
|
iter->Next();
|
1999-04-21 22:53:55 +04:00
|
|
|
}
|
1999-05-07 04:44:46 +04:00
|
|
|
if (!bNodeFound) {
|
|
|
|
char TagBuf[50] = "";
|
|
|
|
printf("No nodes of tag name = %s were found in selection\n", aTagName.ToCString(TagBuf, 50));
|
|
|
|
}
|
1999-04-21 22:53:55 +04:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Should never get here?
|
|
|
|
isCollapsed = PR_TRUE;
|
|
|
|
printf("isCollapsed was FALSE, but no elements found in selection\n");
|
1999-04-08 04:46:10 +04:00
|
|
|
}
|
1999-04-21 22:53:55 +04:00
|
|
|
} else {
|
|
|
|
printf("Could not create enumerator for GetSelectionProperties\n");
|
1999-04-08 04:46:10 +04:00
|
|
|
}
|
1999-04-06 23:49:33 +04:00
|
|
|
}
|
1999-04-21 22:53:55 +04:00
|
|
|
if (bNodeFound)
|
|
|
|
{
|
|
|
|
|
|
|
|
*aReturn = selectedElement;
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
1999-03-30 02:01:26 +04:00
|
|
|
|
1999-04-21 22:53:55 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::CreateElementWithDefaults(const nsString& aTagName, nsIDOMElement** aReturn)
|
|
|
|
{
|
|
|
|
nsresult result=NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (aReturn)
|
|
|
|
*aReturn = nsnull;
|
|
|
|
|
|
|
|
if (aTagName == "" || !aReturn)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsAutoString TagName = aTagName;
|
|
|
|
TagName.ToLowerCase();
|
|
|
|
nsAutoString realTagName;
|
|
|
|
|
|
|
|
PRBool isHREF = (TagName == "href");
|
|
|
|
PRBool isAnchor = (TagName == "anchor");
|
|
|
|
if (isHREF || isAnchor)
|
1999-03-30 02:01:26 +04:00
|
|
|
{
|
1999-04-21 22:53:55 +04:00
|
|
|
realTagName = "a";
|
|
|
|
} else {
|
|
|
|
realTagName = TagName;
|
1999-03-30 02:01:26 +04:00
|
|
|
}
|
1999-04-21 22:53:55 +04:00
|
|
|
//We don't use editor's CreateElement because we don't want to
|
|
|
|
// go through the transaction system
|
1999-03-30 02:01:26 +04:00
|
|
|
|
1999-04-21 22:53:55 +04:00
|
|
|
nsCOMPtr<nsIDOMElement>newElement;
|
|
|
|
result = mDoc->CreateElement(realTagName, getter_AddRefs(newElement));
|
|
|
|
if (!NS_SUCCEEDED(result) || !newElement)
|
|
|
|
return NS_ERROR_FAILURE;
|
1999-04-08 04:46:10 +04:00
|
|
|
|
1999-04-21 22:53:55 +04:00
|
|
|
|
|
|
|
// Set default values for new elements
|
|
|
|
// SHOULD THIS BE PUT IN "RULES" SYSTEM OR
|
|
|
|
// ATTRIBUTES SAVED IN PREFS?
|
|
|
|
if (isAnchor)
|
1999-04-08 04:46:10 +04:00
|
|
|
{
|
1999-04-21 22:53:55 +04:00
|
|
|
// TODO: Get the text of the selection and build a suggested Name
|
|
|
|
// Replace spaces with "_"
|
1999-04-08 04:46:10 +04:00
|
|
|
}
|
1999-04-21 22:53:55 +04:00
|
|
|
// ADD OTHER DEFAULT ATTRIBUTES HERE
|
1999-04-06 23:49:33 +04:00
|
|
|
|
1999-04-21 22:53:55 +04:00
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
*aReturn = newElement; //do_QueryInterface(newElement);
|
|
|
|
}
|
1999-04-06 23:49:33 +04:00
|
|
|
|
1999-04-21 22:53:55 +04:00
|
|
|
return result;
|
1999-03-30 02:01:26 +04:00
|
|
|
}
|
|
|
|
|
1999-04-21 22:53:55 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::InsertElement(nsIDOMElement* aElement, PRBool aDeleteSelection, nsIDOMElement** aReturn)
|
|
|
|
{
|
|
|
|
nsresult result=NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (aReturn)
|
|
|
|
*aReturn = nsnull;
|
|
|
|
|
|
|
|
if (!aElement || !aReturn)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
result = nsEditor::BeginTransaction();
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
return result;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> parentSelectedNode;
|
|
|
|
PRInt32 offsetOfNewNode;
|
|
|
|
|
1999-05-07 04:44:46 +04:00
|
|
|
// Clear current selection.
|
|
|
|
// Should put caret at anchor point?
|
|
|
|
if (!aDeleteSelection)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMSelection>selection;
|
|
|
|
nsresult res = nsEditor::GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_SUCCEEDED(res) && selection)
|
|
|
|
{
|
|
|
|
selection->ClearSelection();
|
|
|
|
}
|
|
|
|
}
|
1999-05-14 00:59:08 +04:00
|
|
|
nsAutoString tagName;
|
|
|
|
|
|
|
|
// MAJOR KLUDGE -- CONVERT THE PLATFORM-SPECIFIC FORMAT INTO URL FORMAT
|
|
|
|
// This should be done by the file-picker widget
|
|
|
|
nsCOMPtr<nsIDOMHTMLImageElement> image = (do_QueryInterface(aElement));
|
|
|
|
|
|
|
|
if (image)
|
|
|
|
{
|
|
|
|
printf("INSERTING AN IMAGE\n");
|
|
|
|
nsAutoString src;
|
|
|
|
image->GetSrc(src);
|
|
|
|
nsFileSpec fileSpec(src);
|
|
|
|
nsFileURL fileURL(fileSpec);
|
|
|
|
}
|
1999-05-07 04:44:46 +04:00
|
|
|
|
1999-04-21 22:53:55 +04:00
|
|
|
DeleteSelectionAndPrepareToCreateNode(parentSelectedNode, offsetOfNewNode);
|
1999-05-07 04:44:46 +04:00
|
|
|
|
1999-04-21 22:53:55 +04:00
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> newNode = do_QueryInterface(aElement);
|
|
|
|
|
|
|
|
result = InsertNode(aElement, parentSelectedNode, offsetOfNewNode);
|
1999-03-30 02:01:26 +04:00
|
|
|
|
1999-04-21 22:53:55 +04:00
|
|
|
}
|
|
|
|
(void)nsEditor::EndTransaction();
|
|
|
|
|
1999-05-06 05:33:48 +04:00
|
|
|
// XXXX: Horrible hack! We are doing this because
|
|
|
|
// of an error in Gecko which is not rendering the
|
|
|
|
// document after a change via the DOM - gpk 2/13/99
|
|
|
|
// BEGIN HACK!!!
|
|
|
|
HACKForceRedraw();
|
|
|
|
// END HACK
|
|
|
|
|
1999-04-21 22:53:55 +04:00
|
|
|
return result;
|
|
|
|
}
|
1999-05-04 02:57:48 +04:00
|
|
|
|
1999-05-08 02:26:23 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::InsertLinkAroundSelection(nsIDOMElement* aAnchorElement)
|
|
|
|
{
|
|
|
|
nsresult result=NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
if (!aAnchorElement)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// We must have a real selection
|
|
|
|
nsCOMPtr<nsIDOMSelection> selection;
|
|
|
|
result = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (!NS_SUCCEEDED(result) || !selection)
|
|
|
|
return result;
|
|
|
|
|
|
|
|
PRBool isCollapsed;
|
|
|
|
result = selection->GetIsCollapsed(&isCollapsed);
|
|
|
|
if (!NS_SUCCEEDED(result))
|
|
|
|
isCollapsed = PR_TRUE;
|
|
|
|
|
|
|
|
if (isCollapsed)
|
|
|
|
{
|
|
|
|
printf("InsertLinkAroundSelection called but there is no selection!!!\n");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Be sure we were given an anchor element
|
|
|
|
nsCOMPtr<nsIDOMHTMLAnchorElement> anchor = do_QueryInterface(aAnchorElement);
|
|
|
|
if (anchor)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
|
|
|
if (NS_SUCCEEDED(anchor->GetHref(href)) && href.GetUnicode() && href.Length() > 0)
|
|
|
|
{
|
|
|
|
result = nsEditor::BeginTransaction();
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
const nsString attribute("href");
|
|
|
|
SetTextProperty(nsIEditProperty::a, &attribute, &href);
|
|
|
|
//TODO: Enumerate through other properties of the anchor tag
|
|
|
|
// and set those as well.
|
|
|
|
// Optimization: Modify SetTextProperty to set all attributes at once?
|
|
|
|
}
|
|
|
|
(void)nsEditor::EndTransaction();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1999-05-05 08:05:19 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::IsRootTag(nsString &aTag, PRBool &aIsTag)
|
|
|
|
{
|
|
|
|
static nsAutoString bodyTag = "body";
|
|
|
|
static nsAutoString tdTag = "td";
|
|
|
|
static nsAutoString thTag = "th";
|
|
|
|
static nsAutoString captionTag = "caption";
|
|
|
|
if (PR_TRUE==aTag.EqualsIgnoreCase(bodyTag) ||
|
|
|
|
PR_TRUE==aTag.EqualsIgnoreCase(tdTag) ||
|
|
|
|
PR_TRUE==aTag.EqualsIgnoreCase(thTag) ||
|
|
|
|
PR_TRUE==aTag.EqualsIgnoreCase(captionTag) )
|
|
|
|
{
|
|
|
|
aIsTag = PR_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aIsTag = PR_FALSE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::IsSubordinateBlock(nsString &aTag, PRBool &aIsTag)
|
|
|
|
{
|
|
|
|
static nsAutoString p = "p";
|
|
|
|
static nsAutoString h1 = "h1";
|
|
|
|
static nsAutoString h2 = "h2";
|
|
|
|
static nsAutoString h3 = "h3";
|
|
|
|
static nsAutoString h4 = "h4";
|
|
|
|
static nsAutoString h5 = "h5";
|
|
|
|
static nsAutoString h6 = "h6";
|
|
|
|
static nsAutoString address = "address";
|
|
|
|
static nsAutoString pre = "pre";
|
|
|
|
static nsAutoString li = "li";
|
|
|
|
static nsAutoString dt = "dt";
|
|
|
|
static nsAutoString dd = "dd";
|
|
|
|
if (PR_TRUE==aTag.EqualsIgnoreCase(p) ||
|
|
|
|
PR_TRUE==aTag.EqualsIgnoreCase(h1) ||
|
|
|
|
PR_TRUE==aTag.EqualsIgnoreCase(h2) ||
|
|
|
|
PR_TRUE==aTag.EqualsIgnoreCase(h3) ||
|
|
|
|
PR_TRUE==aTag.EqualsIgnoreCase(h4) ||
|
|
|
|
PR_TRUE==aTag.EqualsIgnoreCase(h5) ||
|
|
|
|
PR_TRUE==aTag.EqualsIgnoreCase(h6) ||
|
|
|
|
PR_TRUE==aTag.EqualsIgnoreCase(address) ||
|
|
|
|
PR_TRUE==aTag.EqualsIgnoreCase(pre) ||
|
|
|
|
PR_TRUE==aTag.EqualsIgnoreCase(li) ||
|
|
|
|
PR_TRUE==aTag.EqualsIgnoreCase(dt) ||
|
|
|
|
PR_TRUE==aTag.EqualsIgnoreCase(dd) )
|
|
|
|
{
|
|
|
|
aIsTag = PR_TRUE;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
aIsTag = PR_FALSE;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-05-04 02:57:48 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::BeginComposition(void)
|
|
|
|
{
|
|
|
|
return nsTextEditor::BeginComposition();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::EndComposition(void)
|
|
|
|
{
|
|
|
|
return nsTextEditor::EndComposition();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::SetCompositionString(const nsString& aCompositionString)
|
|
|
|
{
|
|
|
|
return nsTextEditor::SetCompositionString(aCompositionString);
|
1999-05-05 08:05:19 +04:00
|
|
|
}
|