2001-09-25 22:53:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1999-03-01 19:54:47 +00:00
|
|
|
*
|
2001-09-25 22:53:13 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
1999-03-01 19:54:47 +00:00
|
|
|
*
|
2001-09-25 22:53:13 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-03-01 19:54:47 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-25 22:53:13 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:43:54 +00:00
|
|
|
*
|
2001-09-25 22:53:13 +00:00
|
|
|
* Contributor(s):
|
2000-02-01 14:26:27 +00:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2002-01-09 13:51:37 +00:00
|
|
|
* Daniel Glazman <glazman@netscape.com>
|
2001-09-25 22:53:13 +00:00
|
|
|
*
|
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-09-17 23:15:12 +00:00
|
|
|
#include "nsICaret.h"
|
2003-03-15 01:04:32 +00:00
|
|
|
#include "nsCRT.h"
|
1999-03-01 19:54:47 +00:00
|
|
|
|
2001-09-29 08:28:41 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2001-10-13 00:16:32 +00:00
|
|
|
#include "nsUnicharUtils.h"
|
1999-08-09 01:37:50 +00:00
|
|
|
|
1999-03-01 19:54:47 +00:00
|
|
|
#include "nsHTMLEditor.h"
|
1999-03-29 08:02:05 +00:00
|
|
|
#include "nsHTMLEditRules.h"
|
2001-04-05 23:48:01 +00:00
|
|
|
#include "nsTextEditUtils.h"
|
2000-01-26 00:57:37 +00:00
|
|
|
#include "nsHTMLEditUtils.h"
|
1999-08-09 01:37:50 +00:00
|
|
|
|
1999-03-01 19:54:47 +00:00
|
|
|
#include "nsEditorEventListeners.h"
|
2002-03-17 20:39:16 +00:00
|
|
|
#include "nsHTMLEditorMouseListener.h"
|
2001-04-05 23:48:01 +00:00
|
|
|
#include "TypeInState.h"
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2001-08-07 21:39:07 +00:00
|
|
|
#include "nsHTMLURIRefObject.h"
|
|
|
|
|
1999-07-04 23:01:10 +00:00
|
|
|
#include "nsIDOMText.h"
|
2002-05-14 00:05:37 +00:00
|
|
|
#include "nsITextContent.h"
|
1999-04-08 00:46:10 +00:00
|
|
|
#include "nsIDOMNodeList.h"
|
1999-03-01 19:54:47 +00:00
|
|
|
#include "nsIDOMDocument.h"
|
2000-03-24 00:26:47 +00:00
|
|
|
#include "nsIDOMAttr.h"
|
1999-06-08 00:02:25 +00:00
|
|
|
#include "nsIDocument.h"
|
1999-03-01 19:54:47 +00:00
|
|
|
#include "nsIDOMEventReceiver.h"
|
2002-11-09 00:25:25 +00:00
|
|
|
#include "nsIDOM3EventTarget.h"
|
1999-11-03 07:11:45 +00:00
|
|
|
#include "nsIDOMKeyEvent.h"
|
1999-03-01 19:54:47 +00:00
|
|
|
#include "nsIDOMKeyListener.h"
|
|
|
|
#include "nsIDOMMouseListener.h"
|
2000-08-04 20:51:33 +00:00
|
|
|
#include "nsIDOMMouseEvent.h"
|
2000-09-14 11:45:01 +00:00
|
|
|
#include "nsISelection.h"
|
|
|
|
#include "nsISelectionPrivate.h"
|
1999-03-29 22:01:26 +00:00
|
|
|
#include "nsIDOMHTMLAnchorElement.h"
|
1999-12-14 23:07:12 +00:00
|
|
|
#include "nsISelectionController.h"
|
2002-01-09 13:51:37 +00:00
|
|
|
#include "nsIDOMHTMLHtmlElement.h"
|
2002-11-09 00:25:25 +00:00
|
|
|
#include "nsGUIEvent.h"
|
|
|
|
#include "nsIDOMEventGroup.h"
|
2000-05-13 08:04:29 +00:00
|
|
|
|
2001-04-05 23:48:01 +00:00
|
|
|
#include "TransactionFactory.h"
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
#include "nsICSSLoader.h"
|
|
|
|
#include "nsICSSStyleSheet.h"
|
|
|
|
#include "nsIHTMLContentContainer.h"
|
|
|
|
#include "nsIDocumentObserver.h"
|
|
|
|
#include "nsIDocumentStateListener.h"
|
|
|
|
|
1999-04-20 17:47:12 +00:00
|
|
|
#include "nsIEnumerator.h"
|
1999-04-21 18:53:55 +00:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIContentIterator.h"
|
|
|
|
#include "nsIDOMRange.h"
|
1999-06-10 21:31:42 +00:00
|
|
|
#include "nsIDOMNSRange.h"
|
1999-04-30 22:40:18 +00:00
|
|
|
#include "nsISupportsArray.h"
|
|
|
|
#include "nsVoidArray.h"
|
2000-05-12 14:57:03 +00:00
|
|
|
#include "nsIURL.h"
|
1999-03-09 09:44:27 +00:00
|
|
|
#include "nsIComponentManager.h"
|
1999-03-01 19:54:47 +00:00
|
|
|
#include "nsIServiceManager.h"
|
1999-06-07 19:32:36 +00:00
|
|
|
#include "nsIDocumentEncoder.h"
|
1999-06-10 21:31:42 +00:00
|
|
|
#include "nsIDOMDocumentFragment.h"
|
1999-06-10 00:35:02 +00:00
|
|
|
#include "nsIPresShell.h"
|
2000-05-15 05:18:45 +00:00
|
|
|
#include "nsIPresContext.h"
|
2001-04-06 03:18:41 +00:00
|
|
|
#include "nsParserCIID.h"
|
1999-07-19 19:37:08 +00:00
|
|
|
#include "nsIImage.h"
|
1999-08-09 01:37:50 +00:00
|
|
|
#include "nsAOLCiter.h"
|
|
|
|
#include "nsInternetCiter.h"
|
2002-09-03 23:36:13 +00:00
|
|
|
#include "nsXPCOM.h"
|
1999-08-25 08:35:06 +00:00
|
|
|
#include "nsISupportsPrimitives.h"
|
2001-02-26 22:02:58 +00:00
|
|
|
#include "SetDocTitleTxn.h"
|
2001-07-16 02:40:48 +00:00
|
|
|
#include "nsGUIEvent.h"
|
2002-05-14 00:05:37 +00:00
|
|
|
#include "nsTextFragment.h"
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
// netwerk
|
|
|
|
#include "nsIURI.h"
|
1999-11-30 04:50:42 +00:00
|
|
|
#include "nsNetUtil.h"
|
1999-07-19 19:37:08 +00:00
|
|
|
|
|
|
|
// Drag & Drop, Clipboard
|
|
|
|
#include "nsIClipboard.h"
|
|
|
|
#include "nsITransferable.h"
|
2000-04-25 14:15:04 +00:00
|
|
|
#include "nsIDragService.h"
|
2000-06-08 14:47:29 +00:00
|
|
|
#include "nsIDOMNSUIEvent.h"
|
2003-04-15 14:07:30 +00:00
|
|
|
#include "nsIContentFilter.h"
|
1999-07-19 19:37:08 +00:00
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
// Transactionas
|
|
|
|
#include "nsStyleSheetTxns.h"
|
|
|
|
|
|
|
|
// Misc
|
|
|
|
#include "TextEditorTest.h"
|
|
|
|
#include "nsEditorUtils.h"
|
2003-01-09 06:49:07 +00:00
|
|
|
#include "nsIStyleSet.h"
|
1999-08-28 02:40:18 +00:00
|
|
|
#include "nsIPref.h"
|
2002-04-14 02:37:23 +00:00
|
|
|
#include "nsITextContent.h"
|
|
|
|
#include "nsWSRunObject.h"
|
2003-02-24 09:37:18 +00:00
|
|
|
#include "nsHTMLObjectResizer.h"
|
|
|
|
|
|
|
|
#include "nsIFrame.h"
|
|
|
|
#include "nsIView.h"
|
|
|
|
#include "nsIWidget.h"
|
2000-05-13 08:04:29 +00:00
|
|
|
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
static NS_DEFINE_CID(kCTransitionalDTDCID, NS_CTRANSITIONAL_DTD_CID);
|
1999-03-01 19:54:47 +00:00
|
|
|
|
1999-08-27 04:12:47 +00:00
|
|
|
// Some utilities to handle annoying overloading of "A" tag for link and named anchor
|
1999-07-27 23:59:22 +00:00
|
|
|
static char hrefText[] = "href";
|
1999-08-20 22:39:48 +00:00
|
|
|
static char anchorTxt[] = "anchor";
|
1999-07-27 23:59:22 +00:00
|
|
|
static char namedanchorText[] = "namedanchor";
|
1999-09-29 20:08:15 +00:00
|
|
|
|
2002-06-11 20:43:49 +00:00
|
|
|
nsCOMPtr<nsIParserService> nsHTMLEditor::sParserService;
|
|
|
|
PRInt32 nsHTMLEditor::sInstanceCount = 0;
|
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
// some prototypes for rules creation shortcuts
|
|
|
|
nsresult NS_NewTextEditRules(nsIEditRules** aInstancePtrResult);
|
|
|
|
nsresult NS_NewHTMLEditRules(nsIEditRules** aInstancePtrResult);
|
1999-06-10 00:35:02 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
#define IsLinkTag(s) (s.EqualsIgnoreCase(hrefText))
|
|
|
|
#define IsNamedAnchorTag(s) (s.EqualsIgnoreCase(anchorTxt) || s.EqualsIgnoreCase(namedanchorText))
|
1999-08-09 01:37:50 +00:00
|
|
|
|
1999-03-01 19:54:47 +00:00
|
|
|
nsHTMLEditor::nsHTMLEditor()
|
2001-01-28 20:13:07 +00:00
|
|
|
: nsPlaintextEditor()
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
, mIgnoreSpuriousDragEvent(PR_FALSE)
|
1999-08-09 01:37:50 +00:00
|
|
|
, mTypeInState(nsnull)
|
2000-03-21 06:05:24 +00:00
|
|
|
, mSelectedCellIndex(0)
|
2002-01-09 13:51:37 +00:00
|
|
|
, mHTMLCSSUtils(nsnull)
|
2003-06-25 08:50:48 +00:00
|
|
|
, mIsObjectResizingEnabled(PR_TRUE)
|
2003-02-24 09:37:18 +00:00
|
|
|
, mIsResizing(PR_FALSE)
|
2003-06-25 08:50:48 +00:00
|
|
|
, mIsAbsolutelyPositioningEnabled(PR_TRUE)
|
2003-09-20 05:59:44 +00:00
|
|
|
, mGrabberClicked(PR_FALSE)
|
2003-06-25 08:50:48 +00:00
|
|
|
, mIsMoving(PR_FALSE)
|
|
|
|
, mResizedObjectIsAbsolutelyPositioned(PR_FALSE)
|
|
|
|
, mIsInlineTableEditingEnabled(PR_TRUE)
|
|
|
|
, mSnapToGridEnabled(PR_FALSE)
|
|
|
|
, mGridSize(0)
|
1999-03-01 19:54:47 +00:00
|
|
|
{
|
2003-07-24 04:53:13 +00:00
|
|
|
mBoldAtom = do_GetAtom("b");
|
|
|
|
mItalicAtom = do_GetAtom("i");
|
|
|
|
mUnderlineAtom = do_GetAtom("u");
|
|
|
|
mFontAtom = do_GetAtom("font");
|
|
|
|
mLinkAtom = do_GetAtom("a");
|
2002-06-11 20:43:49 +00:00
|
|
|
++sInstanceCount;
|
1999-06-10 00:35:02 +00:00
|
|
|
}
|
1999-03-01 19:54:47 +00:00
|
|
|
|
|
|
|
nsHTMLEditor::~nsHTMLEditor()
|
|
|
|
{
|
2000-03-24 00:26:47 +00:00
|
|
|
// remove the rules as an action listener. Else we get a bad ownership loop later on.
|
|
|
|
// it's ok if the rules aren't a listener; we ignore the error.
|
|
|
|
nsCOMPtr<nsIEditActionListener> mListener = do_QueryInterface(mRules);
|
|
|
|
RemoveEditActionListener(mListener);
|
|
|
|
|
1999-03-01 19:54:47 +00:00
|
|
|
//the autopointers will clear themselves up.
|
1999-08-09 01:37:50 +00:00
|
|
|
//but we need to also remove the listeners or we have a leak
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelection>selection;
|
1999-08-19 13:30:48 +00:00
|
|
|
nsresult result = GetSelection(getter_AddRefs(selection));
|
1999-09-29 20:08:15 +00:00
|
|
|
// if we don't get the selection, just skip this
|
1999-08-09 01:37:50 +00:00
|
|
|
if (NS_SUCCEEDED(result) && selection)
|
|
|
|
{
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
|
|
|
|
nsCOMPtr<nsISelectionListener>listener;
|
1999-08-09 01:37:50 +00:00
|
|
|
listener = do_QueryInterface(mTypeInState);
|
|
|
|
if (listener) {
|
2000-09-14 11:45:01 +00:00
|
|
|
selPriv->RemoveSelectionListener(listener);
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
2003-02-24 09:37:18 +00:00
|
|
|
listener = do_QueryInterface(mSelectionListenerP);
|
|
|
|
if (listener) {
|
|
|
|
selPriv->RemoveSelectionListener(listener);
|
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
1999-03-05 21:05:35 +00:00
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
NS_IF_RELEASE(mTypeInState);
|
2003-02-24 09:37:18 +00:00
|
|
|
mSelectionListenerP = nsnull;
|
2002-06-11 20:43:49 +00:00
|
|
|
|
|
|
|
if (--sInstanceCount == 0 && sParserService)
|
|
|
|
sParserService = 0;
|
2002-08-07 00:02:10 +00:00
|
|
|
|
|
|
|
if (mHTMLCSSUtils)
|
|
|
|
delete mHTMLCSSUtils;
|
2003-06-02 22:21:06 +00:00
|
|
|
|
|
|
|
// free any default style propItems
|
|
|
|
RemoveAllDefaultProperties();
|
1999-03-05 21:05:35 +00:00
|
|
|
}
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(nsHTMLEditor, nsEditor)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsHTMLEditor, nsEditor)
|
|
|
|
|
2003-06-25 08:50:48 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsHTMLEditor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIHTMLEditor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIHTMLObjectResizer)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIHTMLAbsPosEditor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIHTMLInlineTableEditor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsITableEditor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIEditorStyleSheets)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsICSSLoaderObserver)
|
2003-06-25 19:21:59 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsPlaintextEditor)
|
2003-06-25 08:50:48 +00:00
|
|
|
|
1999-03-05 21:05:35 +00:00
|
|
|
|
Preparation for ender-based text control
* added focus listener. Doesn't do much yet, but when focus notifications start appearing, we'll be ready for them. The code is in
place to hide selection when we lose focus and paint selection when we get focus. That's probably not quite right, but it's a start.
We will need to be able to determine the distinction between losing focus to another control within our app, and losing focus to
another app.
* added support for disabled and readonly states in the editor. This is accomplished by having flags set by the client, and letting the
rules system deal with those flags. The flags I added are:
TEXT_EDITOR_FLAG_PLAINTEXT 0x01 // only plain text editing is allowed
TEXT_EDITOR_FLAG_SINGLELINE 0x02 // enter key and CR-LF handled specially
TEXT_EDITOR_FLAG_PASSWORD 0x04 // text is not entered into content, only a representative character
TEXT_EDITOR_FLAG_READONLY 0x08 // editing events are disabled. Editor may still accept focus.
TEXT_EDITOR_FLAG_DISALBED 0x10 // all events are disabled (like scrolling). Editor will not accept focus.
* added WillInsertBreak/DidInsertBreak into text rules, so flags could be checked. This gets us readonly, disabled, and single line
behavior.
* cleaned up the code that allocates, registers, and destroys event listeners. Thanks to Kin and Simon for cleaning up the
ownership model on the listeners, it was a big help.
* added support for a max text length. You can now tell the text editor, be no bigger than n characters.
1999-05-28 21:24:18 +00:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::Init(nsIDOMDocument *aDoc,
|
2000-05-04 08:33:48 +00:00
|
|
|
nsIPresShell *aPresShell, nsIContent *aRoot, nsISelectionController *aSelCon, PRUint32 aFlags)
|
1999-03-01 19:54:47 +00:00
|
|
|
{
|
1999-08-09 01:37:50 +00:00
|
|
|
NS_PRECONDITION(aDoc && aPresShell, "bad arg");
|
|
|
|
if (!aDoc || !aPresShell)
|
1999-09-29 20:08:15 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult result = NS_OK, rulesRes = NS_OK;
|
2001-04-05 23:48:01 +00:00
|
|
|
|
2001-05-11 12:43:22 +00:00
|
|
|
// make a range util object for comparing dom points
|
2003-07-08 20:22:56 +00:00
|
|
|
mRangeHelper = do_CreateInstance("@mozilla.org/content/range-utils;1");
|
2001-05-11 12:43:22 +00:00
|
|
|
if (!mRangeHelper) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (1)
|
|
|
|
{
|
|
|
|
// block to scope nsAutoEditInitRulesTrigger
|
|
|
|
nsAutoEditInitRulesTrigger rulesTrigger(NS_STATIC_CAST(nsPlaintextEditor*,this), rulesRes);
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2002-10-08 22:53:55 +00:00
|
|
|
// Set up a DTD
|
|
|
|
mDTD = do_CreateInstance(kCTransitionalDTDCID);
|
|
|
|
if (!mDTD) result = NS_ERROR_FAILURE;
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Init the plaintext editor
|
|
|
|
result = nsPlaintextEditor::Init(aDoc, aPresShell, aRoot, aSelCon, aFlags);
|
|
|
|
if (NS_FAILED(result)) { return result; }
|
2000-05-15 05:18:45 +00:00
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
// the HTML Editor is CSS-aware only in the case of Composer
|
2003-06-13 12:39:35 +00:00
|
|
|
mCSSAware = (0 == aFlags);
|
2002-01-09 13:51:37 +00:00
|
|
|
|
2003-06-25 08:50:48 +00:00
|
|
|
// disable Composer-only features
|
|
|
|
if (aFlags & eEditorMailMask)
|
|
|
|
{
|
|
|
|
SetAbsolutePositioningEnabled(PR_FALSE);
|
|
|
|
SetSnapToGridEnabled(PR_FALSE);
|
|
|
|
}
|
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
// Init the HTML-CSS utils
|
2002-08-07 00:02:10 +00:00
|
|
|
if (mHTMLCSSUtils)
|
|
|
|
delete mHTMLCSSUtils;
|
2002-01-09 13:51:37 +00:00
|
|
|
result = NS_NewHTMLCSSUtils(&mHTMLCSSUtils);
|
|
|
|
if (NS_FAILED(result)) { return result; }
|
|
|
|
mHTMLCSSUtils->Init(this);
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// disable links
|
|
|
|
nsCOMPtr<nsIPresContext> context;
|
|
|
|
aPresShell->GetPresContext(getter_AddRefs(context));
|
|
|
|
if (!context) return NS_ERROR_NULL_POINTER;
|
|
|
|
if (!(mFlags & eEditorPlaintextMask))
|
|
|
|
context->SetLinkHandler(0);
|
1999-11-11 19:22:30 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> bodyElement;
|
|
|
|
result = nsEditor::GetRootElement(getter_AddRefs(bodyElement));
|
|
|
|
if (NS_FAILED(result)) { return result; }
|
|
|
|
if (!bodyElement) { return NS_ERROR_NULL_POINTER; }
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// init the type-in state
|
|
|
|
mTypeInState = new TypeInState();
|
|
|
|
if (!mTypeInState) {return NS_ERROR_NULL_POINTER;}
|
|
|
|
NS_ADDREF(mTypeInState);
|
|
|
|
|
2003-02-24 09:37:18 +00:00
|
|
|
// init the selection listener for image resizing
|
|
|
|
mSelectionListenerP = new ResizerSelectionListener(this);
|
|
|
|
if (!mSelectionListenerP) {return NS_ERROR_NULL_POINTER;}
|
|
|
|
|
2003-04-03 08:05:23 +00:00
|
|
|
// ignore any errors from this in case the file is missing
|
|
|
|
AddOverrideStyleSheet(NS_LITERAL_STRING("resource:/res/EditorOverride.css"));
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
result = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(result)) { return result; }
|
|
|
|
if (selection)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
|
|
|
|
nsCOMPtr<nsISelectionListener>listener;
|
|
|
|
listener = do_QueryInterface(mTypeInState);
|
|
|
|
if (listener) {
|
|
|
|
selPriv->AddSelectionListener(listener);
|
|
|
|
}
|
2003-02-24 09:37:18 +00:00
|
|
|
listener = do_QueryInterface(mSelectionListenerP);
|
|
|
|
if (listener) {
|
|
|
|
selPriv->AddSelectionListener(listener);
|
|
|
|
}
|
1999-07-19 22:49:21 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
1999-11-29 08:28:46 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(rulesRes)) return rulesRes;
|
1999-08-23 06:42:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::PostCreate()
|
|
|
|
{
|
|
|
|
nsresult result = InstallEventListeners();
|
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
|
|
|
|
result = nsEditor::PostCreate();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::InstallEventListeners()
|
|
|
|
{
|
1999-08-25 10:51:55 +00:00
|
|
|
NS_ASSERTION(mDocWeak, "no document set on this editor");
|
|
|
|
if (!mDocWeak) return NS_ERROR_NOT_INITIALIZED;
|
1999-08-23 06:42:04 +00:00
|
|
|
|
2003-02-27 23:09:51 +00:00
|
|
|
if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
1999-08-23 06:42:04 +00:00
|
|
|
nsresult result;
|
1999-08-09 01:37:50 +00:00
|
|
|
// get a key listener
|
|
|
|
result = NS_NewEditorKeyListener(getter_AddRefs(mKeyListenerP), this);
|
1999-08-19 13:30:48 +00:00
|
|
|
if (NS_FAILED(result)) {
|
1999-08-09 01:37:50 +00:00
|
|
|
HandleEventListenerError();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a mouse listener
|
2002-03-17 20:39:16 +00:00
|
|
|
result = NS_NewHTMLEditorMouseListener(getter_AddRefs(mMouseListenerP), this);
|
1999-08-19 13:30:48 +00:00
|
|
|
if (NS_FAILED(result)) {
|
1999-08-09 01:37:50 +00:00
|
|
|
HandleEventListenerError();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get a text listener
|
|
|
|
result = NS_NewEditorTextListener(getter_AddRefs(mTextListenerP),this);
|
1999-08-19 13:30:48 +00:00
|
|
|
if (NS_FAILED(result)) {
|
1999-08-09 01:37:50 +00:00
|
|
|
#ifdef DEBUG_TAGUE
|
2000-10-28 22:17:53 +00:00
|
|
|
printf("nsTextEditor.cpp: failed to get TextEvent Listener\n");
|
1999-08-09 01:37:50 +00:00
|
|
|
#endif
|
1999-09-29 20:08:15 +00:00
|
|
|
HandleEventListenerError();
|
|
|
|
return result;
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get a composition listener
|
|
|
|
result = NS_NewEditorCompositionListener(getter_AddRefs(mCompositionListenerP),this);
|
1999-08-19 13:30:48 +00:00
|
|
|
if (NS_FAILED(result)) {
|
1999-08-09 01:37:50 +00:00
|
|
|
#ifdef DEBUG_TAGUE
|
2000-10-28 22:17:53 +00:00
|
|
|
printf("nsTextEditor.cpp: failed to get TextEvent Listener\n");
|
1999-08-09 01:37:50 +00:00
|
|
|
#endif
|
1999-09-29 20:08:15 +00:00
|
|
|
HandleEventListenerError();
|
|
|
|
return result;
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get a drag listener
|
2003-02-27 23:09:51 +00:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShellWeak);
|
|
|
|
result = NS_NewEditorDragListener(getter_AddRefs(mDragListenerP), presShell, this);
|
1999-08-19 13:30:48 +00:00
|
|
|
if (NS_FAILED(result)) {
|
1999-08-09 01:37:50 +00:00
|
|
|
HandleEventListenerError();
|
1999-08-19 13:30:48 +00:00
|
|
|
return result;
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// get a focus listener
|
|
|
|
result = NS_NewEditorFocusListener(getter_AddRefs(mFocusListenerP), this);
|
1999-08-19 13:30:48 +00:00
|
|
|
if (NS_FAILED(result)) {
|
1999-08-09 01:37:50 +00:00
|
|
|
HandleEventListenerError();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEventReceiver> erP;
|
2000-06-01 02:38:13 +00:00
|
|
|
result = GetDOMEventReceiver(getter_AddRefs(erP));
|
2000-05-04 08:33:48 +00:00
|
|
|
|
|
|
|
//end hack
|
1999-08-19 13:30:48 +00:00
|
|
|
if (NS_FAILED(result)) {
|
1999-08-09 01:37:50 +00:00
|
|
|
HandleEventListenerError();
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
// register the event listeners with the DOM event reveiver
|
2002-11-09 00:25:25 +00:00
|
|
|
nsCOMPtr<nsIDOM3EventTarget> dom3Targ(do_QueryInterface(erP));
|
|
|
|
nsCOMPtr<nsIDOMEventGroup> sysGroup;
|
|
|
|
if (NS_SUCCEEDED(erP->GetSystemEventGroup(getter_AddRefs(sysGroup)))) {
|
|
|
|
result = dom3Targ->AddGroupedEventListener(NS_LITERAL_STRING("keypress"), mKeyListenerP, PR_FALSE, sysGroup);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(result), "failed to register key listener in system group");
|
|
|
|
}
|
|
|
|
|
1999-09-29 20:08:15 +00:00
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
2000-02-01 14:26:27 +00:00
|
|
|
result = erP->AddEventListenerByIID(mMouseListenerP, NS_GET_IID(nsIDOMMouseListener));
|
1999-09-29 20:08:15 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(result), "failed to register mouse listener");
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
2000-02-01 14:26:27 +00:00
|
|
|
result = erP->AddEventListenerByIID(mFocusListenerP, NS_GET_IID(nsIDOMFocusListener));
|
1999-09-29 20:08:15 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(result), "failed to register focus listener");
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
2000-02-01 14:26:27 +00:00
|
|
|
result = erP->AddEventListenerByIID(mTextListenerP, NS_GET_IID(nsIDOMTextListener));
|
1999-09-29 20:08:15 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(result), "failed to register text listener");
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
2000-02-01 14:26:27 +00:00
|
|
|
result = erP->AddEventListenerByIID(mCompositionListenerP, NS_GET_IID(nsIDOMCompositionListener));
|
1999-09-29 20:08:15 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(result), "failed to register composition listener");
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
2000-02-01 14:26:27 +00:00
|
|
|
result = erP->AddEventListenerByIID(mDragListenerP, NS_GET_IID(nsIDOMDragListener));
|
1999-09-29 20:08:15 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(result), "failed to register drag listener");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
1999-08-19 13:30:48 +00:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
HandleEventListenerError();
|
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetFlags(PRUint32 *aFlags)
|
|
|
|
{
|
|
|
|
if (!mRules || !aFlags) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
return mRules->GetFlags(aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::SetFlags(PRUint32 aFlags)
|
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NULL_POINTER; }
|
2003-09-05 17:49:36 +00:00
|
|
|
mCSSAware = ((aFlags & (eEditorNoCSSMask | eEditorMailMask)) == 0);
|
2002-02-05 15:16:14 +00:00
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
return mRules->SetFlags(aFlags);
|
1999-03-01 19:54:47 +00:00
|
|
|
}
|
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::InitRules()
|
1999-03-29 08:02:05 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// instantiate the rules for the html editor
|
|
|
|
nsresult res = NS_NewHTMLEditRules(getter_AddRefs(mRules));
|
2000-03-24 00:26:47 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!mRules) return NS_ERROR_UNEXPECTED;
|
2001-01-28 20:13:07 +00:00
|
|
|
res = mRules->Init(NS_STATIC_CAST(nsPlaintextEditor*,this), mFlags);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
|
|
|
return res;
|
1999-03-29 08:02:05 +00:00
|
|
|
}
|
|
|
|
|
2002-10-08 22:53:55 +00:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::BeginningOfDocument()
|
|
|
|
{
|
|
|
|
if (!mDocWeak || !mPresShellWeak) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
|
|
|
// get the selection
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res))
|
|
|
|
return res;
|
|
|
|
if (!selection)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
// get the root element
|
|
|
|
nsCOMPtr<nsIDOMElement> rootElement;
|
|
|
|
res = GetRootElement(getter_AddRefs(rootElement));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!rootElement) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// find first editable thingy
|
|
|
|
PRBool done = PR_FALSE;
|
|
|
|
nsCOMPtr<nsIDOMNode> curNode(rootElement), selNode;
|
|
|
|
PRInt32 curOffset = 0, selOffset;
|
|
|
|
while (!done)
|
|
|
|
{
|
|
|
|
nsWSRunObject wsObj(this, curNode, curOffset);
|
|
|
|
nsCOMPtr<nsIDOMNode> visNode;
|
|
|
|
PRInt32 visOffset=0;
|
|
|
|
PRInt16 visType=0;
|
|
|
|
wsObj.NextVisibleNode(curNode, curOffset, address_of(visNode), &visOffset, &visType);
|
|
|
|
if ((visType==nsWSRunObject::eNormalWS) ||
|
2002-10-17 03:16:34 +00:00
|
|
|
(visType==nsWSRunObject::eText))
|
2002-10-08 22:53:55 +00:00
|
|
|
{
|
|
|
|
selNode = visNode;
|
|
|
|
selOffset = visOffset;
|
|
|
|
done = PR_TRUE;
|
2002-10-17 03:16:34 +00:00
|
|
|
}
|
|
|
|
else if ((visType==nsWSRunObject::eBreak) ||
|
|
|
|
(visType==nsWSRunObject::eSpecial))
|
|
|
|
{
|
|
|
|
res = GetNodeLocation(visNode, address_of(selNode), &selOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
done = PR_TRUE;
|
2002-10-08 22:53:55 +00:00
|
|
|
}
|
|
|
|
else if (visType==nsWSRunObject::eOtherBlock)
|
|
|
|
{
|
2003-04-16 20:18:41 +00:00
|
|
|
// By definition of nsWSRunObject, a block element terminates
|
|
|
|
// a whitespace run. That is, although we are calling a method
|
|
|
|
// that is named "NextVisibleNode", the node returned
|
|
|
|
// might not be visible/editable!
|
|
|
|
// If the given block does not contain any visible/editable items,
|
|
|
|
// we want to skip it and continue our search.
|
|
|
|
|
|
|
|
if (!IsContainer(visNode))
|
|
|
|
{
|
|
|
|
// However, we were given a block that is not a container.
|
|
|
|
// Since the block can not contain anything that's visible,
|
|
|
|
// such a block only makes sense if it is visible by itself,
|
|
|
|
// like a <hr>
|
|
|
|
// We want to place the caret in front of that block.
|
|
|
|
|
|
|
|
res = GetNodeLocation(visNode, address_of(selNode), &selOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
done = PR_TRUE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PRBool isEmptyBlock;
|
|
|
|
if (NS_SUCCEEDED(IsEmptyNode(visNode, &isEmptyBlock)) &&
|
|
|
|
isEmptyBlock)
|
|
|
|
{
|
|
|
|
// skip the empty block
|
|
|
|
res = GetNodeLocation(visNode, address_of(curNode), &curOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
++curOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
curNode = visNode;
|
|
|
|
curOffset = 0;
|
|
|
|
}
|
|
|
|
// keep looping
|
|
|
|
}
|
2002-10-08 22:53:55 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// else we found nothing useful
|
|
|
|
selNode = curNode;
|
|
|
|
selOffset = curOffset;
|
|
|
|
done = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return selection->Collapse(selNode, selOffset);
|
|
|
|
}
|
|
|
|
|
2001-04-05 23:48:01 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the id represents an element of block type.
|
|
|
|
* Can be used to determine if a new paragraph should be started.
|
|
|
|
*/
|
|
|
|
nsresult
|
2001-04-07 00:45:26 +00:00
|
|
|
nsHTMLEditor::NodeIsBlockStatic(nsIDOMNode *aNode, PRBool *aIsBlock)
|
2001-04-05 23:48:01 +00:00
|
|
|
{
|
2001-04-07 00:45:26 +00:00
|
|
|
if (!aNode || !aIsBlock) { return NS_ERROR_NULL_POINTER; }
|
2001-04-05 23:48:01 +00:00
|
|
|
|
2003-07-19 08:51:54 +00:00
|
|
|
*aIsBlock = PR_FALSE;
|
|
|
|
|
2002-06-11 20:43:49 +00:00
|
|
|
#define USE_PARSER_FOR_BLOCKNESS 1
|
2001-04-06 03:18:41 +00:00
|
|
|
#ifdef USE_PARSER_FOR_BLOCKNESS
|
2001-04-05 23:48:01 +00:00
|
|
|
nsresult rv;
|
|
|
|
|
2003-07-19 08:51:54 +00:00
|
|
|
nsCOMPtr<nsIDOMElement>element = do_QueryInterface(aNode);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (!element)
|
|
|
|
{
|
|
|
|
// We don't have an element -- probably a text node
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-07-19 08:51:54 +00:00
|
|
|
nsCOMPtr<nsIAtom> tagAtom = nsEditor::GetTag(aNode);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (!tagAtom) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if (!sParserService) {
|
2003-07-08 20:22:56 +00:00
|
|
|
sParserService = do_GetService("@mozilla.org/parser/parser-service;1", &rv);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Nodes we know we want to treat as block
|
|
|
|
// even though the parser says they're not:
|
2003-04-17 05:42:33 +00:00
|
|
|
if (tagAtom==nsEditProperty::body ||
|
|
|
|
tagAtom==nsEditProperty::head ||
|
|
|
|
tagAtom==nsEditProperty::tbody ||
|
|
|
|
tagAtom==nsEditProperty::thead ||
|
|
|
|
tagAtom==nsEditProperty::tfoot ||
|
|
|
|
tagAtom==nsEditProperty::tr ||
|
|
|
|
tagAtom==nsEditProperty::th ||
|
|
|
|
tagAtom==nsEditProperty::td ||
|
|
|
|
tagAtom==nsEditProperty::li ||
|
|
|
|
tagAtom==nsEditProperty::pre)
|
2001-04-05 23:48:01 +00:00
|
|
|
{
|
2001-04-07 00:45:26 +00:00
|
|
|
*aIsBlock = PR_TRUE;
|
2001-04-05 23:48:01 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRInt32 id;
|
2003-07-19 08:51:54 +00:00
|
|
|
rv = sParserService->HTMLAtomTagToId(tagAtom, &id);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-04-07 00:45:26 +00:00
|
|
|
rv = sParserService->IsBlock(id, *aIsBlock);
|
2001-04-05 23:48:01 +00:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Check this against what we would have said with the old code:
|
2003-04-17 05:42:33 +00:00
|
|
|
if (tagAtom==nsEditProperty::p ||
|
|
|
|
tagAtom==nsEditProperty::div ||
|
|
|
|
tagAtom==nsEditProperty::blockquote ||
|
|
|
|
tagAtom==nsEditProperty::h1 ||
|
|
|
|
tagAtom==nsEditProperty::h2 ||
|
|
|
|
tagAtom==nsEditProperty::h3 ||
|
|
|
|
tagAtom==nsEditProperty::h4 ||
|
|
|
|
tagAtom==nsEditProperty::h5 ||
|
|
|
|
tagAtom==nsEditProperty::h6 ||
|
|
|
|
tagAtom==nsEditProperty::ul ||
|
|
|
|
tagAtom==nsEditProperty::ol ||
|
|
|
|
tagAtom==nsEditProperty::dl ||
|
|
|
|
tagAtom==nsEditProperty::noscript ||
|
|
|
|
tagAtom==nsEditProperty::form ||
|
|
|
|
tagAtom==nsEditProperty::hr ||
|
|
|
|
tagAtom==nsEditProperty::table ||
|
|
|
|
tagAtom==nsEditProperty::fieldset ||
|
|
|
|
tagAtom==nsEditProperty::address ||
|
|
|
|
tagAtom==nsEditProperty::caption ||
|
|
|
|
tagAtom==nsEditProperty::col ||
|
|
|
|
tagAtom==nsEditProperty::colgroup ||
|
|
|
|
tagAtom==nsEditProperty::li ||
|
|
|
|
tagAtom==nsEditProperty::dt ||
|
|
|
|
tagAtom==nsEditProperty::dd ||
|
|
|
|
tagAtom==nsEditProperty::legend )
|
2001-04-05 23:48:01 +00:00
|
|
|
{
|
2001-04-07 00:45:26 +00:00
|
|
|
if (!(*aIsBlock))
|
2001-04-05 23:48:01 +00:00
|
|
|
{
|
|
|
|
nsAutoString assertmsg (NS_LITERAL_STRING("Parser and editor disagree on blockness: "));
|
2003-07-19 08:51:54 +00:00
|
|
|
|
|
|
|
nsAutoString tagName;
|
|
|
|
rv = element->GetTagName(tagName);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-04-05 23:48:01 +00:00
|
|
|
assertmsg.Append(tagName);
|
2001-09-29 08:28:41 +00:00
|
|
|
char* assertstr = ToNewCString(assertmsg);
|
2001-04-07 00:45:26 +00:00
|
|
|
NS_ASSERTION(*aIsBlock, assertstr);
|
2001-04-05 23:48:01 +00:00
|
|
|
Recycle(assertstr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
return rv;
|
2001-04-06 03:18:41 +00:00
|
|
|
#else /* USE_PARSER_FOR_BLOCKNESS */
|
|
|
|
nsresult result = NS_ERROR_FAILURE;
|
2001-04-07 00:45:26 +00:00
|
|
|
*aIsBlock = PR_FALSE;
|
2001-04-06 03:18:41 +00:00
|
|
|
nsCOMPtr<nsIDOMElement>element;
|
|
|
|
element = do_QueryInterface(aNode);
|
|
|
|
if (element)
|
|
|
|
{
|
|
|
|
nsAutoString tagName;
|
|
|
|
result = element->GetTagName(tagName);
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
2001-12-17 07:14:49 +00:00
|
|
|
ToLowerCase(tagName);
|
2003-07-24 04:53:13 +00:00
|
|
|
nsCOMPtr<nsIAtom> tagAtom = do_GetAtom(tagName);
|
2001-04-06 03:18:41 +00:00
|
|
|
if (!tagAtom) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
|
2003-04-17 05:42:33 +00:00
|
|
|
if (tagAtom==nsEditProperty::p ||
|
|
|
|
tagAtom==nsEditProperty::div ||
|
|
|
|
tagAtom==nsEditProperty::blockquote ||
|
|
|
|
tagAtom==nsEditProperty::h1 ||
|
|
|
|
tagAtom==nsEditProperty::h2 ||
|
|
|
|
tagAtom==nsEditProperty::h3 ||
|
|
|
|
tagAtom==nsEditProperty::h4 ||
|
|
|
|
tagAtom==nsEditProperty::h5 ||
|
|
|
|
tagAtom==nsEditProperty::h6 ||
|
|
|
|
tagAtom==nsEditProperty::ul ||
|
|
|
|
tagAtom==nsEditProperty::ol ||
|
|
|
|
tagAtom==nsEditProperty::dl ||
|
|
|
|
tagAtom==nsEditProperty::pre ||
|
|
|
|
tagAtom==nsEditProperty::noscript ||
|
|
|
|
tagAtom==nsEditProperty::form ||
|
|
|
|
tagAtom==nsEditProperty::hr ||
|
|
|
|
tagAtom==nsEditProperty::fieldset ||
|
|
|
|
tagAtom==nsEditProperty::address ||
|
|
|
|
tagAtom==nsEditProperty::body ||
|
|
|
|
tagAtom==nsEditProperty::caption ||
|
|
|
|
tagAtom==nsEditProperty::table ||
|
|
|
|
tagAtom==nsEditProperty::tbody ||
|
|
|
|
tagAtom==nsEditProperty::thead ||
|
|
|
|
tagAtom==nsEditProperty::tfoot ||
|
|
|
|
tagAtom==nsEditProperty::tr ||
|
|
|
|
tagAtom==nsEditProperty::td ||
|
|
|
|
tagAtom==nsEditProperty::th ||
|
|
|
|
tagAtom==nsEditProperty::col ||
|
|
|
|
tagAtom==nsEditProperty::colgroup ||
|
|
|
|
tagAtom==nsEditProperty::li ||
|
|
|
|
tagAtom==nsEditProperty::dt ||
|
|
|
|
tagAtom==nsEditProperty::dd ||
|
|
|
|
tagAtom==nsEditProperty::legend )
|
2001-04-06 03:18:41 +00:00
|
|
|
{
|
2001-04-07 00:45:26 +00:00
|
|
|
*aIsBlock = PR_TRUE;
|
2001-04-06 03:18:41 +00:00
|
|
|
}
|
|
|
|
result = NS_OK;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We don't have an element -- probably a text node
|
|
|
|
nsCOMPtr<nsIDOMCharacterData>nodeAsText = do_QueryInterface(aNode);
|
|
|
|
if (nodeAsText)
|
|
|
|
{
|
2001-04-07 00:45:26 +00:00
|
|
|
*aIsBlock = PR_FALSE;
|
2001-04-06 03:18:41 +00:00
|
|
|
result = NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
|
|
|
|
#endif /* USE_PARSER_FOR_BLOCKNESS */
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
|
2001-04-07 00:45:26 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::NodeIsBlock(nsIDOMNode *aNode, PRBool *aIsBlock)
|
2001-04-05 23:48:01 +00:00
|
|
|
{
|
|
|
|
return NodeIsBlockStatic(aNode, aIsBlock);
|
|
|
|
}
|
|
|
|
|
2001-04-17 10:15:05 +00:00
|
|
|
PRBool
|
|
|
|
nsHTMLEditor::IsBlockNode(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
PRBool isBlock;
|
|
|
|
NodeIsBlockStatic(aNode, &isBlock);
|
|
|
|
return isBlock;
|
|
|
|
}
|
|
|
|
|
2001-04-07 00:45:26 +00:00
|
|
|
// Non-static version for the nsIEditor interface and JavaScript
|
2001-02-26 22:02:58 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::SetDocumentTitle(const nsAString &aTitle)
|
2001-02-26 22:02:58 +00:00
|
|
|
{
|
|
|
|
SetDocTitleTxn *txn;
|
|
|
|
nsresult result = TransactionFactory::GetNewTransaction(SetDocTitleTxn::GetCID(), (EditTxn **)&txn);
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
2001-04-07 00:45:26 +00:00
|
|
|
result = txn->Init(this, &aTitle);
|
2001-02-26 22:02:58 +00:00
|
|
|
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
2001-04-12 03:41:06 +00:00
|
|
|
//Don't let Rules System change the selection
|
|
|
|
nsAutoTxnsConserveSelection dontChangeSelection(this);
|
|
|
|
|
2003-04-04 20:50:25 +00:00
|
|
|
result = nsEditor::DoTransaction(txn);
|
2001-02-26 22:02:58 +00:00
|
|
|
}
|
|
|
|
// The transaction system (if any) has taken ownwership of txn
|
|
|
|
NS_IF_RELEASE(txn);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2000-01-18 21:50:15 +00:00
|
|
|
|
2001-04-05 23:48:01 +00:00
|
|
|
/* ------------ Block methods moved from nsEditor -------------- */
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetBlockNodeParent: returns enclosing block level ancestor, if any
|
|
|
|
//
|
|
|
|
nsCOMPtr<nsIDOMNode>
|
|
|
|
nsHTMLEditor::GetBlockNodeParent(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
nsCOMPtr<nsIDOMNode> p;
|
|
|
|
|
|
|
|
if (!aNode)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("null node passed to GetBlockNodeParent()");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NS_FAILED(aNode->GetParentNode(getter_AddRefs(p)))) // no parent, ran off top of tree
|
|
|
|
return tmp;
|
|
|
|
|
|
|
|
while (p)
|
|
|
|
{
|
|
|
|
PRBool isBlock;
|
2001-04-07 00:45:26 +00:00
|
|
|
if (NS_FAILED(NodeIsBlockStatic(p, &isBlock)) || isBlock)
|
2001-04-05 23:48:01 +00:00
|
|
|
break;
|
|
|
|
if ( NS_FAILED(p->GetParentNode(getter_AddRefs(tmp))) || !tmp) // no parent, ran off top of tree
|
|
|
|
return p;
|
|
|
|
|
|
|
|
p = tmp;
|
|
|
|
}
|
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// HasSameBlockNodeParent: true if nodes have same block level ancestor
|
|
|
|
//
|
|
|
|
PRBool
|
|
|
|
nsHTMLEditor::HasSameBlockNodeParent(nsIDOMNode *aNode1, nsIDOMNode *aNode2)
|
|
|
|
{
|
|
|
|
if (!aNode1 || !aNode2)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("null node passed to HasSameBlockNodeParent()");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aNode1 == aNode2)
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> p1 = GetBlockNodeParent(aNode1);
|
|
|
|
nsCOMPtr<nsIDOMNode> p2 = GetBlockNodeParent(aNode2);
|
|
|
|
|
|
|
|
return (p1 == p2);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetBlockSection: return leftmost/rightmost nodes in aChild's block
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetBlockSection(nsIDOMNode *aChild,
|
|
|
|
nsIDOMNode **aLeftNode,
|
|
|
|
nsIDOMNode **aRightNode)
|
|
|
|
{
|
|
|
|
nsresult result = NS_OK;
|
|
|
|
if (!aChild || !aLeftNode || !aRightNode) {return NS_ERROR_NULL_POINTER;}
|
|
|
|
*aLeftNode = aChild;
|
|
|
|
*aRightNode = aChild;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode>sibling;
|
|
|
|
result = aChild->GetPreviousSibling(getter_AddRefs(sibling));
|
|
|
|
while ((NS_SUCCEEDED(result)) && sibling)
|
|
|
|
{
|
|
|
|
PRBool isBlock;
|
2001-04-07 00:45:26 +00:00
|
|
|
NodeIsBlockStatic(sibling, &isBlock);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (isBlock)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMCharacterData>nodeAsText = do_QueryInterface(sibling);
|
|
|
|
if (!nodeAsText) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
// XXX: needs some logic to work for other leaf nodes besides text!
|
|
|
|
}
|
|
|
|
*aLeftNode = sibling;
|
|
|
|
result = (*aLeftNode)->GetPreviousSibling(getter_AddRefs(sibling));
|
|
|
|
}
|
|
|
|
NS_ADDREF((*aLeftNode));
|
|
|
|
// now do the right side
|
|
|
|
result = aChild->GetNextSibling(getter_AddRefs(sibling));
|
|
|
|
while ((NS_SUCCEEDED(result)) && sibling)
|
|
|
|
{
|
|
|
|
PRBool isBlock;
|
2001-04-07 00:45:26 +00:00
|
|
|
NodeIsBlockStatic(sibling, &isBlock);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (isBlock)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMCharacterData>nodeAsText = do_QueryInterface(sibling);
|
|
|
|
if (!nodeAsText) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*aRightNode = sibling;
|
|
|
|
result = (*aRightNode)->GetNextSibling(getter_AddRefs(sibling));
|
|
|
|
}
|
|
|
|
NS_ADDREF((*aRightNode));
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetBlockSectionsForRange: return list of block sections that intersect
|
|
|
|
// this range
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetBlockSectionsForRange(nsIDOMRange *aRange,
|
2002-11-12 19:40:11 +00:00
|
|
|
nsCOMArray<nsIDOMRange>& aSections)
|
2001-04-05 23:48:01 +00:00
|
|
|
{
|
2002-11-12 19:40:11 +00:00
|
|
|
if (!aRange) {return NS_ERROR_NULL_POINTER;}
|
2001-04-05 23:48:01 +00:00
|
|
|
|
|
|
|
nsresult result;
|
2003-07-08 20:22:56 +00:00
|
|
|
nsCOMPtr<nsIContentIterator>iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &result);
|
2001-04-05 23:48:01 +00:00
|
|
|
if ((NS_SUCCEEDED(result)) && iter)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMRange> lastRange;
|
|
|
|
iter->Init(aRange);
|
|
|
|
nsCOMPtr<nsIContent> currentContent;
|
|
|
|
iter->CurrentNode(getter_AddRefs(currentContent));
|
|
|
|
while (NS_ENUMERATOR_FALSE == iter->IsDone())
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode>currentNode = do_QueryInterface(currentContent);
|
|
|
|
if (currentNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIAtom> currentContentTag;
|
2003-06-13 20:10:01 +00:00
|
|
|
currentContent->GetTag(getter_AddRefs(currentContentTag));
|
2001-04-05 23:48:01 +00:00
|
|
|
// <BR> divides block content ranges. We can achieve this by nulling out lastRange
|
2003-04-17 05:42:33 +00:00
|
|
|
if (nsEditProperty::br==currentContentTag)
|
2001-04-05 23:48:01 +00:00
|
|
|
{
|
2002-11-12 19:40:11 +00:00
|
|
|
lastRange = nsnull;
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
PRBool isNotInlineOrText;
|
2001-04-07 00:45:26 +00:00
|
|
|
result = NodeIsBlockStatic(currentNode, &isNotInlineOrText);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (isNotInlineOrText)
|
|
|
|
{
|
|
|
|
PRUint16 nodeType;
|
|
|
|
currentNode->GetNodeType(&nodeType);
|
|
|
|
if (nsIDOMNode::TEXT_NODE == nodeType) {
|
|
|
|
isNotInlineOrText = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (PR_FALSE==isNotInlineOrText)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode>leftNode;
|
|
|
|
nsCOMPtr<nsIDOMNode>rightNode;
|
|
|
|
result = GetBlockSection(currentNode,
|
|
|
|
getter_AddRefs(leftNode),
|
|
|
|
getter_AddRefs(rightNode));
|
|
|
|
if ((NS_SUCCEEDED(result)) && leftNode && rightNode)
|
|
|
|
{
|
|
|
|
// add range to the list if it doesn't overlap with the previous range
|
|
|
|
PRBool addRange=PR_TRUE;
|
|
|
|
if (lastRange)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> lastStartNode;
|
|
|
|
nsCOMPtr<nsIDOMElement> blockParentOfLastStartNode;
|
|
|
|
lastRange->GetStartContainer(getter_AddRefs(lastStartNode));
|
|
|
|
blockParentOfLastStartNode = do_QueryInterface(GetBlockNodeParent(lastStartNode));
|
|
|
|
if (blockParentOfLastStartNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMElement> blockParentOfLeftNode;
|
|
|
|
blockParentOfLeftNode = do_QueryInterface(GetBlockNodeParent(leftNode));
|
|
|
|
if (blockParentOfLeftNode)
|
|
|
|
{
|
|
|
|
if (blockParentOfLastStartNode==blockParentOfLeftNode) {
|
|
|
|
addRange = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (PR_TRUE==addRange)
|
|
|
|
{
|
2003-07-08 20:22:56 +00:00
|
|
|
nsCOMPtr<nsIDOMRange> range =
|
|
|
|
do_CreateInstance("@mozilla.org/content/range;1", &result);
|
2001-04-05 23:48:01 +00:00
|
|
|
if ((NS_SUCCEEDED(result)) && range)
|
|
|
|
{ // initialize the range
|
|
|
|
range->SetStart(leftNode, 0);
|
|
|
|
range->SetEnd(rightNode, 0);
|
2002-11-12 19:40:11 +00:00
|
|
|
aSections.AppendObject(range);
|
2001-04-05 23:48:01 +00:00
|
|
|
lastRange = do_QueryInterface(range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* do not check result here, and especially do not return the result code.
|
|
|
|
* we rely on iter->IsDone to tell us when the iteration is complete
|
|
|
|
*/
|
|
|
|
iter->Next();
|
|
|
|
iter->CurrentNode(getter_AddRefs(currentContent));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// NextNodeInBlock: gets the next/prev node in the block, if any. Next node
|
|
|
|
// must be an element or text node, others are ignored
|
|
|
|
nsCOMPtr<nsIDOMNode>
|
|
|
|
nsHTMLEditor::NextNodeInBlock(nsIDOMNode *aNode, IterDirection aDir)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> nullNode;
|
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
nsCOMPtr<nsIContent> blockContent;
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
nsCOMPtr<nsIDOMNode> blockParent;
|
|
|
|
|
|
|
|
if (!aNode) return nullNode;
|
|
|
|
|
2003-07-08 20:22:56 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &rv);
|
|
|
|
if (NS_FAILED(rv))
|
2001-04-05 23:48:01 +00:00
|
|
|
return nullNode;
|
|
|
|
|
|
|
|
// much gnashing of teeth as we twit back and forth between content and domnode types
|
|
|
|
content = do_QueryInterface(aNode);
|
|
|
|
PRBool isBlock;
|
2001-04-07 00:45:26 +00:00
|
|
|
if (NS_SUCCEEDED(NodeIsBlockStatic(aNode, &isBlock)) && isBlock)
|
2001-04-05 23:48:01 +00:00
|
|
|
{
|
2002-11-12 19:40:11 +00:00
|
|
|
blockParent = aNode;
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
blockParent = GetBlockNodeParent(aNode);
|
|
|
|
}
|
|
|
|
if (!blockParent) return nullNode;
|
|
|
|
blockContent = do_QueryInterface(blockParent);
|
|
|
|
if (!blockContent) return nullNode;
|
|
|
|
|
|
|
|
if (NS_FAILED(iter->Init(blockContent))) return nullNode;
|
|
|
|
if (NS_FAILED(iter->PositionAt(content))) return nullNode;
|
|
|
|
|
|
|
|
while (NS_ENUMERATOR_FALSE == iter->IsDone())
|
|
|
|
{
|
|
|
|
if (NS_FAILED(iter->CurrentNode(getter_AddRefs(content)))) return nullNode;
|
|
|
|
// ignore nodes that aren't elements or text, or that are the block parent
|
|
|
|
node = do_QueryInterface(content);
|
|
|
|
if (node && IsTextOrElementNode(node) && (node != blockParent) && (node.get() != aNode))
|
|
|
|
return node;
|
|
|
|
|
|
|
|
if (aDir == kIterForward)
|
|
|
|
iter->Next();
|
|
|
|
else
|
|
|
|
iter->Prev();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
static const PRUnichar nbsp = 160;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsNextCharWhitespace: checks the adjacent content in the same block
|
|
|
|
// to see if following selection is whitespace or nbsp
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::IsNextCharWhitespace(nsIDOMNode *aParentNode,
|
|
|
|
PRInt32 aOffset,
|
|
|
|
PRBool *outIsSpace,
|
|
|
|
PRBool *outIsNBSP,
|
|
|
|
nsCOMPtr<nsIDOMNode> *outNode,
|
|
|
|
PRInt32 *outOffset)
|
|
|
|
{
|
|
|
|
if (!outIsSpace || !outIsNBSP) return NS_ERROR_NULL_POINTER;
|
|
|
|
*outIsSpace = PR_FALSE;
|
|
|
|
*outIsNBSP = PR_FALSE;
|
|
|
|
if (outNode) *outNode = nsnull;
|
|
|
|
if (outOffset) *outOffset = -1;
|
|
|
|
|
|
|
|
nsAutoString tempString;
|
|
|
|
PRUint32 strLength;
|
|
|
|
nsCOMPtr<nsIDOMText> textNode = do_QueryInterface(aParentNode);
|
|
|
|
if (textNode)
|
|
|
|
{
|
|
|
|
textNode->GetLength(&strLength);
|
|
|
|
if ((PRUint32)aOffset < strLength)
|
|
|
|
{
|
|
|
|
// easy case: next char is in same node
|
|
|
|
textNode->SubstringData(aOffset,aOffset+1,tempString);
|
|
|
|
*outIsSpace = nsCRT::IsAsciiSpace(tempString.First());
|
|
|
|
*outIsNBSP = (tempString.First() == nbsp);
|
|
|
|
if (outNode) *outNode = do_QueryInterface(aParentNode);
|
|
|
|
if (outOffset) *outOffset = aOffset+1; // yes, this is _past_ the character;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// harder case: next char in next node.
|
|
|
|
nsCOMPtr<nsIDOMNode> node = NextNodeInBlock(aParentNode, kIterForward);
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
PRBool isBlock (PR_FALSE);
|
2001-04-07 00:45:26 +00:00
|
|
|
NodeIsBlock(node, &isBlock);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (isBlock) // skip over bold, italic, link, ect nodes
|
|
|
|
{
|
|
|
|
if (IsTextNode(node) && IsEditable(node))
|
|
|
|
{
|
|
|
|
textNode = do_QueryInterface(node);
|
|
|
|
textNode->GetLength(&strLength);
|
|
|
|
if (strLength)
|
|
|
|
{
|
|
|
|
textNode->SubstringData(0,1,tempString);
|
|
|
|
*outIsSpace = nsCRT::IsAsciiSpace(tempString.First());
|
|
|
|
*outIsNBSP = (tempString.First() == nbsp);
|
|
|
|
if (outNode) *outNode = do_QueryInterface(node);
|
|
|
|
if (outOffset) *outOffset = 1; // yes, this is _past_ the character;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// else it's an empty text node, or not editable; skip it.
|
|
|
|
}
|
|
|
|
else // node is an image or some other thingy that doesn't count as whitespace
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tmp = node;
|
|
|
|
node = NextNodeInBlock(tmp, kIterForward);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsPrevCharWhitespace: checks the adjacent content in the same block
|
|
|
|
// to see if following selection is whitespace
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::IsPrevCharWhitespace(nsIDOMNode *aParentNode,
|
|
|
|
PRInt32 aOffset,
|
|
|
|
PRBool *outIsSpace,
|
|
|
|
PRBool *outIsNBSP,
|
|
|
|
nsCOMPtr<nsIDOMNode> *outNode,
|
|
|
|
PRInt32 *outOffset)
|
|
|
|
{
|
|
|
|
if (!outIsSpace || !outIsNBSP) return NS_ERROR_NULL_POINTER;
|
|
|
|
*outIsSpace = PR_FALSE;
|
|
|
|
*outIsNBSP = PR_FALSE;
|
|
|
|
if (outNode) *outNode = nsnull;
|
|
|
|
if (outOffset) *outOffset = -1;
|
|
|
|
|
|
|
|
nsAutoString tempString;
|
|
|
|
PRUint32 strLength;
|
|
|
|
nsCOMPtr<nsIDOMText> textNode = do_QueryInterface(aParentNode);
|
|
|
|
if (textNode)
|
|
|
|
{
|
|
|
|
if (aOffset > 0)
|
|
|
|
{
|
|
|
|
// easy case: prev char is in same node
|
|
|
|
textNode->SubstringData(aOffset-1,aOffset,tempString);
|
|
|
|
*outIsSpace = nsCRT::IsAsciiSpace(tempString.First());
|
|
|
|
*outIsNBSP = (tempString.First() == nbsp);
|
|
|
|
if (outNode) *outNode = do_QueryInterface(aParentNode);
|
|
|
|
if (outOffset) *outOffset = aOffset-1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// harder case: prev char in next node
|
|
|
|
nsCOMPtr<nsIDOMNode> node = NextNodeInBlock(aParentNode, kIterBackward);
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
while (node)
|
|
|
|
{
|
|
|
|
PRBool isBlock (PR_FALSE);
|
2001-04-07 00:45:26 +00:00
|
|
|
NodeIsBlock(node, &isBlock);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (isBlock) // skip over bold, italic, link, ect nodes
|
|
|
|
{
|
|
|
|
if (IsTextNode(node) && IsEditable(node))
|
|
|
|
{
|
|
|
|
textNode = do_QueryInterface(node);
|
|
|
|
textNode->GetLength(&strLength);
|
|
|
|
if (strLength)
|
|
|
|
{
|
|
|
|
// you could use nsITextContent::IsOnlyWhitespace here
|
|
|
|
textNode->SubstringData(strLength-1,strLength,tempString);
|
|
|
|
*outIsSpace = nsCRT::IsAsciiSpace(tempString.First());
|
|
|
|
*outIsNBSP = (tempString.First() == nbsp);
|
|
|
|
if (outNode) *outNode = do_QueryInterface(aParentNode);
|
|
|
|
if (outOffset) *outOffset = strLength-1;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// else it's an empty text node, or not editable; skip it.
|
|
|
|
}
|
|
|
|
else // node is an image or some other thingy that doesn't count as whitespace
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// otherwise we found a node we want to skip, keep going
|
|
|
|
tmp = node;
|
|
|
|
node = NextNodeInBlock(tmp, kIterBackward);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ------------ End Block methods -------------- */
|
|
|
|
|
|
|
|
|
2002-05-14 00:05:37 +00:00
|
|
|
PRBool nsHTMLEditor::IsVisBreak(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
if (!aNode)
|
|
|
|
return PR_FALSE;
|
|
|
|
if (!nsTextEditUtils::IsBreak(aNode))
|
|
|
|
return PR_FALSE;
|
|
|
|
// check if there is a later node in block after br
|
|
|
|
nsCOMPtr<nsIDOMNode> priorNode, nextNode;
|
|
|
|
GetPriorHTMLNode(aNode, address_of(priorNode), PR_TRUE);
|
|
|
|
GetNextHTMLNode(aNode, address_of(nextNode), PR_TRUE);
|
|
|
|
// if we are next to another break, we are visible
|
|
|
|
if (priorNode && nsTextEditUtils::IsBreak(priorNode))
|
|
|
|
return PR_TRUE;
|
|
|
|
if (nextNode && nsTextEditUtils::IsBreak(nextNode))
|
|
|
|
return PR_TRUE;
|
|
|
|
|
|
|
|
// if we are right before block boundary, then br not visible
|
|
|
|
if (!nextNode)
|
|
|
|
return PR_FALSE; // this break is trailer in block, it's not visible
|
|
|
|
if (IsBlockNode(nextNode))
|
|
|
|
return PR_FALSE; // break is right before a block, it's not visible
|
|
|
|
|
|
|
|
// sigh. We have to use expensive whitespace calculation code to
|
|
|
|
// determine what is going on
|
|
|
|
nsCOMPtr<nsIDOMNode> selNode, tmp;
|
|
|
|
PRInt32 selOffset;
|
|
|
|
GetNodeLocation(aNode, address_of(selNode), &selOffset);
|
|
|
|
selOffset++; // lets look after the break
|
|
|
|
nsWSRunObject wsObj(this, selNode, selOffset);
|
2002-05-17 02:44:55 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> visNode;
|
|
|
|
PRInt32 visOffset=0;
|
|
|
|
PRInt16 visType=0;
|
|
|
|
wsObj.NextVisibleNode(selNode, selOffset, address_of(visNode), &visOffset, &visType);
|
|
|
|
if (visType & nsWSRunObject::eBlock)
|
2002-05-14 00:05:37 +00:00
|
|
|
return PR_FALSE;
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-18 19:42:38 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetIsDocumentEditable(PRBool *aIsDocumentEditable)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsDocumentEditable);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> doc;
|
|
|
|
GetDocument(getter_AddRefs(doc));
|
2002-10-09 00:26:08 +00:00
|
|
|
*aIsDocumentEditable = doc ? IsModifiable() : PR_FALSE;
|
2002-09-18 19:42:38 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-01-18 21:50:15 +00:00
|
|
|
PRBool nsHTMLEditor::IsModifiable()
|
|
|
|
{
|
|
|
|
PRUint32 flags;
|
|
|
|
if (NS_SUCCEEDED(GetFlags(&flags)))
|
2002-10-09 00:26:08 +00:00
|
|
|
return ((flags & nsIPlaintextEditor::eEditorReadonlyMask) == 0);
|
2000-01-18 21:50:15 +00:00
|
|
|
else
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
2000-03-24 00:26:47 +00:00
|
|
|
#pragma mark nsIHTMLEditor methods
|
1999-08-09 01:37:50 +00:00
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
2002-11-27 00:35:09 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::UpdateBaseURL()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc;
|
|
|
|
GetDocument(getter_AddRefs(domDoc));
|
|
|
|
if (!domDoc) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Look for an HTML <base> tag
|
|
|
|
nsCOMPtr<nsIDOMNodeList> nodeList;
|
|
|
|
nsresult rv = domDoc->GetElementsByTagName(NS_LITERAL_STRING("base"), getter_AddRefs(nodeList));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> baseNode;
|
|
|
|
if (nodeList)
|
|
|
|
{
|
|
|
|
PRUint32 count;
|
|
|
|
nodeList->GetLength(&count);
|
|
|
|
if (count >= 1)
|
|
|
|
{
|
|
|
|
rv = nodeList->Item(0, getter_AddRefs(baseNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// If no base tag, then set baseURL to the document's URL
|
|
|
|
// This is very important, else relative URLs for links and images are wrong
|
|
|
|
if (!baseNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(domDoc);
|
|
|
|
if (!doc) return NS_ERROR_FAILURE;
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = doc->GetDocumentURL(getter_AddRefs(uri));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return doc->SetBaseURL(uri);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-11-17 00:25:31 +00:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::HandleKeyPress(nsIDOMKeyEvent* aKeyEvent)
|
1999-09-13 09:37:51 +00:00
|
|
|
{
|
|
|
|
PRUint32 keyCode, character;
|
|
|
|
PRBool isShift, ctrlKey, altKey, metaKey;
|
|
|
|
nsresult res;
|
1999-09-29 20:08:15 +00:00
|
|
|
|
1999-09-13 09:37:51 +00:00
|
|
|
if (!aKeyEvent) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aKeyEvent->GetKeyCode(&keyCode)) &&
|
2002-10-23 15:18:13 +00:00
|
|
|
NS_SUCCEEDED(aKeyEvent->GetShiftKey(&isShift)) &&
|
1999-09-13 09:37:51 +00:00
|
|
|
NS_SUCCEEDED(aKeyEvent->GetCtrlKey(&ctrlKey)) &&
|
|
|
|
NS_SUCCEEDED(aKeyEvent->GetAltKey(&altKey)) &&
|
|
|
|
NS_SUCCEEDED(aKeyEvent->GetMetaKey(&metaKey)))
|
|
|
|
{
|
|
|
|
// this royally blows: because tabs come in from keyDowns instead
|
|
|
|
// of keyPress, and because GetCharCode refuses to work for keyDown
|
|
|
|
// i have to play games.
|
1999-11-03 07:11:45 +00:00
|
|
|
if (keyCode == nsIDOMKeyEvent::DOM_VK_TAB) character = '\t';
|
1999-09-13 09:37:51 +00:00
|
|
|
else aKeyEvent->GetCharCode(&character);
|
|
|
|
|
2003-02-04 12:12:55 +00:00
|
|
|
if (keyCode == nsIDOMKeyEvent::DOM_VK_TAB)
|
1999-09-13 09:37:51 +00:00
|
|
|
{
|
2003-02-04 12:12:55 +00:00
|
|
|
if (!(mFlags & eEditorPlaintextMask)) {
|
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
PRInt32 offset;
|
|
|
|
nsCOMPtr<nsIDOMNode> node, blockParent;
|
|
|
|
res = GetStartNodeAndOffset(selection, address_of(node), &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!node) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
PRBool isBlock = PR_FALSE;
|
|
|
|
NodeIsBlock(node, &isBlock);
|
|
|
|
if (isBlock) blockParent = node;
|
|
|
|
else blockParent = GetBlockNodeParent(node);
|
2000-04-18 21:39:35 +00:00
|
|
|
|
2003-02-04 12:12:55 +00:00
|
|
|
if (blockParent)
|
2002-06-13 20:35:12 +00:00
|
|
|
{
|
2003-02-04 12:12:55 +00:00
|
|
|
PRBool bHandled = PR_FALSE;
|
|
|
|
|
|
|
|
if (nsHTMLEditUtils::IsTableElement(blockParent))
|
|
|
|
{
|
|
|
|
res = TabInTable(isShift, &bHandled);
|
|
|
|
if (bHandled)
|
|
|
|
ScrollSelectionIntoView(PR_FALSE);
|
|
|
|
}
|
|
|
|
else if (nsHTMLEditUtils::IsListItem(blockParent))
|
|
|
|
{
|
|
|
|
nsAutoString indentstr;
|
|
|
|
if (isShift) indentstr.Assign(NS_LITERAL_STRING("outdent"));
|
|
|
|
else indentstr.Assign(NS_LITERAL_STRING("indent"));
|
|
|
|
res = Indent(indentstr);
|
|
|
|
bHandled = PR_TRUE;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(res)) return res;
|
2002-06-13 20:35:12 +00:00
|
|
|
if (bHandled)
|
2003-02-04 12:12:55 +00:00
|
|
|
return aKeyEvent->PreventDefault(); // consumed
|
2000-04-18 21:39:35 +00:00
|
|
|
}
|
|
|
|
}
|
2003-02-04 12:12:55 +00:00
|
|
|
if (isShift)
|
|
|
|
return NS_OK; // don't type text for shift tabs
|
1999-09-13 09:37:51 +00:00
|
|
|
}
|
1999-11-03 22:05:45 +00:00
|
|
|
else if (keyCode == nsIDOMKeyEvent::DOM_VK_RETURN
|
|
|
|
|| keyCode == nsIDOMKeyEvent::DOM_VK_ENTER)
|
1999-09-13 09:37:51 +00:00
|
|
|
{
|
2003-02-04 12:12:55 +00:00
|
|
|
aKeyEvent->PreventDefault();
|
2000-11-17 00:25:31 +00:00
|
|
|
nsString empty;
|
2002-04-06 19:07:47 +00:00
|
|
|
if (isShift && !(mFlags&eEditorPlaintextMask))
|
1999-09-29 20:08:15 +00:00
|
|
|
{
|
2001-04-07 00:45:26 +00:00
|
|
|
return TypedText(empty, eTypedBR); // only inserts a br node
|
1999-09-29 20:08:15 +00:00
|
|
|
}
|
1999-09-13 09:37:51 +00:00
|
|
|
else
|
1999-10-06 19:34:09 +00:00
|
|
|
{
|
2001-04-07 00:45:26 +00:00
|
|
|
return TypedText(empty, eTypedBreak); // uses rules to figure out what to insert
|
1999-10-06 19:34:09 +00:00
|
|
|
}
|
|
|
|
}
|
2000-04-13 21:50:19 +00:00
|
|
|
else if (keyCode == nsIDOMKeyEvent::DOM_VK_ESCAPE)
|
|
|
|
{
|
2003-02-04 12:12:55 +00:00
|
|
|
aKeyEvent->PreventDefault();
|
2002-01-10 14:51:45 +00:00
|
|
|
// pass escape keypresses through as empty strings: needed forime support
|
|
|
|
nsString empty;
|
|
|
|
return TypedText(empty, eTypedText);
|
2000-04-13 21:50:19 +00:00
|
|
|
}
|
2000-01-15 14:29:29 +00:00
|
|
|
|
|
|
|
// if we got here we either fell out of the tab case or have a normal character.
|
|
|
|
// Either way, treat as normal character.
|
2002-11-07 03:34:13 +00:00
|
|
|
if (character && !altKey && !ctrlKey && !metaKey)
|
landing keyEvent_19991004_BRANCH
bugs # see the log of the check in into branch
author/reviewer:
mozilla/layout/base/src/nsRangeList.cpp brade/mjudge
mozilla/layout/html/forms/src/nsGfxTextControlFrame.cpp brade/ftang
mozilla/layout/events/src/nsDOMEvent.cpp brade/joki
mozilla/layout/events/src/nsEventStateManager.cpp brade/joki
mozilla/widget/public/nsGUIEvent.h akkana/ftang
mozilla/widget/src/windows/nsWindow.cpp ftang/mjudge
mozilla/widget/src/windows/nsWindow.h ftang/mjudge
mozilla/widget/src/mac/nsTextAreaWidget.cpp brade/ftang
mozilla/widget/src/mac/nsMacEventHandler.cpp brade/simon
mozilla/widget/src/xpwidgets/nsKeyBindMgr.cpp brade/ftang
mozilla/widget/src/gtk/nsGtkEventHandler.cpp akkana/?
mozilla/widget/src/gtk/nsWidget.cpp erik/ftang
mozilla/layout/xul/base/src/nsTreeCellFrame.cpp brade/ftang
mozilla/editor/base/nsEditorEventListeners.cpp brade/akkana
mozilla/editor/base/nsHTMLEditor.cpp brade/akkana
mozilla/rdf/content/src/nsXULKeyListener.cpp ftang/saari
fix the master bug- 15693
fix at least, but not limited to, the following bugs
10158,11956,6053,9333,10901,14348,6449,11845,13016,14410,15657,15307,15842,13856
1999-10-14 18:27:01 +00:00
|
|
|
{
|
2002-09-17 21:27:30 +00:00
|
|
|
aKeyEvent->PreventDefault();
|
2000-01-15 14:29:29 +00:00
|
|
|
nsAutoString key(character);
|
2001-04-07 00:45:26 +00:00
|
|
|
return TypedText(key, eTypedText);
|
1999-09-13 09:37:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-10-06 19:34:09 +00:00
|
|
|
/* This routine is needed to provide a bottleneck for typing for logging
|
|
|
|
purposes. Can't use EditorKeyPress() (above) for that since it takes
|
|
|
|
a nsIDOMUIEvent* parameter. So instead we pass enough info through
|
|
|
|
to TypedText() to determine what action to take, but without passing
|
|
|
|
an event.
|
|
|
|
*/
|
2002-03-23 22:08:20 +00:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::TypedText(const nsAString& aString,
|
2000-11-17 00:25:31 +00:00
|
|
|
PRInt32 aAction)
|
1999-10-06 19:34:09 +00:00
|
|
|
{
|
|
|
|
nsAutoPlaceHolderBatch batch(this, gTypingTxnName);
|
|
|
|
|
|
|
|
switch (aAction)
|
|
|
|
{
|
|
|
|
case eTypedText:
|
2001-01-28 20:13:07 +00:00
|
|
|
case eTypedBreak:
|
1999-10-06 19:34:09 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
return nsPlaintextEditor::TypedText(aString, aAction);
|
1999-10-06 19:34:09 +00:00
|
|
|
}
|
|
|
|
case eTypedBR:
|
2000-01-15 14:29:29 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> brNode;
|
2000-12-09 04:46:08 +00:00
|
|
|
return InsertBR(address_of(brNode)); // only inserts a br node
|
2000-01-15 14:29:29 +00:00
|
|
|
}
|
1999-10-06 19:34:09 +00:00
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
1999-09-13 09:37:51 +00:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::TabInTable(PRBool inIsShift, PRBool *outHandled)
|
|
|
|
{
|
|
|
|
if (!outHandled) return NS_ERROR_NULL_POINTER;
|
|
|
|
*outHandled = PR_FALSE;
|
|
|
|
|
2000-04-04 14:51:26 +00:00
|
|
|
// Find enclosing table cell from the selection (cell may be the selected element)
|
|
|
|
nsCOMPtr<nsIDOMElement> cellElement;
|
2000-08-20 00:34:08 +00:00
|
|
|
// can't use |NS_LITERAL_STRING| here until |GetElementOrParentByTagName| is fixed to accept readables
|
2001-11-14 01:33:42 +00:00
|
|
|
nsresult res = GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nsnull, getter_AddRefs(cellElement));
|
1999-09-13 09:37:51 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-04-04 14:51:26 +00:00
|
|
|
// Do nothing -- we didn't find a table cell
|
|
|
|
if (!cellElement) return NS_OK;
|
|
|
|
|
|
|
|
// find enclosing table
|
|
|
|
nsCOMPtr<nsIDOMNode> tbl = GetEnclosingTable(cellElement);
|
|
|
|
if (!tbl) return res;
|
|
|
|
|
|
|
|
// advance to next cell
|
|
|
|
// first create an iterator over the table
|
2003-07-08 20:22:56 +00:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
|
1999-09-13 09:37:51 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-04-04 14:51:26 +00:00
|
|
|
if (!iter) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsCOMPtr<nsIContent> cTbl = do_QueryInterface(tbl);
|
|
|
|
nsCOMPtr<nsIContent> cBlock = do_QueryInterface(cellElement);
|
|
|
|
res = iter->Init(cTbl);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
// position iter at block
|
|
|
|
res = iter->PositionAt(cBlock);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
nsCOMPtr<nsIContent> cNode;
|
|
|
|
do
|
1999-09-13 09:37:51 +00:00
|
|
|
{
|
2000-04-04 14:51:26 +00:00
|
|
|
if (inIsShift) res = iter->Prev();
|
|
|
|
else res = iter->Next();
|
2000-04-18 21:39:35 +00:00
|
|
|
if (NS_FAILED(res)) break;
|
2000-04-04 14:51:26 +00:00
|
|
|
res = iter->CurrentNode(getter_AddRefs(cNode));
|
2000-04-18 21:39:35 +00:00
|
|
|
if (NS_FAILED(res)) break;
|
2000-04-04 14:51:26 +00:00
|
|
|
node = do_QueryInterface(cNode);
|
2000-08-13 23:53:34 +00:00
|
|
|
if (nsHTMLEditUtils::IsTableCell(node) && (GetEnclosingTable(node) == tbl))
|
1999-09-13 09:37:51 +00:00
|
|
|
{
|
2000-04-04 14:51:26 +00:00
|
|
|
res = CollapseSelectionToDeepestNonTableFirstChild(nsnull, node);
|
1999-09-13 09:37:51 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-04-04 14:51:26 +00:00
|
|
|
*outHandled = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
} while (iter->IsDone() == NS_ENUMERATOR_FALSE);
|
2000-04-18 21:39:35 +00:00
|
|
|
|
|
|
|
if (!(*outHandled) && !inIsShift)
|
|
|
|
{
|
|
|
|
// if we havent handled it yet then we must have run off the end of
|
|
|
|
// the table. Insert a new row.
|
|
|
|
res = InsertTableRow(1, PR_TRUE);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
*outHandled = PR_TRUE;
|
|
|
|
// put selection in right place
|
2000-05-02 03:24:11 +00:00
|
|
|
// Use table code to get selection and index to new row...
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelection>selection;
|
2000-05-03 21:18:23 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> tblElement;
|
2000-04-18 21:39:35 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> cell;
|
2000-05-02 03:24:11 +00:00
|
|
|
PRInt32 row;
|
|
|
|
res = GetCellContext(getter_AddRefs(selection),
|
2000-05-03 21:18:23 +00:00
|
|
|
getter_AddRefs(tblElement),
|
2000-05-02 03:24:11 +00:00
|
|
|
getter_AddRefs(cell),
|
|
|
|
nsnull, nsnull,
|
|
|
|
&row, nsnull);
|
2000-04-18 21:39:35 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-05-02 03:24:11 +00:00
|
|
|
// ...so that we can ask for first cell in that row...
|
2001-02-26 22:02:58 +00:00
|
|
|
res = GetCellAt(tblElement, row, 0, getter_AddRefs(cell));
|
2000-04-18 21:39:35 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-05-02 03:24:11 +00:00
|
|
|
// ...and then set selection there.
|
|
|
|
// (Note that normally you should use CollapseSelectionToDeepestNonTableFirstChild(),
|
|
|
|
// but we know cell is an empty new cell, so this works fine)
|
2000-04-18 21:39:35 +00:00
|
|
|
node = do_QueryInterface(cell);
|
|
|
|
if (node) selection->Collapse(node,0);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
1999-09-13 09:37:51 +00:00
|
|
|
}
|
|
|
|
|
2001-05-11 12:43:22 +00:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::CreateBRImpl(nsCOMPtr<nsIDOMNode> *aInOutParent,
|
|
|
|
PRInt32 *aInOutOffset,
|
|
|
|
nsCOMPtr<nsIDOMNode> *outBRNode,
|
|
|
|
EDirection aSelect)
|
1999-11-25 00:19:45 +00:00
|
|
|
{
|
2000-03-24 00:26:47 +00:00
|
|
|
if (!aInOutParent || !*aInOutParent || !aInOutOffset || !outBRNode) return NS_ERROR_NULL_POINTER;
|
1999-11-25 00:19:45 +00:00
|
|
|
*outBRNode = nsnull;
|
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
// we need to insert a br. unfortunately, we may have to split a text node to do it.
|
2000-03-24 00:26:47 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> node = *aInOutParent;
|
|
|
|
PRInt32 theOffset = *aInOutOffset;
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(node);
|
2002-01-14 20:31:43 +00:00
|
|
|
NS_NAMED_LITERAL_STRING(brType, "br");
|
1999-11-25 00:19:45 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> brNode;
|
|
|
|
if (nodeAsText)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
PRInt32 offset;
|
|
|
|
PRUint32 len;
|
|
|
|
nodeAsText->GetLength(&len);
|
2000-12-09 04:46:08 +00:00
|
|
|
GetNodeLocation(node, address_of(tmp), &offset);
|
1999-11-25 00:19:45 +00:00
|
|
|
if (!tmp) return NS_ERROR_FAILURE;
|
2000-03-24 00:26:47 +00:00
|
|
|
if (!theOffset)
|
1999-11-25 00:19:45 +00:00
|
|
|
{
|
|
|
|
// we are already set to go
|
|
|
|
}
|
2000-03-24 00:26:47 +00:00
|
|
|
else if (theOffset == (PRInt32)len)
|
1999-11-25 00:19:45 +00:00
|
|
|
{
|
|
|
|
// update offset to point AFTER the text node
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// split the text node
|
2000-03-24 00:26:47 +00:00
|
|
|
res = SplitNode(node, theOffset, getter_AddRefs(tmp));
|
1999-11-25 00:19:45 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-12-09 04:46:08 +00:00
|
|
|
res = GetNodeLocation(node, address_of(tmp), &offset);
|
1999-11-25 00:19:45 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
// create br
|
|
|
|
res = CreateNode(brType, tmp, offset, getter_AddRefs(brNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
2000-03-24 00:26:47 +00:00
|
|
|
*aInOutParent = tmp;
|
|
|
|
*aInOutOffset = offset+1;
|
1999-11-25 00:19:45 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2000-03-24 00:26:47 +00:00
|
|
|
res = CreateNode(brType, node, theOffset, getter_AddRefs(brNode));
|
1999-11-25 00:19:45 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-03-24 00:26:47 +00:00
|
|
|
(*aInOutOffset)++;
|
1999-11-25 00:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
*outBRNode = brNode;
|
2000-02-25 04:39:30 +00:00
|
|
|
if (*outBRNode && (aSelect != eNone))
|
|
|
|
{
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2000-02-25 04:39:30 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
PRInt32 offset;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
|
2000-12-09 04:46:08 +00:00
|
|
|
res = GetNodeLocation(*outBRNode, address_of(parent), &offset);
|
2000-02-25 04:39:30 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (aSelect == eNext)
|
|
|
|
{
|
|
|
|
// position selection after br
|
2000-09-14 11:45:01 +00:00
|
|
|
selPriv->SetInterlinePosition(PR_TRUE);
|
2000-02-25 04:39:30 +00:00
|
|
|
res = selection->Collapse(parent, offset+1);
|
|
|
|
}
|
|
|
|
else if (aSelect == ePrevious)
|
|
|
|
{
|
|
|
|
// position selection before br
|
2000-09-14 11:45:01 +00:00
|
|
|
selPriv->SetInterlinePosition(PR_TRUE);
|
2000-02-25 04:39:30 +00:00
|
|
|
res = selection->Collapse(parent, offset);
|
|
|
|
}
|
|
|
|
}
|
1999-11-25 00:19:45 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::CreateBR(nsIDOMNode *aNode, PRInt32 aOffset, nsCOMPtr<nsIDOMNode> *outBRNode, EDirection aSelect)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = aNode;
|
|
|
|
PRInt32 offset = aOffset;
|
2000-12-09 04:46:08 +00:00
|
|
|
return CreateBRImpl(address_of(parent), &offset, outBRNode, aSelect);
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
1999-09-29 20:08:15 +00:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::InsertBR(nsCOMPtr<nsIDOMNode> *outBRNode)
|
1999-09-13 09:37:51 +00:00
|
|
|
{
|
|
|
|
PRBool bCollapsed;
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
1999-09-13 09:37:51 +00:00
|
|
|
|
1999-09-29 20:08:15 +00:00
|
|
|
if (!outBRNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
*outBRNode = nsnull;
|
fixes:
14753, 29843, 39864, 40141,
40139, 36679, 39542, 34729,
34855, 37216, 39292, 26447
r=sfraser,cmanske,fm; a=beppe
2000-05-24 23:00:24 +00:00
|
|
|
|
|
|
|
// calling it text insertion to trigger moz br treatment by rules
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpInsertText, nsIEditor::eNext);
|
|
|
|
|
1999-09-13 09:37:51 +00:00
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
|
1999-09-13 09:37:51 +00:00
|
|
|
res = selection->GetIsCollapsed(&bCollapsed);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!bCollapsed)
|
|
|
|
{
|
1999-12-07 08:30:19 +00:00
|
|
|
res = DeleteSelection(nsIEditor::eNone);
|
1999-09-13 09:37:51 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDOMNode> selNode;
|
|
|
|
PRInt32 selOffset;
|
2000-12-09 04:46:08 +00:00
|
|
|
res = GetStartNodeAndOffset(selection, address_of(selNode), &selOffset);
|
1999-09-13 09:37:51 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
1999-11-25 00:19:45 +00:00
|
|
|
res = CreateBR(selNode, selOffset, outBRNode);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
2000-02-25 04:39:30 +00:00
|
|
|
// position selection after br
|
2000-12-09 04:46:08 +00:00
|
|
|
res = GetNodeLocation(*outBRNode, address_of(selNode), &selOffset);
|
fixes:
14753, 29843, 39864, 40141,
40139, 36679, 39542, 34729,
34855, 37216, 39292, 26447
r=sfraser,cmanske,fm; a=beppe
2000-05-24 23:00:24 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-09-14 11:45:01 +00:00
|
|
|
selPriv->SetInterlinePosition(PR_TRUE);
|
2000-02-25 04:39:30 +00:00
|
|
|
res = selection->Collapse(selNode, selOffset+1);
|
|
|
|
|
|
|
|
return res;
|
1999-09-13 09:37:51 +00:00
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetDOMEventReceiver(nsIDOMEventReceiver **aEventReceiver)
|
|
|
|
{
|
|
|
|
if (!aEventReceiver)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-03-01 19:54:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
*aEventReceiver = 0;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> rootElement;
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult result = GetRootElement(getter_AddRefs(rootElement));
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(result))
|
|
|
|
return result;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!rootElement)
|
|
|
|
return NS_ERROR_FAILURE;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Now hack to make sure we are not anonymous content.
|
|
|
|
// If we are grab the parent of root element for our observer.
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(rootElement);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (content)
|
|
|
|
{
|
2003-07-28 21:35:53 +00:00
|
|
|
nsIContent* parent = content->GetParent();
|
|
|
|
if (parent)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2003-09-27 04:18:26 +00:00
|
|
|
if (parent->IndexOf(content) < 0)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
rootElement = do_QueryInterface(parent); //this will put listener on the form element basically
|
2003-07-28 21:35:53 +00:00
|
|
|
result = CallQueryInterface(rootElement, aEventReceiver);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
rootElement = 0; // Let the event receiver work on the document instead of the root element
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
rootElement = 0;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!rootElement && mDocWeak)
|
|
|
|
{
|
|
|
|
// Don't use getDocument here, because we have no way of knowing if
|
|
|
|
// Init() was ever called. So we need to get the document ourselves,
|
|
|
|
// if it exists.
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc = do_QueryReferent(mDocWeak);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!domdoc)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
result = domdoc->QueryInterface(NS_GET_IID(nsIDOMEventReceiver), (void **)aEventReceiver);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::CollapseSelectionToDeepestNonTableFirstChild(nsISelection *aSelection, nsIDOMNode *aNode)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!aNode) return NS_ERROR_NULL_POINTER;
|
2000-03-24 00:26:47 +00:00
|
|
|
nsresult res;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
if (aSelection)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
selection = aSelection;
|
|
|
|
} else {
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selection) return NS_ERROR_FAILURE;
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> node = aNode;
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
do {
|
|
|
|
node->GetFirstChild(getter_AddRefs(child));
|
|
|
|
|
|
|
|
if (child)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Stop if we find a table
|
|
|
|
// don't want to go into nested tables
|
|
|
|
if (nsHTMLEditUtils::IsTable(child)) break;
|
|
|
|
// hey, it'g gotta be a container too!
|
|
|
|
if (!IsContainer(child)) break;
|
|
|
|
node = child;
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
while (child);
|
|
|
|
|
|
|
|
selection->Collapse(node,0);
|
2000-03-24 00:26:47 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-04-13 21:50:19 +00:00
|
|
|
|
|
|
|
|
2003-04-15 14:07:30 +00:00
|
|
|
// This is mostly like InsertHTMLWithCharsetAndContext,
|
2001-01-28 20:13:07 +00:00
|
|
|
// but we can't use that because it is selection-based and
|
|
|
|
// the rules code won't let us edit under the <head> node
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::ReplaceHeadContentsWithHTML(const nsAString& aSourceToInsert)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2001-12-09 09:24:33 +00:00
|
|
|
nsAutoRules beginRulesSniffing(this, kOpIgnore, nsIEditor::eNone); // dont do any post processing, rules get confused
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
ForceCompositionEnd();
|
|
|
|
|
|
|
|
// Do not use nsAutoRules -- rules code won't let us insert in <head>
|
|
|
|
// Use the head node as a parent and delete/insert directly
|
|
|
|
nsCOMPtr<nsIDOMDocument> doc = do_QueryReferent(mDocWeak);
|
|
|
|
if (!doc) return NS_ERROR_NOT_INITIALIZED;
|
2002-01-14 20:31:43 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNodeList>nodeList;
|
|
|
|
res = doc->GetElementsByTagName(NS_LITERAL_STRING("head"), getter_AddRefs(nodeList));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!nodeList) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
PRUint32 count;
|
|
|
|
nodeList->GetLength(&count);
|
|
|
|
if (count < 1) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> headNode;
|
|
|
|
res = nodeList->Item(0, getter_AddRefs(headNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!headNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// First, make sure there are no return chars in the source.
|
|
|
|
// Bad things happen if you insert returns (instead of dom newlines, \n)
|
|
|
|
// into an editor document.
|
|
|
|
nsAutoString inputString (aSourceToInsert); // hope this does copy-on-write
|
|
|
|
|
|
|
|
// Windows linebreaks: Map CRLF to LF:
|
2001-11-14 01:33:42 +00:00
|
|
|
inputString.ReplaceSubstring(NS_LITERAL_STRING("\r\n").get(),
|
|
|
|
NS_LITERAL_STRING("\n").get());
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// Mac linebreaks: Map any remaining CR to LF:
|
2001-11-14 01:33:42 +00:00
|
|
|
inputString.ReplaceSubstring(NS_LITERAL_STRING("\r").get(),
|
|
|
|
NS_LITERAL_STRING("\n").get());
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// Get the first range in the selection, for context:
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
res = selection->GetRangeAt(0, getter_AddRefs(range));
|
|
|
|
if (NS_FAILED(res))
|
|
|
|
return res;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNSRange> nsrange (do_QueryInterface(range));
|
|
|
|
if (!nsrange)
|
|
|
|
return NS_ERROR_NO_INTERFACE;
|
|
|
|
nsCOMPtr<nsIDOMDocumentFragment> docfrag;
|
|
|
|
res = nsrange->CreateContextualFragment(inputString,
|
|
|
|
getter_AddRefs(docfrag));
|
|
|
|
|
|
|
|
//XXXX BUG 50965: This is not returning the text between <title> ... </title>
|
|
|
|
// Special code is needed in JS to handle title anyway, so it really doesn't matter!
|
2000-04-14 03:19:31 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res))
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Couldn't create contextual fragment: error was %d\n", res);
|
|
|
|
#endif
|
|
|
|
return res;
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!docfrag) return NS_ERROR_NULL_POINTER;
|
2000-04-14 03:19:31 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
|
|
|
|
// First delete all children in head
|
|
|
|
do {
|
|
|
|
res = headNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (child)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
res = DeleteNode(child);
|
|
|
|
if (NS_FAILED(res)) return res;
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
} while (child);
|
|
|
|
|
|
|
|
// Now insert the new nodes
|
|
|
|
PRInt32 offsetOfNewNode = 0;
|
|
|
|
nsCOMPtr<nsIDOMNode> fragmentAsNode (do_QueryInterface(docfrag));
|
|
|
|
|
|
|
|
// Loop over the contents of the fragment and move into the document
|
|
|
|
do {
|
|
|
|
res = fragmentAsNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (child)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
res = InsertNode(child, headNode, offsetOfNewNode++);
|
|
|
|
if (NS_FAILED(res)) return res;
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
} while (child);
|
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::RebuildDocumentFromSource(const nsAString& aSourceString)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
ForceCompositionEnd();
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> bodyElement;
|
|
|
|
res = GetRootElement(getter_AddRefs(bodyElement));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!bodyElement) return NS_ERROR_NULL_POINTER;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Find where the <body> tag starts.
|
|
|
|
// If user mangled that, then abort
|
2001-04-07 00:45:26 +00:00
|
|
|
nsReadingIterator<PRUnichar> beginbody;
|
|
|
|
nsReadingIterator<PRUnichar> endbody;
|
|
|
|
aSourceString.BeginReading(beginbody);
|
|
|
|
aSourceString.EndReading(endbody);
|
2002-11-26 22:15:30 +00:00
|
|
|
if (!CaseInsensitiveFindInReadable(NS_LITERAL_STRING("<body"),
|
|
|
|
beginbody, endbody))
|
2001-04-07 00:45:26 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-04-07 00:45:26 +00:00
|
|
|
nsReadingIterator<PRUnichar> beginhead;
|
|
|
|
nsReadingIterator<PRUnichar> endhead;
|
|
|
|
aSourceString.BeginReading(beginhead);
|
|
|
|
aSourceString.EndReading(endhead);
|
2002-11-26 22:15:30 +00:00
|
|
|
if (!CaseInsensitiveFindInReadable(NS_LITERAL_STRING("<head"),
|
|
|
|
beginhead, endhead))
|
2001-04-07 00:45:26 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-04-07 00:45:26 +00:00
|
|
|
nsReadingIterator<PRUnichar> beginclosehead;
|
|
|
|
nsReadingIterator<PRUnichar> endclosehead;
|
|
|
|
aSourceString.BeginReading(beginclosehead);
|
|
|
|
aSourceString.EndReading(endclosehead);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-04-07 00:45:26 +00:00
|
|
|
// Find the index after "<head>"
|
2002-11-26 22:15:30 +00:00
|
|
|
if (!CaseInsensitiveFindInReadable(NS_LITERAL_STRING("</head"),
|
|
|
|
beginclosehead, endclosehead))
|
2001-04-07 00:45:26 +00:00
|
|
|
beginclosehead = beginbody;
|
2001-01-28 20:13:07 +00:00
|
|
|
// We'll be forgiving and assume head ends before body
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Time to change the document
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Try to replace body contents first
|
|
|
|
res = SelectAll();
|
2000-03-24 00:26:47 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2001-04-07 00:45:26 +00:00
|
|
|
nsReadingIterator<PRUnichar> endtotal;
|
|
|
|
aSourceString.EndReading(endtotal);
|
|
|
|
|
2001-10-22 06:22:52 +00:00
|
|
|
res = LoadHTML(Substring(beginbody,endtotal));
|
2000-03-24 00:26:47 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2001-01-28 20:13:07 +00:00
|
|
|
selection->Collapse(bodyElement, 0);
|
|
|
|
|
2001-04-07 00:45:26 +00:00
|
|
|
res = ReplaceHeadContentsWithHTML(Substring(beginhead,beginclosehead));
|
2000-03-24 00:26:47 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// Now we must copy attributes user might have edited on the <body> tag
|
|
|
|
// because InsertHTML (actually, CreateContextualFragment())
|
|
|
|
// will never return a body node in the DOM fragment
|
2001-04-10 03:02:10 +00:00
|
|
|
|
|
|
|
// We already know where "<body" begins
|
|
|
|
nsReadingIterator<PRUnichar> beginclosebody = beginbody;
|
2001-04-07 00:45:26 +00:00
|
|
|
nsReadingIterator<PRUnichar> endclosebody;
|
|
|
|
aSourceString.EndReading(endclosebody);
|
|
|
|
if (!FindInReadable(NS_LITERAL_STRING(">"),beginclosebody,endclosebody))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsAutoString bodyTag(Substring(beginbody,endclosebody));//<bodyXXXX >
|
2001-01-28 20:13:07 +00:00
|
|
|
// Truncate at the end of the body tag
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Kludge of the year: fool the parser by replacing "body" with "div" so we get a node
|
2001-11-14 01:33:42 +00:00
|
|
|
bodyTag.ReplaceSubstring(NS_LITERAL_STRING("body").get(),
|
|
|
|
NS_LITERAL_STRING("div").get());
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
res = selection->GetRangeAt(0, getter_AddRefs(range));
|
2000-03-24 00:26:47 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNSRange> nsrange (do_QueryInterface(range));
|
|
|
|
if (!nsrange) return NS_ERROR_NO_INTERFACE;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMDocumentFragment> docfrag;
|
2001-04-07 00:45:26 +00:00
|
|
|
res = nsrange->CreateContextualFragment(bodyTag, getter_AddRefs(docfrag));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
1999-03-01 19:54:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> fragmentAsNode (do_QueryInterface(docfrag));
|
|
|
|
if (!fragmentAsNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
res = fragmentAsNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!child) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// Copy all attributes from the div child to current body element
|
2002-10-08 22:53:55 +00:00
|
|
|
res = CloneAttributes(bodyElement, child);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// place selection at first editable content
|
|
|
|
return BeginningOfDocument();
|
fixes:
14753, 29843, 39864, 40141,
40139, 36679, 39542, 34729,
34855, 37216, 39292, 26447
r=sfraser,cmanske,fm; a=beppe
2000-05-24 23:00:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-15 20:53:15 +00:00
|
|
|
void
|
|
|
|
nsHTMLEditor::NormalizeEOLInsertPosition(nsIDOMNode *firstNodeToInsert,
|
|
|
|
nsCOMPtr<nsIDOMNode> *insertParentNode,
|
|
|
|
PRInt32 *insertOffset)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
This function will either correct the position passed in,
|
|
|
|
or leave the position unchanged.
|
|
|
|
|
|
|
|
When the (first) item to insert is a block level element,
|
|
|
|
and our insertion position is after the last visible item in a line,
|
|
|
|
i.e. the insertion position is just before a visible line break <br>,
|
|
|
|
we want to skip to the position just after the line break (see bug 68767)
|
|
|
|
|
|
|
|
However, our logic to detect whether we should skip or not
|
|
|
|
needs to be more clever.
|
|
|
|
We must not skip when the caret appears to be positioned at the beginning
|
|
|
|
of a block, in that case skipping the <br> would not insert the <br>
|
|
|
|
at the caret position, but after the current empty line.
|
|
|
|
|
|
|
|
So we have several cases to test:
|
|
|
|
|
|
|
|
1) We only ever want to skip, if the next visible thing after the current position is a break
|
|
|
|
|
|
|
|
2) We do not want to skip if there is no previous visible thing at all
|
|
|
|
That is detected if the call to PriorVisibleNode gives us an offset of zero.
|
|
|
|
Because PriorVisibleNode always positions after the prior node, we would
|
|
|
|
see an offset > 0, if there were a prior node.
|
|
|
|
|
|
|
|
3) We do not want to skip, if both the next and the previous visible things are breaks.
|
|
|
|
|
|
|
|
4) We do not want to skip if the previous visible thing is in a different block
|
|
|
|
than the insertion position.
|
|
|
|
*/
|
|
|
|
|
|
|
|
if (!IsBlockNode(firstNodeToInsert))
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsWSRunObject wsObj(this, *insertParentNode, *insertOffset);
|
|
|
|
nsCOMPtr<nsIDOMNode> nextVisNode;
|
|
|
|
nsCOMPtr<nsIDOMNode> prevVisNode;
|
|
|
|
PRInt32 nextVisOffset=0;
|
|
|
|
PRInt16 nextVisType=0;
|
|
|
|
PRInt32 prevVisOffset=0;
|
|
|
|
PRInt16 prevVisType=0;
|
|
|
|
|
|
|
|
wsObj.NextVisibleNode(*insertParentNode, *insertOffset, address_of(nextVisNode), &nextVisOffset, &nextVisType);
|
|
|
|
if (!nextVisNode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (! (nextVisType & nsWSRunObject::eBreak))
|
|
|
|
return;
|
|
|
|
|
|
|
|
wsObj.PriorVisibleNode(*insertParentNode, *insertOffset, address_of(prevVisNode), &prevVisOffset, &prevVisType);
|
|
|
|
if (!prevVisNode)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (prevVisType & nsWSRunObject::eBreak)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (prevVisType & nsWSRunObject::eThisBlock)
|
|
|
|
return;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> brNode;
|
|
|
|
PRInt32 brOffset=0;
|
|
|
|
|
|
|
|
GetNodeLocation(nextVisNode, address_of(brNode), &brOffset);
|
|
|
|
|
|
|
|
*insertParentNode = brNode;
|
|
|
|
*insertOffset = brOffset + 1;
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::InsertElementAtSelection(nsIDOMElement* aElement, PRBool aDeleteSelection)
|
1999-03-01 19:54:47 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res = NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
if (!aElement)
|
1999-08-09 01:37:50 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aElement);
|
|
|
|
|
|
|
|
ForceCompositionEnd();
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpInsertElement, nsIEditor::eNext);
|
|
|
|
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelection>selection;
|
2001-01-28 20:13:07 +00:00
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2001-11-19 06:45:00 +00:00
|
|
|
if (NS_FAILED(res) || !selection)
|
2001-01-28 20:13:07 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// hand off to the rules system, see if it has anything to say about this
|
|
|
|
PRBool cancel, handled;
|
|
|
|
nsTextRulesInfo ruleInfo(nsTextEditRules::kInsertElement);
|
|
|
|
ruleInfo.insertElement = aElement;
|
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || (NS_FAILED(res))) return res;
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!handled)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if (aDeleteSelection)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tempNode;
|
|
|
|
PRInt32 tempOffset;
|
|
|
|
nsresult result = DeleteSelectionAndPrepareToCreateNode(tempNode,tempOffset);
|
2001-11-19 06:45:00 +00:00
|
|
|
if (NS_FAILED(result))
|
2001-01-28 20:13:07 +00:00
|
|
|
return result;
|
|
|
|
}
|
2000-01-31 10:30:12 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// If deleting, selection will be collapsed.
|
|
|
|
// so if not, we collapse it
|
|
|
|
if (!aDeleteSelection)
|
2000-01-31 10:30:12 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Named Anchor is a special case,
|
|
|
|
// We collapse to insert element BEFORE the selection
|
|
|
|
// For all other tags, we insert AFTER the selection
|
|
|
|
if (nsHTMLEditUtils::IsNamedAnchor(node))
|
2000-01-31 10:30:12 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
selection->CollapseToStart();
|
|
|
|
} else {
|
|
|
|
selection->CollapseToEnd();
|
2000-01-31 10:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parentSelectedNode;
|
|
|
|
PRInt32 offsetForInsert;
|
|
|
|
res = selection->GetAnchorNode(getter_AddRefs(parentSelectedNode));
|
|
|
|
// XXX: ERROR_HANDLING bad XPCOM usage
|
|
|
|
if (NS_SUCCEEDED(res) && NS_SUCCEEDED(selection->GetAnchorOffset(&offsetForInsert)) && parentSelectedNode)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
#ifdef DEBUG_cmanske
|
1999-08-19 13:30:48 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString name;
|
|
|
|
parentSelectedNode->GetNodeName(name);
|
|
|
|
printf("InsertElement: Anchor node of selection: ");
|
2001-06-30 11:02:25 +00:00
|
|
|
wprintf(name.get());
|
2001-01-28 20:13:07 +00:00
|
|
|
printf(" Offset: %d\n", offsetForInsert);
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
#endif
|
1999-03-01 19:54:47 +00:00
|
|
|
|
2003-04-15 20:53:15 +00:00
|
|
|
// Adjust position based on the node we are going to insert.
|
|
|
|
NormalizeEOLInsertPosition(node, address_of(parentSelectedNode), &offsetForInsert);
|
|
|
|
|
2001-06-15 22:29:07 +00:00
|
|
|
res = InsertNodeAtPoint(node, address_of(parentSelectedNode), &offsetForInsert, PR_FALSE);
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
// Set caret after element, but check for special case
|
|
|
|
// of inserting table-related elements: set in first cell instead
|
|
|
|
if (!SetCaretInTableCell(aElement))
|
2001-06-26 23:13:01 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
res = SetCaretAfterElement(aElement);
|
2001-06-26 23:13:01 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
// check for inserting a whole table at the end of a block. If so insert a br after it.
|
|
|
|
if (nsHTMLEditUtils::IsTable(node))
|
|
|
|
{
|
|
|
|
PRBool isLast;
|
|
|
|
res = IsLastEditableChild(node, &isLast);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (isLast)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> brNode;
|
|
|
|
res = CreateBR(parentSelectedNode, offsetForInsert+1, address_of(brNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
2001-09-14 21:12:35 +00:00
|
|
|
selection->Collapse(parentSelectedNode, offsetForInsert+1);
|
2001-06-26 23:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
2000-04-13 21:50:19 +00:00
|
|
|
}
|
|
|
|
|
2001-06-15 22:29:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
InsertNodeAtPoint: attempts to insert aNode into the document, at a point specified by
|
|
|
|
{*ioParent,*ioOffset}. Checks with strict dtd to see if containment is allowed. If not
|
|
|
|
allowed, will attempt to find a parent in the parent heirarchy of *ioParent that will
|
|
|
|
accept aNode as a child. If such a parent is found, will split the document tree from
|
|
|
|
{*ioParent,*ioOffset} up to parent, and then insert aNode. ioParent & ioOffset are then
|
|
|
|
adjusted to point to the actual location that aNode was inserted at. aNoEmptyNodes
|
|
|
|
specifies if the splitting process is allowed to reslt in empty nodes.
|
|
|
|
nsIDOMNode *aNode node to insert
|
|
|
|
nsCOMPtr<nsIDOMNode> *ioParent insertion parent
|
|
|
|
PRInt32 *ioOffset insertion offset
|
|
|
|
PRBool aNoEmptyNodes splitting can result in empty nodes?
|
|
|
|
*/
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::InsertNodeAtPoint(nsIDOMNode *aNode,
|
2001-06-15 22:29:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> *ioParent,
|
|
|
|
PRInt32 *ioOffset,
|
2001-01-28 20:13:07 +00:00
|
|
|
PRBool aNoEmptyNodes)
|
1999-05-26 21:40:51 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
|
2001-06-15 22:29:07 +00:00
|
|
|
NS_ENSURE_TRUE(ioParent, NS_ERROR_NULL_POINTER);
|
|
|
|
NS_ENSURE_TRUE(*ioParent, NS_ERROR_NULL_POINTER);
|
|
|
|
NS_ENSURE_TRUE(ioOffset, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
nsAutoString tagName;
|
|
|
|
aNode->GetNodeName(tagName);
|
2001-12-17 07:14:49 +00:00
|
|
|
ToLowerCase(tagName);
|
2001-06-15 22:29:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parent = *ioParent;
|
|
|
|
nsCOMPtr<nsIDOMNode> topChild = *ioParent;
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
2001-06-15 22:29:07 +00:00
|
|
|
PRInt32 offsetOfInsert = *ioOffset;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// Search up the parent chain to find a suitable container
|
|
|
|
while (!CanContainTag(parent, tagName))
|
|
|
|
{
|
|
|
|
// If the current parent is a root (body or table element)
|
|
|
|
// then go no further - we can't insert
|
2001-04-05 23:48:01 +00:00
|
|
|
if (nsTextEditUtils::IsBody(parent) || nsHTMLEditUtils::IsTableElement(parent))
|
2001-01-28 20:13:07 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
// Get the next parent
|
|
|
|
parent->GetParentNode(getter_AddRefs(tmp));
|
|
|
|
NS_ENSURE_TRUE(tmp, NS_ERROR_FAILURE);
|
|
|
|
topChild = parent;
|
|
|
|
parent = tmp;
|
|
|
|
}
|
|
|
|
if (parent != topChild)
|
|
|
|
{
|
|
|
|
// we need to split some levels above the original selection parent
|
2001-06-15 22:29:07 +00:00
|
|
|
res = SplitNodeDeep(topChild, *ioParent, *ioOffset, &offsetOfInsert, aNoEmptyNodes);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res))
|
|
|
|
return res;
|
2001-06-15 22:29:07 +00:00
|
|
|
*ioParent = parent;
|
|
|
|
*ioOffset = offsetOfInsert;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
// Now we can insert the new node
|
|
|
|
res = InsertNode(aNode, parent, offsetOfInsert);
|
|
|
|
return res;
|
2000-04-13 21:50:19 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::SelectElement(nsIDOMElement* aElement)
|
2000-04-13 21:50:19 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res = NS_ERROR_NULL_POINTER;
|
2000-04-14 03:19:31 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Must be sure that element is contained in the document body
|
|
|
|
if (IsElementInBody(aElement))
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2000-03-29 12:53:23 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsCOMPtr<nsIDOMNode>parent;
|
|
|
|
res = aElement->GetParentNode(getter_AddRefs(parent));
|
|
|
|
if (NS_SUCCEEDED(res) && parent)
|
1999-08-19 13:30:48 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
PRInt32 offsetInParent;
|
|
|
|
res = GetChildOffset(aElement, parent, offsetInParent);
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
// Collapse selection to just before desired element,
|
|
|
|
res = selection->Collapse(parent, offsetInParent);
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
// then extend it to just after
|
|
|
|
res = selection->Extend(parent, offsetInParent+1);
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
1999-08-19 13:30:48 +00:00
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
1999-10-06 19:34:09 +00:00
|
|
|
}
|
2000-03-24 00:26:47 +00:00
|
|
|
return res;
|
1999-05-26 21:40:51 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::SetCaretAfterElement(nsIDOMElement* aElement)
|
1999-11-13 16:37:58 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res = NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// Be sure the element is contained in the document body
|
|
|
|
if (aElement && IsElementInBody(aElement))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsCOMPtr<nsIDOMNode>parent;
|
|
|
|
res = aElement->GetParentNode(getter_AddRefs(parent));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!parent) return NS_ERROR_NULL_POINTER;
|
|
|
|
PRInt32 offsetInParent;
|
|
|
|
res = GetChildOffset(aElement, parent, offsetInParent);
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
// Collapse selection to just after desired element,
|
|
|
|
res = selection->Collapse(parent, offsetInParent+1);
|
|
|
|
#if 0 //def DEBUG_cmanske
|
|
|
|
{
|
|
|
|
nsAutoString name;
|
|
|
|
parent->GetNodeName(name);
|
|
|
|
printf("SetCaretAfterElement: Parent node: ");
|
2001-06-30 11:02:25 +00:00
|
|
|
wprintf(name.get());
|
2001-01-28 20:13:07 +00:00
|
|
|
printf(" Offset: %d\n\nHTML:\n", offsetInParent+1);
|
|
|
|
nsAutoString Format("text/html");
|
|
|
|
nsAutoString ContentsAs;
|
2002-03-25 22:39:19 +00:00
|
|
|
OutputToString(Format, 2, ContentsAs);
|
2001-06-30 11:02:25 +00:00
|
|
|
wprintf(ContentsAs.get());
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
1999-11-13 16:37:58 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::SetParagraphFormat(const nsAString& aParagraphFormat)
|
1999-11-13 16:37:58 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString tag; tag.Assign(aParagraphFormat);
|
2001-12-17 07:14:49 +00:00
|
|
|
ToLowerCase(tag);
|
2001-10-13 00:16:32 +00:00
|
|
|
if (tag.Equals(NS_LITERAL_STRING("dd")) || tag.Equals(NS_LITERAL_STRING("dt")))
|
2001-01-28 20:13:07 +00:00
|
|
|
return MakeDefinitionItem(tag);
|
|
|
|
else
|
|
|
|
return InsertBasicBlock(tag);
|
1999-11-13 16:37:58 +00:00
|
|
|
}
|
1999-09-22 01:21:56 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// XXX: ERROR_HANDLING -- this method needs a little work to ensure all error codes are
|
|
|
|
// checked properly, all null pointers are checked, and no memory leaks occur
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetParentBlockTags(nsStringArray *aTagList, PRBool aGetLists)
|
1999-09-22 01:21:56 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!aTagList) { return NS_ERROR_NULL_POINTER; }
|
2000-03-29 12:53:23 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
|
|
|
|
|
|
|
|
// Find out if the selection is collapsed:
|
|
|
|
PRBool isCollapsed;
|
|
|
|
res = selection->GetIsCollapsed(&isCollapsed);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (isCollapsed)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> node, blockParent;
|
|
|
|
PRInt32 offset;
|
|
|
|
|
|
|
|
res = GetStartNodeAndOffset(selection, address_of(node), &offset);
|
|
|
|
if (!node) res = NS_ERROR_FAILURE;
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> blockParentElem;
|
|
|
|
if (aGetLists)
|
|
|
|
{
|
|
|
|
// Get the "ol", "ul", or "dl" parent element
|
2001-11-14 01:33:42 +00:00
|
|
|
res = GetElementOrParentByTagName(NS_LITERAL_STRING("list"), node, getter_AddRefs(blockParentElem));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2001-04-05 23:48:01 +00:00
|
|
|
PRBool isBlock (PR_FALSE);
|
2001-04-07 00:45:26 +00:00
|
|
|
NodeIsBlock(node, &isBlock);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (isBlock) blockParent = node;
|
2001-01-28 20:13:07 +00:00
|
|
|
else blockParent = GetBlockNodeParent(node);
|
|
|
|
blockParentElem = do_QueryInterface(blockParent);
|
|
|
|
}
|
|
|
|
if (blockParentElem)
|
|
|
|
{
|
|
|
|
nsAutoString blockParentTag;
|
|
|
|
blockParentElem->GetTagName(blockParentTag);
|
|
|
|
aTagList->AppendString(blockParentTag);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2000-03-29 12:53:23 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// else non-collapsed selection
|
2000-03-29 12:53:23 +00:00
|
|
|
nsCOMPtr<nsIEnumerator> enumerator;
|
2001-01-28 20:13:07 +00:00
|
|
|
res = selPriv->GetEnumerator(getter_AddRefs(enumerator));
|
|
|
|
if (NS_FAILED(res)) return res;
|
2000-03-29 12:53:23 +00:00
|
|
|
if (!enumerator) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
enumerator->First();
|
|
|
|
nsCOMPtr<nsISupports> currentItem;
|
2001-01-28 20:13:07 +00:00
|
|
|
res = enumerator->CurrentItem(getter_AddRefs(currentItem));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
//XXX: should be while loop?
|
|
|
|
if (currentItem)
|
2000-03-29 12:53:23 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMRange> range( do_QueryInterface(currentItem) );
|
2001-01-28 20:13:07 +00:00
|
|
|
// scan the range for all the independent block content blockSections
|
|
|
|
// and get the block parent of each
|
2002-11-12 19:40:11 +00:00
|
|
|
nsCOMArray<nsIDOMRange> blockSections;
|
2001-01-28 20:13:07 +00:00
|
|
|
res = GetBlockSectionsForRange(range, blockSections);
|
|
|
|
if (NS_SUCCEEDED(res))
|
2000-03-29 12:53:23 +00:00
|
|
|
{
|
2002-11-12 19:40:11 +00:00
|
|
|
nsCOMPtr<nsIDOMRange> subRange = blockSections[0];
|
2001-01-28 20:13:07 +00:00
|
|
|
while (subRange)
|
2000-03-29 12:53:23 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>startParent;
|
|
|
|
res = subRange->GetStartContainer(getter_AddRefs(startParent));
|
|
|
|
if (NS_SUCCEEDED(res) && startParent)
|
2000-03-29 12:53:23 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> blockParent;
|
|
|
|
if (aGetLists)
|
|
|
|
{
|
|
|
|
// Get the "ol", "ul", or "dl" parent element
|
2001-11-14 01:33:42 +00:00
|
|
|
res = GetElementOrParentByTagName(NS_LITERAL_STRING("list"), startParent, getter_AddRefs(blockParent));
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
blockParent = do_QueryInterface(GetBlockNodeParent(startParent));
|
|
|
|
}
|
|
|
|
if (NS_SUCCEEDED(res) && blockParent)
|
|
|
|
{
|
|
|
|
nsAutoString blockParentTag;
|
|
|
|
blockParent->GetTagName(blockParentTag);
|
|
|
|
PRBool isRoot;
|
|
|
|
IsRootTag(blockParentTag, isRoot);
|
|
|
|
if ((!isRoot) && (-1==aTagList->IndexOf(blockParentTag))) {
|
|
|
|
aTagList->AppendString(blockParentTag);
|
|
|
|
}
|
|
|
|
}
|
2000-03-29 12:53:23 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res))
|
2002-11-12 19:40:11 +00:00
|
|
|
return res;
|
|
|
|
blockSections.RemoveObject(0);
|
|
|
|
if (blockSections.Count() == 0)
|
|
|
|
break;
|
|
|
|
subRange = blockSections[0];
|
2000-03-29 12:53:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
return res;
|
1999-09-22 01:21:56 +00:00
|
|
|
}
|
|
|
|
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::GetParagraphState(PRBool *aMixed, nsAString &outFormat)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2001-04-07 00:45:26 +00:00
|
|
|
if (!aMixed) return NS_ERROR_NULL_POINTER;
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIHTMLEditRules> htmlRules = do_QueryInterface(mRules);
|
|
|
|
if (!htmlRules) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return htmlRules->GetParagraphState(aMixed, outFormat);
|
|
|
|
}
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::GetBackgroundColorState(PRBool *aMixed, nsAString &aOutColor)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
|
|
|
nsresult res;
|
|
|
|
PRBool useCSS;
|
2002-03-14 22:11:14 +00:00
|
|
|
GetIsCSSEnabled(&useCSS);
|
2002-01-09 13:51:37 +00:00
|
|
|
if (useCSS) {
|
|
|
|
// if we are in CSS mode, we have to check if the containing block defines
|
|
|
|
// a background color
|
|
|
|
res = GetCSSBackgroundColorState(aMixed, aOutColor, PR_TRUE);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// in HTML mode, we look only at page's background
|
|
|
|
res = GetHTMLBackgroundColorState(aMixed, aOutColor);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::GetHighlightColorState(PRBool *aMixed, nsAString &aOutColor)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
PRBool useCSS;
|
2002-03-14 22:11:14 +00:00
|
|
|
GetIsCSSEnabled(&useCSS);
|
2002-01-09 13:51:37 +00:00
|
|
|
*aMixed = PR_FALSE;
|
|
|
|
aOutColor.Assign(NS_LITERAL_STRING("transparent"));
|
|
|
|
if (useCSS) {
|
|
|
|
// in CSS mode, text background can be added by the Text Highlight button
|
|
|
|
// we need to query the background of the selection without looking for
|
|
|
|
// the block container of the ranges in the selection
|
|
|
|
res = GetCSSBackgroundColorState(aMixed, aOutColor, PR_FALSE);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetHighlightColor(PRBool *aMixed, PRUnichar **_retval)
|
|
|
|
{
|
|
|
|
if (!aMixed || !_retval) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsAutoString outColorString(NS_LITERAL_STRING("transparent"));
|
|
|
|
*aMixed = PR_FALSE;
|
|
|
|
|
|
|
|
nsresult err = NS_NOINTERFACE;
|
|
|
|
err = GetHighlightColorState(aMixed, outColorString);
|
|
|
|
*_retval = ToNewUnicode(outColorString);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::GetCSSBackgroundColorState(PRBool *aMixed, nsAString &aOutColor, PRBool aBlockLevel)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
|
|
|
if (!aMixed) return NS_ERROR_NULL_POINTER;
|
|
|
|
*aMixed = PR_FALSE;
|
|
|
|
// the default background color is transparent
|
|
|
|
aOutColor.Assign(NS_LITERAL_STRING("transparent"));
|
|
|
|
|
|
|
|
// get selection
|
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// get selection location
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
PRInt32 offset;
|
|
|
|
res = GetStartNodeAndOffset(selection, address_of(parent), &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// is the selection collapsed?
|
|
|
|
PRBool bCollapsed;
|
|
|
|
res = selection->GetIsCollapsed(&bCollapsed);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
nsCOMPtr<nsIDOMNode> nodeToExamine;
|
|
|
|
if (bCollapsed || IsTextNode(parent))
|
|
|
|
{
|
|
|
|
// we want to look at the parent and ancestors
|
|
|
|
nodeToExamine = parent;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// otherwise we want to look at the first editable node after
|
|
|
|
// {parent,offset} and it's ancestors for divs with alignment on them
|
|
|
|
nodeToExamine = GetChildAt(parent, offset);
|
|
|
|
//GetNextNode(parent, offset, PR_TRUE, address_of(nodeToExamine));
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!nodeToExamine) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// is the node to examine a block ?
|
|
|
|
PRBool isBlock;
|
|
|
|
res = NodeIsBlockStatic(nodeToExamine, &isBlock);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMHTMLHtmlElement> htmlElement;
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
|
|
|
|
if (aBlockLevel) {
|
|
|
|
// we are querying the block background (and not the text background), let's
|
|
|
|
// climb to the block container
|
|
|
|
nsCOMPtr<nsIDOMNode> blockParent = nodeToExamine;
|
|
|
|
if (!isBlock) {
|
|
|
|
blockParent = GetBlockNodeParent(nodeToExamine);
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
// retrieve the computed style of background-color for blockParent
|
2003-04-17 05:42:33 +00:00
|
|
|
mHTMLCSSUtils->GetComputedProperty(blockParent, nsEditProperty::cssBackgroundColor,
|
2002-01-09 13:51:37 +00:00
|
|
|
aOutColor);
|
|
|
|
tmp = blockParent;
|
|
|
|
res = tmp->GetParentNode(getter_AddRefs(blockParent));
|
|
|
|
htmlElement = do_QueryInterface(tmp);
|
|
|
|
// look at parent if the queried color is transparent and if the node to
|
|
|
|
// examine is not the root of the document
|
2002-09-20 08:57:04 +00:00
|
|
|
} while ( aOutColor.Equals(NS_LITERAL_STRING("transparent")) && htmlElement );
|
|
|
|
if (!htmlElement && aOutColor.Equals(NS_LITERAL_STRING("transparent"))) {
|
2002-01-09 13:51:37 +00:00
|
|
|
// we have hit the root of the document and the color is still transparent !
|
|
|
|
// Grumble... Let's look at the default background color because that's the
|
|
|
|
// color we are looking for
|
|
|
|
mHTMLCSSUtils->GetDefaultBackgroundColor(aOutColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// no, we are querying the text background for the Text Highlight button
|
|
|
|
if (IsTextNode(nodeToExamine)) {
|
|
|
|
// if the node of interest is a text node, let's climb a level
|
|
|
|
res = nodeToExamine->GetParentNode(getter_AddRefs(parent));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
nodeToExamine = parent;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
// is the node to examine a block ?
|
|
|
|
res = NodeIsBlockStatic(nodeToExamine, &isBlock);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (isBlock) {
|
|
|
|
// yes it is a block; in that case, the text background color is transparent
|
|
|
|
aOutColor.Assign(NS_LITERAL_STRING("transparent"));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// no, it's not; let's retrieve the computed style of background-color for the
|
|
|
|
// node to examine
|
2003-04-17 05:42:33 +00:00
|
|
|
mHTMLCSSUtils->GetComputedProperty(nodeToExamine, nsEditProperty::cssBackgroundColor,
|
2002-01-09 13:51:37 +00:00
|
|
|
aOutColor);
|
|
|
|
if (!aOutColor.Equals(NS_LITERAL_STRING("transparent"))) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = nodeToExamine->GetParentNode(getter_AddRefs(tmp));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
nodeToExamine = tmp;
|
|
|
|
htmlElement = do_QueryInterface(tmp);
|
2002-09-20 08:57:04 +00:00
|
|
|
} while ( aOutColor.Equals(NS_LITERAL_STRING("transparent")) && htmlElement );
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::GetHTMLBackgroundColorState(PRBool *aMixed, nsAString &aOutColor)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
//TODO: We don't handle "mixed" correctly!
|
2001-04-07 00:45:26 +00:00
|
|
|
if (!aMixed) return NS_ERROR_NULL_POINTER;
|
|
|
|
*aMixed = PR_FALSE;
|
|
|
|
aOutColor.Assign(NS_LITERAL_STRING(""));
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> element;
|
|
|
|
PRInt32 selectedCount;
|
|
|
|
nsAutoString tagName;
|
2002-09-13 04:19:30 +00:00
|
|
|
nsresult res = GetSelectedOrParentTableElement(tagName,
|
|
|
|
&selectedCount,
|
|
|
|
getter_AddRefs(element));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2002-01-14 20:31:43 +00:00
|
|
|
NS_NAMED_LITERAL_STRING(styleName, "bgcolor");
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
while (element)
|
|
|
|
{
|
|
|
|
// We are in a cell or selected table
|
|
|
|
res = element->GetAttribute(styleName, aOutColor);
|
|
|
|
if (NS_FAILED(res)) return res;
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Done if we have a color explicitly set
|
2003-05-23 21:34:47 +00:00
|
|
|
if (!aOutColor.IsEmpty())
|
2001-01-28 20:13:07 +00:00
|
|
|
return NS_OK;
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Once we hit the body, we're done
|
2001-04-05 23:48:01 +00:00
|
|
|
if(nsTextEditUtils::IsBody(element)) return NS_OK;
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// No color is set, but we need to report visible color inherited
|
|
|
|
// from nested cells/tables, so search up parent chain
|
|
|
|
nsCOMPtr<nsIDOMNode> parentNode;
|
|
|
|
res = element->GetParentNode(getter_AddRefs(parentNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
element = do_QueryInterface(parentNode);
|
|
|
|
}
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// If no table or cell found, get page body
|
|
|
|
res = nsEditor::GetRootElement(getter_AddRefs(element));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!element) return NS_ERROR_NULL_POINTER;
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
return element->GetAttribute(styleName, aOutColor);
|
|
|
|
}
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2001-04-07 00:45:26 +00:00
|
|
|
nsHTMLEditor::GetListState(PRBool *aMixed, PRBool *aOL, PRBool *aUL, PRBool *aDL)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2001-04-07 00:45:26 +00:00
|
|
|
if (!aMixed || !aOL || !aUL || !aDL) return NS_ERROR_NULL_POINTER;
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIHTMLEditRules> htmlRules = do_QueryInterface(mRules);
|
|
|
|
if (!htmlRules) return NS_ERROR_FAILURE;
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
return htmlRules->GetListState(aMixed, aOL, aUL, aDL);
|
|
|
|
}
|
|
|
|
|
2000-05-04 14:02:03 +00:00
|
|
|
NS_IMETHODIMP
|
2001-04-07 00:45:26 +00:00
|
|
|
nsHTMLEditor::GetListItemState(PRBool *aMixed, PRBool *aLI, PRBool *aDT, PRBool *aDD)
|
2000-05-04 14:02:03 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2001-04-07 00:45:26 +00:00
|
|
|
if (!aMixed || !aLI || !aDT || !aDD) return NS_ERROR_NULL_POINTER;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIHTMLEditRules> htmlRules = do_QueryInterface(mRules);
|
|
|
|
if (!htmlRules) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return htmlRules->GetListItemState(aMixed, aLI, aDT, aDD);
|
2000-05-04 14:02:03 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2001-04-07 00:45:26 +00:00
|
|
|
nsHTMLEditor::GetAlignment(PRBool *aMixed, nsIHTMLEditor::EAlignment *aAlign)
|
2000-04-04 14:51:26 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2001-04-07 00:45:26 +00:00
|
|
|
if (!aMixed || !aAlign) return NS_ERROR_NULL_POINTER;
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIHTMLEditRules> htmlRules = do_QueryInterface(mRules);
|
|
|
|
if (!htmlRules) return NS_ERROR_FAILURE;
|
2000-04-04 14:51:26 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
return htmlRules->GetAlignment(aMixed, aAlign);
|
|
|
|
}
|
2000-04-04 14:51:26 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2001-04-07 00:45:26 +00:00
|
|
|
nsHTMLEditor::GetIndentState(PRBool *aCanIndent, PRBool *aCanOutdent)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2001-04-07 00:45:26 +00:00
|
|
|
if (!aCanIndent || !aCanOutdent) return NS_ERROR_NULL_POINTER;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIHTMLEditRules> htmlRules = do_QueryInterface(mRules);
|
|
|
|
if (!htmlRules) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return htmlRules->GetIndentState(aCanIndent, aCanOutdent);
|
2000-04-04 14:51:26 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::MakeOrChangeList(const nsAString& aListType, PRBool entireList, const nsAString& aBulletType)
|
1999-05-26 21:40:51 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res;
|
1999-08-09 01:37:50 +00:00
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
1999-05-26 21:40:51 +00:00
|
|
|
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
1999-10-06 19:34:09 +00:00
|
|
|
PRBool cancel, handled;
|
1999-05-26 21:40:51 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpMakeList, nsIEditor::eNext);
|
|
|
|
|
|
|
|
// pre-process
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsTextRulesInfo ruleInfo(nsTextEditRules::kMakeList);
|
|
|
|
ruleInfo.blockType = &aListType;
|
|
|
|
ruleInfo.entireList = entireList;
|
2001-12-12 03:57:32 +00:00
|
|
|
ruleInfo.bulletType = &aBulletType;
|
2001-01-28 20:13:07 +00:00
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || (NS_FAILED(res))) return res;
|
2000-05-31 00:03:02 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!handled)
|
1999-12-14 23:07:12 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Find out if the selection is collapsed:
|
|
|
|
PRBool isCollapsed;
|
|
|
|
res = selection->GetIsCollapsed(&isCollapsed);
|
|
|
|
if (NS_FAILED(res)) return res;
|
1999-12-14 23:07:12 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
PRInt32 offset;
|
|
|
|
|
|
|
|
res = GetStartNodeAndOffset(selection, address_of(node), &offset);
|
|
|
|
if (!node) res = NS_ERROR_FAILURE;
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (isCollapsed)
|
1999-12-14 23:07:12 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// have to find a place to put the list
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = node;
|
|
|
|
nsCOMPtr<nsIDOMNode> topChild = node;
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
|
|
|
|
while ( !CanContainTag(parent, aListType))
|
|
|
|
{
|
|
|
|
parent->GetParentNode(getter_AddRefs(tmp));
|
|
|
|
if (!tmp) return NS_ERROR_FAILURE;
|
|
|
|
topChild = parent;
|
|
|
|
parent = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent != node)
|
|
|
|
{
|
|
|
|
// we need to split up to the child of parent
|
|
|
|
res = SplitNodeDeep(topChild, node, offset, &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a list
|
|
|
|
nsCOMPtr<nsIDOMNode> newList;
|
|
|
|
res = CreateNode(aListType, parent, offset, getter_AddRefs(newList));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
// make a list item
|
|
|
|
nsCOMPtr<nsIDOMNode> newItem;
|
2002-01-14 20:31:43 +00:00
|
|
|
res = CreateNode(NS_LITERAL_STRING("li"), newList, 0, getter_AddRefs(newItem));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = selection->Collapse(newItem,0);
|
|
|
|
if (NS_FAILED(res)) return res;
|
1999-12-14 23:07:12 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::RemoveList(const nsAString& aListType)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
nsresult res;
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
PRBool cancel, handled;
|
1999-12-14 23:07:12 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpRemoveList, nsIEditor::eNext);
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
// pre-process
|
2001-01-28 20:13:07 +00:00
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
1999-08-19 13:30:48 +00:00
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsTextRulesInfo ruleInfo(nsTextEditRules::kRemoveList);
|
2002-02-10 15:49:46 +00:00
|
|
|
if (aListType.Equals(NS_LITERAL_STRING("ol"),nsCaseInsensitiveStringComparator()))
|
2001-04-18 06:17:05 +00:00
|
|
|
ruleInfo.bOrdered = PR_TRUE;
|
2001-01-28 20:13:07 +00:00
|
|
|
else ruleInfo.bOrdered = PR_FALSE;
|
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || (NS_FAILED(res))) return res;
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// no default behavior for this yet. what would it mean?
|
|
|
|
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
1999-05-26 21:40:51 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::MakeDefinitionItem(const nsAString& aItemType)
|
1999-03-01 19:54:47 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res;
|
1999-03-29 08:02:05 +00:00
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
1999-10-06 19:34:09 +00:00
|
|
|
PRBool cancel, handled;
|
1999-03-29 08:02:05 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpMakeDefListItem, nsIEditor::eNext);
|
|
|
|
|
1999-03-29 08:02:05 +00:00
|
|
|
// pre-process
|
2001-01-28 20:13:07 +00:00
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
1999-08-19 13:30:48 +00:00
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
2001-01-28 20:13:07 +00:00
|
|
|
nsTextRulesInfo ruleInfo(nsTextEditRules::kMakeDefListItem);
|
|
|
|
ruleInfo.blockType = &aItemType;
|
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || (NS_FAILED(res))) return res;
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!handled)
|
1999-10-06 19:34:09 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// todo: no default for now. we count on rules to handle it.
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
2000-10-11 22:50:14 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::InsertBasicBlock(const nsAString& aBlockType)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res;
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
PRBool cancel, handled;
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpMakeBasicBlock, nsIEditor::eNext);
|
|
|
|
|
|
|
|
// pre-process
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsTextRulesInfo ruleInfo(nsTextEditRules::kMakeBasicBlock);
|
|
|
|
ruleInfo.blockType = &aBlockType;
|
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || (NS_FAILED(res))) return res;
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!handled)
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Find out if the selection is collapsed:
|
|
|
|
PRBool isCollapsed;
|
|
|
|
res = selection->GetIsCollapsed(&isCollapsed);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
PRInt32 offset;
|
|
|
|
|
|
|
|
res = GetStartNodeAndOffset(selection, address_of(node), &offset);
|
|
|
|
if (!node) res = NS_ERROR_FAILURE;
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (isCollapsed)
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// have to find a place to put the block
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = node;
|
|
|
|
nsCOMPtr<nsIDOMNode> topChild = node;
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
|
|
|
|
while ( !CanContainTag(parent, aBlockType))
|
|
|
|
{
|
|
|
|
parent->GetParentNode(getter_AddRefs(tmp));
|
|
|
|
if (!tmp) return NS_ERROR_FAILURE;
|
|
|
|
topChild = parent;
|
|
|
|
parent = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent != node)
|
|
|
|
{
|
|
|
|
// we need to split up to the child of parent
|
|
|
|
res = SplitNodeDeep(topChild, node, offset, &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// make a block
|
|
|
|
nsCOMPtr<nsIDOMNode> newBlock;
|
|
|
|
res = CreateNode(aBlockType, parent, offset, getter_AddRefs(newBlock));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// reposition selection to inside the block
|
|
|
|
res = selection->Collapse(newBlock,0);
|
|
|
|
if (NS_FAILED(res)) return res;
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
2000-12-06 23:11:21 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::Indent(const nsAString& aIndent)
|
2000-12-06 23:11:21 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res;
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
PRBool cancel, handled;
|
|
|
|
PRInt32 theAction = nsTextEditRules::kIndent;
|
|
|
|
PRInt32 opID = kOpIndent;
|
2002-02-10 15:49:46 +00:00
|
|
|
if (aIndent.Equals(NS_LITERAL_STRING("outdent"),nsCaseInsensitiveStringComparator()))
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
theAction = nsTextEditRules::kOutdent;
|
|
|
|
opID = kOpOutdent;
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, opID, nsIEditor::eNext);
|
|
|
|
|
|
|
|
// pre-process
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsTextRulesInfo ruleInfo(theAction);
|
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || (NS_FAILED(res))) return res;
|
|
|
|
|
|
|
|
if (!handled)
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Do default - insert a blockquote node if selection collapsed
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
PRInt32 offset;
|
|
|
|
PRBool isCollapsed;
|
|
|
|
res = selection->GetIsCollapsed(&isCollapsed);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
res = GetStartNodeAndOffset(selection, address_of(node), &offset);
|
|
|
|
if (!node) res = NS_ERROR_FAILURE;
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
2002-01-14 20:31:43 +00:00
|
|
|
if (aIndent == NS_LITERAL_STRING("indent"))
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
if (isCollapsed)
|
|
|
|
{
|
|
|
|
// have to find a place to put the blockquote
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = node;
|
|
|
|
nsCOMPtr<nsIDOMNode> topChild = node;
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
2002-01-14 20:31:43 +00:00
|
|
|
NS_NAMED_LITERAL_STRING(bq, "blockquote");
|
2001-01-28 20:13:07 +00:00
|
|
|
while ( !CanContainTag(parent, bq))
|
|
|
|
{
|
|
|
|
parent->GetParentNode(getter_AddRefs(tmp));
|
|
|
|
if (!tmp) return NS_ERROR_FAILURE;
|
|
|
|
topChild = parent;
|
|
|
|
parent = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent != node)
|
|
|
|
{
|
|
|
|
// we need to split up to the child of parent
|
|
|
|
res = SplitNodeDeep(topChild, node, offset, &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// make a blockquote
|
|
|
|
nsCOMPtr<nsIDOMNode> newBQ;
|
|
|
|
res = CreateNode(bq, parent, offset, getter_AddRefs(newBQ));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
// put a space in it so layout will draw the list item
|
|
|
|
res = selection->Collapse(newBQ,0);
|
|
|
|
if (NS_FAILED(res)) return res;
|
2001-04-07 00:45:26 +00:00
|
|
|
res = InsertText(NS_LITERAL_STRING(" "));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
// reposition selection to before the space character
|
|
|
|
res = GetStartNodeAndOffset(selection, address_of(node), &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = selection->Collapse(node,0);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
2000-12-06 23:11:21 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
//TODO: IMPLEMENT ALIGNMENT!
|
2000-12-06 23:11:21 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::Align(const nsAString& aAlignType)
|
2000-12-06 23:11:21 +00:00
|
|
|
{
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoRules beginRulesSniffing(this, kOpAlign, nsIEditor::eNext);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
PRBool cancel, handled;
|
2000-12-06 23:11:21 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Find out if the selection is collapsed:
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
2000-12-06 23:11:21 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsTextRulesInfo ruleInfo(nsTextEditRules::kAlign);
|
|
|
|
ruleInfo.alignType = &aAlignType;
|
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || NS_FAILED(res))
|
2000-12-06 23:11:21 +00:00
|
|
|
return res;
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
|
|
|
}
|
2000-05-05 20:42:36 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::GetElementOrParentByTagName(const nsAString& aTagName, nsIDOMNode *aNode, nsIDOMElement** aReturn)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2003-05-23 21:34:47 +00:00
|
|
|
if (aTagName.IsEmpty() || !aReturn )
|
2001-01-28 20:13:07 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
nsCOMPtr<nsIDOMNode> currentNode;
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (aNode)
|
|
|
|
currentNode = aNode;
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
else
|
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// If no node supplied, get it from anchor node of current selection
|
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2000-05-05 20:42:36 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> anchorNode;
|
|
|
|
res = selection->GetAnchorNode(getter_AddRefs(anchorNode));
|
|
|
|
if(NS_FAILED(res)) return res;
|
|
|
|
if (!anchorNode) return NS_ERROR_FAILURE;
|
1999-11-29 08:28:46 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Try to get the actual selected node
|
|
|
|
PRBool hasChildren = PR_FALSE;
|
|
|
|
anchorNode->HasChildNodes(&hasChildren);
|
|
|
|
if (hasChildren)
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
PRInt32 offset;
|
|
|
|
res = selection->GetAnchorOffset(&offset);
|
|
|
|
if(NS_FAILED(res)) return res;
|
|
|
|
currentNode = nsEditor::GetChildAt(anchorNode, offset);
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
// anchor node is probably a text node - just use that
|
|
|
|
if (!currentNode)
|
|
|
|
currentNode = anchorNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString TagName(aTagName);
|
2001-12-17 07:14:49 +00:00
|
|
|
ToLowerCase(TagName);
|
2001-01-28 20:13:07 +00:00
|
|
|
PRBool getLink = IsLinkTag(TagName);
|
|
|
|
PRBool getNamedAnchor = IsNamedAnchorTag(TagName);
|
|
|
|
if ( getLink || getNamedAnchor)
|
|
|
|
{
|
2001-10-13 00:16:32 +00:00
|
|
|
TagName.Assign(NS_LITERAL_STRING("a"));
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2001-10-13 00:16:32 +00:00
|
|
|
PRBool findTableCell = TagName.Equals(NS_LITERAL_STRING("td"));
|
|
|
|
PRBool findList = TagName.Equals(NS_LITERAL_STRING("list"));
|
2000-10-11 22:50:14 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// default is null - no element found
|
|
|
|
*aReturn = nsnull;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
PRBool bNodeFound = PR_FALSE;
|
|
|
|
|
|
|
|
while (PR_TRUE)
|
|
|
|
{
|
|
|
|
nsAutoString currentTagName;
|
|
|
|
// Test if we have a link (an anchor with href set)
|
|
|
|
if ( (getLink && nsHTMLEditUtils::IsLink(currentNode)) ||
|
|
|
|
(getNamedAnchor && nsHTMLEditUtils::IsNamedAnchor(currentNode)) )
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
bNodeFound = PR_TRUE;
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (findList)
|
2000-10-11 22:50:14 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Match "ol", "ul", or "dl" for lists
|
|
|
|
if (nsHTMLEditUtils::IsList(currentNode))
|
|
|
|
goto NODE_FOUND;
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
} else if (findTableCell)
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Table cells are another special case:
|
|
|
|
// Match either "td" or "th" for them
|
|
|
|
if (nsHTMLEditUtils::IsTableCell(currentNode))
|
|
|
|
goto NODE_FOUND;
|
|
|
|
|
|
|
|
} else {
|
|
|
|
currentNode->GetNodeName(currentTagName);
|
2002-02-19 22:49:12 +00:00
|
|
|
if (currentTagName.Equals(TagName, nsCaseInsensitiveStringComparator()))
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
NODE_FOUND:
|
|
|
|
bNodeFound = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
// Search up the parent chain
|
|
|
|
// We should never fail because of root test below, but lets be safe
|
|
|
|
// XXX: ERROR_HANDLING error return code lost
|
2001-11-19 06:45:00 +00:00
|
|
|
if (NS_FAILED(currentNode->GetParentNode(getter_AddRefs(parent))) || !parent)
|
2001-01-28 20:13:07 +00:00
|
|
|
break;
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Stop searching if parent is a body tag
|
|
|
|
nsAutoString parentTagName;
|
|
|
|
parent->GetNodeName(parentTagName);
|
|
|
|
// Note: Originally used IsRoot to stop at table cells,
|
|
|
|
// but that's too messy when you are trying to find the parent table
|
|
|
|
//PRBool isRoot;
|
2001-11-19 06:45:00 +00:00
|
|
|
//if (NS_FAILED(IsRootTag(parentTagName, isRoot)) || isRoot)
|
2001-01-28 20:13:07 +00:00
|
|
|
if(parentTagName.EqualsIgnoreCase("body"))
|
|
|
|
break;
|
1999-09-02 17:56:09 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
currentNode = parent;
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
if (bNodeFound)
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> currentElement = do_QueryInterface(currentNode);
|
|
|
|
if (currentElement)
|
1999-11-29 08:28:46 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
*aReturn = currentElement;
|
|
|
|
// Getters must addref
|
|
|
|
NS_ADDREF(*aReturn);
|
1999-11-29 08:28:46 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
else res = NS_EDITOR_ELEMENT_NOT_FOUND;
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2000-08-10 03:43:09 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::GetSelectedElement(const nsAString& aTagName, nsIDOMElement** aReturn)
|
2000-08-10 03:43:09 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!aReturn )
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// default is null - no element found
|
|
|
|
*aReturn = nsnull;
|
|
|
|
|
|
|
|
// First look for a single element in selection
|
|
|
|
nsCOMPtr<nsISelection>selection;
|
2000-08-30 02:44:08 +00:00
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
|
2000-08-30 02:44:08 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
PRBool bNodeFound = PR_FALSE;
|
|
|
|
res=NS_ERROR_NOT_INITIALIZED;
|
|
|
|
PRBool isCollapsed;
|
|
|
|
selection->GetIsCollapsed(&isCollapsed);
|
2000-08-30 02:44:08 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString domTagName;
|
|
|
|
nsAutoString TagName(aTagName);
|
2001-12-17 07:14:49 +00:00
|
|
|
ToLowerCase(TagName);
|
2001-01-28 20:13:07 +00:00
|
|
|
// Empty string indicates we should match any element tag
|
|
|
|
PRBool anyTag = (TagName.IsEmpty());
|
|
|
|
PRBool isLinkTag = IsLinkTag(TagName);
|
|
|
|
PRBool isNamedAnchorTag = IsNamedAnchorTag(TagName);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> selectedElement;
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
res = selection->GetRangeAt(0, getter_AddRefs(range));
|
2000-08-30 02:44:08 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> startParent;
|
|
|
|
PRInt32 startOffset, endOffset;
|
|
|
|
res = range->GetStartContainer(getter_AddRefs(startParent));
|
2000-08-30 02:44:08 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2001-01-28 20:13:07 +00:00
|
|
|
res = range->GetStartOffset(&startOffset);
|
2000-08-30 02:44:08 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-08-30 22:49:08 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> endParent;
|
|
|
|
res = range->GetEndContainer(getter_AddRefs(endParent));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = range->GetEndOffset(&endOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
2000-08-30 22:49:08 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Optimization for a single selected element
|
|
|
|
if (startParent && startParent == endParent && (endOffset-startOffset) == 1)
|
2000-08-30 02:44:08 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> selectedNode = GetChildAt(startParent, startOffset);
|
|
|
|
if (NS_FAILED(res)) return NS_OK;
|
|
|
|
if (selectedNode)
|
1999-05-05 04:05:19 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
selectedNode->GetNodeName(domTagName);
|
2001-12-17 07:14:49 +00:00
|
|
|
ToLowerCase(domTagName);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// Test for appropriate node type requested
|
|
|
|
if (anyTag || (TagName == domTagName) ||
|
|
|
|
(isLinkTag && nsHTMLEditUtils::IsLink(selectedNode)) ||
|
|
|
|
(isNamedAnchorTag && nsHTMLEditUtils::IsNamedAnchor(selectedNode)))
|
|
|
|
{
|
|
|
|
bNodeFound = PR_TRUE;
|
|
|
|
selectedElement = do_QueryInterface(selectedNode);
|
|
|
|
}
|
2000-04-25 14:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
1999-05-05 04:05:19 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!bNodeFound)
|
2000-04-25 14:15:04 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if (isLinkTag)
|
2000-04-25 14:15:04 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Link tag is a special case - we return the anchor node
|
|
|
|
// found for any selection that is totally within a link,
|
|
|
|
// included a collapsed selection (just a caret in a link)
|
|
|
|
nsCOMPtr<nsIDOMNode> anchorNode;
|
|
|
|
res = selection->GetAnchorNode(getter_AddRefs(anchorNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
PRInt32 anchorOffset = -1;
|
|
|
|
if (anchorNode)
|
|
|
|
selection->GetAnchorOffset(&anchorOffset);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> focusNode;
|
|
|
|
res = selection->GetFocusNode(getter_AddRefs(focusNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
PRInt32 focusOffset = -1;
|
|
|
|
if (focusNode)
|
|
|
|
selection->GetFocusOffset(&focusOffset);
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Link node must be the same for both ends of selection
|
|
|
|
if (NS_SUCCEEDED(res) && anchorNode)
|
2000-04-25 14:15:04 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
#ifdef DEBUG_cmanske
|
|
|
|
{
|
|
|
|
nsAutoString name;
|
|
|
|
anchorNode->GetNodeName(name);
|
|
|
|
printf("GetSelectedElement: Anchor node of selection: ");
|
2001-06-30 11:02:25 +00:00
|
|
|
wprintf(name.get());
|
2001-01-28 20:13:07 +00:00
|
|
|
printf(" Offset: %d\n", anchorOffset);
|
|
|
|
focusNode->GetNodeName(name);
|
|
|
|
printf("Focus node of selection: ");
|
2001-06-30 11:02:25 +00:00
|
|
|
wprintf(name.get());
|
2001-01-28 20:13:07 +00:00
|
|
|
printf(" Offset: %d\n", focusOffset);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
nsCOMPtr<nsIDOMElement> parentLinkOfAnchor;
|
2001-11-14 01:33:42 +00:00
|
|
|
res = GetElementOrParentByTagName(NS_LITERAL_STRING("href"), anchorNode, getter_AddRefs(parentLinkOfAnchor));
|
2001-01-28 20:13:07 +00:00
|
|
|
// XXX: ERROR_HANDLING can parentLinkOfAnchor be null?
|
|
|
|
if (NS_SUCCEEDED(res) && parentLinkOfAnchor)
|
|
|
|
{
|
|
|
|
if (isCollapsed)
|
|
|
|
{
|
|
|
|
// We have just a caret in the link
|
|
|
|
bNodeFound = PR_TRUE;
|
|
|
|
} else if(focusNode)
|
|
|
|
{ // Link node must be the same for both ends of selection
|
|
|
|
nsCOMPtr<nsIDOMElement> parentLinkOfFocus;
|
2001-11-14 01:33:42 +00:00
|
|
|
res = GetElementOrParentByTagName(NS_LITERAL_STRING("href"), focusNode, getter_AddRefs(parentLinkOfFocus));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_SUCCEEDED(res) && parentLinkOfFocus == parentLinkOfAnchor)
|
|
|
|
bNodeFound = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We found a link node parent
|
|
|
|
if (bNodeFound) {
|
|
|
|
// GetElementOrParentByTagName addref'd this, so we don't need to do it here
|
|
|
|
*aReturn = parentLinkOfAnchor;
|
|
|
|
NS_IF_ADDREF(*aReturn);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (anchorOffset >= 0) // Check if link node is the only thing selected
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> anchorChild;
|
|
|
|
anchorChild = GetChildAt(anchorNode,anchorOffset);
|
|
|
|
if (anchorChild && nsHTMLEditUtils::IsLink(anchorChild) &&
|
|
|
|
(anchorNode == focusNode) && focusOffset == (anchorOffset+1))
|
|
|
|
{
|
|
|
|
selectedElement = do_QueryInterface(anchorChild);
|
|
|
|
bNodeFound = PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
1999-05-05 04:05:19 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isCollapsed) // Don't bother to examine selection if it is collapsed
|
2000-05-12 14:57:03 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIEnumerator> enumerator;
|
|
|
|
res = selPriv->GetEnumerator(getter_AddRefs(enumerator));
|
|
|
|
if (NS_SUCCEEDED(res))
|
2000-05-12 14:57:03 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if(!enumerator)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
enumerator->First();
|
|
|
|
nsCOMPtr<nsISupports> currentItem;
|
|
|
|
res = enumerator->CurrentItem(getter_AddRefs(currentItem));
|
|
|
|
if ((NS_SUCCEEDED(res)) && currentItem)
|
2000-05-12 14:57:03 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMRange> currange( do_QueryInterface(currentItem) );
|
2003-07-08 20:22:56 +00:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (iter)
|
2000-05-12 14:57:03 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
iter->Init(currange);
|
|
|
|
// loop through the content iterator for each content node
|
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
while (NS_ENUMERATOR_FALSE == iter->IsDone())
|
2000-05-12 14:57:03 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
res = iter->CurrentNode(getter_AddRefs(content));
|
|
|
|
// Note likely!
|
|
|
|
if (NS_FAILED(res))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// 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,
|
|
|
|
// thus there's not just one element selected
|
|
|
|
if (bNodeFound)
|
|
|
|
{
|
|
|
|
bNodeFound = PR_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
selectedElement->GetNodeName(domTagName);
|
2001-12-17 07:14:49 +00:00
|
|
|
ToLowerCase(domTagName);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
if (anyTag)
|
|
|
|
{
|
|
|
|
// Get name of first selected element
|
|
|
|
selectedElement->GetTagName(TagName);
|
2001-12-17 07:14:49 +00:00
|
|
|
ToLowerCase(TagName);
|
2001-01-28 20:13:07 +00:00
|
|
|
anyTag = PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// The "A" tag is a pain,
|
|
|
|
// used for both link(href is set) and "Named Anchor"
|
|
|
|
nsCOMPtr<nsIDOMNode> selectedNode = do_QueryInterface(selectedElement);
|
|
|
|
if ( (isLinkTag && nsHTMLEditUtils::IsLink(selectedNode)) ||
|
|
|
|
(isNamedAnchorTag && nsHTMLEditUtils::IsNamedAnchor(selectedNode)) )
|
|
|
|
{
|
|
|
|
bNodeFound = PR_TRUE;
|
|
|
|
} else if (TagName == 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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iter->Next();
|
2000-05-12 14:57:03 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
} else {
|
|
|
|
// Should never get here?
|
|
|
|
isCollapsed = PR_TRUE;
|
|
|
|
printf("isCollapsed was FALSE, but no elements found in selection\n");
|
2000-05-12 14:57:03 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
} else {
|
|
|
|
printf("Could not create enumerator for GetSelectionProperties\n");
|
2000-05-12 14:57:03 +00:00
|
|
|
}
|
|
|
|
}
|
1999-04-20 17:47:12 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
if (bNodeFound)
|
2000-03-28 00:34:26 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
*aReturn = selectedElement;
|
|
|
|
if (selectedElement)
|
|
|
|
{
|
|
|
|
// Getters must addref
|
|
|
|
NS_ADDREF(*aReturn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else res = NS_EDITOR_ELEMENT_NOT_FOUND;
|
2000-03-28 00:34:26 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
return res;
|
1999-04-20 17:47:12 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::CreateElementWithDefaults(const nsAString& aTagName, nsIDOMElement** aReturn)
|
2000-04-25 14:15:04 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res=NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (aReturn)
|
|
|
|
*aReturn = nsnull;
|
2000-06-08 14:47:29 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (aTagName.IsEmpty() || !aReturn)
|
2001-02-26 22:02:58 +00:00
|
|
|
// if (!aTagName || !aReturn)
|
2001-01-28 20:13:07 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsAutoString TagName(aTagName);
|
2001-12-17 07:14:49 +00:00
|
|
|
ToLowerCase(TagName);
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString realTagName;
|
2000-09-05 23:26:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (IsLinkTag(TagName) || IsNamedAnchorTag(TagName))
|
|
|
|
{
|
2001-10-13 00:16:32 +00:00
|
|
|
realTagName.Assign(NS_LITERAL_STRING("a"));
|
2001-01-28 20:13:07 +00:00
|
|
|
} else {
|
|
|
|
realTagName = TagName;
|
|
|
|
}
|
|
|
|
//We don't use editor's CreateElement because we don't want to
|
|
|
|
// go through the transaction system
|
2000-09-05 23:26:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMElement>newElement;
|
|
|
|
nsCOMPtr<nsIContent> newContent;
|
|
|
|
nsCOMPtr<nsIDOMDocument> doc = do_QueryReferent(mDocWeak);
|
|
|
|
if (!doc) return NS_ERROR_NOT_INITIALIZED;
|
2000-09-05 23:26:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
//new call to use instead to get proper HTML element, bug# 39919
|
|
|
|
res = CreateHTMLContent(realTagName, getter_AddRefs(newContent));
|
|
|
|
newElement = do_QueryInterface(newContent);
|
|
|
|
if (NS_FAILED(res) || !newElement)
|
|
|
|
return NS_ERROR_FAILURE;
|
2000-09-05 23:26:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Mark the new element dirty, so it will be formatted
|
|
|
|
newElement->SetAttribute(NS_LITERAL_STRING("_moz_dirty"), nsAutoString());
|
2000-09-05 23:26:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Set default values for new elements
|
2001-10-13 00:16:32 +00:00
|
|
|
if (TagName.Equals(NS_LITERAL_STRING("hr")))
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
// Note that we read the user's attributes for these from prefs (in InsertHLine JS)
|
2002-09-20 08:53:52 +00:00
|
|
|
res = SetAttributeOrEquivalent(newElement, NS_LITERAL_STRING("width"),
|
|
|
|
NS_LITERAL_STRING("100%"), PR_TRUE);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = SetAttributeOrEquivalent(newElement, NS_LITERAL_STRING("size"),
|
|
|
|
NS_LITERAL_STRING("2"), PR_TRUE);
|
2001-10-13 00:16:32 +00:00
|
|
|
} else if (TagName.Equals(NS_LITERAL_STRING("table")))
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2002-09-20 08:53:52 +00:00
|
|
|
res = newElement->SetAttribute(NS_LITERAL_STRING("cellpadding"),NS_LITERAL_STRING("2"));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = newElement->SetAttribute(NS_LITERAL_STRING("cellspacing"),NS_LITERAL_STRING("2"));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = newElement->SetAttribute(NS_LITERAL_STRING("border"),NS_LITERAL_STRING("1"));
|
2001-10-13 00:16:32 +00:00
|
|
|
} else if (TagName.Equals(NS_LITERAL_STRING("td")))
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2002-09-20 08:53:52 +00:00
|
|
|
res = SetAttributeOrEquivalent(newElement, NS_LITERAL_STRING("valign"),
|
|
|
|
NS_LITERAL_STRING("top"), PR_TRUE);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
// ADD OTHER TAGS HERE
|
2000-09-05 23:26:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
*aReturn = newElement;
|
|
|
|
// Getters must addref
|
|
|
|
NS_ADDREF(*aReturn);
|
2000-04-25 14:15:04 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
return res;
|
2000-04-25 14:15:04 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::InsertLinkAroundSelection(nsIDOMElement* aAnchorElement)
|
2000-06-08 14:47:29 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res=NS_ERROR_NULL_POINTER;
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
|
|
|
|
if (!aAnchorElement) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
|
|
|
|
// We must have a real selection
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (!selection)
|
2000-09-05 23:26:48 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
res = NS_ERROR_NULL_POINTER;
|
2000-09-05 23:26:48 +00:00
|
|
|
}
|
2000-06-08 14:47:29 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2000-06-08 14:47:29 +00:00
|
|
|
PRBool isCollapsed;
|
|
|
|
res = selection->GetIsCollapsed(&isCollapsed);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res))
|
|
|
|
isCollapsed = PR_TRUE;
|
2000-06-08 14:47:29 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (isCollapsed)
|
2000-06-08 14:47:29 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
printf("InsertLinkAroundSelection called but there is no selection!!!\n");
|
|
|
|
res = NS_OK;
|
|
|
|
} else {
|
|
|
|
// Be sure we were given an anchor element
|
|
|
|
nsCOMPtr<nsIDOMHTMLAnchorElement> anchor = do_QueryInterface(aAnchorElement);
|
|
|
|
if (anchor)
|
2000-06-08 14:47:29 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString href;
|
|
|
|
res = anchor->GetHref(href);
|
2000-06-08 14:47:29 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2001-06-30 11:02:25 +00:00
|
|
|
if (!href.IsEmpty())
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
2001-10-30 23:24:44 +00:00
|
|
|
|
|
|
|
// Set all attributes found on the supplied anchor element
|
|
|
|
nsCOMPtr<nsIDOMNamedNodeMap> attrMap;
|
|
|
|
aAnchorElement->GetAttributes(getter_AddRefs(attrMap));
|
|
|
|
if (!attrMap)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
PRUint32 count, i;
|
|
|
|
attrMap->GetLength(&count);
|
|
|
|
nsAutoString name, value;
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> attrNode;
|
|
|
|
res = attrMap->Item(i, getter_AddRefs(attrNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (attrNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMAttr> attribute = do_QueryInterface(attrNode);
|
|
|
|
if (attribute)
|
|
|
|
{
|
|
|
|
// We must clear the string buffers
|
|
|
|
// because GetName, GetValue appends to previous string!
|
2003-07-08 20:22:56 +00:00
|
|
|
name.Truncate();
|
|
|
|
value.Truncate();
|
2001-10-30 23:24:44 +00:00
|
|
|
|
|
|
|
res = attribute->GetName(name);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
res = attribute->GetValue(value);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
2003-04-17 05:42:33 +00:00
|
|
|
res = SetInlineProperty(nsEditProperty::a, name, value);
|
2001-10-30 23:24:44 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2000-06-08 14:47:29 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
return res;
|
2000-06-08 14:47:29 +00:00
|
|
|
}
|
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::SetHTMLBackgroundColor(const nsAString& aColor)
|
2000-06-08 14:47:29 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_PRECONDITION(mDocWeak, "Missing Editor DOM Document");
|
|
|
|
|
|
|
|
// Find a selected or enclosing table element to set background on
|
|
|
|
nsCOMPtr<nsIDOMElement> element;
|
|
|
|
PRInt32 selectedCount;
|
|
|
|
nsAutoString tagName;
|
2002-09-13 04:19:30 +00:00
|
|
|
nsresult res = GetSelectedOrParentTableElement(tagName, &selectedCount,
|
|
|
|
getter_AddRefs(element));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-06-08 14:47:29 +00:00
|
|
|
|
2003-05-23 21:34:47 +00:00
|
|
|
PRBool setColor = !aColor.IsEmpty();
|
2000-06-08 14:47:29 +00:00
|
|
|
|
2002-01-14 20:31:43 +00:00
|
|
|
NS_NAMED_LITERAL_STRING(bgcolor, "bgcolor");
|
2001-01-28 20:13:07 +00:00
|
|
|
if (element)
|
2000-06-08 14:47:29 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if (selectedCount > 0)
|
2000-06-08 14:47:29 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Traverse all selected cells
|
|
|
|
nsCOMPtr<nsIDOMElement> cell;
|
2002-09-13 04:19:30 +00:00
|
|
|
res = GetFirstSelectedCell(nsnull, getter_AddRefs(cell));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_SUCCEEDED(res) && cell)
|
|
|
|
{
|
|
|
|
while(cell)
|
|
|
|
{
|
|
|
|
if (setColor)
|
2002-01-14 20:31:43 +00:00
|
|
|
res = SetAttribute(cell, bgcolor, aColor);
|
2001-01-28 20:13:07 +00:00
|
|
|
else
|
2002-01-14 20:31:43 +00:00
|
|
|
res = RemoveAttribute(cell, bgcolor);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) break;
|
2000-06-08 14:47:29 +00:00
|
|
|
|
2002-09-13 04:19:30 +00:00
|
|
|
GetNextSelectedCell(nsnull, getter_AddRefs(cell));
|
2001-01-28 20:13:07 +00:00
|
|
|
};
|
|
|
|
return res;
|
|
|
|
}
|
2000-06-08 14:47:29 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
// If we failed to find a cell, fall through to use originally-found element
|
|
|
|
} else {
|
|
|
|
// No table element -- set the background color on the body tag
|
|
|
|
res = nsEditor::GetRootElement(getter_AddRefs(element));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!element) return NS_ERROR_NULL_POINTER;
|
2000-06-08 14:47:29 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
// Use the editor method that goes through the transaction system
|
|
|
|
if (setColor)
|
2002-01-14 20:31:43 +00:00
|
|
|
res = SetAttribute(element, bgcolor, aColor);
|
2001-01-28 20:13:07 +00:00
|
|
|
else
|
2002-01-14 20:31:43 +00:00
|
|
|
res = RemoveAttribute(element, bgcolor);
|
2000-06-08 14:47:29 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
return res;
|
2000-06-08 14:47:29 +00:00
|
|
|
}
|
|
|
|
|
2002-03-23 22:08:20 +00:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::SetBodyAttribute(const nsAString& aAttribute, const nsAString& aValue)
|
2000-04-25 14:15:04 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res;
|
|
|
|
// TODO: Check selection for Cell, Row, Column or table and do color on appropriate level
|
2000-04-25 14:15:04 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_ASSERTION(mDocWeak, "Missing Editor DOM Document");
|
|
|
|
|
|
|
|
// Set the background color attribute on the body tag
|
|
|
|
nsCOMPtr<nsIDOMElement> bodyElement;
|
|
|
|
|
|
|
|
res = nsEditor::GetRootElement(getter_AddRefs(bodyElement));
|
|
|
|
if (!bodyElement) res = NS_ERROR_NULL_POINTER;
|
|
|
|
if (NS_SUCCEEDED(res))
|
2000-04-25 14:15:04 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Use the editor method that goes through the transaction system
|
|
|
|
res = SetAttribute(bodyElement, aAttribute, aValue);
|
2000-04-25 14:15:04 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
return res;
|
2000-04-25 14:15:04 +00:00
|
|
|
}
|
|
|
|
|
2001-08-07 21:39:07 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetLinkedObjects(nsISupportsArray** aNodeList)
|
|
|
|
{
|
|
|
|
if (!aNodeList)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
res = NS_NewISupportsArray(aNodeList);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!*aNodeList) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2003-07-08 20:22:56 +00:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
|
2001-08-07 21:39:07 +00:00
|
|
|
if (!iter) return NS_ERROR_NULL_POINTER;
|
|
|
|
if ((NS_SUCCEEDED(res)))
|
|
|
|
{
|
|
|
|
// get the root content
|
|
|
|
nsCOMPtr<nsIContent> rootContent;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc;
|
|
|
|
nsEditor::GetDocument(getter_AddRefs(domdoc));
|
|
|
|
if (!domdoc)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc (do_QueryInterface(domdoc));
|
|
|
|
if (!doc)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
doc->GetRootContent(getter_AddRefs(rootContent));
|
|
|
|
|
|
|
|
iter->Init(rootContent);
|
|
|
|
|
|
|
|
// loop through the content iterator for each content node
|
|
|
|
while (NS_ENUMERATOR_FALSE == iter->IsDone())
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
res = iter->CurrentNode(getter_AddRefs(content));
|
|
|
|
if (NS_FAILED(res))
|
|
|
|
break;
|
|
|
|
nsCOMPtr<nsIDOMNode> node (do_QueryInterface(content));
|
|
|
|
if (node)
|
|
|
|
{
|
|
|
|
// Let nsURIRefObject make the hard decisions:
|
|
|
|
nsCOMPtr<nsIURIRefObject> refObject;
|
|
|
|
res = NS_NewHTMLURIRefObject(getter_AddRefs(refObject), node);
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> isupp (do_QueryInterface(refObject));
|
|
|
|
if (isupp)
|
|
|
|
(*aNodeList)->AppendElement(isupp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
iter->Next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark nsIEditorStyleSheets methods
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
2000-04-25 14:15:04 +00:00
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::AddStyleSheet(const nsAString &aURL)
|
|
|
|
{
|
|
|
|
// Enable existing sheet if already loaded.
|
|
|
|
if (EnableExistingStyleSheet(aURL))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Lose the previously-loaded sheet so there's nothing to replace
|
|
|
|
// This pattern is different from Override methods because
|
|
|
|
// we must wait to remove mLastStyleSheetURL and add new sheet
|
|
|
|
// at the same time (in StyleSheetLoaded callback) so they are undoable together
|
|
|
|
mLastStyleSheetURL.Truncate();
|
|
|
|
return ReplaceStyleSheet(aURL);
|
|
|
|
}
|
2000-01-18 21:50:15 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-09-13 19:47:38 +00:00
|
|
|
nsHTMLEditor::ReplaceStyleSheet(const nsAString& aURL)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2002-09-13 19:47:38 +00:00
|
|
|
// Enable existing sheet if already loaded.
|
|
|
|
if (EnableExistingStyleSheet(aURL))
|
2000-01-18 21:50:15 +00:00
|
|
|
{
|
2002-09-13 19:47:38 +00:00
|
|
|
// Disable last sheet if not the same as new one
|
|
|
|
if (!mLastStyleSheetURL.IsEmpty() && mLastStyleSheetURL.Equals(aURL))
|
|
|
|
return EnableStyleSheet(mLastStyleSheetURL, PR_FALSE);
|
|
|
|
|
|
|
|
return NS_OK;
|
2000-01-18 21:50:15 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
nsCOMPtr<nsICSSLoader> cssLoader;
|
|
|
|
nsresult rv = GetCSSLoader(aURL, getter_AddRefs(cssLoader));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> document;
|
|
|
|
if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!ps) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
rv = ps->GetDocument(getter_AddRefs(document));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);;
|
|
|
|
if (!document) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uaURI;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uaURI), aURL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsICSSStyleSheet> sheet;
|
2002-12-03 05:48:14 +00:00
|
|
|
rv = cssLoader->LoadAgentSheet(uaURI, this);
|
2002-09-13 19:47:38 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-09-13 19:47:38 +00:00
|
|
|
nsHTMLEditor::RemoveStyleSheet(const nsAString &aURL)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2002-09-13 19:47:38 +00:00
|
|
|
nsCOMPtr<nsICSSStyleSheet> sheet;
|
|
|
|
nsresult rv = GetStyleSheetForURL(aURL, getter_AddRefs(sheet));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!sheet)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
RemoveStyleSheetTxn* txn;
|
2002-09-13 19:47:38 +00:00
|
|
|
rv = CreateTxnForRemoveStyleSheet(sheet, &txn);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!txn) rv = NS_ERROR_NULL_POINTER;
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2000-01-18 21:50:15 +00:00
|
|
|
{
|
2003-04-04 20:50:25 +00:00
|
|
|
rv = DoTransaction(txn);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-09-13 19:47:38 +00:00
|
|
|
mLastStyleSheetURL.Truncate(); // forget it
|
|
|
|
|
|
|
|
// Remove it from our internal list
|
|
|
|
rv = RemoveStyleSheetFromList(aURL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2000-01-18 21:50:15 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
// The transaction system (if any) has taken ownwership of txns
|
|
|
|
NS_IF_RELEASE(txn);
|
2000-01-18 21:50:15 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
return rv;
|
1999-11-10 23:42:11 +00:00
|
|
|
}
|
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::AddOverrideStyleSheet(const nsAString& aURL)
|
|
|
|
{
|
|
|
|
// Enable existing sheet if already loaded.
|
|
|
|
if (EnableExistingStyleSheet(aURL))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsCOMPtr<nsICSSLoader> cssLoader;
|
|
|
|
nsresult rv = GetCSSLoader(aURL, getter_AddRefs(cssLoader));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uaURI;
|
|
|
|
rv = NS_NewURI(getter_AddRefs(uaURI), aURL);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
// We MUST ONLY load synchronous local files (no @import)
|
2002-09-13 19:47:38 +00:00
|
|
|
nsCOMPtr<nsICSSStyleSheet> sheet;
|
2002-12-03 05:48:14 +00:00
|
|
|
rv = cssLoader->LoadAgentSheet(uaURI, getter_AddRefs(sheet));
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
// Synchronous loads should ALWAYS return completed
|
2002-12-03 05:48:14 +00:00
|
|
|
if (!sheet)
|
2002-09-13 19:47:38 +00:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStyleSheet> styleSheet;
|
|
|
|
styleSheet = do_QueryInterface(sheet);
|
|
|
|
nsCOMPtr<nsIStyleSet> styleSet;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!ps)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
rv = ps->GetStyleSet(getter_AddRefs(styleSet));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!styleSet)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// Add the override style sheet
|
|
|
|
// (This checks if already exists)
|
|
|
|
styleSet->AppendOverrideStyleSheet(styleSheet);
|
|
|
|
|
|
|
|
// Save doc pointer to be able to use nsIStyleSheet::SetEnabled()
|
|
|
|
nsCOMPtr<nsIDocument> document;
|
|
|
|
rv = ps->GetDocument(getter_AddRefs(document));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
if (!document)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
styleSheet->SetOwningDocument(document);
|
|
|
|
|
2003-10-02 21:29:40 +00:00
|
|
|
// This notifies document observers to recompute style data
|
2002-09-13 19:47:38 +00:00
|
|
|
// (this doesn't affect style sheet because it is not a doc sheet)
|
2003-10-02 21:29:40 +00:00
|
|
|
// XXXbz this is a major misuse of the API....
|
|
|
|
document->BeginUpdate(UPDATE_STYLE);
|
2002-12-03 05:48:14 +00:00
|
|
|
document->SetStyleSheetApplicableState(styleSheet, PR_TRUE);
|
2003-10-02 21:29:40 +00:00
|
|
|
document->EndUpdate(UPDATE_STYLE);
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
// Save as the last-loaded sheet
|
|
|
|
mLastOverrideStyleSheetURL = aURL;
|
|
|
|
|
|
|
|
//Add URL and style sheet to our lists
|
|
|
|
return AddNewStyleSheetToList(aURL, sheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::ReplaceOverrideStyleSheet(const nsAString& aURL)
|
|
|
|
{
|
|
|
|
// Enable existing sheet if already loaded.
|
|
|
|
if (EnableExistingStyleSheet(aURL))
|
|
|
|
{
|
|
|
|
// Disable last sheet if not the same as new one
|
|
|
|
if (!mLastOverrideStyleSheetURL.IsEmpty() && !mLastOverrideStyleSheetURL.Equals(aURL))
|
|
|
|
return EnableStyleSheet(mLastOverrideStyleSheetURL, PR_FALSE);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// Remove the previous sheet
|
|
|
|
if (!mLastOverrideStyleSheetURL.IsEmpty())
|
|
|
|
RemoveOverrideStyleSheet(mLastOverrideStyleSheetURL);
|
|
|
|
|
|
|
|
return AddOverrideStyleSheet(aURL);
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Do NOT use transaction system for override style sheets
|
|
|
|
NS_IMETHODIMP
|
2002-09-13 19:47:38 +00:00
|
|
|
nsHTMLEditor::RemoveOverrideStyleSheet(const nsAString &aURL)
|
1999-11-10 23:42:11 +00:00
|
|
|
{
|
2002-09-13 19:47:38 +00:00
|
|
|
nsCOMPtr<nsICSSStyleSheet> sheet;
|
|
|
|
nsresult rv = GetStyleSheetForURL(aURL, getter_AddRefs(sheet));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!sheet)
|
|
|
|
return NS_OK; /// Don't fail if sheet not found
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!ps) return NS_ERROR_NOT_INITIALIZED;
|
1999-11-10 23:42:11 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDocument> document;
|
2002-09-13 19:47:38 +00:00
|
|
|
rv = ps->GetDocument(getter_AddRefs(document));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);;
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!document) return NS_ERROR_NULL_POINTER;
|
1999-11-10 23:42:11 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIStyleSet> styleSet;
|
|
|
|
rv = ps->GetStyleSet(getter_AddRefs(styleSet));
|
2002-09-13 19:47:38 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!styleSet) return NS_ERROR_NULL_POINTER;
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIStyleSheet> styleSheet = do_QueryInterface(sheet);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!styleSheet) return NS_ERROR_NULL_POINTER;
|
2002-09-13 19:47:38 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
styleSet->RemoveOverrideStyleSheet(styleSheet);
|
2000-01-10 10:13:58 +00:00
|
|
|
|
2003-10-02 21:29:40 +00:00
|
|
|
// This notifies document observers to recompute style data
|
2001-01-28 20:13:07 +00:00
|
|
|
// (this doesn't affect style sheet because it is not a doc sheet)
|
2003-10-02 21:29:40 +00:00
|
|
|
// XXXbz this is a major misuse of the API....
|
|
|
|
document->BeginUpdate(UPDATE_STYLE);
|
2002-12-03 05:48:14 +00:00
|
|
|
document->SetStyleSheetApplicableState(styleSheet, PR_FALSE);
|
2003-10-02 21:29:40 +00:00
|
|
|
document->EndUpdate(UPDATE_STYLE);
|
1999-11-10 23:42:11 +00:00
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
// Remove it from our internal list
|
|
|
|
return RemoveStyleSheetFromList(aURL);
|
1999-11-10 23:42:11 +00:00
|
|
|
}
|
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::EnableStyleSheet(const nsAString &aURL, PRBool aEnable)
|
1999-11-10 23:42:11 +00:00
|
|
|
{
|
2002-09-13 19:47:38 +00:00
|
|
|
nsCOMPtr<nsICSSStyleSheet> sheet;
|
|
|
|
nsresult rv = GetStyleSheetForURL(aURL, getter_AddRefs(sheet));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!sheet)
|
|
|
|
return NS_OK; // Don't fail if sheet not found
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStyleSheet> nsISheet = do_QueryInterface(sheet);
|
|
|
|
return nsISheet->SetEnabled(aEnable);
|
1999-11-10 23:42:11 +00:00
|
|
|
}
|
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
PRBool
|
|
|
|
nsHTMLEditor::EnableExistingStyleSheet(const nsAString &aURL)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsICSSStyleSheet> sheet;
|
|
|
|
nsresult rv = GetStyleSheetForURL(aURL, getter_AddRefs(sheet));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Enable sheet if already loaded.
|
|
|
|
if (sheet)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIStyleSheet> nsISheet = do_QueryInterface(sheet);
|
|
|
|
nsISheet->SetEnabled(PR_TRUE);
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::AddNewStyleSheetToList(const nsAString &aURL,
|
|
|
|
nsICSSStyleSheet *aStyleSheet)
|
1999-11-10 23:42:11 +00:00
|
|
|
{
|
2002-11-12 19:40:11 +00:00
|
|
|
PRInt32 countSS = mStyleSheets.Count();
|
2002-09-13 19:47:38 +00:00
|
|
|
PRInt32 countU = mStyleSheetURLs.Count();
|
1999-11-10 23:42:11 +00:00
|
|
|
|
2002-11-12 19:40:11 +00:00
|
|
|
if (countU < 0 || countSS != countU)
|
2002-09-13 19:47:38 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2000-08-07 06:05:27 +00:00
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
if (!mStyleSheetURLs.AppendString(aURL))
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2002-11-12 19:40:11 +00:00
|
|
|
return mStyleSheets.AppendObject(aStyleSheet) ? NS_OK : NS_ERROR_UNEXPECTED;
|
2002-09-13 19:47:38 +00:00
|
|
|
}
|
2000-07-12 23:44:56 +00:00
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::RemoveStyleSheetFromList(const nsAString &aURL)
|
|
|
|
{
|
|
|
|
// is it already in the list?
|
|
|
|
PRInt32 foundIndex;
|
|
|
|
foundIndex = mStyleSheetURLs.IndexOf(aURL);
|
|
|
|
if (foundIndex < 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Attempt both removals; if one fails there's not much we can do.
|
2002-11-12 19:40:11 +00:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!mStyleSheets.RemoveObjectAt(foundIndex))
|
2002-09-13 19:47:38 +00:00
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
if (!mStyleSheetURLs.RemoveStringAt(foundIndex))
|
|
|
|
rv = NS_ERROR_FAILURE;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
1999-11-24 20:48:59 +00:00
|
|
|
return rv;
|
1999-11-10 23:42:11 +00:00
|
|
|
}
|
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetStyleSheetForURL(const nsAString &aURL,
|
|
|
|
nsICSSStyleSheet **aStyleSheet)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aStyleSheet);
|
|
|
|
*aStyleSheet = 0;
|
|
|
|
|
|
|
|
// is it already in the list?
|
|
|
|
PRInt32 foundIndex;
|
|
|
|
foundIndex = mStyleSheetURLs.IndexOf(aURL);
|
|
|
|
if (foundIndex < 0)
|
|
|
|
return NS_OK; //No sheet -- don't fail!
|
|
|
|
|
2002-11-12 19:40:11 +00:00
|
|
|
*aStyleSheet = mStyleSheets[foundIndex];
|
2002-09-13 19:47:38 +00:00
|
|
|
if (!*aStyleSheet)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
NS_ADDREF(*aStyleSheet);
|
2002-11-12 19:40:11 +00:00
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetURLForStyleSheet(nsICSSStyleSheet *aStyleSheet,
|
|
|
|
nsAString &aURL)
|
|
|
|
{
|
|
|
|
// is it already in the list?
|
2002-11-12 19:40:11 +00:00
|
|
|
PRInt32 foundIndex = mStyleSheets.IndexOf(aStyleSheet);
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
// Don't fail if we don't find it in our list
|
|
|
|
if (foundIndex == -1)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Found it in the list!
|
|
|
|
nsAString* strp = mStyleSheetURLs.StringAt(foundIndex);
|
|
|
|
if (!strp)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
aURL = *strp;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetCSSLoader(const nsAString& aURL, nsICSSLoader** aCSSLoader)
|
|
|
|
{
|
|
|
|
if (!aCSSLoader)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
*aCSSLoader = 0;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> document;
|
|
|
|
|
|
|
|
if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!ps) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
rv = ps->GetDocument(getter_AddRefs(document));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);;
|
|
|
|
if (!document) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIHTMLContentContainer> container = do_QueryInterface(document);
|
|
|
|
if (!container) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsCOMPtr<nsICSSLoader> cssLoader;
|
|
|
|
nsCOMPtr<nsICSSStyleSheet> cssStyleSheet;
|
|
|
|
|
|
|
|
rv = container->GetCSSLoader(*getter_AddRefs(cssLoader));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);;
|
|
|
|
if (!cssLoader) return NS_ERROR_NULL_POINTER;
|
|
|
|
*aCSSLoader = cssLoader;
|
|
|
|
NS_ADDREF(*aCSSLoader);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark nsIEditorMailSupport methods
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
1999-11-24 20:48:59 +00:00
|
|
|
NS_IMETHODIMP
|
2001-01-28 20:13:07 +00:00
|
|
|
nsHTMLEditor::GetEmbeddedObjects(nsISupportsArray** aNodeList)
|
1999-11-10 23:42:11 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!aNodeList)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
1999-11-10 23:42:11 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res;
|
1999-11-10 23:42:11 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
res = NS_NewISupportsArray(aNodeList);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!*aNodeList) return NS_ERROR_NULL_POINTER;
|
1999-11-18 19:43:14 +00:00
|
|
|
|
2003-07-08 20:22:56 +00:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!iter) return NS_ERROR_NULL_POINTER;
|
|
|
|
if ((NS_SUCCEEDED(res)))
|
|
|
|
{
|
|
|
|
// get the root content
|
|
|
|
nsCOMPtr<nsIContent> rootContent;
|
1999-11-10 23:42:11 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc;
|
|
|
|
nsEditor::GetDocument(getter_AddRefs(domdoc));
|
|
|
|
if (!domdoc)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2000-01-10 10:13:58 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc (do_QueryInterface(domdoc));
|
|
|
|
if (!doc)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2000-03-20 23:13:25 +00:00
|
|
|
|
2001-06-20 03:27:48 +00:00
|
|
|
doc->GetRootContent(getter_AddRefs(rootContent));
|
2000-03-20 23:13:25 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
iter->Init(rootContent);
|
2000-07-12 23:44:56 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// loop through the content iterator for each content node
|
|
|
|
while (NS_ENUMERATOR_FALSE == iter->IsDone())
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
res = iter->CurrentNode(getter_AddRefs(content));
|
|
|
|
if (NS_FAILED(res))
|
|
|
|
break;
|
|
|
|
nsCOMPtr<nsIDOMNode> node (do_QueryInterface(content));
|
|
|
|
if (node)
|
2000-01-10 10:13:58 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString tagName;
|
|
|
|
node->GetNodeName(tagName);
|
2001-12-17 07:14:49 +00:00
|
|
|
ToLowerCase(tagName);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// See if it's an image or an embed
|
2001-10-13 00:16:32 +00:00
|
|
|
if (tagName.Equals(NS_LITERAL_STRING("img")) || tagName.Equals(NS_LITERAL_STRING("embed")))
|
2001-01-28 20:13:07 +00:00
|
|
|
(*aNodeList)->AppendElement(node);
|
2001-10-13 00:16:32 +00:00
|
|
|
else if (tagName.Equals(NS_LITERAL_STRING("a")))
|
2000-07-12 23:44:56 +00:00
|
|
|
{
|
2001-08-07 21:39:07 +00:00
|
|
|
// Only include links if they're links to file: URLs
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMHTMLAnchorElement> anchor (do_QueryInterface(content));
|
|
|
|
if (anchor)
|
|
|
|
{
|
|
|
|
nsAutoString href;
|
|
|
|
if (NS_SUCCEEDED(anchor->GetHref(href)))
|
2003-07-14 07:37:39 +00:00
|
|
|
if (StringBeginsWith(href, NS_LITERAL_STRING("file:"),
|
|
|
|
nsCaseInsensitiveStringComparator()))
|
2001-01-28 20:13:07 +00:00
|
|
|
(*aNodeList)->AppendElement(node);
|
|
|
|
}
|
2000-07-12 23:44:56 +00:00
|
|
|
}
|
2002-03-11 14:54:59 +00:00
|
|
|
else if (tagName.Equals(NS_LITERAL_STRING("body")))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(node);
|
|
|
|
if (element)
|
|
|
|
{
|
2003-09-05 17:49:36 +00:00
|
|
|
PRBool hasBackground = PR_FALSE;
|
|
|
|
if (NS_SUCCEEDED(element->HasAttribute(NS_LITERAL_STRING("background"), &hasBackground)) && hasBackground)
|
2003-08-18 05:18:05 +00:00
|
|
|
(*aNodeList)->AppendElement(node);
|
2002-03-11 14:54:59 +00:00
|
|
|
}
|
|
|
|
}
|
2000-01-10 10:13:58 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
iter->Next();
|
1999-11-24 20:48:59 +00:00
|
|
|
}
|
2000-07-12 23:44:56 +00:00
|
|
|
}
|
1999-11-29 21:15:57 +00:00
|
|
|
|
1999-11-10 23:42:11 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark nsIEditor overrides
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
2002-12-22 01:51:14 +00:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::DeleteNode(nsIDOMNode * aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> selectAllNode = FindUserSelectAllNode(aNode);
|
|
|
|
|
|
|
|
if (selectAllNode)
|
|
|
|
{
|
|
|
|
return nsEditor::DeleteNode(selectAllNode);
|
|
|
|
}
|
|
|
|
return nsEditor::DeleteNode(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::DeleteText(nsIDOMCharacterData *aTextNode,
|
|
|
|
PRUint32 aOffset,
|
|
|
|
PRUint32 aLength)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> selectAllNode = FindUserSelectAllNode(aTextNode);
|
|
|
|
|
|
|
|
if (selectAllNode)
|
|
|
|
{
|
|
|
|
return nsEditor::DeleteNode(selectAllNode);
|
|
|
|
}
|
|
|
|
return nsEditor::DeleteText(aTextNode, aOffset, aLength);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark support utils
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* This routine examines aNode and it's ancestors looking for any node which has the
|
2003-04-07 14:32:23 +00:00
|
|
|
-moz-user-select: all style lit. Return the highest such ancestor. */
|
2002-12-22 01:51:14 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> nsHTMLEditor::FindUserSelectAllNode(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> resultNode; // starts out empty
|
|
|
|
nsCOMPtr<nsIDOMNode> node = aNode;
|
2003-04-07 14:32:23 +00:00
|
|
|
nsCOMPtr<nsIDOMElement>root;
|
|
|
|
GetRootElement(getter_AddRefs(root));
|
|
|
|
if (!nsEditorUtils::IsDescendantOf(aNode, root))
|
|
|
|
return nsnull;
|
2002-12-22 01:51:14 +00:00
|
|
|
|
|
|
|
// retrieve the computed style of -moz-user-select for aNode
|
|
|
|
nsAutoString mozUserSelectValue;
|
|
|
|
while (node)
|
|
|
|
{
|
2003-04-17 05:42:33 +00:00
|
|
|
mHTMLCSSUtils->GetComputedProperty(node, nsEditProperty::cssMozUserSelect, mozUserSelectValue);
|
2003-04-07 14:32:23 +00:00
|
|
|
if (mozUserSelectValue.Equals(NS_LITERAL_STRING("all")))
|
2002-12-22 01:51:14 +00:00
|
|
|
{
|
2003-04-07 14:32:23 +00:00
|
|
|
resultNode = node;
|
2002-12-22 01:51:14 +00:00
|
|
|
}
|
2003-04-07 14:32:23 +00:00
|
|
|
if (node != root)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
node->GetParentNode(getter_AddRefs(tmp));
|
|
|
|
node = tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-12-22 01:51:14 +00:00
|
|
|
return resultNode;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2000-09-14 11:45:01 +00:00
|
|
|
static nsresult SetSelectionAroundHeadChildren(nsCOMPtr<nsISelection> aSelection, nsWeakPtr aDocWeak)
|
2000-04-28 05:59:16 +00:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
// Set selection around <head> node
|
|
|
|
nsCOMPtr<nsIDOMDocument> doc = do_QueryReferent(aDocWeak);
|
|
|
|
if (!doc) return NS_ERROR_NOT_INITIALIZED;
|
2002-01-14 20:31:43 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNodeList>nodeList;
|
|
|
|
res = doc->GetElementsByTagName(NS_LITERAL_STRING("head"), getter_AddRefs(nodeList));
|
2000-04-28 05:59:16 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!nodeList) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
PRUint32 count;
|
|
|
|
nodeList->GetLength(&count);
|
|
|
|
if (count < 1) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> headNode;
|
|
|
|
res = nodeList->Item(0, getter_AddRefs(headNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!headNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// Collapse selection to before first child of the head,
|
|
|
|
res = aSelection->Collapse(headNode, 0);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// then extend it to just after
|
|
|
|
nsCOMPtr<nsIDOMNodeList> childNodes;
|
|
|
|
res = headNode->GetChildNodes(getter_AddRefs(childNodes));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!childNodes) return NS_ERROR_NULL_POINTER;
|
|
|
|
PRUint32 childCount;
|
|
|
|
childNodes->GetLength(&childCount);
|
|
|
|
|
|
|
|
return aSelection->Extend(headNode, childCount+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::GetHeadContentsAsHTML(nsAString& aOutputString)
|
2000-04-28 05:59:16 +00:00
|
|
|
{
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2000-04-28 05:59:16 +00:00
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// Save current selection
|
|
|
|
nsAutoSelectionReset selectionResetter(selection, this);
|
|
|
|
|
|
|
|
res = SetSelectionAroundHeadChildren(selection, mDocWeak);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
2002-03-25 22:39:19 +00:00
|
|
|
res = OutputToString(NS_LITERAL_STRING("text/html"),
|
|
|
|
nsIDocumentEncoder::OutputSelectionOnly,
|
|
|
|
aOutputString);
|
2000-04-28 23:39:27 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
// Selection always includes <body></body>,
|
|
|
|
// so terminate there
|
2001-04-07 00:45:26 +00:00
|
|
|
nsReadingIterator<PRUnichar> findIter,endFindIter;
|
|
|
|
aOutputString.BeginReading(findIter);
|
|
|
|
aOutputString.EndReading(endFindIter);
|
|
|
|
//counting on our parser to always lower case!!!
|
2002-11-26 22:15:30 +00:00
|
|
|
if (CaseInsensitiveFindInReadable(NS_LITERAL_STRING("<body"),
|
|
|
|
findIter, endFindIter))
|
2000-04-28 23:39:27 +00:00
|
|
|
{
|
2001-04-07 00:45:26 +00:00
|
|
|
nsReadingIterator<PRUnichar> beginIter;
|
|
|
|
aOutputString.BeginReading(beginIter);
|
|
|
|
PRInt32 offset = Distance(beginIter, findIter);//get the distance
|
|
|
|
|
|
|
|
nsWritingIterator<PRUnichar> writeIter;
|
|
|
|
aOutputString.BeginWriting(writeIter);
|
2000-04-28 23:39:27 +00:00
|
|
|
// Ensure the string ends in a newline
|
|
|
|
PRUnichar newline ('\n');
|
2001-04-07 00:45:26 +00:00
|
|
|
findIter.advance(-1);
|
|
|
|
if (offset ==0 || (offset >0 && (*findIter) != newline)) //check for 0
|
|
|
|
{
|
|
|
|
writeIter.advance(offset);
|
|
|
|
*writeIter = newline;
|
|
|
|
aOutputString.Truncate(offset+1);
|
|
|
|
}
|
2000-04-28 23:39:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
2000-04-28 05:59:16 +00:00
|
|
|
}
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::DebugUnitTests(PRInt32 *outNumTests, PRInt32 *outNumTestsFailed)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (!outNumTests || !outNumTestsFailed)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-09-29 20:08:15 +00:00
|
|
|
TextEditorTest *tester = new TextEditorTest();
|
|
|
|
if (!tester)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
tester->Run(this, outNumTests, outNumTestsFailed);
|
|
|
|
delete tester;
|
|
|
|
return NS_OK;
|
|
|
|
#else
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
1999-09-17 23:15:12 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
2000-03-24 00:26:47 +00:00
|
|
|
#pragma mark nsIEditorIMESupport overrides
|
1999-09-17 23:15:12 +00:00
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::SetCompositionString(const nsAString& aCompositionString, nsIPrivateTextRangeList* aTextRangeList,nsTextEventReply* aReply)
|
1999-09-17 23:15:12 +00:00
|
|
|
{
|
1999-10-26 20:04:47 +00:00
|
|
|
NS_ASSERTION(aTextRangeList, "null ptr");
|
2002-06-13 20:35:12 +00:00
|
|
|
if (nsnull == aTextRangeList)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
1999-09-17 23:15:12 +00:00
|
|
|
nsCOMPtr<nsICaret> caretP;
|
|
|
|
|
2000-02-25 23:50:17 +00:00
|
|
|
// workaround for windows ime bug 23558: we get every ime event twice.
|
|
|
|
// for escape keypress, this causes an empty string to be passed
|
|
|
|
// twice, which freaks out the editor. This is to detect and aviod that
|
|
|
|
// situation:
|
|
|
|
if (aCompositionString.IsEmpty() && !mIMETextNode)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
1999-09-17 23:15:12 +00:00
|
|
|
nsresult result = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(result)) return result;
|
1999-10-26 18:54:47 +00:00
|
|
|
|
1999-10-26 20:04:47 +00:00
|
|
|
mIMETextRangeList = aTextRangeList;
|
1999-10-26 18:54:47 +00:00
|
|
|
|
2002-06-13 20:35:12 +00:00
|
|
|
if (!mPresShellWeak)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!ps)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
// XXX_kin: BEGIN HACK! HACK! HACK!
|
|
|
|
// XXX_kin:
|
|
|
|
// XXX_kin: This is lame! The IME stuff needs caret coordinates
|
|
|
|
// XXX_kin: synchronously, but the editor could be using async
|
|
|
|
// XXX_kin: updates (reflows and paints) for performance reasons.
|
|
|
|
// XXX_kin: In order to give IME what it needs, we have to temporarily
|
|
|
|
// XXX_kin: switch to sync updating during this call so that the
|
|
|
|
// XXX_kin: nsAutoPlaceHolderBatch can force sync reflows, paints,
|
|
|
|
// XXX_kin: and selection scrolling, so that we get back accurate
|
|
|
|
// XXX_kin: caret coordinates.
|
|
|
|
|
|
|
|
PRUint32 flags = 0;
|
|
|
|
PRBool restoreFlags = PR_FALSE;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(GetFlags(&flags)) &&
|
|
|
|
(flags & nsIPlaintextEditor::eEditorUseAsyncUpdatesMask))
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(SetFlags(flags & (~nsIPlaintextEditor::eEditorUseAsyncUpdatesMask))))
|
|
|
|
restoreFlags = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX_kin: END HACK! HACK! HACK!
|
|
|
|
|
2002-03-07 14:34:35 +00:00
|
|
|
// we need the nsAutoPlaceHolderBatch destructor called before hitting
|
|
|
|
// GetCaretCoordinates so the states in Frame system sync with content
|
2002-06-13 20:35:12 +00:00
|
|
|
// therefore, we put the nsAutoPlaceHolderBatch into an inner block
|
2002-03-07 14:34:35 +00:00
|
|
|
{
|
|
|
|
nsAutoPlaceHolderBatch batch(this, gIMETxnName);
|
1999-10-26 20:04:47 +00:00
|
|
|
|
2002-03-07 14:34:35 +00:00
|
|
|
result = InsertText(aCompositionString);
|
1999-09-17 23:15:12 +00:00
|
|
|
|
2002-03-07 14:34:35 +00:00
|
|
|
mIMEBufferLength = aCompositionString.Length();
|
|
|
|
|
|
|
|
ps->GetCaret(getter_AddRefs(caretP));
|
2003-02-27 23:09:51 +00:00
|
|
|
if (caretP)
|
|
|
|
caretP->SetCaretDOMSelection(selection);
|
2002-03-07 14:34:35 +00:00
|
|
|
|
|
|
|
// second part of 23558 fix:
|
|
|
|
if (aCompositionString.IsEmpty())
|
|
|
|
{
|
|
|
|
mIMETextNode = nsnull;
|
|
|
|
}
|
|
|
|
}
|
2002-06-13 20:35:12 +00:00
|
|
|
|
|
|
|
// XXX_kin: BEGIN HACK! HACK! HACK!
|
|
|
|
// XXX_kin:
|
|
|
|
// XXX_kin: Restore the previous set of flags!
|
|
|
|
|
|
|
|
if (restoreFlags)
|
|
|
|
SetFlags(flags);
|
|
|
|
|
|
|
|
// XXX_kin: END HACK! HACK! HACK!
|
|
|
|
|
2001-04-19 22:46:29 +00:00
|
|
|
result = caretP->GetCaretCoordinates(nsICaret::eIMECoordinates, selection,
|
2002-08-26 20:55:30 +00:00
|
|
|
&(aReply->mCursorPosition), &(aReply->mCursorIsCollapsed), nsnull);
|
2002-03-07 14:34:35 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(result), "cannot get caret position");
|
1999-09-17 23:15:12 +00:00
|
|
|
|
2000-02-25 23:50:17 +00:00
|
|
|
|
1999-09-17 23:15:12 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2000-08-04 21:43:29 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetReconversionString(nsReconversionEventReply* aReply)
|
|
|
|
{
|
|
|
|
nsresult res;
|
|
|
|
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2000-08-04 21:43:29 +00:00
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res) || !selection)
|
|
|
|
return (res == NS_OK) ? NS_ERROR_FAILURE : res;
|
|
|
|
|
|
|
|
// get the first range in the selection. Since it is
|
|
|
|
// unclear what to do if reconversion happens with a
|
|
|
|
// multirange selection, we will ignore any additional ranges.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
res = selection->GetRangeAt(0, getter_AddRefs(range));
|
|
|
|
if (NS_FAILED(res) || !range)
|
|
|
|
return (res == NS_OK) ? NS_ERROR_FAILURE : res;
|
|
|
|
|
|
|
|
nsAutoString textValue;
|
|
|
|
res = range->ToString(textValue);
|
|
|
|
if (NS_FAILED(res))
|
|
|
|
return res;
|
|
|
|
|
2001-06-30 11:02:25 +00:00
|
|
|
aReply->mReconversionString = (PRUnichar*) nsMemory::Clone(textValue.get(),
|
2000-08-04 21:43:29 +00:00
|
|
|
(textValue.Length() + 1) * sizeof(PRUnichar));
|
|
|
|
if (!aReply->mReconversionString)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// delete the selection
|
|
|
|
res = DeleteSelection(eNone);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
2000-03-24 00:26:47 +00:00
|
|
|
#pragma mark StyleSheet utils
|
1999-08-09 01:37:50 +00:00
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::StyleSheetLoaded(nsICSSStyleSheet* aSheet, PRBool aNotify)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2002-09-13 19:47:38 +00:00
|
|
|
nsresult rv = NS_OK;
|
1999-09-29 20:08:15 +00:00
|
|
|
nsAutoEditBatch batchIt(this);
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
if (!mLastStyleSheetURL.IsEmpty())
|
|
|
|
RemoveStyleSheet(mLastStyleSheetURL);
|
|
|
|
|
|
|
|
AddStyleSheetTxn* txn;
|
|
|
|
rv = CreateTxnForAddStyleSheet(aSheet, &txn);
|
|
|
|
if (!txn) rv = NS_ERROR_NULL_POINTER;
|
|
|
|
if (NS_SUCCEEDED(rv))
|
1999-04-22 14:45:48 +00:00
|
|
|
{
|
2003-04-04 20:50:25 +00:00
|
|
|
rv = DoTransaction(txn);
|
2002-09-13 19:47:38 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
// Get the URI, then url spec from the sheet
|
|
|
|
nsCOMPtr<nsIStyleSheet> sheet = do_QueryInterface(aSheet);
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
rv = sheet->GetURL(*getter_AddRefs(uri));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
nsCAutoString spec;
|
|
|
|
rv = uri->GetSpec(spec);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
// Save it so we can remove before applying the next one
|
|
|
|
mLastStyleSheetURL.AssignWithConversion(spec.get());
|
|
|
|
|
|
|
|
// Also save in our arrays of urls and sheets
|
|
|
|
AddNewStyleSheetToList(mLastStyleSheetURL, aSheet);
|
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
2002-09-13 19:47:38 +00:00
|
|
|
// The transaction system (if any) has taken ownwership of txns
|
|
|
|
NS_IF_RELEASE(txn);
|
1999-08-09 01:37:50 +00:00
|
|
|
|
1999-12-04 01:29:18 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
|
1999-12-07 08:30:19 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
2000-03-24 00:26:47 +00:00
|
|
|
#pragma mark nsEditor overrides
|
1999-12-07 08:30:19 +00:00
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/** All editor operations which alter the doc should be prefaced
|
|
|
|
* with a call to StartOperation, naming the action and direction */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::StartOperation(PRInt32 opID, nsIEditor::EDirection aDirection)
|
|
|
|
{
|
2000-08-14 02:39:37 +00:00
|
|
|
nsEditor::StartOperation(opID, aDirection); // will set mAction, mDirection
|
|
|
|
if (! ((mAction==kOpInsertText) || (mAction==kOpInsertIMEText)) )
|
2000-01-31 10:30:12 +00:00
|
|
|
ClearInlineStylesCache();
|
2000-08-14 02:39:37 +00:00
|
|
|
if (mRules) return mRules->BeforeEdit(mAction, mDirection);
|
1999-12-07 08:30:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** All editor operations which alter the doc should be followed
|
2000-08-14 02:39:37 +00:00
|
|
|
* with a call to EndOperation */
|
1999-12-07 08:30:19 +00:00
|
|
|
NS_IMETHODIMP
|
2000-08-14 02:39:37 +00:00
|
|
|
nsHTMLEditor::EndOperation()
|
1999-12-07 08:30:19 +00:00
|
|
|
{
|
2000-08-14 02:39:37 +00:00
|
|
|
// post processing
|
|
|
|
if (! ((mAction==kOpInsertText) || (mAction==kOpInsertIMEText) || (mAction==kOpIgnore)) )
|
2000-01-31 10:30:12 +00:00
|
|
|
ClearInlineStylesCache();
|
2000-08-14 02:39:37 +00:00
|
|
|
nsresult res = NS_OK;
|
|
|
|
if (mRules) res = mRules->AfterEdit(mAction, mDirection);
|
|
|
|
nsEditor::EndOperation(); // will clear mAction, mDirection
|
|
|
|
return res;
|
|
|
|
}
|
1999-12-07 08:30:19 +00:00
|
|
|
|
2000-01-10 10:13:58 +00:00
|
|
|
PRBool
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::TagCanContainTag(const nsAString& aParentTag, const nsAString& aChildTag)
|
2000-01-10 10:13:58 +00:00
|
|
|
{
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
// COtherDTD gives some unwanted results. We override them here.
|
2002-02-10 15:49:46 +00:00
|
|
|
if (aParentTag.Equals(NS_LITERAL_STRING("ol"),nsCaseInsensitiveStringComparator()) ||
|
|
|
|
aParentTag.Equals(NS_LITERAL_STRING("ul"),nsCaseInsensitiveStringComparator()))
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
{
|
|
|
|
// if parent is a list and tag is also a list, say "yes".
|
|
|
|
// This is because the editor does sublists illegally for now.
|
2002-02-10 15:49:46 +00:00
|
|
|
if (aChildTag.Equals(NS_LITERAL_STRING("ol"),nsCaseInsensitiveStringComparator()) ||
|
|
|
|
aChildTag.Equals(NS_LITERAL_STRING("ul"),nsCaseInsensitiveStringComparator()))
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
return PR_TRUE;
|
|
|
|
}
|
2000-06-29 09:23:41 +00:00
|
|
|
|
2002-02-10 15:49:46 +00:00
|
|
|
if (aParentTag.Equals(NS_LITERAL_STRING("li"),nsCaseInsensitiveStringComparator()))
|
2000-06-29 09:23:41 +00:00
|
|
|
{
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
// list items cant contain list items
|
2002-02-10 15:49:46 +00:00
|
|
|
if (aChildTag.Equals(NS_LITERAL_STRING("li"),nsCaseInsensitiveStringComparator()))
|
2000-06-29 09:23:41 +00:00
|
|
|
return PR_FALSE;
|
|
|
|
}
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
|
|
|
/*
|
2000-06-29 09:23:41 +00:00
|
|
|
// if parent is a pre, and child is not inline, say "no"
|
2001-10-13 00:16:32 +00:00
|
|
|
if ( aParentTag.Equals(NS_LITERAL_STRING("pre")) )
|
2000-06-29 09:23:41 +00:00
|
|
|
{
|
2001-10-13 00:16:32 +00:00
|
|
|
if (aChildTag.Equals(NS_LITERAL_STRING("__moz_text")))
|
2000-06-29 09:23:41 +00:00
|
|
|
return PR_TRUE;
|
|
|
|
PRInt32 childTagEnum, parentTagEnum;
|
|
|
|
nsAutoString non_const_childTag(aChildTag);
|
|
|
|
nsAutoString non_const_parentTag(aParentTag);
|
|
|
|
nsresult res = mDTD->StringTagToIntTag(non_const_childTag,&childTagEnum);
|
|
|
|
if (NS_FAILED(res)) return PR_FALSE;
|
|
|
|
res = mDTD->StringTagToIntTag(non_const_parentTag,&parentTagEnum);
|
|
|
|
if (NS_FAILED(res)) return PR_FALSE;
|
|
|
|
if (!mDTD->IsInlineElement(childTagEnum,parentTagEnum))
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
*/
|
2000-01-10 10:13:58 +00:00
|
|
|
// else fall thru
|
2000-06-29 09:23:41 +00:00
|
|
|
return nsEditor::TagCanContainTag(aParentTag, aChildTag);
|
2000-01-10 10:13:58 +00:00
|
|
|
}
|
|
|
|
|
1999-12-07 08:30:19 +00:00
|
|
|
|
2000-01-13 10:17:35 +00:00
|
|
|
NS_IMETHODIMP
|
2000-09-14 11:45:01 +00:00
|
|
|
nsHTMLEditor::SelectEntireDocument(nsISelection *aSelection)
|
2000-01-13 10:17:35 +00:00
|
|
|
{
|
|
|
|
nsresult res;
|
|
|
|
if (!aSelection || !mRules) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
|
|
|
|
// get body node
|
|
|
|
nsCOMPtr<nsIDOMElement>bodyElement;
|
2000-05-04 08:33:48 +00:00
|
|
|
res = GetRootElement(getter_AddRefs(bodyElement));
|
2000-01-13 10:17:35 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
nsCOMPtr<nsIDOMNode>bodyNode = do_QueryInterface(bodyElement);
|
|
|
|
if (!bodyNode) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// is doc empty?
|
|
|
|
PRBool bDocIsEmpty;
|
|
|
|
res = mRules->DocumentIsEmpty(&bDocIsEmpty);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (bDocIsEmpty)
|
|
|
|
{
|
|
|
|
// if its empty dont select entire doc - that would select the bogus node
|
|
|
|
return aSelection->Collapse(bodyNode, 0);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
return nsEditor::SelectEntireDocument(aSelection);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
2000-03-24 00:26:47 +00:00
|
|
|
#pragma mark Random methods
|
1999-08-09 01:37:50 +00:00
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::GetLayoutObject(nsIDOMNode *aNode, nsISupports **aLayoutObject)
|
|
|
|
{
|
|
|
|
nsresult result = NS_ERROR_FAILURE; // we return an error unless we get the index
|
1999-08-25 10:51:55 +00:00
|
|
|
if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!ps) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
if ((nsnull!=aNode))
|
|
|
|
{ // get the content interface
|
|
|
|
nsCOMPtr<nsIContent> nodeAsContent( do_QueryInterface(aNode) );
|
|
|
|
if (nodeAsContent)
|
|
|
|
{ // get the frame from the content interface
|
|
|
|
//Note: frames are not ref counted, so don't use an nsCOMPtr
|
|
|
|
*aLayoutObject = nsnull;
|
|
|
|
result = ps->GetLayoutObjectFor(nodeAsContent, aLayoutObject);
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
1999-08-25 10:51:55 +00:00
|
|
|
else {
|
|
|
|
result = NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// this will NOT find aAttribute unless aAttribute has a non-null value
|
|
|
|
// so singleton attributes like <Table border> will not be matched!
|
2001-11-29 10:08:25 +00:00
|
|
|
void nsHTMLEditor::IsTextPropertySetByContent(nsIDOMNode *aNode,
|
|
|
|
nsIAtom *aProperty,
|
2002-03-23 22:08:20 +00:00
|
|
|
const nsAString *aAttribute,
|
|
|
|
const nsAString *aValue,
|
2001-11-29 10:08:25 +00:00
|
|
|
PRBool &aIsSet,
|
|
|
|
nsIDOMNode **aStyleNode,
|
2003-06-02 22:21:06 +00:00
|
|
|
nsAString *outValue)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
aIsSet = PR_FALSE; // must be initialized to false for code below to work
|
|
|
|
nsAutoString propName;
|
|
|
|
aProperty->ToString(propName);
|
2000-05-07 01:33:42 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>node = aNode;
|
|
|
|
|
|
|
|
while (node)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMElement>element;
|
2000-05-07 01:33:42 +00:00
|
|
|
element = do_QueryInterface(node);
|
1999-08-09 01:37:50 +00:00
|
|
|
if (element)
|
|
|
|
{
|
fixes:
14753, 29843, 39864, 40141,
40139, 36679, 39542, 34729,
34855, 37216, 39292, 26447
r=sfraser,cmanske,fm; a=beppe
2000-05-24 23:00:24 +00:00
|
|
|
nsAutoString tag, value;
|
1999-08-09 01:37:50 +00:00
|
|
|
element->GetTagName(tag);
|
2002-02-19 22:49:12 +00:00
|
|
|
if (propName.Equals(tag, nsCaseInsensitiveStringComparator()))
|
1999-04-22 14:45:48 +00:00
|
|
|
{
|
1999-08-09 01:37:50 +00:00
|
|
|
PRBool found = PR_FALSE;
|
|
|
|
if (aAttribute && 0!=aAttribute->Length())
|
|
|
|
{
|
|
|
|
element->GetAttribute(*aAttribute, value);
|
fixes:
14753, 29843, 39864, 40141,
40139, 36679, 39542, 34729,
34855, 37216, 39292, 26447
r=sfraser,cmanske,fm; a=beppe
2000-05-24 23:00:24 +00:00
|
|
|
if (outValue) *outValue = value;
|
2003-05-23 21:34:47 +00:00
|
|
|
if (!value.IsEmpty())
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
|
|
|
if (!aValue) {
|
|
|
|
found = PR_TRUE;
|
|
|
|
}
|
2001-04-07 00:45:26 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nsString tString(*aValue);
|
2002-02-19 22:49:12 +00:00
|
|
|
if (tString.Equals(value, nsCaseInsensitiveStringComparator())) {
|
2001-04-07 00:45:26 +00:00
|
|
|
found = PR_TRUE;
|
|
|
|
}
|
|
|
|
else { // we found the prop with the attribute, but the value doesn't match
|
|
|
|
break;
|
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
found = PR_TRUE;
|
|
|
|
}
|
2000-03-24 00:26:47 +00:00
|
|
|
if (found)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
|
|
|
aIsSet = PR_TRUE;
|
|
|
|
break;
|
|
|
|
}
|
1999-05-05 04:05:19 +00:00
|
|
|
}
|
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>temp;
|
2000-05-07 01:33:42 +00:00
|
|
|
result = node->GetParentNode(getter_AddRefs(temp));
|
1999-08-09 01:37:50 +00:00
|
|
|
if (NS_SUCCEEDED(result) && temp) {
|
2000-05-07 01:33:42 +00:00
|
|
|
node = do_QueryInterface(temp);
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2000-05-07 01:33:42 +00:00
|
|
|
node = do_QueryInterface(nsnull);
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
1999-05-05 04:05:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
//================================================================
|
|
|
|
// HTML Editor methods
|
|
|
|
//
|
|
|
|
// Note: Table Editing methods are implemented in nsTableEditor.cpp
|
|
|
|
//
|
1999-08-09 01:37:50 +00:00
|
|
|
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
PRBool nsHTMLEditor::IsElementInBody(nsIDOMElement* aElement)
|
|
|
|
{
|
2001-04-05 23:48:01 +00:00
|
|
|
return nsTextEditUtils::InBody(aElement, this);
|
1999-09-13 20:44:38 +00:00
|
|
|
}
|
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
PRBool
|
|
|
|
nsHTMLEditor::SetCaretInTableCell(nsIDOMElement* aElement)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2000-03-29 12:53:23 +00:00
|
|
|
PRBool caretIsSet = PR_FALSE;
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
if (aElement && IsElementInBody(aElement))
|
1999-05-07 22:26:23 +00:00
|
|
|
{
|
2000-03-29 12:53:23 +00:00
|
|
|
nsresult res = NS_OK;
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
|
|
|
|
if (content)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2000-03-29 12:53:23 +00:00
|
|
|
nsCOMPtr<nsIAtom> atom;
|
2003-06-13 20:10:01 +00:00
|
|
|
content->GetTag(getter_AddRefs(atom));
|
2003-04-17 05:42:33 +00:00
|
|
|
if (atom.get() == nsEditProperty::table ||
|
|
|
|
atom.get() == nsEditProperty::tbody ||
|
|
|
|
atom.get() == nsEditProperty::thead ||
|
|
|
|
atom.get() == nsEditProperty::tfoot ||
|
|
|
|
atom.get() == nsEditProperty::caption ||
|
|
|
|
atom.get() == nsEditProperty::tr ||
|
|
|
|
atom.get() == nsEditProperty::td )
|
1999-08-19 13:30:48 +00:00
|
|
|
{
|
2000-03-29 12:53:23 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aElement);
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
// This MUST succeed if IsElementInBody was TRUE
|
|
|
|
node->GetParentNode(getter_AddRefs(parent));
|
|
|
|
nsCOMPtr<nsIDOMNode>firstChild;
|
|
|
|
// Find deepest child
|
|
|
|
PRBool hasChild;
|
|
|
|
while (NS_SUCCEEDED(node->HasChildNodes(&hasChild)) && hasChild)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2000-03-29 12:53:23 +00:00
|
|
|
if (NS_SUCCEEDED(node->GetFirstChild(getter_AddRefs(firstChild))))
|
|
|
|
{
|
|
|
|
parent = node;
|
|
|
|
node = firstChild;
|
1999-05-26 21:40:51 +00:00
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
2000-03-29 12:53:23 +00:00
|
|
|
// Set selection at beginning of deepest node
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2000-03-29 12:53:23 +00:00
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_SUCCEEDED(res) && selection && firstChild)
|
|
|
|
{
|
|
|
|
res = selection->Collapse(firstChild, 0);
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
caretIsSet = PR_TRUE;
|
|
|
|
}
|
1999-05-26 21:40:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-03-29 12:53:23 +00:00
|
|
|
return caretIsSet;
|
|
|
|
}
|
1999-08-19 13:30:48 +00:00
|
|
|
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-03-29 12:53:23 +00:00
|
|
|
nsHTMLEditor::IsRootTag(nsString &aTag, PRBool &aIsTag)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2000-03-29 12:53:23 +00:00
|
|
|
static char bodyTag[] = "body";
|
|
|
|
static char tdTag[] = "td";
|
|
|
|
static char thTag[] = "th";
|
|
|
|
static char captionTag[] = "caption";
|
|
|
|
if (aTag.EqualsIgnoreCase(bodyTag) ||
|
|
|
|
aTag.EqualsIgnoreCase(tdTag) ||
|
|
|
|
aTag.EqualsIgnoreCase(thTag) ||
|
|
|
|
aTag.EqualsIgnoreCase(captionTag) )
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2000-03-29 12:53:23 +00:00
|
|
|
aIsTag = PR_TRUE;
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2000-03-29 12:53:23 +00:00
|
|
|
aIsTag = PR_FALSE;
|
1999-07-14 15:24:33 +00:00
|
|
|
}
|
1999-09-23 04:01:10 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::IsSubordinateBlock(nsString &aTag, PRBool &aIsTag)
|
|
|
|
{
|
|
|
|
static char p[] = "p";
|
|
|
|
static char h1[] = "h1";
|
|
|
|
static char h2[] = "h2";
|
|
|
|
static char h3[] = "h3";
|
|
|
|
static char h4[] = "h4";
|
|
|
|
static char h5[] = "h5";
|
|
|
|
static char h6[] = "h6";
|
|
|
|
static char address[] = "address";
|
|
|
|
static char pre[] = "pre";
|
|
|
|
static char li[] = "li";
|
|
|
|
static char dt[] = "dt";
|
|
|
|
static char dd[] = "dd";
|
|
|
|
if (aTag.EqualsIgnoreCase(p) ||
|
|
|
|
aTag.EqualsIgnoreCase(h1) ||
|
|
|
|
aTag.EqualsIgnoreCase(h2) ||
|
|
|
|
aTag.EqualsIgnoreCase(h3) ||
|
|
|
|
aTag.EqualsIgnoreCase(h4) ||
|
|
|
|
aTag.EqualsIgnoreCase(h5) ||
|
|
|
|
aTag.EqualsIgnoreCase(h6) ||
|
|
|
|
aTag.EqualsIgnoreCase(address) ||
|
|
|
|
aTag.EqualsIgnoreCase(pre) ||
|
|
|
|
aTag.EqualsIgnoreCase(li) ||
|
|
|
|
aTag.EqualsIgnoreCase(dt) ||
|
|
|
|
aTag.EqualsIgnoreCase(dd) )
|
|
|
|
{
|
|
|
|
aIsTag = PR_TRUE;
|
1999-08-19 13:30:48 +00:00
|
|
|
}
|
|
|
|
else {
|
2000-03-29 12:53:23 +00:00
|
|
|
aIsTag = PR_FALSE;
|
1999-08-19 13:30:48 +00:00
|
|
|
}
|
2000-03-29 12:53:23 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-08-19 13:30:48 +00:00
|
|
|
|
1999-05-07 22:26:23 +00:00
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetEnclosingTable: find ancestor who is a table, if any
|
|
|
|
//
|
|
|
|
nsCOMPtr<nsIDOMNode>
|
|
|
|
nsHTMLEditor::GetEnclosingTable(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aNode, "null node passed to nsHTMLEditor::GetEnclosingTable");
|
|
|
|
nsCOMPtr<nsIDOMNode> tbl, tmp, node = aNode;
|
|
|
|
|
|
|
|
while (!tbl)
|
1999-07-19 19:06:39 +00:00
|
|
|
{
|
2000-03-29 12:53:23 +00:00
|
|
|
tmp = GetBlockNodeParent(node);
|
|
|
|
if (!tmp) break;
|
2000-08-13 23:53:34 +00:00
|
|
|
if (nsHTMLEditUtils::IsTable(tmp)) tbl = tmp;
|
2000-03-29 12:53:23 +00:00
|
|
|
node = tmp;
|
1999-07-19 19:06:39 +00:00
|
|
|
}
|
2000-03-29 12:53:23 +00:00
|
|
|
return tbl;
|
1999-07-19 19:06:39 +00:00
|
|
|
}
|
1999-05-17 12:22:31 +00:00
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
void nsHTMLEditor::ClearInlineStylesCache()
|
|
|
|
{
|
|
|
|
mCachedNode = nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PRE_NODE_IN_BODY
|
|
|
|
nsCOMPtr<nsIDOMElement> nsHTMLEditor::FindPreElement()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc;
|
|
|
|
nsEditor::GetDocument(getter_AddRefs(domdoc));
|
|
|
|
if (!domdoc)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc (do_QueryInterface(domdoc));
|
|
|
|
if (!doc)
|
|
|
|
return 0;
|
|
|
|
|
2001-06-20 03:27:48 +00:00
|
|
|
nsCOMPtr<nsIContent> rootContent;
|
|
|
|
doc->GetRootContent(getter_AddRefs(rootContent));
|
2000-03-29 12:53:23 +00:00
|
|
|
if (!rootContent)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> rootNode (do_QueryInterface(rootContent));
|
|
|
|
if (!rootNode)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
nsString prestr ("PRE"); // GetFirstNodeOfType requires capitals
|
|
|
|
nsCOMPtr<nsIDOMNode> preNode;
|
2001-11-19 06:45:00 +00:00
|
|
|
if (NS_FAILED(nsEditor::GetFirstNodeOfType(rootNode, prestr,
|
2000-03-29 12:53:23 +00:00
|
|
|
getter_AddRefs(preNode))))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return do_QueryInterface(preNode);
|
|
|
|
}
|
|
|
|
#endif /* PRE_NODE_IN_BODY */
|
|
|
|
|
|
|
|
void nsHTMLEditor::HandleEventListenerError()
|
|
|
|
{
|
|
|
|
// null out the nsCOMPtrs
|
|
|
|
mKeyListenerP = nsnull;
|
|
|
|
mMouseListenerP = nsnull;
|
|
|
|
mTextListenerP = nsnull;
|
|
|
|
mDragListenerP = nsnull;
|
|
|
|
mCompositionListenerP = nsnull;
|
|
|
|
mFocusListenerP = nsnull;
|
|
|
|
}
|
|
|
|
|
1999-08-19 15:15:41 +00:00
|
|
|
/* this method scans the selection for adjacent text nodes
|
|
|
|
* and collapses them into a single text node.
|
|
|
|
* "adjacent" means literally adjacent siblings of the same parent.
|
|
|
|
* Uses nsEditor::JoinNodes so action is undoable.
|
|
|
|
* Should be called within the context of a batch transaction.
|
1999-08-19 13:30:48 +00:00
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2000-03-29 12:53:23 +00:00
|
|
|
nsHTMLEditor::CollapseAdjacentTextNodes(nsIDOMRange *aInRange)
|
1999-08-19 13:30:48 +00:00
|
|
|
{
|
2000-03-29 12:53:23 +00:00
|
|
|
if (!aInRange) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsAutoTxnsConserveSelection dontSpazMySelection(this);
|
1999-09-29 20:08:15 +00:00
|
|
|
nsVoidArray textNodes; // we can't actually do anything during iteration, so store the text nodes in an array
|
1999-08-19 13:30:48 +00:00
|
|
|
// don't bother ref counting them because we know we can hold them for the
|
|
|
|
// lifetime of this method
|
|
|
|
|
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
// build a list of editable text nodes
|
2003-07-08 20:22:56 +00:00
|
|
|
nsresult result;
|
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1", &result);
|
1999-08-19 13:30:48 +00:00
|
|
|
if (NS_FAILED(result)) return result;
|
2000-03-29 12:53:23 +00:00
|
|
|
if (!iter) return NS_ERROR_NULL_POINTER;
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
iter->Init(aInRange);
|
|
|
|
nsCOMPtr<nsIContent> content;
|
2000-04-20 03:59:50 +00:00
|
|
|
result = iter->CurrentNode(getter_AddRefs(content));
|
|
|
|
if (!content) return NS_OK;
|
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
while (NS_ENUMERATOR_FALSE == iter->IsDone())
|
1999-08-19 13:30:48 +00:00
|
|
|
{
|
2000-03-29 12:53:23 +00:00
|
|
|
nsCOMPtr<nsIDOMCharacterData> text = do_QueryInterface(content);
|
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(content);
|
|
|
|
if (text && node && IsEditable(node))
|
1999-08-19 13:30:48 +00:00
|
|
|
{
|
2000-03-29 12:53:23 +00:00
|
|
|
textNodes.AppendElement((void*)(node.get()));
|
1999-08-19 13:30:48 +00:00
|
|
|
}
|
2000-03-29 12:53:23 +00:00
|
|
|
iter->Next();
|
|
|
|
iter->CurrentNode(getter_AddRefs(content));
|
1999-08-19 13:30:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now that I have a list of text nodes, collapse adjacent text nodes
|
2000-03-29 12:53:23 +00:00
|
|
|
// NOTE: assumption that JoinNodes keeps the righthand node
|
2001-12-21 01:10:07 +00:00
|
|
|
while (textNodes.Count() > 1)
|
2000-03-29 12:53:23 +00:00
|
|
|
{
|
2001-12-21 01:10:07 +00:00
|
|
|
// we assume a textNodes entry can't be nsnull
|
|
|
|
nsIDOMNode *leftTextNode = (nsIDOMNode *)(textNodes.ElementAt(0));
|
|
|
|
nsIDOMNode *rightTextNode = (nsIDOMNode *)(textNodes.ElementAt(1));
|
|
|
|
NS_ASSERTION(leftTextNode && rightTextNode,"left or rightTextNode null in CollapseAdjacentTextNodes");
|
|
|
|
|
1999-09-29 20:08:15 +00:00
|
|
|
// get the prev sibling of the right node, and see if it's leftTextNode
|
2000-03-29 12:53:23 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> prevSibOfRightNode;
|
2000-12-09 04:46:08 +00:00
|
|
|
result = GetPriorHTMLSibling(rightTextNode, address_of(prevSibOfRightNode));
|
1999-08-19 13:30:48 +00:00
|
|
|
if (NS_FAILED(result)) return result;
|
2000-03-29 12:53:23 +00:00
|
|
|
if (prevSibOfRightNode && (prevSibOfRightNode.get() == leftTextNode))
|
1999-08-19 13:30:48 +00:00
|
|
|
{
|
2000-03-29 12:53:23 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
result = rightTextNode->GetParentNode(getter_AddRefs(parent));
|
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!parent) return NS_ERROR_NULL_POINTER;
|
|
|
|
result = JoinNodes(leftTextNode, rightTextNode, parent);
|
1999-08-19 13:30:48 +00:00
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
textNodes.RemoveElementAt(0); // remove the leftmost text node from the list
|
|
|
|
}
|
|
|
|
|
1999-09-29 20:08:15 +00:00
|
|
|
return result;
|
1999-08-19 13:30:48 +00:00
|
|
|
}
|
1999-09-10 18:54:13 +00:00
|
|
|
|
2000-01-26 14:57:43 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetNextElementByTagName(nsIDOMElement *aCurrentElement,
|
2002-03-23 22:08:20 +00:00
|
|
|
const nsAString *aTagName,
|
2000-01-26 14:57:43 +00:00
|
|
|
nsIDOMElement **aReturn)
|
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
if (!aCurrentElement || !aTagName || !aReturn)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2003-07-24 04:53:13 +00:00
|
|
|
nsCOMPtr<nsIAtom> tagAtom = do_GetAtom(*aTagName);
|
2000-01-26 14:57:43 +00:00
|
|
|
if (!tagAtom) { return NS_ERROR_NULL_POINTER; }
|
2003-04-17 05:42:33 +00:00
|
|
|
if (tagAtom==nsEditProperty::th)
|
|
|
|
tagAtom=nsEditProperty::td;
|
2000-01-26 14:57:43 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> currentNode = do_QueryInterface(aCurrentElement);
|
|
|
|
if (!currentNode)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
*aReturn = nsnull;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> nextNode;
|
|
|
|
PRBool done = PR_FALSE;
|
|
|
|
|
|
|
|
do {
|
2001-08-22 05:32:44 +00:00
|
|
|
res = GetNextNode(currentNode, PR_TRUE, address_of(nextNode));
|
2000-01-26 14:57:43 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!nextNode) break;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAtom> atom = GetTag(currentNode);
|
|
|
|
|
2003-07-24 04:53:13 +00:00
|
|
|
if (tagAtom == atom)
|
2000-01-26 14:57:43 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(currentNode);
|
|
|
|
if (!element) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*aReturn = element;
|
|
|
|
NS_ADDREF(*aReturn);
|
|
|
|
done = PR_TRUE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
currentNode = nextNode;
|
|
|
|
} while (!done);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
2000-02-08 01:11:13 +00:00
|
|
|
|
2000-02-10 05:14:52 +00:00
|
|
|
NS_IMETHODIMP
|
2000-09-14 11:45:01 +00:00
|
|
|
nsHTMLEditor::SetSelectionAtDocumentStart(nsISelection *aSelection)
|
2000-02-10 05:14:52 +00:00
|
|
|
{
|
2000-03-24 00:26:47 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> bodyElement;
|
2000-05-04 08:33:48 +00:00
|
|
|
nsresult res = GetRootElement(getter_AddRefs(bodyElement));
|
2000-03-24 00:26:47 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
2000-02-10 05:14:52 +00:00
|
|
|
{
|
2002-02-19 12:02:09 +00:00
|
|
|
if (!bodyElement) return NS_ERROR_NULL_POINTER;
|
2000-02-10 05:14:52 +00:00
|
|
|
res = aSelection->Collapse(bodyElement,0);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2000-02-08 12:53:34 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
2001-06-18 21:15:43 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// RemoveBlockContainer: remove inNode, reparenting it's children into their
|
|
|
|
// the parent of inNode. In addition, INSERT ANY BR's NEEDED
|
|
|
|
// TO PRESERVE IDENTITY OF REMOVED BLOCK.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::RemoveBlockContainer(nsIDOMNode *inNode)
|
|
|
|
{
|
|
|
|
if (!inNode)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsIDOMNode> sibling, child, unused;
|
|
|
|
|
|
|
|
// Two possibilities: the container cold be empty of editable content.
|
|
|
|
// If that is the case, we need to compare what is before and after inNode
|
|
|
|
// to determine if we need a br.
|
|
|
|
// Or it could not be empty, in which case we have to compare previous
|
|
|
|
// sibling and first child to determine if we need a leading br,
|
|
|
|
// and compare following sibling and last child to determine if we need a
|
|
|
|
// trailing br.
|
|
|
|
|
|
|
|
res = GetFirstEditableChild(inNode, address_of(child));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (child) // the case of inNode not being empty
|
|
|
|
{
|
|
|
|
// we need a br at start unless:
|
|
|
|
// 1) previous sibling of inNode is a block, OR
|
|
|
|
// 2) previous sibling of inNode is a br, OR
|
|
|
|
// 3) first child of inNode is a block OR
|
|
|
|
// 4) either is null
|
|
|
|
|
|
|
|
res = GetPriorHTMLSibling(inNode, address_of(sibling));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (sibling && !IsBlockNode(sibling) && !nsTextEditUtils::IsBreak(sibling))
|
|
|
|
{
|
|
|
|
res = GetFirstEditableChild(inNode, address_of(child));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (child && !IsBlockNode(child))
|
|
|
|
{
|
|
|
|
// insert br node
|
|
|
|
res = CreateBR(inNode, 0, address_of(unused));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// we need a br at end unless:
|
|
|
|
// 1) following sibling of inNode is a block, OR
|
|
|
|
// 2) last child of inNode is a block, OR
|
|
|
|
// 3) last child of inNode is a block OR
|
|
|
|
// 4) either is null
|
|
|
|
|
|
|
|
res = GetNextHTMLSibling(inNode, address_of(sibling));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (sibling && !IsBlockNode(sibling))
|
|
|
|
{
|
|
|
|
res = GetLastEditableChild(inNode, address_of(child));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (child && !IsBlockNode(child) && !nsTextEditUtils::IsBreak(child))
|
|
|
|
{
|
|
|
|
// insert br node
|
|
|
|
PRUint32 len;
|
|
|
|
res = GetLengthOfDOMNode(inNode, len);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = CreateBR(inNode, (PRInt32)len, address_of(unused));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else // the case of inNode being empty
|
|
|
|
{
|
|
|
|
// we need a br at start unless:
|
|
|
|
// 1) previous sibling of inNode is a block, OR
|
|
|
|
// 2) previous sibling of inNode is a br, OR
|
|
|
|
// 3) following sibling of inNode is a block, OR
|
|
|
|
// 4) following sibling of inNode is a br OR
|
|
|
|
// 5) either is null
|
|
|
|
res = GetPriorHTMLSibling(inNode, address_of(sibling));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (sibling && !IsBlockNode(sibling) && !nsTextEditUtils::IsBreak(sibling))
|
|
|
|
{
|
|
|
|
res = GetNextHTMLSibling(inNode, address_of(sibling));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (sibling && !IsBlockNode(sibling) && !nsTextEditUtils::IsBreak(sibling))
|
|
|
|
{
|
|
|
|
// insert br node
|
|
|
|
res = CreateBR(inNode, 0, address_of(unused));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now remove container
|
|
|
|
return RemoveContainer(inNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetPriorHTMLSibling: returns the previous editable sibling, if there is
|
|
|
|
// one within the parent
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetPriorHTMLSibling(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode)
|
|
|
|
{
|
|
|
|
if (!outNode || !inNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
*outNode = nsnull;
|
|
|
|
nsCOMPtr<nsIDOMNode> temp, node = do_QueryInterface(inNode);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
res = node->GetPreviousSibling(getter_AddRefs(temp));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!temp) return NS_OK; // return null sibling
|
|
|
|
// if it's editable, we're done
|
|
|
|
if (IsEditable(temp)) break;
|
|
|
|
// otherwise try again
|
|
|
|
node = temp;
|
|
|
|
}
|
|
|
|
*outNode = temp;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-08 12:53:34 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2000-03-24 00:26:47 +00:00
|
|
|
// GetPriorHTMLSibling: returns the previous editable sibling, if there is
|
|
|
|
// one within the parent. just like above routine but
|
|
|
|
// takes a parent/offset instead of a node.
|
|
|
|
//
|
2000-02-08 12:53:34 +00:00
|
|
|
nsresult
|
2000-03-24 00:26:47 +00:00
|
|
|
nsHTMLEditor::GetPriorHTMLSibling(nsIDOMNode *inParent, PRInt32 inOffset, nsCOMPtr<nsIDOMNode> *outNode)
|
2000-02-08 12:53:34 +00:00
|
|
|
{
|
2000-03-24 00:26:47 +00:00
|
|
|
if (!outNode || !inParent) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
*outNode = nsnull;
|
|
|
|
if (!inOffset) return NS_OK; // return null sibling if at offset zero
|
|
|
|
nsCOMPtr<nsIDOMNode> node = nsEditor::GetChildAt(inParent,inOffset-1);
|
|
|
|
if (IsEditable(node))
|
|
|
|
{
|
|
|
|
*outNode = node;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
// else
|
|
|
|
return GetPriorHTMLSibling(node, outNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetNextHTMLSibling: returns the next editable sibling, if there is
|
|
|
|
// one within the parent
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetNextHTMLSibling(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode)
|
|
|
|
{
|
|
|
|
if (!outNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
*outNode = nsnull;
|
|
|
|
nsCOMPtr<nsIDOMNode> temp, node = do_QueryInterface(inNode);
|
|
|
|
|
|
|
|
while (1)
|
|
|
|
{
|
|
|
|
res = node->GetNextSibling(getter_AddRefs(temp));
|
|
|
|
if (NS_FAILED(res)) return res;
|
2001-06-18 21:15:43 +00:00
|
|
|
if (!temp) return NS_OK; // return null sibling
|
2000-03-24 00:26:47 +00:00
|
|
|
// if it's editable, we're done
|
|
|
|
if (IsEditable(temp)) break;
|
|
|
|
// otherwise try again
|
|
|
|
node = temp;
|
|
|
|
}
|
|
|
|
*outNode = temp;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetNextHTMLSibling: returns the next editable sibling, if there is
|
|
|
|
// one within the parent. just like above routine but
|
|
|
|
// takes a parent/offset instead of a node.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetNextHTMLSibling(nsIDOMNode *inParent, PRInt32 inOffset, nsCOMPtr<nsIDOMNode> *outNode)
|
|
|
|
{
|
|
|
|
if (!outNode || !inParent) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
*outNode = nsnull;
|
|
|
|
nsCOMPtr<nsIDOMNode> node = nsEditor::GetChildAt(inParent,inOffset);
|
|
|
|
if (!node) return NS_OK; // return null sibling if no sibling
|
|
|
|
if (IsEditable(node))
|
|
|
|
{
|
|
|
|
*outNode = node;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
// else
|
|
|
|
return GetPriorHTMLSibling(node, outNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetPriorHTMLNode: returns the previous editable leaf node, if there is
|
|
|
|
// one within the <body>
|
|
|
|
//
|
|
|
|
nsresult
|
2001-08-22 05:32:44 +00:00
|
|
|
nsHTMLEditor::GetPriorHTMLNode(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode, PRBool bNoBlockCrossing)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
|
|
|
if (!outNode) return NS_ERROR_NULL_POINTER;
|
2001-08-22 05:32:44 +00:00
|
|
|
nsresult res = GetPriorNode(inNode, PR_TRUE, address_of(*outNode), bNoBlockCrossing);
|
2000-02-08 12:53:34 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
|
|
|
// if it's not in the body, then zero it out
|
2001-04-05 23:48:01 +00:00
|
|
|
if (*outNode && !nsTextEditUtils::InBody(*outNode, this))
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
|
|
|
*outNode = nsnull;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetPriorHTMLNode: same as above but takes {parent,offset} instead of node
|
|
|
|
//
|
|
|
|
nsresult
|
2001-08-22 05:32:44 +00:00
|
|
|
nsHTMLEditor::GetPriorHTMLNode(nsIDOMNode *inParent, PRInt32 inOffset, nsCOMPtr<nsIDOMNode> *outNode, PRBool bNoBlockCrossing)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
|
|
|
if (!outNode) return NS_ERROR_NULL_POINTER;
|
2001-08-22 05:32:44 +00:00
|
|
|
nsresult res = GetPriorNode(inParent, inOffset, PR_TRUE, address_of(*outNode), bNoBlockCrossing);
|
2000-02-08 12:53:34 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
|
|
|
// if it's not in the body, then zero it out
|
2001-04-05 23:48:01 +00:00
|
|
|
if (*outNode && !nsTextEditUtils::InBody(*outNode, this))
|
2000-02-08 12:53:34 +00:00
|
|
|
{
|
2000-03-24 00:26:47 +00:00
|
|
|
*outNode = nsnull;
|
2000-02-08 12:53:34 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2000-08-26 04:03:50 +00:00
|
|
|
// GetNextHTMLNode: returns the next editable leaf node, if there is
|
2000-03-24 00:26:47 +00:00
|
|
|
// one within the <body>
|
|
|
|
//
|
2000-02-08 12:53:34 +00:00
|
|
|
nsresult
|
2001-08-22 05:32:44 +00:00
|
|
|
nsHTMLEditor::GetNextHTMLNode(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode, PRBool bNoBlockCrossing)
|
2000-02-08 12:53:34 +00:00
|
|
|
{
|
2000-03-24 00:26:47 +00:00
|
|
|
if (!outNode) return NS_ERROR_NULL_POINTER;
|
2001-08-22 05:32:44 +00:00
|
|
|
nsresult res = GetNextNode(inNode, PR_TRUE, address_of(*outNode), bNoBlockCrossing);
|
2000-02-08 12:53:34 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
// if it's not in the body, then zero it out
|
2001-04-05 23:48:01 +00:00
|
|
|
if (*outNode && !nsTextEditUtils::InBody(*outNode, this))
|
2000-02-08 12:53:34 +00:00
|
|
|
{
|
2000-03-24 00:26:47 +00:00
|
|
|
*outNode = nsnull;
|
2000-02-08 12:53:34 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2000-03-24 00:26:47 +00:00
|
|
|
// GetNHTMLextNode: same as above but takes {parent,offset} instead of node
|
|
|
|
//
|
2000-02-08 12:53:34 +00:00
|
|
|
nsresult
|
2001-08-22 05:32:44 +00:00
|
|
|
nsHTMLEditor::GetNextHTMLNode(nsIDOMNode *inParent, PRInt32 inOffset, nsCOMPtr<nsIDOMNode> *outNode, PRBool bNoBlockCrossing)
|
2000-02-08 12:53:34 +00:00
|
|
|
{
|
2000-03-24 00:26:47 +00:00
|
|
|
if (!outNode) return NS_ERROR_NULL_POINTER;
|
2001-08-22 05:32:44 +00:00
|
|
|
nsresult res = GetNextNode(inParent, inOffset, PR_TRUE, address_of(*outNode), bNoBlockCrossing);
|
2000-02-08 12:53:34 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
// if it's not in the body, then zero it out
|
2001-04-05 23:48:01 +00:00
|
|
|
if (*outNode && !nsTextEditUtils::InBody(*outNode, this))
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
|
|
|
*outNode = nsnull;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::IsFirstEditableChild( nsIDOMNode *aNode, PRBool *aOutIsFirst)
|
|
|
|
{
|
|
|
|
// check parms
|
|
|
|
if (!aOutIsFirst || !aNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// init out parms
|
|
|
|
*aOutIsFirst = PR_FALSE;
|
|
|
|
|
|
|
|
// find first editable child and compare it to aNode
|
|
|
|
nsCOMPtr<nsIDOMNode> parent, firstChild;
|
|
|
|
nsresult res = aNode->GetParentNode(getter_AddRefs(parent));
|
2000-02-08 12:53:34 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-03-24 00:26:47 +00:00
|
|
|
if (!parent) return NS_ERROR_FAILURE;
|
2000-12-09 04:46:08 +00:00
|
|
|
res = GetFirstEditableChild(parent, address_of(firstChild));
|
2000-03-24 00:26:47 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
*aOutIsFirst = (firstChild.get() == aNode);
|
|
|
|
return res;
|
|
|
|
}
|
2000-02-08 12:53:34 +00:00
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::IsLastEditableChild( nsIDOMNode *aNode, PRBool *aOutIsLast)
|
|
|
|
{
|
|
|
|
// check parms
|
|
|
|
if (!aOutIsLast || !aNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// init out parms
|
|
|
|
*aOutIsLast = PR_FALSE;
|
|
|
|
|
|
|
|
// find last editable child and compare it to aNode
|
|
|
|
nsCOMPtr<nsIDOMNode> parent, lastChild;
|
|
|
|
nsresult res = aNode->GetParentNode(getter_AddRefs(parent));
|
2000-02-08 12:53:34 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-03-24 00:26:47 +00:00
|
|
|
if (!parent) return NS_ERROR_FAILURE;
|
2000-12-09 04:46:08 +00:00
|
|
|
res = GetLastEditableChild(parent, address_of(lastChild));
|
2000-02-08 12:53:34 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
|
|
|
*aOutIsLast = (lastChild.get() == aNode);
|
|
|
|
return res;
|
|
|
|
}
|
2000-02-08 12:53:34 +00:00
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetFirstEditableChild( nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutFirstChild)
|
|
|
|
{
|
|
|
|
// check parms
|
|
|
|
if (!aOutFirstChild || !aNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// init out parms
|
|
|
|
*aOutFirstChild = nsnull;
|
|
|
|
|
|
|
|
// find first editable child
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
nsresult res = aNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
while (child && !IsEditable(child))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
res = child->GetNextSibling(getter_AddRefs(tmp));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!tmp) return NS_ERROR_FAILURE;
|
|
|
|
child = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aOutFirstChild = child;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetLastEditableChild( nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutLastChild)
|
|
|
|
{
|
|
|
|
// check parms
|
|
|
|
if (!aOutLastChild || !aNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// init out parms
|
2002-08-29 07:49:31 +00:00
|
|
|
*aOutLastChild = aNode;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
|
|
|
// find last editable child
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
nsresult res = aNode->GetLastChild(getter_AddRefs(child));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
while (child && !IsEditable(child))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
res = child->GetPreviousSibling(getter_AddRefs(tmp));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!tmp) return NS_ERROR_FAILURE;
|
|
|
|
child = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aOutLastChild = child;
|
|
|
|
return res;
|
2000-02-08 12:53:34 +00:00
|
|
|
}
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2000-08-26 04:03:50 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetFirstEditableLeaf( nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutFirstLeaf)
|
|
|
|
{
|
|
|
|
// check parms
|
|
|
|
if (!aOutFirstLeaf || !aNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// init out parms
|
2002-08-29 07:49:31 +00:00
|
|
|
*aOutFirstLeaf = aNode;
|
2000-08-26 04:03:50 +00:00
|
|
|
|
|
|
|
// find leftmost leaf
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
2001-08-22 05:32:44 +00:00
|
|
|
nsresult res = NS_OK;
|
|
|
|
child = GetLeftmostChild(aNode);
|
2002-12-22 01:51:14 +00:00
|
|
|
while (child && (!IsEditable(child) || !nsEditorUtils::IsLeafNode(child)))
|
2000-08-26 04:03:50 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
2000-12-09 04:46:08 +00:00
|
|
|
res = GetNextHTMLNode(child, address_of(tmp));
|
2000-08-26 04:03:50 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!tmp) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// only accept nodes that are descendants of aNode
|
2002-12-22 01:51:14 +00:00
|
|
|
if (nsEditorUtils::IsDescendantOf(tmp, aNode))
|
2000-08-26 04:03:50 +00:00
|
|
|
child = tmp;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
child = nsnull; // this will abort the loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aOutFirstLeaf = child;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetLastEditableLeaf( nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutLastLeaf)
|
|
|
|
{
|
|
|
|
// check parms
|
|
|
|
if (!aOutLastLeaf || !aNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// init out parms
|
|
|
|
*aOutLastLeaf = nsnull;
|
|
|
|
|
2001-08-22 05:32:44 +00:00
|
|
|
// find rightmost leaf
|
2000-08-26 04:03:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
2001-08-22 05:32:44 +00:00
|
|
|
nsresult res = NS_OK;
|
2002-05-14 00:05:37 +00:00
|
|
|
child = GetRightmostChild(aNode, PR_FALSE);
|
2002-12-22 01:51:14 +00:00
|
|
|
while (child && (!IsEditable(child) || !nsEditorUtils::IsLeafNode(child)))
|
2000-08-26 04:03:50 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
2000-12-09 04:46:08 +00:00
|
|
|
res = GetPriorHTMLNode(child, address_of(tmp));
|
2000-08-26 04:03:50 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!tmp) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// only accept nodes that are descendants of aNode
|
2002-12-22 01:51:14 +00:00
|
|
|
if (nsEditorUtils::IsDescendantOf(tmp, aNode))
|
2000-08-26 04:03:50 +00:00
|
|
|
child = tmp;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
child = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aOutLastLeaf = child;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-06-11 11:50:36 +00:00
|
|
|
PRBool
|
|
|
|
nsHTMLEditor::IsTextInDirtyFrameVisible(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
PRBool isEmptyTextNode;
|
|
|
|
nsresult res = IsVisTextNode(aNode, &isEmptyTextNode, PR_FALSE);
|
|
|
|
if (NS_FAILED(res))
|
|
|
|
{
|
|
|
|
// We are following the historical decision:
|
|
|
|
// if we don't know, we say it's visible...
|
|
|
|
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return !isEmptyTextNode;
|
|
|
|
}
|
2002-02-07 03:56:20 +00:00
|
|
|
|
2002-04-14 02:37:23 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsVisTextNode: figure out if textnode aTextNode has any visible content.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::IsVisTextNode( nsIDOMNode *aNode,
|
|
|
|
PRBool *outIsEmptyNode,
|
|
|
|
PRBool aSafeToAskFrames)
|
|
|
|
{
|
|
|
|
if (!aNode || !outIsEmptyNode)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
*outIsEmptyNode = PR_TRUE;
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
|
|
|
|
PRUint32 length = 0;
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(aNode);
|
|
|
|
// callers job to only call us with text nodes
|
|
|
|
if (!nodeAsText)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
nodeAsText->GetLength(&length);
|
|
|
|
if (aSafeToAskFrames)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
res = GetSelectionController(getter_AddRefs(selCon));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selCon) return NS_ERROR_FAILURE;
|
|
|
|
PRBool isVisible = PR_FALSE;
|
|
|
|
// ask the selection controller for information about whether any
|
|
|
|
// of the data in the node is really rendered. This is really
|
|
|
|
// something that frames know about, but we aren't supposed to talk to frames.
|
|
|
|
// So we put a call in the selection controller interface, since it's already
|
|
|
|
// in bed with frames anyway. (this is a fix for bug 22227, and a
|
|
|
|
// partial fix for bug 46209)
|
|
|
|
res = selCon->CheckVisibility(aNode, 0, length, &isVisible);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (isVisible)
|
|
|
|
{
|
|
|
|
*outIsEmptyNode = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (length)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsITextContent> tc = do_QueryInterface(nodeAsText);
|
|
|
|
PRBool justWS = PR_FALSE;
|
|
|
|
tc->IsOnlyWhitespace(&justWS);
|
|
|
|
if (justWS)
|
|
|
|
{
|
|
|
|
nsWSRunObject wsRunObj(this, aNode, 0);
|
|
|
|
nsCOMPtr<nsIDOMNode> visNode;
|
|
|
|
PRInt32 outVisOffset=0;
|
|
|
|
PRInt16 visType=0;
|
|
|
|
res = wsRunObj.NextVisibleNode(aNode, 0, address_of(visNode), &outVisOffset, &visType);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if ( (visType == nsWSRunObject::eNormalWS) ||
|
|
|
|
(visType == nsWSRunObject::eText) )
|
|
|
|
{
|
|
|
|
*outIsEmptyNode = (aNode != visNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*outIsEmptyNode = PR_FALSE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-22 05:39:54 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsEmptyNode: figure out if aNode is an empty node.
|
|
|
|
// A block can have children and still be considered empty,
|
|
|
|
// if the children are empty or non-editable.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::IsEmptyNode( nsIDOMNode *aNode,
|
|
|
|
PRBool *outIsEmptyNode,
|
2001-05-04 21:49:48 +00:00
|
|
|
PRBool aSingleBRDoesntCount,
|
2000-07-27 01:03:16 +00:00
|
|
|
PRBool aListOrCellNotEmpty,
|
|
|
|
PRBool aSafeToAskFrames)
|
2000-06-22 05:39:54 +00:00
|
|
|
{
|
|
|
|
if (!aNode || !outIsEmptyNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
*outIsEmptyNode = PR_TRUE;
|
2002-04-14 02:37:23 +00:00
|
|
|
PRBool seenBR = PR_FALSE;
|
|
|
|
return IsEmptyNodeImpl(aNode, outIsEmptyNode, aSingleBRDoesntCount,
|
|
|
|
aListOrCellNotEmpty, aSafeToAskFrames, &seenBR);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsEmptyNodeImpl: workhorse for IsEmptyNode.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::IsEmptyNodeImpl( nsIDOMNode *aNode,
|
|
|
|
PRBool *outIsEmptyNode,
|
|
|
|
PRBool aSingleBRDoesntCount,
|
|
|
|
PRBool aListOrCellNotEmpty,
|
|
|
|
PRBool aSafeToAskFrames,
|
|
|
|
PRBool *aSeenBR)
|
|
|
|
{
|
|
|
|
if (!aNode || !outIsEmptyNode || !aSeenBR) return NS_ERROR_NULL_POINTER;
|
2002-02-07 03:56:20 +00:00
|
|
|
nsresult res = NS_OK;
|
2002-04-14 02:37:23 +00:00
|
|
|
|
2000-06-22 05:39:54 +00:00
|
|
|
if (nsEditor::IsTextNode(aNode))
|
|
|
|
{
|
2002-04-14 02:37:23 +00:00
|
|
|
res = IsVisTextNode(aNode, outIsEmptyNode, aSafeToAskFrames);
|
|
|
|
return res;
|
2000-06-22 05:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if it's not a text node (handled above) and it's not a container,
|
|
|
|
// then we dont call it empty (it's an <hr>, or <br>, etc).
|
|
|
|
// Also, if it's an anchor then dont treat it as empty - even though
|
|
|
|
// anchors are containers, named anchors are "empty" but we don't
|
|
|
|
// want to treat them as such. Also, don't call ListItems or table
|
2001-12-12 06:02:15 +00:00
|
|
|
// cells empty if caller desires. Form Widgets not empty.
|
2001-06-12 22:49:09 +00:00
|
|
|
if (!IsContainer(aNode) || nsHTMLEditUtils::IsNamedAnchor(aNode) ||
|
2001-12-12 06:02:15 +00:00
|
|
|
nsHTMLEditUtils::IsFormWidget(aNode) ||
|
2000-06-22 05:39:54 +00:00
|
|
|
(aListOrCellNotEmpty && nsHTMLEditUtils::IsListItem(aNode)) ||
|
2000-08-14 00:53:41 +00:00
|
|
|
(aListOrCellNotEmpty && nsHTMLEditUtils::IsTableCell(aNode)) )
|
2000-06-22 05:39:54 +00:00
|
|
|
{
|
|
|
|
*outIsEmptyNode = PR_FALSE;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-04-14 02:37:23 +00:00
|
|
|
|
2001-04-17 10:15:05 +00:00
|
|
|
// need this for later
|
|
|
|
PRBool isListItemOrCell =
|
|
|
|
nsHTMLEditUtils::IsListItem(aNode) || nsHTMLEditUtils::IsTableCell(aNode);
|
|
|
|
|
2002-02-07 03:56:20 +00:00
|
|
|
// loop over children of node. if no children, or all children are either
|
2000-06-22 05:39:54 +00:00
|
|
|
// empty text nodes or non-editable, then node qualifies as empty
|
2002-02-07 03:56:20 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
aNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
|
|
|
|
while (child)
|
2000-06-22 05:39:54 +00:00
|
|
|
{
|
2002-02-07 03:56:20 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> node = child;
|
2000-06-22 05:39:54 +00:00
|
|
|
// is the node editable and non-empty? if so, return false
|
|
|
|
if (nsEditor::IsEditable(node))
|
|
|
|
{
|
|
|
|
if (nsEditor::IsTextNode(node))
|
|
|
|
{
|
2002-04-14 02:37:23 +00:00
|
|
|
res = IsVisTextNode(node, outIsEmptyNode, aSafeToAskFrames);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!*outIsEmptyNode) return NS_OK; // break out if we find we aren't emtpy
|
2000-06-22 05:39:54 +00:00
|
|
|
}
|
2002-02-07 03:56:20 +00:00
|
|
|
else // an editable, non-text node. we need to check it's content.
|
2000-06-22 05:39:54 +00:00
|
|
|
{
|
|
|
|
// is it the node we are iterating over?
|
|
|
|
if (node.get() == aNode) break;
|
2002-04-14 02:37:23 +00:00
|
|
|
else if (aSingleBRDoesntCount && !*aSeenBR && nsTextEditUtils::IsBreak(node))
|
2001-04-17 10:15:05 +00:00
|
|
|
{
|
2001-05-04 21:49:48 +00:00
|
|
|
// the first br in a block doesn't count if the caller so indicated
|
2002-04-14 02:37:23 +00:00
|
|
|
*aSeenBR = PR_TRUE;
|
2001-04-17 10:15:05 +00:00
|
|
|
}
|
|
|
|
else
|
2000-06-22 05:39:54 +00:00
|
|
|
{
|
|
|
|
// is it an empty node of some sort?
|
2001-04-17 10:15:05 +00:00
|
|
|
// note: list items or table cells are not considered empty
|
|
|
|
// if they contain other lists or tables
|
|
|
|
if (isListItemOrCell)
|
|
|
|
{
|
|
|
|
if (nsHTMLEditUtils::IsList(node) || nsHTMLEditUtils::IsTable(node))
|
2003-07-28 20:31:08 +00:00
|
|
|
{ // break out if we find we aren't empty
|
2001-04-17 10:15:05 +00:00
|
|
|
*outIsEmptyNode = PR_FALSE;
|
2002-02-07 03:56:20 +00:00
|
|
|
return NS_OK;
|
2001-04-17 10:15:05 +00:00
|
|
|
}
|
|
|
|
}
|
2001-12-12 06:02:15 +00:00
|
|
|
// is it a form widget?
|
|
|
|
else if (nsHTMLEditUtils::IsFormWidget(aNode))
|
2003-07-28 20:31:08 +00:00
|
|
|
{ // break out if we find we aren't empty
|
2001-12-12 06:02:15 +00:00
|
|
|
*outIsEmptyNode = PR_FALSE;
|
2002-02-07 03:56:20 +00:00
|
|
|
return NS_OK;
|
2001-12-12 06:02:15 +00:00
|
|
|
}
|
|
|
|
|
2002-12-22 01:51:14 +00:00
|
|
|
PRBool isEmptyNode = PR_TRUE;
|
2002-04-14 02:37:23 +00:00
|
|
|
res = IsEmptyNodeImpl(node, &isEmptyNode, aSingleBRDoesntCount,
|
|
|
|
aListOrCellNotEmpty, aSafeToAskFrames, aSeenBR);
|
2000-06-22 05:39:54 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!isEmptyNode)
|
2002-04-14 02:37:23 +00:00
|
|
|
{
|
2000-06-22 05:39:54 +00:00
|
|
|
// otherwise it ain't empty
|
|
|
|
*outIsEmptyNode = PR_FALSE;
|
2002-02-07 03:56:20 +00:00
|
|
|
return NS_OK;
|
2000-06-22 05:39:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2002-02-07 03:56:20 +00:00
|
|
|
node->GetNextSibling(getter_AddRefs(child));
|
2000-06-22 05:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
// add to aElement the CSS inline styles corresponding to the HTML attribute
|
|
|
|
// aAttribute with its value aValue
|
|
|
|
nsresult
|
2002-01-25 10:16:52 +00:00
|
|
|
nsHTMLEditor::SetAttributeOrEquivalent(nsIDOMElement * aElement,
|
2002-03-23 22:08:20 +00:00
|
|
|
const nsAString & aAttribute,
|
2002-09-17 12:04:59 +00:00
|
|
|
const nsAString & aValue,
|
|
|
|
PRBool aSuppressTransaction)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
|
|
|
PRBool useCSS;
|
|
|
|
nsresult res = NS_OK;
|
2002-03-14 22:11:14 +00:00
|
|
|
GetIsCSSEnabled(&useCSS);
|
2002-01-09 13:51:37 +00:00
|
|
|
if (useCSS && mHTMLCSSUtils) {
|
|
|
|
PRInt32 count;
|
2002-09-17 12:04:59 +00:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(aElement, nsnull, &aAttribute, &aValue, &count,
|
|
|
|
aSuppressTransaction);
|
2002-01-09 13:51:37 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2002-03-15 15:33:29 +00:00
|
|
|
if (count) {
|
|
|
|
// we found an equivalence ; let's remove the HTML attribute itself if it is set
|
|
|
|
nsAutoString existingValue;
|
|
|
|
PRBool wasSet = PR_FALSE;
|
|
|
|
res = GetAttributeValue(aElement, aAttribute, existingValue, &wasSet);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (wasSet) {
|
2002-09-20 02:09:04 +00:00
|
|
|
if (aSuppressTransaction)
|
|
|
|
res = aElement->RemoveAttribute(aAttribute);
|
|
|
|
else
|
|
|
|
res = RemoveAttribute(aElement, aAttribute);
|
2002-03-15 15:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2002-01-09 13:51:37 +00:00
|
|
|
// count is an integer that represents the number of CSS declarations applied to the
|
|
|
|
// element. If it is zero, we found no equivalence in this implementation for the
|
|
|
|
// attribute
|
|
|
|
if (aAttribute.Equals(NS_LITERAL_STRING("style"))) {
|
|
|
|
// if it is the style attribute, just add the new value to the existing style
|
|
|
|
// attribute's value
|
|
|
|
nsAutoString existingValue;
|
|
|
|
PRBool wasSet = PR_FALSE;
|
|
|
|
res = GetAttributeValue(aElement, NS_LITERAL_STRING("style"), existingValue, &wasSet);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
existingValue.Append(NS_LITERAL_STRING(" "));
|
|
|
|
existingValue.Append(aValue);
|
2002-09-20 02:09:04 +00:00
|
|
|
if (aSuppressTransaction)
|
|
|
|
res = aElement->SetAttribute(aAttribute, existingValue);
|
|
|
|
else
|
|
|
|
res = SetAttribute(aElement, aAttribute, existingValue);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// we have no CSS equivalence for this attribute and it is not the style
|
|
|
|
// attribute; let's set it the good'n'old HTML way
|
2002-09-20 02:09:04 +00:00
|
|
|
if (aSuppressTransaction)
|
|
|
|
res = aElement->SetAttribute(aAttribute, aValue);
|
|
|
|
else
|
|
|
|
res = SetAttribute(aElement, aAttribute, aValue);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// we are not in an HTML+CSS editor; let's set the attribute the HTML way
|
2002-09-20 02:09:04 +00:00
|
|
|
if (aSuppressTransaction)
|
|
|
|
res = aElement->SetAttribute(aAttribute, aValue);
|
|
|
|
else
|
|
|
|
res = SetAttribute(aElement, aAttribute, aValue);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-03-15 15:33:29 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::RemoveAttributeOrEquivalent(nsIDOMElement * aElement,
|
2002-09-17 12:04:59 +00:00
|
|
|
const nsAString & aAttribute,
|
|
|
|
PRBool aSuppressTransaction)
|
2002-03-15 15:33:29 +00:00
|
|
|
{
|
|
|
|
PRBool useCSS;
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
GetIsCSSEnabled(&useCSS);
|
|
|
|
if (useCSS && mHTMLCSSUtils) {
|
2002-09-17 12:04:59 +00:00
|
|
|
res = mHTMLCSSUtils->RemoveCSSEquivalentToHTMLStyle(aElement, nsnull, &aAttribute, nsnull,
|
|
|
|
aSuppressTransaction);
|
2002-03-15 15:33:29 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString existingValue;
|
|
|
|
PRBool wasSet = PR_FALSE;
|
|
|
|
res = GetAttributeValue(aElement, aAttribute, existingValue, &wasSet);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (wasSet) {
|
2002-09-20 02:09:04 +00:00
|
|
|
if (aSuppressTransaction)
|
|
|
|
res = aElement->RemoveAttribute(aAttribute);
|
|
|
|
else
|
|
|
|
res = RemoveAttribute(aElement, aAttribute);
|
2002-03-15 15:33:29 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
nsresult
|
2002-03-14 22:11:14 +00:00
|
|
|
nsHTMLEditor::SetIsCSSEnabled(PRBool aIsCSSPrefChecked)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
|
|
|
nsresult err = NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (mHTMLCSSUtils)
|
|
|
|
{
|
|
|
|
err = mHTMLCSSUtils->SetCSSEnabled(aIsCSSPrefChecked);
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set the block background color
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::SetCSSBackgroundColor(const nsAString& aColor)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
ForceCompositionEnd();
|
|
|
|
|
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
|
|
|
|
|
|
|
|
PRBool isCollapsed;
|
|
|
|
selection->GetIsCollapsed(&isCollapsed);
|
|
|
|
|
|
|
|
nsAutoEditBatch batchIt(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpInsertElement, nsIEditor::eNext);
|
|
|
|
nsAutoSelectionReset selectionResetter(selection, this);
|
|
|
|
nsAutoTxnsConserveSelection dontSpazMySelection(this);
|
|
|
|
|
|
|
|
PRBool cancel, handled;
|
|
|
|
nsTextRulesInfo ruleInfo(nsTextEditRules::kSetTextProperty);
|
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!cancel && !handled)
|
|
|
|
{
|
|
|
|
// get selection range enumerator
|
|
|
|
nsCOMPtr<nsIEnumerator> enumerator;
|
|
|
|
res = selPriv->GetEnumerator(getter_AddRefs(enumerator));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!enumerator) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// loop thru the ranges in the selection
|
|
|
|
enumerator->First();
|
|
|
|
nsCOMPtr<nsISupports> currentItem;
|
|
|
|
nsAutoString bgcolor; bgcolor.AssignWithConversion("bgcolor");
|
|
|
|
nsCOMPtr<nsIDOMNode> cachedBlockParent = nsnull;
|
|
|
|
while ((NS_ENUMERATOR_FALSE == enumerator->IsDone()))
|
|
|
|
{
|
|
|
|
res = enumerator->CurrentItem(getter_AddRefs(currentItem));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!currentItem) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRange> range( do_QueryInterface(currentItem) );
|
|
|
|
|
|
|
|
// check for easy case: both range endpoints in same text node
|
|
|
|
nsCOMPtr<nsIDOMNode> startNode, endNode;
|
|
|
|
PRInt32 startOffset, endOffset;
|
|
|
|
res = range->GetStartContainer(getter_AddRefs(startNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = range->GetEndContainer(getter_AddRefs(endNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = range->GetStartOffset(&startOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = range->GetEndOffset(&endOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if ((startNode == endNode) && IsTextNode(startNode))
|
|
|
|
{
|
|
|
|
// let's find the block container of the text node
|
|
|
|
nsCOMPtr<nsIDOMNode> blockParent;
|
|
|
|
blockParent = GetBlockNodeParent(startNode);
|
|
|
|
// and apply the background color to that block container
|
|
|
|
if (cachedBlockParent != blockParent)
|
|
|
|
{
|
|
|
|
cachedBlockParent = blockParent;
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(blockParent);
|
|
|
|
PRInt32 count;
|
2002-09-17 12:04:59 +00:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nsnull, &bgcolor, &aColor, &count, PR_FALSE);
|
2002-01-09 13:51:37 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
2002-02-18 10:06:07 +00:00
|
|
|
else if ((startNode == endNode) && nsTextEditUtils::IsBody(startNode) && isCollapsed)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
|
|
|
// we have no block in the document, let's apply the background to the body
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(startNode);
|
|
|
|
PRInt32 count;
|
2002-09-17 12:04:59 +00:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nsnull, &bgcolor, &aColor, &count, PR_FALSE);
|
2002-01-09 13:51:37 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
else if ((startNode == endNode) && (((endOffset-startOffset) == 1) || (!startOffset && !endOffset)))
|
|
|
|
{
|
|
|
|
// a unique node is selected, let's also apply the background color
|
|
|
|
// to the containing block, possibly the node itself
|
|
|
|
nsCOMPtr<nsIDOMNode> selectedNode = GetChildAt(startNode, startOffset);
|
|
|
|
PRBool isBlock =PR_FALSE;
|
|
|
|
res = NodeIsBlockStatic(selectedNode, &isBlock);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
nsCOMPtr<nsIDOMNode> blockParent = selectedNode;
|
|
|
|
if (!isBlock) {
|
|
|
|
blockParent = GetBlockNodeParent(selectedNode);
|
|
|
|
}
|
|
|
|
if (cachedBlockParent != blockParent)
|
|
|
|
{
|
|
|
|
cachedBlockParent = blockParent;
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(blockParent);
|
|
|
|
PRInt32 count;
|
2002-09-17 12:04:59 +00:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nsnull, &bgcolor, &aColor, &count, PR_FALSE);
|
2002-01-09 13:51:37 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// not the easy case. range not contained in single text node.
|
|
|
|
// there are up to three phases here. There are all the nodes
|
|
|
|
// reported by the subtree iterator to be processed. And there
|
|
|
|
// are potentially a starting textnode and an ending textnode
|
|
|
|
// which are only partially contained by the range.
|
|
|
|
|
|
|
|
// lets handle the nodes reported by the iterator. These nodes
|
|
|
|
// are entirely contained in the selection range. We build up
|
|
|
|
// a list of them (since doing operations on the document during
|
|
|
|
// iteration would perturb the iterator).
|
|
|
|
|
2003-07-08 20:22:56 +00:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1", &res);
|
2002-01-09 13:51:37 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!iter) return NS_ERROR_FAILURE;
|
|
|
|
|
2002-11-12 19:40:11 +00:00
|
|
|
nsCOMArray<nsIDOMNode> arrayOfNodes;
|
2002-01-09 13:51:37 +00:00
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
2002-11-12 19:40:11 +00:00
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
// iterate range and build up array
|
|
|
|
res = iter->Init(range);
|
|
|
|
// init returns an error if no nodes in range.
|
|
|
|
// this can easily happen with the subtree
|
|
|
|
// iterator if the selection doesn't contain
|
|
|
|
// any *whole* nodes.
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
while (NS_ENUMERATOR_FALSE == iter->IsDone())
|
|
|
|
{
|
|
|
|
res = iter->CurrentNode(getter_AddRefs(content));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
node = do_QueryInterface(content);
|
|
|
|
if (!node) return NS_ERROR_FAILURE;
|
|
|
|
if (IsEditable(node))
|
2002-11-12 19:40:11 +00:00
|
|
|
{
|
|
|
|
arrayOfNodes.AppendObject(node);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
res = iter->Next();
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// first check the start parent of the range to see if it needs to
|
2003-03-24 04:15:34 +00:00
|
|
|
// be separately handled (it does if it's a text node, due to how the
|
2002-01-09 13:51:37 +00:00
|
|
|
// subtree iterator works - it will not have reported it).
|
|
|
|
if (IsTextNode(startNode) && IsEditable(startNode))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> blockParent;
|
|
|
|
blockParent = GetBlockNodeParent(startNode);
|
|
|
|
if (cachedBlockParent != blockParent)
|
|
|
|
{
|
|
|
|
cachedBlockParent = blockParent;
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(blockParent);
|
|
|
|
PRInt32 count;
|
2002-09-17 12:04:59 +00:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nsnull, &bgcolor, &aColor, &count, PR_FALSE);
|
2002-01-09 13:51:37 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then loop through the list, set the property on each node
|
2002-11-12 19:40:11 +00:00
|
|
|
PRInt32 listCount = arrayOfNodes.Count();
|
|
|
|
PRInt32 j;
|
2002-01-09 13:51:37 +00:00
|
|
|
for (j = 0; j < listCount; j++)
|
|
|
|
{
|
2002-11-12 19:40:11 +00:00
|
|
|
node = arrayOfNodes[j];
|
2002-01-09 13:51:37 +00:00
|
|
|
// do we have a block here ?
|
|
|
|
PRBool isBlock =PR_FALSE;
|
|
|
|
res = NodeIsBlockStatic(node, &isBlock);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
nsCOMPtr<nsIDOMNode> blockParent = node;
|
|
|
|
if (!isBlock) {
|
|
|
|
// no we don't, let's find the block ancestor
|
|
|
|
blockParent = GetBlockNodeParent(node);
|
|
|
|
}
|
|
|
|
if (cachedBlockParent != blockParent)
|
|
|
|
{
|
|
|
|
cachedBlockParent = blockParent;
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(blockParent);
|
|
|
|
PRInt32 count;
|
|
|
|
// and set the property on it
|
2002-09-17 12:04:59 +00:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nsnull, &bgcolor, &aColor, &count, PR_FALSE);
|
2002-01-09 13:51:37 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
2002-11-12 19:40:11 +00:00
|
|
|
arrayOfNodes.Clear();
|
2002-01-09 13:51:37 +00:00
|
|
|
|
|
|
|
// last check the end parent of the range to see if it needs to
|
2003-03-24 04:15:34 +00:00
|
|
|
// be separately handled (it does if it's a text node, due to how the
|
2002-01-09 13:51:37 +00:00
|
|
|
// subtree iterator works - it will not have reported it).
|
|
|
|
if (IsTextNode(endNode) && IsEditable(endNode))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> blockParent;
|
|
|
|
blockParent = GetBlockNodeParent(endNode);
|
|
|
|
if (cachedBlockParent != blockParent)
|
|
|
|
{
|
|
|
|
cachedBlockParent = blockParent;
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(blockParent);
|
|
|
|
PRInt32 count;
|
2002-09-17 12:04:59 +00:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nsnull, &bgcolor, &aColor, &count, PR_FALSE);
|
2002-01-09 13:51:37 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->Next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!cancel)
|
|
|
|
{
|
|
|
|
// post-process
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::SetBackgroundColor(const nsAString& aColor)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
|
|
|
nsresult res;
|
|
|
|
PRBool useCSS;
|
2002-03-14 22:11:14 +00:00
|
|
|
GetIsCSSEnabled(&useCSS);
|
2002-01-09 13:51:37 +00:00
|
|
|
if (useCSS) {
|
|
|
|
// if we are in CSS mode, we have to apply the background color to the
|
|
|
|
// containing block (or the body if we have no block-level element in
|
|
|
|
// the document)
|
|
|
|
res = SetCSSBackgroundColor(aColor);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// but in HTML mode, we can only set the document's background color
|
|
|
|
res = SetHTMLBackgroundColor(aColor);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// NodesSameType: do these nodes have the same tag?
|
|
|
|
//
|
|
|
|
PRBool
|
|
|
|
nsHTMLEditor::NodesSameType(nsIDOMNode *aNode1, nsIDOMNode *aNode2)
|
|
|
|
{
|
|
|
|
if (!aNode1 || !aNode2)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("null node passed to nsEditor::NodesSameType()");
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
PRBool useCSS;
|
2002-03-14 22:11:14 +00:00
|
|
|
GetIsCSSEnabled(&useCSS);
|
2002-01-09 13:51:37 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIAtom> atom1 = GetTag(aNode1);
|
|
|
|
nsCOMPtr<nsIAtom> atom2 = GetTag(aNode2);
|
|
|
|
|
|
|
|
if (atom1.get() == atom2.get()) {
|
2003-07-15 13:47:15 +00:00
|
|
|
if (useCSS && atom1 == nsEditProperty::span) {
|
2002-01-09 13:51:37 +00:00
|
|
|
if (mHTMLCSSUtils->ElementsSameStyle(aNode1, aNode2)) {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return PR_TRUE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return PR_FALSE;
|
|
|
|
}
|
|
|
|
|
2002-03-11 13:11:48 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::CopyLastEditableChildStyles(nsIDOMNode * aPreviousBlock, nsIDOMNode * aNewBlock,
|
|
|
|
nsIDOMNode **aOutBrNode)
|
|
|
|
{
|
|
|
|
*aOutBrNode = nsnull;
|
2002-04-06 19:07:47 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> child, tmp;
|
2002-03-11 13:11:48 +00:00
|
|
|
nsresult res;
|
2002-04-06 19:07:47 +00:00
|
|
|
// first, clear out aNewBlock. Contract is that we want only the styles from previousBlock.
|
|
|
|
res = aNewBlock->GetFirstChild(getter_AddRefs(child));
|
|
|
|
while (NS_SUCCEEDED(res) && child)
|
|
|
|
{
|
|
|
|
res = DeleteNode(child);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = aNewBlock->GetFirstChild(getter_AddRefs(child));
|
|
|
|
}
|
|
|
|
// now find and clone the styles
|
|
|
|
child = aPreviousBlock;
|
|
|
|
tmp = aPreviousBlock;
|
2002-03-11 13:11:48 +00:00
|
|
|
while (tmp) {
|
|
|
|
child = tmp;
|
|
|
|
res = GetLastEditableChild(child, address_of(tmp));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
while (child && nsTextEditUtils::IsBreak(child)) {
|
|
|
|
nsCOMPtr<nsIDOMNode> priorNode;
|
|
|
|
res = GetPriorHTMLNode(child, address_of(priorNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
child = priorNode;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDOMNode> newStyles = nsnull, deepestStyle = nsnull;
|
|
|
|
while (child && (child != aPreviousBlock)) {
|
2003-07-28 20:31:08 +00:00
|
|
|
if (nsHTMLEditUtils::IsInlineStyle(child) ||
|
2003-07-15 13:47:15 +00:00
|
|
|
nsEditor::NodeIsType(child, nsEditProperty::span)) {
|
2002-03-11 13:11:48 +00:00
|
|
|
nsAutoString domTagName;
|
|
|
|
child->GetNodeName(domTagName);
|
|
|
|
ToLowerCase(domTagName);
|
|
|
|
if (newStyles) {
|
|
|
|
nsCOMPtr<nsIDOMNode> newContainer;
|
|
|
|
res = InsertContainerAbove(newStyles, address_of(newContainer), domTagName);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
newStyles = newContainer;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res = CreateNode(domTagName, aNewBlock, 0, getter_AddRefs(newStyles));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
deepestStyle = newStyles;
|
|
|
|
}
|
|
|
|
res = CloneAttributes(newStyles, child);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
res = child->GetParentNode(getter_AddRefs(tmp));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
child = tmp;
|
|
|
|
}
|
|
|
|
if (deepestStyle) {
|
|
|
|
nsCOMPtr<nsIDOMNode> outBRNode;
|
|
|
|
res = CreateBR(deepestStyle, 0, address_of(outBRNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
// Getters must addref
|
|
|
|
*aOutBrNode = outBRNode;
|
|
|
|
NS_ADDREF(*aOutBrNode);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-02-24 09:37:18 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetElementOrigin(nsIDOMElement * aElement, PRInt32 & aX, PRInt32 & aY)
|
|
|
|
{
|
|
|
|
// we are going to need the PresShell
|
|
|
|
if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!ps) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
|
|
|
|
nsIFrame *frame = 0; // not ref-counted
|
|
|
|
ps->GetPrimaryFrameFor(content, &frame);
|
|
|
|
|
|
|
|
float t2p;
|
|
|
|
nsCOMPtr<nsIPresContext> pcontext;
|
|
|
|
ps->GetPresContext(getter_AddRefs(pcontext));
|
|
|
|
pcontext->GetTwipsToPixels(&t2p);
|
|
|
|
|
|
|
|
|
2003-07-15 13:47:15 +00:00
|
|
|
if (nsHTMLEditUtils::IsHR(aElement)) {
|
2003-09-23 17:05:29 +00:00
|
|
|
frame = frame->GetNextSibling();
|
2003-02-24 09:37:18 +00:00
|
|
|
}
|
|
|
|
PRInt32 offsetX = 0, offsetY = 0;
|
|
|
|
while (frame) {
|
|
|
|
// Look for a widget so we can get screen coordinates
|
2003-09-23 17:05:29 +00:00
|
|
|
nsIView* view = frame->GetViewExternal();
|
|
|
|
if (view && view->HasWidget())
|
|
|
|
break;
|
2003-02-24 09:37:18 +00:00
|
|
|
|
|
|
|
// No widget yet, so count up the coordinates of the frame
|
2003-09-23 17:05:29 +00:00
|
|
|
nsPoint origin = frame->GetPosition();
|
2003-02-24 09:37:18 +00:00
|
|
|
offsetX += origin.x;
|
|
|
|
offsetY += origin.y;
|
|
|
|
|
2003-09-23 17:05:29 +00:00
|
|
|
frame = frame->GetParent();
|
2003-02-24 09:37:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
aX = NSTwipsToIntPixels(offsetX , t2p);
|
|
|
|
aY = NSTwipsToIntPixels(offsetY , t2p);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-25 15:24:08 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::EndUpdateViewBatch()
|
|
|
|
{
|
|
|
|
nsresult res = nsEditor::EndUpdateViewBatch();
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// We may need to show resizing handles or update existing ones after
|
|
|
|
// all transactions are done. This way of doing is preferred to DOM
|
|
|
|
// mutation events listeners because all the changes the user can apply
|
|
|
|
// to a document may result in multiple events, some of them quite hard
|
|
|
|
// to listen too (in particular when an ancestor of the selection is
|
|
|
|
// changed but the selection itself is not changed).
|
|
|
|
if (mUpdateCount == 0) {
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selection) return NS_ERROR_NOT_INITIALIZED;
|
2003-06-25 08:50:48 +00:00
|
|
|
res = CheckSelectionStateForAnonymousButtons(selection);
|
2003-03-25 15:24:08 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-06-25 08:50:48 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::IgnoreSpuriousDragEvent(PRBool aIgnoreSpuriousDragEvent)
|
|
|
|
{
|
|
|
|
mIgnoreSpuriousDragEvent = aIgnoreSpuriousDragEvent;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetSelectionContainer(nsIDOMElement ** aReturn)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
|
|
|
// if we don't get the selection, just skip this
|
|
|
|
if (NS_FAILED(res) || !selection) return res;
|
|
|
|
|
|
|
|
PRBool bCollapsed;
|
|
|
|
res = selection->GetIsCollapsed(&bCollapsed);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> focusNode;
|
|
|
|
|
|
|
|
if (bCollapsed) {
|
|
|
|
res = selection->GetFocusNode(getter_AddRefs(focusNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
PRInt32 rangeCount;
|
|
|
|
res = selection->GetRangeCount(&rangeCount);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
if (rangeCount == 1) {
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
res = selection->GetRangeAt(0, getter_AddRefs(range));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!range) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> startContainer, endContainer;
|
|
|
|
res = range->GetStartContainer(getter_AddRefs(startContainer));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = range->GetEndContainer(getter_AddRefs(endContainer));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
PRInt32 startOffset, endOffset;
|
|
|
|
res = range->GetStartOffset(&startOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
res = range->GetEndOffset(&endOffset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> focusElement;
|
|
|
|
if (startContainer == endContainer && startOffset + 1 == endOffset) {
|
|
|
|
res = GetSelectedElement(NS_LITERAL_STRING(""), getter_AddRefs(focusElement));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (focusElement)
|
|
|
|
focusNode = do_QueryInterface(focusElement);
|
|
|
|
}
|
|
|
|
if (!focusNode) {
|
|
|
|
res = range->GetCommonAncestorContainer(getter_AddRefs(focusNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PRInt32 i;
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
for (i = 0; i < rangeCount; i++)
|
|
|
|
{
|
|
|
|
res = selection->GetRangeAt(i, getter_AddRefs(range));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
nsCOMPtr<nsIDOMNode> startContainer;
|
|
|
|
range->GetStartContainer(getter_AddRefs(startContainer));
|
|
|
|
if (!focusNode)
|
|
|
|
focusNode = startContainer;
|
|
|
|
else if (focusNode != startContainer) {
|
|
|
|
res = startContainer->GetParentNode(getter_AddRefs(focusNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (focusNode) {
|
|
|
|
PRUint16 nodeType;
|
|
|
|
focusNode->GetNodeType(&nodeType);
|
|
|
|
if (nsIDOMNode::TEXT_NODE == nodeType) {
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
res = focusNode->GetParentNode(getter_AddRefs(parent));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
focusNode = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> focusElement = do_QueryInterface(focusNode);
|
|
|
|
*aReturn = focusElement;
|
|
|
|
NS_IF_ADDREF(*aReturn);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-06-19 14:06:05 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::IsAnonymousElement(nsIDOMElement * aElement, PRBool * aReturn)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aElement, NS_ERROR_NULL_POINTER);
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
|
|
|
|
*aReturn = content->IsNativeAnonymous();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-06-25 08:50:48 +00:00
|
|
|
|