2001-09-26 02:53:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 18:21:17 +04:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
1999-03-01 22:54:47 +03:00
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The contents of this file are subject to the Mozilla 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/MPL/
|
1999-03-01 22:54:47 +03:00
|
|
|
*
|
2001-09-26 02:53:13 +04: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 22:54:47 +03:00
|
|
|
*
|
1999-11-06 06:43:54 +03:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 18:21:17 +04:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-26 02:53:13 +04:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 06:43:54 +03:00
|
|
|
*
|
2001-09-26 02:53:13 +04:00
|
|
|
* Contributor(s):
|
2000-02-01 17:26:27 +03:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2002-01-09 16:51:37 +03:00
|
|
|
* Daniel Glazman <glazman@netscape.com>
|
2011-05-15 14:07:30 +04:00
|
|
|
* Ms2ger <ms2ger@gmail.com>
|
2001-09-26 02:53:13 +04:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 18:21:17 +04:00
|
|
|
* either of 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"),
|
2001-09-26 02:53:13 +04:00
|
|
|
* 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
|
2004-04-18 18:21:17 +04:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-26 02:53:13 +04:00
|
|
|
* 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
|
2004-04-18 18:21:17 +04:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-26 02:53:13 +04:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2003-03-15 04:04:32 +03:00
|
|
|
#include "nsCRT.h"
|
1999-03-01 22:54:47 +03:00
|
|
|
|
2001-10-13 04:16:32 +04:00
|
|
|
#include "nsUnicharUtils.h"
|
1999-08-09 05:37:50 +04:00
|
|
|
|
1999-03-01 22:54:47 +03:00
|
|
|
#include "nsHTMLEditor.h"
|
1999-03-29 12:02:05 +04:00
|
|
|
#include "nsHTMLEditRules.h"
|
2001-04-06 03:48:01 +04:00
|
|
|
#include "nsTextEditUtils.h"
|
2000-01-26 03:57:37 +03:00
|
|
|
#include "nsHTMLEditUtils.h"
|
1999-08-09 05:37:50 +04:00
|
|
|
|
2010-03-06 07:01:28 +03:00
|
|
|
#include "nsHTMLEditorEventListener.h"
|
2001-04-06 03:48:01 +04:00
|
|
|
#include "TypeInState.h"
|
1999-08-09 05:37:50 +04:00
|
|
|
|
2001-08-08 01:39:07 +04:00
|
|
|
#include "nsHTMLURIRefObject.h"
|
|
|
|
|
1999-07-05 03:01:10 +04:00
|
|
|
#include "nsIDOMText.h"
|
1999-04-08 04:46:10 +04:00
|
|
|
#include "nsIDOMNodeList.h"
|
1999-03-01 22:54:47 +03:00
|
|
|
#include "nsIDOMDocument.h"
|
2000-03-24 03:26:47 +03:00
|
|
|
#include "nsIDOMAttr.h"
|
1999-06-08 04:02:25 +04:00
|
|
|
#include "nsIDocument.h"
|
2007-05-14 13:11:38 +04:00
|
|
|
#include "nsIDOMEventTarget.h"
|
1999-11-03 10:11:45 +03:00
|
|
|
#include "nsIDOMKeyEvent.h"
|
2000-09-14 15:45:01 +04:00
|
|
|
#include "nsISelectionPrivate.h"
|
1999-03-30 02:01:26 +04:00
|
|
|
#include "nsIDOMHTMLAnchorElement.h"
|
1999-12-15 02:07:12 +03:00
|
|
|
#include "nsISelectionController.h"
|
2010-06-17 09:29:40 +04:00
|
|
|
#include "nsIDOMHTMLDocument.h"
|
2005-06-10 02:50:41 +04:00
|
|
|
#include "nsILinkHandler.h"
|
2000-05-13 12:04:29 +04:00
|
|
|
|
2010-06-29 02:49:35 +04:00
|
|
|
#include "mozilla/css/Loader.h"
|
2010-05-12 00:41:47 +04:00
|
|
|
#include "nsCSSStyleSheet.h"
|
2007-01-11 22:32:31 +03:00
|
|
|
#include "nsIDOMStyleSheet.h"
|
1999-08-09 05:37:50 +04:00
|
|
|
|
1999-04-20 21:47:12 +04:00
|
|
|
#include "nsIEnumerator.h"
|
1999-04-21 22:53:55 +04:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIContentIterator.h"
|
|
|
|
#include "nsIDOMRange.h"
|
1999-05-01 02:40:18 +04:00
|
|
|
#include "nsISupportsArray.h"
|
2007-02-24 23:07:06 +03:00
|
|
|
#include "nsContentUtils.h"
|
1999-06-07 23:32:36 +04:00
|
|
|
#include "nsIDocumentEncoder.h"
|
1999-06-11 01:31:42 +04:00
|
|
|
#include "nsIDOMDocumentFragment.h"
|
1999-06-10 04:35:02 +04:00
|
|
|
#include "nsIPresShell.h"
|
2004-08-01 03:15:21 +04:00
|
|
|
#include "nsPresContext.h"
|
2001-02-27 01:02:58 +03:00
|
|
|
#include "SetDocTitleTxn.h"
|
2010-05-04 21:40:39 +04:00
|
|
|
#include "nsFocusManager.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2002-05-14 04:05:37 +04:00
|
|
|
|
1999-08-09 05:37:50 +04:00
|
|
|
// netwerk
|
|
|
|
#include "nsIURI.h"
|
1999-11-30 07:50:42 +03:00
|
|
|
#include "nsNetUtil.h"
|
1999-07-19 23:37:08 +04:00
|
|
|
|
1999-08-09 05:37:50 +04:00
|
|
|
// Transactionas
|
|
|
|
#include "nsStyleSheetTxns.h"
|
|
|
|
|
|
|
|
// Misc
|
|
|
|
#include "TextEditorTest.h"
|
|
|
|
#include "nsEditorUtils.h"
|
2002-04-14 06:37:23 +04:00
|
|
|
#include "nsWSRunObject.h"
|
2008-03-13 21:54:01 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2011-11-27 15:51:53 +04:00
|
|
|
#include "nsIWidget.h"
|
2003-02-24 12:37:18 +03:00
|
|
|
|
|
|
|
#include "nsIFrame.h"
|
2005-06-16 17:10:58 +04:00
|
|
|
#include "nsIParserService.h"
|
2011-07-20 23:18:54 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2000-05-13 12:04:29 +04:00
|
|
|
|
2011-11-16 11:50:20 +04:00
|
|
|
using namespace mozilla;
|
2011-11-27 15:51:53 +04:00
|
|
|
using namespace mozilla::widget;
|
2011-11-16 11:50:20 +04:00
|
|
|
|
1999-08-27 08:12:47 +04:00
|
|
|
// Some utilities to handle annoying overloading of "A" tag for link and named anchor
|
1999-07-28 03:59:22 +04:00
|
|
|
static char hrefText[] = "href";
|
1999-08-21 02:39:48 +04:00
|
|
|
static char anchorTxt[] = "anchor";
|
1999-07-28 03:59:22 +04:00
|
|
|
static char namedanchorText[] = "namedanchor";
|
1999-09-30 00:08:15 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
#define IsLinkTag(s) (s.EqualsIgnoreCase(hrefText))
|
|
|
|
#define IsNamedAnchorTag(s) (s.EqualsIgnoreCase(anchorTxt) || s.EqualsIgnoreCase(namedanchorText))
|
1999-08-09 05:37:50 +04:00
|
|
|
|
1999-03-01 22:54:47 +03:00
|
|
|
nsHTMLEditor::nsHTMLEditor()
|
2001-01-28 23:13:07 +03:00
|
|
|
: nsPlaintextEditor()
|
2011-10-17 18:59:28 +04:00
|
|
|
, mCRInParagraphCreatesParagraph(false)
|
2005-01-12 22:11:48 +03:00
|
|
|
, mSelectedCellIndex(0)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mIsObjectResizingEnabled(true)
|
|
|
|
, mIsResizing(false)
|
|
|
|
, mIsAbsolutelyPositioningEnabled(true)
|
|
|
|
, mResizedObjectIsAbsolutelyPositioned(false)
|
|
|
|
, mGrabberClicked(false)
|
|
|
|
, mIsMoving(false)
|
|
|
|
, mSnapToGridEnabled(false)
|
|
|
|
, mIsInlineTableEditingEnabled(true)
|
2007-08-26 09:53:38 +04:00
|
|
|
, mInfoXIncrement(20)
|
|
|
|
, mInfoYIncrement(20)
|
2008-02-19 10:36:50 +03:00
|
|
|
, mGridSize(0)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
1999-06-10 04:35:02 +04:00
|
|
|
}
|
1999-03-01 22:54:47 +03:00
|
|
|
|
|
|
|
nsHTMLEditor::~nsHTMLEditor()
|
|
|
|
{
|
2005-03-24 22:00:01 +03: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.
|
2000-03-24 03:26:47 +03:00
|
|
|
nsCOMPtr<nsIEditActionListener> mListener = do_QueryInterface(mRules);
|
|
|
|
RemoveEditActionListener(mListener);
|
2005-07-13 20:45:58 +04:00
|
|
|
|
1999-03-01 22:54:47 +03:00
|
|
|
//the autopointers will clear themselves up.
|
1999-08-09 05:37:50 +04:00
|
|
|
//but we need to also remove the listeners or we have a leak
|
2000-09-14 15:45:01 +04:00
|
|
|
nsCOMPtr<nsISelection>selection;
|
2006-09-01 23:51:05 +04:00
|
|
|
nsresult result = GetSelection(getter_AddRefs(selection));
|
1999-09-30 00:08:15 +04:00
|
|
|
// if we don't get the selection, just skip this
|
1999-08-09 05:37:50 +04:00
|
|
|
if (NS_SUCCEEDED(result) && selection)
|
|
|
|
{
|
2000-09-14 15:45:01 +04:00
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
|
|
|
|
nsCOMPtr<nsISelectionListener>listener;
|
1999-08-09 05:37:50 +04:00
|
|
|
listener = do_QueryInterface(mTypeInState);
|
2005-06-10 02:50:41 +04:00
|
|
|
if (listener)
|
|
|
|
{
|
2000-09-14 15:45:01 +04:00
|
|
|
selPriv->RemoveSelectionListener(listener);
|
1999-08-09 05:37:50 +04:00
|
|
|
}
|
2003-02-24 12:37:18 +03:00
|
|
|
listener = do_QueryInterface(mSelectionListenerP);
|
2005-06-10 02:50:41 +04:00
|
|
|
if (listener)
|
|
|
|
{
|
2003-02-24 12:37:18 +03:00
|
|
|
selPriv->RemoveSelectionListener(listener);
|
|
|
|
}
|
1999-08-09 05:37:50 +04:00
|
|
|
}
|
1999-03-06 00:05:35 +03:00
|
|
|
|
2010-11-19 00:01:12 +03:00
|
|
|
mTypeInState = nsnull;
|
2003-02-24 12:37:18 +03:00
|
|
|
mSelectionListenerP = nsnull;
|
2002-06-12 00:43:49 +04:00
|
|
|
|
2003-06-03 02:21:06 +04:00
|
|
|
// free any default style propItems
|
|
|
|
RemoveAllDefaultProperties();
|
2005-03-24 22:00:01 +03:00
|
|
|
|
2011-03-11 08:40:30 +03:00
|
|
|
if (mLinkHandler && mDocWeak)
|
2005-06-10 02:50:41 +04:00
|
|
|
{
|
2011-05-22 16:43:13 +04:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2005-06-10 02:50:41 +04:00
|
|
|
|
|
|
|
if (ps && ps->GetPresContext())
|
|
|
|
{
|
|
|
|
ps->GetPresContext()->SetLinkHandler(mLinkHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-24 22:00:01 +03:00
|
|
|
RemoveEventListeners();
|
1999-03-06 00:05:35 +03:00
|
|
|
}
|
|
|
|
|
2011-02-20 08:43:07 +03:00
|
|
|
void
|
|
|
|
nsHTMLEditor::HideAnonymousEditingUIs()
|
|
|
|
{
|
|
|
|
if (mAbsolutelyPositionedObject)
|
|
|
|
HideGrabber();
|
|
|
|
if (mInlineEditedCell)
|
|
|
|
HideInlineTableEditingUI();
|
|
|
|
if (mResizedObject)
|
|
|
|
HideResizers();
|
|
|
|
}
|
|
|
|
|
2011-02-16 15:07:47 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsHTMLEditor)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsHTMLEditor, nsPlaintextEditor)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mTypeInState)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_NSCOMPTR(mTextServices)
|
|
|
|
|
2011-02-20 08:43:07 +03:00
|
|
|
tmp->HideAnonymousEditingUIs();
|
2011-02-16 15:07:47 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsHTMLEditor, nsPlaintextEditor)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mTypeInState)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mTextServices)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mTopLeftHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mTopHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mTopRightHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mLeftHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mRightHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mBottomLeftHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mBottomHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mBottomRightHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mActivatedHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mResizingShadow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mResizingInfo)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mResizedObject)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mMouseMotionListenerP)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mSelectionListenerP)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mResizeEventListenerP)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMARRAY(objectResizeEventListeners)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mAbsolutelyPositionedObject)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mGrabber)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mPositioningShadow)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mInlineEditedCell)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mAddColumnBeforeButton)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mRemoveColumnButton)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mAddColumnAfterButton)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mAddRowBeforeButton)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mRemoveRowButton)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_NSCOMPTR(mAddRowAfterButton)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
1999-08-09 05:37:50 +04:00
|
|
|
NS_IMPL_ADDREF_INHERITED(nsHTMLEditor, nsEditor)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsHTMLEditor, nsEditor)
|
|
|
|
|
2011-02-16 15:07:47 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsHTMLEditor)
|
2003-06-25 12:50:48 +04:00
|
|
|
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)
|
2010-06-17 09:31:15 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
|
2003-06-25 23:21:59 +04:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsPlaintextEditor)
|
2003-06-25 12:50:48 +04:00
|
|
|
|
1999-03-06 00:05:35 +03:00
|
|
|
|
2005-06-10 02:50:41 +04:00
|
|
|
NS_IMETHODIMP
|
2011-03-11 08:40:31 +03:00
|
|
|
nsHTMLEditor::Init(nsIDOMDocument *aDoc,
|
|
|
|
nsIContent *aRoot,
|
|
|
|
nsISelectionController *aSelCon,
|
2005-06-10 02:50:41 +04:00
|
|
|
PRUint32 aFlags)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
2011-03-11 08:40:31 +03:00
|
|
|
NS_PRECONDITION(aDoc && !aSelCon, "bad arg");
|
|
|
|
NS_ENSURE_TRUE(aDoc, NS_ERROR_NULL_POINTER);
|
1999-08-09 05:37:50 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult result = NS_OK, rulesRes = NS_OK;
|
2001-05-11 16:43:22 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
if (1)
|
|
|
|
{
|
|
|
|
// block to scope nsAutoEditInitRulesTrigger
|
2007-07-08 11:08:04 +04:00
|
|
|
nsAutoEditInitRulesTrigger rulesTrigger(static_cast<nsPlaintextEditor*>(this), rulesRes);
|
1999-08-09 05:37:50 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Init the plaintext editor
|
2011-03-11 08:40:31 +03:00
|
|
|
result = nsPlaintextEditor::Init(aDoc, aRoot, nsnull, aFlags);
|
2001-01-28 23:13:07 +03:00
|
|
|
if (NS_FAILED(result)) { return result; }
|
2000-05-15 09:18:45 +04:00
|
|
|
|
2010-06-17 09:31:15 +04:00
|
|
|
// Init mutation observer
|
|
|
|
nsCOMPtr<nsINode> document = do_QueryInterface(aDoc);
|
2010-12-09 05:44:15 +03:00
|
|
|
document->AddMutationObserverUnlessExists(this);
|
2010-06-17 09:31:15 +04:00
|
|
|
|
2003-06-25 12:50:48 +04:00
|
|
|
// disable Composer-only features
|
2010-04-12 06:35:18 +04:00
|
|
|
if (IsMailEditor())
|
2003-06-25 12:50:48 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
SetAbsolutePositioningEnabled(false);
|
|
|
|
SetSnapToGridEnabled(false);
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
// Init the HTML-CSS utils
|
2011-05-15 14:07:30 +04:00
|
|
|
mHTMLCSSUtils = new nsHTMLCSSUtils(this);
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// disable links
|
2011-05-22 16:43:13 +04:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = GetPresShell();
|
2011-03-11 08:40:31 +03:00
|
|
|
NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE);
|
|
|
|
nsPresContext *context = presShell->GetPresContext();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(context, NS_ERROR_NULL_POINTER);
|
2010-04-12 06:35:18 +04:00
|
|
|
if (!IsPlaintextEditor() && !IsInteractionAllowed()) {
|
2005-06-10 02:50:41 +04:00
|
|
|
mLinkHandler = context->GetLinkHandler();
|
|
|
|
|
|
|
|
context->SetLinkHandler(nsnull);
|
|
|
|
}
|
1999-11-11 22:22:30 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// init the type-in state
|
|
|
|
mTypeInState = new TypeInState();
|
|
|
|
|
2003-02-24 12:37:18 +03:00
|
|
|
// init the selection listener for image resizing
|
|
|
|
mSelectionListenerP = new ResizerSelectionListener(this);
|
|
|
|
|
2010-04-12 06:35:18 +04:00
|
|
|
if (!IsInteractionAllowed()) {
|
2007-06-28 06:48:16 +04:00
|
|
|
// ignore any errors from this in case the file is missing
|
2007-10-31 21:51:49 +03:00
|
|
|
AddOverrideStyleSheet(NS_LITERAL_STRING("resource://gre/res/EditorOverride.css"));
|
2007-06-28 06:48:16 +04:00
|
|
|
}
|
2003-04-03 12:05:23 +04:00
|
|
|
|
2001-01-28 23:13:07 +03: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 12:37:18 +03:00
|
|
|
listener = do_QueryInterface(mSelectionListenerP);
|
|
|
|
if (listener) {
|
|
|
|
selPriv->AddSelectionListener(listener);
|
|
|
|
}
|
1999-07-20 02:49:21 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
1999-11-29 11:28:46 +03:00
|
|
|
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(rulesRes, rulesRes);
|
1999-08-23 10:42:04 +04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-06-17 09:31:15 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::PreDestroy(bool aDestroyingFrames)
|
2010-06-17 09:31:15 +04:00
|
|
|
{
|
|
|
|
if (mDidPreDestroy) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> document = do_QueryReferent(mDocWeak);
|
|
|
|
if (document) {
|
|
|
|
document->RemoveMutationObserver(this);
|
|
|
|
}
|
|
|
|
|
2011-06-24 21:55:43 +04:00
|
|
|
while (mStyleSheetURLs.Length())
|
|
|
|
{
|
|
|
|
RemoveOverrideStyleSheet(mStyleSheetURLs[0]);
|
|
|
|
}
|
|
|
|
|
2012-04-19 04:10:49 +04:00
|
|
|
// Clean up after our anonymous content -- we don't want these nodes to
|
|
|
|
// stay around (which they would, since the frames have an owning reference).
|
|
|
|
HideAnonymousEditingUIs();
|
|
|
|
|
2010-06-17 09:31:15 +04:00
|
|
|
return nsPlaintextEditor::PreDestroy(aDestroyingFrames);
|
|
|
|
}
|
|
|
|
|
2010-06-17 09:29:40 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetRootElement(nsIDOMElement **aRootElement)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRootElement);
|
|
|
|
|
|
|
|
if (mRootElement) {
|
|
|
|
return nsEditor::GetRootElement(aRootElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
*aRootElement = nsnull;
|
|
|
|
|
|
|
|
// Use the HTML documents body element as the editor root if we didn't
|
|
|
|
// get a root element during initialization.
|
|
|
|
|
2011-12-04 01:50:15 +04:00
|
|
|
nsCOMPtr<nsIDOMElement> rootElement;
|
2010-06-17 09:29:40 +04:00
|
|
|
nsCOMPtr<nsIDOMHTMLElement> bodyElement;
|
|
|
|
nsresult rv = GetBodyElement(getter_AddRefs(bodyElement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (bodyElement) {
|
2011-12-04 01:50:15 +04:00
|
|
|
rootElement = bodyElement;
|
2010-06-17 09:29:40 +04:00
|
|
|
} else {
|
|
|
|
// If there is no HTML body element,
|
|
|
|
// we should use the document root element instead.
|
|
|
|
nsCOMPtr<nsIDOMDocument> doc = do_QueryReferent(mDocWeak);
|
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED);
|
|
|
|
|
2011-12-04 01:50:15 +04:00
|
|
|
rv = doc->GetDocumentElement(getter_AddRefs(rootElement));
|
2010-06-17 09:29:40 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// Document can have no elements
|
2011-12-04 01:50:15 +04:00
|
|
|
if (!rootElement) {
|
2010-06-17 09:29:40 +04:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-04 01:50:15 +04:00
|
|
|
mRootElement = do_QueryInterface(rootElement);
|
|
|
|
rootElement.forget(aRootElement);
|
2010-06-17 09:29:40 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-17 09:30:42 +04:00
|
|
|
already_AddRefed<nsIContent>
|
|
|
|
nsHTMLEditor::FindSelectionRoot(nsINode *aNode)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aNode->IsNodeOfType(nsINode::eDOCUMENT) ||
|
|
|
|
aNode->IsNodeOfType(nsINode::eCONTENT),
|
|
|
|
"aNode must be content or document node");
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
|
|
nsCOMPtr<nsIDocument> doc = aNode->GetCurrentDoc();
|
|
|
|
if (!doc) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (doc->HasFlag(NODE_IS_EDITABLE) || !content) {
|
|
|
|
content = doc->GetRootElement();
|
|
|
|
return content.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX If we have readonly flag, shouldn't return the element which has
|
|
|
|
// contenteditable="true"? However, such case isn't there without chrome
|
|
|
|
// permission script.
|
|
|
|
if (IsReadonly()) {
|
|
|
|
// We still want to allow selection in a readonly editor.
|
|
|
|
content = do_QueryInterface(GetRoot());
|
|
|
|
return content.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!content->HasFlag(NODE_IS_EDITABLE)) {
|
2010-11-16 23:45:49 +03:00
|
|
|
// If the content is in read-write state but is not editable itself,
|
|
|
|
// return it as the selection root.
|
|
|
|
if (content->IsElement() &&
|
|
|
|
content->AsElement()->State().HasState(NS_EVENT_STATE_MOZ_READWRITE)) {
|
|
|
|
return content.forget();
|
|
|
|
}
|
2010-06-17 09:30:42 +04:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
// For non-readonly editors we want to find the root of the editable subtree
|
|
|
|
// containing aContent.
|
2010-07-22 20:22:44 +04:00
|
|
|
content = content->GetEditingHost();
|
2010-06-17 09:30:42 +04:00
|
|
|
return content.forget();
|
|
|
|
}
|
|
|
|
|
2011-07-15 14:18:35 +04:00
|
|
|
/* virtual */
|
|
|
|
void
|
2005-03-24 22:00:01 +03:00
|
|
|
nsHTMLEditor::CreateEventListeners()
|
1999-08-23 10:42:04 +04:00
|
|
|
{
|
2010-11-19 00:01:12 +03:00
|
|
|
// Don't create the handler twice
|
2011-06-28 03:34:54 +04:00
|
|
|
if (!mEventListener) {
|
|
|
|
mEventListener = new nsHTMLEditorEventListener();
|
|
|
|
}
|
2005-03-24 22:00:01 +03:00
|
|
|
}
|
1999-08-09 05:37:50 +04:00
|
|
|
|
2010-04-19 16:20:42 +04:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::InstallEventListeners()
|
|
|
|
{
|
2011-03-11 08:40:30 +03:00
|
|
|
NS_ENSURE_TRUE(mDocWeak && mEventListener,
|
2010-04-19 16:20:42 +04:00
|
|
|
NS_ERROR_NOT_INITIALIZED);
|
2010-06-17 09:30:10 +04:00
|
|
|
|
|
|
|
// NOTE: nsHTMLEditor doesn't need to initialize mEventTarget here because
|
|
|
|
// the target must be document node and it must be referenced as weak pointer.
|
|
|
|
|
2010-04-19 16:20:42 +04:00
|
|
|
nsHTMLEditorEventListener* listener =
|
|
|
|
reinterpret_cast<nsHTMLEditorEventListener*>(mEventListener.get());
|
|
|
|
return listener->Connect(this);
|
|
|
|
}
|
|
|
|
|
2005-03-24 22:00:01 +03:00
|
|
|
void
|
|
|
|
nsHTMLEditor::RemoveEventListeners()
|
|
|
|
{
|
2006-06-17 04:17:26 +04:00
|
|
|
if (!mDocWeak)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-24 06:18:01 +04:00
|
|
|
nsCOMPtr<nsIDOMEventTarget> target = GetDOMEventTarget();
|
2000-05-04 12:33:48 +04:00
|
|
|
|
2011-06-24 06:18:01 +04:00
|
|
|
if (target)
|
2005-03-24 22:00:01 +03:00
|
|
|
{
|
|
|
|
// Both mMouseMotionListenerP and mResizeEventListenerP can be
|
|
|
|
// registerd with other targets than the DOM event receiver that
|
|
|
|
// we can reach from here. But nonetheless, unregister the event
|
|
|
|
// listeners with the DOM event reveiver (if it's registerd with
|
|
|
|
// other targets, it'll get unregisterd once the target goes
|
|
|
|
// away).
|
|
|
|
|
|
|
|
if (mMouseMotionListenerP)
|
|
|
|
{
|
2011-06-28 03:34:54 +04:00
|
|
|
// mMouseMotionListenerP might be registerd either as bubbling or
|
|
|
|
// capturing, unregister by both.
|
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("mousemove"),
|
2011-10-17 18:59:28 +04:00
|
|
|
mMouseMotionListenerP, false);
|
2007-05-14 13:11:38 +04:00
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("mousemove"),
|
2011-10-17 18:59:28 +04:00
|
|
|
mMouseMotionListenerP, true);
|
2005-03-24 22:00:01 +03:00
|
|
|
}
|
2002-11-09 03:25:25 +03:00
|
|
|
|
2005-03-24 22:00:01 +03:00
|
|
|
if (mResizeEventListenerP)
|
1999-09-30 00:08:15 +04:00
|
|
|
{
|
2007-05-14 13:11:38 +04:00
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("resize"),
|
2011-10-17 18:59:28 +04:00
|
|
|
mResizeEventListenerP, false);
|
1999-09-30 00:08:15 +04:00
|
|
|
}
|
|
|
|
}
|
2005-03-24 22:00:01 +03:00
|
|
|
|
|
|
|
mMouseMotionListenerP = nsnull;
|
|
|
|
mResizeEventListenerP = nsnull;
|
|
|
|
|
|
|
|
nsPlaintextEditor::RemoveEventListeners();
|
1999-08-09 05:37:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::SetFlags(PRUint32 aFlags)
|
|
|
|
{
|
2010-04-12 06:35:18 +04:00
|
|
|
nsresult rv = nsPlaintextEditor::SetFlags(aFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-11-13 11:10:03 +03:00
|
|
|
|
2010-04-12 06:35:18 +04:00
|
|
|
// Sets mCSSAware to correspond to aFlags. This toggles whether CSS is
|
|
|
|
// used to style elements in the editor. Note that the editor is only CSS
|
|
|
|
// aware by default in Composer and in the mail editor.
|
|
|
|
mCSSAware = !NoCSS() && !IsMailEditor();
|
2002-02-05 18:16:14 +03:00
|
|
|
|
2010-04-12 06:35:18 +04:00
|
|
|
return NS_OK;
|
1999-03-01 22:54:47 +03:00
|
|
|
}
|
|
|
|
|
2005-06-10 02:50:41 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::InitRules()
|
1999-03-29 12:02:05 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
// instantiate the rules for the html editor
|
2011-05-15 14:15:35 +04:00
|
|
|
mRules = new nsHTMLEditRules();
|
|
|
|
return mRules->Init(static_cast<nsPlaintextEditor*>(this));
|
1999-03-29 12:02:05 +04:00
|
|
|
}
|
|
|
|
|
2005-06-10 02:50:41 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::BeginningOfDocument()
|
2002-10-09 02:53:55 +04:00
|
|
|
{
|
2011-03-11 08:40:30 +03:00
|
|
|
if (!mDocWeak) { return NS_ERROR_NOT_INITIALIZED; }
|
2002-10-09 02:53:55 +04:00
|
|
|
|
|
|
|
// get the selection
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NOT_INITIALIZED);
|
2011-02-11 04:01:49 +03:00
|
|
|
|
|
|
|
// Get the root element.
|
2011-12-04 01:50:15 +04:00
|
|
|
nsCOMPtr<nsIDOMElement> rootElement = do_QueryInterface(GetRoot());
|
2011-02-11 04:01:49 +03:00
|
|
|
if (!rootElement) {
|
|
|
|
NS_WARNING("GetRoot() returned a null pointer (mRootElement is null)");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-10-09 02:53:55 +04:00
|
|
|
// find first editable thingy
|
2011-09-29 10:19:26 +04:00
|
|
|
bool done = false;
|
2002-10-09 02:53:55 +04:00
|
|
|
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 07:16:34 +04:00
|
|
|
(visType==nsWSRunObject::eText))
|
2002-10-09 02:53:55 +04:00
|
|
|
{
|
|
|
|
selNode = visNode;
|
|
|
|
selOffset = visOffset;
|
2011-10-17 18:59:28 +04:00
|
|
|
done = true;
|
2002-10-17 07:16:34 +04:00
|
|
|
}
|
|
|
|
else if ((visType==nsWSRunObject::eBreak) ||
|
|
|
|
(visType==nsWSRunObject::eSpecial))
|
|
|
|
{
|
|
|
|
res = GetNodeLocation(visNode, address_of(selNode), &selOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2011-10-17 18:59:28 +04:00
|
|
|
done = true;
|
2002-10-09 02:53:55 +04:00
|
|
|
}
|
|
|
|
else if (visType==nsWSRunObject::eOtherBlock)
|
|
|
|
{
|
2003-04-17 00:18:41 +04: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);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2011-10-17 18:59:28 +04:00
|
|
|
done = true;
|
2003-04-17 00:18:41 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isEmptyBlock;
|
2003-04-17 00:18:41 +04:00
|
|
|
if (NS_SUCCEEDED(IsEmptyNode(visNode, &isEmptyBlock)) &&
|
|
|
|
isEmptyBlock)
|
|
|
|
{
|
|
|
|
// skip the empty block
|
|
|
|
res = GetNodeLocation(visNode, address_of(curNode), &curOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-04-17 00:18:41 +04:00
|
|
|
++curOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
curNode = visNode;
|
|
|
|
curOffset = 0;
|
|
|
|
}
|
|
|
|
// keep looping
|
|
|
|
}
|
2002-10-09 02:53:55 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// else we found nothing useful
|
|
|
|
selNode = curNode;
|
|
|
|
selOffset = curOffset;
|
2011-10-17 18:59:28 +04:00
|
|
|
done = true;
|
2002-10-09 02:53:55 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return selection->Collapse(selNode, selOffset);
|
|
|
|
}
|
|
|
|
|
2010-06-10 05:16:58 +04:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::HandleKeyPressEvent(nsIDOMKeyEvent* aKeyEvent)
|
|
|
|
{
|
|
|
|
// NOTE: When you change this method, you should also change:
|
|
|
|
// * editor/libeditor/html/tests/test_htmleditor_keyevent_handling.html
|
|
|
|
|
|
|
|
if (IsReadonly() || IsDisabled()) {
|
|
|
|
// When we're not editable, the events are handled on nsEditor, so, we can
|
|
|
|
// bypass nsPlaintextEditor.
|
|
|
|
return nsEditor::HandleKeyPressEvent(aKeyEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsKeyEvent* nativeKeyEvent = GetNativeKeyEvent(aKeyEvent);
|
|
|
|
NS_ENSURE_TRUE(nativeKeyEvent, NS_ERROR_UNEXPECTED);
|
|
|
|
NS_ASSERTION(nativeKeyEvent->message == NS_KEY_PRESS,
|
|
|
|
"HandleKeyPressEvent gets non-keypress event");
|
|
|
|
|
|
|
|
switch (nativeKeyEvent->keyCode) {
|
|
|
|
case nsIDOMKeyEvent::DOM_VK_META:
|
|
|
|
case nsIDOMKeyEvent::DOM_VK_SHIFT:
|
|
|
|
case nsIDOMKeyEvent::DOM_VK_CONTROL:
|
|
|
|
case nsIDOMKeyEvent::DOM_VK_ALT:
|
|
|
|
case nsIDOMKeyEvent::DOM_VK_BACK_SPACE:
|
|
|
|
case nsIDOMKeyEvent::DOM_VK_DELETE:
|
|
|
|
// These keys are handled on nsEditor, so, we can bypass
|
|
|
|
// nsPlaintextEditor.
|
|
|
|
return nsEditor::HandleKeyPressEvent(aKeyEvent);
|
|
|
|
case nsIDOMKeyEvent::DOM_VK_TAB: {
|
|
|
|
if (IsPlaintextEditor()) {
|
|
|
|
// If this works as plain text editor, e.g., mail editor for plain
|
|
|
|
// text, should be handled on nsPlaintextEditor.
|
|
|
|
return nsPlaintextEditor::HandleKeyPressEvent(aKeyEvent);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (IsTabbable()) {
|
|
|
|
return NS_OK; // let it be used for focus switching
|
|
|
|
}
|
|
|
|
|
2012-04-25 07:00:02 +04:00
|
|
|
if (nativeKeyEvent->IsControl() || nativeKeyEvent->IsAlt() ||
|
|
|
|
nativeKeyEvent->IsMeta()) {
|
2010-06-10 05:16:58 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult rv = GetSelection(getter_AddRefs(selection));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
PRInt32 offset;
|
|
|
|
nsCOMPtr<nsIDOMNode> node, blockParent;
|
2010-06-11 06:46:51 +04:00
|
|
|
rv = GetStartNodeAndOffset(selection, getter_AddRefs(node), &offset);
|
2010-06-10 05:16:58 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_FAILURE);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isBlock = false;
|
2010-06-10 05:16:58 +04:00
|
|
|
NodeIsBlock(node, &isBlock);
|
|
|
|
if (isBlock) {
|
|
|
|
blockParent = node;
|
|
|
|
} else {
|
|
|
|
blockParent = GetBlockNodeParent(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!blockParent) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool handled = false;
|
2010-06-10 05:16:58 +04:00
|
|
|
if (nsHTMLEditUtils::IsTableElement(blockParent)) {
|
2012-04-25 07:00:02 +04:00
|
|
|
rv = TabInTable(nativeKeyEvent->IsShift(), &handled);
|
2010-06-10 05:16:58 +04:00
|
|
|
if (handled) {
|
2011-10-17 18:59:28 +04:00
|
|
|
ScrollSelectionIntoView(false);
|
2010-06-10 05:16:58 +04:00
|
|
|
}
|
|
|
|
} else if (nsHTMLEditUtils::IsListItem(blockParent)) {
|
2012-04-25 07:00:02 +04:00
|
|
|
rv = Indent(nativeKeyEvent->IsShift() ?
|
2010-06-10 05:16:58 +04:00
|
|
|
NS_LITERAL_STRING("outdent") :
|
|
|
|
NS_LITERAL_STRING("indent"));
|
2011-10-17 18:59:28 +04:00
|
|
|
handled = true;
|
2010-06-10 05:16:58 +04:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (handled) {
|
|
|
|
return aKeyEvent->PreventDefault(); // consumed
|
|
|
|
}
|
2012-04-25 07:00:02 +04:00
|
|
|
if (nativeKeyEvent->IsShift()) {
|
2010-06-10 05:16:58 +04:00
|
|
|
return NS_OK; // don't type text for shift tabs
|
|
|
|
}
|
|
|
|
aKeyEvent->PreventDefault();
|
|
|
|
return TypedText(NS_LITERAL_STRING("\t"), eTypedText);
|
|
|
|
}
|
|
|
|
case nsIDOMKeyEvent::DOM_VK_RETURN:
|
|
|
|
case nsIDOMKeyEvent::DOM_VK_ENTER:
|
2012-04-25 07:00:02 +04:00
|
|
|
if (nativeKeyEvent->IsControl() || nativeKeyEvent->IsAlt() ||
|
|
|
|
nativeKeyEvent->IsMeta()) {
|
2010-06-10 05:16:58 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
aKeyEvent->PreventDefault(); // consumed
|
2012-04-25 07:00:02 +04:00
|
|
|
if (nativeKeyEvent->IsShift() && !IsPlaintextEditor()) {
|
2010-06-10 05:16:58 +04:00
|
|
|
// only inserts a br node
|
|
|
|
return TypedText(EmptyString(), eTypedBR);
|
|
|
|
}
|
|
|
|
// uses rules to figure out what to insert
|
|
|
|
return TypedText(EmptyString(), eTypedBreak);
|
|
|
|
}
|
|
|
|
|
|
|
|
// NOTE: On some keyboard layout, some characters are inputted with Control
|
|
|
|
// key or Alt key, but at that time, widget sets FALSE to these keys.
|
2012-04-25 07:00:02 +04:00
|
|
|
if (nativeKeyEvent->charCode == 0 || nativeKeyEvent->IsControl() ||
|
|
|
|
nativeKeyEvent->IsAlt() || nativeKeyEvent->IsMeta()) {
|
2010-06-10 05:16:58 +04:00
|
|
|
// we don't PreventDefault() here or keybindings like control-x won't work
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
aKeyEvent->PreventDefault();
|
|
|
|
nsAutoString str(nativeKeyEvent->charCode);
|
|
|
|
return TypedText(str, eTypedText);
|
|
|
|
}
|
|
|
|
|
2001-04-06 03:48:01 +04: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
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::NodeIsBlockStatic(nsIDOMNode *aNode, bool *aIsBlock)
|
2001-04-06 03:48:01 +04:00
|
|
|
{
|
2001-04-07 04:45:26 +04:00
|
|
|
if (!aNode || !aIsBlock) { return NS_ERROR_NULL_POINTER; }
|
2001-04-06 03:48:01 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*aIsBlock = false;
|
2003-07-19 12:51:54 +04:00
|
|
|
|
2002-06-12 00:43:49 +04:00
|
|
|
#define USE_PARSER_FOR_BLOCKNESS 1
|
2001-04-06 07:18:41 +04:00
|
|
|
#ifdef USE_PARSER_FOR_BLOCKNESS
|
2001-04-06 03:48:01 +04:00
|
|
|
nsresult rv;
|
|
|
|
|
2003-07-19 12:51:54 +04:00
|
|
|
nsCOMPtr<nsIDOMElement>element = do_QueryInterface(aNode);
|
2001-04-06 03:48:01 +04:00
|
|
|
if (!element)
|
|
|
|
{
|
|
|
|
// We don't have an element -- probably a text node
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-11-19 04:20:56 +03:00
|
|
|
nsIAtom *tagAtom = GetTag(aNode);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(tagAtom, NS_ERROR_NULL_POINTER);
|
2001-04-06 03:48:01 +04:00
|
|
|
|
|
|
|
// Nodes we know we want to treat as block
|
|
|
|
// even though the parser says they're not:
|
2003-04-17 09:42:33 +04: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 ||
|
2004-04-09 17:55:59 +04:00
|
|
|
tagAtom==nsEditProperty::dt ||
|
|
|
|
tagAtom==nsEditProperty::dd ||
|
2003-04-17 09:42:33 +04:00
|
|
|
tagAtom==nsEditProperty::pre)
|
2001-04-06 03:48:01 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*aIsBlock = true;
|
2001-04-06 03:48:01 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-02-24 23:07:06 +03:00
|
|
|
rv = nsContentUtils::GetParserService()->
|
|
|
|
IsBlock(nsContentUtils::GetParserService()->HTMLAtomTagToId(tagAtom),
|
|
|
|
*aIsBlock);
|
2001-04-06 03:48:01 +04:00
|
|
|
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Check this against what we would have said with the old code:
|
2003-04-17 09:42:33 +04: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-06 03:48:01 +04:00
|
|
|
{
|
2001-04-07 04:45:26 +04:00
|
|
|
if (!(*aIsBlock))
|
2001-04-06 03:48:01 +04:00
|
|
|
{
|
|
|
|
nsAutoString assertmsg (NS_LITERAL_STRING("Parser and editor disagree on blockness: "));
|
2003-07-19 12:51:54 +04:00
|
|
|
|
|
|
|
nsAutoString tagName;
|
|
|
|
rv = element->GetTagName(tagName);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-07-19 12:51:54 +04:00
|
|
|
|
2001-04-06 03:48:01 +04:00
|
|
|
assertmsg.Append(tagName);
|
2001-09-29 12:28:41 +04:00
|
|
|
char* assertstr = ToNewCString(assertmsg);
|
2001-04-07 04:45:26 +04:00
|
|
|
NS_ASSERTION(*aIsBlock, assertstr);
|
2007-03-31 02:44:22 +04:00
|
|
|
NS_Free(assertstr);
|
2001-04-06 03:48:01 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
return rv;
|
2001-04-06 07:18:41 +04:00
|
|
|
#else /* USE_PARSER_FOR_BLOCKNESS */
|
|
|
|
nsresult result = NS_ERROR_FAILURE;
|
2011-10-17 18:59:28 +04:00
|
|
|
*aIsBlock = false;
|
2001-04-06 07:18:41 +04:00
|
|
|
nsCOMPtr<nsIDOMElement>element;
|
|
|
|
element = do_QueryInterface(aNode);
|
|
|
|
if (element)
|
|
|
|
{
|
|
|
|
nsAutoString tagName;
|
|
|
|
result = element->GetTagName(tagName);
|
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
{
|
2001-12-17 10:14:49 +03:00
|
|
|
ToLowerCase(tagName);
|
2003-07-24 08:53:13 +04:00
|
|
|
nsCOMPtr<nsIAtom> tagAtom = do_GetAtom(tagName);
|
2001-04-06 07:18:41 +04:00
|
|
|
if (!tagAtom) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
|
2003-04-17 09:42:33 +04: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 07:18:41 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*aIsBlock = true;
|
2001-04-06 07:18:41 +04:00
|
|
|
}
|
|
|
|
result = NS_OK;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// We don't have an element -- probably a text node
|
|
|
|
nsCOMPtr<nsIDOMCharacterData>nodeAsText = do_QueryInterface(aNode);
|
|
|
|
if (nodeAsText)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*aIsBlock = false;
|
2001-04-06 07:18:41 +04:00
|
|
|
result = NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
|
|
|
|
#endif /* USE_PARSER_FOR_BLOCKNESS */
|
2001-04-06 03:48:01 +04:00
|
|
|
}
|
|
|
|
|
2001-04-07 04:45:26 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::NodeIsBlock(nsIDOMNode *aNode, bool *aIsBlock)
|
2001-04-06 03:48:01 +04:00
|
|
|
{
|
|
|
|
return NodeIsBlockStatic(aNode, aIsBlock);
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2001-04-17 14:15:05 +04:00
|
|
|
nsHTMLEditor::IsBlockNode(nsIDOMNode *aNode)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isBlock;
|
2001-04-17 14:15:05 +04:00
|
|
|
NodeIsBlockStatic(aNode, &isBlock);
|
|
|
|
return isBlock;
|
|
|
|
}
|
|
|
|
|
2011-11-28 16:53:29 +04:00
|
|
|
bool
|
|
|
|
nsHTMLEditor::IsBlockNode(nsINode *aNode)
|
|
|
|
{
|
|
|
|
bool isBlock;
|
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aNode);
|
|
|
|
NodeIsBlockStatic(node, &isBlock);
|
|
|
|
return isBlock;
|
|
|
|
}
|
|
|
|
|
2001-04-07 04:45:26 +04:00
|
|
|
// Non-static version for the nsIEditor interface and JavaScript
|
2001-02-27 01:02:58 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::SetDocumentTitle(const nsAString &aTitle)
|
2001-02-27 01:02:58 +03:00
|
|
|
{
|
2009-04-25 02:45:34 +04:00
|
|
|
nsRefPtr<SetDocTitleTxn> txn = new SetDocTitleTxn();
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(txn, NS_ERROR_OUT_OF_MEMORY);
|
2001-02-27 01:02:58 +03:00
|
|
|
|
2009-04-25 02:45:34 +04:00
|
|
|
nsresult result = txn->Init(this, &aTitle);
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2001-04-12 07:41:06 +04:00
|
|
|
|
2009-04-25 02:45:34 +04:00
|
|
|
//Don't let Rules System change the selection
|
|
|
|
nsAutoTxnsConserveSelection dontChangeSelection(this);
|
|
|
|
return nsEditor::DoTransaction(txn);
|
2001-02-27 01:02:58 +03:00
|
|
|
}
|
2000-01-19 00:50:15 +03:00
|
|
|
|
2001-04-06 03:48:01 +04:00
|
|
|
/* ------------ Block methods moved from nsEditor -------------- */
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetBlockNodeParent: returns enclosing block level ancestor, if any
|
|
|
|
//
|
2011-08-11 17:29:59 +04:00
|
|
|
already_AddRefed<nsIDOMNode>
|
2001-04-06 03:48:01 +04:00
|
|
|
nsHTMLEditor::GetBlockNodeParent(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
if (!aNode)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("null node passed to GetBlockNodeParent()");
|
2011-12-18 10:00:47 +04:00
|
|
|
return nsnull;
|
2001-04-06 03:48:01 +04:00
|
|
|
}
|
|
|
|
|
2011-08-11 17:29:59 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> p;
|
2001-04-06 03:48:01 +04:00
|
|
|
if (NS_FAILED(aNode->GetParentNode(getter_AddRefs(p)))) // no parent, ran off top of tree
|
2011-08-11 17:29:59 +04:00
|
|
|
return nsnull;
|
2001-04-06 03:48:01 +04:00
|
|
|
|
2011-08-11 17:29:59 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
2001-04-06 03:48:01 +04:00
|
|
|
while (p)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isBlock;
|
2001-04-07 04:45:26 +04:00
|
|
|
if (NS_FAILED(NodeIsBlockStatic(p, &isBlock)) || isBlock)
|
2001-04-06 03:48:01 +04:00
|
|
|
break;
|
2011-08-11 17:29:59 +04:00
|
|
|
if (NS_FAILED(p->GetParentNode(getter_AddRefs(tmp))) || !tmp) // no parent, ran off top of tree
|
|
|
|
break;
|
2001-04-06 03:48:01 +04:00
|
|
|
|
|
|
|
p = tmp;
|
|
|
|
}
|
2011-08-11 17:29:59 +04:00
|
|
|
return p.forget();
|
2001-04-06 03:48:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// NextNodeInBlock: gets the next/prev node in the block, if any. Next node
|
|
|
|
// must be an element or text node, others are ignored
|
2011-08-11 17:29:59 +04:00
|
|
|
already_AddRefed<nsIDOMNode>
|
2001-04-06 03:48:01 +04:00
|
|
|
nsHTMLEditor::NextNodeInBlock(nsIDOMNode *aNode, IterDirection aDir)
|
|
|
|
{
|
2011-08-11 17:29:59 +04:00
|
|
|
NS_ENSURE_TRUE(aNode, nsnull);
|
2001-04-06 03:48:01 +04:00
|
|
|
|
2003-07-09 00:22:56 +04:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &rv);
|
2011-08-11 17:29:59 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
2001-04-06 03:48:01 +04:00
|
|
|
|
|
|
|
// much gnashing of teeth as we twit back and forth between content and domnode types
|
2011-08-11 17:29:59 +04:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
|
|
nsCOMPtr<nsIDOMNode> blockParent;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isBlock;
|
2011-08-11 17:29:59 +04:00
|
|
|
if (NS_SUCCEEDED(NodeIsBlockStatic(aNode, &isBlock)) && isBlock) {
|
2002-11-12 22:40:11 +03:00
|
|
|
blockParent = aNode;
|
2011-08-11 17:29:59 +04:00
|
|
|
} else {
|
2001-04-06 03:48:01 +04:00
|
|
|
blockParent = GetBlockNodeParent(aNode);
|
|
|
|
}
|
2011-08-11 17:29:59 +04:00
|
|
|
NS_ENSURE_TRUE(blockParent, nsnull);
|
|
|
|
nsCOMPtr<nsIContent> blockContent = do_QueryInterface(blockParent);
|
|
|
|
NS_ENSURE_TRUE(blockContent, nsnull);
|
2001-04-06 03:48:01 +04:00
|
|
|
|
2011-08-11 17:29:59 +04:00
|
|
|
if (NS_FAILED(iter->Init(blockContent))) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
if (NS_FAILED(iter->PositionAt(content))) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
2001-04-06 03:48:01 +04:00
|
|
|
|
2011-08-11 17:29:59 +04:00
|
|
|
while (!iter->IsDone()) {
|
2004-01-24 03:46:17 +03:00
|
|
|
// ignore nodes that aren't elements or text, or that are the
|
|
|
|
// block parent
|
2011-08-11 17:29:59 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(iter->GetCurrentNode());
|
2004-01-24 03:46:17 +03:00
|
|
|
if (node && IsTextOrElementNode(node) && node != blockParent &&
|
|
|
|
node != aNode)
|
2011-08-11 17:29:59 +04:00
|
|
|
return node.forget();
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2001-04-06 03:48:01 +04:00
|
|
|
if (aDir == kIterForward)
|
|
|
|
iter->Next();
|
|
|
|
else
|
|
|
|
iter->Prev();
|
|
|
|
}
|
|
|
|
|
2011-08-11 17:29:59 +04:00
|
|
|
return nsnull;
|
2001-04-06 03:48:01 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *outIsSpace,
|
|
|
|
bool *outIsNBSP,
|
2001-04-06 03:48:01 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> *outNode,
|
|
|
|
PRInt32 *outOffset)
|
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(outIsSpace && outIsNBSP, NS_ERROR_NULL_POINTER);
|
2011-10-17 18:59:28 +04:00
|
|
|
*outIsSpace = false;
|
|
|
|
*outIsNBSP = false;
|
2001-04-06 03:48:01 +04:00
|
|
|
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)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isBlock (false);
|
2001-04-07 04:45:26 +04:00
|
|
|
NodeIsBlock(node, &isBlock);
|
2001-04-06 03:48:01 +04: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,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *outIsSpace,
|
|
|
|
bool *outIsNBSP,
|
2001-04-06 03:48:01 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> *outNode,
|
|
|
|
PRInt32 *outOffset)
|
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(outIsSpace && outIsNBSP, NS_ERROR_NULL_POINTER);
|
2011-10-17 18:59:28 +04:00
|
|
|
*outIsSpace = false;
|
|
|
|
*outIsNBSP = false;
|
2001-04-06 03:48:01 +04:00
|
|
|
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)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isBlock (false);
|
2001-04-07 04:45:26 +04:00
|
|
|
NodeIsBlock(node, &isBlock);
|
2001-04-06 03:48:01 +04:00
|
|
|
if (isBlock) // skip over bold, italic, link, ect nodes
|
|
|
|
{
|
|
|
|
if (IsTextNode(node) && IsEditable(node))
|
|
|
|
{
|
|
|
|
textNode = do_QueryInterface(node);
|
|
|
|
textNode->GetLength(&strLength);
|
|
|
|
if (strLength)
|
|
|
|
{
|
2006-07-19 08:36:36 +04:00
|
|
|
// you could use nsIContent::TextIsOnlyWhitespace here
|
2001-04-06 03:48:01 +04:00
|
|
|
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 -------------- */
|
|
|
|
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsHTMLEditor::IsVisBreak(nsIDOMNode *aNode)
|
2002-05-14 04:05:37 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(aNode, false);
|
2002-05-14 04:05:37 +04:00
|
|
|
if (!nsTextEditUtils::IsBreak(aNode))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-05-14 04:05:37 +04:00
|
|
|
// check if there is a later node in block after br
|
|
|
|
nsCOMPtr<nsIDOMNode> priorNode, nextNode;
|
2011-10-17 18:59:28 +04:00
|
|
|
GetPriorHTMLNode(aNode, address_of(priorNode), true);
|
|
|
|
GetNextHTMLNode(aNode, address_of(nextNode), true);
|
2002-05-14 04:05:37 +04:00
|
|
|
// if we are next to another break, we are visible
|
|
|
|
if (priorNode && nsTextEditUtils::IsBreak(priorNode))
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-05-14 04:05:37 +04:00
|
|
|
if (nextNode && nsTextEditUtils::IsBreak(nextNode))
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-05-14 04:05:37 +04:00
|
|
|
|
|
|
|
// if we are right before block boundary, then br not visible
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(nextNode, false); // this break is trailer in block, it's not visible
|
2002-05-14 04:05:37 +04:00
|
|
|
if (IsBlockNode(nextNode))
|
2011-10-17 18:59:28 +04:00
|
|
|
return false; // break is right before a block, it's not visible
|
2002-05-14 04:05:37 +04:00
|
|
|
|
|
|
|
// 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 06:44:55 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> visNode;
|
|
|
|
PRInt32 visOffset=0;
|
|
|
|
PRInt16 visType=0;
|
|
|
|
wsObj.NextVisibleNode(selNode, selOffset, address_of(visNode), &visOffset, &visType);
|
|
|
|
if (visType & nsWSRunObject::eBlock)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-05-14 04:05:37 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-05-14 04:05:37 +04:00
|
|
|
}
|
|
|
|
|
2010-11-07 01:49:26 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::BreakIsVisible(nsIDOMNode *aNode, bool *aIsVisible)
|
2010-11-07 01:49:26 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aNode && aIsVisible);
|
|
|
|
|
|
|
|
*aIsVisible = IsVisBreak(aNode);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-05-14 04:05:37 +04:00
|
|
|
|
2002-09-18 23:42:38 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetIsDocumentEditable(bool *aIsDocumentEditable)
|
2002-09-18 23:42:38 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsDocumentEditable);
|
|
|
|
|
2012-03-24 12:19:14 +04:00
|
|
|
nsCOMPtr<nsIDOMDocument> doc = GetDOMDocument();
|
|
|
|
*aIsDocumentEditable = doc && IsModifiable();
|
2002-09-18 23:42:38 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsHTMLEditor::IsModifiable()
|
2000-01-19 00:50:15 +03:00
|
|
|
{
|
2010-04-12 06:35:18 +04:00
|
|
|
return !IsReadonly();
|
2000-01-19 00:50:15 +03:00
|
|
|
}
|
|
|
|
|
2002-11-27 03:35:09 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::UpdateBaseURL()
|
|
|
|
{
|
2012-03-24 12:19:14 +04:00
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc = GetDOMDocument();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(domDoc, NS_ERROR_FAILURE);
|
2002-11-27 03:35:09 +03:00
|
|
|
|
|
|
|
// 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);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
|
2002-11-27 03:35:09 +03:00
|
|
|
|
2004-01-10 02:54:21 +03:00
|
|
|
return doc->SetBaseURI(doc->GetDocumentURI());
|
2002-11-27 03:35:09 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-10-06 23:34:09 +04:00
|
|
|
/* This routine is needed to provide a bottleneck for typing for logging
|
2004-05-01 03:55:16 +04:00
|
|
|
purposes. Can't use HandleKeyPress() (above) for that since it takes
|
|
|
|
a nsIDOMKeyEvent* parameter. So instead we pass enough info through
|
1999-10-06 23:34:09 +04:00
|
|
|
to TypedText() to determine what action to take, but without passing
|
|
|
|
an event.
|
|
|
|
*/
|
2002-03-24 01:08:20 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::TypedText(const nsAString& aString,
|
2000-11-17 03:25:31 +03:00
|
|
|
PRInt32 aAction)
|
1999-10-06 23:34:09 +04:00
|
|
|
{
|
2008-03-13 21:54:01 +03:00
|
|
|
nsAutoPlaceHolderBatch batch(this, nsGkAtoms::TypingTxnName);
|
1999-10-06 23:34:09 +04:00
|
|
|
|
|
|
|
switch (aAction)
|
|
|
|
{
|
|
|
|
case eTypedText:
|
2001-01-28 23:13:07 +03:00
|
|
|
case eTypedBreak:
|
1999-10-06 23:34:09 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
return nsPlaintextEditor::TypedText(aString, aAction);
|
1999-10-06 23:34:09 +04:00
|
|
|
}
|
|
|
|
case eTypedBR:
|
2000-01-15 17:29:29 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> brNode;
|
2000-12-09 07:46:08 +03:00
|
|
|
return InsertBR(address_of(brNode)); // only inserts a br node
|
2000-01-15 17:29:29 +03:00
|
|
|
}
|
1999-10-06 23:34:09 +04:00
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::TabInTable(bool inIsShift, bool *outHandled)
|
1999-09-13 13:37:51 +04:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(outHandled, NS_ERROR_NULL_POINTER);
|
2011-10-17 18:59:28 +04:00
|
|
|
*outHandled = false;
|
1999-09-13 13:37:51 +04:00
|
|
|
|
2000-04-04 18:51:26 +04:00
|
|
|
// Find enclosing table cell from the selection (cell may be the selected element)
|
|
|
|
nsCOMPtr<nsIDOMElement> cellElement;
|
2000-08-20 04:34:08 +04:00
|
|
|
// can't use |NS_LITERAL_STRING| here until |GetElementOrParentByTagName| is fixed to accept readables
|
2001-11-14 04:33:42 +03:00
|
|
|
nsresult res = GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nsnull, getter_AddRefs(cellElement));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-04-04 18:51:26 +04:00
|
|
|
// Do nothing -- we didn't find a table cell
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(cellElement, NS_OK);
|
2000-04-04 18:51:26 +04:00
|
|
|
|
|
|
|
// find enclosing table
|
|
|
|
nsCOMPtr<nsIDOMNode> tbl = GetEnclosingTable(cellElement);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(tbl, res);
|
2000-04-04 18:51:26 +04:00
|
|
|
|
|
|
|
// advance to next cell
|
|
|
|
// first create an iterator over the table
|
2003-07-09 00:22:56 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(iter, NS_ERROR_NULL_POINTER);
|
2000-04-04 18:51:26 +04:00
|
|
|
nsCOMPtr<nsIContent> cTbl = do_QueryInterface(tbl);
|
|
|
|
nsCOMPtr<nsIContent> cBlock = do_QueryInterface(cellElement);
|
|
|
|
res = iter->Init(cTbl);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-04-04 18:51:26 +04:00
|
|
|
// position iter at block
|
|
|
|
res = iter->PositionAt(cBlock);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2000-04-04 18:51:26 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
do
|
1999-09-13 13:37:51 +04:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
if (inIsShift)
|
|
|
|
iter->Prev();
|
|
|
|
else
|
|
|
|
iter->Next();
|
|
|
|
|
|
|
|
node = do_QueryInterface(iter->GetCurrentNode());
|
|
|
|
|
|
|
|
if (node && nsHTMLEditUtils::IsTableCell(node) &&
|
|
|
|
GetEnclosingTable(node) == tbl)
|
1999-09-13 13:37:51 +04:00
|
|
|
{
|
2000-04-04 18:51:26 +04:00
|
|
|
res = CollapseSelectionToDeepestNonTableFirstChild(nsnull, node);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2011-10-17 18:59:28 +04:00
|
|
|
*outHandled = true;
|
2000-04-04 18:51:26 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
} while (!iter->IsDone());
|
2000-04-19 01:39:35 +04: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.
|
2011-10-17 18:59:28 +04:00
|
|
|
res = InsertTableRow(1, true);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2011-10-17 18:59:28 +04:00
|
|
|
*outHandled = true;
|
2000-04-19 01:39:35 +04:00
|
|
|
// put selection in right place
|
2000-05-02 07:24:11 +04:00
|
|
|
// Use table code to get selection and index to new row...
|
2000-09-14 15:45:01 +04:00
|
|
|
nsCOMPtr<nsISelection>selection;
|
2000-05-04 01:18:23 +04:00
|
|
|
nsCOMPtr<nsIDOMElement> tblElement;
|
2000-04-19 01:39:35 +04:00
|
|
|
nsCOMPtr<nsIDOMElement> cell;
|
2000-05-02 07:24:11 +04:00
|
|
|
PRInt32 row;
|
|
|
|
res = GetCellContext(getter_AddRefs(selection),
|
2000-05-04 01:18:23 +04:00
|
|
|
getter_AddRefs(tblElement),
|
2000-05-02 07:24:11 +04:00
|
|
|
getter_AddRefs(cell),
|
|
|
|
nsnull, nsnull,
|
|
|
|
&row, nsnull);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-05-02 07:24:11 +04:00
|
|
|
// ...so that we can ask for first cell in that row...
|
2001-02-27 01:02:58 +03:00
|
|
|
res = GetCellAt(tblElement, row, 0, getter_AddRefs(cell));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-05-02 07:24:11 +04: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-19 01:39:35 +04:00
|
|
|
node = do_QueryInterface(cell);
|
|
|
|
if (node) selection->Collapse(node,0);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
1999-09-13 13:37:51 +04:00
|
|
|
}
|
|
|
|
|
2000-03-24 03:26:47 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::CreateBR(nsIDOMNode *aNode, PRInt32 aOffset, nsCOMPtr<nsIDOMNode> *outBRNode, EDirection aSelect)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = aNode;
|
|
|
|
PRInt32 offset = aOffset;
|
2000-12-09 07:46:08 +03:00
|
|
|
return CreateBRImpl(address_of(parent), &offset, outBRNode, aSelect);
|
2000-03-24 03:26:47 +03:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::CollapseSelectionToDeepestNonTableFirstChild(nsISelection *aSelection, nsIDOMNode *aNode)
|
2000-03-24 03:26:47 +03:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
|
2000-03-24 03:26:47 +03:00
|
|
|
nsresult res;
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
if (aSelection)
|
2000-03-24 03:26:47 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
selection = aSelection;
|
|
|
|
} else {
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_FAILURE);
|
2000-03-24 03:26:47 +03:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> node = aNode;
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
2000-03-24 03:26:47 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
do {
|
|
|
|
node->GetFirstChild(getter_AddRefs(child));
|
|
|
|
|
|
|
|
if (child)
|
2000-03-24 03:26:47 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03: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 03:26:47 +03:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
while (child);
|
|
|
|
|
|
|
|
selection->Collapse(node,0);
|
2000-03-24 03:26:47 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-04-14 01:50:19 +04:00
|
|
|
|
|
|
|
|
2003-04-15 18:07:30 +04:00
|
|
|
// This is mostly like InsertHTMLWithCharsetAndContext,
|
2001-01-28 23:13:07 +03: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-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::ReplaceHeadContentsWithHTML(const nsAString& aSourceToInsert)
|
2000-03-24 03:26:47 +03:00
|
|
|
{
|
2005-11-25 11:16:51 +03:00
|
|
|
nsAutoRules beginRulesSniffing(this, kOpIgnore, nsIEditor::eNone); // don't do any post processing, rules get confused
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
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);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED);
|
2002-01-14 23:31:43 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNodeList>nodeList;
|
|
|
|
res = doc->GetElementsByTagName(NS_LITERAL_STRING("head"), getter_AddRefs(nodeList));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(nodeList, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
PRUint32 count;
|
|
|
|
nodeList->GetLength(&count);
|
|
|
|
if (count < 1) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> headNode;
|
|
|
|
res = nodeList->Item(0, getter_AddRefs(headNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(headNode, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// 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 04:33:42 +03:00
|
|
|
inputString.ReplaceSubstring(NS_LITERAL_STRING("\r\n").get(),
|
|
|
|
NS_LITERAL_STRING("\n").get());
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// Mac linebreaks: Map any remaining CR to LF:
|
2001-11-14 04:33:42 +03:00
|
|
|
inputString.ReplaceSubstring(NS_LITERAL_STRING("\r").get(),
|
|
|
|
NS_LITERAL_STRING("\n").get());
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// Get the first range in the selection, for context:
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
res = selection->GetRangeAt(0, getter_AddRefs(range));
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocumentFragment> docfrag;
|
2012-01-10 18:19:54 +04:00
|
|
|
res = range->CreateContextualFragment(inputString,
|
|
|
|
getter_AddRefs(docfrag));
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
//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 07:19:31 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
if (NS_FAILED(res))
|
2000-03-24 03:26:47 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
printf("Couldn't create contextual fragment: error was %d\n", res);
|
|
|
|
#endif
|
|
|
|
return res;
|
2000-03-24 03:26:47 +03:00
|
|
|
}
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(docfrag, NS_ERROR_NULL_POINTER);
|
2000-04-14 07:19:31 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
|
|
|
|
// First delete all children in head
|
|
|
|
do {
|
|
|
|
res = headNode->GetFirstChild(getter_AddRefs(child));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
if (child)
|
2000-03-24 03:26:47 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
res = DeleteNode(child);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-03-24 03:26:47 +03:00
|
|
|
}
|
2001-01-28 23:13:07 +03: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));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
if (child)
|
2000-03-24 03:26:47 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
res = InsertNode(child, headNode, offsetOfNewNode++);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-03-24 03:26:47 +03:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
} while (child);
|
|
|
|
|
2000-03-24 03:26:47 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::RebuildDocumentFromSource(const nsAString& aSourceString)
|
2000-03-24 03:26:47 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
ForceCompositionEnd();
|
2000-03-24 03:26:47 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
2011-12-04 01:50:15 +04:00
|
|
|
nsCOMPtr<nsIDOMElement> bodyElement = do_QueryInterface(GetRoot());
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(bodyElement, NS_ERROR_NULL_POINTER);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Find where the <body> tag starts.
|
2001-04-07 04:45:26 +04:00
|
|
|
nsReadingIterator<PRUnichar> beginbody;
|
|
|
|
nsReadingIterator<PRUnichar> endbody;
|
|
|
|
aSourceString.BeginReading(beginbody);
|
|
|
|
aSourceString.EndReading(endbody);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool foundbody = CaseInsensitiveFindInReadable(NS_LITERAL_STRING("<body"),
|
2004-07-08 02:34:20 +04:00
|
|
|
beginbody, endbody);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
2001-04-07 04:45:26 +04:00
|
|
|
nsReadingIterator<PRUnichar> beginhead;
|
|
|
|
nsReadingIterator<PRUnichar> endhead;
|
|
|
|
aSourceString.BeginReading(beginhead);
|
|
|
|
aSourceString.EndReading(endhead);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool foundhead = CaseInsensitiveFindInReadable(NS_LITERAL_STRING("<head"),
|
2004-07-08 02:34:20 +04:00
|
|
|
beginhead, endhead);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
2001-04-07 04:45:26 +04:00
|
|
|
nsReadingIterator<PRUnichar> beginclosehead;
|
|
|
|
nsReadingIterator<PRUnichar> endclosehead;
|
|
|
|
aSourceString.BeginReading(beginclosehead);
|
|
|
|
aSourceString.EndReading(endclosehead);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
2001-04-07 04:45:26 +04:00
|
|
|
// Find the index after "<head>"
|
2011-09-29 10:19:26 +04:00
|
|
|
bool foundclosehead = CaseInsensitiveFindInReadable(
|
2004-07-08 02:34:20 +04:00
|
|
|
NS_LITERAL_STRING("</head>"), beginclosehead, endclosehead);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Time to change the document
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
2001-04-07 04:45:26 +04:00
|
|
|
nsReadingIterator<PRUnichar> endtotal;
|
|
|
|
aSourceString.EndReading(endtotal);
|
|
|
|
|
2004-07-08 02:34:20 +04:00
|
|
|
if (foundhead) {
|
|
|
|
if (foundclosehead)
|
|
|
|
res = ReplaceHeadContentsWithHTML(Substring(beginhead, beginclosehead));
|
|
|
|
else if (foundbody)
|
|
|
|
res = ReplaceHeadContentsWithHTML(Substring(beginhead, beginbody));
|
|
|
|
else
|
|
|
|
// XXX Without recourse to some parser/content sink/docshell hackery
|
|
|
|
// we don't really know where the head ends and the body begins
|
|
|
|
// so we assume that there is no body
|
|
|
|
res = ReplaceHeadContentsWithHTML(Substring(beginhead, endtotal));
|
|
|
|
} else {
|
|
|
|
nsReadingIterator<PRUnichar> begintotal;
|
|
|
|
aSourceString.BeginReading(begintotal);
|
|
|
|
NS_NAMED_LITERAL_STRING(head, "<head>");
|
|
|
|
if (foundclosehead)
|
|
|
|
res = ReplaceHeadContentsWithHTML(head + Substring(begintotal, beginclosehead));
|
|
|
|
else if (foundbody)
|
|
|
|
res = ReplaceHeadContentsWithHTML(head + Substring(begintotal, beginbody));
|
|
|
|
else
|
|
|
|
// XXX Without recourse to some parser/content sink/docshell hackery
|
|
|
|
// we don't really know where the head ends and the body begins
|
|
|
|
// so we assume that there is no head
|
|
|
|
res = ReplaceHeadContentsWithHTML(head);
|
|
|
|
}
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2004-07-21 13:57:49 +04:00
|
|
|
res = SelectAll();
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2004-07-21 13:57:49 +04:00
|
|
|
|
2004-07-08 02:34:20 +04:00
|
|
|
if (!foundbody) {
|
|
|
|
NS_NAMED_LITERAL_STRING(body, "<body>");
|
|
|
|
// XXX Without recourse to some parser/content sink/docshell hackery
|
|
|
|
// we don't really know where the head ends and the body begins
|
|
|
|
if (foundclosehead) // assume body starts after the head ends
|
|
|
|
res = LoadHTML(body + Substring(endclosehead, endtotal));
|
|
|
|
else if (foundhead) // assume there is no body
|
|
|
|
res = LoadHTML(body);
|
|
|
|
else // assume there is no head, the entire source is body
|
|
|
|
res = LoadHTML(body + aSourceString);
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2005-11-30 14:01:37 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> divElement;
|
|
|
|
res = CreateElementWithDefaults(NS_LITERAL_STRING("div"), getter_AddRefs(divElement));
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2005-11-30 14:01:37 +03:00
|
|
|
|
|
|
|
res = CloneAttributes(bodyElement, divElement);
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2004-07-08 02:34:20 +04:00
|
|
|
|
|
|
|
return BeginningOfDocument();
|
|
|
|
}
|
|
|
|
|
|
|
|
res = LoadHTML(Substring(beginbody, endtotal));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03: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 07:02:10 +04:00
|
|
|
|
|
|
|
// We already know where "<body" begins
|
|
|
|
nsReadingIterator<PRUnichar> beginclosebody = beginbody;
|
2001-04-07 04:45:26 +04:00
|
|
|
nsReadingIterator<PRUnichar> endclosebody;
|
|
|
|
aSourceString.EndReading(endclosebody);
|
|
|
|
if (!FindInReadable(NS_LITERAL_STRING(">"),beginclosebody,endclosebody))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Truncate at the end of the body tag
|
|
|
|
// Kludge of the year: fool the parser by replacing "body" with "div" so we get a node
|
2006-03-28 20:36:40 +04:00
|
|
|
nsAutoString bodyTag;
|
|
|
|
bodyTag.AssignLiteral("<div ");
|
|
|
|
bodyTag.Append(Substring(endbody, endclosebody));
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
res = selection->GetRangeAt(0, getter_AddRefs(range));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMDocumentFragment> docfrag;
|
2012-01-10 18:19:54 +04:00
|
|
|
res = range->CreateContextualFragment(bodyTag, getter_AddRefs(docfrag));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
1999-03-01 22:54:47 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> fragmentAsNode (do_QueryInterface(docfrag));
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(fragmentAsNode, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
res = fragmentAsNode->GetFirstChild(getter_AddRefs(child));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(child, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// Copy all attributes from the div child to current body element
|
2002-10-09 02:53:55 +04:00
|
|
|
res = CloneAttributes(bodyElement, child);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-10-09 02:53:55 +04:00
|
|
|
|
|
|
|
// 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-25 03:00:24 +04:00
|
|
|
}
|
|
|
|
|
2003-04-16 00:53:15 +04: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 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::InsertElementAtSelection(nsIDOMElement* aElement, bool aDeleteSelection)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
2010-05-28 05:18:48 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult res = NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(aElement, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aElement);
|
|
|
|
|
|
|
|
ForceCompositionEnd();
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpInsertElement, nsIEditor::eNext);
|
|
|
|
|
2000-09-14 15:45:01 +04:00
|
|
|
nsCOMPtr<nsISelection>selection;
|
2001-01-28 23:13:07 +03:00
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2001-11-19 09:45:00 +03:00
|
|
|
if (NS_FAILED(res) || !selection)
|
2001-01-28 23:13:07 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
1999-08-19 17:30:48 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// hand off to the rules system, see if it has anything to say about this
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
2012-05-05 22:52:29 +04:00
|
|
|
nsTextRulesInfo ruleInfo(kOpInsertElement);
|
2001-01-28 23:13:07 +03:00
|
|
|
ruleInfo.insertElement = aElement;
|
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || (NS_FAILED(res))) return res;
|
1999-08-19 17:30:48 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
if (!handled)
|
1999-08-09 05:37:50 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
if (aDeleteSelection)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tempNode;
|
|
|
|
PRInt32 tempOffset;
|
|
|
|
nsresult result = DeleteSelectionAndPrepareToCreateNode(tempNode,tempOffset);
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2000-01-31 13:30:12 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// If deleting, selection will be collapsed.
|
|
|
|
// so if not, we collapse it
|
|
|
|
if (!aDeleteSelection)
|
2000-01-31 13:30:12 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03: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 13:30:12 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
selection->CollapseToStart();
|
|
|
|
} else {
|
|
|
|
selection->CollapseToEnd();
|
2000-01-31 13:30:12 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03: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 05:37:50 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
#ifdef DEBUG_cmanske
|
1999-08-19 17:30:48 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoString name;
|
|
|
|
parentSelectedNode->GetNodeName(name);
|
|
|
|
printf("InsertElement: Anchor node of selection: ");
|
2001-06-30 15:02:25 +04:00
|
|
|
wprintf(name.get());
|
2001-01-28 23:13:07 +03:00
|
|
|
printf(" Offset: %d\n", offsetForInsert);
|
1999-08-09 05:37:50 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
#endif
|
1999-03-01 22:54:47 +03:00
|
|
|
|
2003-04-16 00:53:15 +04:00
|
|
|
// Adjust position based on the node we are going to insert.
|
|
|
|
NormalizeEOLInsertPosition(node, address_of(parentSelectedNode), &offsetForInsert);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
res = InsertNodeAtPoint(node, address_of(parentSelectedNode), &offsetForInsert, false);
|
2001-01-28 23:13:07 +03: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-27 03:13:01 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
res = SetCaretAfterElement(aElement);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-27 03:13:01 +04:00
|
|
|
}
|
|
|
|
// check for inserting a whole table at the end of a block. If so insert a br after it.
|
|
|
|
if (nsHTMLEditUtils::IsTable(node))
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isLast;
|
2001-06-27 03:13:01 +04:00
|
|
|
res = IsLastEditableChild(node, &isLast);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-27 03:13:01 +04:00
|
|
|
if (isLast)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> brNode;
|
|
|
|
res = CreateBR(parentSelectedNode, offsetForInsert+1, address_of(brNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-09-15 01:12:35 +04:00
|
|
|
selection->Collapse(parentSelectedNode, offsetForInsert+1);
|
2001-06-27 03:13:01 +04:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
2000-04-14 01:50:19 +04:00
|
|
|
}
|
|
|
|
|
2001-06-16 02:29:07 +04: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
|
2010-04-29 23:01:11 +04:00
|
|
|
allowed, will attempt to find a parent in the parent hierarchy of *ioParent that will
|
2001-06-16 02:29:07 +04:00
|
|
|
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
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aNoEmptyNodes splitting can result in empty nodes?
|
2001-06-16 02:29:07 +04:00
|
|
|
*/
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::InsertNodeAtPoint(nsIDOMNode *aNode,
|
2001-06-16 02:29:07 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> *ioParent,
|
|
|
|
PRInt32 *ioOffset,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aNoEmptyNodes)
|
1999-05-27 01:40:51 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_ENSURE_TRUE(aNode, NS_ERROR_NULL_POINTER);
|
2001-06-16 02:29:07 +04: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 23:13:07 +03:00
|
|
|
|
|
|
|
nsresult res = NS_OK;
|
2001-06-16 02:29:07 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> parent = *ioParent;
|
|
|
|
nsCOMPtr<nsIDOMNode> topChild = *ioParent;
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
2001-06-16 02:29:07 +04:00
|
|
|
PRInt32 offsetOfInsert = *ioOffset;
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// Search up the parent chain to find a suitable container
|
2012-05-01 10:34:52 +04:00
|
|
|
while (!CanContain(parent, aNode)) {
|
2001-01-28 23:13:07 +03:00
|
|
|
// If the current parent is a root (body or table element)
|
|
|
|
// then go no further - we can't insert
|
2001-04-06 03:48:01 +04:00
|
|
|
if (nsTextEditUtils::IsBody(parent) || nsHTMLEditUtils::IsTableElement(parent))
|
2001-01-28 23:13:07 +03: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-16 02:29:07 +04:00
|
|
|
res = SplitNodeDeep(topChild, *ioParent, *ioOffset, &offsetOfInsert, aNoEmptyNodes);
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-16 02:29:07 +04:00
|
|
|
*ioParent = parent;
|
|
|
|
*ioOffset = offsetOfInsert;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
// Now we can insert the new node
|
|
|
|
res = InsertNode(aNode, parent, offsetOfInsert);
|
|
|
|
return res;
|
2000-04-14 01:50:19 +04:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::SelectElement(nsIDOMElement* aElement)
|
2000-04-14 01:50:19 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult res = NS_ERROR_NULL_POINTER;
|
2000-04-14 07:19:31 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Must be sure that element is contained in the document body
|
2012-05-06 11:53:11 +04:00
|
|
|
if (IsDescendantOfEditorRoot(aElement)) {
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode>parent;
|
|
|
|
res = aElement->GetParentNode(getter_AddRefs(parent));
|
|
|
|
if (NS_SUCCEEDED(res) && parent)
|
1999-08-19 17:30:48 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
PRInt32 offsetInParent;
|
|
|
|
res = GetChildOffset(aElement, parent, offsetInParent);
|
1999-08-19 17:30:48 +04:00
|
|
|
|
2001-01-28 23:13:07 +03: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 03:26:47 +03:00
|
|
|
}
|
1999-08-19 17:30:48 +04:00
|
|
|
}
|
1999-08-09 05:37:50 +04:00
|
|
|
}
|
1999-10-06 23:34:09 +04:00
|
|
|
}
|
2000-03-24 03:26:47 +03:00
|
|
|
return res;
|
1999-05-27 01:40:51 +04:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::SetCaretAfterElement(nsIDOMElement* aElement)
|
1999-11-13 19:37:58 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult res = NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// Be sure the element is contained in the document body
|
2012-05-06 11:53:11 +04:00
|
|
|
if (aElement && IsDescendantOfEditorRoot(aElement)) {
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode>parent;
|
|
|
|
res = aElement->GetParentNode(getter_AddRefs(parent));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
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 15:02:25 +04:00
|
|
|
wprintf(name.get());
|
2001-01-28 23:13:07 +03:00
|
|
|
printf(" Offset: %d\n\nHTML:\n", offsetInParent+1);
|
|
|
|
nsAutoString Format("text/html");
|
|
|
|
nsAutoString ContentsAs;
|
2002-03-26 01:39:19 +03:00
|
|
|
OutputToString(Format, 2, ContentsAs);
|
2001-06-30 15:02:25 +04:00
|
|
|
wprintf(ContentsAs.get());
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
1999-11-13 19:37:58 +03:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::SetParagraphFormat(const nsAString& aParagraphFormat)
|
1999-11-13 19:37:58 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoString tag; tag.Assign(aParagraphFormat);
|
2001-12-17 10:14:49 +03:00
|
|
|
ToLowerCase(tag);
|
2004-05-23 02:15:22 +04:00
|
|
|
if (tag.EqualsLiteral("dd") || tag.EqualsLiteral("dt"))
|
2001-01-28 23:13:07 +03:00
|
|
|
return MakeDefinitionItem(tag);
|
|
|
|
else
|
|
|
|
return InsertBasicBlock(tag);
|
1999-11-13 19:37:58 +03:00
|
|
|
}
|
1999-09-22 05:21:56 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetParagraphState(bool *aMixed, nsAString &outFormat)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aMixed, NS_ERROR_NULL_POINTER);
|
2011-05-07 12:04:56 +04:00
|
|
|
nsHTMLEditRules* htmlRules = static_cast<nsHTMLEditRules*>(mRules.get());
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(htmlRules, NS_ERROR_FAILURE);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
return htmlRules->GetParagraphState(aMixed, outFormat);
|
|
|
|
}
|
2000-06-01 06:38:13 +04:00
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetBackgroundColorState(bool *aMixed, nsAString &aOutColor)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
nsresult res;
|
2012-02-01 14:54:22 +04:00
|
|
|
if (IsCSSEnabled()) {
|
2002-01-09 16:51:37 +03:00
|
|
|
// if we are in CSS mode, we have to check if the containing block defines
|
|
|
|
// a background color
|
2011-10-17 18:59:28 +04:00
|
|
|
res = GetCSSBackgroundColorState(aMixed, aOutColor, true);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// in HTML mode, we look only at page's background
|
|
|
|
res = GetHTMLBackgroundColorState(aMixed, aOutColor);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetHighlightColorState(bool *aMixed, nsAString &aOutColor)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
2011-10-17 18:59:28 +04:00
|
|
|
*aMixed = false;
|
2004-06-17 04:13:25 +04:00
|
|
|
aOutColor.AssignLiteral("transparent");
|
2012-02-01 14:54:22 +04:00
|
|
|
if (IsCSSEnabled()) {
|
2002-01-09 16:51:37 +03:00
|
|
|
// 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
|
2011-10-17 18:59:28 +04:00
|
|
|
res = GetCSSBackgroundColorState(aMixed, aOutColor, false);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetCSSBackgroundColorState(bool *aMixed, nsAString &aOutColor, bool aBlockLevel)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aMixed, NS_ERROR_NULL_POINTER);
|
2011-10-17 18:59:28 +04:00
|
|
|
*aMixed = false;
|
2002-01-09 16:51:37 +03:00
|
|
|
// the default background color is transparent
|
2004-06-17 04:13:25 +04:00
|
|
|
aOutColor.AssignLiteral("transparent");
|
2002-01-09 16:51:37 +03:00
|
|
|
|
|
|
|
// get selection
|
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
|
|
|
|
// get selection location
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
PRInt32 offset;
|
2010-06-11 06:46:51 +04:00
|
|
|
res = GetStartNodeAndOffset(selection, getter_AddRefs(parent), &offset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
|
2009-02-19 10:55:40 +03:00
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
// is the selection collapsed?
|
2011-09-29 10:19:26 +04:00
|
|
|
bool bCollapsed;
|
2002-01-09 16:51:37 +03:00
|
|
|
res = selection->GetIsCollapsed(&bCollapsed);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
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
|
2012-02-15 13:18:24 +04:00
|
|
|
// {parent,offset} and its ancestors for divs with alignment on them
|
2002-01-09 16:51:37 +03:00
|
|
|
nodeToExamine = GetChildAt(parent, offset);
|
2011-10-17 18:59:28 +04:00
|
|
|
//GetNextNode(parent, offset, true, address_of(nodeToExamine));
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(nodeToExamine, NS_ERROR_NULL_POINTER);
|
2002-01-09 16:51:37 +03:00
|
|
|
|
|
|
|
// is the node to examine a block ?
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isBlock;
|
2002-01-09 16:51:37 +03:00
|
|
|
res = NodeIsBlockStatic(nodeToExamine, &isBlock);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
|
|
|
|
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);
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(blockParent, NS_OK);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2006-02-24 07:54:10 +03:00
|
|
|
|
|
|
|
// Make sure to not walk off onto the Document node
|
|
|
|
nsCOMPtr<nsIDOMElement> element;
|
2002-01-09 16:51:37 +03:00
|
|
|
do {
|
|
|
|
// retrieve the computed style of background-color for blockParent
|
2005-03-24 22:00:01 +03:00
|
|
|
mHTMLCSSUtils->GetComputedProperty(blockParent,
|
|
|
|
nsEditProperty::cssBackgroundColor,
|
2002-01-09 16:51:37 +03:00
|
|
|
aOutColor);
|
2005-03-24 22:00:01 +03:00
|
|
|
tmp.swap(blockParent);
|
2002-01-09 16:51:37 +03:00
|
|
|
res = tmp->GetParentNode(getter_AddRefs(blockParent));
|
2006-02-24 07:54:10 +03:00
|
|
|
element = do_QueryInterface(blockParent);
|
2002-01-09 16:51:37 +03:00
|
|
|
// look at parent if the queried color is transparent and if the node to
|
|
|
|
// examine is not the root of the document
|
2006-02-24 07:54:10 +03:00
|
|
|
} while (aOutColor.EqualsLiteral("transparent") && element);
|
2005-03-24 22:00:01 +03:00
|
|
|
if (aOutColor.EqualsLiteral("transparent")) {
|
2002-01-09 16:51:37 +03: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));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
nodeToExamine = parent;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
// is the node to examine a block ?
|
|
|
|
res = NodeIsBlockStatic(nodeToExamine, &isBlock);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
if (isBlock) {
|
|
|
|
// yes it is a block; in that case, the text background color is transparent
|
2004-06-17 04:13:25 +04:00
|
|
|
aOutColor.AssignLiteral("transparent");
|
2002-01-09 16:51:37 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// no, it's not; let's retrieve the computed style of background-color for the
|
|
|
|
// node to examine
|
2003-04-17 09:42:33 +04:00
|
|
|
mHTMLCSSUtils->GetComputedProperty(nodeToExamine, nsEditProperty::cssBackgroundColor,
|
2002-01-09 16:51:37 +03:00
|
|
|
aOutColor);
|
2004-05-23 02:15:22 +04:00
|
|
|
if (!aOutColor.EqualsLiteral("transparent")) {
|
2002-01-09 16:51:37 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-03-24 22:00:01 +03:00
|
|
|
tmp.swap(nodeToExamine);
|
|
|
|
res = tmp->GetParentNode(getter_AddRefs(nodeToExamine));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2005-03-24 22:00:01 +03:00
|
|
|
} while ( aOutColor.EqualsLiteral("transparent") && nodeToExamine );
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetHTMLBackgroundColorState(bool *aMixed, nsAString &aOutColor)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
//TODO: We don't handle "mixed" correctly!
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aMixed, NS_ERROR_NULL_POINTER);
|
2011-10-17 18:59:28 +04:00
|
|
|
*aMixed = false;
|
2004-05-23 00:02:43 +04:00
|
|
|
aOutColor.Truncate();
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-02-01 14:54:22 +04:00
|
|
|
nsCOMPtr<nsIDOMElement> domElement;
|
2001-01-28 23:13:07 +03:00
|
|
|
PRInt32 selectedCount;
|
|
|
|
nsAutoString tagName;
|
2002-09-13 08:19:30 +04:00
|
|
|
nsresult res = GetSelectedOrParentTableElement(tagName,
|
|
|
|
&selectedCount,
|
2012-02-01 14:54:22 +04:00
|
|
|
getter_AddRefs(domElement));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-06-01 06:38:13 +04:00
|
|
|
|
2012-02-01 14:54:22 +04:00
|
|
|
nsCOMPtr<nsINode> element = do_QueryInterface(domElement);
|
2000-06-01 06:38:13 +04:00
|
|
|
|
2012-02-01 14:54:22 +04:00
|
|
|
while (element) {
|
2001-01-28 23:13:07 +03:00
|
|
|
// We are in a cell or selected table
|
2012-02-01 14:54:22 +04:00
|
|
|
element->AsElement()->GetAttr(kNameSpaceID_None, nsGkAtoms::bgcolor, aOutColor);
|
2000-06-01 06:38:13 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Done if we have a color explicitly set
|
2012-02-01 14:54:22 +04:00
|
|
|
if (!aOutColor.IsEmpty()) {
|
2001-01-28 23:13:07 +03:00
|
|
|
return NS_OK;
|
2012-02-01 14:54:22 +04:00
|
|
|
}
|
2000-06-01 06:38:13 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Once we hit the body, we're done
|
2012-02-01 14:54:22 +04:00
|
|
|
if (element->AsElement()->IsHTML(nsGkAtoms::body)) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-06-01 06:38:13 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// No color is set, but we need to report visible color inherited
|
|
|
|
// from nested cells/tables, so search up parent chain
|
2012-02-01 14:54:22 +04:00
|
|
|
element = element->GetElementParent();
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2000-06-01 06:38:13 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// If no table or cell found, get page body
|
2012-02-01 14:54:22 +04:00
|
|
|
dom::Element* bodyElement = GetRoot();
|
2011-12-04 01:50:15 +04:00
|
|
|
NS_ENSURE_TRUE(bodyElement, NS_ERROR_NULL_POINTER);
|
2000-06-01 06:38:13 +04:00
|
|
|
|
2012-02-01 14:54:22 +04:00
|
|
|
bodyElement->GetAttr(kNameSpaceID_None, nsGkAtoms::bgcolor, aOutColor);
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2000-06-01 06:38:13 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetListState(bool *aMixed, bool *aOL, bool *aUL, bool *aDL)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aMixed && aOL && aUL && aDL, NS_ERROR_NULL_POINTER);
|
2011-05-07 12:04:56 +04:00
|
|
|
nsHTMLEditRules* htmlRules = static_cast<nsHTMLEditRules*>(mRules.get());
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(htmlRules, NS_ERROR_FAILURE);
|
2000-06-01 06:38:13 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
return htmlRules->GetListState(aMixed, aOL, aUL, aDL);
|
|
|
|
}
|
|
|
|
|
2000-05-04 18:02:03 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetListItemState(bool *aMixed, bool *aLI, bool *aDT, bool *aDD)
|
2000-05-04 18:02:03 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aMixed && aLI && aDT && aDD, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2011-05-07 12:04:56 +04:00
|
|
|
nsHTMLEditRules* htmlRules = static_cast<nsHTMLEditRules*>(mRules.get());
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(htmlRules, NS_ERROR_FAILURE);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
return htmlRules->GetListItemState(aMixed, aLI, aDT, aDD);
|
2000-05-04 18:02:03 +04:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetAlignment(bool *aMixed, nsIHTMLEditor::EAlignment *aAlign)
|
2000-04-04 18:51:26 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aMixed && aAlign, NS_ERROR_NULL_POINTER);
|
2011-05-07 12:04:56 +04:00
|
|
|
nsHTMLEditRules* htmlRules = static_cast<nsHTMLEditRules*>(mRules.get());
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(htmlRules, NS_ERROR_FAILURE);
|
2000-04-04 18:51:26 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
return htmlRules->GetAlignment(aMixed, aAlign);
|
|
|
|
}
|
2000-04-04 18:51:26 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetIndentState(bool *aCanIndent, bool *aCanOutdent)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aCanIndent && aCanOutdent, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2011-05-07 12:04:56 +04:00
|
|
|
nsHTMLEditRules* htmlRules = static_cast<nsHTMLEditRules*>(mRules.get());
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(htmlRules, NS_ERROR_FAILURE);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
return htmlRules->GetIndentState(aCanIndent, aCanOutdent);
|
2000-04-04 18:51:26 +04:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::MakeOrChangeList(const nsAString& aListType, bool entireList, const nsAString& aBulletType)
|
1999-05-27 01:40:51 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult res;
|
1999-08-09 05:37:50 +04:00
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
1999-05-27 01:40:51 +04:00
|
|
|
|
2010-05-28 05:18:48 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2000-09-14 15:45:01 +04:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
1999-05-27 01:40:51 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpMakeList, nsIEditor::eNext);
|
|
|
|
|
|
|
|
// pre-process
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-05-05 22:52:29 +04:00
|
|
|
nsTextRulesInfo ruleInfo(kOpMakeList);
|
2001-01-28 23:13:07 +03:00
|
|
|
ruleInfo.blockType = &aListType;
|
|
|
|
ruleInfo.entireList = entireList;
|
2001-12-12 06:57:32 +03:00
|
|
|
ruleInfo.bulletType = &aBulletType;
|
2001-01-28 23:13:07 +03:00
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || (NS_FAILED(res))) return res;
|
2000-05-31 04:03:02 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
if (!handled)
|
1999-12-15 02:07:12 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
// Find out if the selection is collapsed:
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isCollapsed;
|
2001-01-28 23:13:07 +03:00
|
|
|
res = selection->GetIsCollapsed(&isCollapsed);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
1999-12-15 02:07:12 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
PRInt32 offset;
|
|
|
|
|
2010-06-11 06:46:51 +04:00
|
|
|
res = GetStartNodeAndOffset(selection, getter_AddRefs(node), &offset);
|
2001-01-28 23:13:07 +03:00
|
|
|
if (!node) res = NS_ERROR_FAILURE;
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
if (isCollapsed)
|
1999-12-15 02:07:12 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
// have to find a place to put the list
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = node;
|
|
|
|
nsCOMPtr<nsIDOMNode> topChild = node;
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
|
2012-05-01 10:34:52 +04:00
|
|
|
nsCOMPtr<nsIAtom> listAtom = do_GetAtom(aListType);
|
|
|
|
while (!CanContainTag(parent, listAtom)) {
|
2001-01-28 23:13:07 +03:00
|
|
|
parent->GetParentNode(getter_AddRefs(tmp));
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(tmp, NS_ERROR_FAILURE);
|
2001-01-28 23:13:07 +03:00
|
|
|
topChild = parent;
|
|
|
|
parent = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent != node)
|
|
|
|
{
|
|
|
|
// we need to split up to the child of parent
|
|
|
|
res = SplitNodeDeep(topChild, node, offset, &offset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// make a list
|
|
|
|
nsCOMPtr<nsIDOMNode> newList;
|
|
|
|
res = CreateNode(aListType, parent, offset, getter_AddRefs(newList));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
// make a list item
|
|
|
|
nsCOMPtr<nsIDOMNode> newItem;
|
2002-01-14 23:31:43 +03:00
|
|
|
res = CreateNode(NS_LITERAL_STRING("li"), newList, 0, getter_AddRefs(newItem));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = selection->Collapse(newItem,0);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
1999-12-15 02:07:12 +03:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::RemoveList(const nsAString& aListType)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
nsresult res;
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
2010-05-28 05:18:48 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
1999-12-15 02:07:12 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpRemoveList, nsIEditor::eNext);
|
|
|
|
|
1999-08-09 05:37:50 +04:00
|
|
|
// pre-process
|
2001-01-28 23:13:07 +03:00
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
1999-08-19 17:30:48 +04:00
|
|
|
|
2012-05-05 22:52:29 +04:00
|
|
|
nsTextRulesInfo ruleInfo(kOpRemoveList);
|
2004-06-17 04:13:25 +04:00
|
|
|
if (aListType.LowerCaseEqualsLiteral("ol"))
|
2011-10-17 18:59:28 +04:00
|
|
|
ruleInfo.bOrdered = true;
|
|
|
|
else ruleInfo.bOrdered = false;
|
2001-01-28 23:13:07 +03:00
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || (NS_FAILED(res))) return res;
|
1999-08-09 05:37:50 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// no default behavior for this yet. what would it mean?
|
|
|
|
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
1999-05-27 01:40:51 +04:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::MakeDefinitionItem(const nsAString& aItemType)
|
1999-03-01 22:54:47 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult res;
|
1999-03-29 12:02:05 +04:00
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
2010-05-28 05:18:48 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2000-09-14 15:45:01 +04:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
1999-03-29 12:02:05 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpMakeDefListItem, nsIEditor::eNext);
|
|
|
|
|
1999-03-29 12:02:05 +04:00
|
|
|
// pre-process
|
2001-01-28 23:13:07 +03:00
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2012-05-05 22:52:29 +04:00
|
|
|
nsTextRulesInfo ruleInfo(kOpMakeDefListItem);
|
2001-01-28 23:13:07 +03:00
|
|
|
ruleInfo.blockType = &aItemType;
|
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || (NS_FAILED(res))) return res;
|
1999-08-09 05:37:50 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
if (!handled)
|
1999-10-06 23:34:09 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
// todo: no default for now. we count on rules to handle it.
|
1999-08-09 05:37:50 +04:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
2000-10-12 02:50:14 +04:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::InsertBasicBlock(const nsAString& aBlockType)
|
1999-08-09 05:37:50 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03: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 14:57:30 +04:00
|
|
|
|
2010-05-28 05:18:48 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool 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 14:57:30 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpMakeBasicBlock, nsIEditor::eNext);
|
|
|
|
|
|
|
|
// pre-process
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2012-05-05 22:52:29 +04:00
|
|
|
nsTextRulesInfo ruleInfo(kOpMakeBasicBlock);
|
2001-01-28 23:13:07 +03:00
|
|
|
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 14:57:30 +04:00
|
|
|
|
2001-01-28 23:13:07 +03: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 14:57:30 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
// Find out if the selection is collapsed:
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isCollapsed;
|
2001-01-28 23:13:07 +03:00
|
|
|
res = selection->GetIsCollapsed(&isCollapsed);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
PRInt32 offset;
|
|
|
|
|
2010-06-11 06:46:51 +04:00
|
|
|
res = GetStartNodeAndOffset(selection, getter_AddRefs(node), &offset);
|
2001-01-28 23:13:07 +03:00
|
|
|
if (!node) res = NS_ERROR_FAILURE;
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
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 14:57:30 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
// have to find a place to put the block
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = node;
|
|
|
|
nsCOMPtr<nsIDOMNode> topChild = node;
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
|
2012-05-01 10:34:52 +04:00
|
|
|
nsCOMPtr<nsIAtom> blockAtom = do_GetAtom(aBlockType);
|
|
|
|
while (!CanContainTag(parent, blockAtom)) {
|
2001-01-28 23:13:07 +03:00
|
|
|
parent->GetParentNode(getter_AddRefs(tmp));
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(tmp, NS_ERROR_FAILURE);
|
2001-01-28 23:13:07 +03:00
|
|
|
topChild = parent;
|
|
|
|
parent = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent != node)
|
|
|
|
{
|
|
|
|
// we need to split up to the child of parent
|
|
|
|
res = SplitNodeDeep(topChild, node, offset, &offset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// make a block
|
|
|
|
nsCOMPtr<nsIDOMNode> newBlock;
|
|
|
|
res = CreateNode(aBlockType, parent, offset, getter_AddRefs(newBlock));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// reposition selection to inside the block
|
|
|
|
res = selection->Collapse(newBlock,0);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, 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 14:57:30 +04:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
2000-12-07 02:11:21 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::Indent(const nsAString& aIndent)
|
2000-12-07 02:11:21 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03: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 14:57:30 +04:00
|
|
|
|
2010-05-28 05:18:48 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
2012-05-05 22:52:29 +04:00
|
|
|
OperationID opID = kOpIndent;
|
2004-06-17 04:13:25 +04:00
|
|
|
if (aIndent.LowerCaseEqualsLiteral("outdent"))
|
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 14:57:30 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
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 14:57:30 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, opID, nsIEditor::eNext);
|
|
|
|
|
|
|
|
// pre-process
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-05-05 22:52:29 +04:00
|
|
|
nsTextRulesInfo ruleInfo(opID);
|
2001-01-28 23:13:07 +03:00
|
|
|
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 14:57:30 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
// Do default - insert a blockquote node if selection collapsed
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
|
|
|
PRInt32 offset;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isCollapsed;
|
2001-01-28 23:13:07 +03:00
|
|
|
res = selection->GetIsCollapsed(&isCollapsed);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2010-06-11 06:46:51 +04:00
|
|
|
res = GetStartNodeAndOffset(selection, getter_AddRefs(node), &offset);
|
2001-01-28 23:13:07 +03:00
|
|
|
if (!node) res = NS_ERROR_FAILURE;
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2004-05-23 02:15:22 +04:00
|
|
|
if (aIndent.EqualsLiteral("indent"))
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
if (isCollapsed)
|
|
|
|
{
|
|
|
|
// have to find a place to put the blockquote
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = node;
|
|
|
|
nsCOMPtr<nsIDOMNode> topChild = node;
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
2012-05-01 10:34:52 +04:00
|
|
|
while (!CanContainTag(parent, nsGkAtoms::blockquote)) {
|
2001-01-28 23:13:07 +03:00
|
|
|
parent->GetParentNode(getter_AddRefs(tmp));
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(tmp, NS_ERROR_FAILURE);
|
2001-01-28 23:13:07 +03:00
|
|
|
topChild = parent;
|
|
|
|
parent = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (parent != node)
|
|
|
|
{
|
|
|
|
// we need to split up to the child of parent
|
|
|
|
res = SplitNodeDeep(topChild, node, offset, &offset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03: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 14:57:30 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// make a blockquote
|
|
|
|
nsCOMPtr<nsIDOMNode> newBQ;
|
2012-05-01 10:34:52 +04:00
|
|
|
res = CreateNode(NS_LITERAL_STRING("blockquote"), parent, offset,
|
|
|
|
getter_AddRefs(newBQ));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
// put a space in it so layout will draw the list item
|
|
|
|
res = selection->Collapse(newBQ,0);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-04-07 04:45:26 +04:00
|
|
|
res = InsertText(NS_LITERAL_STRING(" "));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
// reposition selection to before the space character
|
2010-06-11 06:46:51 +04:00
|
|
|
res = GetStartNodeAndOffset(selection, getter_AddRefs(node), &offset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = selection->Collapse(node,0);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
2000-12-07 02:11:21 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
//TODO: IMPLEMENT ALIGNMENT!
|
2000-12-07 02:11:21 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::Align(const nsAString& aAlignType)
|
2000-12-07 02:11:21 +03:00
|
|
|
{
|
2010-05-28 05:18:48 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2000-12-07 02:11:21 +03:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoRules beginRulesSniffing(this, kOpAlign, nsIEditor::eNext);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
2000-12-07 02:11:21 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Find out if the selection is collapsed:
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2012-05-05 22:52:29 +04:00
|
|
|
nsTextRulesInfo ruleInfo(kOpAlign);
|
2001-01-28 23:13:07 +03:00
|
|
|
ruleInfo.alignType = &aAlignType;
|
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || NS_FAILED(res))
|
2000-12-07 02:11:21 +03: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 14:57:30 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
|
|
|
}
|
2000-05-06 00:42:36 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::GetElementOrParentByTagName(const nsAString& aTagName, nsIDOMNode *aNode, nsIDOMElement** aReturn)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2003-05-24 01:34:47 +04:00
|
|
|
if (aTagName.IsEmpty() || !aReturn )
|
2001-01-28 23:13:07 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
nsCOMPtr<nsIDOMNode> currentNode;
|
1999-08-09 05:37:50 +04:00
|
|
|
|
2001-01-28 23:13:07 +03: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 14:57:30 +04:00
|
|
|
else
|
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
// If no node supplied, get it from anchor node of current selection
|
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> anchorNode;
|
|
|
|
res = selection->GetAnchorNode(getter_AddRefs(anchorNode));
|
|
|
|
if(NS_FAILED(res)) return res;
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(anchorNode, NS_ERROR_FAILURE);
|
1999-11-29 11:28:46 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Try to get the actual selected node
|
2011-09-29 10:19:26 +04:00
|
|
|
bool hasChildren = false;
|
2001-01-28 23:13:07 +03:00
|
|
|
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 14:57:30 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03: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 14:57:30 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
// anchor node is probably a text node - just use that
|
|
|
|
if (!currentNode)
|
|
|
|
currentNode = anchorNode;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString TagName(aTagName);
|
2001-12-17 10:14:49 +03:00
|
|
|
ToLowerCase(TagName);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool getLink = IsLinkTag(TagName);
|
|
|
|
bool getNamedAnchor = IsNamedAnchorTag(TagName);
|
2001-01-28 23:13:07 +03:00
|
|
|
if ( getLink || getNamedAnchor)
|
|
|
|
{
|
2004-06-17 04:13:25 +04:00
|
|
|
TagName.AssignLiteral("a");
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2011-09-29 10:19:26 +04:00
|
|
|
bool findTableCell = TagName.EqualsLiteral("td");
|
|
|
|
bool findList = TagName.EqualsLiteral("list");
|
2000-10-12 02:50:14 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// default is null - no element found
|
|
|
|
*aReturn = nsnull;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool bNodeFound = false;
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
while (true)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
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 14:57:30 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
bNodeFound = true;
|
2001-01-28 23:13:07 +03:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
if (findList)
|
2000-10-12 02:50:14 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
// Match "ol", "ul", or "dl" for lists
|
|
|
|
if (nsHTMLEditUtils::IsList(currentNode))
|
|
|
|
goto NODE_FOUND;
|
1999-08-09 05:37:50 +04:00
|
|
|
|
2001-01-28 23:13:07 +03: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 14:57:30 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03: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-20 01:49:12 +03: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 14:57:30 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
NODE_FOUND:
|
2011-10-17 18:59:28 +04:00
|
|
|
bNodeFound = true;
|
2001-01-28 23:13:07 +03:00
|
|
|
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 14:57:30 +04:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 23:13:07 +03: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 09:45:00 +03:00
|
|
|
if (NS_FAILED(currentNode->GetParentNode(getter_AddRefs(parent))) || !parent)
|
2001-01-28 23:13:07 +03:00
|
|
|
break;
|
1999-08-09 05:37:50 +04:00
|
|
|
|
2001-01-28 23:13:07 +03: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
|
2004-06-17 04:13:25 +04:00
|
|
|
if(parentTagName.LowerCaseEqualsLiteral("body"))
|
2001-01-28 23:13:07 +03:00
|
|
|
break;
|
1999-09-02 21:56:09 +04:00
|
|
|
|
2001-01-28 23:13:07 +03: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 14:57:30 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03: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 14:57:30 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMElement> currentElement = do_QueryInterface(currentNode);
|
|
|
|
if (currentElement)
|
1999-11-29 11:28:46 +03:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
*aReturn = currentElement;
|
|
|
|
// Getters must addref
|
|
|
|
NS_ADDREF(*aReturn);
|
1999-11-29 11:28:46 +03:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
else res = NS_EDITOR_ELEMENT_NOT_FOUND;
|
|
|
|
|
1999-08-09 05:37:50 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2000-08-10 07:43:09 +04:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::GetSelectedElement(const nsAString& aTagName, nsIDOMElement** aReturn)
|
2000-08-10 07:43:09 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(aReturn , NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// default is null - no element found
|
|
|
|
*aReturn = nsnull;
|
|
|
|
|
|
|
|
// First look for a single element in selection
|
|
|
|
nsCOMPtr<nsISelection>selection;
|
2000-08-30 06:44:08 +04:00
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
|
2000-08-30 06:44:08 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool bNodeFound = false;
|
2001-01-28 23:13:07 +03:00
|
|
|
res=NS_ERROR_NOT_INITIALIZED;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isCollapsed;
|
2001-01-28 23:13:07 +03:00
|
|
|
selection->GetIsCollapsed(&isCollapsed);
|
2000-08-30 06:44:08 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoString domTagName;
|
|
|
|
nsAutoString TagName(aTagName);
|
2001-12-17 10:14:49 +03:00
|
|
|
ToLowerCase(TagName);
|
2001-01-28 23:13:07 +03:00
|
|
|
// Empty string indicates we should match any element tag
|
2011-09-29 10:19:26 +04:00
|
|
|
bool anyTag = (TagName.IsEmpty());
|
|
|
|
bool isLinkTag = IsLinkTag(TagName);
|
|
|
|
bool isNamedAnchorTag = IsNamedAnchorTag(TagName);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> selectedElement;
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
res = selection->GetRangeAt(0, getter_AddRefs(range));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-08-30 06:44:08 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> startParent;
|
|
|
|
PRInt32 startOffset, endOffset;
|
|
|
|
res = range->GetStartContainer(getter_AddRefs(startParent));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = range->GetStartOffset(&startOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-08-31 02:49:08 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> endParent;
|
|
|
|
res = range->GetEndContainer(getter_AddRefs(endParent));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
res = range->GetEndOffset(&endOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-08-31 02:49:08 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Optimization for a single selected element
|
|
|
|
if (startParent && startParent == endParent && (endOffset-startOffset) == 1)
|
2000-08-30 06:44:08 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> selectedNode = GetChildAt(startParent, startOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, NS_OK);
|
2001-01-28 23:13:07 +03:00
|
|
|
if (selectedNode)
|
1999-05-05 08:05:19 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
selectedNode->GetNodeName(domTagName);
|
2001-12-17 10:14:49 +03:00
|
|
|
ToLowerCase(domTagName);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// Test for appropriate node type requested
|
|
|
|
if (anyTag || (TagName == domTagName) ||
|
|
|
|
(isLinkTag && nsHTMLEditUtils::IsLink(selectedNode)) ||
|
|
|
|
(isNamedAnchorTag && nsHTMLEditUtils::IsNamedAnchor(selectedNode)))
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
bNodeFound = true;
|
2001-01-28 23:13:07 +03:00
|
|
|
selectedElement = do_QueryInterface(selectedNode);
|
|
|
|
}
|
2000-04-25 18:15:04 +04:00
|
|
|
}
|
|
|
|
}
|
1999-05-05 08:05:19 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
if (!bNodeFound)
|
2000-04-25 18:15:04 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
if (isLinkTag)
|
2000-04-25 18:15:04 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03: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));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
PRInt32 anchorOffset = -1;
|
|
|
|
if (anchorNode)
|
|
|
|
selection->GetAnchorOffset(&anchorOffset);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> focusNode;
|
|
|
|
res = selection->GetFocusNode(getter_AddRefs(focusNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 23:13:07 +03:00
|
|
|
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 14:57:30 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Link node must be the same for both ends of selection
|
|
|
|
if (NS_SUCCEEDED(res) && anchorNode)
|
2000-04-25 18:15:04 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
#ifdef DEBUG_cmanske
|
|
|
|
{
|
|
|
|
nsAutoString name;
|
|
|
|
anchorNode->GetNodeName(name);
|
|
|
|
printf("GetSelectedElement: Anchor node of selection: ");
|
2001-06-30 15:02:25 +04:00
|
|
|
wprintf(name.get());
|
2001-01-28 23:13:07 +03:00
|
|
|
printf(" Offset: %d\n", anchorOffset);
|
|
|
|
focusNode->GetNodeName(name);
|
|
|
|
printf("Focus node of selection: ");
|
2001-06-30 15:02:25 +04:00
|
|
|
wprintf(name.get());
|
2001-01-28 23:13:07 +03:00
|
|
|
printf(" Offset: %d\n", focusOffset);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
nsCOMPtr<nsIDOMElement> parentLinkOfAnchor;
|
2001-11-14 04:33:42 +03:00
|
|
|
res = GetElementOrParentByTagName(NS_LITERAL_STRING("href"), anchorNode, getter_AddRefs(parentLinkOfAnchor));
|
2001-01-28 23:13:07 +03:00
|
|
|
// XXX: ERROR_HANDLING can parentLinkOfAnchor be null?
|
|
|
|
if (NS_SUCCEEDED(res) && parentLinkOfAnchor)
|
|
|
|
{
|
|
|
|
if (isCollapsed)
|
|
|
|
{
|
|
|
|
// We have just a caret in the link
|
2011-10-17 18:59:28 +04:00
|
|
|
bNodeFound = true;
|
2001-01-28 23:13:07 +03:00
|
|
|
} else if(focusNode)
|
|
|
|
{ // Link node must be the same for both ends of selection
|
|
|
|
nsCOMPtr<nsIDOMElement> parentLinkOfFocus;
|
2001-11-14 04:33:42 +03:00
|
|
|
res = GetElementOrParentByTagName(NS_LITERAL_STRING("href"), focusNode, getter_AddRefs(parentLinkOfFocus));
|
2001-01-28 23:13:07 +03:00
|
|
|
if (NS_SUCCEEDED(res) && parentLinkOfFocus == parentLinkOfAnchor)
|
2011-10-17 18:59:28 +04:00
|
|
|
bNodeFound = true;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
2011-10-17 18:59:28 +04:00
|
|
|
bNodeFound = true;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
1999-05-05 08:05:19 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isCollapsed) // Don't bother to examine selection if it is collapsed
|
2000-05-12 18:57:03 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIEnumerator> enumerator;
|
|
|
|
res = selPriv->GetEnumerator(getter_AddRefs(enumerator));
|
|
|
|
if (NS_SUCCEEDED(res))
|
2000-05-12 18:57:03 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03: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 18:57:03 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMRange> currange( do_QueryInterface(currentItem) );
|
2003-07-09 00:22:56 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
|
|
|
iter->Init(currange);
|
|
|
|
// loop through the content iterator for each content node
|
|
|
|
while (!iter->IsDone())
|
2000-05-12 18:57:03 +04:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
// 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(iter->GetCurrentNode());
|
|
|
|
if (selectedElement)
|
2000-05-12 18:57:03 +04:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
// If we already found a node, then we have another element,
|
|
|
|
// thus there's not just one element selected
|
|
|
|
if (bNodeFound)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
bNodeFound = false;
|
2004-01-24 03:46:17 +03:00
|
|
|
break;
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
selectedElement->GetNodeName(domTagName);
|
|
|
|
ToLowerCase(domTagName);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
if (anyTag)
|
|
|
|
{
|
|
|
|
// Get name of first selected element
|
|
|
|
selectedElement->GetTagName(TagName);
|
|
|
|
ToLowerCase(TagName);
|
2011-10-17 18:59:28 +04:00
|
|
|
anyTag = false;
|
2004-01-24 03:46:17 +03:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
// 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)) )
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
bNodeFound = true;
|
2004-01-24 03:46:17 +03:00
|
|
|
} else if (TagName == domTagName) { // All other tag names are handled here
|
2011-10-17 18:59:28 +04:00
|
|
|
bNodeFound = true;
|
2004-01-24 03:46:17 +03:00
|
|
|
}
|
|
|
|
if (!bNodeFound)
|
|
|
|
{
|
|
|
|
// Check if node we have is really part of the selection???
|
|
|
|
break;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2000-05-12 18:57:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
iter->Next();
|
2000-05-12 18:57:03 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
} else {
|
|
|
|
// Should never get here?
|
2011-10-17 18:59:28 +04:00
|
|
|
isCollapsed = true;
|
2001-01-28 23:13:07 +03:00
|
|
|
printf("isCollapsed was FALSE, but no elements found in selection\n");
|
2000-05-12 18:57:03 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
} else {
|
|
|
|
printf("Could not create enumerator for GetSelectionProperties\n");
|
2000-05-12 18:57:03 +04:00
|
|
|
}
|
|
|
|
}
|
1999-04-20 21:47:12 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
if (bNodeFound)
|
2000-03-28 04:34:26 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
*aReturn = selectedElement;
|
|
|
|
if (selectedElement)
|
|
|
|
{
|
|
|
|
// Getters must addref
|
|
|
|
NS_ADDREF(*aReturn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else res = NS_EDITOR_ELEMENT_NOT_FOUND;
|
2000-03-28 04:34:26 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
return res;
|
1999-04-20 21:47:12 +04:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::CreateElementWithDefaults(const nsAString& aTagName, nsIDOMElement** aReturn)
|
2000-04-25 18:15:04 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult res=NS_ERROR_NOT_INITIALIZED;
|
|
|
|
if (aReturn)
|
|
|
|
*aReturn = nsnull;
|
2000-06-08 18:47:29 +04:00
|
|
|
|
2010-06-18 00:40:48 +04:00
|
|
|
// NS_ENSURE_TRUE(aTagName && aReturn, NS_ERROR_NULL_POINTER);
|
|
|
|
NS_ENSURE_TRUE(!aTagName.IsEmpty() && aReturn, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
nsAutoString TagName(aTagName);
|
2001-12-17 10:14:49 +03:00
|
|
|
ToLowerCase(TagName);
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoString realTagName;
|
2000-09-06 03:26:48 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
if (IsLinkTag(TagName) || IsNamedAnchorTag(TagName))
|
|
|
|
{
|
2004-06-17 04:13:25 +04:00
|
|
|
realTagName.AssignLiteral("a");
|
2001-01-28 23:13:07 +03:00
|
|
|
} else {
|
|
|
|
realTagName = TagName;
|
|
|
|
}
|
|
|
|
//We don't use editor's CreateElement because we don't want to
|
|
|
|
// go through the transaction system
|
2000-09-06 03:26:48 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMElement>newElement;
|
|
|
|
nsCOMPtr<nsIContent> newContent;
|
|
|
|
nsCOMPtr<nsIDOMDocument> doc = do_QueryReferent(mDocWeak);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED);
|
2000-09-06 03:26:48 +04:00
|
|
|
|
2001-01-28 23:13:07 +03: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-06 03:26:48 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Mark the new element dirty, so it will be formatted
|
2004-05-23 00:02:43 +04:00
|
|
|
newElement->SetAttribute(NS_LITERAL_STRING("_moz_dirty"), EmptyString());
|
2000-09-06 03:26:48 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Set default values for new elements
|
2012-04-04 14:47:36 +04:00
|
|
|
if (TagName.EqualsLiteral("table")) {
|
2002-09-20 12:53:52 +04:00
|
|
|
res = newElement->SetAttribute(NS_LITERAL_STRING("cellpadding"),NS_LITERAL_STRING("2"));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-09-20 12:53:52 +04:00
|
|
|
res = newElement->SetAttribute(NS_LITERAL_STRING("cellspacing"),NS_LITERAL_STRING("2"));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-09-20 12:53:52 +04:00
|
|
|
res = newElement->SetAttribute(NS_LITERAL_STRING("border"),NS_LITERAL_STRING("1"));
|
2004-05-23 02:15:22 +04:00
|
|
|
} else if (TagName.EqualsLiteral("td"))
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2002-09-20 12:53:52 +04:00
|
|
|
res = SetAttributeOrEquivalent(newElement, NS_LITERAL_STRING("valign"),
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_LITERAL_STRING("top"), true);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
// ADD OTHER TAGS HERE
|
2000-09-06 03:26:48 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
*aReturn = newElement;
|
|
|
|
// Getters must addref
|
|
|
|
NS_ADDREF(*aReturn);
|
2000-04-25 18:15:04 +04:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
return res;
|
2000-04-25 18:15:04 +04:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::InsertLinkAroundSelection(nsIDOMElement* aAnchorElement)
|
2000-06-08 18:47:29 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult res=NS_ERROR_NULL_POINTER;
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aAnchorElement, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
|
|
|
|
// We must have a real selection
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (!selection)
|
2000-09-06 03:26:48 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
res = NS_ERROR_NULL_POINTER;
|
2000-09-06 03:26:48 +04:00
|
|
|
}
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isCollapsed;
|
2000-06-08 18:47:29 +04:00
|
|
|
res = selection->GetIsCollapsed(&isCollapsed);
|
2001-01-28 23:13:07 +03:00
|
|
|
if (NS_FAILED(res))
|
2011-10-17 18:59:28 +04:00
|
|
|
isCollapsed = true;
|
2000-06-08 18:47:29 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
if (isCollapsed)
|
2000-06-08 18:47:29 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03: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 18:47:29 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
nsAutoString href;
|
|
|
|
res = anchor->GetHref(href);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-30 15:02:25 +04:00
|
|
|
if (!href.IsEmpty())
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
2001-10-31 02:24:44 +03:00
|
|
|
|
|
|
|
// Set all attributes found on the supplied anchor element
|
|
|
|
nsCOMPtr<nsIDOMNamedNodeMap> attrMap;
|
|
|
|
aAnchorElement->GetAttributes(getter_AddRefs(attrMap));
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(attrMap, NS_ERROR_FAILURE);
|
2001-10-31 02:24:44 +03:00
|
|
|
|
|
|
|
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));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-10-31 02:24:44 +03:00
|
|
|
|
|
|
|
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-09 00:22:56 +04:00
|
|
|
name.Truncate();
|
|
|
|
value.Truncate();
|
2001-10-31 02:24:44 +03:00
|
|
|
|
|
|
|
res = attribute->GetName(name);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-10-31 02:24:44 +03:00
|
|
|
|
|
|
|
res = attribute->GetValue(value);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-10-31 02:24:44 +03:00
|
|
|
|
2003-04-17 09:42:33 +04:00
|
|
|
res = SetInlineProperty(nsEditProperty::a, name, value);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-10-31 02:24:44 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2000-06-08 18:47:29 +04:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
return res;
|
2000-06-08 18:47:29 +04:00
|
|
|
}
|
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::SetHTMLBackgroundColor(const nsAString& aColor)
|
2000-06-08 18:47:29 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03: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 08:19:30 +04:00
|
|
|
nsresult res = GetSelectedOrParentTableElement(tagName, &selectedCount,
|
|
|
|
getter_AddRefs(element));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-06-08 18:47:29 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool setColor = !aColor.IsEmpty();
|
2000-06-08 18:47:29 +04:00
|
|
|
|
2002-01-14 23:31:43 +03:00
|
|
|
NS_NAMED_LITERAL_STRING(bgcolor, "bgcolor");
|
2001-01-28 23:13:07 +03:00
|
|
|
if (element)
|
2000-06-08 18:47:29 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
if (selectedCount > 0)
|
2000-06-08 18:47:29 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
// Traverse all selected cells
|
|
|
|
nsCOMPtr<nsIDOMElement> cell;
|
2002-09-13 08:19:30 +04:00
|
|
|
res = GetFirstSelectedCell(nsnull, getter_AddRefs(cell));
|
2001-01-28 23:13:07 +03:00
|
|
|
if (NS_SUCCEEDED(res) && cell)
|
|
|
|
{
|
|
|
|
while(cell)
|
|
|
|
{
|
|
|
|
if (setColor)
|
2002-01-14 23:31:43 +03:00
|
|
|
res = SetAttribute(cell, bgcolor, aColor);
|
2001-01-28 23:13:07 +03:00
|
|
|
else
|
2002-01-14 23:31:43 +03:00
|
|
|
res = RemoveAttribute(cell, bgcolor);
|
2001-01-28 23:13:07 +03:00
|
|
|
if (NS_FAILED(res)) break;
|
2000-06-08 18:47:29 +04:00
|
|
|
|
2002-09-13 08:19:30 +04:00
|
|
|
GetNextSelectedCell(nsnull, getter_AddRefs(cell));
|
2001-01-28 23:13:07 +03:00
|
|
|
};
|
|
|
|
return res;
|
|
|
|
}
|
2000-06-08 18:47:29 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03: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
|
2011-12-04 01:50:15 +04:00
|
|
|
element = do_QueryInterface(GetRoot());
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_NULL_POINTER);
|
2000-06-08 18:47:29 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
// Use the editor method that goes through the transaction system
|
|
|
|
if (setColor)
|
2002-01-14 23:31:43 +03:00
|
|
|
res = SetAttribute(element, bgcolor, aColor);
|
2001-01-28 23:13:07 +03:00
|
|
|
else
|
2002-01-14 23:31:43 +03:00
|
|
|
res = RemoveAttribute(element, bgcolor);
|
2000-06-08 18:47:29 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
return res;
|
2000-06-08 18:47:29 +04:00
|
|
|
}
|
|
|
|
|
2002-03-24 01:08:20 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::SetBodyAttribute(const nsAString& aAttribute, const nsAString& aValue)
|
2000-04-25 18:15:04 +04:00
|
|
|
{
|
2001-01-28 23:13:07 +03:00
|
|
|
// TODO: Check selection for Cell, Row, Column or table and do color on appropriate level
|
2000-04-25 18:15:04 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_ASSERTION(mDocWeak, "Missing Editor DOM Document");
|
|
|
|
|
|
|
|
// Set the background color attribute on the body tag
|
2011-12-04 01:50:15 +04:00
|
|
|
nsCOMPtr<nsIDOMElement> bodyElement = do_QueryInterface(GetRoot());
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(bodyElement, NS_ERROR_NULL_POINTER);
|
2005-03-24 22:00:01 +03:00
|
|
|
|
|
|
|
// Use the editor method that goes through the transaction system
|
|
|
|
return SetAttribute(bodyElement, aAttribute, aValue);
|
2000-04-25 18:15:04 +04:00
|
|
|
}
|
|
|
|
|
2001-08-08 01:39:07 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetLinkedObjects(nsISupportsArray** aNodeList)
|
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(aNodeList, NS_ERROR_NULL_POINTER);
|
2001-08-08 01:39:07 +04:00
|
|
|
|
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
res = NS_NewISupportsArray(aNodeList);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(*aNodeList, NS_ERROR_NULL_POINTER);
|
2001-08-08 01:39:07 +04:00
|
|
|
|
2003-07-09 00:22:56 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(iter, NS_ERROR_NULL_POINTER);
|
2001-08-08 01:39:07 +04:00
|
|
|
if ((NS_SUCCEEDED(res)))
|
|
|
|
{
|
2012-03-24 12:19:14 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc = GetDocument();
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
|
2001-08-08 01:39:07 +04:00
|
|
|
|
2010-04-30 17:12:05 +04:00
|
|
|
iter->Init(doc->GetRootElement());
|
2001-08-08 01:39:07 +04:00
|
|
|
|
|
|
|
// loop through the content iterator for each content node
|
2004-01-24 03:46:17 +03:00
|
|
|
while (!iter->IsDone())
|
2001-08-08 01:39:07 +04:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> node (do_QueryInterface(iter->GetCurrentNode()));
|
2001-08-08 01:39:07 +04:00
|
|
|
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));
|
2004-01-24 03:46:17 +03:00
|
|
|
|
|
|
|
(*aNodeList)->AppendElement(isupp);
|
2001-08-08 01:39:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
iter->Next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-04-25 18:15:04 +04:00
|
|
|
|
2002-09-13 23:47:38 +04: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-19 00:50:15 +03:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
2002-09-13 23:47:38 +04:00
|
|
|
nsHTMLEditor::ReplaceStyleSheet(const nsAString& aURL)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2002-09-13 23:47:38 +04:00
|
|
|
// Enable existing sheet if already loaded.
|
|
|
|
if (EnableExistingStyleSheet(aURL))
|
2000-01-19 00:50:15 +03:00
|
|
|
{
|
2002-09-13 23:47:38 +04:00
|
|
|
// Disable last sheet if not the same as new one
|
2009-06-27 09:31:36 +04:00
|
|
|
if (!mLastStyleSheetURL.IsEmpty() && !mLastStyleSheetURL.Equals(aURL))
|
2011-10-17 18:59:28 +04:00
|
|
|
return EnableStyleSheet(mLastStyleSheetURL, false);
|
2002-09-13 23:47:38 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2000-01-19 00:50:15 +03:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2010-03-03 00:00:53 +03:00
|
|
|
// Make sure the pres shell doesn't disappear during the load.
|
2011-03-11 08:40:30 +03:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, NS_ERROR_NOT_INITIALIZED);
|
2011-05-22 16:43:13 +04:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
|
2002-09-13 23:47:38 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uaURI;
|
2010-03-03 00:00:53 +03:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uaURI), aURL);
|
2002-09-13 23:47:38 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-03-03 00:00:53 +03:00
|
|
|
return ps->GetDocument()->CSSLoader()->
|
|
|
|
LoadSheet(uaURI, nsnull, EmptyCString(), this);
|
2002-09-13 23:47:38 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-09-13 23:47:38 +04:00
|
|
|
nsHTMLEditor::RemoveStyleSheet(const nsAString &aURL)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2010-05-12 00:41:47 +04:00
|
|
|
nsRefPtr<nsCSSStyleSheet> sheet;
|
2002-09-13 23:47:38 +04:00
|
|
|
nsresult rv = GetStyleSheetForURL(aURL, getter_AddRefs(sheet));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(sheet, NS_ERROR_UNEXPECTED);
|
2002-09-13 23:47:38 +04:00
|
|
|
|
2007-07-02 18:01:27 +04:00
|
|
|
nsRefPtr<RemoveStyleSheetTxn> txn;
|
|
|
|
rv = CreateTxnForRemoveStyleSheet(sheet, getter_AddRefs(txn));
|
2001-01-28 23:13:07 +03:00
|
|
|
if (!txn) rv = NS_ERROR_NULL_POINTER;
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2000-01-19 00:50:15 +03:00
|
|
|
{
|
2003-04-05 00:50:25 +04:00
|
|
|
rv = DoTransaction(txn);
|
2001-01-28 23:13:07 +03:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-09-13 23:47:38 +04:00
|
|
|
mLastStyleSheetURL.Truncate(); // forget it
|
|
|
|
|
|
|
|
// Remove it from our internal list
|
|
|
|
rv = RemoveStyleSheetFromList(aURL);
|
2000-01-19 00:50:15 +03:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
return rv;
|
1999-11-11 02:42:11 +03:00
|
|
|
}
|
|
|
|
|
2002-09-13 23:47:38 +04:00
|
|
|
|
2010-03-03 00:00:53 +03:00
|
|
|
NS_IMETHODIMP
|
2002-09-13 23:47:38 +04:00
|
|
|
nsHTMLEditor::AddOverrideStyleSheet(const nsAString& aURL)
|
|
|
|
{
|
|
|
|
// Enable existing sheet if already loaded.
|
|
|
|
if (EnableExistingStyleSheet(aURL))
|
|
|
|
return NS_OK;
|
|
|
|
|
2010-03-03 00:00:53 +03:00
|
|
|
// Make sure the pres shell doesn't disappear during the load.
|
2011-05-22 16:43:13 +04:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
|
2002-09-13 23:47:38 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uaURI;
|
2010-03-03 00:00:53 +03:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uaURI), aURL);
|
2002-09-13 23:47:38 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2002-12-03 08:48:14 +03:00
|
|
|
// We MUST ONLY load synchronous local files (no @import)
|
2005-09-12 22:41:15 +04:00
|
|
|
// XXXbz Except this will actually try to load remote files
|
|
|
|
// synchronously, of course..
|
2010-05-12 00:41:47 +04:00
|
|
|
nsRefPtr<nsCSSStyleSheet> sheet;
|
2006-06-15 07:20:19 +04:00
|
|
|
// Editor override style sheets may want to style Gecko anonymous boxes
|
2010-03-03 00:00:53 +03:00
|
|
|
rv = ps->GetDocument()->CSSLoader()->
|
2011-10-17 18:59:28 +04:00
|
|
|
LoadSheetSync(uaURI, true, true, getter_AddRefs(sheet));
|
2002-09-13 23:47:38 +04:00
|
|
|
|
|
|
|
// Synchronous loads should ALWAYS return completed
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(sheet, NS_ERROR_NULL_POINTER);
|
2002-09-13 23:47:38 +04:00
|
|
|
|
|
|
|
// Add the override style sheet
|
|
|
|
// (This checks if already exists)
|
2004-01-08 01:30:53 +03:00
|
|
|
ps->AddOverrideStyleSheet(sheet);
|
2002-09-13 23:47:38 +04:00
|
|
|
|
2004-01-08 01:30:53 +03:00
|
|
|
ps->ReconstructStyleData();
|
2002-09-13 23:47:38 +04: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))
|
2011-10-17 18:59:28 +04:00
|
|
|
return EnableStyleSheet(mLastOverrideStyleSheetURL, false);
|
2002-09-13 23:47:38 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// Remove the previous sheet
|
|
|
|
if (!mLastOverrideStyleSheetURL.IsEmpty())
|
|
|
|
RemoveOverrideStyleSheet(mLastOverrideStyleSheetURL);
|
|
|
|
|
|
|
|
return AddOverrideStyleSheet(aURL);
|
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Do NOT use transaction system for override style sheets
|
|
|
|
NS_IMETHODIMP
|
2002-09-13 23:47:38 +04:00
|
|
|
nsHTMLEditor::RemoveOverrideStyleSheet(const nsAString &aURL)
|
1999-11-11 02:42:11 +03:00
|
|
|
{
|
2010-05-12 00:41:47 +04:00
|
|
|
nsRefPtr<nsCSSStyleSheet> sheet;
|
2005-06-10 02:50:41 +04:00
|
|
|
GetStyleSheetForURL(aURL, getter_AddRefs(sheet));
|
|
|
|
|
|
|
|
// Make sure we remove the stylesheet from our internal list in all
|
|
|
|
// cases.
|
|
|
|
nsresult rv = RemoveStyleSheetFromList(aURL);
|
|
|
|
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(sheet, NS_OK); /// Don't fail if sheet not found
|
2002-09-13 23:47:38 +04:00
|
|
|
|
2011-03-11 08:40:30 +03:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, NS_ERROR_NOT_INITIALIZED);
|
2011-05-22 16:43:13 +04:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
|
1999-11-11 02:42:11 +03:00
|
|
|
|
2004-01-08 01:30:53 +03:00
|
|
|
ps->RemoveOverrideStyleSheet(sheet);
|
|
|
|
ps->ReconstructStyleData();
|
1999-11-11 02:42:11 +03:00
|
|
|
|
2002-09-13 23:47:38 +04:00
|
|
|
// Remove it from our internal list
|
2005-06-10 02:50:41 +04:00
|
|
|
return rv;
|
1999-11-11 02:42:11 +03:00
|
|
|
}
|
|
|
|
|
2002-09-13 23:47:38 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::EnableStyleSheet(const nsAString &aURL, bool aEnable)
|
1999-11-11 02:42:11 +03:00
|
|
|
{
|
2010-05-12 00:41:47 +04:00
|
|
|
nsRefPtr<nsCSSStyleSheet> sheet;
|
2002-09-13 23:47:38 +04:00
|
|
|
nsresult rv = GetStyleSheetForURL(aURL, getter_AddRefs(sheet));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(sheet, NS_OK); // Don't fail if sheet not found
|
2002-09-13 23:47:38 +04:00
|
|
|
|
2009-02-19 10:52:54 +03:00
|
|
|
// Ensure the style sheet is owned by our document.
|
2009-09-22 03:05:26 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
2010-05-13 00:18:47 +04:00
|
|
|
sheet->SetOwningDocument(doc);
|
|
|
|
|
2010-05-12 00:41:47 +04:00
|
|
|
return sheet->SetDisabled(!aEnable);
|
1999-11-11 02:42:11 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2002-09-13 23:47:38 +04:00
|
|
|
nsHTMLEditor::EnableExistingStyleSheet(const nsAString &aURL)
|
|
|
|
{
|
2010-05-12 00:41:47 +04:00
|
|
|
nsRefPtr<nsCSSStyleSheet> sheet;
|
2002-09-13 23:47:38 +04:00
|
|
|
nsresult rv = GetStyleSheetForURL(aURL, getter_AddRefs(sheet));
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2002-09-13 23:47:38 +04:00
|
|
|
|
|
|
|
// Enable sheet if already loaded.
|
|
|
|
if (sheet)
|
|
|
|
{
|
2009-02-19 10:52:54 +03:00
|
|
|
// Ensure the style sheet is owned by our document.
|
2009-09-22 03:05:26 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
2010-05-13 00:18:47 +04:00
|
|
|
sheet->SetOwningDocument(doc);
|
2009-02-19 10:52:54 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
sheet->SetDisabled(false);
|
|
|
|
return true;
|
2002-09-13 23:47:38 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-09-13 23:47:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::AddNewStyleSheetToList(const nsAString &aURL,
|
2010-05-12 00:41:47 +04:00
|
|
|
nsCSSStyleSheet *aStyleSheet)
|
1999-11-11 02:42:11 +03:00
|
|
|
{
|
2010-05-12 00:41:47 +04:00
|
|
|
PRUint32 countSS = mStyleSheets.Length();
|
2009-01-18 23:14:14 +03:00
|
|
|
PRUint32 countU = mStyleSheetURLs.Length();
|
1999-11-11 02:42:11 +03:00
|
|
|
|
2010-10-13 02:30:41 +04:00
|
|
|
if (countSS != countU)
|
2002-09-13 23:47:38 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2000-08-07 10:05:27 +04:00
|
|
|
|
2009-01-18 23:14:14 +03:00
|
|
|
if (!mStyleSheetURLs.AppendElement(aURL))
|
2002-09-13 23:47:38 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2010-05-12 00:41:47 +04:00
|
|
|
return mStyleSheets.AppendElement(aStyleSheet) ? NS_OK : NS_ERROR_UNEXPECTED;
|
2002-09-13 23:47:38 +04:00
|
|
|
}
|
2000-07-13 03:44:56 +04:00
|
|
|
|
2002-09-13 23:47:38 +04:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::RemoveStyleSheetFromList(const nsAString &aURL)
|
|
|
|
{
|
|
|
|
// is it already in the list?
|
2009-02-10 23:39:43 +03:00
|
|
|
PRUint32 foundIndex;
|
2002-09-13 23:47:38 +04:00
|
|
|
foundIndex = mStyleSheetURLs.IndexOf(aURL);
|
2009-02-10 23:39:43 +03:00
|
|
|
if (foundIndex == mStyleSheetURLs.NoIndex)
|
2002-09-13 23:47:38 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Attempt both removals; if one fails there's not much we can do.
|
2010-05-12 00:41:47 +04:00
|
|
|
mStyleSheets.RemoveElementAt(foundIndex);
|
2009-01-18 23:14:14 +03:00
|
|
|
mStyleSheetURLs.RemoveElementAt(foundIndex);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2010-05-12 00:41:47 +04:00
|
|
|
return NS_OK;
|
1999-11-11 02:42:11 +03:00
|
|
|
}
|
|
|
|
|
2002-09-13 23:47:38 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetStyleSheetForURL(const nsAString &aURL,
|
2010-05-12 00:41:47 +04:00
|
|
|
nsCSSStyleSheet **aStyleSheet)
|
2002-09-13 23:47:38 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aStyleSheet);
|
|
|
|
*aStyleSheet = 0;
|
|
|
|
|
|
|
|
// is it already in the list?
|
2009-02-10 23:39:43 +03:00
|
|
|
PRUint32 foundIndex;
|
2002-09-13 23:47:38 +04:00
|
|
|
foundIndex = mStyleSheetURLs.IndexOf(aURL);
|
2009-02-10 23:39:43 +03:00
|
|
|
if (foundIndex == mStyleSheetURLs.NoIndex)
|
2002-09-13 23:47:38 +04:00
|
|
|
return NS_OK; //No sheet -- don't fail!
|
|
|
|
|
2002-11-12 22:40:11 +03:00
|
|
|
*aStyleSheet = mStyleSheets[foundIndex];
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(*aStyleSheet, NS_ERROR_FAILURE);
|
2002-09-13 23:47:38 +04:00
|
|
|
|
|
|
|
NS_ADDREF(*aStyleSheet);
|
2002-11-12 22:40:11 +03:00
|
|
|
|
2002-09-13 23:47:38 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-05-12 00:41:47 +04:00
|
|
|
nsHTMLEditor::GetURLForStyleSheet(nsCSSStyleSheet *aStyleSheet,
|
2002-09-13 23:47:38 +04:00
|
|
|
nsAString &aURL)
|
|
|
|
{
|
|
|
|
// is it already in the list?
|
2002-11-12 22:40:11 +03:00
|
|
|
PRInt32 foundIndex = mStyleSheets.IndexOf(aStyleSheet);
|
2002-09-13 23:47:38 +04:00
|
|
|
|
|
|
|
// Don't fail if we don't find it in our list
|
2009-02-10 23:39:43 +03:00
|
|
|
// Note: mStyleSheets is nsCOMArray, so its IndexOf() method
|
|
|
|
// returns -1 on failure.
|
2002-09-13 23:47:38 +04:00
|
|
|
if (foundIndex == -1)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Found it in the list!
|
2009-01-18 23:14:14 +03:00
|
|
|
aURL = mStyleSheetURLs[foundIndex];
|
2002-09-13 23:47:38 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-03 00:00:53 +03:00
|
|
|
/*
|
|
|
|
* nsIEditorMailSupport methods
|
|
|
|
*/
|
2001-01-28 23:13:07 +03:00
|
|
|
|
1999-11-24 23:48:59 +03:00
|
|
|
NS_IMETHODIMP
|
2001-01-28 23:13:07 +03:00
|
|
|
nsHTMLEditor::GetEmbeddedObjects(nsISupportsArray** aNodeList)
|
1999-11-11 02:42:11 +03:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(aNodeList, NS_ERROR_NULL_POINTER);
|
1999-11-11 02:42:11 +03:00
|
|
|
|
2011-11-16 11:50:20 +04:00
|
|
|
nsresult rv = NS_NewISupportsArray(aNodeList);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(*aNodeList, NS_ERROR_NULL_POINTER);
|
1999-11-18 22:43:14 +03:00
|
|
|
|
2003-07-09 00:22:56 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
2011-11-16 11:50:20 +04:00
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &rv);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(iter, NS_ERROR_NULL_POINTER);
|
2011-11-16 11:50:20 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2000-01-10 13:13:58 +03:00
|
|
|
|
2012-03-24 12:19:14 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc = GetDocument();
|
2011-11-16 11:50:20 +04:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
|
2000-07-13 03:44:56 +04:00
|
|
|
|
2011-11-16 11:50:20 +04:00
|
|
|
iter->Init(doc->GetRootElement());
|
|
|
|
|
|
|
|
// Loop through the content iterator for each content node.
|
|
|
|
while (!iter->IsDone()) {
|
|
|
|
nsINode* node = iter->GetCurrentNode();
|
|
|
|
if (node->IsElement()) {
|
|
|
|
dom::Element* element = node->AsElement();
|
|
|
|
|
|
|
|
// See if it's an image or an embed and also include all links.
|
|
|
|
// Let mail decide which link to send or not
|
|
|
|
if (element->IsHTML(nsGkAtoms::img) ||
|
|
|
|
element->IsHTML(nsGkAtoms::embed) ||
|
|
|
|
element->IsHTML(nsGkAtoms::a) ||
|
|
|
|
(element->IsHTML(nsGkAtoms::body) &&
|
|
|
|
element->HasAttr(kNameSpaceID_None, nsGkAtoms::background))) {
|
|
|
|
nsCOMPtr<nsIDOMNode> domNode = do_QueryInterface(node);
|
|
|
|
(*aNodeList)->AppendElement(domNode);
|
2000-01-10 13:13:58 +03:00
|
|
|
}
|
1999-11-24 23:48:59 +03:00
|
|
|
}
|
2011-11-16 11:50:20 +04:00
|
|
|
iter->Next();
|
2000-07-13 03:44:56 +04:00
|
|
|
}
|
1999-11-30 00:15:57 +03:00
|
|
|
|
2011-11-16 11:50:20 +04:00
|
|
|
return rv;
|
1999-11-11 02:42:11 +03:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-02-01 14:54:22 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::DeleteNode(nsIDOMNode* aNode)
|
2002-12-22 04:51:14 +03:00
|
|
|
{
|
2007-06-28 06:48:16 +04:00
|
|
|
// do nothing if the node is read-only
|
2012-02-01 14:54:22 +04:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
|
|
if (!IsModifiableNode(aNode) && !IsMozEditorBogusNode(content)) {
|
2007-06-28 06:48:16 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-12-22 04:51:14 +03:00
|
|
|
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)
|
|
|
|
{
|
2007-06-28 06:48:16 +04:00
|
|
|
// do nothing if the node is read-only
|
|
|
|
if (!IsModifiableNode(aTextNode)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-12-22 04:51:14 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> selectAllNode = FindUserSelectAllNode(aTextNode);
|
|
|
|
|
|
|
|
if (selectAllNode)
|
|
|
|
{
|
|
|
|
return nsEditor::DeleteNode(selectAllNode);
|
|
|
|
}
|
|
|
|
return nsEditor::DeleteText(aTextNode, aOffset, aLength);
|
|
|
|
}
|
|
|
|
|
2007-06-28 06:48:16 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::InsertTextImpl(const nsAString& aStringToInsert,
|
|
|
|
nsCOMPtr<nsIDOMNode> *aInOutNode,
|
|
|
|
PRInt32 *aInOutOffset,
|
|
|
|
nsIDOMDocument *aDoc)
|
|
|
|
{
|
|
|
|
// do nothing if the node is read-only
|
|
|
|
if (!IsModifiableNode(*aInOutNode)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsEditor::InsertTextImpl(aStringToInsert, aInOutNode, aInOutOffset, aDoc);
|
|
|
|
}
|
2002-12-22 04:51:14 +03:00
|
|
|
|
2010-06-17 09:31:15 +04:00
|
|
|
void
|
|
|
|
nsHTMLEditor::ContentAppended(nsIDocument *aDocument, nsIContent* aContainer,
|
|
|
|
nsIContent* aFirstNewContent,
|
2012-04-25 09:56:32 +04:00
|
|
|
PRInt32 aIndexInContainer)
|
2010-06-17 09:31:15 +04:00
|
|
|
{
|
2012-04-25 09:56:32 +04:00
|
|
|
ContentInserted(aDocument, aContainer, aFirstNewContent, aIndexInContainer);
|
2010-06-17 09:31:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHTMLEditor::ContentInserted(nsIDocument *aDocument, nsIContent* aContainer,
|
2012-04-25 09:56:32 +04:00
|
|
|
nsIContent* aChild, PRInt32 aIndexInContainer)
|
2010-06-17 09:31:15 +04:00
|
|
|
{
|
2010-11-12 00:40:52 +03:00
|
|
|
if (!aChild) {
|
2010-06-17 09:32:05 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-19 00:01:12 +03:00
|
|
|
nsCOMPtr<nsIHTMLEditor> kungFuDeathGrip(this);
|
|
|
|
|
2010-06-17 09:32:05 +04:00
|
|
|
if (ShouldReplaceRootElement()) {
|
|
|
|
ResetRootElementAndEventTarget();
|
2010-06-17 09:31:15 +04:00
|
|
|
}
|
2010-11-12 00:40:52 +03:00
|
|
|
// We don't need to handle our own modifications
|
2010-11-13 23:12:15 +03:00
|
|
|
else if (!mAction && (aContainer ? aContainer->IsEditable() : aDocument->IsEditable())) {
|
2012-02-01 14:54:22 +04:00
|
|
|
if (IsMozEditorBogusNode(aChild)) {
|
2010-11-16 23:43:46 +03:00
|
|
|
// Ignore insertion of the bogus node
|
|
|
|
return;
|
|
|
|
}
|
2010-11-12 00:40:52 +03:00
|
|
|
mRules->DocumentModified();
|
2012-04-25 09:56:32 +04:00
|
|
|
|
|
|
|
// Update spellcheck for only the newly-inserted node (bug 743819)
|
|
|
|
if (mInlineSpellChecker) {
|
|
|
|
nsRefPtr<nsRange> range = new nsRange();
|
|
|
|
nsresult res = range->Set(aContainer, aIndexInContainer,
|
|
|
|
aContainer, aIndexInContainer + 1);
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
mInlineSpellChecker->SpellCheckRange(range);
|
|
|
|
}
|
|
|
|
}
|
2010-11-12 00:40:52 +03:00
|
|
|
}
|
2010-06-17 09:31:15 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHTMLEditor::ContentRemoved(nsIDocument *aDocument, nsIContent* aContainer,
|
2010-07-22 02:05:17 +04:00
|
|
|
nsIContent* aChild, PRInt32 aIndexInContainer,
|
|
|
|
nsIContent* aPreviousSibling)
|
2010-06-17 09:31:15 +04:00
|
|
|
{
|
2010-11-19 00:01:12 +03:00
|
|
|
nsCOMPtr<nsIHTMLEditor> kungFuDeathGrip(this);
|
|
|
|
|
2010-06-17 09:32:05 +04:00
|
|
|
if (SameCOMIdentity(aChild, mRootElement)) {
|
|
|
|
ResetRootElementAndEventTarget();
|
2010-06-17 09:31:15 +04:00
|
|
|
}
|
2010-11-12 00:40:52 +03:00
|
|
|
// We don't need to handle our own modifications
|
2010-11-13 23:12:15 +03:00
|
|
|
else if (!mAction && (aContainer ? aContainer->IsEditable() : aDocument->IsEditable())) {
|
2012-02-01 14:54:22 +04:00
|
|
|
if (aChild && IsMozEditorBogusNode(aChild)) {
|
2010-11-16 23:43:46 +03:00
|
|
|
// Ignore removal of the bogus node
|
|
|
|
return;
|
|
|
|
}
|
2010-11-12 00:40:52 +03:00
|
|
|
mRules->DocumentModified();
|
|
|
|
}
|
2010-06-17 09:31:15 +04:00
|
|
|
}
|
|
|
|
|
2002-12-22 04:51:14 +03:00
|
|
|
|
2012-02-15 13:18:24 +04:00
|
|
|
/* This routine examines aNode and its ancestors looking for any node which has the
|
2003-04-07 18:32:23 +04:00
|
|
|
-moz-user-select: all style lit. Return the highest such ancestor. */
|
2011-08-11 17:29:59 +04:00
|
|
|
already_AddRefed<nsIDOMNode>
|
|
|
|
nsHTMLEditor::FindUserSelectAllNode(nsIDOMNode* aNode)
|
2002-12-22 04:51:14 +03:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> node = aNode;
|
2011-12-04 01:50:15 +04:00
|
|
|
nsCOMPtr<nsIDOMElement> root = do_QueryInterface(GetRoot());
|
2003-04-07 18:32:23 +04:00
|
|
|
if (!nsEditorUtils::IsDescendantOf(aNode, root))
|
|
|
|
return nsnull;
|
2002-12-22 04:51:14 +03:00
|
|
|
|
2011-08-11 17:29:59 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> resultNode; // starts out empty
|
2002-12-22 04:51:14 +03:00
|
|
|
nsAutoString mozUserSelectValue;
|
|
|
|
while (node)
|
|
|
|
{
|
2011-08-11 17:29:59 +04:00
|
|
|
// retrieve the computed style of -moz-user-select for node
|
2003-04-17 09:42:33 +04:00
|
|
|
mHTMLCSSUtils->GetComputedProperty(node, nsEditProperty::cssMozUserSelect, mozUserSelectValue);
|
2004-05-23 02:15:22 +04:00
|
|
|
if (mozUserSelectValue.EqualsLiteral("all"))
|
2002-12-22 04:51:14 +03:00
|
|
|
{
|
2003-04-07 18:32:23 +04:00
|
|
|
resultNode = node;
|
2002-12-22 04:51:14 +03:00
|
|
|
}
|
2003-04-07 18:32:23 +04:00
|
|
|
if (node != root)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
node->GetParentNode(getter_AddRefs(tmp));
|
|
|
|
node = tmp;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
node = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-08-11 17:29:59 +04:00
|
|
|
return resultNode.forget();
|
2002-12-22 04:51:14 +03:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP_(bool)
|
2007-06-28 06:48:16 +04:00
|
|
|
nsHTMLEditor::IsModifiableNode(nsIDOMNode *aNode)
|
|
|
|
{
|
2011-11-28 16:53:29 +04:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
return IsModifiableNode(node);
|
|
|
|
}
|
2007-06-28 06:48:16 +04:00
|
|
|
|
2011-11-28 16:53:29 +04:00
|
|
|
bool
|
|
|
|
nsHTMLEditor::IsModifiableNode(nsINode *aNode)
|
|
|
|
{
|
|
|
|
return !aNode || aNode->IsEditable();
|
2007-06-28 06:48:16 +04:00
|
|
|
}
|
|
|
|
|
2000-09-14 15:45:01 +04:00
|
|
|
static nsresult SetSelectionAroundHeadChildren(nsCOMPtr<nsISelection> aSelection, nsWeakPtr aDocWeak)
|
2000-04-28 09:59:16 +04:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
// Set selection around <head> node
|
|
|
|
nsCOMPtr<nsIDOMDocument> doc = do_QueryReferent(aDocWeak);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED);
|
2002-01-14 23:31:43 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNodeList>nodeList;
|
|
|
|
res = doc->GetElementsByTagName(NS_LITERAL_STRING("head"), getter_AddRefs(nodeList));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(nodeList, NS_ERROR_NULL_POINTER);
|
2000-04-28 09:59:16 +04:00
|
|
|
|
|
|
|
PRUint32 count;
|
|
|
|
nodeList->GetLength(&count);
|
|
|
|
if (count < 1) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> headNode;
|
|
|
|
res = nodeList->Item(0, getter_AddRefs(headNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(headNode, NS_ERROR_NULL_POINTER);
|
2000-04-28 09:59:16 +04:00
|
|
|
|
|
|
|
// Collapse selection to before first child of the head,
|
|
|
|
res = aSelection->Collapse(headNode, 0);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-04-28 09:59:16 +04:00
|
|
|
|
|
|
|
// then extend it to just after
|
|
|
|
nsCOMPtr<nsIDOMNodeList> childNodes;
|
|
|
|
res = headNode->GetChildNodes(getter_AddRefs(childNodes));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(childNodes, NS_ERROR_NULL_POINTER);
|
2000-04-28 09:59:16 +04:00
|
|
|
PRUint32 childCount;
|
|
|
|
childNodes->GetLength(&childCount);
|
|
|
|
|
|
|
|
return aSelection->Extend(headNode, childCount+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::GetHeadContentsAsHTML(nsAString& aOutputString)
|
2000-04-28 09:59:16 +04:00
|
|
|
{
|
2000-09-14 15:45:01 +04:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2000-04-28 09:59:16 +04:00
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2000-04-28 09:59:16 +04:00
|
|
|
|
|
|
|
// Save current selection
|
|
|
|
nsAutoSelectionReset selectionResetter(selection, this);
|
|
|
|
|
|
|
|
res = SetSelectionAroundHeadChildren(selection, mDocWeak);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-04-28 09:59:16 +04:00
|
|
|
|
2002-03-26 01:39:19 +03:00
|
|
|
res = OutputToString(NS_LITERAL_STRING("text/html"),
|
|
|
|
nsIDocumentEncoder::OutputSelectionOnly,
|
|
|
|
aOutputString);
|
2000-04-29 03:39:27 +04:00
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
// Selection always includes <body></body>,
|
|
|
|
// so terminate there
|
2001-04-07 04:45:26 +04:00
|
|
|
nsReadingIterator<PRUnichar> findIter,endFindIter;
|
|
|
|
aOutputString.BeginReading(findIter);
|
|
|
|
aOutputString.EndReading(endFindIter);
|
|
|
|
//counting on our parser to always lower case!!!
|
2002-11-27 01:15:30 +03:00
|
|
|
if (CaseInsensitiveFindInReadable(NS_LITERAL_STRING("<body"),
|
|
|
|
findIter, endFindIter))
|
2000-04-29 03:39:27 +04:00
|
|
|
{
|
2001-04-07 04:45:26 +04:00
|
|
|
nsReadingIterator<PRUnichar> beginIter;
|
|
|
|
aOutputString.BeginReading(beginIter);
|
|
|
|
PRInt32 offset = Distance(beginIter, findIter);//get the distance
|
|
|
|
|
|
|
|
nsWritingIterator<PRUnichar> writeIter;
|
|
|
|
aOutputString.BeginWriting(writeIter);
|
2000-04-29 03:39:27 +04:00
|
|
|
// Ensure the string ends in a newline
|
|
|
|
PRUnichar newline ('\n');
|
2001-04-07 04:45:26 +04: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-29 03:39:27 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
2000-04-28 09:59:16 +04:00
|
|
|
}
|
|
|
|
|
1999-08-09 05:37:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::DebugUnitTests(PRInt32 *outNumTests, PRInt32 *outNumTestsFailed)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(outNumTests && outNumTestsFailed, NS_ERROR_NULL_POINTER);
|
1999-08-09 05:37:50 +04:00
|
|
|
|
1999-09-30 00:08:15 +04:00
|
|
|
TextEditorTest *tester = new TextEditorTest();
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(tester, NS_ERROR_OUT_OF_MEMORY);
|
1999-09-30 00:08:15 +04:00
|
|
|
|
1999-08-09 05:37:50 +04:00
|
|
|
tester->Run(this, outNumTests, outNumTestsFailed);
|
|
|
|
delete tester;
|
|
|
|
return NS_OK;
|
|
|
|
#else
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-13 23:47:38 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::StyleSheetLoaded(nsCSSStyleSheet* aSheet, bool aWasAlternate,
|
2005-09-12 22:41:15 +04:00
|
|
|
nsresult aStatus)
|
1999-08-09 05:37:50 +04:00
|
|
|
{
|
2002-09-13 23:47:38 +04:00
|
|
|
nsresult rv = NS_OK;
|
1999-09-30 00:08:15 +04:00
|
|
|
nsAutoEditBatch batchIt(this);
|
1999-08-09 05:37:50 +04:00
|
|
|
|
2002-09-13 23:47:38 +04:00
|
|
|
if (!mLastStyleSheetURL.IsEmpty())
|
|
|
|
RemoveStyleSheet(mLastStyleSheetURL);
|
|
|
|
|
2007-07-02 18:01:27 +04:00
|
|
|
nsRefPtr<AddStyleSheetTxn> txn;
|
|
|
|
rv = CreateTxnForAddStyleSheet(aSheet, getter_AddRefs(txn));
|
2002-09-13 23:47:38 +04:00
|
|
|
if (!txn) rv = NS_ERROR_NULL_POINTER;
|
|
|
|
if (NS_SUCCEEDED(rv))
|
1999-04-22 18:45:48 +04:00
|
|
|
{
|
2003-04-05 00:50:25 +04:00
|
|
|
rv = DoTransaction(txn);
|
2002-09-13 23:47:38 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
// Get the URI, then url spec from the sheet
|
2010-05-13 00:18:47 +04:00
|
|
|
nsCAutoString spec;
|
2010-05-18 08:00:40 +04:00
|
|
|
rv = aSheet->GetSheetURI()->GetSpec(spec);
|
2002-09-13 23:47:38 +04:00
|
|
|
|
2005-06-10 02:50:41 +04:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
2010-05-13 00:18:47 +04:00
|
|
|
// Save it so we can remove before applying the next one
|
|
|
|
mLastStyleSheetURL.AssignWithConversion(spec.get());
|
2002-09-13 23:47:38 +04:00
|
|
|
|
2010-05-13 00:18:47 +04:00
|
|
|
// Also save in our arrays of urls and sheets
|
|
|
|
AddNewStyleSheetToList(mLastStyleSheetURL, aSheet);
|
2005-06-10 02:50:41 +04:00
|
|
|
}
|
2002-09-13 23:47:38 +04:00
|
|
|
}
|
1999-08-09 05:37:50 +04:00
|
|
|
}
|
|
|
|
|
1999-12-04 04:29:18 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-08-09 05:37:50 +04:00
|
|
|
|
1999-12-07 11:30:19 +03:00
|
|
|
|
|
|
|
/** All editor operations which alter the doc should be prefaced
|
|
|
|
* with a call to StartOperation, naming the action and direction */
|
|
|
|
NS_IMETHODIMP
|
2012-05-05 22:52:29 +04:00
|
|
|
nsHTMLEditor::StartOperation(OperationID opID,
|
|
|
|
nsIEditor::EDirection aDirection)
|
1999-12-07 11:30:19 +03:00
|
|
|
{
|
2010-05-28 05:18:48 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2000-08-14 06:39:37 +04:00
|
|
|
nsEditor::StartOperation(opID, aDirection); // will set mAction, mDirection
|
|
|
|
if (mRules) return mRules->BeforeEdit(mAction, mDirection);
|
1999-12-07 11:30:19 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** All editor operations which alter the doc should be followed
|
2000-08-14 06:39:37 +04:00
|
|
|
* with a call to EndOperation */
|
1999-12-07 11:30:19 +03:00
|
|
|
NS_IMETHODIMP
|
2000-08-14 06:39:37 +04:00
|
|
|
nsHTMLEditor::EndOperation()
|
1999-12-07 11:30:19 +03:00
|
|
|
{
|
2010-05-28 05:18:48 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2000-08-14 06:39:37 +04:00
|
|
|
// post processing
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
if (mRules) res = mRules->AfterEdit(mAction, mDirection);
|
|
|
|
nsEditor::EndOperation(); // will clear mAction, mDirection
|
|
|
|
return res;
|
|
|
|
}
|
1999-12-07 11:30:19 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-05-01 10:34:52 +04:00
|
|
|
nsHTMLEditor::TagCanContainTag(nsIAtom* aParentTag, nsIAtom* aChildTag)
|
2000-01-10 13:13:58 +03:00
|
|
|
{
|
2012-05-01 10:34:52 +04:00
|
|
|
MOZ_ASSERT(aParentTag && aChildTag);
|
|
|
|
|
2007-02-24 23:07:06 +03:00
|
|
|
nsIParserService* parserService = nsContentUtils::GetParserService();
|
|
|
|
|
2005-09-05 18:18:56 +04:00
|
|
|
PRInt32 childTagEnum;
|
|
|
|
// XXX Should this handle #cdata-section too?
|
2012-05-01 10:34:52 +04:00
|
|
|
if (aChildTag == nsGkAtoms::textTagName) {
|
2005-09-05 18:18:56 +04:00
|
|
|
childTagEnum = eHTMLTag_text;
|
2012-05-01 10:34:52 +04:00
|
|
|
} else {
|
|
|
|
childTagEnum = parserService->HTMLAtomTagToId(aChildTag);
|
2000-06-29 13:23:41 +04: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 14:57:30 +04:00
|
|
|
|
2012-05-01 10:34:52 +04:00
|
|
|
PRInt32 parentTagEnum = parserService->HTMLAtomTagToId(aParentTag);
|
2005-09-05 18:18:56 +04:00
|
|
|
NS_ASSERTION(parentTagEnum < NS_HTML_TAG_MAX,
|
|
|
|
"Fix the caller, this type of node can never contain children.");
|
2005-06-16 17:10:58 +04:00
|
|
|
|
2005-09-05 18:18:56 +04:00
|
|
|
return nsHTMLEditUtils::CanContain(parentTagEnum, childTagEnum);
|
|
|
|
}
|
2005-06-16 17:10:58 +04:00
|
|
|
|
2012-01-25 11:50:05 +04:00
|
|
|
bool
|
|
|
|
nsHTMLEditor::IsContainer(nsINode* aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aNode);
|
|
|
|
return IsContainer(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2005-09-05 18:18:56 +04:00
|
|
|
nsHTMLEditor::IsContainer(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
if (!aNode) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2000-06-29 13:23:41 +04:00
|
|
|
}
|
2005-09-05 18:18:56 +04:00
|
|
|
|
|
|
|
nsAutoString stringTag;
|
|
|
|
|
|
|
|
nsresult rv = aNode->GetNodeName(stringTag);
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2005-09-05 18:18:56 +04:00
|
|
|
|
2005-12-22 18:05:41 +03:00
|
|
|
PRInt32 tagEnum;
|
|
|
|
// XXX Should this handle #cdata-section too?
|
|
|
|
if (stringTag.EqualsLiteral("#text")) {
|
|
|
|
tagEnum = eHTMLTag_text;
|
|
|
|
}
|
|
|
|
else {
|
2007-02-24 23:07:06 +03:00
|
|
|
tagEnum = nsContentUtils::GetParserService()->HTMLStringTagToId(stringTag);
|
2005-12-22 18:05:41 +03:00
|
|
|
}
|
2005-09-05 18:18:56 +04:00
|
|
|
|
|
|
|
return nsHTMLEditUtils::IsContainer(tagEnum);
|
2000-01-10 13:13:58 +03:00
|
|
|
}
|
|
|
|
|
1999-12-07 11:30:19 +03:00
|
|
|
|
2000-01-13 13:17:35 +03:00
|
|
|
NS_IMETHODIMP
|
2000-09-14 15:45:01 +04:00
|
|
|
nsHTMLEditor::SelectEntireDocument(nsISelection *aSelection)
|
2000-01-13 13:17:35 +03:00
|
|
|
{
|
|
|
|
if (!aSelection || !mRules) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
|
2010-05-28 05:18:48 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2005-03-24 22:00:01 +03:00
|
|
|
// get editor root node
|
2011-12-04 01:50:15 +04:00
|
|
|
nsCOMPtr<nsIDOMElement> rootElement = do_QueryInterface(GetRoot());
|
2000-01-13 13:17:35 +03:00
|
|
|
|
|
|
|
// is doc empty?
|
2011-09-29 10:19:26 +04:00
|
|
|
bool bDocIsEmpty;
|
2005-03-24 22:00:01 +03:00
|
|
|
nsresult res = mRules->DocumentIsEmpty(&bDocIsEmpty);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-01-13 13:17:35 +03:00
|
|
|
|
|
|
|
if (bDocIsEmpty)
|
|
|
|
{
|
|
|
|
// if its empty dont select entire doc - that would select the bogus node
|
2005-03-24 22:00:01 +03:00
|
|
|
return aSelection->Collapse(rootElement, 0);
|
2000-01-13 13:17:35 +03:00
|
|
|
}
|
2005-03-24 22:00:01 +03:00
|
|
|
|
|
|
|
return nsEditor::SelectEntireDocument(aSelection);
|
2000-01-13 13:17:35 +03:00
|
|
|
}
|
|
|
|
|
2007-06-28 06:48:16 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::SelectAll()
|
|
|
|
{
|
|
|
|
ForceCompositionEnd();
|
|
|
|
|
|
|
|
nsresult rv;
|
2011-03-11 08:40:30 +03:00
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
|
|
|
rv = GetSelectionController(getter_AddRefs(selCon));
|
2007-06-28 06:48:16 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
rv = selCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
|
|
|
|
getter_AddRefs(selection));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> anchorNode;
|
|
|
|
rv = selection->GetAnchorNode(getter_AddRefs(anchorNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> anchorContent = do_QueryInterface(anchorNode, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-07-12 08:20:20 +04:00
|
|
|
|
|
|
|
// If the anchor content has independent selection, we never need to explicitly
|
|
|
|
// select its children.
|
2010-07-22 20:22:44 +04:00
|
|
|
if (anchorContent->HasIndependentSelection()) {
|
2010-07-12 08:20:20 +04:00
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv = do_QueryInterface(selection);
|
|
|
|
NS_ENSURE_TRUE(selPriv, NS_ERROR_UNEXPECTED);
|
|
|
|
rv = selPriv->SetAncestorLimiter(nsnull);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-12-04 01:50:15 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> rootElement = do_QueryInterface(mRootElement, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return selection->SelectAllChildren(rootElement);
|
2010-07-12 08:20:20 +04:00
|
|
|
}
|
2007-06-28 06:48:16 +04:00
|
|
|
|
2011-05-22 16:43:13 +04:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2008-02-20 10:40:04 +03:00
|
|
|
nsIContent *rootContent = anchorContent->GetSelectionRootContent(ps);
|
2010-01-20 13:22:09 +03:00
|
|
|
NS_ENSURE_TRUE(rootContent, NS_ERROR_UNEXPECTED);
|
2007-06-28 06:48:16 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> rootElement = do_QueryInterface(rootContent, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return selection->SelectAllChildren(rootElement);
|
|
|
|
}
|
2000-01-13 13:17:35 +03:00
|
|
|
|
|
|
|
|
1999-08-09 05:37:50 +04:00
|
|
|
// 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 13:08:25 +03:00
|
|
|
void nsHTMLEditor::IsTextPropertySetByContent(nsIDOMNode *aNode,
|
|
|
|
nsIAtom *aProperty,
|
2002-03-24 01:08:20 +03:00
|
|
|
const nsAString *aAttribute,
|
|
|
|
const nsAString *aValue,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool &aIsSet,
|
2003-06-03 02:21:06 +04:00
|
|
|
nsAString *outValue)
|
1999-08-09 05:37:50 +04:00
|
|
|
{
|
|
|
|
nsresult result;
|
2011-10-17 18:59:28 +04:00
|
|
|
aIsSet = false; // must be initialized to false for code below to work
|
1999-08-09 05:37:50 +04:00
|
|
|
nsAutoString propName;
|
|
|
|
aProperty->ToString(propName);
|
2000-05-07 05:33:42 +04:00
|
|
|
nsCOMPtr<nsIDOMNode>node = aNode;
|
|
|
|
|
|
|
|
while (node)
|
1999-08-09 05:37:50 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMElement>element;
|
2000-05-07 05:33:42 +04:00
|
|
|
element = do_QueryInterface(node);
|
1999-08-09 05:37:50 +04:00
|
|
|
if (element)
|
|
|
|
{
|
fixes:
14753, 29843, 39864, 40141,
40139, 36679, 39542, 34729,
34855, 37216, 39292, 26447
r=sfraser,cmanske,fm; a=beppe
2000-05-25 03:00:24 +04:00
|
|
|
nsAutoString tag, value;
|
1999-08-09 05:37:50 +04:00
|
|
|
element->GetTagName(tag);
|
2002-02-20 01:49:12 +03:00
|
|
|
if (propName.Equals(tag, nsCaseInsensitiveStringComparator()))
|
1999-04-22 18:45:48 +04:00
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool found = false;
|
1999-08-09 05:37:50 +04:00
|
|
|
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-25 03:00:24 +04:00
|
|
|
if (outValue) *outValue = value;
|
2003-05-24 01:34:47 +04:00
|
|
|
if (!value.IsEmpty())
|
1999-08-09 05:37:50 +04:00
|
|
|
{
|
|
|
|
if (!aValue) {
|
2011-10-17 18:59:28 +04:00
|
|
|
found = true;
|
1999-08-09 05:37:50 +04:00
|
|
|
}
|
2001-04-07 04:45:26 +04:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nsString tString(*aValue);
|
2002-02-20 01:49:12 +03:00
|
|
|
if (tString.Equals(value, nsCaseInsensitiveStringComparator())) {
|
2011-10-17 18:59:28 +04:00
|
|
|
found = true;
|
2001-04-07 04:45:26 +04:00
|
|
|
}
|
|
|
|
else { // we found the prop with the attribute, but the value doesn't match
|
|
|
|
break;
|
|
|
|
}
|
1999-08-09 05:37:50 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2011-10-17 18:59:28 +04:00
|
|
|
found = true;
|
1999-08-09 05:37:50 +04:00
|
|
|
}
|
2000-03-24 03:26:47 +03:00
|
|
|
if (found)
|
1999-08-09 05:37:50 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
aIsSet = true;
|
1999-08-09 05:37:50 +04:00
|
|
|
break;
|
|
|
|
}
|
1999-05-05 08:05:19 +04:00
|
|
|
}
|
|
|
|
}
|
1999-08-09 05:37:50 +04:00
|
|
|
nsCOMPtr<nsIDOMNode>temp;
|
2000-05-07 05:33:42 +04:00
|
|
|
result = node->GetParentNode(getter_AddRefs(temp));
|
1999-08-09 05:37:50 +04:00
|
|
|
if (NS_SUCCEEDED(result) && temp) {
|
2005-03-24 22:00:01 +03:00
|
|
|
node = temp;
|
1999-08-09 05:37:50 +04:00
|
|
|
}
|
|
|
|
else {
|
2005-03-24 22:00:01 +03:00
|
|
|
node = nsnull;
|
1999-08-09 05:37:50 +04:00
|
|
|
}
|
1999-05-05 08:05:19 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-09 05:37:50 +04:00
|
|
|
|
2000-03-29 16:53:23 +04:00
|
|
|
//================================================================
|
|
|
|
// HTML Editor methods
|
|
|
|
//
|
|
|
|
// Note: Table Editing methods are implemented in nsTableEditor.cpp
|
|
|
|
//
|
1999-08-09 05:37:50 +04:00
|
|
|
|
1999-08-19 17:30:48 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2000-03-29 16:53:23 +04:00
|
|
|
nsHTMLEditor::SetCaretInTableCell(nsIDOMElement* aElement)
|
1999-08-09 05:37:50 +04:00
|
|
|
{
|
2012-02-01 14:54:22 +04:00
|
|
|
nsCOMPtr<dom::Element> element = do_QueryInterface(aElement);
|
2012-02-10 14:04:46 +04:00
|
|
|
if (!element || !element->IsHTML() ||
|
|
|
|
!nsHTMLEditUtils::IsTableElement(element) ||
|
2012-05-06 11:53:11 +04:00
|
|
|
!IsDescendantOfEditorRoot(element)) {
|
2012-02-01 14:54:22 +04:00
|
|
|
return false;
|
1999-05-27 01:40:51 +04:00
|
|
|
}
|
2012-02-01 14:54:22 +04:00
|
|
|
|
|
|
|
nsIContent* node = element;
|
|
|
|
while (node->HasChildren()) {
|
|
|
|
node = node->GetFirstChild();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set selection at beginning of the found node
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult rv = GetSelection(getter_AddRefs(selection));
|
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
|
|
|
NS_ENSURE_TRUE(selection, false);
|
|
|
|
|
|
|
|
return NS_SUCCEEDED(selection->CollapseNative(node, 0));
|
2000-03-29 16:53:23 +04:00
|
|
|
}
|
1999-08-19 17:30:48 +04:00
|
|
|
|
2000-03-29 16:53:23 +04: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 23:06:39 +04:00
|
|
|
{
|
2000-03-29 16:53:23 +04:00
|
|
|
tmp = GetBlockNodeParent(node);
|
|
|
|
if (!tmp) break;
|
2000-08-14 03:53:34 +04:00
|
|
|
if (nsHTMLEditUtils::IsTable(tmp)) tbl = tmp;
|
2000-03-29 16:53:23 +04:00
|
|
|
node = tmp;
|
1999-07-19 23:06:39 +04:00
|
|
|
}
|
2000-03-29 16:53:23 +04:00
|
|
|
return tbl;
|
1999-07-19 23:06:39 +04:00
|
|
|
}
|
1999-05-17 16:22:31 +04:00
|
|
|
|
2000-03-29 16:53:23 +04:00
|
|
|
|
1999-08-19 19:15:41 +04: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 17:30:48 +04:00
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2000-03-29 16:53:23 +04:00
|
|
|
nsHTMLEditor::CollapseAdjacentTextNodes(nsIDOMRange *aInRange)
|
1999-08-19 17:30:48 +04:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aInRange, NS_ERROR_NULL_POINTER);
|
2000-03-29 16:53:23 +04:00
|
|
|
nsAutoTxnsConserveSelection dontSpazMySelection(this);
|
2009-04-22 12:43:15 +04:00
|
|
|
nsTArray<nsIDOMNode*> textNodes;
|
|
|
|
// we can't actually do anything during iteration, so store the text nodes in an array
|
|
|
|
// don't bother ref counting them because we know we can hold them for the
|
|
|
|
// lifetime of this method
|
1999-08-19 17:30:48 +04:00
|
|
|
|
|
|
|
|
2000-03-29 16:53:23 +04:00
|
|
|
// build a list of editable text nodes
|
2003-07-09 00:22:56 +04:00
|
|
|
nsresult result;
|
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1", &result);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
1999-08-19 17:30:48 +04:00
|
|
|
|
2000-03-29 16:53:23 +04:00
|
|
|
iter->Init(aInRange);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
|
|
|
while (!iter->IsDone())
|
1999-08-19 17:30:48 +04:00
|
|
|
{
|
2012-02-10 14:04:46 +04:00
|
|
|
nsINode* node = iter->GetCurrentNode();
|
|
|
|
if (node->NodeType() == nsIDOMNode::TEXT_NODE &&
|
|
|
|
IsEditable(static_cast<nsIContent*>(node))) {
|
|
|
|
nsCOMPtr<nsIDOMNode> domNode = do_QueryInterface(node);
|
|
|
|
textNodes.AppendElement(domNode);
|
1999-08-19 17:30:48 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2000-03-29 16:53:23 +04:00
|
|
|
iter->Next();
|
1999-08-19 17:30:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// now that I have a list of text nodes, collapse adjacent text nodes
|
2000-03-29 16:53:23 +04:00
|
|
|
// NOTE: assumption that JoinNodes keeps the righthand node
|
2009-04-22 12:43:15 +04:00
|
|
|
while (textNodes.Length() > 1)
|
2000-03-29 16:53:23 +04:00
|
|
|
{
|
2001-12-21 04:10:07 +03:00
|
|
|
// we assume a textNodes entry can't be nsnull
|
2009-04-22 12:43:15 +04:00
|
|
|
nsIDOMNode *leftTextNode = textNodes[0];
|
|
|
|
nsIDOMNode *rightTextNode = textNodes[1];
|
2001-12-21 04:10:07 +03:00
|
|
|
NS_ASSERTION(leftTextNode && rightTextNode,"left or rightTextNode null in CollapseAdjacentTextNodes");
|
|
|
|
|
2012-02-15 13:18:24 +04:00
|
|
|
// get the prev sibling of the right node, and see if its leftTextNode
|
2000-03-29 16:53:23 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> prevSibOfRightNode;
|
2008-04-30 22:13:11 +04:00
|
|
|
result =
|
|
|
|
rightTextNode->GetPreviousSibling(getter_AddRefs(prevSibOfRightNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2004-01-24 03:46:17 +03:00
|
|
|
if (prevSibOfRightNode && (prevSibOfRightNode == leftTextNode))
|
1999-08-19 17:30:48 +04:00
|
|
|
{
|
2000-03-29 16:53:23 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
result = rightTextNode->GetParentNode(getter_AddRefs(parent));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
|
2000-03-29 16:53:23 +04:00
|
|
|
result = JoinNodes(leftTextNode, rightTextNode, parent);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
1999-08-19 17:30:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
textNodes.RemoveElementAt(0); // remove the leftmost text node from the list
|
|
|
|
}
|
|
|
|
|
1999-09-30 00:08:15 +04:00
|
|
|
return result;
|
1999-08-19 17:30:48 +04:00
|
|
|
}
|
1999-09-10 22:54:13 +04:00
|
|
|
|
2000-02-10 08:14:52 +03:00
|
|
|
NS_IMETHODIMP
|
2000-09-14 15:45:01 +04:00
|
|
|
nsHTMLEditor::SetSelectionAtDocumentStart(nsISelection *aSelection)
|
2000-02-10 08:14:52 +03:00
|
|
|
{
|
2012-02-10 14:04:46 +04:00
|
|
|
dom::Element* rootElement = GetRoot();
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(rootElement, NS_ERROR_NULL_POINTER);
|
2005-03-24 22:00:01 +03:00
|
|
|
|
2012-02-10 14:04:46 +04:00
|
|
|
return aSelection->CollapseNative(rootElement, 0);
|
2000-02-10 08:14:52 +03:00
|
|
|
}
|
|
|
|
|
2000-02-08 15:53:34 +03:00
|
|
|
|
2001-06-19 01:15:43 +04:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2012-02-15 13:18:24 +04:00
|
|
|
// RemoveBlockContainer: remove inNode, reparenting its children into their
|
2001-06-19 01:15:43 +04:00
|
|
|
// the parent of inNode. In addition, INSERT ANY BR's NEEDED
|
|
|
|
// TO PRESERVE IDENTITY OF REMOVED BLOCK.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::RemoveBlockContainer(nsIDOMNode *inNode)
|
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(inNode, NS_ERROR_NULL_POINTER);
|
2001-06-19 01:15:43 +04:00
|
|
|
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));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-19 01:15:43 +04:00
|
|
|
|
|
|
|
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));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-19 01:15:43 +04:00
|
|
|
if (sibling && !IsBlockNode(sibling) && !nsTextEditUtils::IsBreak(sibling))
|
|
|
|
{
|
|
|
|
res = GetFirstEditableChild(inNode, address_of(child));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-19 01:15:43 +04:00
|
|
|
if (child && !IsBlockNode(child))
|
|
|
|
{
|
|
|
|
// insert br node
|
|
|
|
res = CreateBR(inNode, 0, address_of(unused));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-19 01:15:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-19 01:15:43 +04:00
|
|
|
if (sibling && !IsBlockNode(sibling))
|
|
|
|
{
|
|
|
|
res = GetLastEditableChild(inNode, address_of(child));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-19 01:15:43 +04:00
|
|
|
if (child && !IsBlockNode(child) && !nsTextEditUtils::IsBreak(child))
|
|
|
|
{
|
|
|
|
// insert br node
|
|
|
|
PRUint32 len;
|
|
|
|
res = GetLengthOfDOMNode(inNode, len);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-19 01:15:43 +04:00
|
|
|
res = CreateBR(inNode, (PRInt32)len, address_of(unused));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-19 01:15:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-19 01:15:43 +04:00
|
|
|
if (sibling && !IsBlockNode(sibling) && !nsTextEditUtils::IsBreak(sibling))
|
|
|
|
{
|
|
|
|
res = GetNextHTMLSibling(inNode, address_of(sibling));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-19 01:15:43 +04:00
|
|
|
if (sibling && !IsBlockNode(sibling) && !nsTextEditUtils::IsBreak(sibling))
|
|
|
|
{
|
|
|
|
// insert br node
|
|
|
|
res = CreateBR(inNode, 0, address_of(unused));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-19 01:15:43 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now remove container
|
|
|
|
return RemoveContainer(inNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-24 03:26:47 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetPriorHTMLSibling: returns the previous editable sibling, if there is
|
|
|
|
// one within the parent
|
|
|
|
//
|
2012-05-05 13:00:06 +04:00
|
|
|
nsIContent*
|
2012-04-14 17:09:38 +04:00
|
|
|
nsHTMLEditor::GetPriorHTMLSibling(nsINode* aNode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
|
|
|
|
nsIContent* node = aNode->GetPreviousSibling();
|
|
|
|
while (node && !IsEditable(node)) {
|
|
|
|
node = node->GetPreviousSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2000-03-24 03:26:47 +03:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetPriorHTMLSibling(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode)
|
|
|
|
{
|
2012-04-14 17:09:38 +04:00
|
|
|
NS_ENSURE_TRUE(outNode, NS_ERROR_NULL_POINTER);
|
|
|
|
*outNode = NULL;
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(inNode);
|
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
*outNode = do_QueryInterface(GetPriorHTMLSibling(node));
|
|
|
|
return NS_OK;
|
2000-03-24 03:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-08 15:53:34 +03:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2000-03-24 03:26:47 +03: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.
|
|
|
|
//
|
2012-05-05 13:00:06 +04:00
|
|
|
nsIContent*
|
2012-04-14 17:09:38 +04:00
|
|
|
nsHTMLEditor::GetPriorHTMLSibling(nsINode* aParent, PRInt32 aOffset)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
|
|
|
|
nsIContent* node = aParent->GetChildAt(aOffset - 1);
|
|
|
|
if (!node || IsEditable(node)) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetPriorHTMLSibling(node);
|
|
|
|
}
|
|
|
|
|
2000-02-08 15:53:34 +03:00
|
|
|
nsresult
|
2000-03-24 03:26:47 +03:00
|
|
|
nsHTMLEditor::GetPriorHTMLSibling(nsIDOMNode *inParent, PRInt32 inOffset, nsCOMPtr<nsIDOMNode> *outNode)
|
2000-02-08 15:53:34 +03:00
|
|
|
{
|
2012-04-14 17:09:38 +04:00
|
|
|
NS_ENSURE_TRUE(outNode, NS_ERROR_NULL_POINTER);
|
|
|
|
*outNode = NULL;
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> parent = do_QueryInterface(inParent);
|
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
*outNode = do_QueryInterface(GetPriorHTMLSibling(parent, inOffset));
|
|
|
|
return NS_OK;
|
2000-03-24 03:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetNextHTMLSibling: returns the next editable sibling, if there is
|
|
|
|
// one within the parent
|
|
|
|
//
|
2012-05-05 13:00:06 +04:00
|
|
|
nsIContent*
|
2012-04-14 17:09:38 +04:00
|
|
|
nsHTMLEditor::GetNextHTMLSibling(nsINode* aNode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
|
|
|
|
nsIContent* node = aNode->GetNextSibling();
|
|
|
|
while (node && !IsEditable(node)) {
|
|
|
|
node = node->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2000-03-24 03:26:47 +03:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetNextHTMLSibling(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode)
|
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(outNode, NS_ERROR_NULL_POINTER);
|
2000-03-24 03:26:47 +03:00
|
|
|
*outNode = nsnull;
|
2012-04-14 17:09:38 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(inNode);
|
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
2012-04-14 17:09:38 +04:00
|
|
|
*outNode = do_QueryInterface(GetNextHTMLSibling(node));
|
|
|
|
return NS_OK;
|
2000-03-24 03:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// 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.
|
2012-05-05 13:00:06 +04:00
|
|
|
nsIContent*
|
2012-04-14 17:09:38 +04:00
|
|
|
nsHTMLEditor::GetNextHTMLSibling(nsINode* aParent, PRInt32 aOffset)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
|
|
|
|
nsIContent* node = aParent->GetChildAt(aOffset + 1);
|
|
|
|
if (!node || IsEditable(node)) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetNextHTMLSibling(node);
|
|
|
|
}
|
|
|
|
|
2000-03-24 03:26:47 +03:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetNextHTMLSibling(nsIDOMNode *inParent, PRInt32 inOffset, nsCOMPtr<nsIDOMNode> *outNode)
|
|
|
|
{
|
2012-04-14 17:09:38 +04:00
|
|
|
NS_ENSURE_TRUE(outNode, NS_ERROR_NULL_POINTER);
|
|
|
|
*outNode = NULL;
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> parent = do_QueryInterface(inParent);
|
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
*outNode = do_QueryInterface(GetNextHTMLSibling(parent, inOffset));
|
|
|
|
return NS_OK;
|
2000-03-24 03:26:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetPriorHTMLNode: returns the previous editable leaf node, if there is
|
|
|
|
// one within the <body>
|
2005-03-24 22:00:01 +03:00
|
|
|
//
|
2000-03-24 03:26:47 +03:00
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetPriorHTMLNode(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode, bool bNoBlockCrossing)
|
2000-03-24 03:26:47 +03:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(outNode, NS_ERROR_NULL_POINTER);
|
2011-11-28 16:54:32 +04:00
|
|
|
|
|
|
|
nsIContent* activeEditingHost = GetActiveEditingHost();
|
|
|
|
if (!activeEditingHost) {
|
2000-03-24 03:26:47 +03:00
|
|
|
*outNode = nsnull;
|
2011-11-28 16:54:32 +04:00
|
|
|
return NS_OK;
|
2000-03-24 03:26:47 +03:00
|
|
|
}
|
2011-11-28 16:54:32 +04:00
|
|
|
|
2012-05-06 11:53:11 +04:00
|
|
|
nsresult res = GetPriorNode(inNode, true, address_of(*outNode),
|
|
|
|
bNoBlockCrossing);
|
2011-11-28 16:54:32 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
2012-05-06 11:53:11 +04:00
|
|
|
NS_ASSERTION(!*outNode || IsDescendantOfEditorRoot(*outNode),
|
2011-11-28 16:54:32 +04:00
|
|
|
"GetPriorNode screwed up");
|
2000-03-24 03:26:47 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetPriorHTMLNode: same as above but takes {parent,offset} instead of node
|
|
|
|
//
|
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetPriorHTMLNode(nsIDOMNode *inParent, PRInt32 inOffset, nsCOMPtr<nsIDOMNode> *outNode, bool bNoBlockCrossing)
|
2000-03-24 03:26:47 +03:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(outNode, NS_ERROR_NULL_POINTER);
|
2011-11-28 16:54:32 +04:00
|
|
|
|
|
|
|
nsIContent* activeEditingHost = GetActiveEditingHost();
|
|
|
|
if (!activeEditingHost) {
|
2000-03-24 03:26:47 +03:00
|
|
|
*outNode = nsnull;
|
2011-11-28 16:54:32 +04:00
|
|
|
return NS_OK;
|
2000-02-08 15:53:34 +03:00
|
|
|
}
|
2011-11-28 16:54:32 +04:00
|
|
|
|
2012-05-06 11:53:11 +04:00
|
|
|
nsresult res = GetPriorNode(inParent, inOffset, true, address_of(*outNode),
|
|
|
|
bNoBlockCrossing);
|
2011-11-28 16:54:32 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
2012-05-06 11:53:11 +04:00
|
|
|
NS_ASSERTION(!*outNode || IsDescendantOfEditorRoot(*outNode),
|
2011-11-28 16:54:32 +04:00
|
|
|
"GetPriorNode screwed up");
|
2000-02-08 15:53:34 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2000-08-26 08:03:50 +04:00
|
|
|
// GetNextHTMLNode: returns the next editable leaf node, if there is
|
2000-03-24 03:26:47 +03:00
|
|
|
// one within the <body>
|
|
|
|
//
|
2000-02-08 15:53:34 +03:00
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetNextHTMLNode(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode, bool bNoBlockCrossing)
|
2000-02-08 15:53:34 +03:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(outNode, NS_ERROR_NULL_POINTER);
|
2011-10-17 18:59:28 +04:00
|
|
|
nsresult res = GetNextNode(inNode, true, address_of(*outNode), bNoBlockCrossing);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-02-08 15:53:34 +03:00
|
|
|
|
2000-03-24 03:26:47 +03:00
|
|
|
// if it's not in the body, then zero it out
|
2012-05-06 11:53:11 +04:00
|
|
|
if (*outNode && !IsDescendantOfEditorRoot(*outNode)) {
|
2000-03-24 03:26:47 +03:00
|
|
|
*outNode = nsnull;
|
2000-02-08 15:53:34 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2000-03-24 03:26:47 +03:00
|
|
|
// GetNHTMLextNode: same as above but takes {parent,offset} instead of node
|
|
|
|
//
|
2000-02-08 15:53:34 +03:00
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetNextHTMLNode(nsIDOMNode *inParent, PRInt32 inOffset, nsCOMPtr<nsIDOMNode> *outNode, bool bNoBlockCrossing)
|
2000-02-08 15:53:34 +03:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(outNode, NS_ERROR_NULL_POINTER);
|
2011-10-17 18:59:28 +04:00
|
|
|
nsresult res = GetNextNode(inParent, inOffset, true, address_of(*outNode), bNoBlockCrossing);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-02-08 15:53:34 +03:00
|
|
|
|
2000-03-24 03:26:47 +03:00
|
|
|
// if it's not in the body, then zero it out
|
2012-05-06 11:53:11 +04:00
|
|
|
if (*outNode && !IsDescendantOfEditorRoot(*outNode)) {
|
2000-03-24 03:26:47 +03:00
|
|
|
*outNode = nsnull;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::IsFirstEditableChild( nsIDOMNode *aNode, bool *aOutIsFirst)
|
2000-03-24 03:26:47 +03:00
|
|
|
{
|
|
|
|
// check parms
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aOutIsFirst && aNode, NS_ERROR_NULL_POINTER);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
|
|
|
// init out parms
|
2011-10-17 18:59:28 +04:00
|
|
|
*aOutIsFirst = false;
|
2000-03-24 03:26:47 +03:00
|
|
|
|
|
|
|
// find first editable child and compare it to aNode
|
|
|
|
nsCOMPtr<nsIDOMNode> parent, firstChild;
|
|
|
|
nsresult res = aNode->GetParentNode(getter_AddRefs(parent));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_FAILURE);
|
2000-12-09 07:46:08 +03:00
|
|
|
res = GetFirstEditableChild(parent, address_of(firstChild));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
|
|
|
*aOutIsFirst = (firstChild.get() == aNode);
|
|
|
|
return res;
|
|
|
|
}
|
2000-02-08 15:53:34 +03:00
|
|
|
|
2000-03-24 03:26:47 +03:00
|
|
|
|
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::IsLastEditableChild( nsIDOMNode *aNode, bool *aOutIsLast)
|
2000-03-24 03:26:47 +03:00
|
|
|
{
|
|
|
|
// check parms
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aOutIsLast && aNode, NS_ERROR_NULL_POINTER);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
|
|
|
// init out parms
|
2011-10-17 18:59:28 +04:00
|
|
|
*aOutIsLast = false;
|
2000-03-24 03:26:47 +03:00
|
|
|
|
|
|
|
// find last editable child and compare it to aNode
|
|
|
|
nsCOMPtr<nsIDOMNode> parent, lastChild;
|
|
|
|
nsresult res = aNode->GetParentNode(getter_AddRefs(parent));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_FAILURE);
|
2000-12-09 07:46:08 +03:00
|
|
|
res = GetLastEditableChild(parent, address_of(lastChild));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
|
|
|
*aOutIsLast = (lastChild.get() == aNode);
|
|
|
|
return res;
|
|
|
|
}
|
2000-02-08 15:53:34 +03:00
|
|
|
|
2000-03-24 03:26:47 +03:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetFirstEditableChild( nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutFirstChild)
|
|
|
|
{
|
|
|
|
// check parms
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aOutFirstChild && aNode, NS_ERROR_NULL_POINTER);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
|
|
|
// init out parms
|
|
|
|
*aOutFirstChild = nsnull;
|
|
|
|
|
|
|
|
// find first editable child
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
nsresult res = aNode->GetFirstChild(getter_AddRefs(child));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
|
|
|
while (child && !IsEditable(child))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
res = child->GetNextSibling(getter_AddRefs(tmp));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(tmp, NS_ERROR_FAILURE);
|
2000-03-24 03:26:47 +03:00
|
|
|
child = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aOutFirstChild = child;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetLastEditableChild( nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutLastChild)
|
|
|
|
{
|
|
|
|
// check parms
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aOutLastChild && aNode, NS_ERROR_NULL_POINTER);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
|
|
|
// init out parms
|
2002-08-29 11:49:31 +04:00
|
|
|
*aOutLastChild = aNode;
|
2000-03-24 03:26:47 +03:00
|
|
|
|
|
|
|
// find last editable child
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
nsresult res = aNode->GetLastChild(getter_AddRefs(child));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-03-24 03:26:47 +03:00
|
|
|
|
|
|
|
while (child && !IsEditable(child))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
res = child->GetPreviousSibling(getter_AddRefs(tmp));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(tmp, NS_ERROR_FAILURE);
|
2000-03-24 03:26:47 +03:00
|
|
|
child = tmp;
|
|
|
|
}
|
|
|
|
|
|
|
|
*aOutLastChild = child;
|
|
|
|
return res;
|
2000-02-08 15:53:34 +03:00
|
|
|
}
|
2000-03-24 03:26:47 +03:00
|
|
|
|
2000-08-26 08:03:50 +04:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetFirstEditableLeaf( nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutFirstLeaf)
|
|
|
|
{
|
|
|
|
// check parms
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aOutFirstLeaf && aNode, NS_ERROR_NULL_POINTER);
|
2000-08-26 08:03:50 +04:00
|
|
|
|
|
|
|
// init out parms
|
2002-08-29 11:49:31 +04:00
|
|
|
*aOutFirstLeaf = aNode;
|
2000-08-26 08:03:50 +04:00
|
|
|
|
|
|
|
// find leftmost leaf
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
2001-08-22 09:32:44 +04:00
|
|
|
nsresult res = NS_OK;
|
|
|
|
child = GetLeftmostChild(aNode);
|
2002-12-22 04:51:14 +03:00
|
|
|
while (child && (!IsEditable(child) || !nsEditorUtils::IsLeafNode(child)))
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
2000-12-09 07:46:08 +03:00
|
|
|
res = GetNextHTMLNode(child, address_of(tmp));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(tmp, NS_ERROR_FAILURE);
|
2000-08-26 08:03:50 +04:00
|
|
|
|
|
|
|
// only accept nodes that are descendants of aNode
|
2002-12-22 04:51:14 +03:00
|
|
|
if (nsEditorUtils::IsDescendantOf(tmp, aNode))
|
2000-08-26 08:03:50 +04:00
|
|
|
child = tmp;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
child = nsnull; // this will abort the loop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aOutFirstLeaf = child;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2011-08-11 17:29:59 +04:00
|
|
|
nsHTMLEditor::GetLastEditableLeaf(nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *aOutLastLeaf)
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
|
|
|
// check parms
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aOutLastLeaf && aNode, NS_ERROR_NULL_POINTER);
|
2000-08-26 08:03:50 +04:00
|
|
|
|
|
|
|
// init out parms
|
|
|
|
*aOutLastLeaf = nsnull;
|
|
|
|
|
2001-08-22 09:32:44 +04:00
|
|
|
// find rightmost leaf
|
2011-10-17 18:59:28 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> child = GetRightmostChild(aNode, false);
|
2001-08-22 09:32:44 +04:00
|
|
|
nsresult res = NS_OK;
|
2002-12-22 04:51:14 +03:00
|
|
|
while (child && (!IsEditable(child) || !nsEditorUtils::IsLeafNode(child)))
|
2000-08-26 08:03:50 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
2000-12-09 07:46:08 +03:00
|
|
|
res = GetPriorHTMLNode(child, address_of(tmp));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(tmp, NS_ERROR_FAILURE);
|
2000-08-26 08:03:50 +04:00
|
|
|
|
|
|
|
// only accept nodes that are descendants of aNode
|
2002-12-22 04:51:14 +03:00
|
|
|
if (nsEditorUtils::IsDescendantOf(tmp, aNode))
|
2000-08-26 08:03:50 +04:00
|
|
|
child = tmp;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
child = nsnull;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*aOutLastLeaf = child;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2011-11-28 16:53:29 +04:00
|
|
|
nsHTMLEditor::IsTextInDirtyFrameVisible(nsIContent *aNode)
|
2003-06-11 15:50:36 +04:00
|
|
|
{
|
2012-05-05 13:00:06 +04:00
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
MOZ_ASSERT(aNode->NodeType() == nsIDOMNode::TEXT_NODE);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isEmptyTextNode;
|
2012-01-25 11:50:05 +04:00
|
|
|
nsresult rv = IsVisTextNode(aNode, &isEmptyTextNode, false);
|
|
|
|
if (NS_FAILED(rv)) {
|
2003-06-11 15:50:36 +04:00
|
|
|
// We are following the historical decision:
|
|
|
|
// if we don't know, we say it's visible...
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2003-06-11 15:50:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return !isEmptyTextNode;
|
|
|
|
}
|
2002-02-07 06:56:20 +03:00
|
|
|
|
2002-04-14 06:37:23 +04:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsVisTextNode: figure out if textnode aTextNode has any visible content.
|
|
|
|
//
|
2012-01-25 11:50:05 +04:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::IsVisTextNode(nsIContent* aNode,
|
|
|
|
bool* outIsEmptyNode,
|
|
|
|
bool aSafeToAskFrames)
|
2002-04-14 06:37:23 +04:00
|
|
|
{
|
2012-05-05 13:00:06 +04:00
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
MOZ_ASSERT(aNode->NodeType() == nsIDOMNode::TEXT_NODE);
|
|
|
|
MOZ_ASSERT(outIsEmptyNode);
|
2002-04-14 06:37:23 +04:00
|
|
|
|
2012-05-05 13:00:06 +04:00
|
|
|
*outIsEmptyNode = true;
|
2012-01-25 11:50:05 +04:00
|
|
|
|
|
|
|
PRUint32 length = aNode->TextLength();
|
2002-04-14 06:37:23 +04:00
|
|
|
if (aSafeToAskFrames)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
2012-01-25 11:50:05 +04:00
|
|
|
nsresult res = GetSelectionController(getter_AddRefs(selCon));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selCon, NS_ERROR_FAILURE);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isVisible = false;
|
2002-04-14 06:37:23 +04:00
|
|
|
// 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)
|
2012-01-25 11:50:05 +04:00
|
|
|
res = selCon->CheckVisibilityContent(aNode, 0, length, &isVisible);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-04-14 06:37:23 +04:00
|
|
|
if (isVisible)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*outIsEmptyNode = false;
|
2002-04-14 06:37:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (length)
|
|
|
|
{
|
2012-01-25 11:50:05 +04:00
|
|
|
if (aNode->TextIsOnlyWhitespace())
|
2002-04-14 06:37:23 +04:00
|
|
|
{
|
2012-01-25 11:50:05 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aNode);
|
|
|
|
nsWSRunObject wsRunObj(this, node, 0);
|
2002-04-14 06:37:23 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> visNode;
|
|
|
|
PRInt32 outVisOffset=0;
|
|
|
|
PRInt16 visType=0;
|
2012-01-25 11:50:05 +04:00
|
|
|
nsresult res = wsRunObj.NextVisibleNode(node, 0, address_of(visNode),
|
|
|
|
&outVisOffset, &visType);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-04-14 06:37:23 +04:00
|
|
|
if ( (visType == nsWSRunObject::eNormalWS) ||
|
|
|
|
(visType == nsWSRunObject::eText) )
|
|
|
|
{
|
2012-01-25 11:50:05 +04:00
|
|
|
*outIsEmptyNode = (node != visNode);
|
2002-04-14 06:37:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*outIsEmptyNode = false;
|
2002-04-14 06:37:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-22 09:39:54 +04: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,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *outIsEmptyNode,
|
|
|
|
bool aSingleBRDoesntCount,
|
|
|
|
bool aListOrCellNotEmpty,
|
|
|
|
bool aSafeToAskFrames)
|
2000-06-22 09:39:54 +04:00
|
|
|
{
|
2012-01-25 11:50:05 +04:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
2012-01-25 11:50:06 +04:00
|
|
|
return IsEmptyNode(node, outIsEmptyNode, aSingleBRDoesntCount,
|
|
|
|
aListOrCellNotEmpty, aSafeToAskFrames);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::IsEmptyNode(nsINode* aNode,
|
|
|
|
bool* outIsEmptyNode,
|
|
|
|
bool aSingleBRDoesntCount,
|
|
|
|
bool aListOrCellNotEmpty,
|
|
|
|
bool aSafeToAskFrames)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aNode && outIsEmptyNode, NS_ERROR_NULL_POINTER);
|
2011-10-17 18:59:28 +04:00
|
|
|
*outIsEmptyNode = true;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool seenBR = false;
|
2012-01-25 11:50:06 +04:00
|
|
|
return IsEmptyNodeImpl(aNode, outIsEmptyNode, aSingleBRDoesntCount,
|
2002-04-14 06:37:23 +04:00
|
|
|
aListOrCellNotEmpty, aSafeToAskFrames, &seenBR);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsEmptyNodeImpl: workhorse for IsEmptyNode.
|
|
|
|
//
|
|
|
|
nsresult
|
2012-01-25 11:50:05 +04:00
|
|
|
nsHTMLEditor::IsEmptyNodeImpl(nsINode* aNode,
|
|
|
|
bool *outIsEmptyNode,
|
|
|
|
bool aSingleBRDoesntCount,
|
|
|
|
bool aListOrCellNotEmpty,
|
|
|
|
bool aSafeToAskFrames,
|
|
|
|
bool *aSeenBR)
|
2002-04-14 06:37:23 +04:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(aNode && outIsEmptyNode && aSeenBR, NS_ERROR_NULL_POINTER);
|
2002-04-14 06:37:23 +04:00
|
|
|
|
2012-01-25 11:50:05 +04:00
|
|
|
if (aNode->NodeType() == nsIDOMNode::TEXT_NODE) {
|
|
|
|
return IsVisTextNode(static_cast<nsIContent*>(aNode), outIsEmptyNode, aSafeToAskFrames);
|
2000-06-22 09:39:54 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// if it's not a text node (handled above) and it's not a container,
|
2005-11-25 11:16:51 +03:00
|
|
|
// then we don't call it empty (it's an <hr>, or <br>, etc).
|
|
|
|
// Also, if it's an anchor then don't treat it as empty - even though
|
2000-06-22 09:39:54 +04:00
|
|
|
// 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 09:02:15 +03:00
|
|
|
// cells empty if caller desires. Form Widgets not empty.
|
2012-01-25 11:50:05 +04:00
|
|
|
if (!IsContainer(aNode) ||
|
|
|
|
(aNode->IsElement() &&
|
|
|
|
(nsHTMLEditUtils::IsNamedAnchor(aNode->AsElement()) ||
|
|
|
|
nsHTMLEditUtils::IsFormWidget(aNode->AsElement()) ||
|
|
|
|
(aListOrCellNotEmpty &&
|
|
|
|
(nsHTMLEditUtils::IsListItem(aNode->AsElement()) ||
|
|
|
|
nsHTMLEditUtils::IsTableCell(aNode->AsElement())))))) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*outIsEmptyNode = false;
|
2000-06-22 09:39:54 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-04-14 06:37:23 +04:00
|
|
|
|
2001-04-17 14:15:05 +04:00
|
|
|
// need this for later
|
2012-01-25 11:50:05 +04:00
|
|
|
bool isListItemOrCell = aNode->IsElement() &&
|
|
|
|
(nsHTMLEditUtils::IsListItem(aNode->AsElement()) ||
|
|
|
|
nsHTMLEditUtils::IsTableCell(aNode->AsElement()));
|
2001-04-17 14:15:05 +04:00
|
|
|
|
2002-02-07 06:56:20 +03:00
|
|
|
// loop over children of node. if no children, or all children are either
|
2000-06-22 09:39:54 +04:00
|
|
|
// empty text nodes or non-editable, then node qualifies as empty
|
2012-01-25 11:50:05 +04:00
|
|
|
for (nsCOMPtr<nsIContent> child = aNode->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
|
|
|
// Is the child editable and non-empty? if so, return false
|
|
|
|
if (nsEditor::IsEditable(child)) {
|
|
|
|
if (child->NodeType() == nsIDOMNode::TEXT_NODE) {
|
|
|
|
nsresult rv = IsVisTextNode(child, outIsEmptyNode, aSafeToAskFrames);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2011-07-14 19:25:07 +04:00
|
|
|
// break out if we find we aren't emtpy
|
|
|
|
if (!*outIsEmptyNode) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-01-25 11:50:05 +04:00
|
|
|
} else {
|
|
|
|
// An editable, non-text node. We need to check its content.
|
|
|
|
// Is it the node we are iterating over?
|
|
|
|
if (child == aNode) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSingleBRDoesntCount && !*aSeenBR && child->IsHTML(nsGkAtoms::br)) {
|
2001-05-05 01:49:48 +04:00
|
|
|
// the first br in a block doesn't count if the caller so indicated
|
2011-10-17 18:59:28 +04:00
|
|
|
*aSeenBR = true;
|
2012-01-25 11:50:05 +04:00
|
|
|
} else {
|
2000-06-22 09:39:54 +04:00
|
|
|
// is it an empty node of some sort?
|
2001-04-17 14:15:05 +04:00
|
|
|
// note: list items or table cells are not considered empty
|
|
|
|
// if they contain other lists or tables
|
2012-01-25 11:50:05 +04:00
|
|
|
if (child->IsElement()) {
|
|
|
|
if (isListItemOrCell) {
|
|
|
|
if (nsHTMLEditUtils::IsList(child->AsElement()) || child->IsHTML(nsGkAtoms::table)) {
|
|
|
|
// break out if we find we aren't empty
|
|
|
|
*outIsEmptyNode = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
} else if (nsHTMLEditUtils::IsFormWidget(child->AsElement())) {
|
|
|
|
// is it a form widget?
|
|
|
|
// break out if we find we aren't empty
|
2011-10-17 18:59:28 +04:00
|
|
|
*outIsEmptyNode = false;
|
2002-02-07 06:56:20 +03:00
|
|
|
return NS_OK;
|
2001-04-17 14:15:05 +04:00
|
|
|
}
|
|
|
|
}
|
2012-01-25 11:50:05 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isEmptyNode = true;
|
2012-01-25 11:50:05 +04:00
|
|
|
nsresult rv = IsEmptyNodeImpl(child, &isEmptyNode,
|
|
|
|
aSingleBRDoesntCount,
|
|
|
|
aListOrCellNotEmpty, aSafeToAskFrames,
|
|
|
|
aSeenBR);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!isEmptyNode) {
|
2000-06-22 09:39:54 +04:00
|
|
|
// otherwise it ain't empty
|
2011-10-17 18:59:28 +04:00
|
|
|
*outIsEmptyNode = false;
|
2002-02-07 06:56:20 +03:00
|
|
|
return NS_OK;
|
2000-06-22 09:39:54 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
// add to aElement the CSS inline styles corresponding to the HTML attribute
|
|
|
|
// aAttribute with its value aValue
|
|
|
|
nsresult
|
2002-01-25 13:16:52 +03:00
|
|
|
nsHTMLEditor::SetAttributeOrEquivalent(nsIDOMElement * aElement,
|
2002-03-24 01:08:20 +03:00
|
|
|
const nsAString & aAttribute,
|
2002-09-17 16:04:59 +04:00
|
|
|
const nsAString & aValue,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aSuppressTransaction)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
2012-02-01 14:54:22 +04:00
|
|
|
if (IsCSSEnabled() && mHTMLCSSUtils) {
|
2002-01-09 16:51:37 +03:00
|
|
|
PRInt32 count;
|
2002-09-17 16:04:59 +04:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(aElement, nsnull, &aAttribute, &aValue, &count,
|
|
|
|
aSuppressTransaction);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-15 18:33:29 +03:00
|
|
|
if (count) {
|
|
|
|
// we found an equivalence ; let's remove the HTML attribute itself if it is set
|
|
|
|
nsAutoString existingValue;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool wasSet = false;
|
2002-03-15 18:33:29 +03:00
|
|
|
res = GetAttributeValue(aElement, aAttribute, existingValue, &wasSet);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-15 18:33:29 +03:00
|
|
|
if (wasSet) {
|
2002-09-20 06:09:04 +04:00
|
|
|
if (aSuppressTransaction)
|
|
|
|
res = aElement->RemoveAttribute(aAttribute);
|
|
|
|
else
|
|
|
|
res = RemoveAttribute(aElement, aAttribute);
|
2002-03-15 18:33:29 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2002-01-09 16:51:37 +03: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
|
2004-05-23 02:15:22 +04:00
|
|
|
if (aAttribute.EqualsLiteral("style")) {
|
2002-01-09 16:51:37 +03:00
|
|
|
// if it is the style attribute, just add the new value to the existing style
|
|
|
|
// attribute's value
|
|
|
|
nsAutoString existingValue;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool wasSet = false;
|
2002-01-09 16:51:37 +03:00
|
|
|
res = GetAttributeValue(aElement, NS_LITERAL_STRING("style"), existingValue, &wasSet);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2004-06-17 04:13:25 +04:00
|
|
|
existingValue.AppendLiteral(" ");
|
2002-01-09 16:51:37 +03:00
|
|
|
existingValue.Append(aValue);
|
2002-09-20 06:09:04 +04:00
|
|
|
if (aSuppressTransaction)
|
|
|
|
res = aElement->SetAttribute(aAttribute, existingValue);
|
|
|
|
else
|
|
|
|
res = SetAttribute(aElement, aAttribute, existingValue);
|
2002-01-09 16:51:37 +03: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 06:09:04 +04:00
|
|
|
if (aSuppressTransaction)
|
|
|
|
res = aElement->SetAttribute(aAttribute, aValue);
|
|
|
|
else
|
|
|
|
res = SetAttribute(aElement, aAttribute, aValue);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// we are not in an HTML+CSS editor; let's set the attribute the HTML way
|
2002-09-20 06:09:04 +04:00
|
|
|
if (aSuppressTransaction)
|
|
|
|
res = aElement->SetAttribute(aAttribute, aValue);
|
|
|
|
else
|
|
|
|
res = SetAttribute(aElement, aAttribute, aValue);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-03-15 18:33:29 +03:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::RemoveAttributeOrEquivalent(nsIDOMElement * aElement,
|
2002-09-17 16:04:59 +04:00
|
|
|
const nsAString & aAttribute,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aSuppressTransaction)
|
2002-03-15 18:33:29 +03:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
2012-02-01 14:54:22 +04:00
|
|
|
if (IsCSSEnabled() && mHTMLCSSUtils) {
|
2002-09-17 16:04:59 +04:00
|
|
|
res = mHTMLCSSUtils->RemoveCSSEquivalentToHTMLStyle(aElement, nsnull, &aAttribute, nsnull,
|
|
|
|
aSuppressTransaction);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-15 18:33:29 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString existingValue;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool wasSet = false;
|
2002-03-15 18:33:29 +03:00
|
|
|
res = GetAttributeValue(aElement, aAttribute, existingValue, &wasSet);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-15 18:33:29 +03:00
|
|
|
if (wasSet) {
|
2002-09-20 06:09:04 +04:00
|
|
|
if (aSuppressTransaction)
|
|
|
|
res = aElement->RemoveAttribute(aAttribute);
|
|
|
|
else
|
|
|
|
res = RemoveAttribute(aElement, aAttribute);
|
2002-03-15 18:33:29 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::SetIsCSSEnabled(bool aIsCSSPrefChecked)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2012-02-10 14:04:46 +04:00
|
|
|
if (!mHTMLCSSUtils) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2007-11-13 11:10:03 +03:00
|
|
|
|
2012-02-10 14:04:46 +04:00
|
|
|
nsresult rv = mHTMLCSSUtils->SetCSSEnabled(aIsCSSPrefChecked);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Disable the eEditorNoCSSMask flag if we're enabling StyleWithCSS.
|
|
|
|
PRUint32 flags = mFlags;
|
|
|
|
if (aIsCSSPrefChecked) {
|
|
|
|
// Turn off NoCSS as we're enabling CSS
|
|
|
|
flags &= ~eEditorNoCSSMask;
|
|
|
|
} else {
|
|
|
|
// Turn on NoCSS, as we're disabling CSS.
|
|
|
|
flags |= eEditorNoCSSMask;
|
2007-11-13 11:10:03 +03:00
|
|
|
}
|
2012-02-10 14:04:46 +04:00
|
|
|
|
|
|
|
return SetFlags(flags);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the block background color
|
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::SetCSSBackgroundColor(const nsAString& aColor)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
ForceCompositionEnd();
|
|
|
|
|
2010-05-28 05:18:48 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
nsresult res;
|
|
|
|
nsCOMPtr<nsISelection>selection;
|
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2002-01-09 16:51:37 +03:00
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isCollapsed;
|
2002-01-09 16:51:37 +03:00
|
|
|
selection->GetIsCollapsed(&isCollapsed);
|
|
|
|
|
|
|
|
nsAutoEditBatch batchIt(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpInsertElement, nsIEditor::eNext);
|
|
|
|
nsAutoSelectionReset selectionResetter(selection, this);
|
|
|
|
nsAutoTxnsConserveSelection dontSpazMySelection(this);
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
2012-05-05 22:52:29 +04:00
|
|
|
nsTextRulesInfo ruleInfo(kOpSetTextProperty);
|
2002-01-09 16:51:37 +03:00
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
if (!cancel && !handled)
|
|
|
|
{
|
|
|
|
// get selection range enumerator
|
|
|
|
nsCOMPtr<nsIEnumerator> enumerator;
|
|
|
|
res = selPriv->GetEnumerator(getter_AddRefs(enumerator));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(enumerator, NS_ERROR_FAILURE);
|
2002-01-09 16:51:37 +03:00
|
|
|
|
|
|
|
// loop thru the ranges in the selection
|
|
|
|
enumerator->First();
|
|
|
|
nsCOMPtr<nsISupports> currentItem;
|
2004-06-17 04:13:25 +04:00
|
|
|
nsAutoString bgcolor; bgcolor.AssignLiteral("bgcolor");
|
2002-01-09 16:51:37 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> cachedBlockParent = nsnull;
|
|
|
|
while ((NS_ENUMERATOR_FALSE == enumerator->IsDone()))
|
|
|
|
{
|
|
|
|
res = enumerator->CurrentItem(getter_AddRefs(currentItem));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(currentItem, NS_ERROR_FAILURE);
|
2002-01-09 16:51:37 +03:00
|
|
|
|
|
|
|
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));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
res = range->GetEndContainer(getter_AddRefs(endNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
res = range->GetStartOffset(&startOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
res = range->GetEndOffset(&endOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
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;
|
2011-10-17 18:59:28 +04:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nsnull, &bgcolor, &aColor, &count, false);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
2002-02-18 13:06:07 +03:00
|
|
|
else if ((startNode == endNode) && nsTextEditUtils::IsBody(startNode) && isCollapsed)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
// we have no block in the document, let's apply the background to the body
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(startNode);
|
|
|
|
PRInt32 count;
|
2011-10-17 18:59:28 +04:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nsnull, &bgcolor, &aColor, &count, false);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
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);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isBlock =false;
|
2002-01-09 16:51:37 +03:00
|
|
|
res = NodeIsBlockStatic(selectedNode, &isBlock);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> blockParent = selectedNode;
|
|
|
|
if (!isBlock) {
|
|
|
|
blockParent = GetBlockNodeParent(selectedNode);
|
|
|
|
}
|
|
|
|
if (cachedBlockParent != blockParent)
|
|
|
|
{
|
|
|
|
cachedBlockParent = blockParent;
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(blockParent);
|
|
|
|
PRInt32 count;
|
2011-10-17 18:59:28 +04:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nsnull, &bgcolor, &aColor, &count, false);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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-09 00:22:56 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1", &res);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(iter, NS_ERROR_FAILURE);
|
2002-01-09 16:51:37 +03:00
|
|
|
|
2002-11-12 22:40:11 +03:00
|
|
|
nsCOMArray<nsIDOMNode> arrayOfNodes;
|
2002-01-09 16:51:37 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
2002-11-12 22:40:11 +03:00
|
|
|
|
2002-01-09 16:51:37 +03: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))
|
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
while (!iter->IsDone())
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
node = do_QueryInterface(iter->GetCurrentNode());
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_FAILURE);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2002-01-09 16:51:37 +03:00
|
|
|
if (IsEditable(node))
|
2002-11-12 22:40:11 +03:00
|
|
|
{
|
|
|
|
arrayOfNodes.AppendObject(node);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
|
|
|
iter->Next();
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// first check the start parent of the range to see if it needs to
|
2003-03-24 07:15:34 +03:00
|
|
|
// be separately handled (it does if it's a text node, due to how the
|
2002-01-09 16:51:37 +03: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;
|
2011-10-17 18:59:28 +04:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nsnull, &bgcolor, &aColor, &count, false);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then loop through the list, set the property on each node
|
2002-11-12 22:40:11 +03:00
|
|
|
PRInt32 listCount = arrayOfNodes.Count();
|
|
|
|
PRInt32 j;
|
2002-01-09 16:51:37 +03:00
|
|
|
for (j = 0; j < listCount; j++)
|
|
|
|
{
|
2002-11-12 22:40:11 +03:00
|
|
|
node = arrayOfNodes[j];
|
2002-01-09 16:51:37 +03:00
|
|
|
// do we have a block here ?
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isBlock =false;
|
2002-01-09 16:51:37 +03:00
|
|
|
res = NodeIsBlockStatic(node, &isBlock);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
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
|
2011-10-17 18:59:28 +04:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nsnull, &bgcolor, &aColor, &count, false);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
2002-11-12 22:40:11 +03:00
|
|
|
arrayOfNodes.Clear();
|
2002-01-09 16:51:37 +03:00
|
|
|
|
|
|
|
// last check the end parent of the range to see if it needs to
|
2003-03-24 07:15:34 +03:00
|
|
|
// be separately handled (it does if it's a text node, due to how the
|
2002-01-09 16:51:37 +03: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;
|
2011-10-17 18:59:28 +04:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nsnull, &bgcolor, &aColor, &count, false);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
enumerator->Next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!cancel)
|
|
|
|
{
|
|
|
|
// post-process
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-24 01:08:20 +03:00
|
|
|
nsHTMLEditor::SetBackgroundColor(const nsAString& aColor)
|
2002-01-09 16:51:37 +03:00
|
|
|
{
|
|
|
|
nsresult res;
|
2012-02-01 14:54:22 +04:00
|
|
|
if (IsCSSEnabled()) {
|
2002-01-09 16:51:37 +03:00
|
|
|
// 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?
|
|
|
|
//
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2002-01-09 16:51:37 +03:00
|
|
|
nsHTMLEditor::NodesSameType(nsIDOMNode *aNode1, nsIDOMNode *aNode2)
|
|
|
|
{
|
|
|
|
if (!aNode1 || !aNode2)
|
|
|
|
{
|
|
|
|
NS_NOTREACHED("null node passed to nsEditor::NodesSameType()");
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
2003-11-19 04:20:56 +03:00
|
|
|
nsIAtom *tag1 = GetTag(aNode1);
|
|
|
|
|
|
|
|
if (tag1 == GetTag(aNode2)) {
|
2012-02-01 14:54:22 +04:00
|
|
|
if (IsCSSEnabled() && tag1 == nsEditProperty::span) {
|
2002-01-09 16:51:37 +03:00
|
|
|
if (mHTMLCSSUtils->ElementsSameStyle(aNode1, aNode2)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-01-09 16:51:37 +03:00
|
|
|
}
|
|
|
|
|
2002-03-11 16:11:48 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::CopyLastEditableChildStyles(nsIDOMNode * aPreviousBlock, nsIDOMNode * aNewBlock,
|
|
|
|
nsIDOMNode **aOutBrNode)
|
|
|
|
{
|
|
|
|
*aOutBrNode = nsnull;
|
2002-04-06 23:07:47 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> child, tmp;
|
2002-03-11 16:11:48 +03:00
|
|
|
nsresult res;
|
2002-04-06 23:07:47 +04: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);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-04-06 23:07:47 +04:00
|
|
|
res = aNewBlock->GetFirstChild(getter_AddRefs(child));
|
|
|
|
}
|
|
|
|
// now find and clone the styles
|
|
|
|
child = aPreviousBlock;
|
|
|
|
tmp = aPreviousBlock;
|
2002-03-11 16:11:48 +03:00
|
|
|
while (tmp) {
|
|
|
|
child = tmp;
|
|
|
|
res = GetLastEditableChild(child, address_of(tmp));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-11 16:11:48 +03:00
|
|
|
}
|
|
|
|
while (child && nsTextEditUtils::IsBreak(child)) {
|
|
|
|
nsCOMPtr<nsIDOMNode> priorNode;
|
|
|
|
res = GetPriorHTMLNode(child, address_of(priorNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-11 16:11:48 +03:00
|
|
|
child = priorNode;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDOMNode> newStyles = nsnull, deepestStyle = nsnull;
|
|
|
|
while (child && (child != aPreviousBlock)) {
|
2003-07-29 00:31:08 +04:00
|
|
|
if (nsHTMLEditUtils::IsInlineStyle(child) ||
|
2003-07-15 17:47:15 +04:00
|
|
|
nsEditor::NodeIsType(child, nsEditProperty::span)) {
|
2002-03-11 16:11:48 +03:00
|
|
|
nsAutoString domTagName;
|
|
|
|
child->GetNodeName(domTagName);
|
|
|
|
ToLowerCase(domTagName);
|
|
|
|
if (newStyles) {
|
|
|
|
nsCOMPtr<nsIDOMNode> newContainer;
|
|
|
|
res = InsertContainerAbove(newStyles, address_of(newContainer), domTagName);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-11 16:11:48 +03:00
|
|
|
newStyles = newContainer;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
res = CreateNode(domTagName, aNewBlock, 0, getter_AddRefs(newStyles));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-11 16:11:48 +03:00
|
|
|
deepestStyle = newStyles;
|
|
|
|
}
|
|
|
|
res = CloneAttributes(newStyles, child);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-11 16:11:48 +03:00
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
res = child->GetParentNode(getter_AddRefs(tmp));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-11 16:11:48 +03:00
|
|
|
child = tmp;
|
|
|
|
}
|
|
|
|
if (deepestStyle) {
|
|
|
|
nsCOMPtr<nsIDOMNode> outBRNode;
|
|
|
|
res = CreateBR(deepestStyle, 0, address_of(outBRNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-11 16:11:48 +03:00
|
|
|
// Getters must addref
|
2012-02-10 14:04:46 +04:00
|
|
|
outBRNode.forget(aOutBrNode);
|
2002-03-11 16:11:48 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-02-24 12:37:18 +03:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetElementOrigin(nsIDOMElement * aElement, PRInt32 & aX, PRInt32 & aY)
|
|
|
|
{
|
2008-01-22 10:39:06 +03:00
|
|
|
aX = 0;
|
|
|
|
aY = 0;
|
|
|
|
|
2011-03-11 08:40:30 +03:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, NS_ERROR_NOT_INITIALIZED);
|
2011-05-22 16:43:13 +04:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
|
2003-02-24 12:37:18 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
|
2009-12-25 00:20:06 +03:00
|
|
|
nsIFrame *frame = content->GetPrimaryFrame();
|
2010-08-26 04:20:32 +04:00
|
|
|
NS_ENSURE_TRUE(frame, NS_OK);
|
2003-02-24 12:37:18 +03:00
|
|
|
|
2008-01-22 10:39:06 +03:00
|
|
|
nsIFrame *container = ps->GetAbsoluteContainingBlock(frame);
|
2010-08-26 04:20:32 +04:00
|
|
|
NS_ENSURE_TRUE(container, NS_OK);
|
2008-01-22 10:39:06 +03:00
|
|
|
nsPoint off = frame->GetOffsetTo(container);
|
|
|
|
aX = nsPresContext::AppUnitsToIntCSSPixels(off.x);
|
|
|
|
aY = nsPresContext::AppUnitsToIntCSSPixels(off.y);
|
2003-02-24 12:37:18 +03:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-25 18:24:08 +03:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::EndUpdateViewBatch()
|
|
|
|
{
|
|
|
|
nsresult res = nsEditor::EndUpdateViewBatch();
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-03-25 18:24:08 +03:00
|
|
|
|
|
|
|
// 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));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NOT_INITIALIZED);
|
2003-06-25 12:50:48 +04:00
|
|
|
res = CheckSelectionStateForAnonymousButtons(selection);
|
2003-03-25 18:24:08 +03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-06-25 12:50:48 +04:00
|
|
|
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;
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool bCollapsed;
|
2003-06-25 12:50:48 +04:00
|
|
|
res = selection->GetIsCollapsed(&bCollapsed);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> focusNode;
|
|
|
|
|
|
|
|
if (bCollapsed) {
|
|
|
|
res = selection->GetFocusNode(getter_AddRefs(focusNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
|
|
|
|
PRInt32 rangeCount;
|
|
|
|
res = selection->GetRangeCount(&rangeCount);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
if (rangeCount == 1) {
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
res = selection->GetRangeAt(0, getter_AddRefs(range));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(range, NS_ERROR_NULL_POINTER);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> startContainer, endContainer;
|
|
|
|
res = range->GetStartContainer(getter_AddRefs(startContainer));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 12:50:48 +04:00
|
|
|
res = range->GetEndContainer(getter_AddRefs(endContainer));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 12:50:48 +04:00
|
|
|
PRInt32 startOffset, endOffset;
|
|
|
|
res = range->GetStartOffset(&startOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 12:50:48 +04:00
|
|
|
res = range->GetEndOffset(&endOffset);
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 12:50:48 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> focusElement;
|
|
|
|
if (startContainer == endContainer && startOffset + 1 == endOffset) {
|
2004-05-23 00:02:43 +04:00
|
|
|
res = GetSelectedElement(EmptyString(), getter_AddRefs(focusElement));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 12:50:48 +04:00
|
|
|
if (focusElement)
|
|
|
|
focusNode = do_QueryInterface(focusElement);
|
|
|
|
}
|
|
|
|
if (!focusNode) {
|
|
|
|
res = range->GetCommonAncestorContainer(getter_AddRefs(focusNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 12:50:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
PRInt32 i;
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
for (i = 0; i < rangeCount; i++)
|
|
|
|
{
|
|
|
|
res = selection->GetRangeAt(i, getter_AddRefs(range));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 12:50:48 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> startContainer;
|
2009-03-30 13:58:42 +04:00
|
|
|
res = range->GetStartContainer(getter_AddRefs(startContainer));
|
|
|
|
if (NS_FAILED(res)) continue;
|
2003-06-25 12:50:48 +04:00
|
|
|
if (!focusNode)
|
|
|
|
focusNode = startContainer;
|
|
|
|
else if (focusNode != startContainer) {
|
|
|
|
res = startContainer->GetParentNode(getter_AddRefs(focusNode));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 12:50:48 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (focusNode) {
|
|
|
|
PRUint16 nodeType;
|
|
|
|
focusNode->GetNodeType(&nodeType);
|
|
|
|
if (nsIDOMNode::TEXT_NODE == nodeType) {
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
res = focusNode->GetParentNode(getter_AddRefs(parent));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 12:50:48 +04:00
|
|
|
focusNode = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> focusElement = do_QueryInterface(focusNode);
|
2012-02-10 14:04:46 +04:00
|
|
|
focusElement.forget(aReturn);
|
2003-06-25 12:50:48 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-06-19 18:06:05 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::IsAnonymousElement(nsIDOMElement * aElement, bool * aReturn)
|
2003-06-19 18:06:05 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aElement, NS_ERROR_NULL_POINTER);
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
|
2008-07-23 08:50:20 +04:00
|
|
|
*aReturn = content->IsRootOfNativeAnonymousSubtree();
|
2003-06-19 18:06:05 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-06-25 12:50:48 +04:00
|
|
|
|
2005-01-12 22:11:48 +03:00
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::SetReturnInParagraphCreatesNewParagraph(bool aCreatesNewParagraph)
|
2005-01-12 22:11:48 +03:00
|
|
|
{
|
|
|
|
mCRInParagraphCreatesParagraph = aCreatesNewParagraph;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::GetReturnInParagraphCreatesNewParagraph(bool *aCreatesNewParagraph)
|
2005-01-12 22:11:48 +03:00
|
|
|
{
|
|
|
|
*aCreatesNewParagraph = mCRInParagraphCreatesParagraph;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-05-04 21:40:39 +04:00
|
|
|
|
2010-10-29 20:30:52 +04:00
|
|
|
already_AddRefed<nsIContent>
|
|
|
|
nsHTMLEditor::GetFocusedContent()
|
2010-05-04 21:40:39 +04:00
|
|
|
{
|
2010-10-29 20:30:52 +04:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, nsnull);
|
2010-05-04 21:40:39 +04:00
|
|
|
|
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
2010-10-29 20:30:52 +04:00
|
|
|
NS_ENSURE_TRUE(fm, nsnull);
|
2010-05-04 21:40:39 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> focusedContent = fm->GetFocusedContent();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool inDesignMode = doc->HasFlag(NODE_IS_EDITABLE);
|
2010-05-04 21:40:39 +04:00
|
|
|
if (!focusedContent) {
|
|
|
|
// in designMode, nobody gets focus in most cases.
|
2010-10-29 20:30:52 +04:00
|
|
|
if (inDesignMode && OurWindowHasFocus()) {
|
|
|
|
nsCOMPtr<nsIContent> docRoot = doc->GetRootElement();
|
|
|
|
return docRoot.forget();
|
|
|
|
}
|
|
|
|
return nsnull;
|
2010-05-04 21:40:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (inDesignMode) {
|
2010-10-29 20:30:52 +04:00
|
|
|
return OurWindowHasFocus() &&
|
|
|
|
nsContentUtils::ContentIsDescendantOf(focusedContent, doc) ?
|
|
|
|
focusedContent.forget() : nsnull;
|
2010-05-04 21:40:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// We're HTML editor for contenteditable
|
|
|
|
|
|
|
|
// If the focused content isn't editable, or it has independent selection,
|
|
|
|
// we don't have focus.
|
|
|
|
if (!focusedContent->HasFlag(NODE_IS_EDITABLE) ||
|
2010-07-22 20:22:44 +04:00
|
|
|
focusedContent->HasIndependentSelection()) {
|
2010-10-29 20:30:52 +04:00
|
|
|
return nsnull;
|
2010-05-04 21:40:39 +04:00
|
|
|
}
|
2010-05-28 07:02:59 +04:00
|
|
|
// If our window is focused, we're focused.
|
2010-10-29 20:30:52 +04:00
|
|
|
return OurWindowHasFocus() ? focusedContent.forget() : nsnull;
|
2010-05-04 21:40:39 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-07-22 20:22:44 +04:00
|
|
|
nsHTMLEditor::IsActiveInDOMWindow()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, false);
|
2010-07-22 20:22:44 +04:00
|
|
|
|
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(fm, false);
|
2010-07-22 20:22:44 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool inDesignMode = doc->HasFlag(NODE_IS_EDITABLE);
|
2010-07-22 20:22:44 +04:00
|
|
|
|
|
|
|
// If we're in designMode, we're always active in the DOM window.
|
|
|
|
if (inDesignMode) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-07-22 20:22:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindow* ourWindow = doc->GetWindow();
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win;
|
|
|
|
nsIContent* content =
|
2011-10-17 18:59:28 +04:00
|
|
|
nsFocusManager::GetFocusedDescendant(ourWindow, false,
|
2010-07-22 20:22:44 +04:00
|
|
|
getter_AddRefs(win));
|
|
|
|
if (!content) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-07-22 20:22:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// We're HTML editor for contenteditable
|
|
|
|
|
|
|
|
// If the active content isn't editable, or it has independent selection,
|
|
|
|
// we're not active).
|
|
|
|
if (!content->HasFlag(NODE_IS_EDITABLE) ||
|
|
|
|
content->HasIndependentSelection()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-07-22 20:22:44 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-07-22 20:22:44 +04:00
|
|
|
}
|
|
|
|
|
2012-05-06 11:53:11 +04:00
|
|
|
dom::Element*
|
2011-07-14 19:25:07 +04:00
|
|
|
nsHTMLEditor::GetActiveEditingHost()
|
|
|
|
{
|
2011-12-18 10:00:47 +04:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, nsnull);
|
2011-07-14 19:25:07 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
|
|
|
NS_ENSURE_TRUE(doc, nsnull);
|
|
|
|
if (doc->HasFlag(NODE_IS_EDITABLE)) {
|
|
|
|
return doc->GetBodyElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're HTML editor for contenteditable
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult rv = GetSelection(getter_AddRefs(selection));
|
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
nsCOMPtr<nsIDOMNode> focusNode;
|
|
|
|
rv = selection->GetFocusNode(getter_AddRefs(focusNode));
|
|
|
|
NS_ENSURE_SUCCESS(rv, nsnull);
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(focusNode);
|
|
|
|
if (!content) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the active content isn't editable, or it has independent selection,
|
|
|
|
// we're not active.
|
|
|
|
if (!content->HasFlag(NODE_IS_EDITABLE) ||
|
|
|
|
content->HasIndependentSelection()) {
|
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
return content->GetEditingHost();
|
|
|
|
}
|
|
|
|
|
2011-06-24 06:18:00 +04:00
|
|
|
already_AddRefed<nsIDOMEventTarget>
|
2011-06-24 06:18:01 +04:00
|
|
|
nsHTMLEditor::GetDOMEventTarget()
|
2010-06-17 09:30:10 +04:00
|
|
|
{
|
|
|
|
// Don't use getDocument here, because we have no way of knowing
|
|
|
|
// whether Init() was ever called. So we need to get the document
|
|
|
|
// ourselves, if it exists.
|
|
|
|
NS_PRECONDITION(mDocWeak, "This editor has not been initialized yet");
|
2011-06-24 06:18:01 +04:00
|
|
|
nsCOMPtr<nsIDOMEventTarget> target = do_QueryReferent(mDocWeak.get());
|
|
|
|
return target.forget();
|
2010-06-17 09:30:10 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-06-17 09:32:05 +04:00
|
|
|
nsHTMLEditor::ShouldReplaceRootElement()
|
|
|
|
{
|
|
|
|
if (!mRootElement) {
|
|
|
|
// If we don't know what is our root element, we should find our root.
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2010-06-17 09:32:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we temporary set document root element to mRootElement, but there is
|
|
|
|
// body element now, we should replace the root element by the body element.
|
|
|
|
nsCOMPtr<nsIDOMHTMLElement> docBody;
|
|
|
|
GetBodyElement(getter_AddRefs(docBody));
|
|
|
|
return !SameCOMIdentity(docBody, mRootElement);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHTMLEditor::ResetRootElementAndEventTarget()
|
|
|
|
{
|
2010-07-21 19:33:32 +04:00
|
|
|
nsCOMPtr<nsIMutationObserver> kungFuDeathGrip(this);
|
|
|
|
|
2010-06-17 09:32:05 +04:00
|
|
|
// Need to remove the event listeners first because BeginningOfDocument
|
|
|
|
// could set a new root (and event target is set by InstallEventListeners())
|
|
|
|
// and we won't be able to remove them from the old event target then.
|
|
|
|
RemoveEventListeners();
|
|
|
|
mRootElement = nsnull;
|
|
|
|
nsresult rv = InstallEventListeners();
|
2012-01-11 12:22:16 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-19 14:39:39 +04:00
|
|
|
|
|
|
|
// We must have mRootElement now.
|
|
|
|
nsCOMPtr<nsIDOMElement> root;
|
|
|
|
rv = GetRootElement(getter_AddRefs(root));
|
2012-01-11 12:22:16 +04:00
|
|
|
if (NS_FAILED(rv) || !mRootElement) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-17 09:32:05 +04:00
|
|
|
|
|
|
|
rv = BeginningOfDocument();
|
2012-01-11 12:22:16 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-17 09:32:05 +04:00
|
|
|
|
|
|
|
// When this editor has focus, we need to reset the selection limiter to
|
|
|
|
// new root. Otherwise, that is going to be done when this gets focus.
|
|
|
|
nsCOMPtr<nsINode> node = GetFocusedNode();
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target = do_QueryInterface(node);
|
|
|
|
if (target) {
|
|
|
|
InitializeSelection(target);
|
|
|
|
}
|
|
|
|
|
|
|
|
SyncRealTimeSpell();
|
|
|
|
}
|
|
|
|
|
2010-06-17 09:29:40 +04:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetBodyElement(nsIDOMHTMLElement** aBody)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(mDocWeak, "bad state, null mDocWeak");
|
|
|
|
nsCOMPtr<nsIDOMHTMLDocument> htmlDoc = do_QueryReferent(mDocWeak);
|
|
|
|
if (!htmlDoc) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
return htmlDoc->GetBody(aBody);
|
|
|
|
}
|
|
|
|
|
2010-06-17 09:32:05 +04:00
|
|
|
already_AddRefed<nsINode>
|
|
|
|
nsHTMLEditor::GetFocusedNode()
|
|
|
|
{
|
2010-10-29 20:30:52 +04:00
|
|
|
nsCOMPtr<nsIContent> focusedContent = GetFocusedContent();
|
|
|
|
if (!focusedContent) {
|
2010-06-17 09:32:05 +04:00
|
|
|
return nsnull;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
NS_ASSERTION(fm, "Focus manager is null");
|
|
|
|
nsCOMPtr<nsIDOMElement> focusedElement;
|
|
|
|
fm->GetFocusedElement(getter_AddRefs(focusedElement));
|
|
|
|
if (focusedElement) {
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(focusedElement);
|
|
|
|
return node.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
2012-02-10 14:04:46 +04:00
|
|
|
return doc.forget();
|
2010-06-17 09:32:05 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-05-04 21:40:39 +04:00
|
|
|
nsHTMLEditor::OurWindowHasFocus()
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, false);
|
2010-05-04 21:40:39 +04:00
|
|
|
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(fm, false);
|
2010-05-04 21:40:39 +04:00
|
|
|
nsCOMPtr<nsIDOMWindow> focusedWindow;
|
|
|
|
fm->GetFocusedWindow(getter_AddRefs(focusedWindow));
|
|
|
|
if (!focusedWindow) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-05-04 21:40:39 +04:00
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
|
|
|
nsCOMPtr<nsIDOMWindow> ourWindow = do_QueryInterface(doc->GetWindow());
|
|
|
|
return ourWindow == focusedWindow;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2010-07-22 20:22:44 +04:00
|
|
|
nsHTMLEditor::IsAcceptableInputEvent(nsIDOMEvent* aEvent)
|
2010-05-04 21:40:39 +04:00
|
|
|
{
|
2010-07-22 20:22:44 +04:00
|
|
|
if (!nsEditor::IsAcceptableInputEvent(aEvent)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-07-22 20:22:44 +04:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, false);
|
2010-07-22 20:22:44 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target;
|
|
|
|
aEvent->GetTarget(getter_AddRefs(target));
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(target, false);
|
2010-07-22 20:22:44 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> document = do_QueryReferent(mDocWeak);
|
|
|
|
if (document->HasFlag(NODE_IS_EDITABLE)) {
|
|
|
|
// If this editor is in designMode and the event target is the document,
|
|
|
|
// the event is for this editor.
|
|
|
|
nsCOMPtr<nsIDocument> targetDocument = do_QueryInterface(target);
|
|
|
|
if (targetDocument) {
|
|
|
|
return targetDocument == document;
|
|
|
|
}
|
|
|
|
// Otherwise, check whether the event target is in this document or not.
|
|
|
|
nsCOMPtr<nsIContent> targetContent = do_QueryInterface(target);
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(targetContent, false);
|
2010-07-22 20:22:44 +04:00
|
|
|
return document == targetContent->GetCurrentDoc();
|
|
|
|
}
|
|
|
|
|
2011-11-26 08:51:48 +04:00
|
|
|
// This HTML editor is for contenteditable. We need to check the validity of
|
|
|
|
// the target.
|
2010-07-22 20:22:44 +04:00
|
|
|
nsCOMPtr<nsIContent> targetContent = do_QueryInterface(target);
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(targetContent, false);
|
2011-11-26 08:51:48 +04:00
|
|
|
|
|
|
|
// If the event is a mouse event, we need to check if the target content is
|
|
|
|
// the focused editing host or its descendant.
|
|
|
|
nsCOMPtr<nsIDOMMouseEvent> mouseEvent = do_QueryInterface(aEvent);
|
|
|
|
if (mouseEvent) {
|
|
|
|
nsIContent* editingHost = GetActiveEditingHost();
|
|
|
|
// If there is no active editing host, we cannot handle the mouse event
|
|
|
|
// correctly.
|
|
|
|
if (!editingHost) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If clicked on non-editable root element but the body element is the
|
|
|
|
// active editing host, we should assume that the click event is targetted.
|
|
|
|
if (targetContent == document->GetRootElement() &&
|
|
|
|
!targetContent->HasFlag(NODE_IS_EDITABLE) &&
|
|
|
|
editingHost == document->GetBodyElement()) {
|
|
|
|
targetContent = editingHost;
|
|
|
|
}
|
|
|
|
// If the target element is neither the active editing host nor a descendant
|
|
|
|
// of it, we may not be able to handle the event.
|
|
|
|
if (!nsContentUtils::ContentIsDescendantOf(targetContent, editingHost)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If the clicked element has an independent selection, we shouldn't
|
|
|
|
// handle this click event.
|
|
|
|
if (targetContent->HasIndependentSelection()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// If the target content is editable, we should handle this event.
|
|
|
|
return targetContent->HasFlag(NODE_IS_EDITABLE);
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the target of the other events which target focused element isn't
|
|
|
|
// editable or has an independent selection, this editor shouldn't handle the
|
|
|
|
// event.
|
2010-07-22 20:22:44 +04:00
|
|
|
if (!targetContent->HasFlag(NODE_IS_EDITABLE) ||
|
|
|
|
targetContent->HasIndependentSelection()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2010-07-22 20:22:44 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally, check whether we're actually focused or not. When we're not
|
|
|
|
// focused, we should ignore the dispatched event by script (or something)
|
|
|
|
// because content editable element needs selection in itself for editing.
|
|
|
|
// However, when we're not focused, it's not guaranteed.
|
|
|
|
return IsActiveInDOMWindow();
|
2010-05-04 21:40:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-11-27 15:51:53 +04:00
|
|
|
nsHTMLEditor::GetPreferredIMEState(IMEState *aState)
|
2010-05-04 21:40:39 +04:00
|
|
|
{
|
2011-11-27 15:51:53 +04:00
|
|
|
// HTML editor don't prefer the CSS ime-mode because IE didn't do so too.
|
|
|
|
aState->mOpen = IMEState::DONT_CHANGE_OPEN_STATE;
|
2010-05-04 21:40:39 +04:00
|
|
|
if (IsReadonly() || IsDisabled()) {
|
2011-11-27 15:51:53 +04:00
|
|
|
aState->mEnabled = IMEState::DISABLED;
|
|
|
|
} else {
|
|
|
|
aState->mEnabled = IMEState::ENABLED;
|
2010-05-04 21:40:39 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-27 05:36:44 +04:00
|
|
|
|
|
|
|
already_AddRefed<nsIContent>
|
|
|
|
nsHTMLEditor::GetInputEventTargetContent()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> target = GetActiveEditingHost();
|
|
|
|
return target.forget();
|
|
|
|
}
|
2012-05-06 11:53:11 +04:00
|
|
|
|
2010-11-16 23:45:49 +03:00
|
|
|
bool
|
|
|
|
nsHTMLEditor::IsEditable(nsIContent* aNode) {
|
|
|
|
if (!nsPlaintextEditor::IsEditable(aNode)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (aNode->IsElement()) {
|
|
|
|
// If we're dealing with an element, then ask it whether it's editable.
|
|
|
|
return aNode->IsEditable();
|
|
|
|
}
|
|
|
|
// We might be dealing with a text node for example, which we always consider
|
|
|
|
// to be editable.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-05-06 11:53:11 +04:00
|
|
|
// virtual MOZ_OVERRIDE
|
|
|
|
dom::Element*
|
|
|
|
nsHTMLEditor::GetEditorRoot()
|
|
|
|
{
|
|
|
|
return GetActiveEditingHost();
|
|
|
|
}
|