2001-09-25 22:53:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2012-05-21 11:12:37 +00:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2012-12-14 23:58:45 +00:00
|
|
|
|
2014-12-02 05:07:42 +00:00
|
|
|
#include "nsHTMLEditor.h"
|
|
|
|
|
2012-12-14 23:58:45 +00:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2014-04-03 04:18:36 +00:00
|
|
|
#include "mozilla/EventStates.h"
|
2013-09-25 11:21:19 +00:00
|
|
|
#include "mozilla/TextEvents.h"
|
2012-12-14 23:58:45 +00:00
|
|
|
|
2003-03-15 01:04:32 +00:00
|
|
|
#include "nsCRT.h"
|
1999-03-01 19:54:47 +00:00
|
|
|
|
2001-10-13 00:16:32 +00:00
|
|
|
#include "nsUnicharUtils.h"
|
1999-08-09 01:37:50 +00:00
|
|
|
|
1999-03-29 08:02:05 +00:00
|
|
|
#include "nsHTMLEditRules.h"
|
2001-04-05 23:48:01 +00:00
|
|
|
#include "nsTextEditUtils.h"
|
2000-01-26 00:57:37 +00:00
|
|
|
#include "nsHTMLEditUtils.h"
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2010-03-06 04:01:28 +00:00
|
|
|
#include "nsHTMLEditorEventListener.h"
|
2001-04-05 23:48:01 +00:00
|
|
|
#include "TypeInState.h"
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2001-08-07 21:39:07 +00:00
|
|
|
#include "nsHTMLURIRefObject.h"
|
|
|
|
|
1999-07-04 23:01:10 +00:00
|
|
|
#include "nsIDOMText.h"
|
2013-03-10 08:00:33 +00:00
|
|
|
#include "nsIDOMMozNamedAttrMap.h"
|
1999-04-08 00:46:10 +00:00
|
|
|
#include "nsIDOMNodeList.h"
|
1999-03-01 19:54:47 +00:00
|
|
|
#include "nsIDOMDocument.h"
|
2000-03-24 00:26:47 +00:00
|
|
|
#include "nsIDOMAttr.h"
|
2013-01-12 23:20:57 +00:00
|
|
|
#include "nsIDocumentInlines.h"
|
2007-05-14 09:11:38 +00:00
|
|
|
#include "nsIDOMEventTarget.h"
|
1999-11-03 07:11:45 +00:00
|
|
|
#include "nsIDOMKeyEvent.h"
|
2013-09-25 11:21:18 +00:00
|
|
|
#include "nsIDOMMouseEvent.h"
|
1999-03-29 22:01:26 +00:00
|
|
|
#include "nsIDOMHTMLAnchorElement.h"
|
1999-12-14 23:07:12 +00:00
|
|
|
#include "nsISelectionController.h"
|
2010-06-17 05:29:40 +00:00
|
|
|
#include "nsIDOMHTMLDocument.h"
|
2005-06-09 22:50:41 +00:00
|
|
|
#include "nsILinkHandler.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsIInlineSpellChecker.h"
|
2000-05-13 08:04:29 +00:00
|
|
|
|
2014-06-20 10:32:49 +00:00
|
|
|
#include "mozilla/CSSStyleSheet.h"
|
2010-06-28 22:49:35 +00:00
|
|
|
#include "mozilla/css/Loader.h"
|
2007-01-11 19:32:31 +00:00
|
|
|
#include "nsIDOMStyleSheet.h"
|
1999-08-09 01:37:50 +00:00
|
|
|
|
1999-04-21 18:53:55 +00:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIContentIterator.h"
|
1999-04-30 22:40:18 +00:00
|
|
|
#include "nsISupportsArray.h"
|
2007-02-24 20:07:06 +00:00
|
|
|
#include "nsContentUtils.h"
|
1999-06-07 19:32:36 +00:00
|
|
|
#include "nsIDocumentEncoder.h"
|
1999-06-10 00:35:02 +00:00
|
|
|
#include "nsIPresShell.h"
|
2004-07-31 23:15:21 +00:00
|
|
|
#include "nsPresContext.h"
|
2001-02-26 22:02:58 +00:00
|
|
|
#include "SetDocTitleTxn.h"
|
2010-05-04 17:40:39 +00:00
|
|
|
#include "nsFocusManager.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
2002-05-14 00:05:37 +00:00
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
// netwerk
|
|
|
|
#include "nsIURI.h"
|
1999-11-30 04:50:42 +00:00
|
|
|
#include "nsNetUtil.h"
|
1999-07-19 19:37:08 +00:00
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
// Transactionas
|
|
|
|
#include "nsStyleSheetTxns.h"
|
|
|
|
|
|
|
|
// Misc
|
|
|
|
#include "TextEditorTest.h"
|
|
|
|
#include "nsEditorUtils.h"
|
2002-04-14 02:37:23 +00:00
|
|
|
#include "nsWSRunObject.h"
|
2008-03-13 18:54:01 +00:00
|
|
|
#include "nsGkAtoms.h"
|
2011-11-27 11:51:53 +00:00
|
|
|
#include "nsIWidget.h"
|
2003-02-24 09:37:18 +00:00
|
|
|
|
|
|
|
#include "nsIFrame.h"
|
2005-06-16 13:10:58 +00:00
|
|
|
#include "nsIParserService.h"
|
2014-04-10 16:09:40 +00:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2014-04-28 11:54:46 +00:00
|
|
|
#include "mozilla/dom/DocumentFragment.h"
|
2011-07-20 19:18:54 +00:00
|
|
|
#include "mozilla/dom/Element.h"
|
2013-10-22 23:32:04 +00:00
|
|
|
#include "mozilla/dom/EventTarget.h"
|
2013-01-12 21:53:01 +00:00
|
|
|
#include "mozilla/dom/HTMLBodyElement.h"
|
2012-07-01 11:07:16 +00:00
|
|
|
#include "nsTextFragment.h"
|
2014-04-25 10:34:42 +00:00
|
|
|
#include "nsContentList.h"
|
2000-05-13 08:04:29 +00:00
|
|
|
|
2011-11-16 07:50:20 +00:00
|
|
|
using namespace mozilla;
|
2014-04-10 16:09:40 +00:00
|
|
|
using namespace mozilla::dom;
|
2011-11-27 11:51:53 +00:00
|
|
|
using namespace mozilla::widget;
|
2011-11-16 07:50:20 +00:00
|
|
|
|
2014-05-07 04:29:20 +00:00
|
|
|
// Some utilities to handle overloading of "A" tag for link and named anchor.
|
|
|
|
static bool
|
|
|
|
IsLinkTag(const nsString& s)
|
|
|
|
{
|
|
|
|
return s.EqualsIgnoreCase("href");
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
IsNamedAnchorTag(const nsString& s)
|
|
|
|
{
|
|
|
|
return s.EqualsIgnoreCase("anchor") || s.EqualsIgnoreCase("namedanchor");
|
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
|
1999-03-01 19:54:47 +00:00
|
|
|
nsHTMLEditor::nsHTMLEditor()
|
2001-01-28 20:13:07 +00:00
|
|
|
: nsPlaintextEditor()
|
2011-10-17 14:59:28 +00:00
|
|
|
, mCRInParagraphCreatesParagraph(false)
|
2005-01-12 19:11:48 +00:00
|
|
|
, mSelectedCellIndex(0)
|
2011-10-17 14:59:28 +00:00
|
|
|
, mIsObjectResizingEnabled(true)
|
|
|
|
, mIsResizing(false)
|
|
|
|
, mIsAbsolutelyPositioningEnabled(true)
|
|
|
|
, mResizedObjectIsAbsolutelyPositioned(false)
|
|
|
|
, mGrabberClicked(false)
|
|
|
|
, mIsMoving(false)
|
|
|
|
, mSnapToGridEnabled(false)
|
|
|
|
, mIsInlineTableEditingEnabled(true)
|
2007-08-26 05:53:38 +00:00
|
|
|
, mInfoXIncrement(20)
|
|
|
|
, mInfoYIncrement(20)
|
2008-02-19 07:36:50 +00:00
|
|
|
, mGridSize(0)
|
1999-03-01 19:54:47 +00:00
|
|
|
{
|
1999-06-10 00:35:02 +00:00
|
|
|
}
|
1999-03-01 19:54:47 +00:00
|
|
|
|
|
|
|
nsHTMLEditor::~nsHTMLEditor()
|
|
|
|
{
|
2005-03-24 19:00:01 +00:00
|
|
|
// remove the rules as an action listener. Else we get a bad
|
|
|
|
// ownership loop later on. it's ok if the rules aren't a listener;
|
|
|
|
// we ignore the error.
|
2000-03-24 00:26:47 +00:00
|
|
|
nsCOMPtr<nsIEditActionListener> mListener = do_QueryInterface(mRules);
|
|
|
|
RemoveEditActionListener(mListener);
|
2005-07-13 16:45:58 +00:00
|
|
|
|
1999-03-01 19:54:47 +00:00
|
|
|
//the autopointers will clear themselves up.
|
1999-08-09 01:37:50 +00:00
|
|
|
//but we need to also remove the listeners or we have a leak
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
1999-09-29 20:08:15 +00:00
|
|
|
// if we don't get the selection, just skip this
|
2014-11-02 12:04:13 +00:00
|
|
|
if (selection) {
|
2000-09-14 11:45:01 +00:00
|
|
|
nsCOMPtr<nsISelectionListener>listener;
|
1999-08-09 01:37:50 +00:00
|
|
|
listener = do_QueryInterface(mTypeInState);
|
2005-06-09 22:50:41 +00:00
|
|
|
if (listener)
|
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
selection->RemoveSelectionListener(listener);
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
2003-02-24 09:37:18 +00:00
|
|
|
listener = do_QueryInterface(mSelectionListenerP);
|
2005-06-09 22:50:41 +00:00
|
|
|
if (listener)
|
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
selection->RemoveSelectionListener(listener);
|
2003-02-24 09:37:18 +00:00
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
1999-03-05 21:05:35 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mTypeInState = nullptr;
|
|
|
|
mSelectionListenerP = nullptr;
|
2002-06-11 20:43:49 +00:00
|
|
|
|
2003-06-02 22:21:06 +00:00
|
|
|
// free any default style propItems
|
|
|
|
RemoveAllDefaultProperties();
|
2005-03-24 19:00:01 +00:00
|
|
|
|
2011-03-11 05:40:30 +00:00
|
|
|
if (mLinkHandler && mDocWeak)
|
2005-06-09 22:50:41 +00:00
|
|
|
{
|
2011-05-22 12:43:13 +00:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2005-06-09 22:50:41 +00:00
|
|
|
|
|
|
|
if (ps && ps->GetPresContext())
|
|
|
|
{
|
|
|
|
ps->GetPresContext()->SetLinkHandler(mLinkHandler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-03-24 19:00:01 +00:00
|
|
|
RemoveEventListeners();
|
1999-03-05 21:05:35 +00:00
|
|
|
}
|
|
|
|
|
2011-02-20 05:43:07 +00:00
|
|
|
void
|
|
|
|
nsHTMLEditor::HideAnonymousEditingUIs()
|
|
|
|
{
|
|
|
|
if (mAbsolutelyPositionedObject)
|
|
|
|
HideGrabber();
|
|
|
|
if (mInlineEditedCell)
|
|
|
|
HideInlineTableEditingUI();
|
|
|
|
if (mResizedObject)
|
|
|
|
HideResizers();
|
|
|
|
}
|
|
|
|
|
2013-08-02 01:29:05 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsHTMLEditor)
|
|
|
|
|
2011-02-16 12:07:47 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN_INHERITED(nsHTMLEditor, nsPlaintextEditor)
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mTypeInState)
|
2013-04-05 17:03:56 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mStyleSheets)
|
2011-02-16 12:07:47 +00:00
|
|
|
|
2011-02-20 05:43:07 +00:00
|
|
|
tmp->HideAnonymousEditingUIs();
|
2011-02-16 12:07:47 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN_INHERITED(nsHTMLEditor, nsPlaintextEditor)
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTypeInState)
|
2013-04-05 17:03:56 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStyleSheets)
|
2012-11-15 07:32:40 +00:00
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTopLeftHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTopHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mTopRightHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mLeftHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRightHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBottomLeftHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBottomHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mBottomRightHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mActivatedHandle)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResizingShadow)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResizingInfo)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResizedObject)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mMouseMotionListenerP)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSelectionListenerP)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mResizeEventListenerP)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(objectResizeEventListeners)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAbsolutelyPositionedObject)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mGrabber)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPositioningShadow)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mInlineEditedCell)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAddColumnBeforeButton)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRemoveColumnButton)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAddColumnAfterButton)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAddRowBeforeButton)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRemoveRowButton)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAddRowAfterButton)
|
2011-02-16 12:07:47 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
NS_IMPL_ADDREF_INHERITED(nsHTMLEditor, nsEditor)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsHTMLEditor, nsEditor)
|
|
|
|
|
2011-02-16 12:07:47 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION_INHERITED(nsHTMLEditor)
|
2003-06-25 08:50:48 +00: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 05:31:15 +00:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
|
2003-06-25 19:21:59 +00:00
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsPlaintextEditor)
|
2003-06-25 08:50:48 +00:00
|
|
|
|
1999-03-05 21:05:35 +00:00
|
|
|
|
2005-06-09 22:50:41 +00:00
|
|
|
NS_IMETHODIMP
|
2011-03-11 05:40:31 +00:00
|
|
|
nsHTMLEditor::Init(nsIDOMDocument *aDoc,
|
|
|
|
nsIContent *aRoot,
|
|
|
|
nsISelectionController *aSelCon,
|
2014-04-14 12:33:47 +00:00
|
|
|
uint32_t aFlags,
|
|
|
|
const nsAString& aInitialValue)
|
1999-03-01 19:54:47 +00:00
|
|
|
{
|
2011-03-11 05:40:31 +00:00
|
|
|
NS_PRECONDITION(aDoc && !aSelCon, "bad arg");
|
|
|
|
NS_ENSURE_TRUE(aDoc, NS_ERROR_NULL_POINTER);
|
2014-04-14 12:33:47 +00:00
|
|
|
MOZ_ASSERT(aInitialValue.IsEmpty(), "Non-empty initial values not supported");
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult result = NS_OK, rulesRes = NS_OK;
|
2001-05-11 12:43:22 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (1)
|
|
|
|
{
|
|
|
|
// block to scope nsAutoEditInitRulesTrigger
|
2007-07-08 07:08:04 +00:00
|
|
|
nsAutoEditInitRulesTrigger rulesTrigger(static_cast<nsPlaintextEditor*>(this), rulesRes);
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Init the plaintext editor
|
2014-04-14 12:33:47 +00:00
|
|
|
result = nsPlaintextEditor::Init(aDoc, aRoot, nullptr, aFlags, aInitialValue);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(result)) { return result; }
|
2000-05-15 05:18:45 +00:00
|
|
|
|
2010-06-17 05:31:15 +00:00
|
|
|
// Init mutation observer
|
|
|
|
nsCOMPtr<nsINode> document = do_QueryInterface(aDoc);
|
2010-12-09 02:44:15 +00:00
|
|
|
document->AddMutationObserverUnlessExists(this);
|
2010-06-17 05:31:15 +00:00
|
|
|
|
2003-06-25 08:50:48 +00:00
|
|
|
// disable Composer-only features
|
2010-04-12 02:35:18 +00:00
|
|
|
if (IsMailEditor())
|
2003-06-25 08:50:48 +00:00
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
SetAbsolutePositioningEnabled(false);
|
|
|
|
SetSnapToGridEnabled(false);
|
2003-06-25 08:50:48 +00:00
|
|
|
}
|
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
// Init the HTML-CSS utils
|
2011-05-15 10:07:30 +00:00
|
|
|
mHTMLCSSUtils = new nsHTMLCSSUtils(this);
|
2002-01-09 13:51:37 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// disable links
|
2011-05-22 12:43:13 +00:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = GetPresShell();
|
2011-03-11 05:40:31 +00:00
|
|
|
NS_ENSURE_TRUE(presShell, NS_ERROR_FAILURE);
|
|
|
|
nsPresContext *context = presShell->GetPresContext();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(context, NS_ERROR_NULL_POINTER);
|
2010-04-12 02:35:18 +00:00
|
|
|
if (!IsPlaintextEditor() && !IsInteractionAllowed()) {
|
2005-06-09 22:50:41 +00:00
|
|
|
mLinkHandler = context->GetLinkHandler();
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
context->SetLinkHandler(nullptr);
|
2005-06-09 22:50:41 +00:00
|
|
|
}
|
1999-11-11 19:22:30 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// init the type-in state
|
|
|
|
mTypeInState = new TypeInState();
|
|
|
|
|
2003-02-24 09:37:18 +00:00
|
|
|
// init the selection listener for image resizing
|
|
|
|
mSelectionListenerP = new ResizerSelectionListener(this);
|
|
|
|
|
2010-04-12 02:35:18 +00:00
|
|
|
if (!IsInteractionAllowed()) {
|
2007-06-28 02:48:16 +00:00
|
|
|
// ignore any errors from this in case the file is missing
|
2007-10-31 18:51:49 +00:00
|
|
|
AddOverrideStyleSheet(NS_LITERAL_STRING("resource://gre/res/EditorOverride.css"));
|
2007-06-28 02:48:16 +00:00
|
|
|
}
|
2003-04-03 08:05:23 +00:00
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2001-01-28 20:13:07 +00:00
|
|
|
if (selection)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelectionListener>listener;
|
|
|
|
listener = do_QueryInterface(mTypeInState);
|
|
|
|
if (listener) {
|
2014-11-02 12:04:13 +00:00
|
|
|
selection->AddSelectionListener(listener);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2003-02-24 09:37:18 +00:00
|
|
|
listener = do_QueryInterface(mSelectionListenerP);
|
|
|
|
if (listener) {
|
2014-11-02 12:04:13 +00:00
|
|
|
selection->AddSelectionListener(listener);
|
2003-02-24 09:37:18 +00:00
|
|
|
}
|
1999-07-19 22:49:21 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
1999-11-29 08:28:46 +00:00
|
|
|
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(rulesRes, rulesRes);
|
1999-08-23 06:42:04 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-06-17 05:31:15 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::PreDestroy(bool aDestroyingFrames)
|
2010-06-17 05:31:15 +00:00
|
|
|
{
|
|
|
|
if (mDidPreDestroy) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> document = do_QueryReferent(mDocWeak);
|
|
|
|
if (document) {
|
|
|
|
document->RemoveMutationObserver(this);
|
|
|
|
}
|
|
|
|
|
2011-06-24 17:55:43 +00:00
|
|
|
while (mStyleSheetURLs.Length())
|
|
|
|
{
|
|
|
|
RemoveOverrideStyleSheet(mStyleSheetURLs[0]);
|
|
|
|
}
|
|
|
|
|
2012-04-19 00:10:49 +00: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 05:31:15 +00:00
|
|
|
return nsPlaintextEditor::PreDestroy(aDestroyingFrames);
|
|
|
|
}
|
|
|
|
|
2010-06-17 05:29:40 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetRootElement(nsIDOMElement **aRootElement)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRootElement);
|
|
|
|
|
|
|
|
if (mRootElement) {
|
|
|
|
return nsEditor::GetRootElement(aRootElement);
|
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
*aRootElement = nullptr;
|
2010-06-17 05:29:40 +00:00
|
|
|
|
|
|
|
// Use the HTML documents body element as the editor root if we didn't
|
|
|
|
// get a root element during initialization.
|
|
|
|
|
2011-12-03 21:50:15 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> rootElement;
|
2010-06-17 05:29:40 +00:00
|
|
|
nsCOMPtr<nsIDOMHTMLElement> bodyElement;
|
|
|
|
nsresult rv = GetBodyElement(getter_AddRefs(bodyElement));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (bodyElement) {
|
2011-12-03 21:50:15 +00:00
|
|
|
rootElement = bodyElement;
|
2010-06-17 05:29:40 +00: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-03 21:50:15 +00:00
|
|
|
rv = doc->GetDocumentElement(getter_AddRefs(rootElement));
|
2010-06-17 05:29:40 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// Document can have no elements
|
2011-12-03 21:50:15 +00:00
|
|
|
if (!rootElement) {
|
2010-06-17 05:29:40 +00:00
|
|
|
return NS_ERROR_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-12-03 21:50:15 +00:00
|
|
|
mRootElement = do_QueryInterface(rootElement);
|
|
|
|
rootElement.forget(aRootElement);
|
2010-06-17 05:29:40 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-06-17 05:30:42 +00: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<nsIDocument> doc = aNode->GetCurrentDoc();
|
|
|
|
if (!doc) {
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2010-06-17 05:30:42 +00:00
|
|
|
}
|
|
|
|
|
2012-06-10 12:39:21 +00:00
|
|
|
nsCOMPtr<nsIContent> content;
|
|
|
|
if (doc->HasFlag(NODE_IS_EDITABLE) || !aNode->IsContent()) {
|
2010-06-17 05:30:42 +00:00
|
|
|
content = doc->GetRootElement();
|
|
|
|
return content.forget();
|
|
|
|
}
|
2012-06-10 12:39:21 +00:00
|
|
|
content = aNode->AsContent();
|
2010-06-17 05:30:42 +00:00
|
|
|
|
|
|
|
// 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 20:45:49 +00: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();
|
|
|
|
}
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2010-06-17 05:30:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// For non-readonly editors we want to find the root of the editable subtree
|
|
|
|
// containing aContent.
|
2010-07-22 16:22:44 +00:00
|
|
|
content = content->GetEditingHost();
|
2010-06-17 05:30:42 +00:00
|
|
|
return content.forget();
|
|
|
|
}
|
|
|
|
|
2011-07-15 10:18:35 +00:00
|
|
|
/* virtual */
|
|
|
|
void
|
2005-03-24 19:00:01 +00:00
|
|
|
nsHTMLEditor::CreateEventListeners()
|
1999-08-23 06:42:04 +00:00
|
|
|
{
|
2010-11-18 21:01:12 +00:00
|
|
|
// Don't create the handler twice
|
2011-06-27 23:34:54 +00:00
|
|
|
if (!mEventListener) {
|
|
|
|
mEventListener = new nsHTMLEditorEventListener();
|
|
|
|
}
|
2005-03-24 19:00:01 +00:00
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2010-04-19 12:20:42 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::InstallEventListeners()
|
|
|
|
{
|
2011-03-11 05:40:30 +00:00
|
|
|
NS_ENSURE_TRUE(mDocWeak && mEventListener,
|
2010-04-19 12:20:42 +00:00
|
|
|
NS_ERROR_NOT_INITIALIZED);
|
2010-06-17 05:30:10 +00: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 12:20:42 +00:00
|
|
|
nsHTMLEditorEventListener* listener =
|
|
|
|
reinterpret_cast<nsHTMLEditorEventListener*>(mEventListener.get());
|
|
|
|
return listener->Connect(this);
|
|
|
|
}
|
|
|
|
|
2005-03-24 19:00:01 +00:00
|
|
|
void
|
|
|
|
nsHTMLEditor::RemoveEventListeners()
|
|
|
|
{
|
2006-06-17 00:17:26 +00:00
|
|
|
if (!mDocWeak)
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-06-24 02:18:01 +00:00
|
|
|
nsCOMPtr<nsIDOMEventTarget> target = GetDOMEventTarget();
|
2000-05-04 08:33:48 +00:00
|
|
|
|
2011-06-24 02:18:01 +00:00
|
|
|
if (target)
|
2005-03-24 19:00:01 +00: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-27 23:34:54 +00:00
|
|
|
// mMouseMotionListenerP might be registerd either as bubbling or
|
|
|
|
// capturing, unregister by both.
|
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("mousemove"),
|
2011-10-17 14:59:28 +00:00
|
|
|
mMouseMotionListenerP, false);
|
2007-05-14 09:11:38 +00:00
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("mousemove"),
|
2011-10-17 14:59:28 +00:00
|
|
|
mMouseMotionListenerP, true);
|
2005-03-24 19:00:01 +00:00
|
|
|
}
|
2002-11-09 00:25:25 +00:00
|
|
|
|
2005-03-24 19:00:01 +00:00
|
|
|
if (mResizeEventListenerP)
|
1999-09-29 20:08:15 +00:00
|
|
|
{
|
2007-05-14 09:11:38 +00:00
|
|
|
target->RemoveEventListener(NS_LITERAL_STRING("resize"),
|
2011-10-17 14:59:28 +00:00
|
|
|
mResizeEventListenerP, false);
|
1999-09-29 20:08:15 +00:00
|
|
|
}
|
|
|
|
}
|
2005-03-24 19:00:01 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mMouseMotionListenerP = nullptr;
|
|
|
|
mResizeEventListenerP = nullptr;
|
2005-03-24 19:00:01 +00:00
|
|
|
|
|
|
|
nsPlaintextEditor::RemoveEventListeners();
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsHTMLEditor::SetFlags(uint32_t aFlags)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2010-04-12 02:35:18 +00:00
|
|
|
nsresult rv = nsPlaintextEditor::SetFlags(aFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2007-11-13 08:10:03 +00:00
|
|
|
|
2010-04-12 02:35:18 +00: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 15:16:14 +00:00
|
|
|
|
2010-04-12 02:35:18 +00:00
|
|
|
return NS_OK;
|
1999-03-01 19:54:47 +00:00
|
|
|
}
|
|
|
|
|
2005-06-09 22:50:41 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::InitRules()
|
1999-03-29 08:02:05 +00:00
|
|
|
{
|
2014-05-09 21:02:29 +00:00
|
|
|
if (!mRules) {
|
|
|
|
// instantiate the rules for the html editor
|
|
|
|
mRules = new nsHTMLEditRules();
|
|
|
|
}
|
2011-05-15 10:15:35 +00:00
|
|
|
return mRules->Init(static_cast<nsPlaintextEditor*>(this));
|
1999-03-29 08:02:05 +00:00
|
|
|
}
|
|
|
|
|
2005-06-09 22:50:41 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::BeginningOfDocument()
|
2002-10-08 22:53:55 +00:00
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
if (!mDocWeak) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
2002-10-08 22:53:55 +00:00
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
// Get the selection
|
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NOT_INITIALIZED);
|
2011-02-11 01:01:49 +00:00
|
|
|
|
|
|
|
// Get the root element.
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<Element> rootElement = GetRoot();
|
2011-02-11 01:01:49 +00:00
|
|
|
if (!rootElement) {
|
|
|
|
NS_WARNING("GetRoot() returned a null pointer (mRootElement is null)");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
// Find first editable thingy
|
2011-09-29 06:19:26 +00:00
|
|
|
bool done = false;
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsINode> curNode = rootElement.get(), selNode;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t curOffset = 0, selOffset;
|
2014-11-02 12:04:13 +00:00
|
|
|
while (!done) {
|
2002-10-08 22:53:55 +00:00
|
|
|
nsWSRunObject wsObj(this, curNode, curOffset);
|
2014-11-02 12:04:13 +00:00
|
|
|
int32_t visOffset = 0;
|
2012-07-13 06:33:43 +00:00
|
|
|
WSType visType;
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsINode> visNode;
|
|
|
|
wsObj.NextVisibleNode(curNode, curOffset, address_of(visNode), &visOffset,
|
|
|
|
&visType);
|
2012-07-13 06:33:43 +00:00
|
|
|
if (visType == WSType::normalWS || visType == WSType::text) {
|
2014-11-02 12:04:13 +00:00
|
|
|
selNode = visNode;
|
2002-10-08 22:53:55 +00:00
|
|
|
selOffset = visOffset;
|
2011-10-17 14:59:28 +00:00
|
|
|
done = true;
|
2012-07-13 06:33:43 +00:00
|
|
|
} else if (visType == WSType::br || visType == WSType::special) {
|
2014-11-02 12:04:13 +00:00
|
|
|
selNode = visNode->GetParentNode();
|
|
|
|
selOffset = selNode ? selNode->IndexOf(visNode) : -1;
|
2011-10-17 14:59:28 +00:00
|
|
|
done = true;
|
2012-07-13 06:33:43 +00:00
|
|
|
} else if (visType == WSType::otherBlock) {
|
2014-11-02 12:04:13 +00:00
|
|
|
// By definition of nsWSRunObject, a block element terminates a
|
|
|
|
// whitespace run. That is, although we are calling a method that is
|
|
|
|
// named "NextVisibleNode", the node returned might not be
|
|
|
|
// visible/editable!
|
|
|
|
//
|
|
|
|
// If the given block does not contain any visible/editable items, we
|
|
|
|
// want to skip it and continue our search.
|
|
|
|
|
|
|
|
if (!IsContainer(visNode)) {
|
|
|
|
// However, we were given a block that is not a container. Since the
|
|
|
|
// block can not contain anything that's visible, such a block only
|
|
|
|
// makes sense if it is visible by itself, like a <hr>. We want to
|
|
|
|
// place the caret in front of that block.
|
|
|
|
selNode = visNode->GetParentNode();
|
|
|
|
selOffset = selNode ? selNode->IndexOf(visNode) : -1;
|
2011-10-17 14:59:28 +00:00
|
|
|
done = true;
|
2014-11-02 12:04:13 +00:00
|
|
|
} else {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isEmptyBlock;
|
2014-11-02 12:04:13 +00:00
|
|
|
if (NS_SUCCEEDED(IsEmptyNode(visNode, &isEmptyBlock)) &&
|
|
|
|
isEmptyBlock) {
|
|
|
|
// Skip the empty block
|
|
|
|
curNode = visNode->GetParentNode();
|
|
|
|
curOffset = curNode ? curNode->IndexOf(visNode) : -1;
|
|
|
|
curOffset++;
|
|
|
|
} else {
|
|
|
|
curNode = visNode;
|
2003-04-16 20:18:41 +00:00
|
|
|
curOffset = 0;
|
|
|
|
}
|
2014-11-02 12:04:13 +00:00
|
|
|
// Keep looping
|
2003-04-16 20:18:41 +00:00
|
|
|
}
|
2014-11-02 12:04:13 +00:00
|
|
|
} else {
|
|
|
|
// Else we found nothing useful
|
2002-10-08 22:53:55 +00:00
|
|
|
selNode = curNode;
|
|
|
|
selOffset = curOffset;
|
2011-10-17 14:59:28 +00:00
|
|
|
done = true;
|
2002-10-08 22:53:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return selection->Collapse(selNode, selOffset);
|
|
|
|
}
|
|
|
|
|
2010-06-10 01:16:58 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::HandleKeyPressEvent(nsIDOMKeyEvent* aKeyEvent)
|
|
|
|
{
|
|
|
|
// NOTE: When you change this method, you should also change:
|
2014-08-18 21:28:17 +00:00
|
|
|
// * editor/libeditor/tests/test_htmleditor_keyevent_handling.html
|
2010-06-10 01:16:58 +00:00
|
|
|
|
|
|
|
if (IsReadonly() || IsDisabled()) {
|
|
|
|
// When we're not editable, the events are handled on nsEditor, so, we can
|
|
|
|
// bypass nsPlaintextEditor.
|
|
|
|
return nsEditor::HandleKeyPressEvent(aKeyEvent);
|
|
|
|
}
|
|
|
|
|
2013-10-18 06:10:24 +00:00
|
|
|
WidgetKeyboardEvent* nativeKeyEvent =
|
|
|
|
aKeyEvent->GetInternalNSEvent()->AsKeyboardEvent();
|
2010-06-10 01:16:58 +00:00
|
|
|
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:
|
2012-07-19 01:28:17 +00:00
|
|
|
case nsIDOMKeyEvent::DOM_VK_WIN:
|
2010-06-10 01:16:58 +00:00
|
|
|
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 03:00:02 +00:00
|
|
|
if (nativeKeyEvent->IsControl() || nativeKeyEvent->IsAlt() ||
|
2012-07-19 01:28:17 +00:00
|
|
|
nativeKeyEvent->IsMeta() || nativeKeyEvent->IsOS()) {
|
2010-06-10 01:16:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-25 10:34:42 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
|
|
|
NS_ENSURE_TRUE(selection && selection->RangeCount(), NS_ERROR_FAILURE);
|
2010-06-10 01:16:58 +00:00
|
|
|
|
2014-04-25 10:34:42 +00:00
|
|
|
nsCOMPtr<nsINode> node = selection->GetRangeAt(0)->GetStartParent();
|
|
|
|
MOZ_ASSERT(node);
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> blockParent;
|
|
|
|
if (IsBlockNode(node)) {
|
2010-06-10 01:16:58 +00:00
|
|
|
blockParent = node;
|
|
|
|
} else {
|
|
|
|
blockParent = GetBlockNodeParent(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!blockParent) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool handled = false;
|
2014-05-07 04:07:50 +00:00
|
|
|
nsresult rv = NS_OK;
|
2010-06-10 01:16:58 +00:00
|
|
|
if (nsHTMLEditUtils::IsTableElement(blockParent)) {
|
2012-04-25 03:00:02 +00:00
|
|
|
rv = TabInTable(nativeKeyEvent->IsShift(), &handled);
|
2010-06-10 01:16:58 +00:00
|
|
|
if (handled) {
|
2011-10-17 14:59:28 +00:00
|
|
|
ScrollSelectionIntoView(false);
|
2010-06-10 01:16:58 +00:00
|
|
|
}
|
|
|
|
} else if (nsHTMLEditUtils::IsListItem(blockParent)) {
|
2014-04-25 10:34:42 +00:00
|
|
|
rv = Indent(nativeKeyEvent->IsShift()
|
|
|
|
? NS_LITERAL_STRING("outdent")
|
|
|
|
: NS_LITERAL_STRING("indent"));
|
2011-10-17 14:59:28 +00:00
|
|
|
handled = true;
|
2010-06-10 01:16:58 +00:00
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (handled) {
|
|
|
|
return aKeyEvent->PreventDefault(); // consumed
|
|
|
|
}
|
2012-04-25 03:00:02 +00:00
|
|
|
if (nativeKeyEvent->IsShift()) {
|
2010-06-10 01:16:58 +00: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:
|
2012-04-25 03:00:02 +00:00
|
|
|
if (nativeKeyEvent->IsControl() || nativeKeyEvent->IsAlt() ||
|
2012-07-19 01:28:17 +00:00
|
|
|
nativeKeyEvent->IsMeta() || nativeKeyEvent->IsOS()) {
|
2010-06-10 01:16:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
aKeyEvent->PreventDefault(); // consumed
|
2012-04-25 03:00:02 +00:00
|
|
|
if (nativeKeyEvent->IsShift() && !IsPlaintextEditor()) {
|
2010-06-10 01:16:58 +00: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 03:00:02 +00:00
|
|
|
if (nativeKeyEvent->charCode == 0 || nativeKeyEvent->IsControl() ||
|
2012-07-19 01:28:17 +00:00
|
|
|
nativeKeyEvent->IsAlt() || nativeKeyEvent->IsMeta() ||
|
|
|
|
nativeKeyEvent->IsOS()) {
|
2010-06-10 01:16:58 +00: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);
|
|
|
|
}
|
|
|
|
|
2012-07-01 19:53:16 +00:00
|
|
|
static void
|
|
|
|
AssertParserServiceIsCorrect(nsIAtom* aTag, bool aIsBlock)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
// Check this against what we would have said with the old code:
|
2014-08-29 11:43:24 +00:00
|
|
|
if (aTag == nsGkAtoms::p ||
|
|
|
|
aTag == nsGkAtoms::div ||
|
|
|
|
aTag == nsGkAtoms::blockquote ||
|
|
|
|
aTag == nsGkAtoms::h1 ||
|
|
|
|
aTag == nsGkAtoms::h2 ||
|
|
|
|
aTag == nsGkAtoms::h3 ||
|
|
|
|
aTag == nsGkAtoms::h4 ||
|
|
|
|
aTag == nsGkAtoms::h5 ||
|
|
|
|
aTag == nsGkAtoms::h6 ||
|
|
|
|
aTag == nsGkAtoms::ul ||
|
|
|
|
aTag == nsGkAtoms::ol ||
|
|
|
|
aTag == nsGkAtoms::dl ||
|
|
|
|
aTag == nsGkAtoms::noscript ||
|
|
|
|
aTag == nsGkAtoms::form ||
|
|
|
|
aTag == nsGkAtoms::hr ||
|
|
|
|
aTag == nsGkAtoms::table ||
|
|
|
|
aTag == nsGkAtoms::fieldset ||
|
|
|
|
aTag == nsGkAtoms::address ||
|
|
|
|
aTag == nsGkAtoms::col ||
|
|
|
|
aTag == nsGkAtoms::colgroup ||
|
|
|
|
aTag == nsGkAtoms::li ||
|
|
|
|
aTag == nsGkAtoms::dt ||
|
|
|
|
aTag == nsGkAtoms::dd ||
|
|
|
|
aTag == nsGkAtoms::legend) {
|
2012-07-01 19:53:16 +00:00
|
|
|
if (!aIsBlock) {
|
|
|
|
nsAutoString assertmsg (NS_LITERAL_STRING("Parser and editor disagree on blockness: "));
|
|
|
|
|
|
|
|
nsAutoString tagName;
|
|
|
|
aTag->ToString(tagName);
|
|
|
|
assertmsg.Append(tagName);
|
|
|
|
char* assertstr = ToNewCString(assertmsg);
|
|
|
|
NS_ASSERTION(aIsBlock, assertstr);
|
|
|
|
NS_Free(assertstr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
}
|
|
|
|
|
2001-04-05 23:48:01 +00:00
|
|
|
/**
|
|
|
|
* Returns true if the id represents an element of block type.
|
|
|
|
* Can be used to determine if a new paragraph should be started.
|
|
|
|
*/
|
2012-07-01 19:53:16 +00:00
|
|
|
bool
|
|
|
|
nsHTMLEditor::NodeIsBlockStatic(const dom::Element* aElement)
|
2001-04-05 23:48:01 +00:00
|
|
|
{
|
2012-07-01 19:53:16 +00:00
|
|
|
MOZ_ASSERT(aElement);
|
2001-04-05 23:48:01 +00:00
|
|
|
|
2012-07-01 19:53:16 +00:00
|
|
|
nsIAtom* tagAtom = aElement->Tag();
|
|
|
|
MOZ_ASSERT(tagAtom);
|
2001-04-05 23:48:01 +00:00
|
|
|
|
|
|
|
// Nodes we know we want to treat as block
|
|
|
|
// even though the parser says they're not:
|
2014-08-29 11:43:24 +00:00
|
|
|
if (tagAtom == nsGkAtoms::body ||
|
|
|
|
tagAtom == nsGkAtoms::head ||
|
|
|
|
tagAtom == nsGkAtoms::tbody ||
|
|
|
|
tagAtom == nsGkAtoms::thead ||
|
|
|
|
tagAtom == nsGkAtoms::tfoot ||
|
|
|
|
tagAtom == nsGkAtoms::tr ||
|
|
|
|
tagAtom == nsGkAtoms::th ||
|
|
|
|
tagAtom == nsGkAtoms::td ||
|
|
|
|
tagAtom == nsGkAtoms::li ||
|
|
|
|
tagAtom == nsGkAtoms::dt ||
|
|
|
|
tagAtom == nsGkAtoms::dd ||
|
|
|
|
tagAtom == nsGkAtoms::pre) {
|
2012-07-01 19:53:16 +00:00
|
|
|
return true;
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
|
2012-07-01 19:53:16 +00:00
|
|
|
bool isBlock;
|
2012-10-18 00:07:05 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
// XXX we can't use DebugOnly here because VC++ is stupid (bug 802884)
|
|
|
|
nsresult rv =
|
|
|
|
#endif
|
|
|
|
nsContentUtils::GetParserService()->
|
2007-02-24 20:07:06 +00:00
|
|
|
IsBlock(nsContentUtils::GetParserService()->HTMLAtomTagToId(tagAtom),
|
2012-07-01 19:53:16 +00:00
|
|
|
isBlock);
|
|
|
|
MOZ_ASSERT(rv == NS_OK);
|
2001-04-05 23:48:01 +00:00
|
|
|
|
2012-07-01 19:53:16 +00:00
|
|
|
AssertParserServiceIsCorrect(tagAtom, isBlock);
|
2003-07-19 08:51:54 +00:00
|
|
|
|
2012-07-01 19:53:16 +00:00
|
|
|
return isBlock;
|
|
|
|
}
|
2003-07-19 08:51:54 +00:00
|
|
|
|
2012-07-01 19:53:16 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::NodeIsBlockStatic(nsIDOMNode *aNode, bool *aIsBlock)
|
|
|
|
{
|
|
|
|
if (!aNode || !aIsBlock) { return NS_ERROR_NULL_POINTER; }
|
2001-04-06 03:18:41 +00:00
|
|
|
|
2012-07-01 19:53:16 +00:00
|
|
|
nsCOMPtr<dom::Element> element = do_QueryInterface(aNode);
|
|
|
|
*aIsBlock = element && NodeIsBlockStatic(element);
|
|
|
|
return NS_OK;
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
|
2001-04-07 00:45:26 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::NodeIsBlock(nsIDOMNode *aNode, bool *aIsBlock)
|
2001-04-05 23:48:01 +00:00
|
|
|
{
|
|
|
|
return NodeIsBlockStatic(aNode, aIsBlock);
|
|
|
|
}
|
|
|
|
|
2011-11-28 12:53:29 +00:00
|
|
|
bool
|
|
|
|
nsHTMLEditor::IsBlockNode(nsINode *aNode)
|
|
|
|
{
|
2012-07-01 19:53:16 +00:00
|
|
|
return aNode && aNode->IsElement() && NodeIsBlockStatic(aNode->AsElement());
|
2011-11-28 12:53:29 +00:00
|
|
|
}
|
|
|
|
|
2001-04-07 00:45:26 +00:00
|
|
|
// Non-static version for the nsIEditor interface and JavaScript
|
2001-02-26 22:02:58 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::SetDocumentTitle(const nsAString &aTitle)
|
2001-02-26 22:02:58 +00:00
|
|
|
{
|
2009-04-24 22:45:34 +00:00
|
|
|
nsRefPtr<SetDocTitleTxn> txn = new SetDocTitleTxn();
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(txn, NS_ERROR_OUT_OF_MEMORY);
|
2001-02-26 22:02:58 +00:00
|
|
|
|
2009-04-24 22:45:34 +00:00
|
|
|
nsresult result = txn->Init(this, &aTitle);
|
2010-06-17 20:44:35 +00:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2001-04-12 03:41:06 +00:00
|
|
|
|
2009-04-24 22:45:34 +00:00
|
|
|
//Don't let Rules System change the selection
|
|
|
|
nsAutoTxnsConserveSelection dontChangeSelection(this);
|
|
|
|
return nsEditor::DoTransaction(txn);
|
2001-02-26 22:02:58 +00:00
|
|
|
}
|
2000-01-18 21:50:15 +00:00
|
|
|
|
2001-04-05 23:48:01 +00:00
|
|
|
/* ------------ Block methods moved from nsEditor -------------- */
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetBlockNodeParent: returns enclosing block level ancestor, if any
|
|
|
|
//
|
2014-04-25 10:34:42 +00:00
|
|
|
already_AddRefed<Element>
|
|
|
|
nsHTMLEditor::GetBlockNodeParent(nsINode* aNode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> p = aNode->GetParentNode();
|
|
|
|
|
|
|
|
while (p) {
|
|
|
|
if (p->IsElement() && NodeIsBlockStatic(p->AsElement())) {
|
|
|
|
return p.forget().downcast<Element>();
|
|
|
|
}
|
|
|
|
p = p->GetParentNode();
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2011-08-11 13:29:59 +00:00
|
|
|
already_AddRefed<nsIDOMNode>
|
2001-04-05 23:48:01 +00:00
|
|
|
nsHTMLEditor::GetBlockNodeParent(nsIDOMNode *aNode)
|
|
|
|
{
|
2014-04-25 10:34:42 +00:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
|
|
|
|
if (!node) {
|
2001-04-05 23:48:01 +00:00
|
|
|
NS_NOTREACHED("null node passed to GetBlockNodeParent()");
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 10:34:42 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> ret =
|
|
|
|
dont_AddRef(GetAsDOMNode(GetBlockNodeParent(node).take()));
|
2014-04-25 10:34:42 +00:00
|
|
|
return ret.forget();
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
static const char16_t nbsp = 160;
|
2001-04-05 23:48:01 +00:00
|
|
|
|
2012-07-01 11:07:16 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsNextCharInNodeWhitespace: checks the adjacent content in the same node to
|
|
|
|
// see if following selection is whitespace or nbsp
|
2012-06-01 11:03:17 +00:00
|
|
|
void
|
2012-07-01 11:07:16 +00:00
|
|
|
nsHTMLEditor::IsNextCharInNodeWhitespace(nsIContent* aContent,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aOffset,
|
2012-07-01 11:07:16 +00:00
|
|
|
bool* outIsSpace,
|
|
|
|
bool* outIsNBSP,
|
|
|
|
nsIContent** outNode,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t* outOffset)
|
2001-04-05 23:48:01 +00:00
|
|
|
{
|
2012-07-01 11:07:16 +00:00
|
|
|
MOZ_ASSERT(aContent && outIsSpace && outIsNBSP);
|
|
|
|
MOZ_ASSERT((outNode && outOffset) || (!outNode && !outOffset));
|
2011-10-17 14:59:28 +00:00
|
|
|
*outIsSpace = false;
|
|
|
|
*outIsNBSP = false;
|
2012-07-01 11:07:16 +00:00
|
|
|
if (outNode && outOffset) {
|
2012-07-30 14:20:58 +00:00
|
|
|
*outNode = nullptr;
|
2012-07-01 11:07:16 +00:00
|
|
|
*outOffset = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aContent->IsNodeOfType(nsINode::eTEXT) &&
|
2012-08-22 15:56:38 +00:00
|
|
|
(uint32_t)aOffset < aContent->Length()) {
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t ch = aContent->GetText()->CharAt(aOffset);
|
2012-07-01 11:07:16 +00:00
|
|
|
*outIsSpace = nsCRT::IsAsciiSpace(ch);
|
|
|
|
*outIsNBSP = (ch == nbsp);
|
|
|
|
if (outNode && outOffset) {
|
|
|
|
NS_IF_ADDREF(*outNode = aContent);
|
|
|
|
// yes, this is _past_ the character
|
|
|
|
*outOffset = aOffset + 1;
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-07-01 11:07:16 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsPrevCharInNodeWhitespace: checks the adjacent content in the same node to
|
|
|
|
// see if following selection is whitespace
|
2012-06-01 11:03:17 +00:00
|
|
|
void
|
2012-07-01 11:07:16 +00:00
|
|
|
nsHTMLEditor::IsPrevCharInNodeWhitespace(nsIContent* aContent,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aOffset,
|
2012-07-01 11:07:16 +00:00
|
|
|
bool* outIsSpace,
|
|
|
|
bool* outIsNBSP,
|
|
|
|
nsIContent** outNode,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t* outOffset)
|
2001-04-05 23:48:01 +00:00
|
|
|
{
|
2012-07-01 11:07:16 +00:00
|
|
|
MOZ_ASSERT(aContent && outIsSpace && outIsNBSP);
|
|
|
|
MOZ_ASSERT((outNode && outOffset) || (!outNode && !outOffset));
|
2011-10-17 14:59:28 +00:00
|
|
|
*outIsSpace = false;
|
|
|
|
*outIsNBSP = false;
|
2012-07-01 11:07:16 +00:00
|
|
|
if (outNode && outOffset) {
|
2012-07-30 14:20:58 +00:00
|
|
|
*outNode = nullptr;
|
2012-07-01 11:07:16 +00:00
|
|
|
*outOffset = -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aContent->IsNodeOfType(nsINode::eTEXT) && aOffset > 0) {
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t ch = aContent->GetText()->CharAt(aOffset - 1);
|
2012-07-01 11:07:16 +00:00
|
|
|
*outIsSpace = nsCRT::IsAsciiSpace(ch);
|
|
|
|
*outIsNBSP = (ch == nbsp);
|
|
|
|
if (outNode && outOffset) {
|
|
|
|
NS_IF_ADDREF(*outNode = aContent);
|
|
|
|
*outOffset = aOffset - 1;
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* ------------ End Block methods -------------- */
|
|
|
|
|
|
|
|
|
2014-04-25 10:34:42 +00:00
|
|
|
bool
|
|
|
|
nsHTMLEditor::IsVisBreak(nsINode* aNode)
|
2002-05-14 00:05:37 +00:00
|
|
|
{
|
2014-04-25 10:34:42 +00:00
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
if (!nsTextEditUtils::IsBreak(aNode)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2014-04-25 10:34:42 +00:00
|
|
|
}
|
|
|
|
// Check if there is a later node in block after br
|
|
|
|
nsCOMPtr<nsINode> priorNode = GetPriorHTMLNode(aNode, true);
|
|
|
|
if (priorNode && nsTextEditUtils::IsBreak(priorNode)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2014-04-25 10:34:42 +00:00
|
|
|
}
|
|
|
|
nsCOMPtr<nsINode> nextNode = GetNextHTMLNode(aNode, true);
|
|
|
|
if (nextNode && nsTextEditUtils::IsBreak(nextNode)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2014-04-25 10:34:42 +00:00
|
|
|
}
|
2002-05-14 00:05:37 +00:00
|
|
|
|
2014-04-25 10:34:42 +00:00
|
|
|
// If we are right before block boundary, then br not visible
|
|
|
|
if (!nextNode) {
|
|
|
|
// This break is trailer in block, it's not visible
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (IsBlockNode(nextNode)) {
|
|
|
|
// Break is right before a block, it's not visible
|
|
|
|
return false;
|
|
|
|
}
|
2002-05-14 00:05:37 +00:00
|
|
|
|
2014-04-25 10:34:42 +00:00
|
|
|
// Sigh. We have to use expensive whitespace calculation code to
|
2002-05-14 00:05:37 +00:00
|
|
|
// determine what is going on
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t selOffset;
|
2014-04-25 10:34:42 +00:00
|
|
|
nsCOMPtr<nsINode> selNode = GetNodeLocation(aNode, &selOffset);
|
|
|
|
// Let's look after the break
|
|
|
|
selOffset++;
|
2014-05-02 12:15:26 +00:00
|
|
|
nsWSRunObject wsObj(this, selNode, selOffset);
|
2014-05-02 12:15:27 +00:00
|
|
|
nsCOMPtr<nsINode> unused;
|
2014-04-25 10:34:42 +00:00
|
|
|
int32_t visOffset = 0;
|
2012-07-13 06:33:43 +00:00
|
|
|
WSType visType;
|
2014-05-02 12:15:27 +00:00
|
|
|
wsObj.NextVisibleNode(selNode, selOffset, address_of(unused),
|
2014-04-25 10:34:42 +00:00
|
|
|
&visOffset, &visType);
|
2012-07-13 06:33:43 +00:00
|
|
|
if (visType & WSType::block) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2012-07-13 06:33:43 +00:00
|
|
|
}
|
2002-05-14 00:05:37 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2002-05-14 00:05:37 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 10:34:42 +00:00
|
|
|
bool
|
|
|
|
nsHTMLEditor::IsVisBreak(nsIDOMNode* aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(node, false);
|
|
|
|
return IsVisBreak(node);
|
|
|
|
}
|
|
|
|
|
2002-09-18 19:42:38 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::GetIsDocumentEditable(bool *aIsDocumentEditable)
|
2002-09-18 19:42:38 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsDocumentEditable);
|
|
|
|
|
2012-03-24 08:19:14 +00:00
|
|
|
nsCOMPtr<nsIDOMDocument> doc = GetDOMDocument();
|
|
|
|
*aIsDocumentEditable = doc && IsModifiable();
|
2002-09-18 19:42:38 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool nsHTMLEditor::IsModifiable()
|
2000-01-18 21:50:15 +00:00
|
|
|
{
|
2010-04-12 02:35:18 +00:00
|
|
|
return !IsReadonly();
|
2000-01-18 21:50:15 +00:00
|
|
|
}
|
|
|
|
|
2002-11-27 00:35:09 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::UpdateBaseURL()
|
|
|
|
{
|
2014-04-25 10:34:42 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = GetDocument();
|
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
|
2002-11-27 00:35:09 +00:00
|
|
|
|
|
|
|
// Look for an HTML <base> tag
|
2014-04-25 10:34:42 +00:00
|
|
|
nsRefPtr<nsContentList> nodeList =
|
|
|
|
doc->GetElementsByTagName(NS_LITERAL_STRING("base"));
|
2002-11-27 00:35:09 +00:00
|
|
|
|
2014-04-25 10:34:42 +00:00
|
|
|
// 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 (!nodeList || !nodeList->Item(0)) {
|
2004-01-09 23:54:21 +00:00
|
|
|
return doc->SetBaseURI(doc->GetDocumentURI());
|
2002-11-27 00:35:09 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-10-06 19:34:09 +00:00
|
|
|
/* This routine is needed to provide a bottleneck for typing for logging
|
2004-04-30 23:55:16 +00: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 19:34:09 +00:00
|
|
|
to TypedText() to determine what action to take, but without passing
|
|
|
|
an event.
|
|
|
|
*/
|
2012-05-22 09:37:17 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::TypedText(const nsAString& aString, ETypingAction aAction)
|
1999-10-06 19:34:09 +00:00
|
|
|
{
|
2008-03-13 18:54:01 +00:00
|
|
|
nsAutoPlaceHolderBatch batch(this, nsGkAtoms::TypingTxnName);
|
1999-10-06 19:34:09 +00:00
|
|
|
|
2012-05-22 09:37:17 +00:00
|
|
|
if (aAction == eTypedBR) {
|
|
|
|
// only inserts a br node
|
|
|
|
nsCOMPtr<nsIDOMNode> brNode;
|
|
|
|
return InsertBR(address_of(brNode));
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsPlaintextEditor::TypedText(aString, aAction);
|
1999-10-06 19:34:09 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::TabInTable(bool inIsShift, bool* outHandled)
|
1999-09-13 09:37:51 +00:00
|
|
|
{
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(outHandled, NS_ERROR_NULL_POINTER);
|
2011-10-17 14:59:28 +00:00
|
|
|
*outHandled = false;
|
1999-09-13 09:37:51 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
// Find enclosing table cell from selection (cell may be selected element)
|
2014-04-28 11:54:46 +00:00
|
|
|
nsCOMPtr<Element> cellElement =
|
|
|
|
GetElementOrParentByTagName(NS_LITERAL_STRING("td"), nullptr);
|
2000-04-04 14:51:26 +00:00
|
|
|
// Do nothing -- we didn't find a table cell
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(cellElement, NS_OK);
|
2000-04-04 14:51:26 +00:00
|
|
|
|
|
|
|
// find enclosing table
|
2014-04-28 11:54:46 +00:00
|
|
|
nsCOMPtr<Element> table = GetEnclosingTable(cellElement);
|
|
|
|
NS_ENSURE_TRUE(table, NS_OK);
|
2000-04-04 14:51:26 +00:00
|
|
|
|
|
|
|
// advance to next cell
|
|
|
|
// first create an iterator over the table
|
2014-04-28 11:54:46 +00:00
|
|
|
nsCOMPtr<nsIContentIterator> iter = NS_NewContentIterator();
|
|
|
|
nsresult res = iter->Init(table);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-04-04 14:51:26 +00:00
|
|
|
// position iter at block
|
2014-04-28 11:54:46 +00:00
|
|
|
res = iter->PositionAt(cellElement);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2004-01-24 00:46:17 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
nsCOMPtr<nsINode> node;
|
|
|
|
do {
|
|
|
|
if (inIsShift) {
|
2004-01-24 00:46:17 +00:00
|
|
|
iter->Prev();
|
2014-04-28 11:54:46 +00:00
|
|
|
} else {
|
2004-01-24 00:46:17 +00:00
|
|
|
iter->Next();
|
2014-04-28 11:54:46 +00:00
|
|
|
}
|
2004-01-24 00:46:17 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
node = iter->GetCurrentNode();
|
2004-01-24 00:46:17 +00:00
|
|
|
|
|
|
|
if (node && nsHTMLEditUtils::IsTableCell(node) &&
|
2014-04-28 11:54:46 +00:00
|
|
|
nsCOMPtr<Element>(GetEnclosingTable(node)) == table) {
|
|
|
|
CollapseSelectionToDeepestNonTableFirstChild(nullptr, node);
|
2011-10-17 14:59:28 +00:00
|
|
|
*outHandled = true;
|
2000-04-04 14:51:26 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2004-01-24 00:46:17 +00:00
|
|
|
} while (!iter->IsDone());
|
2000-04-18 21:39:35 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
if (!(*outHandled) && !inIsShift) {
|
|
|
|
// If we haven't handled it yet, then we must have run off the end of the
|
|
|
|
// table. Insert a new row.
|
2011-10-17 14:59:28 +00:00
|
|
|
res = InsertTableRow(1, true);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2011-10-17 14:59:28 +00:00
|
|
|
*outHandled = true;
|
2014-04-28 11:54:46 +00:00
|
|
|
// Put selection in right place. Use table code to get selection and index
|
|
|
|
// to new row...
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<Selection> selection;
|
2014-04-28 11:54:46 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> tblElement, cell;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t row;
|
2000-05-02 03:24:11 +00:00
|
|
|
res = GetCellContext(getter_AddRefs(selection),
|
2000-05-03 21:18:23 +00:00
|
|
|
getter_AddRefs(tblElement),
|
2000-05-02 03:24:11 +00:00
|
|
|
getter_AddRefs(cell),
|
2012-07-30 14:20:58 +00:00
|
|
|
nullptr, nullptr,
|
|
|
|
&row, nullptr);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-05-02 03:24:11 +00:00
|
|
|
// ...so that we can ask for first cell in that row...
|
2001-02-26 22:02:58 +00:00
|
|
|
res = GetCellAt(tblElement, row, 0, getter_AddRefs(cell));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2014-04-28 11:54:46 +00:00
|
|
|
// ...and then set selection there. (Note that normally you should use
|
|
|
|
// CollapseSelectionToDeepestNonTableFirstChild(), but we know cell is an
|
|
|
|
// empty new cell, so this works fine)
|
|
|
|
if (cell) {
|
|
|
|
selection->Collapse(cell, 0);
|
|
|
|
}
|
2000-04-18 21:39:35 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
return NS_OK;
|
1999-09-13 09:37:51 +00:00
|
|
|
}
|
|
|
|
|
2014-05-02 11:11:26 +00:00
|
|
|
already_AddRefed<Element>
|
|
|
|
nsHTMLEditor::CreateBR(nsINode* aNode, int32_t aOffset, EDirection aSelect)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = GetAsDOMNode(aNode);
|
|
|
|
int32_t offset = aOffset;
|
|
|
|
nsCOMPtr<nsIDOMNode> outBRNode;
|
|
|
|
// We assume everything is fine if the br is not null, irrespective of retval
|
|
|
|
CreateBRImpl(address_of(parent), &offset, address_of(outBRNode), aSelect);
|
|
|
|
nsCOMPtr<Element> ret = do_QueryInterface(outBRNode);
|
|
|
|
return ret.forget();
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::CreateBR(nsIDOMNode *aNode, int32_t aOffset, nsCOMPtr<nsIDOMNode> *outBRNode, EDirection aSelect)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = aNode;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t offset = aOffset;
|
2000-12-09 04:46:08 +00:00
|
|
|
return CreateBRImpl(address_of(parent), &offset, outBRNode, aSelect);
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
void
|
|
|
|
nsHTMLEditor::CollapseSelectionToDeepestNonTableFirstChild(
|
|
|
|
Selection* aSelection, nsINode* aNode)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2014-04-28 11:54:46 +00:00
|
|
|
MOZ_ASSERT(aNode);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
nsRefPtr<Selection> selection = aSelection;
|
|
|
|
if (!selection) {
|
|
|
|
selection = GetSelection();
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
2014-04-28 11:54:46 +00:00
|
|
|
if (!selection) {
|
|
|
|
// Nothing to do
|
|
|
|
return;
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
nsCOMPtr<nsINode> node = aNode;
|
|
|
|
|
|
|
|
for (nsCOMPtr<nsIContent> child = node->GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetFirstChild()) {
|
|
|
|
// Stop if we find a table, don't want to go into nested tables
|
|
|
|
if (nsHTMLEditUtils::IsTable(child) || !IsContainer(child)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
node = child;
|
|
|
|
};
|
|
|
|
|
|
|
|
selection->Collapse(node, 0);
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
2000-04-13 21:50:19 +00:00
|
|
|
|
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
/**
|
|
|
|
* This is mostly like InsertHTMLWithCharsetAndContext, but we can't use that
|
|
|
|
* because it is selection-based and the rules code won't let us edit under the
|
|
|
|
* <head> node
|
|
|
|
*/
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::ReplaceHeadContentsWithHTML(const nsAString& aSourceToInsert)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2014-04-28 11:54:46 +00:00
|
|
|
// don't do any post processing, rules get confused
|
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::ignore, nsIEditor::eNone);
|
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
ForceCompositionEnd();
|
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
// 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<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED);
|
2002-01-14 20:31:43 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
nsRefPtr<nsContentList> nodeList =
|
|
|
|
doc->GetElementsByTagName(NS_LITERAL_STRING("head"));
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(nodeList, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
nsCOMPtr<nsIContent> headNode = nodeList->Item(0);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(headNode, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2014-04-28 11:54:46 +00: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.
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString inputString (aSourceToInsert); // hope this does copy-on-write
|
|
|
|
|
|
|
|
// Windows linebreaks: Map CRLF to LF:
|
2013-12-13 01:50:01 +00:00
|
|
|
inputString.ReplaceSubstring(MOZ_UTF16("\r\n"),
|
|
|
|
MOZ_UTF16("\n"));
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// Mac linebreaks: Map any remaining CR to LF:
|
2013-12-13 01:50:01 +00:00
|
|
|
inputString.ReplaceSubstring(MOZ_UTF16("\r"),
|
|
|
|
MOZ_UTF16("\n"));
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
|
|
|
|
// Get the first range in the selection, for context:
|
2014-04-28 11:54:46 +00:00
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(0);
|
|
|
|
NS_ENSURE_TRUE(range, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
ErrorResult err;
|
|
|
|
nsRefPtr<DocumentFragment> docfrag =
|
|
|
|
range->CreateContextualFragment(inputString, err);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2014-04-28 11:54:46 +00: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 doesn't matter!
|
2000-04-14 03:19:31 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
if (err.Failed()) {
|
2001-01-28 20:13:07 +00:00
|
|
|
#ifdef DEBUG
|
2012-10-15 21:39:46 +00:00
|
|
|
printf("Couldn't create contextual fragment: error was %X\n",
|
2014-04-28 11:54:46 +00:00
|
|
|
static_cast<uint32_t>(err.ErrorCode()));
|
2001-01-28 20:13:07 +00:00
|
|
|
#endif
|
2014-04-28 11:54:46 +00:00
|
|
|
return err.ErrorCode();
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(docfrag, NS_ERROR_NULL_POINTER);
|
2000-04-14 03:19:31 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// First delete all children in head
|
2014-04-28 11:54:46 +00:00
|
|
|
while (nsCOMPtr<nsIContent> child = headNode->GetFirstChild()) {
|
|
|
|
nsresult res = DeleteNode(child);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2014-04-28 11:54:46 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// Now insert the new nodes
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t offsetOfNewNode = 0;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// Loop over the contents of the fragment and move into the document
|
2014-04-28 11:54:46 +00:00
|
|
|
while (nsCOMPtr<nsIContent> child = docfrag->GetFirstChild()) {
|
2014-08-20 12:25:17 +00:00
|
|
|
nsresult res = InsertNode(*child, *headNode, offsetOfNewNode++);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2014-04-28 11:54:46 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
return NS_OK;
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::RebuildDocumentFromSource(const nsAString& aSourceString)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
ForceCompositionEnd();
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2014-04-28 11:54:47 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2014-04-28 11:54:47 +00:00
|
|
|
nsCOMPtr<Element> bodyElement = GetRoot();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(bodyElement, NS_ERROR_NULL_POINTER);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Find where the <body> tag starts.
|
2014-01-04 15:02:17 +00:00
|
|
|
nsReadingIterator<char16_t> beginbody;
|
|
|
|
nsReadingIterator<char16_t> endbody;
|
2001-04-07 00:45:26 +00:00
|
|
|
aSourceString.BeginReading(beginbody);
|
|
|
|
aSourceString.EndReading(endbody);
|
2011-09-29 06:19:26 +00:00
|
|
|
bool foundbody = CaseInsensitiveFindInReadable(NS_LITERAL_STRING("<body"),
|
2014-04-28 11:54:47 +00:00
|
|
|
beginbody, endbody);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
nsReadingIterator<char16_t> beginhead;
|
|
|
|
nsReadingIterator<char16_t> endhead;
|
2001-04-07 00:45:26 +00:00
|
|
|
aSourceString.BeginReading(beginhead);
|
|
|
|
aSourceString.EndReading(endhead);
|
2011-09-29 06:19:26 +00:00
|
|
|
bool foundhead = CaseInsensitiveFindInReadable(NS_LITERAL_STRING("<head"),
|
2014-04-28 11:54:47 +00:00
|
|
|
beginhead, endhead);
|
2012-08-17 15:39:52 +00:00
|
|
|
// a valid head appears before the body
|
2014-04-28 11:54:47 +00:00
|
|
|
if (foundbody && beginhead.get() > beginbody.get()) {
|
2012-08-17 15:39:52 +00:00
|
|
|
foundhead = false;
|
2014-04-28 11:54:47 +00:00
|
|
|
}
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
nsReadingIterator<char16_t> beginclosehead;
|
|
|
|
nsReadingIterator<char16_t> endclosehead;
|
2001-04-07 00:45:26 +00:00
|
|
|
aSourceString.BeginReading(beginclosehead);
|
|
|
|
aSourceString.EndReading(endclosehead);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-04-07 00:45:26 +00:00
|
|
|
// Find the index after "<head>"
|
2011-09-29 06:19:26 +00:00
|
|
|
bool foundclosehead = CaseInsensitiveFindInReadable(
|
2004-07-07 22:34:20 +00:00
|
|
|
NS_LITERAL_STRING("</head>"), beginclosehead, endclosehead);
|
2012-08-17 15:39:52 +00:00
|
|
|
// a valid close head appears after a found head
|
2014-04-28 11:54:47 +00:00
|
|
|
if (foundhead && beginhead.get() > beginclosehead.get()) {
|
2012-08-17 15:39:52 +00:00
|
|
|
foundclosehead = false;
|
2014-04-28 11:54:47 +00:00
|
|
|
}
|
2012-08-17 15:39:52 +00:00
|
|
|
// a valid close head appears before a found body
|
2014-04-28 11:54:47 +00:00
|
|
|
if (foundbody && beginclosehead.get() > beginbody.get()) {
|
2012-08-17 15:39:52 +00:00
|
|
|
foundclosehead = false;
|
2014-04-28 11:54:47 +00:00
|
|
|
}
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Time to change the document
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
nsReadingIterator<char16_t> endtotal;
|
2001-04-07 00:45:26 +00:00
|
|
|
aSourceString.EndReading(endtotal);
|
|
|
|
|
2014-04-28 11:54:47 +00:00
|
|
|
nsresult res;
|
2004-07-07 22:34:20 +00:00
|
|
|
if (foundhead) {
|
2014-04-28 11:54:47 +00:00
|
|
|
if (foundclosehead) {
|
2004-07-07 22:34:20 +00:00
|
|
|
res = ReplaceHeadContentsWithHTML(Substring(beginhead, beginclosehead));
|
2014-04-28 11:54:47 +00:00
|
|
|
} else if (foundbody) {
|
2004-07-07 22:34:20 +00:00
|
|
|
res = ReplaceHeadContentsWithHTML(Substring(beginhead, beginbody));
|
2014-04-28 11:54:47 +00:00
|
|
|
} 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
|
2004-07-07 22:34:20 +00:00
|
|
|
res = ReplaceHeadContentsWithHTML(Substring(beginhead, endtotal));
|
2014-04-28 11:54:47 +00:00
|
|
|
}
|
2004-07-07 22:34:20 +00:00
|
|
|
} else {
|
2014-01-04 15:02:17 +00:00
|
|
|
nsReadingIterator<char16_t> begintotal;
|
2004-07-07 22:34:20 +00:00
|
|
|
aSourceString.BeginReading(begintotal);
|
|
|
|
NS_NAMED_LITERAL_STRING(head, "<head>");
|
2014-04-28 11:54:47 +00:00
|
|
|
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
|
2004-07-07 22:34:20 +00:00
|
|
|
res = ReplaceHeadContentsWithHTML(head);
|
2014-04-28 11:54:47 +00:00
|
|
|
}
|
2004-07-07 22:34:20 +00:00
|
|
|
}
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2004-07-21 09:57:49 +00:00
|
|
|
res = SelectAll();
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2004-07-21 09:57:49 +00:00
|
|
|
|
2004-07-07 22:34:20 +00:00
|
|
|
if (!foundbody) {
|
|
|
|
NS_NAMED_LITERAL_STRING(body, "<body>");
|
2014-04-28 11:54:47 +00:00
|
|
|
// 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
|
2004-07-07 22:34:20 +00:00
|
|
|
res = LoadHTML(body + Substring(endclosehead, endtotal));
|
2014-04-28 11:54:47 +00:00
|
|
|
} else if (foundhead) {
|
|
|
|
// assume there is no body
|
2004-07-07 22:34:20 +00:00
|
|
|
res = LoadHTML(body);
|
2014-04-28 11:54:47 +00:00
|
|
|
} else {
|
|
|
|
// assume there is no head, the entire source is body
|
2004-07-07 22:34:20 +00:00
|
|
|
res = LoadHTML(body + aSourceString);
|
2014-04-28 11:54:47 +00:00
|
|
|
}
|
2010-06-17 20:44:35 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2005-11-30 11:01:37 +00:00
|
|
|
|
2014-04-28 11:54:47 +00:00
|
|
|
nsCOMPtr<Element> divElement =
|
|
|
|
CreateElementWithDefaults(NS_LITERAL_STRING("div"));
|
|
|
|
NS_ENSURE_TRUE(divElement, NS_ERROR_FAILURE);
|
2005-11-30 11:01:37 +00:00
|
|
|
|
2014-08-18 14:44:50 +00:00
|
|
|
CloneAttributes(bodyElement, divElement);
|
2004-07-07 22:34:20 +00:00
|
|
|
|
|
|
|
return BeginningOfDocument();
|
|
|
|
}
|
|
|
|
|
|
|
|
res = LoadHTML(Substring(beginbody, endtotal));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// Now we must copy attributes user might have edited on the <body> tag
|
2014-04-28 11:54:47 +00:00
|
|
|
// because InsertHTML (actually, CreateContextualFragment()) will never
|
|
|
|
// return a body node in the DOM fragment
|
2001-04-10 03:02:10 +00:00
|
|
|
|
|
|
|
// We already know where "<body" begins
|
2014-01-04 15:02:17 +00:00
|
|
|
nsReadingIterator<char16_t> beginclosebody = beginbody;
|
|
|
|
nsReadingIterator<char16_t> endclosebody;
|
2001-04-07 00:45:26 +00:00
|
|
|
aSourceString.EndReading(endclosebody);
|
2014-04-28 11:54:47 +00:00
|
|
|
if (!FindInReadable(NS_LITERAL_STRING(">"), beginclosebody, endclosebody)) {
|
2001-04-07 00:45:26 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-04-28 11:54:47 +00:00
|
|
|
}
|
2001-04-07 00:45:26 +00:00
|
|
|
|
2014-04-28 11:54:47 +00: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 16:36:40 +00:00
|
|
|
nsAutoString bodyTag;
|
|
|
|
bodyTag.AssignLiteral("<div ");
|
|
|
|
bodyTag.Append(Substring(endbody, endclosebody));
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2014-04-28 11:54:47 +00:00
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(0);
|
|
|
|
NS_ENSURE_TRUE(range, NS_ERROR_FAILURE);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2014-04-28 11:54:47 +00:00
|
|
|
ErrorResult rv;
|
|
|
|
nsRefPtr<DocumentFragment> docfrag =
|
|
|
|
range->CreateContextualFragment(bodyTag, rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv.ErrorCode(), rv.ErrorCode());
|
|
|
|
NS_ENSURE_TRUE(docfrag, NS_ERROR_NULL_POINTER);
|
1999-03-01 19:54:47 +00:00
|
|
|
|
2014-04-28 11:54:47 +00:00
|
|
|
nsCOMPtr<nsIContent> child = docfrag->GetFirstChild();
|
2014-08-18 14:44:50 +00:00
|
|
|
NS_ENSURE_TRUE(child && child->IsElement(), NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// Copy all attributes from the div child to current body element
|
2014-08-18 14:44:50 +00:00
|
|
|
CloneAttributes(bodyElement, child->AsElement());
|
2002-10-08 22:53:55 +00: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-24 23:00:24 +00:00
|
|
|
}
|
|
|
|
|
2003-04-15 20:53:15 +00:00
|
|
|
void
|
|
|
|
nsHTMLEditor::NormalizeEOLInsertPosition(nsIDOMNode *firstNodeToInsert,
|
|
|
|
nsCOMPtr<nsIDOMNode> *insertParentNode,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t *insertOffset)
|
2003-04-15 20:53:15 +00:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
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);
|
2014-05-02 12:15:27 +00:00
|
|
|
nsCOMPtr<nsINode> nextVisNode, prevVisNode;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t nextVisOffset=0;
|
2012-07-13 06:33:43 +00:00
|
|
|
WSType nextVisType;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t prevVisOffset=0;
|
2012-07-13 06:33:43 +00:00
|
|
|
WSType prevVisType;
|
2003-04-15 20:53:15 +00:00
|
|
|
|
2014-05-02 12:15:27 +00:00
|
|
|
nsCOMPtr<nsINode> parent(do_QueryInterface(*insertParentNode));
|
|
|
|
wsObj.NextVisibleNode(parent, *insertOffset, address_of(nextVisNode), &nextVisOffset, &nextVisType);
|
2003-04-15 20:53:15 +00:00
|
|
|
if (!nextVisNode)
|
|
|
|
return;
|
|
|
|
|
2012-07-13 06:33:43 +00:00
|
|
|
if (!(nextVisType & WSType::br)) {
|
2003-04-15 20:53:15 +00:00
|
|
|
return;
|
2012-07-13 06:33:43 +00:00
|
|
|
}
|
2003-04-15 20:53:15 +00:00
|
|
|
|
2014-05-02 12:15:27 +00:00
|
|
|
wsObj.PriorVisibleNode(parent, *insertOffset, address_of(prevVisNode), &prevVisOffset, &prevVisType);
|
2003-04-15 20:53:15 +00:00
|
|
|
if (!prevVisNode)
|
|
|
|
return;
|
|
|
|
|
2012-07-13 06:33:43 +00:00
|
|
|
if (prevVisType & WSType::br) {
|
2003-04-15 20:53:15 +00:00
|
|
|
return;
|
2012-07-13 06:33:43 +00:00
|
|
|
}
|
2003-04-15 20:53:15 +00:00
|
|
|
|
2012-07-13 06:33:43 +00:00
|
|
|
if (prevVisType & WSType::thisBlock) {
|
2003-04-15 20:53:15 +00:00
|
|
|
return;
|
2012-07-13 06:33:43 +00:00
|
|
|
}
|
2003-04-15 20:53:15 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t brOffset=0;
|
2014-05-02 12:15:27 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> brNode = GetNodeLocation(GetAsDOMNode(nextVisNode), &brOffset);
|
2003-04-15 20:53:15 +00:00
|
|
|
|
|
|
|
*insertParentNode = brNode;
|
|
|
|
*insertOffset = brOffset + 1;
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::InsertElementAtSelection(nsIDOMElement* aElement, bool aDeleteSelection)
|
1999-03-01 19:54:47 +00:00
|
|
|
{
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res = NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(aElement, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aElement);
|
|
|
|
|
|
|
|
ForceCompositionEnd();
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
2012-08-12 18:28:26 +00:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::insertElement, nsIEditor::eNext);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2012-06-08 14:20:55 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2012-05-22 09:37:17 +00:00
|
|
|
if (!selection) {
|
2001-01-28 20:13:07 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2012-05-22 09:37:17 +00:00
|
|
|
}
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// hand off to the rules system, see if it has anything to say about this
|
2011-09-29 06:19:26 +00:00
|
|
|
bool cancel, handled;
|
2012-08-12 18:28:26 +00:00
|
|
|
nsTextRulesInfo ruleInfo(EditAction::insertElement);
|
2001-01-28 20:13:07 +00:00
|
|
|
ruleInfo.insertElement = aElement;
|
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || (NS_FAILED(res))) return res;
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!handled)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if (aDeleteSelection)
|
|
|
|
{
|
2012-05-10 14:54:33 +00:00
|
|
|
if (!IsBlockNode(aElement)) {
|
|
|
|
// E.g., inserting an image. In this case we don't need to delete any
|
|
|
|
// inline wrappers before we do the insertion. Otherwise we let
|
|
|
|
// DeleteSelectionAndPrepareToCreateNode do the deletion for us, which
|
|
|
|
// calls DeleteSelection with aStripWrappers = eStrip.
|
|
|
|
res = DeleteSelection(nsIEditor::eNone, nsIEditor::eNoStrip);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
}
|
|
|
|
|
2012-06-21 12:54:26 +00:00
|
|
|
nsresult result = DeleteSelectionAndPrepareToCreateNode();
|
2010-06-17 20:44:35 +00:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2000-01-31 10:30:12 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// If deleting, selection will be collapsed.
|
|
|
|
// so if not, we collapse it
|
|
|
|
if (!aDeleteSelection)
|
2000-01-31 10:30:12 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Named Anchor is a special case,
|
|
|
|
// We collapse to insert element BEFORE the selection
|
|
|
|
// For all other tags, we insert AFTER the selection
|
|
|
|
if (nsHTMLEditUtils::IsNamedAnchor(node))
|
2000-01-31 10:30:12 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
selection->CollapseToStart();
|
|
|
|
} else {
|
|
|
|
selection->CollapseToEnd();
|
2000-01-31 10:30:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parentSelectedNode;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t offsetForInsert;
|
2001-01-28 20:13:07 +00:00
|
|
|
res = selection->GetAnchorNode(getter_AddRefs(parentSelectedNode));
|
|
|
|
// XXX: ERROR_HANDLING bad XPCOM usage
|
|
|
|
if (NS_SUCCEEDED(res) && NS_SUCCEEDED(selection->GetAnchorOffset(&offsetForInsert)) && parentSelectedNode)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2003-04-15 20:53:15 +00:00
|
|
|
// Adjust position based on the node we are going to insert.
|
|
|
|
NormalizeEOLInsertPosition(node, address_of(parentSelectedNode), &offsetForInsert);
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
res = InsertNodeAtPoint(node, address_of(parentSelectedNode), &offsetForInsert, false);
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
// Set caret after element, but check for special case
|
|
|
|
// of inserting table-related elements: set in first cell instead
|
|
|
|
if (!SetCaretInTableCell(aElement))
|
2001-06-26 23:13:01 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
res = SetCaretAfterElement(aElement);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-26 23:13:01 +00: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 06:19:26 +00:00
|
|
|
bool isLast;
|
2001-06-26 23:13:01 +00:00
|
|
|
res = IsLastEditableChild(node, &isLast);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-26 23:13:01 +00:00
|
|
|
if (isLast)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> brNode;
|
|
|
|
res = CreateBR(parentSelectedNode, offsetForInsert+1, address_of(brNode));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-09-14 21:12:35 +00:00
|
|
|
selection->Collapse(parentSelectedNode, offsetForInsert+1);
|
2001-06-26 23:13:01 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
2000-04-13 21:50:19 +00:00
|
|
|
}
|
|
|
|
|
2001-06-15 22:29:07 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
InsertNodeAtPoint: attempts to insert aNode into the document, at a point specified by
|
|
|
|
{*ioParent,*ioOffset}. Checks with strict dtd to see if containment is allowed. If not
|
2010-04-29 19:01:11 +00:00
|
|
|
allowed, will attempt to find a parent in the parent hierarchy of *ioParent that will
|
2001-06-15 22:29:07 +00: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
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t *ioOffset insertion offset
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aNoEmptyNodes splitting can result in empty nodes?
|
2001-06-15 22:29:07 +00:00
|
|
|
*/
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::InsertNodeAtPoint(nsIDOMNode *aNode,
|
2001-06-15 22:29:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> *ioParent,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t *ioOffset,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aNoEmptyNodes)
|
1999-05-26 21:40:51 +00:00
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsIContent> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
|
2001-06-15 22:29:07 +00:00
|
|
|
NS_ENSURE_TRUE(ioParent, NS_ERROR_NULL_POINTER);
|
|
|
|
NS_ENSURE_TRUE(*ioParent, NS_ERROR_NULL_POINTER);
|
|
|
|
NS_ENSURE_TRUE(ioOffset, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsresult res = NS_OK;
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsINode> parent = do_QueryInterface(*ioParent);
|
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
|
|
|
|
nsCOMPtr<nsINode> topChild = parent;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t offsetOfInsert = *ioOffset;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// Search up the parent chain to find a suitable container
|
2014-11-02 12:04:13 +00:00
|
|
|
while (!CanContain(*parent, *node)) {
|
2001-01-28 20:13:07 +00:00
|
|
|
// If the current parent is a root (body or table element)
|
|
|
|
// then go no further - we can't insert
|
2014-11-02 12:04:13 +00:00
|
|
|
if (parent->Tag() == nsGkAtoms::body ||
|
|
|
|
nsHTMLEditUtils::IsTableElement(parent)) {
|
2001-01-28 20:13:07 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
2014-11-02 12:04:13 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
// Get the next parent
|
2014-11-02 12:04:13 +00:00
|
|
|
NS_ENSURE_TRUE(parent->GetParentNode(), NS_ERROR_FAILURE);
|
|
|
|
if (!IsEditable(parent->GetParentNode())) {
|
2014-10-22 12:45:30 +00:00
|
|
|
// There's no suitable place to put the node in this editing host. Maybe
|
|
|
|
// someone is trying to put block content in a span. So just put it
|
|
|
|
// where we were originally asked.
|
2014-11-02 12:04:13 +00:00
|
|
|
parent = topChild = do_QueryInterface(*ioParent);
|
|
|
|
NS_ENSURE_STATE(parent);
|
2014-10-22 12:45:30 +00:00
|
|
|
break;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
topChild = parent;
|
2014-11-02 12:04:13 +00:00
|
|
|
parent = parent->GetParentNode();
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
if (parent != topChild)
|
|
|
|
{
|
|
|
|
// we need to split some levels above the original selection parent
|
2014-11-02 12:04:13 +00:00
|
|
|
res = SplitNodeDeep(GetAsDOMNode(topChild), *ioParent, *ioOffset,
|
|
|
|
&offsetOfInsert, aNoEmptyNodes);
|
2010-06-17 20:44:35 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2014-11-02 12:04:13 +00:00
|
|
|
*ioParent = GetAsDOMNode(parent);
|
2001-06-15 22:29:07 +00:00
|
|
|
*ioOffset = offsetOfInsert;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
// Now we can insert the new node
|
2014-11-02 12:04:13 +00:00
|
|
|
res = InsertNode(*node, *parent, offsetOfInsert);
|
2001-01-28 20:13:07 +00:00
|
|
|
return res;
|
2000-04-13 21:50:19 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::SelectElement(nsIDOMElement* aElement)
|
2000-04-13 21:50:19 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res = NS_ERROR_NULL_POINTER;
|
2000-04-14 03:19:31 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Must be sure that element is contained in the document body
|
2012-05-06 07:53:11 +00:00
|
|
|
if (IsDescendantOfEditorRoot(aElement)) {
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>parent;
|
|
|
|
res = aElement->GetParentNode(getter_AddRefs(parent));
|
|
|
|
if (NS_SUCCEEDED(res) && parent)
|
1999-08-19 13:30:48 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t offsetInParent = GetChildOffset(aElement, parent);
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2012-07-02 11:42:19 +00:00
|
|
|
// 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);
|
1999-08-19 13:30:48 +00:00
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
1999-10-06 19:34:09 +00:00
|
|
|
}
|
2000-03-24 00:26:47 +00:00
|
|
|
return res;
|
1999-05-26 21:40:51 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::SetCaretAfterElement(nsIDOMElement* aElement)
|
1999-11-13 16:37:58 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res = NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// Be sure the element is contained in the document body
|
2012-05-06 07:53:11 +00:00
|
|
|
if (aElement && IsDescendantOfEditorRoot(aElement)) {
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>parent;
|
|
|
|
res = aElement->GetParentNode(getter_AddRefs(parent));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t offsetInParent = GetChildOffset(aElement, parent);
|
2012-07-02 11:42:19 +00:00
|
|
|
// Collapse selection to just after desired element,
|
|
|
|
res = selection->Collapse(parent, offsetInParent + 1);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
return res;
|
1999-11-13 16:37:58 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::SetParagraphFormat(const nsAString& aParagraphFormat)
|
1999-11-13 16:37:58 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString tag; tag.Assign(aParagraphFormat);
|
2001-12-17 07:14:49 +00:00
|
|
|
ToLowerCase(tag);
|
2004-05-22 22:15:22 +00:00
|
|
|
if (tag.EqualsLiteral("dd") || tag.EqualsLiteral("dt"))
|
2001-01-28 20:13:07 +00:00
|
|
|
return MakeDefinitionItem(tag);
|
|
|
|
else
|
|
|
|
return InsertBasicBlock(tag);
|
1999-11-13 16:37:58 +00:00
|
|
|
}
|
1999-09-22 01:21:56 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::GetParagraphState(bool *aMixed, nsAString &outFormat)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(aMixed, NS_ERROR_NULL_POINTER);
|
2013-03-23 03:47:57 +00:00
|
|
|
nsRefPtr<nsHTMLEditRules> htmlRules = static_cast<nsHTMLEditRules*>(mRules.get());
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
return htmlRules->GetParagraphState(aMixed, outFormat);
|
|
|
|
}
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::GetBackgroundColorState(bool *aMixed, nsAString &aOutColor)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
|
|
|
nsresult res;
|
2012-02-01 10:54:22 +00:00
|
|
|
if (IsCSSEnabled()) {
|
2002-01-09 13:51:37 +00:00
|
|
|
// if we are in CSS mode, we have to check if the containing block defines
|
|
|
|
// a background color
|
2011-10-17 14:59:28 +00:00
|
|
|
res = GetCSSBackgroundColorState(aMixed, aOutColor, true);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// in HTML mode, we look only at page's background
|
|
|
|
res = GetHTMLBackgroundColorState(aMixed, aOutColor);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::GetHighlightColorState(bool *aMixed, nsAString &aOutColor)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
2011-10-17 14:59:28 +00:00
|
|
|
*aMixed = false;
|
2004-06-17 00:13:25 +00:00
|
|
|
aOutColor.AssignLiteral("transparent");
|
2012-02-01 10:54:22 +00:00
|
|
|
if (IsCSSEnabled()) {
|
2002-01-09 13:51:37 +00: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 14:59:28 +00:00
|
|
|
res = GetCSSBackgroundColorState(aMixed, aOutColor, false);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::GetCSSBackgroundColorState(bool *aMixed, nsAString &aOutColor, bool aBlockLevel)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(aMixed, NS_ERROR_NULL_POINTER);
|
2011-10-17 14:59:28 +00:00
|
|
|
*aMixed = false;
|
2002-01-09 13:51:37 +00:00
|
|
|
// the default background color is transparent
|
2004-06-17 00:13:25 +00:00
|
|
|
aOutColor.AssignLiteral("transparent");
|
2002-01-09 13:51:37 +00:00
|
|
|
|
|
|
|
// get selection
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
|
|
|
NS_ENSURE_STATE(selection);
|
2002-01-09 13:51:37 +00:00
|
|
|
|
|
|
|
// get selection location
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t offset;
|
2014-11-02 12:04:13 +00:00
|
|
|
nsresult res = GetStartNodeAndOffset(selection, getter_AddRefs(parent),
|
|
|
|
&offset);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
|
2009-02-19 07:55:40 +00:00
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
// is the selection collapsed?
|
|
|
|
nsCOMPtr<nsIDOMNode> nodeToExamine;
|
2012-05-18 08:29:39 +00:00
|
|
|
if (selection->Collapsed() || IsTextNode(parent)) {
|
2002-01-09 13:51:37 +00:00
|
|
|
// we want to look at the parent and ancestors
|
|
|
|
nodeToExamine = parent;
|
2012-05-18 08:29:39 +00:00
|
|
|
} else {
|
2002-01-09 13:51:37 +00:00
|
|
|
// otherwise we want to look at the first editable node after
|
2012-02-15 09:18:24 +00:00
|
|
|
// {parent,offset} and its ancestors for divs with alignment on them
|
2002-01-09 13:51:37 +00:00
|
|
|
nodeToExamine = GetChildAt(parent, offset);
|
2011-10-17 14:59:28 +00:00
|
|
|
//GetNextNode(parent, offset, true, address_of(nodeToExamine));
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(nodeToExamine, NS_ERROR_NULL_POINTER);
|
2002-01-09 13:51:37 +00:00
|
|
|
|
|
|
|
// is the node to examine a block ?
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isBlock;
|
2002-01-09 13:51:37 +00:00
|
|
|
res = NodeIsBlockStatic(nodeToExamine, &isBlock);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00: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-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(blockParent, NS_OK);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
2006-02-24 04:54:10 +00:00
|
|
|
|
|
|
|
// Make sure to not walk off onto the Document node
|
|
|
|
nsCOMPtr<nsIDOMElement> element;
|
2002-01-09 13:51:37 +00:00
|
|
|
do {
|
|
|
|
// retrieve the computed style of background-color for blockParent
|
2005-03-24 19:00:01 +00:00
|
|
|
mHTMLCSSUtils->GetComputedProperty(blockParent,
|
2014-08-29 11:43:24 +00:00
|
|
|
nsGkAtoms::backgroundColor,
|
2002-01-09 13:51:37 +00:00
|
|
|
aOutColor);
|
2005-03-24 19:00:01 +00:00
|
|
|
tmp.swap(blockParent);
|
2002-01-09 13:51:37 +00:00
|
|
|
res = tmp->GetParentNode(getter_AddRefs(blockParent));
|
2006-02-24 04:54:10 +00:00
|
|
|
element = do_QueryInterface(blockParent);
|
2002-01-09 13:51:37 +00: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 04:54:10 +00:00
|
|
|
} while (aOutColor.EqualsLiteral("transparent") && element);
|
2005-03-24 19:00:01 +00:00
|
|
|
if (aOutColor.EqualsLiteral("transparent")) {
|
2002-01-09 13:51:37 +00:00
|
|
|
// we have hit the root of the document and the color is still transparent !
|
|
|
|
// Grumble... Let's look at the default background color because that's the
|
|
|
|
// color we are looking for
|
|
|
|
mHTMLCSSUtils->GetDefaultBackgroundColor(aOutColor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// no, we are querying the text background for the Text Highlight button
|
|
|
|
if (IsTextNode(nodeToExamine)) {
|
|
|
|
// if the node of interest is a text node, let's climb a level
|
|
|
|
res = nodeToExamine->GetParentNode(getter_AddRefs(parent));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00:00
|
|
|
nodeToExamine = parent;
|
|
|
|
}
|
|
|
|
do {
|
|
|
|
// is the node to examine a block ?
|
|
|
|
res = NodeIsBlockStatic(nodeToExamine, &isBlock);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00:00
|
|
|
if (isBlock) {
|
|
|
|
// yes it is a block; in that case, the text background color is transparent
|
2004-06-17 00:13:25 +00:00
|
|
|
aOutColor.AssignLiteral("transparent");
|
2002-01-09 13:51:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// no, it's not; let's retrieve the computed style of background-color for the
|
|
|
|
// node to examine
|
2014-08-29 11:43:24 +00:00
|
|
|
mHTMLCSSUtils->GetComputedProperty(nodeToExamine,
|
|
|
|
nsGkAtoms::backgroundColor,
|
|
|
|
aOutColor);
|
2004-05-22 22:15:22 +00:00
|
|
|
if (!aOutColor.EqualsLiteral("transparent")) {
|
2002-01-09 13:51:37 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2005-03-24 19:00:01 +00:00
|
|
|
tmp.swap(nodeToExamine);
|
|
|
|
res = tmp->GetParentNode(getter_AddRefs(nodeToExamine));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2005-03-24 19:00:01 +00:00
|
|
|
} while ( aOutColor.EqualsLiteral("transparent") && nodeToExamine );
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::GetHTMLBackgroundColorState(bool *aMixed, nsAString &aOutColor)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
//TODO: We don't handle "mixed" correctly!
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(aMixed, NS_ERROR_NULL_POINTER);
|
2011-10-17 14:59:28 +00:00
|
|
|
*aMixed = false;
|
2004-05-22 20:02:43 +00:00
|
|
|
aOutColor.Truncate();
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2012-02-01 10:54:22 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> domElement;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t selectedCount;
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString tagName;
|
2002-09-13 04:19:30 +00:00
|
|
|
nsresult res = GetSelectedOrParentTableElement(tagName,
|
|
|
|
&selectedCount,
|
2012-02-01 10:54:22 +00:00
|
|
|
getter_AddRefs(domElement));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2012-06-06 07:36:52 +00:00
|
|
|
nsCOMPtr<dom::Element> element = do_QueryInterface(domElement);
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2012-02-01 10:54:22 +00:00
|
|
|
while (element) {
|
2001-01-28 20:13:07 +00:00
|
|
|
// We are in a cell or selected table
|
2012-06-06 07:36:52 +00:00
|
|
|
element->GetAttr(kNameSpaceID_None, nsGkAtoms::bgcolor, aOutColor);
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Done if we have a color explicitly set
|
2012-02-01 10:54:22 +00:00
|
|
|
if (!aOutColor.IsEmpty()) {
|
2001-01-28 20:13:07 +00:00
|
|
|
return NS_OK;
|
2012-02-01 10:54:22 +00:00
|
|
|
}
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Once we hit the body, we're done
|
2012-06-06 07:36:52 +00:00
|
|
|
if (element->IsHTML(nsGkAtoms::body)) {
|
2012-02-01 10:54:22 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// No color is set, but we need to report visible color inherited
|
|
|
|
// from nested cells/tables, so search up parent chain
|
2013-01-11 08:43:01 +00:00
|
|
|
element = element->GetParentElement();
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// If no table or cell found, get page body
|
2012-02-01 10:54:22 +00:00
|
|
|
dom::Element* bodyElement = GetRoot();
|
2011-12-03 21:50:15 +00:00
|
|
|
NS_ENSURE_TRUE(bodyElement, NS_ERROR_NULL_POINTER);
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2012-02-01 10:54:22 +00:00
|
|
|
bodyElement->GetAttr(kNameSpaceID_None, nsGkAtoms::bgcolor, aOutColor);
|
|
|
|
return NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::GetListState(bool *aMixed, bool *aOL, bool *aUL, bool *aDL)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(aMixed && aOL && aUL && aDL, NS_ERROR_NULL_POINTER);
|
2013-03-23 03:47:57 +00:00
|
|
|
nsRefPtr<nsHTMLEditRules> htmlRules = static_cast<nsHTMLEditRules*>(mRules.get());
|
2000-06-01 02:38:13 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
return htmlRules->GetListState(aMixed, aOL, aUL, aDL);
|
|
|
|
}
|
|
|
|
|
2000-05-04 14:02:03 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::GetListItemState(bool *aMixed, bool *aLI, bool *aDT, bool *aDD)
|
2000-05-04 14:02:03 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(aMixed && aLI && aDT && aDD, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2013-03-23 03:47:57 +00:00
|
|
|
nsRefPtr<nsHTMLEditRules> htmlRules = static_cast<nsHTMLEditRules*>(mRules.get());
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
return htmlRules->GetListItemState(aMixed, aLI, aDT, aDD);
|
2000-05-04 14:02:03 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::GetAlignment(bool *aMixed, nsIHTMLEditor::EAlignment *aAlign)
|
2000-04-04 14:51:26 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(aMixed && aAlign, NS_ERROR_NULL_POINTER);
|
2013-03-23 03:47:57 +00:00
|
|
|
nsRefPtr<nsHTMLEditRules> htmlRules = static_cast<nsHTMLEditRules*>(mRules.get());
|
2000-04-04 14:51:26 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
return htmlRules->GetAlignment(aMixed, aAlign);
|
|
|
|
}
|
2000-04-04 14:51:26 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::GetIndentState(bool *aCanIndent, bool *aCanOutdent)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(aCanIndent && aCanOutdent, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2013-03-23 03:47:57 +00:00
|
|
|
nsRefPtr<nsHTMLEditRules> htmlRules = static_cast<nsHTMLEditRules*>(mRules.get());
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
return htmlRules->GetIndentState(aCanIndent, aCanOutdent);
|
2000-04-04 14:51:26 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::MakeOrChangeList(const nsAString& aListType, bool entireList, const nsAString& aBulletType)
|
1999-05-26 21:40:51 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res;
|
1999-08-09 01:37:50 +00:00
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
1999-05-26 21:40:51 +00:00
|
|
|
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool cancel, handled;
|
1999-05-26 21:40:51 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
2012-08-12 18:28:26 +00:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::makeList, nsIEditor::eNext);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// pre-process
|
2012-06-08 14:20:55 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2012-08-12 18:28:26 +00:00
|
|
|
nsTextRulesInfo ruleInfo(EditAction::makeList);
|
2001-01-28 20:13:07 +00:00
|
|
|
ruleInfo.blockType = &aListType;
|
|
|
|
ruleInfo.entireList = entireList;
|
2001-12-12 03:57:32 +00:00
|
|
|
ruleInfo.bulletType = &aBulletType;
|
2001-01-28 20:13:07 +00:00
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || (NS_FAILED(res))) return res;
|
2000-05-31 00:03:02 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!handled)
|
1999-12-14 23:07:12 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Find out if the selection is collapsed:
|
2012-05-18 08:29:39 +00:00
|
|
|
bool isCollapsed = selection->Collapsed();
|
1999-12-14 23:07:12 +00:00
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsINode> node;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t offset;
|
2010-06-11 02:46:51 +00:00
|
|
|
res = GetStartNodeAndOffset(selection, getter_AddRefs(node), &offset);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!node) res = NS_ERROR_FAILURE;
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
if (isCollapsed)
|
1999-12-14 23:07:12 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// have to find a place to put the list
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsINode> parent = node;
|
|
|
|
nsCOMPtr<nsINode> topChild = node;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2012-05-01 06:34:52 +00:00
|
|
|
nsCOMPtr<nsIAtom> listAtom = do_GetAtom(aListType);
|
2014-11-02 12:04:13 +00:00
|
|
|
while (!CanContainTag(*parent, *listAtom)) {
|
2001-01-28 20:13:07 +00:00
|
|
|
topChild = parent;
|
2014-11-02 12:04:13 +00:00
|
|
|
parent = parent->GetParentNode();
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parent != node)
|
|
|
|
{
|
|
|
|
// we need to split up to the child of parent
|
2014-11-02 12:04:13 +00:00
|
|
|
res = SplitNodeDeep(GetAsDOMNode(topChild), GetAsDOMNode(node), offset,
|
|
|
|
&offset);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make a list
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<Element> newList = CreateNode(listAtom, parent, offset);
|
|
|
|
NS_ENSURE_STATE(newList);
|
2001-01-28 20:13:07 +00:00
|
|
|
// make a list item
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<Element> newItem = CreateNode(nsGkAtoms::li, newList, 0);
|
|
|
|
NS_ENSURE_STATE(newItem);
|
2001-01-28 20:13:07 +00:00
|
|
|
res = selection->Collapse(newItem,0);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
1999-12-14 23:07:12 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::RemoveList(const nsAString& aListType)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
nsresult res;
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool cancel, handled;
|
1999-12-14 23:07:12 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
2012-08-12 18:28:26 +00:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::removeList, nsIEditor::eNext);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
// pre-process
|
2012-06-08 14:20:55 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2012-08-12 18:28:26 +00:00
|
|
|
nsTextRulesInfo ruleInfo(EditAction::removeList);
|
2004-06-17 00:13:25 +00:00
|
|
|
if (aListType.LowerCaseEqualsLiteral("ol"))
|
2011-10-17 14:59:28 +00:00
|
|
|
ruleInfo.bOrdered = true;
|
|
|
|
else ruleInfo.bOrdered = false;
|
2001-01-28 20:13:07 +00:00
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || (NS_FAILED(res))) return res;
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// no default behavior for this yet. what would it mean?
|
|
|
|
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
1999-05-26 21:40:51 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::MakeDefinitionItem(const nsAString& aItemType)
|
1999-03-01 19:54:47 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res;
|
1999-03-29 08:02:05 +00:00
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool cancel, handled;
|
1999-03-29 08:02:05 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
2012-08-12 18:28:26 +00:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::makeDefListItem, nsIEditor::eNext);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
1999-03-29 08:02:05 +00:00
|
|
|
// pre-process
|
2012-06-08 14:20:55 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2012-08-12 18:28:26 +00:00
|
|
|
nsTextRulesInfo ruleInfo(EditAction::makeDefListItem);
|
2001-01-28 20:13:07 +00:00
|
|
|
ruleInfo.blockType = &aItemType;
|
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || (NS_FAILED(res))) return res;
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!handled)
|
1999-10-06 19:34:09 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// todo: no default for now. we count on rules to handle it.
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
2000-10-11 22:50:14 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::InsertBasicBlock(const nsAString& aBlockType)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res;
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2011-09-29 06:19:26 +00: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 10:57:30 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
2012-08-12 18:28:26 +00:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::makeBasicBlock, nsIEditor::eNext);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// pre-process
|
2012-06-08 14:20:55 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2012-08-12 18:28:26 +00:00
|
|
|
nsTextRulesInfo ruleInfo(EditAction::makeBasicBlock);
|
2001-01-28 20:13:07 +00: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 10:57:30 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!handled)
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Find out if the selection is collapsed:
|
2012-05-18 08:29:39 +00:00
|
|
|
bool isCollapsed = selection->Collapsed();
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsINode> node;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t offset;
|
2010-06-11 02:46:51 +00:00
|
|
|
res = GetStartNodeAndOffset(selection, getter_AddRefs(node), &offset);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!node) res = NS_ERROR_FAILURE;
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 20:13:07 +00: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 10:57:30 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// have to find a place to put the block
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsINode> parent = node;
|
|
|
|
nsCOMPtr<nsINode> topChild = node;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2012-05-01 06:34:52 +00:00
|
|
|
nsCOMPtr<nsIAtom> blockAtom = do_GetAtom(aBlockType);
|
2014-11-02 12:04:13 +00:00
|
|
|
while (!CanContainTag(*parent, *blockAtom)) {
|
|
|
|
NS_ENSURE_TRUE(parent->GetParentNode(), NS_ERROR_FAILURE);
|
2001-01-28 20:13:07 +00:00
|
|
|
topChild = parent;
|
2014-11-02 12:04:13 +00:00
|
|
|
parent = parent->GetParentNode();
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parent != node)
|
|
|
|
{
|
|
|
|
// we need to split up to the child of parent
|
2014-11-02 12:04:13 +00:00
|
|
|
res = SplitNodeDeep(GetAsDOMNode(topChild), GetAsDOMNode(node), offset,
|
|
|
|
&offset);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// make a block
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<Element> newBlock = CreateNode(blockAtom, parent, offset);
|
|
|
|
NS_ENSURE_STATE(newBlock);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// reposition selection to inside the block
|
|
|
|
res = selection->Collapse(newBlock,0);
|
2010-06-17 19:27:24 +00: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 10:57:30 +00:00
|
|
|
}
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
2000-12-06 23:11:21 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::Indent(const nsAString& aIndent)
|
2000-12-06 23:11:21 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res;
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool cancel, handled;
|
2012-08-12 18:28:26 +00:00
|
|
|
EditAction opID = EditAction::indent;
|
2004-06-17 00:13:25 +00: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 10:57:30 +00:00
|
|
|
{
|
2012-08-12 18:28:26 +00:00
|
|
|
opID = EditAction::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 10:57:30 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, opID, nsIEditor::eNext);
|
|
|
|
|
|
|
|
// pre-process
|
2012-06-08 14:20:55 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2012-05-05 18:52:29 +00:00
|
|
|
nsTextRulesInfo ruleInfo(opID);
|
2001-01-28 20:13:07 +00: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 10:57:30 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Do default - insert a blockquote node if selection collapsed
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsINode> node;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t offset;
|
2012-05-18 08:29:39 +00:00
|
|
|
bool isCollapsed = selection->Collapsed();
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2010-06-11 02:46:51 +00:00
|
|
|
res = GetStartNodeAndOffset(selection, getter_AddRefs(node), &offset);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!node) res = NS_ERROR_FAILURE;
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2004-05-22 22:15:22 +00:00
|
|
|
if (aIndent.EqualsLiteral("indent"))
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
if (isCollapsed)
|
|
|
|
{
|
|
|
|
// have to find a place to put the blockquote
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsINode> parent = node;
|
|
|
|
nsCOMPtr<nsINode> topChild = node;
|
|
|
|
while (!CanContainTag(*parent, *nsGkAtoms::blockquote)) {
|
|
|
|
NS_ENSURE_TRUE(parent->GetParentNode(), NS_ERROR_FAILURE);
|
2001-01-28 20:13:07 +00:00
|
|
|
topChild = parent;
|
2014-11-02 12:04:13 +00:00
|
|
|
parent = parent->GetParentNode();
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (parent != node)
|
|
|
|
{
|
|
|
|
// we need to split up to the child of parent
|
2014-11-02 12:04:13 +00:00
|
|
|
res = SplitNodeDeep(GetAsDOMNode(topChild), GetAsDOMNode(node),
|
|
|
|
offset, &offset);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// make a blockquote
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<Element> newBQ = CreateNode(nsGkAtoms::blockquote, parent, offset);
|
|
|
|
NS_ENSURE_STATE(newBQ);
|
2001-01-28 20:13:07 +00:00
|
|
|
// put a space in it so layout will draw the list item
|
|
|
|
res = selection->Collapse(newBQ,0);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-04-07 00:45:26 +00:00
|
|
|
res = InsertText(NS_LITERAL_STRING(" "));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 20:13:07 +00:00
|
|
|
// reposition selection to before the space character
|
2010-06-11 02:46:51 +00:00
|
|
|
res = GetStartNodeAndOffset(selection, getter_AddRefs(node), &offset);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 20:13:07 +00:00
|
|
|
res = selection->Collapse(node,0);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
2000-12-06 23:11:21 +00:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
//TODO: IMPLEMENT ALIGNMENT!
|
2000-12-06 23:11:21 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::Align(const nsAString& aAlignType)
|
2000-12-06 23:11:21 +00:00
|
|
|
{
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2000-12-06 23:11:21 +00:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
2012-08-12 18:28:26 +00:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::align, nsIEditor::eNext);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool cancel, handled;
|
2000-12-06 23:11:21 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Find out if the selection is collapsed:
|
2012-06-08 14:20:55 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2012-08-12 18:28:26 +00:00
|
|
|
nsTextRulesInfo ruleInfo(EditAction::align);
|
2001-01-28 20:13:07 +00:00
|
|
|
ruleInfo.alignType = &aAlignType;
|
2012-05-22 09:37:17 +00:00
|
|
|
nsresult res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (cancel || NS_FAILED(res))
|
2000-12-06 23:11:21 +00:00
|
|
|
return res;
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
return res;
|
|
|
|
}
|
2000-05-05 20:42:36 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
already_AddRefed<Element>
|
|
|
|
nsHTMLEditor::GetElementOrParentByTagName(const nsAString& aTagName,
|
|
|
|
nsINode* aNode)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2014-04-28 11:54:46 +00:00
|
|
|
MOZ_ASSERT(!aTagName.IsEmpty());
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
nsCOMPtr<nsINode> node = aNode;
|
|
|
|
if (!node) {
|
2001-01-28 20:13:07 +00:00
|
|
|
// If no node supplied, get it from anchor node of current selection
|
2012-12-02 08:59:41 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2014-04-28 11:54:46 +00:00
|
|
|
NS_ENSURE_TRUE(selection, nullptr);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2012-12-02 08:59:41 +00:00
|
|
|
nsCOMPtr<nsINode> anchorNode = selection->GetAnchorNode();
|
2014-04-28 11:54:46 +00:00
|
|
|
NS_ENSURE_TRUE(anchorNode, nullptr);
|
1999-11-29 08:28:46 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Try to get the actual selected node
|
2012-12-02 08:59:41 +00:00
|
|
|
if (anchorNode->HasChildNodes() && anchorNode->IsContent()) {
|
2014-04-28 11:54:46 +00:00
|
|
|
node = anchorNode->GetChildAt(selection->AnchorOffset());
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
}
|
2014-04-28 11:54:46 +00:00
|
|
|
// Anchor node is probably a text node - just use that
|
|
|
|
if (!node) {
|
|
|
|
node = anchorNode;
|
2012-12-02 08:59:41 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2012-12-02 08:59:41 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
nsCOMPtr<Element> current;
|
|
|
|
if (node->IsElement()) {
|
|
|
|
current = node->AsElement();
|
|
|
|
} else if (node->GetParentElement()) {
|
|
|
|
current = node->GetParentElement();
|
|
|
|
} else {
|
|
|
|
// Neither aNode nor its parent is an element, so no ancestor is
|
|
|
|
MOZ_ASSERT(!node->GetParentNode() ||
|
|
|
|
!node->GetParentNode()->GetParentNode());
|
|
|
|
return nullptr;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2000-10-11 22:50:14 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
nsAutoString tagName(aTagName);
|
|
|
|
ToLowerCase(tagName);
|
|
|
|
bool getLink = IsLinkTag(tagName);
|
|
|
|
bool getNamedAnchor = IsNamedAnchorTag(tagName);
|
|
|
|
if (getLink || getNamedAnchor) {
|
2014-05-26 18:55:08 +00:00
|
|
|
tagName.Assign('a');
|
2014-04-28 11:54:46 +00:00
|
|
|
}
|
|
|
|
bool findTableCell = tagName.EqualsLiteral("td");
|
|
|
|
bool findList = tagName.EqualsLiteral("list");
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
for (; current; current = current->GetParentElement()) {
|
2001-01-28 20:13:07 +00:00
|
|
|
// Test if we have a link (an anchor with href set)
|
2014-04-28 11:54:46 +00:00
|
|
|
if ((getLink && nsHTMLEditUtils::IsLink(current)) ||
|
|
|
|
(getNamedAnchor && nsHTMLEditUtils::IsNamedAnchor(current))) {
|
|
|
|
return current.forget();
|
|
|
|
}
|
|
|
|
if (findList) {
|
|
|
|
// Match "ol", "ul", or "dl" for lists
|
|
|
|
if (nsHTMLEditUtils::IsList(current)) {
|
|
|
|
return current.forget();
|
|
|
|
}
|
|
|
|
} else if (findTableCell) {
|
|
|
|
// Table cells are another special case: match either "td" or "th"
|
|
|
|
if (nsHTMLEditUtils::IsTableCell(current)) {
|
|
|
|
return current.forget();
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
}
|
2014-04-28 11:54:46 +00:00
|
|
|
} else if (current->NodeName().Equals(tagName,
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
|
|
|
return current.forget();
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
// Stop searching if parent is a body tag. Note: Originally used IsRoot to
|
|
|
|
// stop at table cells, but that's too messy when you are trying to find
|
|
|
|
// the parent table
|
|
|
|
if (current->GetParentElement() &&
|
|
|
|
current->GetParentElement()->Tag() == nsGkAtoms::body) {
|
2001-01-28 20:13:07 +00:00
|
|
|
break;
|
2014-04-28 11:54:46 +00:00
|
|
|
}
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
}
|
2012-12-02 08:59:41 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetElementOrParentByTagName(const nsAString& aTagName,
|
|
|
|
nsIDOMNode* aNode,
|
|
|
|
nsIDOMElement** aReturn)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(!aTagName.IsEmpty(), NS_ERROR_NULL_POINTER);
|
|
|
|
NS_ENSURE_TRUE(aReturn, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
nsCOMPtr<Element> parent =
|
|
|
|
GetElementOrParentByTagName(aTagName, node);
|
|
|
|
nsCOMPtr<nsIDOMElement> ret = do_QueryInterface(parent);
|
|
|
|
|
|
|
|
if (!ret) {
|
2012-12-02 08:59:41 +00:00
|
|
|
return NS_EDITOR_ELEMENT_NOT_FOUND;
|
1999-11-29 08:28:46 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
ret.forget(aReturn);
|
2012-12-02 08:59:41 +00:00
|
|
|
return NS_OK;
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
|
|
|
|
2000-08-10 03:43:09 +00:00
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::GetSelectedElement(const nsAString& aTagName, nsIDOMElement** aReturn)
|
2000-08-10 03:43:09 +00:00
|
|
|
{
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(aReturn , NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// default is null - no element found
|
2012-07-30 14:20:58 +00:00
|
|
|
*aReturn = nullptr;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// First look for a single element in selection
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2000-08-30 02:44:08 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool bNodeFound = false;
|
2012-05-18 08:29:39 +00:00
|
|
|
bool isCollapsed = selection->Collapsed();
|
2000-08-30 02:44:08 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString domTagName;
|
|
|
|
nsAutoString TagName(aTagName);
|
2001-12-17 07:14:49 +00:00
|
|
|
ToLowerCase(TagName);
|
2001-01-28 20:13:07 +00:00
|
|
|
// Empty string indicates we should match any element tag
|
2011-09-29 06:19:26 +00:00
|
|
|
bool anyTag = (TagName.IsEmpty());
|
|
|
|
bool isLinkTag = IsLinkTag(TagName);
|
|
|
|
bool isNamedAnchorTag = IsNamedAnchorTag(TagName);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> selectedElement;
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(0);
|
|
|
|
NS_ENSURE_STATE(range);
|
2000-08-30 02:44:08 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> startParent;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t startOffset, endOffset;
|
2014-11-02 12:04:13 +00:00
|
|
|
nsresult res = range->GetStartContainer(getter_AddRefs(startParent));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 20:13:07 +00:00
|
|
|
res = range->GetStartOffset(&startOffset);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-08-30 22:49:08 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> endParent;
|
|
|
|
res = range->GetEndContainer(getter_AddRefs(endParent));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 20:13:07 +00:00
|
|
|
res = range->GetEndOffset(&endOffset);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-08-30 22:49:08 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Optimization for a single selected element
|
|
|
|
if (startParent && startParent == endParent && (endOffset-startOffset) == 1)
|
2000-08-30 02:44:08 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> selectedNode = GetChildAt(startParent, startOffset);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, NS_OK);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (selectedNode)
|
1999-05-05 04:05:19 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
selectedNode->GetNodeName(domTagName);
|
2001-12-17 07:14:49 +00:00
|
|
|
ToLowerCase(domTagName);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// Test for appropriate node type requested
|
|
|
|
if (anyTag || (TagName == domTagName) ||
|
|
|
|
(isLinkTag && nsHTMLEditUtils::IsLink(selectedNode)) ||
|
|
|
|
(isNamedAnchorTag && nsHTMLEditUtils::IsNamedAnchor(selectedNode)))
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
bNodeFound = true;
|
2001-01-28 20:13:07 +00:00
|
|
|
selectedElement = do_QueryInterface(selectedNode);
|
|
|
|
}
|
2000-04-25 14:15:04 +00:00
|
|
|
}
|
|
|
|
}
|
1999-05-05 04:05:19 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!bNodeFound)
|
2000-04-25 14:15:04 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if (isLinkTag)
|
2000-04-25 14:15:04 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Link tag is a special case - we return the anchor node
|
|
|
|
// found for any selection that is totally within a link,
|
|
|
|
// included a collapsed selection (just a caret in a link)
|
|
|
|
nsCOMPtr<nsIDOMNode> anchorNode;
|
|
|
|
res = selection->GetAnchorNode(getter_AddRefs(anchorNode));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t anchorOffset = -1;
|
2001-01-28 20:13:07 +00:00
|
|
|
if (anchorNode)
|
|
|
|
selection->GetAnchorOffset(&anchorOffset);
|
2013-02-07 14:17:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> focusNode;
|
|
|
|
res = selection->GetFocusNode(getter_AddRefs(focusNode));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t focusOffset = -1;
|
2001-01-28 20:13:07 +00:00
|
|
|
if (focusNode)
|
|
|
|
selection->GetFocusOffset(&focusOffset);
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Link node must be the same for both ends of selection
|
|
|
|
if (NS_SUCCEEDED(res) && anchorNode)
|
2000-04-25 14:15:04 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> parentLinkOfAnchor;
|
2001-11-14 01:33:42 +00:00
|
|
|
res = GetElementOrParentByTagName(NS_LITERAL_STRING("href"), anchorNode, getter_AddRefs(parentLinkOfAnchor));
|
2001-01-28 20:13:07 +00:00
|
|
|
// XXX: ERROR_HANDLING can parentLinkOfAnchor be null?
|
|
|
|
if (NS_SUCCEEDED(res) && parentLinkOfAnchor)
|
|
|
|
{
|
|
|
|
if (isCollapsed)
|
|
|
|
{
|
|
|
|
// We have just a caret in the link
|
2011-10-17 14:59:28 +00:00
|
|
|
bNodeFound = true;
|
2001-01-28 20:13:07 +00:00
|
|
|
} else if(focusNode)
|
|
|
|
{ // Link node must be the same for both ends of selection
|
|
|
|
nsCOMPtr<nsIDOMElement> parentLinkOfFocus;
|
2001-11-14 01:33:42 +00:00
|
|
|
res = GetElementOrParentByTagName(NS_LITERAL_STRING("href"), focusNode, getter_AddRefs(parentLinkOfFocus));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_SUCCEEDED(res) && parentLinkOfFocus == parentLinkOfAnchor)
|
2011-10-17 14:59:28 +00:00
|
|
|
bNodeFound = true;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2013-02-07 14:17:47 +00:00
|
|
|
|
2001-01-28 20:13:07 +00: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 14:59:28 +00:00
|
|
|
bNodeFound = true;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
1999-05-05 04:05:19 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!isCollapsed) // Don't bother to examine selection if it is collapsed
|
2000-05-12 14:57:03 +00:00
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<nsRange> currange = selection->GetRangeAt(0);
|
2013-02-07 14:17:47 +00:00
|
|
|
if (currange) {
|
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2013-02-07 14:17:47 +00:00
|
|
|
iter->Init(currange);
|
|
|
|
// loop through the content iterator for each content node
|
|
|
|
while (!iter->IsDone())
|
2000-05-12 14:57:03 +00:00
|
|
|
{
|
2013-02-07 14:17:47 +00: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 14:57:03 +00:00
|
|
|
{
|
2013-02-07 14:17:47 +00:00
|
|
|
// If we already found a node, then we have another element,
|
|
|
|
// thus there's not just one element selected
|
|
|
|
if (bNodeFound)
|
2000-05-12 14:57:03 +00:00
|
|
|
{
|
2013-02-07 14:17:47 +00:00
|
|
|
bNodeFound = false;
|
|
|
|
break;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2013-02-07 14:17:47 +00:00
|
|
|
selectedElement->GetNodeName(domTagName);
|
|
|
|
ToLowerCase(domTagName);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2013-02-07 14:17:47 +00:00
|
|
|
if (anyTag)
|
|
|
|
{
|
|
|
|
// Get name of first selected element
|
|
|
|
selectedElement->GetTagName(TagName);
|
|
|
|
ToLowerCase(TagName);
|
|
|
|
anyTag = false;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2013-02-07 14:17:47 +00: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)) )
|
|
|
|
{
|
|
|
|
bNodeFound = true;
|
|
|
|
} else if (TagName == domTagName) { // All other tag names are handled here
|
|
|
|
bNodeFound = true;
|
|
|
|
}
|
|
|
|
if (!bNodeFound)
|
|
|
|
{
|
|
|
|
// Check if node we have is really part of the selection???
|
|
|
|
break;
|
2000-05-12 14:57:03 +00:00
|
|
|
}
|
|
|
|
}
|
2013-02-07 14:17:47 +00:00
|
|
|
iter->Next();
|
2000-05-12 14:57:03 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
} else {
|
2013-02-07 14:17:47 +00:00
|
|
|
// Should never get here?
|
|
|
|
isCollapsed = true;
|
|
|
|
NS_WARNING("isCollapsed was FALSE, but no elements found in selection\n");
|
2000-05-12 14:57:03 +00:00
|
|
|
}
|
|
|
|
}
|
1999-04-20 17:47:12 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
if (bNodeFound)
|
2000-03-28 00:34:26 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
*aReturn = selectedElement;
|
|
|
|
if (selectedElement)
|
|
|
|
{
|
|
|
|
// Getters must addref
|
|
|
|
NS_ADDREF(*aReturn);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else res = NS_EDITOR_ELEMENT_NOT_FOUND;
|
2000-03-28 00:34:26 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
return res;
|
1999-04-20 17:47:12 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 11:54:47 +00:00
|
|
|
already_AddRefed<Element>
|
|
|
|
nsHTMLEditor::CreateElementWithDefaults(const nsAString& aTagName)
|
2000-04-25 14:15:04 +00:00
|
|
|
{
|
2014-04-28 11:54:47 +00:00
|
|
|
MOZ_ASSERT(!aTagName.IsEmpty());
|
2000-06-08 14:47:29 +00:00
|
|
|
|
2014-04-28 11:54:47 +00:00
|
|
|
nsAutoString tagName(aTagName);
|
|
|
|
ToLowerCase(tagName);
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString realTagName;
|
2000-09-05 23:26:48 +00:00
|
|
|
|
2014-04-28 11:54:47 +00:00
|
|
|
if (IsLinkTag(tagName) || IsNamedAnchorTag(tagName)) {
|
2014-05-26 18:55:08 +00:00
|
|
|
realTagName.Assign('a');
|
2001-01-28 20:13:07 +00:00
|
|
|
} else {
|
2014-04-28 11:54:47 +00:00
|
|
|
realTagName = tagName;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2014-04-28 11:54:47 +00:00
|
|
|
// We don't use editor's CreateElement because we don't want to go through
|
|
|
|
// the transaction system
|
2000-09-05 23:26:48 +00:00
|
|
|
|
2014-04-28 11:54:47 +00:00
|
|
|
// New call to use instead to get proper HTML element, bug 39919
|
2014-08-13 11:53:33 +00:00
|
|
|
nsCOMPtr<Element> newElement =
|
|
|
|
CreateHTMLContent(nsCOMPtr<nsIAtom>(do_GetAtom(realTagName)));
|
|
|
|
if (!newElement) {
|
2014-04-28 11:54:47 +00:00
|
|
|
return nullptr;
|
2014-04-28 11:54:47 +00:00
|
|
|
}
|
2000-09-05 23:26:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Mark the new element dirty, so it will be formatted
|
2014-08-13 11:53:33 +00:00
|
|
|
ErrorResult rv;
|
2014-04-28 11:54:47 +00:00
|
|
|
newElement->SetAttribute(NS_LITERAL_STRING("_moz_dirty"), EmptyString(), rv);
|
2000-09-05 23:26:48 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Set default values for new elements
|
2014-04-28 11:54:47 +00:00
|
|
|
if (tagName.EqualsLiteral("table")) {
|
|
|
|
newElement->SetAttribute(NS_LITERAL_STRING("cellpadding"),
|
|
|
|
NS_LITERAL_STRING("2"), rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv.ErrorCode(), nullptr);
|
|
|
|
newElement->SetAttribute(NS_LITERAL_STRING("cellspacing"),
|
|
|
|
NS_LITERAL_STRING("2"), rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv.ErrorCode(), nullptr);
|
|
|
|
newElement->SetAttribute(NS_LITERAL_STRING("border"),
|
|
|
|
NS_LITERAL_STRING("1"), rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv.ErrorCode(), nullptr);
|
|
|
|
} else if (tagName.EqualsLiteral("td")) {
|
|
|
|
nsresult res = SetAttributeOrEquivalent(
|
|
|
|
static_cast<nsIDOMElement*>(newElement->AsDOMNode()),
|
|
|
|
NS_LITERAL_STRING("valign"), NS_LITERAL_STRING("top"), true);
|
|
|
|
NS_ENSURE_SUCCESS(res, nullptr);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
// ADD OTHER TAGS HERE
|
2000-09-05 23:26:48 +00:00
|
|
|
|
2014-04-28 11:54:47 +00:00
|
|
|
return newElement.forget();
|
|
|
|
}
|
2000-04-25 14:15:04 +00:00
|
|
|
|
2014-04-28 11:54:47 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::CreateElementWithDefaults(const nsAString& aTagName, nsIDOMElement** aReturn)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(!aTagName.IsEmpty() && aReturn, NS_ERROR_NULL_POINTER);
|
|
|
|
*aReturn = nullptr;
|
|
|
|
|
|
|
|
nsCOMPtr<Element> newElement = CreateElementWithDefaults(aTagName);
|
|
|
|
nsCOMPtr<nsIDOMElement> ret = do_QueryInterface(newElement);
|
|
|
|
NS_ENSURE_TRUE(ret, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
ret.forget(aReturn);
|
|
|
|
return NS_OK;
|
2000-04-25 14:15:04 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::InsertLinkAroundSelection(nsIDOMElement* aAnchorElement)
|
2000-06-08 14:47:29 +00:00
|
|
|
{
|
2012-05-18 08:29:39 +00:00
|
|
|
NS_ENSURE_TRUE(aAnchorElement, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// We must have a real selection
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2012-05-18 08:29:39 +00:00
|
|
|
if (selection->Collapsed()) {
|
|
|
|
NS_WARNING("InsertLinkAroundSelection called but there is no selection!!!");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Be sure we were given an anchor element
|
|
|
|
nsCOMPtr<nsIDOMHTMLAnchorElement> anchor = do_QueryInterface(aAnchorElement);
|
|
|
|
if (!anchor) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-10-30 23:24:44 +00:00
|
|
|
|
2012-05-18 08:29:39 +00:00
|
|
|
nsAutoString href;
|
2014-11-02 12:04:13 +00:00
|
|
|
nsresult res = anchor->GetHref(href);
|
2012-05-18 08:29:39 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
if (href.IsEmpty()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-10-30 23:24:44 +00:00
|
|
|
|
2012-05-18 08:29:39 +00:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
2001-10-30 23:24:44 +00:00
|
|
|
|
2012-05-18 08:29:39 +00:00
|
|
|
// Set all attributes found on the supplied anchor element
|
2013-03-10 08:00:33 +00:00
|
|
|
nsCOMPtr<nsIDOMMozNamedAttrMap> attrMap;
|
2012-05-18 08:29:39 +00:00
|
|
|
aAnchorElement->GetAttributes(getter_AddRefs(attrMap));
|
|
|
|
NS_ENSURE_TRUE(attrMap, NS_ERROR_FAILURE);
|
2001-10-30 23:24:44 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t count;
|
2012-05-18 08:29:39 +00:00
|
|
|
attrMap->GetLength(&count);
|
|
|
|
nsAutoString name, value;
|
2001-10-30 23:24:44 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2013-03-10 08:00:33 +00:00
|
|
|
nsCOMPtr<nsIDOMAttr> attribute;
|
|
|
|
res = attrMap->Item(i, getter_AddRefs(attribute));
|
2012-05-18 08:29:39 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-10-30 23:24:44 +00:00
|
|
|
|
2012-05-18 08:29:39 +00:00
|
|
|
if (attribute) {
|
|
|
|
// We must clear the string buffers
|
|
|
|
// because GetName, GetValue appends to previous string!
|
|
|
|
name.Truncate();
|
|
|
|
value.Truncate();
|
2001-10-30 23:24:44 +00:00
|
|
|
|
2012-05-18 08:29:39 +00:00
|
|
|
res = attribute->GetName(name);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
|
|
|
res = attribute->GetValue(value);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
2014-08-29 11:43:24 +00:00
|
|
|
res = SetInlineProperty(nsGkAtoms::a, name, value);
|
2012-05-18 08:29:39 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-06-08 14:47:29 +00:00
|
|
|
}
|
|
|
|
}
|
2012-05-18 08:29:39 +00:00
|
|
|
return NS_OK;
|
2000-06-08 14:47:29 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 06:14:13 +00:00
|
|
|
nsresult
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::SetHTMLBackgroundColor(const nsAString& aColor)
|
2000-06-08 14:47:29 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_PRECONDITION(mDocWeak, "Missing Editor DOM Document");
|
|
|
|
|
|
|
|
// Find a selected or enclosing table element to set background on
|
|
|
|
nsCOMPtr<nsIDOMElement> element;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t selectedCount;
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString tagName;
|
2002-09-13 04:19:30 +00:00
|
|
|
nsresult res = GetSelectedOrParentTableElement(tagName, &selectedCount,
|
|
|
|
getter_AddRefs(element));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-06-08 14:47:29 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool setColor = !aColor.IsEmpty();
|
2000-06-08 14:47:29 +00:00
|
|
|
|
2002-01-14 20:31:43 +00:00
|
|
|
NS_NAMED_LITERAL_STRING(bgcolor, "bgcolor");
|
2001-01-28 20:13:07 +00:00
|
|
|
if (element)
|
2000-06-08 14:47:29 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
if (selectedCount > 0)
|
2000-06-08 14:47:29 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// Traverse all selected cells
|
|
|
|
nsCOMPtr<nsIDOMElement> cell;
|
2012-07-30 14:20:58 +00:00
|
|
|
res = GetFirstSelectedCell(nullptr, getter_AddRefs(cell));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_SUCCEEDED(res) && cell)
|
|
|
|
{
|
|
|
|
while(cell)
|
|
|
|
{
|
|
|
|
if (setColor)
|
2002-01-14 20:31:43 +00:00
|
|
|
res = SetAttribute(cell, bgcolor, aColor);
|
2001-01-28 20:13:07 +00:00
|
|
|
else
|
2002-01-14 20:31:43 +00:00
|
|
|
res = RemoveAttribute(cell, bgcolor);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) break;
|
2000-06-08 14:47:29 +00:00
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
GetNextSelectedCell(nullptr, getter_AddRefs(cell));
|
2001-01-28 20:13:07 +00:00
|
|
|
};
|
|
|
|
return res;
|
|
|
|
}
|
2000-06-08 14:47:29 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
// If we failed to find a cell, fall through to use originally-found element
|
|
|
|
} else {
|
|
|
|
// No table element -- set the background color on the body tag
|
2011-12-03 21:50:15 +00:00
|
|
|
element = do_QueryInterface(GetRoot());
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(element, NS_ERROR_NULL_POINTER);
|
2000-06-08 14:47:29 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
// Use the editor method that goes through the transaction system
|
|
|
|
if (setColor)
|
2002-01-14 20:31:43 +00:00
|
|
|
res = SetAttribute(element, bgcolor, aColor);
|
2001-01-28 20:13:07 +00:00
|
|
|
else
|
2002-01-14 20:31:43 +00:00
|
|
|
res = RemoveAttribute(element, bgcolor);
|
2000-06-08 14:47:29 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
return res;
|
2000-06-08 14:47:29 +00:00
|
|
|
}
|
|
|
|
|
2002-03-23 22:08:20 +00:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::SetBodyAttribute(const nsAString& aAttribute, const nsAString& aValue)
|
2000-04-25 14:15:04 +00:00
|
|
|
{
|
2001-01-28 20:13:07 +00:00
|
|
|
// TODO: Check selection for Cell, Row, Column or table and do color on appropriate level
|
2000-04-25 14:15:04 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_ASSERTION(mDocWeak, "Missing Editor DOM Document");
|
|
|
|
|
|
|
|
// Set the background color attribute on the body tag
|
2011-12-03 21:50:15 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> bodyElement = do_QueryInterface(GetRoot());
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(bodyElement, NS_ERROR_NULL_POINTER);
|
2005-03-24 19:00:01 +00:00
|
|
|
|
|
|
|
// Use the editor method that goes through the transaction system
|
|
|
|
return SetAttribute(bodyElement, aAttribute, aValue);
|
2000-04-25 14:15:04 +00:00
|
|
|
}
|
|
|
|
|
2001-08-07 21:39:07 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetLinkedObjects(nsISupportsArray** aNodeList)
|
|
|
|
{
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(aNodeList, NS_ERROR_NULL_POINTER);
|
2001-08-07 21:39:07 +00:00
|
|
|
|
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
res = NS_NewISupportsArray(aNodeList);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(*aNodeList, NS_ERROR_NULL_POINTER);
|
2001-08-07 21:39:07 +00:00
|
|
|
|
2003-07-08 20:22:56 +00:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &res);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(iter, NS_ERROR_NULL_POINTER);
|
2001-08-07 21:39:07 +00:00
|
|
|
if ((NS_SUCCEEDED(res)))
|
|
|
|
{
|
2012-03-24 08:19:14 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = GetDocument();
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
|
2001-08-07 21:39:07 +00:00
|
|
|
|
2010-04-30 13:12:05 +00:00
|
|
|
iter->Init(doc->GetRootElement());
|
2001-08-07 21:39:07 +00:00
|
|
|
|
|
|
|
// loop through the content iterator for each content node
|
2004-01-24 00:46:17 +00:00
|
|
|
while (!iter->IsDone())
|
2001-08-07 21:39:07 +00:00
|
|
|
{
|
2004-01-24 00:46:17 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> node (do_QueryInterface(iter->GetCurrentNode()));
|
2001-08-07 21:39:07 +00: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 00:46:17 +00:00
|
|
|
|
|
|
|
(*aNodeList)->AppendElement(isupp);
|
2001-08-07 21:39:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
iter->Next();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-04-25 14:15:04 +00:00
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::AddStyleSheet(const nsAString &aURL)
|
|
|
|
{
|
|
|
|
// Enable existing sheet if already loaded.
|
|
|
|
if (EnableExistingStyleSheet(aURL))
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Lose the previously-loaded sheet so there's nothing to replace
|
|
|
|
// This pattern is different from Override methods because
|
|
|
|
// we must wait to remove mLastStyleSheetURL and add new sheet
|
|
|
|
// at the same time (in StyleSheetLoaded callback) so they are undoable together
|
|
|
|
mLastStyleSheetURL.Truncate();
|
|
|
|
return ReplaceStyleSheet(aURL);
|
|
|
|
}
|
2000-01-18 21:50:15 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_IMETHODIMP
|
2002-09-13 19:47:38 +00:00
|
|
|
nsHTMLEditor::ReplaceStyleSheet(const nsAString& aURL)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2002-09-13 19:47:38 +00:00
|
|
|
// Enable existing sheet if already loaded.
|
|
|
|
if (EnableExistingStyleSheet(aURL))
|
2000-01-18 21:50:15 +00:00
|
|
|
{
|
2002-09-13 19:47:38 +00:00
|
|
|
// Disable last sheet if not the same as new one
|
2009-06-27 05:31:36 +00:00
|
|
|
if (!mLastStyleSheetURL.IsEmpty() && !mLastStyleSheetURL.Equals(aURL))
|
2011-10-17 14:59:28 +00:00
|
|
|
return EnableStyleSheet(mLastStyleSheetURL, false);
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2000-01-18 21:50:15 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
// Make sure the pres shell doesn't disappear during the load.
|
2011-03-11 05:40:30 +00:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, NS_ERROR_NOT_INITIALIZED);
|
2011-05-22 12:43:13 +00:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uaURI;
|
2010-03-02 21:00:53 +00:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uaURI), aURL);
|
2002-09-13 19:47:38 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
return ps->GetDocument()->CSSLoader()->
|
2012-07-30 14:20:58 +00:00
|
|
|
LoadSheet(uaURI, nullptr, EmptyCString(), this);
|
2002-09-13 19:47:38 +00:00
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-09-13 19:47:38 +00:00
|
|
|
nsHTMLEditor::RemoveStyleSheet(const nsAString &aURL)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2014-06-20 10:32:49 +00:00
|
|
|
nsRefPtr<CSSStyleSheet> sheet;
|
2002-09-13 19:47:38 +00:00
|
|
|
nsresult rv = GetStyleSheetForURL(aURL, getter_AddRefs(sheet));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(sheet, NS_ERROR_UNEXPECTED);
|
2002-09-13 19:47:38 +00:00
|
|
|
|
2007-07-02 14:01:27 +00:00
|
|
|
nsRefPtr<RemoveStyleSheetTxn> txn;
|
|
|
|
rv = CreateTxnForRemoveStyleSheet(sheet, getter_AddRefs(txn));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!txn) rv = NS_ERROR_NULL_POINTER;
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2000-01-18 21:50:15 +00:00
|
|
|
{
|
2003-04-04 20:50:25 +00:00
|
|
|
rv = DoTransaction(txn);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-09-13 19:47:38 +00:00
|
|
|
mLastStyleSheetURL.Truncate(); // forget it
|
|
|
|
|
|
|
|
// Remove it from our internal list
|
|
|
|
rv = RemoveStyleSheetFromList(aURL);
|
2000-01-18 21:50:15 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
return rv;
|
1999-11-10 23:42:11 +00:00
|
|
|
}
|
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
NS_IMETHODIMP
|
2002-09-13 19:47:38 +00:00
|
|
|
nsHTMLEditor::AddOverrideStyleSheet(const nsAString& aURL)
|
|
|
|
{
|
|
|
|
// Enable existing sheet if already loaded.
|
|
|
|
if (EnableExistingStyleSheet(aURL))
|
|
|
|
return NS_OK;
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
// Make sure the pres shell doesn't disappear during the load.
|
2011-05-22 12:43:13 +00:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uaURI;
|
2010-03-02 21:00:53 +00:00
|
|
|
nsresult rv = NS_NewURI(getter_AddRefs(uaURI), aURL);
|
2002-09-13 19:47:38 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2002-12-03 05:48:14 +00:00
|
|
|
// We MUST ONLY load synchronous local files (no @import)
|
2005-09-12 18:41:15 +00:00
|
|
|
// XXXbz Except this will actually try to load remote files
|
|
|
|
// synchronously, of course..
|
2014-06-20 10:32:49 +00:00
|
|
|
nsRefPtr<CSSStyleSheet> sheet;
|
2006-06-15 03:20:19 +00:00
|
|
|
// Editor override style sheets may want to style Gecko anonymous boxes
|
2010-03-02 21:00:53 +00:00
|
|
|
rv = ps->GetDocument()->CSSLoader()->
|
2011-10-17 14:59:28 +00:00
|
|
|
LoadSheetSync(uaURI, true, true, getter_AddRefs(sheet));
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
// Synchronous loads should ALWAYS return completed
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(sheet, NS_ERROR_NULL_POINTER);
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
// Add the override style sheet
|
|
|
|
// (This checks if already exists)
|
2004-01-07 22:30:53 +00:00
|
|
|
ps->AddOverrideStyleSheet(sheet);
|
2002-09-13 19:47:38 +00:00
|
|
|
|
2004-01-07 22:30:53 +00:00
|
|
|
ps->ReconstructStyleData();
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
// Save as the last-loaded sheet
|
|
|
|
mLastOverrideStyleSheetURL = aURL;
|
|
|
|
|
|
|
|
//Add URL and style sheet to our lists
|
|
|
|
return AddNewStyleSheetToList(aURL, sheet);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::ReplaceOverrideStyleSheet(const nsAString& aURL)
|
|
|
|
{
|
|
|
|
// Enable existing sheet if already loaded.
|
|
|
|
if (EnableExistingStyleSheet(aURL))
|
|
|
|
{
|
|
|
|
// Disable last sheet if not the same as new one
|
|
|
|
if (!mLastOverrideStyleSheetURL.IsEmpty() && !mLastOverrideStyleSheetURL.Equals(aURL))
|
2011-10-17 14:59:28 +00:00
|
|
|
return EnableStyleSheet(mLastOverrideStyleSheetURL, false);
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
// Remove the previous sheet
|
|
|
|
if (!mLastOverrideStyleSheetURL.IsEmpty())
|
|
|
|
RemoveOverrideStyleSheet(mLastOverrideStyleSheetURL);
|
|
|
|
|
|
|
|
return AddOverrideStyleSheet(aURL);
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Do NOT use transaction system for override style sheets
|
|
|
|
NS_IMETHODIMP
|
2002-09-13 19:47:38 +00:00
|
|
|
nsHTMLEditor::RemoveOverrideStyleSheet(const nsAString &aURL)
|
1999-11-10 23:42:11 +00:00
|
|
|
{
|
2014-06-20 10:32:49 +00:00
|
|
|
nsRefPtr<CSSStyleSheet> sheet;
|
2005-06-09 22:50:41 +00: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-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(sheet, NS_OK); /// Don't fail if sheet not found
|
2002-09-13 19:47:38 +00:00
|
|
|
|
2011-03-11 05:40:30 +00:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, NS_ERROR_NOT_INITIALIZED);
|
2011-05-22 12:43:13 +00:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
|
1999-11-10 23:42:11 +00:00
|
|
|
|
2004-01-07 22:30:53 +00:00
|
|
|
ps->RemoveOverrideStyleSheet(sheet);
|
|
|
|
ps->ReconstructStyleData();
|
1999-11-10 23:42:11 +00:00
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
// Remove it from our internal list
|
2005-06-09 22:50:41 +00:00
|
|
|
return rv;
|
1999-11-10 23:42:11 +00:00
|
|
|
}
|
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::EnableStyleSheet(const nsAString &aURL, bool aEnable)
|
1999-11-10 23:42:11 +00:00
|
|
|
{
|
2014-06-20 10:32:49 +00:00
|
|
|
nsRefPtr<CSSStyleSheet> sheet;
|
2002-09-13 19:47:38 +00:00
|
|
|
nsresult rv = GetStyleSheetForURL(aURL, getter_AddRefs(sheet));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(sheet, NS_OK); // Don't fail if sheet not found
|
2002-09-13 19:47:38 +00:00
|
|
|
|
2009-02-19 07:52:54 +00:00
|
|
|
// Ensure the style sheet is owned by our document.
|
2009-09-21 23:05:26 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
2010-05-12 20:18:47 +00:00
|
|
|
sheet->SetOwningDocument(doc);
|
|
|
|
|
2010-05-11 20:41:47 +00:00
|
|
|
return sheet->SetDisabled(!aEnable);
|
1999-11-10 23:42:11 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2002-09-13 19:47:38 +00:00
|
|
|
nsHTMLEditor::EnableExistingStyleSheet(const nsAString &aURL)
|
|
|
|
{
|
2014-06-20 10:32:49 +00:00
|
|
|
nsRefPtr<CSSStyleSheet> sheet;
|
2002-09-13 19:47:38 +00:00
|
|
|
nsresult rv = GetStyleSheetForURL(aURL, getter_AddRefs(sheet));
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, false);
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
// Enable sheet if already loaded.
|
|
|
|
if (sheet)
|
|
|
|
{
|
2009-02-19 07:52:54 +00:00
|
|
|
// Ensure the style sheet is owned by our document.
|
2009-09-21 23:05:26 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
2010-05-12 20:18:47 +00:00
|
|
|
sheet->SetOwningDocument(doc);
|
2009-02-19 07:52:54 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
sheet->SetDisabled(false);
|
|
|
|
return true;
|
2002-09-13 19:47:38 +00:00
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2002-09-13 19:47:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::AddNewStyleSheetToList(const nsAString &aURL,
|
2014-06-20 10:32:49 +00:00
|
|
|
CSSStyleSheet* aStyleSheet)
|
1999-11-10 23:42:11 +00:00
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t countSS = mStyleSheets.Length();
|
|
|
|
uint32_t countU = mStyleSheetURLs.Length();
|
1999-11-10 23:42:11 +00:00
|
|
|
|
2010-10-12 22:30:41 +00:00
|
|
|
if (countSS != countU)
|
2002-09-13 19:47:38 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2000-08-07 06:05:27 +00:00
|
|
|
|
2009-01-18 20:14:14 +00:00
|
|
|
if (!mStyleSheetURLs.AppendElement(aURL))
|
2002-09-13 19:47:38 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2010-05-11 20:41:47 +00:00
|
|
|
return mStyleSheets.AppendElement(aStyleSheet) ? NS_OK : NS_ERROR_UNEXPECTED;
|
2002-09-13 19:47:38 +00:00
|
|
|
}
|
2000-07-12 23:44:56 +00:00
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::RemoveStyleSheetFromList(const nsAString &aURL)
|
|
|
|
{
|
|
|
|
// is it already in the list?
|
2014-05-09 01:03:35 +00:00
|
|
|
size_t foundIndex;
|
2002-09-13 19:47:38 +00:00
|
|
|
foundIndex = mStyleSheetURLs.IndexOf(aURL);
|
2009-02-10 20:39:43 +00:00
|
|
|
if (foundIndex == mStyleSheetURLs.NoIndex)
|
2002-09-13 19:47:38 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Attempt both removals; if one fails there's not much we can do.
|
2010-05-11 20:41:47 +00:00
|
|
|
mStyleSheets.RemoveElementAt(foundIndex);
|
2009-01-18 20:14:14 +00:00
|
|
|
mStyleSheetURLs.RemoveElementAt(foundIndex);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2010-05-11 20:41:47 +00:00
|
|
|
return NS_OK;
|
1999-11-10 23:42:11 +00:00
|
|
|
}
|
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetStyleSheetForURL(const nsAString &aURL,
|
2014-06-20 10:32:49 +00:00
|
|
|
CSSStyleSheet** aStyleSheet)
|
2002-09-13 19:47:38 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aStyleSheet);
|
|
|
|
*aStyleSheet = 0;
|
|
|
|
|
|
|
|
// is it already in the list?
|
2014-05-09 01:03:35 +00:00
|
|
|
size_t foundIndex;
|
2002-09-13 19:47:38 +00:00
|
|
|
foundIndex = mStyleSheetURLs.IndexOf(aURL);
|
2009-02-10 20:39:43 +00:00
|
|
|
if (foundIndex == mStyleSheetURLs.NoIndex)
|
2002-09-13 19:47:38 +00:00
|
|
|
return NS_OK; //No sheet -- don't fail!
|
|
|
|
|
2002-11-12 19:40:11 +00:00
|
|
|
*aStyleSheet = mStyleSheets[foundIndex];
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(*aStyleSheet, NS_ERROR_FAILURE);
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
NS_ADDREF(*aStyleSheet);
|
2002-11-12 19:40:11 +00:00
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2014-06-20 10:32:49 +00:00
|
|
|
nsHTMLEditor::GetURLForStyleSheet(CSSStyleSheet* aStyleSheet,
|
2002-09-13 19:47:38 +00:00
|
|
|
nsAString &aURL)
|
|
|
|
{
|
|
|
|
// is it already in the list?
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t foundIndex = mStyleSheets.IndexOf(aStyleSheet);
|
2002-09-13 19:47:38 +00:00
|
|
|
|
|
|
|
// Don't fail if we don't find it in our list
|
2009-02-10 20:39:43 +00:00
|
|
|
// Note: mStyleSheets is nsCOMArray, so its IndexOf() method
|
|
|
|
// returns -1 on failure.
|
2002-09-13 19:47:38 +00:00
|
|
|
if (foundIndex == -1)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Found it in the list!
|
2009-01-18 20:14:14 +00:00
|
|
|
aURL = mStyleSheetURLs[foundIndex];
|
2002-09-13 19:47:38 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-03-02 21:00:53 +00:00
|
|
|
/*
|
|
|
|
* nsIEditorMailSupport methods
|
|
|
|
*/
|
2001-01-28 20:13:07 +00:00
|
|
|
|
1999-11-24 20:48:59 +00:00
|
|
|
NS_IMETHODIMP
|
2001-01-28 20:13:07 +00:00
|
|
|
nsHTMLEditor::GetEmbeddedObjects(nsISupportsArray** aNodeList)
|
1999-11-10 23:42:11 +00:00
|
|
|
{
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(aNodeList, NS_ERROR_NULL_POINTER);
|
1999-11-10 23:42:11 +00:00
|
|
|
|
2011-11-16 07:50:20 +00:00
|
|
|
nsresult rv = NS_NewISupportsArray(aNodeList);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(*aNodeList, NS_ERROR_NULL_POINTER);
|
1999-11-18 19:43:14 +00:00
|
|
|
|
2003-07-08 20:22:56 +00:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
2011-11-16 07:50:20 +00:00
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &rv);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(iter, NS_ERROR_NULL_POINTER);
|
2011-11-16 07:50:20 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2000-01-10 10:13:58 +00:00
|
|
|
|
2012-03-24 08:19:14 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = GetDocument();
|
2011-11-16 07:50:20 +00:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
|
2000-07-12 23:44:56 +00:00
|
|
|
|
2011-11-16 07:50:20 +00: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 10:13:58 +00:00
|
|
|
}
|
1999-11-24 20:48:59 +00:00
|
|
|
}
|
2011-11-16 07:50:20 +00:00
|
|
|
iter->Next();
|
2000-07-12 23:44:56 +00:00
|
|
|
}
|
1999-11-29 21:15:57 +00:00
|
|
|
|
2011-11-16 07:50:20 +00:00
|
|
|
return rv;
|
1999-11-10 23:42:11 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2012-05-10 14:54:33 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::DeleteSelectionImpl(EDirection aAction,
|
|
|
|
EStripWrappers aStripWrappers)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aStripWrappers == eStrip || aStripWrappers == eNoStrip);
|
|
|
|
|
|
|
|
nsresult res = nsEditor::DeleteSelectionImpl(aAction, aStripWrappers);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
|
|
|
// If we weren't asked to strip any wrappers, we're done.
|
|
|
|
if (aStripWrappers == eNoStrip) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-08 14:20:55 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2012-05-10 14:54:33 +00:00
|
|
|
// Just checking that the selection itself is collapsed doesn't seem to work
|
|
|
|
// right in the multi-range case
|
2012-06-08 14:20:55 +00:00
|
|
|
NS_ENSURE_STATE(selection);
|
|
|
|
NS_ENSURE_STATE(selection->GetAnchorFocusRange());
|
|
|
|
NS_ENSURE_STATE(selection->GetAnchorFocusRange()->Collapsed());
|
2012-05-10 14:54:33 +00:00
|
|
|
|
2012-06-10 12:39:21 +00:00
|
|
|
NS_ENSURE_STATE(selection->GetAnchorNode()->IsContent());
|
|
|
|
nsCOMPtr<nsIContent> content = selection->GetAnchorNode()->AsContent();
|
2012-05-10 14:54:33 +00:00
|
|
|
|
|
|
|
// Don't strip wrappers if this is the only wrapper in the block. Then we'll
|
|
|
|
// add a <br> later, so it won't be an empty wrapper in the end.
|
|
|
|
nsCOMPtr<nsIContent> blockParent = content;
|
2012-06-06 11:19:16 +00:00
|
|
|
while (blockParent && !IsBlockNode(blockParent)) {
|
2012-05-10 14:54:33 +00:00
|
|
|
blockParent = blockParent->GetParent();
|
|
|
|
}
|
2012-06-06 11:19:16 +00:00
|
|
|
if (!blockParent) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-05-10 14:54:33 +00:00
|
|
|
bool emptyBlockParent;
|
|
|
|
res = IsEmptyNode(blockParent, &emptyBlockParent);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
if (emptyBlockParent) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content && !IsBlockNode(content) && !content->Length() &&
|
|
|
|
content->IsEditable() && content != content->GetEditingHost()) {
|
|
|
|
while (content->GetParent() && !IsBlockNode(content->GetParent()) &&
|
|
|
|
content->GetParent()->Length() == 1 &&
|
|
|
|
content->GetParent()->IsEditable() &&
|
|
|
|
content->GetParent() != content->GetEditingHost()) {
|
|
|
|
content = content->GetParent();
|
|
|
|
}
|
|
|
|
res = DeleteNode(content);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::DeleteNode(nsINode* aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> node = do_QueryInterface(aNode);
|
|
|
|
return DeleteNode(node);
|
|
|
|
}
|
|
|
|
|
2012-02-01 10:54:22 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::DeleteNode(nsIDOMNode* aNode)
|
2002-12-22 01:51:14 +00:00
|
|
|
{
|
2007-06-28 02:48:16 +00:00
|
|
|
// do nothing if the node is read-only
|
2012-02-01 10:54:22 +00:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aNode);
|
|
|
|
if (!IsModifiableNode(aNode) && !IsMozEditorBogusNode(content)) {
|
2007-06-28 02:48:16 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2002-12-22 01:51:14 +00:00
|
|
|
return nsEditor::DeleteNode(aNode);
|
|
|
|
}
|
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::DeleteText(nsGenericDOMDataNode& aCharData, uint32_t aOffset,
|
|
|
|
uint32_t aLength)
|
2002-12-22 01:51:14 +00:00
|
|
|
{
|
2014-08-20 12:25:16 +00:00
|
|
|
// Do nothing if the node is read-only
|
|
|
|
if (!IsModifiableNode(&aCharData)) {
|
2007-06-28 02:48:16 +00:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-08-20 12:25:16 +00:00
|
|
|
return nsEditor::DeleteText(aCharData, aOffset, aLength);
|
2002-12-22 01:51:14 +00:00
|
|
|
}
|
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::InsertTextImpl(const nsAString& aStringToInsert,
|
|
|
|
nsCOMPtr<nsINode>* aInOutNode,
|
|
|
|
int32_t* aInOutOffset, nsIDocument* aDoc)
|
2007-06-28 02:48:16 +00:00
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
// Do nothing if the node is read-only
|
2007-06-28 02:48:16 +00:00
|
|
|
if (!IsModifiableNode(*aInOutNode)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
return nsEditor::InsertTextImpl(aStringToInsert, aInOutNode, aInOutOffset,
|
|
|
|
aDoc);
|
2007-06-28 02:48:16 +00:00
|
|
|
}
|
2002-12-22 01:51:14 +00:00
|
|
|
|
2010-06-17 05:31:15 +00:00
|
|
|
void
|
|
|
|
nsHTMLEditor::ContentAppended(nsIDocument *aDocument, nsIContent* aContainer,
|
|
|
|
nsIContent* aFirstNewContent,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aIndexInContainer)
|
2010-06-17 05:31:15 +00:00
|
|
|
{
|
2014-04-06 14:56:08 +00:00
|
|
|
DoContentInserted(aDocument, aContainer, aFirstNewContent, aIndexInContainer,
|
|
|
|
eAppended);
|
2010-06-17 05:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHTMLEditor::ContentInserted(nsIDocument *aDocument, nsIContent* aContainer,
|
2012-08-22 15:56:38 +00:00
|
|
|
nsIContent* aChild, int32_t aIndexInContainer)
|
2014-04-06 14:56:08 +00:00
|
|
|
{
|
|
|
|
DoContentInserted(aDocument, aContainer, aChild, aIndexInContainer,
|
|
|
|
eInserted);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHTMLEditor::DoContentInserted(nsIDocument* aDocument, nsIContent* aContainer,
|
|
|
|
nsIContent* aChild, int32_t aIndexInContainer,
|
|
|
|
InsertedOrAppended aInsertedOrAppended)
|
2010-06-17 05:31:15 +00:00
|
|
|
{
|
2010-11-11 21:40:52 +00:00
|
|
|
if (!aChild) {
|
2010-06-17 05:32:05 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2010-11-18 21:01:12 +00:00
|
|
|
nsCOMPtr<nsIHTMLEditor> kungFuDeathGrip(this);
|
|
|
|
|
2010-06-17 05:32:05 +00:00
|
|
|
if (ShouldReplaceRootElement()) {
|
2012-07-05 07:45:08 +00:00
|
|
|
nsContentUtils::AddScriptRunner(NS_NewRunnableMethod(
|
|
|
|
this, &nsHTMLEditor::ResetRootElementAndEventTarget));
|
2010-06-17 05:31:15 +00:00
|
|
|
}
|
2010-11-11 21:40:52 +00:00
|
|
|
// We don't need to handle our own modifications
|
2010-11-13 20:12:15 +00:00
|
|
|
else if (!mAction && (aContainer ? aContainer->IsEditable() : aDocument->IsEditable())) {
|
2012-02-01 10:54:22 +00:00
|
|
|
if (IsMozEditorBogusNode(aChild)) {
|
2010-11-16 20:43:46 +00:00
|
|
|
// Ignore insertion of the bogus node
|
|
|
|
return;
|
|
|
|
}
|
2013-03-23 03:47:57 +00:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
2010-11-11 21:40:52 +00:00
|
|
|
mRules->DocumentModified();
|
2012-04-25 05:56:32 +00:00
|
|
|
|
|
|
|
// Update spellcheck for only the newly-inserted node (bug 743819)
|
|
|
|
if (mInlineSpellChecker) {
|
2013-02-07 12:09:41 +00:00
|
|
|
nsRefPtr<nsRange> range = new nsRange(aChild);
|
2014-04-06 14:56:08 +00:00
|
|
|
int32_t endIndex = aIndexInContainer + 1;
|
|
|
|
if (aInsertedOrAppended == eAppended) {
|
|
|
|
// Count all the appended nodes
|
|
|
|
nsIContent* sibling = aChild->GetNextSibling();
|
|
|
|
while (sibling) {
|
|
|
|
endIndex++;
|
|
|
|
sibling = sibling->GetNextSibling();
|
|
|
|
}
|
|
|
|
}
|
2012-04-25 05:56:32 +00:00
|
|
|
nsresult res = range->Set(aContainer, aIndexInContainer,
|
2014-04-06 14:56:08 +00:00
|
|
|
aContainer, endIndex);
|
2012-04-25 05:56:32 +00:00
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
mInlineSpellChecker->SpellCheckRange(range);
|
|
|
|
}
|
|
|
|
}
|
2010-11-11 21:40:52 +00:00
|
|
|
}
|
2010-06-17 05:31:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsHTMLEditor::ContentRemoved(nsIDocument *aDocument, nsIContent* aContainer,
|
2012-08-22 15:56:38 +00:00
|
|
|
nsIContent* aChild, int32_t aIndexInContainer,
|
2010-07-21 22:05:17 +00:00
|
|
|
nsIContent* aPreviousSibling)
|
2010-06-17 05:31:15 +00:00
|
|
|
{
|
2010-11-18 21:01:12 +00:00
|
|
|
nsCOMPtr<nsIHTMLEditor> kungFuDeathGrip(this);
|
|
|
|
|
2010-06-17 05:32:05 +00:00
|
|
|
if (SameCOMIdentity(aChild, mRootElement)) {
|
2012-07-05 07:45:08 +00:00
|
|
|
nsContentUtils::AddScriptRunner(NS_NewRunnableMethod(
|
|
|
|
this, &nsHTMLEditor::ResetRootElementAndEventTarget));
|
2010-06-17 05:31:15 +00:00
|
|
|
}
|
2010-11-11 21:40:52 +00:00
|
|
|
// We don't need to handle our own modifications
|
2010-11-13 20:12:15 +00:00
|
|
|
else if (!mAction && (aContainer ? aContainer->IsEditable() : aDocument->IsEditable())) {
|
2012-02-01 10:54:22 +00:00
|
|
|
if (aChild && IsMozEditorBogusNode(aChild)) {
|
2010-11-16 20:43:46 +00:00
|
|
|
// Ignore removal of the bogus node
|
|
|
|
return;
|
|
|
|
}
|
2013-03-23 03:47:57 +00:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
2010-11-11 21:40:52 +00:00
|
|
|
mRules->DocumentModified();
|
|
|
|
}
|
2010-06-17 05:31:15 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
NS_IMETHODIMP_(bool)
|
2007-06-28 02:48:16 +00:00
|
|
|
nsHTMLEditor::IsModifiableNode(nsIDOMNode *aNode)
|
|
|
|
{
|
2011-11-28 12:53:29 +00:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
return IsModifiableNode(node);
|
|
|
|
}
|
2007-06-28 02:48:16 +00:00
|
|
|
|
2011-11-28 12:53:29 +00:00
|
|
|
bool
|
|
|
|
nsHTMLEditor::IsModifiableNode(nsINode *aNode)
|
|
|
|
{
|
|
|
|
return !aNode || aNode->IsEditable();
|
2007-06-28 02:48:16 +00:00
|
|
|
}
|
|
|
|
|
2012-06-24 14:39:11 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetIsSelectionEditable(bool* aIsSelectionEditable)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aIsSelectionEditable);
|
|
|
|
|
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
// Per the editing spec as of June 2012: we have to have a selection whose
|
|
|
|
// start and end nodes are editable, and which share an ancestor editing
|
|
|
|
// host. (Bug 766387.)
|
|
|
|
*aIsSelectionEditable = selection->GetRangeCount() &&
|
|
|
|
selection->GetAnchorNode()->IsEditable() &&
|
|
|
|
selection->GetFocusNode()->IsEditable();
|
|
|
|
|
|
|
|
if (*aIsSelectionEditable) {
|
|
|
|
nsINode* commonAncestor =
|
|
|
|
selection->GetAnchorFocusRange()->GetCommonAncestor();
|
|
|
|
while (commonAncestor && !commonAncestor->IsEditable()) {
|
2012-10-09 12:31:24 +00:00
|
|
|
commonAncestor = commonAncestor->GetParentNode();
|
2012-06-24 14:39:11 +00:00
|
|
|
}
|
|
|
|
if (!commonAncestor) {
|
|
|
|
// No editable common ancestor
|
|
|
|
*aIsSelectionEditable = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-05-18 08:29:39 +00:00
|
|
|
static nsresult
|
2014-11-02 12:04:13 +00:00
|
|
|
SetSelectionAroundHeadChildren(Selection* aSelection,
|
2012-05-18 08:29:39 +00:00
|
|
|
nsIWeakReference* aDocWeak)
|
2000-04-28 05:59:16 +00:00
|
|
|
{
|
|
|
|
// Set selection around <head> node
|
2012-05-18 08:29:39 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(aDocWeak);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_NOT_INITIALIZED);
|
2002-01-14 20:31:43 +00:00
|
|
|
|
2012-05-18 08:29:39 +00:00
|
|
|
dom::Element* headNode = doc->GetHeadElement();
|
|
|
|
NS_ENSURE_STATE(headNode);
|
2000-04-28 05:59:16 +00:00
|
|
|
|
|
|
|
// Collapse selection to before first child of the head,
|
2012-05-18 08:29:39 +00:00
|
|
|
nsresult rv = aSelection->CollapseNative(headNode, 0);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2000-04-28 05:59:16 +00:00
|
|
|
|
2012-05-18 08:29:39 +00:00
|
|
|
// Then extend it to just after.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t childCount = headNode->GetChildCount();
|
2012-05-18 08:29:39 +00:00
|
|
|
return aSelection->ExtendNative(headNode, childCount + 1);
|
2000-04-28 05:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::GetHeadContentsAsHTML(nsAString& aOutputString)
|
2000-04-28 05:59:16 +00:00
|
|
|
{
|
2012-07-23 10:27:22 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2000-04-28 05:59:16 +00:00
|
|
|
|
|
|
|
// Save current selection
|
|
|
|
nsAutoSelectionReset selectionResetter(selection, this);
|
|
|
|
|
2012-07-23 10:27:22 +00:00
|
|
|
nsresult res = SetSelectionAroundHeadChildren(selection, mDocWeak);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-04-28 05:59:16 +00:00
|
|
|
|
2002-03-25 22:39:19 +00:00
|
|
|
res = OutputToString(NS_LITERAL_STRING("text/html"),
|
|
|
|
nsIDocumentEncoder::OutputSelectionOnly,
|
|
|
|
aOutputString);
|
2000-04-28 23:39:27 +00:00
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
// Selection always includes <body></body>,
|
|
|
|
// so terminate there
|
2014-01-04 15:02:17 +00:00
|
|
|
nsReadingIterator<char16_t> findIter,endFindIter;
|
2001-04-07 00:45:26 +00:00
|
|
|
aOutputString.BeginReading(findIter);
|
|
|
|
aOutputString.EndReading(endFindIter);
|
|
|
|
//counting on our parser to always lower case!!!
|
2002-11-26 22:15:30 +00:00
|
|
|
if (CaseInsensitiveFindInReadable(NS_LITERAL_STRING("<body"),
|
|
|
|
findIter, endFindIter))
|
2000-04-28 23:39:27 +00:00
|
|
|
{
|
2014-01-04 15:02:17 +00:00
|
|
|
nsReadingIterator<char16_t> beginIter;
|
2001-04-07 00:45:26 +00:00
|
|
|
aOutputString.BeginReading(beginIter);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t offset = Distance(beginIter, findIter);//get the distance
|
2001-04-07 00:45:26 +00:00
|
|
|
|
2014-01-04 15:02:17 +00:00
|
|
|
nsWritingIterator<char16_t> writeIter;
|
2001-04-07 00:45:26 +00:00
|
|
|
aOutputString.BeginWriting(writeIter);
|
2000-04-28 23:39:27 +00:00
|
|
|
// Ensure the string ends in a newline
|
2014-01-04 15:02:17 +00:00
|
|
|
char16_t newline ('\n');
|
2001-04-07 00:45:26 +00:00
|
|
|
findIter.advance(-1);
|
|
|
|
if (offset ==0 || (offset >0 && (*findIter) != newline)) //check for 0
|
|
|
|
{
|
|
|
|
writeIter.advance(offset);
|
|
|
|
*writeIter = newline;
|
|
|
|
aOutputString.Truncate(offset+1);
|
|
|
|
}
|
2000-04-28 23:39:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return res;
|
2000-04-28 05:59:16 +00:00
|
|
|
}
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsHTMLEditor::DebugUnitTests(int32_t *outNumTests, int32_t *outNumTestsFailed)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(outNumTests && outNumTestsFailed, NS_ERROR_NULL_POINTER);
|
1999-08-09 01:37:50 +00:00
|
|
|
|
1999-09-29 20:08:15 +00:00
|
|
|
TextEditorTest *tester = new TextEditorTest();
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(tester, NS_ERROR_OUT_OF_MEMORY);
|
1999-09-29 20:08:15 +00:00
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
tester->Run(this, outNumTests, outNumTestsFailed);
|
|
|
|
delete tester;
|
|
|
|
return NS_OK;
|
|
|
|
#else
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
NS_IMETHODIMP
|
2014-06-20 10:32:49 +00:00
|
|
|
nsHTMLEditor::StyleSheetLoaded(CSSStyleSheet* aSheet, bool aWasAlternate,
|
2005-09-12 18:41:15 +00:00
|
|
|
nsresult aStatus)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2002-09-13 19:47:38 +00:00
|
|
|
nsresult rv = NS_OK;
|
1999-09-29 20:08:15 +00:00
|
|
|
nsAutoEditBatch batchIt(this);
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2002-09-13 19:47:38 +00:00
|
|
|
if (!mLastStyleSheetURL.IsEmpty())
|
|
|
|
RemoveStyleSheet(mLastStyleSheetURL);
|
|
|
|
|
2007-07-02 14:01:27 +00:00
|
|
|
nsRefPtr<AddStyleSheetTxn> txn;
|
|
|
|
rv = CreateTxnForAddStyleSheet(aSheet, getter_AddRefs(txn));
|
2002-09-13 19:47:38 +00:00
|
|
|
if (!txn) rv = NS_ERROR_NULL_POINTER;
|
|
|
|
if (NS_SUCCEEDED(rv))
|
1999-04-22 14:45:48 +00:00
|
|
|
{
|
2003-04-04 20:50:25 +00:00
|
|
|
rv = DoTransaction(txn);
|
2002-09-13 19:47:38 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
// Get the URI, then url spec from the sheet
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString spec;
|
2010-05-18 04:00:40 +00:00
|
|
|
rv = aSheet->GetSheetURI()->GetSpec(spec);
|
2002-09-13 19:47:38 +00:00
|
|
|
|
2005-06-09 22:50:41 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
{
|
2010-05-12 20:18:47 +00:00
|
|
|
// Save it so we can remove before applying the next one
|
|
|
|
mLastStyleSheetURL.AssignWithConversion(spec.get());
|
2002-09-13 19:47:38 +00:00
|
|
|
|
2010-05-12 20:18:47 +00:00
|
|
|
// Also save in our arrays of urls and sheets
|
|
|
|
AddNewStyleSheetToList(mLastStyleSheetURL, aSheet);
|
2005-06-09 22:50:41 +00:00
|
|
|
}
|
2002-09-13 19:47:38 +00:00
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
|
|
|
|
1999-12-04 01:29:18 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
|
1999-12-07 08:30:19 +00:00
|
|
|
|
|
|
|
/** All editor operations which alter the doc should be prefaced
|
|
|
|
* with a call to StartOperation, naming the action and direction */
|
|
|
|
NS_IMETHODIMP
|
2012-08-12 18:28:26 +00:00
|
|
|
nsHTMLEditor::StartOperation(EditAction opID,
|
2012-05-05 18:52:29 +00:00
|
|
|
nsIEditor::EDirection aDirection)
|
1999-12-07 08:30:19 +00:00
|
|
|
{
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2000-08-14 02:39:37 +00:00
|
|
|
nsEditor::StartOperation(opID, aDirection); // will set mAction, mDirection
|
|
|
|
if (mRules) return mRules->BeforeEdit(mAction, mDirection);
|
1999-12-07 08:30:19 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** All editor operations which alter the doc should be followed
|
2000-08-14 02:39:37 +00:00
|
|
|
* with a call to EndOperation */
|
1999-12-07 08:30:19 +00:00
|
|
|
NS_IMETHODIMP
|
2000-08-14 02:39:37 +00:00
|
|
|
nsHTMLEditor::EndOperation()
|
1999-12-07 08:30:19 +00:00
|
|
|
{
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2000-08-14 02:39:37 +00: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 08:30:19 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2014-11-02 12:04:13 +00:00
|
|
|
nsHTMLEditor::TagCanContainTag(nsIAtom& aParentTag, nsIAtom& aChildTag)
|
2000-01-10 10:13:58 +00:00
|
|
|
{
|
2007-02-24 20:07:06 +00:00
|
|
|
nsIParserService* parserService = nsContentUtils::GetParserService();
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t childTagEnum;
|
2005-09-05 14:18:56 +00:00
|
|
|
// XXX Should this handle #cdata-section too?
|
2014-11-02 12:04:13 +00:00
|
|
|
if (&aChildTag == nsGkAtoms::textTagName) {
|
2005-09-05 14:18:56 +00:00
|
|
|
childTagEnum = eHTMLTag_text;
|
2012-05-01 06:34:52 +00:00
|
|
|
} else {
|
2014-11-02 12:04:13 +00:00
|
|
|
childTagEnum = parserService->HTMLAtomTagToId(&aChildTag);
|
2000-06-29 09:23:41 +00:00
|
|
|
}
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 10:57:30 +00:00
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
int32_t parentTagEnum = parserService->HTMLAtomTagToId(&aParentTag);
|
2005-09-05 14:18:56 +00:00
|
|
|
return nsHTMLEditUtils::CanContain(parentTagEnum, childTagEnum);
|
|
|
|
}
|
2005-06-16 13:10:58 +00:00
|
|
|
|
2012-01-25 07:50:05 +00:00
|
|
|
bool
|
2014-04-28 11:54:46 +00:00
|
|
|
nsHTMLEditor::IsContainer(nsINode* aNode) {
|
|
|
|
MOZ_ASSERT(aNode);
|
2005-09-05 14:18:56 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t tagEnum;
|
2005-12-22 15:05:41 +00:00
|
|
|
// XXX Should this handle #cdata-section too?
|
2014-04-28 11:54:46 +00:00
|
|
|
if (aNode->IsNodeOfType(nsINode::eTEXT)) {
|
2005-12-22 15:05:41 +00:00
|
|
|
tagEnum = eHTMLTag_text;
|
2014-04-28 11:54:46 +00:00
|
|
|
} else {
|
|
|
|
tagEnum =
|
|
|
|
nsContentUtils::GetParserService()->HTMLStringTagToId(aNode->NodeName());
|
2005-12-22 15:05:41 +00:00
|
|
|
}
|
2005-09-05 14:18:56 +00:00
|
|
|
|
|
|
|
return nsHTMLEditUtils::IsContainer(tagEnum);
|
2000-01-10 10:13:58 +00:00
|
|
|
}
|
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
bool
|
|
|
|
nsHTMLEditor::IsContainer(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
if (!node) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return IsContainer(node);
|
|
|
|
}
|
|
|
|
|
1999-12-07 08:30:19 +00:00
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::SelectEntireDocument(Selection* aSelection)
|
2000-01-13 10:17:35 +00:00
|
|
|
{
|
|
|
|
if (!aSelection || !mRules) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2005-03-24 19:00:01 +00:00
|
|
|
// get editor root node
|
2011-12-03 21:50:15 +00:00
|
|
|
nsCOMPtr<nsIDOMElement> rootElement = do_QueryInterface(GetRoot());
|
2000-01-13 10:17:35 +00:00
|
|
|
|
|
|
|
// is doc empty?
|
2011-09-29 06:19:26 +00:00
|
|
|
bool bDocIsEmpty;
|
2005-03-24 19:00:01 +00:00
|
|
|
nsresult res = mRules->DocumentIsEmpty(&bDocIsEmpty);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2000-01-13 10:17:35 +00:00
|
|
|
|
|
|
|
if (bDocIsEmpty)
|
|
|
|
{
|
|
|
|
// if its empty dont select entire doc - that would select the bogus node
|
2005-03-24 19:00:01 +00:00
|
|
|
return aSelection->Collapse(rootElement, 0);
|
2000-01-13 10:17:35 +00:00
|
|
|
}
|
2005-03-24 19:00:01 +00:00
|
|
|
|
|
|
|
return nsEditor::SelectEntireDocument(aSelection);
|
2000-01-13 10:17:35 +00:00
|
|
|
}
|
|
|
|
|
2007-06-28 02:48:16 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::SelectAll()
|
|
|
|
{
|
|
|
|
ForceCompositionEnd();
|
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
|
|
|
NS_ENSURE_STATE(selection);
|
2007-06-28 02:48:16 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> anchorNode;
|
2014-11-02 12:04:13 +00:00
|
|
|
nsresult rv = selection->GetAnchorNode(getter_AddRefs(anchorNode));
|
2007-06-28 02:48:16 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> anchorContent = do_QueryInterface(anchorNode, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-12-12 06:15:00 +00:00
|
|
|
|
|
|
|
nsIContent *rootContent;
|
2010-07-22 16:22:44 +00:00
|
|
|
if (anchorContent->HasIndependentSelection()) {
|
2014-11-02 12:04:13 +00:00
|
|
|
rv = selection->SetAncestorLimiter(nullptr);
|
2010-07-12 04:20:20 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2014-12-12 06:15:00 +00:00
|
|
|
rootContent = mRootElement;
|
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
|
|
|
rootContent = anchorContent->GetSelectionRootContent(ps);
|
2010-07-12 04:20:20 +00:00
|
|
|
}
|
2007-06-28 02:48:16 +00:00
|
|
|
|
2010-01-20 10:22:09 +00:00
|
|
|
NS_ENSURE_TRUE(rootContent, NS_ERROR_UNEXPECTED);
|
2007-06-28 02:48:16 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> rootElement = do_QueryInterface(rootContent, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2014-12-12 06:15:00 +00:00
|
|
|
Maybe<mozilla::dom::Selection::AutoApplyUserSelectStyle> userSelection;
|
|
|
|
if (!rootContent->IsEditable()) {
|
|
|
|
userSelection.emplace(selection);
|
|
|
|
}
|
2007-06-28 02:48:16 +00:00
|
|
|
return selection->SelectAllChildren(rootElement);
|
|
|
|
}
|
2000-01-13 10:17:35 +00:00
|
|
|
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
// this will NOT find aAttribute unless aAttribute has a non-null value
|
|
|
|
// so singleton attributes like <Table border> will not be matched!
|
2012-05-23 07:49:29 +00:00
|
|
|
bool nsHTMLEditor::IsTextPropertySetByContent(nsIContent* aContent,
|
|
|
|
nsIAtom* aProperty,
|
|
|
|
const nsAString* aAttribute,
|
|
|
|
const nsAString* aValue,
|
|
|
|
nsAString* outValue)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aContent && aProperty);
|
|
|
|
MOZ_ASSERT_IF(aAttribute, aValue);
|
|
|
|
bool isSet;
|
|
|
|
IsTextPropertySetByContent(aContent->AsDOMNode(), aProperty, aAttribute,
|
|
|
|
aValue, isSet, outValue);
|
|
|
|
return isSet;
|
|
|
|
}
|
|
|
|
|
2001-11-29 10:08:25 +00:00
|
|
|
void nsHTMLEditor::IsTextPropertySetByContent(nsIDOMNode *aNode,
|
|
|
|
nsIAtom *aProperty,
|
2002-03-23 22:08:20 +00:00
|
|
|
const nsAString *aAttribute,
|
|
|
|
const nsAString *aValue,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool &aIsSet,
|
2003-06-02 22:21:06 +00:00
|
|
|
nsAString *outValue)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
|
|
|
nsresult result;
|
2011-10-17 14:59:28 +00:00
|
|
|
aIsSet = false; // must be initialized to false for code below to work
|
1999-08-09 01:37:50 +00:00
|
|
|
nsAutoString propName;
|
|
|
|
aProperty->ToString(propName);
|
2000-05-07 01:33:42 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>node = aNode;
|
|
|
|
|
|
|
|
while (node)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMElement>element;
|
2000-05-07 01:33:42 +00:00
|
|
|
element = do_QueryInterface(node);
|
1999-08-09 01:37:50 +00:00
|
|
|
if (element)
|
|
|
|
{
|
fixes:
14753, 29843, 39864, 40141,
40139, 36679, 39542, 34729,
34855, 37216, 39292, 26447
r=sfraser,cmanske,fm; a=beppe
2000-05-24 23:00:24 +00:00
|
|
|
nsAutoString tag, value;
|
1999-08-09 01:37:50 +00:00
|
|
|
element->GetTagName(tag);
|
2002-02-19 22:49:12 +00:00
|
|
|
if (propName.Equals(tag, nsCaseInsensitiveStringComparator()))
|
1999-04-22 14:45:48 +00:00
|
|
|
{
|
2011-09-29 06:19:26 +00:00
|
|
|
bool found = false;
|
1999-08-09 01:37:50 +00: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-24 23:00:24 +00:00
|
|
|
if (outValue) *outValue = value;
|
2003-05-23 21:34:47 +00:00
|
|
|
if (!value.IsEmpty())
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
|
|
|
if (!aValue) {
|
2011-10-17 14:59:28 +00:00
|
|
|
found = true;
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
2001-04-07 00:45:26 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nsString tString(*aValue);
|
2002-02-19 22:49:12 +00:00
|
|
|
if (tString.Equals(value, nsCaseInsensitiveStringComparator())) {
|
2011-10-17 14:59:28 +00:00
|
|
|
found = true;
|
2001-04-07 00:45:26 +00:00
|
|
|
}
|
|
|
|
else { // we found the prop with the attribute, but the value doesn't match
|
|
|
|
break;
|
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2011-10-17 14:59:28 +00:00
|
|
|
found = true;
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
2000-03-24 00:26:47 +00:00
|
|
|
if (found)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
aIsSet = true;
|
1999-08-09 01:37:50 +00:00
|
|
|
break;
|
|
|
|
}
|
1999-05-05 04:05:19 +00:00
|
|
|
}
|
|
|
|
}
|
1999-08-09 01:37:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>temp;
|
2000-05-07 01:33:42 +00:00
|
|
|
result = node->GetParentNode(getter_AddRefs(temp));
|
1999-08-09 01:37:50 +00:00
|
|
|
if (NS_SUCCEEDED(result) && temp) {
|
2005-03-24 19:00:01 +00:00
|
|
|
node = temp;
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
|
|
|
else {
|
2012-07-30 14:20:58 +00:00
|
|
|
node = nullptr;
|
1999-08-09 01:37:50 +00:00
|
|
|
}
|
1999-05-05 04:05:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-08-09 01:37:50 +00:00
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
//================================================================
|
|
|
|
// HTML Editor methods
|
|
|
|
//
|
|
|
|
// Note: Table Editing methods are implemented in nsTableEditor.cpp
|
|
|
|
//
|
1999-08-09 01:37:50 +00:00
|
|
|
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2000-03-29 12:53:23 +00:00
|
|
|
nsHTMLEditor::SetCaretInTableCell(nsIDOMElement* aElement)
|
1999-08-09 01:37:50 +00:00
|
|
|
{
|
2012-02-01 10:54:22 +00:00
|
|
|
nsCOMPtr<dom::Element> element = do_QueryInterface(aElement);
|
2012-02-10 10:04:46 +00:00
|
|
|
if (!element || !element->IsHTML() ||
|
|
|
|
!nsHTMLEditUtils::IsTableElement(element) ||
|
2012-05-06 07:53:11 +00:00
|
|
|
!IsDescendantOfEditorRoot(element)) {
|
2012-02-01 10:54:22 +00:00
|
|
|
return false;
|
1999-05-26 21:40:51 +00:00
|
|
|
}
|
2012-02-01 10:54:22 +00:00
|
|
|
|
|
|
|
nsIContent* node = element;
|
|
|
|
while (node->HasChildren()) {
|
|
|
|
node = node->GetFirstChild();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set selection at beginning of the found node
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2012-02-01 10:54:22 +00:00
|
|
|
NS_ENSURE_TRUE(selection, false);
|
|
|
|
|
|
|
|
return NS_SUCCEEDED(selection->CollapseNative(node, 0));
|
2000-03-29 12:53:23 +00:00
|
|
|
}
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetEnclosingTable: find ancestor who is a table, if any
|
2014-04-28 11:54:46 +00:00
|
|
|
//
|
|
|
|
already_AddRefed<Element>
|
|
|
|
nsHTMLEditor::GetEnclosingTable(nsINode* aNode)
|
2000-03-29 12:53:23 +00:00
|
|
|
{
|
2014-04-28 11:54:46 +00:00
|
|
|
MOZ_ASSERT(aNode);
|
2000-03-29 12:53:23 +00:00
|
|
|
|
2014-04-28 11:54:46 +00:00
|
|
|
for (nsCOMPtr<Element> block = GetBlockNodeParent(aNode);
|
|
|
|
block;
|
|
|
|
block = GetBlockNodeParent(block)) {
|
|
|
|
if (nsHTMLEditUtils::IsTable(block)) {
|
|
|
|
return block.forget();
|
|
|
|
}
|
1999-07-19 19:06:39 +00:00
|
|
|
}
|
2014-04-28 11:54:46 +00:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode>
|
|
|
|
nsHTMLEditor::GetEnclosingTable(nsIDOMNode *aNode)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aNode, "null node passed to nsHTMLEditor::GetEnclosingTable");
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(node, nullptr);
|
|
|
|
nsCOMPtr<Element> table = GetEnclosingTable(node);
|
|
|
|
nsCOMPtr<nsIDOMNode> ret = do_QueryInterface(table);
|
|
|
|
return ret;
|
1999-07-19 19:06:39 +00:00
|
|
|
}
|
1999-05-17 12:22:31 +00:00
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
|
1999-08-19 15:15:41 +00:00
|
|
|
/* this method scans the selection for adjacent text nodes
|
|
|
|
* and collapses them into a single text node.
|
|
|
|
* "adjacent" means literally adjacent siblings of the same parent.
|
|
|
|
* Uses nsEditor::JoinNodes so action is undoable.
|
|
|
|
* Should be called within the context of a batch transaction.
|
1999-08-19 13:30:48 +00:00
|
|
|
*/
|
2014-11-02 12:04:13 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::CollapseAdjacentTextNodes(nsRange* aInRange)
|
1999-08-19 13:30:48 +00:00
|
|
|
{
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(aInRange, NS_ERROR_NULL_POINTER);
|
2000-03-29 12:53:23 +00:00
|
|
|
nsAutoTxnsConserveSelection dontSpazMySelection(this);
|
2012-08-01 08:17:51 +00:00
|
|
|
nsTArray<nsCOMPtr<nsIDOMNode> > textNodes;
|
2009-04-22 08:43:15 +00:00
|
|
|
// 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 13:30:48 +00:00
|
|
|
|
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
// build a list of editable text nodes
|
2003-07-08 20:22:56 +00:00
|
|
|
nsresult result;
|
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1", &result);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
1999-08-19 13:30:48 +00:00
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
iter->Init(aInRange);
|
2004-01-24 00:46:17 +00:00
|
|
|
|
|
|
|
while (!iter->IsDone())
|
1999-08-19 13:30:48 +00:00
|
|
|
{
|
2012-02-10 10:04:46 +00: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 13:30:48 +00:00
|
|
|
}
|
2004-01-24 00:46:17 +00:00
|
|
|
|
2000-03-29 12:53:23 +00:00
|
|
|
iter->Next();
|
1999-08-19 13:30:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// now that I have a list of text nodes, collapse adjacent text nodes
|
2000-03-29 12:53:23 +00:00
|
|
|
// NOTE: assumption that JoinNodes keeps the righthand node
|
2009-04-22 08:43:15 +00:00
|
|
|
while (textNodes.Length() > 1)
|
2000-03-29 12:53:23 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
// we assume a textNodes entry can't be nullptr
|
2009-04-22 08:43:15 +00:00
|
|
|
nsIDOMNode *leftTextNode = textNodes[0];
|
|
|
|
nsIDOMNode *rightTextNode = textNodes[1];
|
2001-12-21 01:10:07 +00:00
|
|
|
NS_ASSERTION(leftTextNode && rightTextNode,"left or rightTextNode null in CollapseAdjacentTextNodes");
|
|
|
|
|
2012-02-15 09:18:24 +00:00
|
|
|
// get the prev sibling of the right node, and see if its leftTextNode
|
2000-03-29 12:53:23 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> prevSibOfRightNode;
|
2008-04-30 18:13:11 +00:00
|
|
|
result =
|
|
|
|
rightTextNode->GetPreviousSibling(getter_AddRefs(prevSibOfRightNode));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2004-01-24 00:46:17 +00:00
|
|
|
if (prevSibOfRightNode && (prevSibOfRightNode == leftTextNode))
|
1999-08-19 13:30:48 +00:00
|
|
|
{
|
2000-03-29 12:53:23 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
result = rightTextNode->GetParentNode(getter_AddRefs(parent));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
|
2000-03-29 12:53:23 +00:00
|
|
|
result = JoinNodes(leftTextNode, rightTextNode, parent);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
1999-08-19 13:30:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
textNodes.RemoveElementAt(0); // remove the leftmost text node from the list
|
|
|
|
}
|
|
|
|
|
1999-09-29 20:08:15 +00:00
|
|
|
return result;
|
1999-08-19 13:30:48 +00:00
|
|
|
}
|
1999-09-10 18:54:13 +00:00
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::SetSelectionAtDocumentStart(Selection* aSelection)
|
2000-02-10 05:14:52 +00:00
|
|
|
{
|
2012-02-10 10:04:46 +00:00
|
|
|
dom::Element* rootElement = GetRoot();
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(rootElement, NS_ERROR_NULL_POINTER);
|
2005-03-24 19:00:01 +00:00
|
|
|
|
2012-02-10 10:04:46 +00:00
|
|
|
return aSelection->CollapseNative(rootElement, 0);
|
2000-02-10 05:14:52 +00:00
|
|
|
}
|
|
|
|
|
2000-02-08 12:53:34 +00:00
|
|
|
|
2001-06-18 21:15:43 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2012-02-15 09:18:24 +00:00
|
|
|
// RemoveBlockContainer: remove inNode, reparenting its children into their
|
2001-06-18 21:15:43 +00:00
|
|
|
// the parent of inNode. In addition, INSERT ANY BR's NEEDED
|
|
|
|
// TO PRESERVE IDENTITY OF REMOVED BLOCK.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::RemoveBlockContainer(nsIDOMNode *inNode)
|
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsIContent> node = do_QueryInterface(inNode);
|
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
|
2001-06-18 21:15:43 +00: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.
|
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
child = GetAsDOMNode(GetFirstEditableChild(*node));
|
2001-06-18 21:15:43 +00: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 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-18 21:15:43 +00:00
|
|
|
if (sibling && !IsBlockNode(sibling) && !nsTextEditUtils::IsBreak(sibling))
|
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
if (!IsBlockNode(child)) {
|
2001-06-18 21:15:43 +00:00
|
|
|
// insert br node
|
|
|
|
res = CreateBR(inNode, 0, address_of(unused));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-18 21:15:43 +00: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 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-18 21:15:43 +00:00
|
|
|
if (sibling && !IsBlockNode(sibling))
|
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
child = GetAsDOMNode(GetLastEditableChild(*node));
|
2001-06-18 21:15:43 +00:00
|
|
|
if (child && !IsBlockNode(child) && !nsTextEditUtils::IsBreak(child))
|
|
|
|
{
|
|
|
|
// insert br node
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t len;
|
2001-06-18 21:15:43 +00:00
|
|
|
res = GetLengthOfDOMNode(inNode, len);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2012-08-22 15:56:38 +00:00
|
|
|
res = CreateBR(inNode, (int32_t)len, address_of(unused));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-18 21:15:43 +00: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 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-18 21:15:43 +00:00
|
|
|
if (sibling && !IsBlockNode(sibling) && !nsTextEditUtils::IsBreak(sibling))
|
|
|
|
{
|
|
|
|
res = GetNextHTMLSibling(inNode, address_of(sibling));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-18 21:15:43 +00:00
|
|
|
if (sibling && !IsBlockNode(sibling) && !nsTextEditUtils::IsBreak(sibling))
|
|
|
|
{
|
|
|
|
// insert br node
|
|
|
|
res = CreateBR(inNode, 0, address_of(unused));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2001-06-18 21:15:43 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// now remove container
|
2014-08-20 12:25:16 +00:00
|
|
|
return RemoveContainer(node);
|
2001-06-18 21:15:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetPriorHTMLSibling: returns the previous editable sibling, if there is
|
|
|
|
// one within the parent
|
|
|
|
//
|
2012-05-05 09:00:06 +00:00
|
|
|
nsIContent*
|
2012-04-14 13:09:38 +00:00
|
|
|
nsHTMLEditor::GetPriorHTMLSibling(nsINode* aNode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
|
|
|
|
nsIContent* node = aNode->GetPreviousSibling();
|
|
|
|
while (node && !IsEditable(node)) {
|
|
|
|
node = node->GetPreviousSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetPriorHTMLSibling(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode)
|
|
|
|
{
|
2012-04-14 13:09:38 +00:00
|
|
|
NS_ENSURE_TRUE(outNode, NS_ERROR_NULL_POINTER);
|
2013-04-03 00:13:10 +00:00
|
|
|
*outNode = nullptr;
|
2012-04-14 13:09:38 +00:00
|
|
|
|
|
|
|
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 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-02-08 12:53:34 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2000-03-24 00:26:47 +00:00
|
|
|
// GetPriorHTMLSibling: returns the previous editable sibling, if there is
|
|
|
|
// one within the parent. just like above routine but
|
|
|
|
// takes a parent/offset instead of a node.
|
|
|
|
//
|
2012-05-05 09:00:06 +00:00
|
|
|
nsIContent*
|
2012-08-22 15:56:38 +00:00
|
|
|
nsHTMLEditor::GetPriorHTMLSibling(nsINode* aParent, int32_t aOffset)
|
2012-04-14 13:09:38 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
|
|
|
|
nsIContent* node = aParent->GetChildAt(aOffset - 1);
|
|
|
|
if (!node || IsEditable(node)) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetPriorHTMLSibling(node);
|
|
|
|
}
|
|
|
|
|
2000-02-08 12:53:34 +00:00
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
nsHTMLEditor::GetPriorHTMLSibling(nsIDOMNode *inParent, int32_t inOffset, nsCOMPtr<nsIDOMNode> *outNode)
|
2000-02-08 12:53:34 +00:00
|
|
|
{
|
2012-04-14 13:09:38 +00:00
|
|
|
NS_ENSURE_TRUE(outNode, NS_ERROR_NULL_POINTER);
|
2013-04-03 00:13:10 +00:00
|
|
|
*outNode = nullptr;
|
2012-04-14 13:09:38 +00:00
|
|
|
|
|
|
|
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 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetNextHTMLSibling: returns the next editable sibling, if there is
|
|
|
|
// one within the parent
|
|
|
|
//
|
2012-05-05 09:00:06 +00:00
|
|
|
nsIContent*
|
2012-04-14 13:09:38 +00:00
|
|
|
nsHTMLEditor::GetNextHTMLSibling(nsINode* aNode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
|
|
|
|
nsIContent* node = aNode->GetNextSibling();
|
|
|
|
while (node && !IsEditable(node)) {
|
|
|
|
node = node->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetNextHTMLSibling(nsIDOMNode *inNode, nsCOMPtr<nsIDOMNode> *outNode)
|
|
|
|
{
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(outNode, NS_ERROR_NULL_POINTER);
|
2012-07-30 14:20:58 +00:00
|
|
|
*outNode = nullptr;
|
2012-04-14 13:09:38 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(inNode);
|
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2012-04-14 13:09:38 +00:00
|
|
|
*outNode = do_QueryInterface(GetNextHTMLSibling(node));
|
|
|
|
return NS_OK;
|
2000-03-24 00:26:47 +00: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 09:00:06 +00:00
|
|
|
nsIContent*
|
2012-08-22 15:56:38 +00:00
|
|
|
nsHTMLEditor::GetNextHTMLSibling(nsINode* aParent, int32_t aOffset)
|
2012-04-14 13:09:38 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
|
|
|
|
nsIContent* node = aParent->GetChildAt(aOffset + 1);
|
|
|
|
if (!node || IsEditable(node)) {
|
|
|
|
return node;
|
|
|
|
}
|
|
|
|
|
|
|
|
return GetNextHTMLSibling(node);
|
|
|
|
}
|
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
nsHTMLEditor::GetNextHTMLSibling(nsIDOMNode *inParent, int32_t inOffset, nsCOMPtr<nsIDOMNode> *outNode)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2012-04-14 13:09:38 +00:00
|
|
|
NS_ENSURE_TRUE(outNode, NS_ERROR_NULL_POINTER);
|
2013-04-03 00:13:10 +00:00
|
|
|
*outNode = nullptr;
|
2012-04-14 13:09:38 +00:00
|
|
|
|
|
|
|
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 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetPriorHTMLNode: returns the previous editable leaf node, if there is
|
|
|
|
// one within the <body>
|
2005-03-24 19:00:01 +00:00
|
|
|
//
|
2012-12-22 08:17:05 +00:00
|
|
|
nsIContent*
|
|
|
|
nsHTMLEditor::GetPriorHTMLNode(nsINode* aNode, bool aNoBlockCrossing)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
MOZ_ASSERT(aNode);
|
2011-11-28 12:54:32 +00:00
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
if (!GetActiveEditingHost()) {
|
|
|
|
return nullptr;
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
2011-11-28 12:54:32 +00:00
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
return GetPriorNode(aNode, true, aNoBlockCrossing);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetPriorHTMLNode(nsIDOMNode* aNode,
|
|
|
|
nsCOMPtr<nsIDOMNode>* aResultNode,
|
|
|
|
bool aNoBlockCrossing)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aResultNode, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
*aResultNode = do_QueryInterface(GetPriorHTMLNode(node, aNoBlockCrossing));
|
|
|
|
return NS_OK;
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// GetPriorHTMLNode: same as above but takes {parent,offset} instead of node
|
2012-12-22 08:17:05 +00:00
|
|
|
//
|
2012-07-01 11:53:45 +00:00
|
|
|
nsIContent*
|
2012-08-22 15:56:38 +00:00
|
|
|
nsHTMLEditor::GetPriorHTMLNode(nsINode* aParent, int32_t aOffset,
|
2012-07-01 11:53:45 +00:00
|
|
|
bool aNoBlockCrossing)
|
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
MOZ_ASSERT(aParent);
|
|
|
|
|
2012-07-01 11:53:45 +00:00
|
|
|
if (!GetActiveEditingHost()) {
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2012-07-01 11:53:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return GetPriorNode(aParent, aOffset, true, aNoBlockCrossing);
|
|
|
|
}
|
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
nsHTMLEditor::GetPriorHTMLNode(nsIDOMNode* aNode, int32_t aOffset,
|
|
|
|
nsCOMPtr<nsIDOMNode>* aResultNode,
|
|
|
|
bool aNoBlockCrossing)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
NS_ENSURE_TRUE(aResultNode, NS_ERROR_NULL_POINTER);
|
2011-11-28 12:54:32 +00:00
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
|
2012-07-01 11:53:45 +00:00
|
|
|
|
2012-12-22 08:17:05 +00:00
|
|
|
*aResultNode = do_QueryInterface(GetPriorHTMLNode(node, aOffset,
|
|
|
|
aNoBlockCrossing));
|
|
|
|
return NS_OK;
|
2000-02-08 12:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2000-08-26 04:03:50 +00:00
|
|
|
// GetNextHTMLNode: returns the next editable leaf node, if there is
|
2000-03-24 00:26:47 +00:00
|
|
|
// one within the <body>
|
|
|
|
//
|
2012-12-22 08:17:05 +00:00
|
|
|
nsIContent*
|
|
|
|
nsHTMLEditor::GetNextHTMLNode(nsINode* aNode, bool aNoBlockCrossing)
|
2000-02-08 12:53:34 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
|
|
|
|
nsIContent* result = GetNextNode(aNode, true, aNoBlockCrossing);
|
|
|
|
|
|
|
|
if (result && !IsDescendantOfEditorRoot(result)) {
|
|
|
|
return nullptr;
|
2000-02-08 12:53:34 +00:00
|
|
|
}
|
2012-12-22 08:17:05 +00:00
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::GetNextHTMLNode(nsIDOMNode* aNode,
|
|
|
|
nsCOMPtr<nsIDOMNode>* aResultNode,
|
|
|
|
bool aNoBlockCrossing)
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aResultNode, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
*aResultNode = do_QueryInterface(GetNextHTMLNode(node, aNoBlockCrossing));
|
|
|
|
return NS_OK;
|
2000-02-08 12:53:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2012-12-22 08:17:05 +00:00
|
|
|
// GetNextHTMLNode: same as above but takes {parent,offset} instead of node
|
2000-03-24 00:26:47 +00:00
|
|
|
//
|
2012-07-01 11:53:45 +00:00
|
|
|
nsIContent*
|
2012-08-22 15:56:38 +00:00
|
|
|
nsHTMLEditor::GetNextHTMLNode(nsINode* aParent, int32_t aOffset,
|
2012-07-01 11:53:45 +00:00
|
|
|
bool aNoBlockCrossing)
|
|
|
|
{
|
|
|
|
nsIContent* content = GetNextNode(aParent, aOffset, true, aNoBlockCrossing);
|
|
|
|
if (content && !IsDescendantOfEditorRoot(content)) {
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2012-07-01 11:53:45 +00:00
|
|
|
}
|
|
|
|
return content;
|
|
|
|
}
|
|
|
|
|
2000-02-08 12:53:34 +00:00
|
|
|
nsresult
|
2012-12-22 08:17:05 +00:00
|
|
|
nsHTMLEditor::GetNextHTMLNode(nsIDOMNode* aNode, int32_t aOffset,
|
|
|
|
nsCOMPtr<nsIDOMNode>* aResultNode,
|
|
|
|
bool aNoBlockCrossing)
|
2000-02-08 12:53:34 +00:00
|
|
|
{
|
2012-12-22 08:17:05 +00:00
|
|
|
NS_ENSURE_TRUE(aResultNode, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
*aResultNode = do_QueryInterface(GetNextHTMLNode(node, aOffset,
|
|
|
|
aNoBlockCrossing));
|
|
|
|
return NS_OK;
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::IsFirstEditableChild( nsIDOMNode *aNode, bool *aOutIsFirst)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(aOutIsFirst && node, NS_ERROR_NULL_POINTER);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
|
|
|
// init out parms
|
2011-10-17 14:59:28 +00:00
|
|
|
*aOutIsFirst = false;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
|
|
|
// find first editable child and compare it to aNode
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsINode> parent = node->GetParentNode();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_FAILURE);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
*aOutIsFirst = (GetFirstEditableChild(*parent) == node);
|
|
|
|
return NS_OK;
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
2000-02-08 12:53:34 +00:00
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
|
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::IsLastEditableChild( nsIDOMNode *aNode, bool *aOutIsLast)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
NS_ENSURE_TRUE(aOutIsLast && node, NS_ERROR_NULL_POINTER);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
|
|
|
// init out parms
|
2011-10-17 14:59:28 +00:00
|
|
|
*aOutIsLast = false;
|
2000-03-24 00:26:47 +00:00
|
|
|
|
|
|
|
// find last editable child and compare it to aNode
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsINode> parent = node->GetParentNode();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_FAILURE);
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
*aOutIsLast = (GetLastEditableChild(*parent) == node);
|
|
|
|
return NS_OK;
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
2000-02-08 12:53:34 +00:00
|
|
|
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
nsIContent*
|
|
|
|
nsHTMLEditor::GetFirstEditableChild(nsINode& aNode)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsIContent> child = aNode.GetFirstChild();
|
|
|
|
|
|
|
|
while (child && !IsEditable(child)) {
|
|
|
|
child = child->GetNextSibling();
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
2014-11-02 12:04:13 +00:00
|
|
|
|
|
|
|
return child;
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
nsIContent*
|
|
|
|
nsHTMLEditor::GetLastEditableChild(nsINode& aNode)
|
2000-03-24 00:26:47 +00:00
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsIContent> child = aNode.GetLastChild();
|
|
|
|
|
|
|
|
while (child && !IsEditable(child)) {
|
|
|
|
child = child->GetPreviousSibling();
|
2000-03-24 00:26:47 +00:00
|
|
|
}
|
2014-11-02 12:04:13 +00:00
|
|
|
|
|
|
|
return child;
|
2000-02-08 12:53:34 +00:00
|
|
|
}
|
2000-03-24 00:26:47 +00:00
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
nsIContent*
|
|
|
|
nsHTMLEditor::GetFirstEditableLeaf(nsINode& aNode)
|
2000-08-26 04:03:50 +00:00
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsIContent> child = GetLeftmostChild(&aNode);
|
|
|
|
while (child && (!IsEditable(child) || child->HasChildren())) {
|
|
|
|
child = GetNextHTMLNode(child);
|
|
|
|
|
|
|
|
// Only accept nodes that are descendants of aNode
|
|
|
|
if (!aNode.Contains(child)) {
|
|
|
|
return nullptr;
|
2000-08-26 04:03:50 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-02 12:04:13 +00:00
|
|
|
|
|
|
|
return child;
|
2000-08-26 04:03:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
nsIContent*
|
|
|
|
nsHTMLEditor::GetLastEditableLeaf(nsINode& aNode)
|
2000-08-26 04:03:50 +00:00
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsIContent> child = GetRightmostChild(&aNode, false);
|
|
|
|
while (child && (!IsEditable(child) || child->HasChildren())) {
|
|
|
|
child = GetPriorHTMLNode(child);
|
|
|
|
|
|
|
|
// Only accept nodes that are descendants of aNode
|
|
|
|
if (!aNode.Contains(child)) {
|
|
|
|
return nullptr;
|
2000-08-26 04:03:50 +00:00
|
|
|
}
|
|
|
|
}
|
2014-11-02 12:04:13 +00:00
|
|
|
|
|
|
|
return child;
|
2000-08-26 04:03:50 +00:00
|
|
|
}
|
|
|
|
|
2002-04-14 02:37:23 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsVisTextNode: figure out if textnode aTextNode has any visible content.
|
|
|
|
//
|
2012-01-25 07:50:05 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::IsVisTextNode(nsIContent* aNode,
|
|
|
|
bool* outIsEmptyNode,
|
|
|
|
bool aSafeToAskFrames)
|
2002-04-14 02:37:23 +00:00
|
|
|
{
|
2012-05-05 09:00:06 +00:00
|
|
|
MOZ_ASSERT(aNode);
|
|
|
|
MOZ_ASSERT(aNode->NodeType() == nsIDOMNode::TEXT_NODE);
|
|
|
|
MOZ_ASSERT(outIsEmptyNode);
|
2002-04-14 02:37:23 +00:00
|
|
|
|
2012-05-05 09:00:06 +00:00
|
|
|
*outIsEmptyNode = true;
|
2012-01-25 07:50:05 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t length = aNode->TextLength();
|
2002-04-14 02:37:23 +00:00
|
|
|
if (aSafeToAskFrames)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelectionController> selCon;
|
2012-01-25 07:50:05 +00:00
|
|
|
nsresult res = GetSelectionController(getter_AddRefs(selCon));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selCon, NS_ERROR_FAILURE);
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isVisible = false;
|
2002-04-14 02:37:23 +00: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 07:50:05 +00:00
|
|
|
res = selCon->CheckVisibilityContent(aNode, 0, length, &isVisible);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-04-14 02:37:23 +00:00
|
|
|
if (isVisible)
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
*outIsEmptyNode = false;
|
2002-04-14 02:37:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (length)
|
|
|
|
{
|
2012-01-25 07:50:05 +00:00
|
|
|
if (aNode->TextIsOnlyWhitespace())
|
2002-04-14 02:37:23 +00:00
|
|
|
{
|
2014-05-02 12:15:27 +00:00
|
|
|
nsWSRunObject wsRunObj(this, aNode, 0);
|
|
|
|
nsCOMPtr<nsINode> visNode;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t outVisOffset=0;
|
2012-07-13 06:33:43 +00:00
|
|
|
WSType visType;
|
2014-05-02 12:15:27 +00:00
|
|
|
wsRunObj.NextVisibleNode(aNode, 0, address_of(visNode),
|
2012-06-01 11:03:17 +00:00
|
|
|
&outVisOffset, &visType);
|
2012-07-13 06:33:43 +00:00
|
|
|
if (visType == WSType::normalWS || visType == WSType::text) {
|
2014-05-02 12:15:27 +00:00
|
|
|
*outIsEmptyNode = (aNode != visNode);
|
2002-04-14 02:37:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
*outIsEmptyNode = false;
|
2002-04-14 02:37:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2000-06-22 05:39:54 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsEmptyNode: figure out if aNode is an empty node.
|
|
|
|
// A block can have children and still be considered empty,
|
|
|
|
// if the children are empty or non-editable.
|
|
|
|
//
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::IsEmptyNode( nsIDOMNode *aNode,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool *outIsEmptyNode,
|
|
|
|
bool aSingleBRDoesntCount,
|
|
|
|
bool aListOrCellNotEmpty,
|
|
|
|
bool aSafeToAskFrames)
|
2000-06-22 05:39:54 +00:00
|
|
|
{
|
2012-01-25 07:50:05 +00:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
2012-01-25 07:50:06 +00: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 14:59:28 +00:00
|
|
|
*outIsEmptyNode = true;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool seenBR = false;
|
2012-01-25 07:50:06 +00:00
|
|
|
return IsEmptyNodeImpl(aNode, outIsEmptyNode, aSingleBRDoesntCount,
|
2002-04-14 02:37:23 +00:00
|
|
|
aListOrCellNotEmpty, aSafeToAskFrames, &seenBR);
|
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////
|
|
|
|
// IsEmptyNodeImpl: workhorse for IsEmptyNode.
|
|
|
|
//
|
|
|
|
nsresult
|
2012-01-25 07:50:05 +00:00
|
|
|
nsHTMLEditor::IsEmptyNodeImpl(nsINode* aNode,
|
|
|
|
bool *outIsEmptyNode,
|
|
|
|
bool aSingleBRDoesntCount,
|
|
|
|
bool aListOrCellNotEmpty,
|
|
|
|
bool aSafeToAskFrames,
|
|
|
|
bool *aSeenBR)
|
2002-04-14 02:37:23 +00:00
|
|
|
{
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(aNode && outIsEmptyNode && aSeenBR, NS_ERROR_NULL_POINTER);
|
2002-04-14 02:37:23 +00:00
|
|
|
|
2012-01-25 07:50:05 +00:00
|
|
|
if (aNode->NodeType() == nsIDOMNode::TEXT_NODE) {
|
|
|
|
return IsVisTextNode(static_cast<nsIContent*>(aNode), outIsEmptyNode, aSafeToAskFrames);
|
2000-06-22 05:39:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// if it's not a text node (handled above) and it's not a container,
|
2005-11-25 08:16:51 +00: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 05:39:54 +00: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 06:02:15 +00:00
|
|
|
// cells empty if caller desires. Form Widgets not empty.
|
2012-06-06 07:36:00 +00:00
|
|
|
if (!IsContainer(aNode->AsDOMNode()) ||
|
2012-07-27 14:03:28 +00:00
|
|
|
(nsHTMLEditUtils::IsNamedAnchor(aNode) ||
|
|
|
|
nsHTMLEditUtils::IsFormWidget(aNode) ||
|
|
|
|
(aListOrCellNotEmpty &&
|
|
|
|
(nsHTMLEditUtils::IsListItem(aNode) ||
|
|
|
|
nsHTMLEditUtils::IsTableCell(aNode))))) {
|
2011-10-17 14:59:28 +00:00
|
|
|
*outIsEmptyNode = false;
|
2000-06-22 05:39:54 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-04-14 02:37:23 +00:00
|
|
|
|
2001-04-17 10:15:05 +00:00
|
|
|
// need this for later
|
2012-07-27 14:03:28 +00:00
|
|
|
bool isListItemOrCell = nsHTMLEditUtils::IsListItem(aNode) ||
|
|
|
|
nsHTMLEditUtils::IsTableCell(aNode);
|
2001-04-17 10:15:05 +00:00
|
|
|
|
2002-02-07 03:56:20 +00:00
|
|
|
// loop over children of node. if no children, or all children are either
|
2000-06-22 05:39:54 +00:00
|
|
|
// empty text nodes or non-editable, then node qualifies as empty
|
2012-01-25 07:50:05 +00: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 15:25:07 +00:00
|
|
|
// break out if we find we aren't emtpy
|
|
|
|
if (!*outIsEmptyNode) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-01-25 07:50:05 +00: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-04 21:49:48 +00:00
|
|
|
// the first br in a block doesn't count if the caller so indicated
|
2011-10-17 14:59:28 +00:00
|
|
|
*aSeenBR = true;
|
2012-01-25 07:50:05 +00:00
|
|
|
} else {
|
2000-06-22 05:39:54 +00:00
|
|
|
// is it an empty node of some sort?
|
2001-04-17 10:15:05 +00:00
|
|
|
// note: list items or table cells are not considered empty
|
|
|
|
// if they contain other lists or tables
|
2012-01-25 07:50:05 +00:00
|
|
|
if (child->IsElement()) {
|
|
|
|
if (isListItemOrCell) {
|
2012-07-27 14:03:28 +00:00
|
|
|
if (nsHTMLEditUtils::IsList(child) ||
|
|
|
|
child->IsHTML(nsGkAtoms::table)) {
|
2012-01-25 07:50:05 +00:00
|
|
|
// break out if we find we aren't empty
|
|
|
|
*outIsEmptyNode = false;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-07-27 14:03:28 +00:00
|
|
|
} else if (nsHTMLEditUtils::IsFormWidget(child)) {
|
2012-01-25 07:50:05 +00:00
|
|
|
// is it a form widget?
|
|
|
|
// break out if we find we aren't empty
|
2011-10-17 14:59:28 +00:00
|
|
|
*outIsEmptyNode = false;
|
2002-02-07 03:56:20 +00:00
|
|
|
return NS_OK;
|
2001-04-17 10:15:05 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-25 07:50:05 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isEmptyNode = true;
|
2012-01-25 07:50:05 +00:00
|
|
|
nsresult rv = IsEmptyNodeImpl(child, &isEmptyNode,
|
|
|
|
aSingleBRDoesntCount,
|
|
|
|
aListOrCellNotEmpty, aSafeToAskFrames,
|
|
|
|
aSeenBR);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!isEmptyNode) {
|
2000-06-22 05:39:54 +00:00
|
|
|
// otherwise it ain't empty
|
2011-10-17 14:59:28 +00:00
|
|
|
*outIsEmptyNode = false;
|
2002-02-07 03:56:20 +00:00
|
|
|
return NS_OK;
|
2000-06-22 05:39:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
// add to aElement the CSS inline styles corresponding to the HTML attribute
|
|
|
|
// aAttribute with its value aValue
|
|
|
|
nsresult
|
2002-01-25 10:16:52 +00:00
|
|
|
nsHTMLEditor::SetAttributeOrEquivalent(nsIDOMElement * aElement,
|
2002-03-23 22:08:20 +00:00
|
|
|
const nsAString & aAttribute,
|
2002-09-17 12:04:59 +00:00
|
|
|
const nsAString & aValue,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aSuppressTransaction)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
2012-08-29 20:43:32 +00:00
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
nsresult res = NS_OK;
|
2012-02-01 10:54:22 +00:00
|
|
|
if (IsCSSEnabled() && mHTMLCSSUtils) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t count;
|
2012-07-30 14:20:58 +00:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(aElement, nullptr, &aAttribute, &aValue, &count,
|
2002-09-17 12:04:59 +00:00
|
|
|
aSuppressTransaction);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-15 15:33:29 +00:00
|
|
|
if (count) {
|
|
|
|
// we found an equivalence ; let's remove the HTML attribute itself if it is set
|
|
|
|
nsAutoString existingValue;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool wasSet = false;
|
2002-03-15 15:33:29 +00:00
|
|
|
res = GetAttributeValue(aElement, aAttribute, existingValue, &wasSet);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-15 15:33:29 +00:00
|
|
|
if (wasSet) {
|
2002-09-20 02:09:04 +00:00
|
|
|
if (aSuppressTransaction)
|
|
|
|
res = aElement->RemoveAttribute(aAttribute);
|
|
|
|
else
|
|
|
|
res = RemoveAttribute(aElement, aAttribute);
|
2002-03-15 15:33:29 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2002-01-09 13:51:37 +00:00
|
|
|
// count is an integer that represents the number of CSS declarations applied to the
|
|
|
|
// element. If it is zero, we found no equivalence in this implementation for the
|
|
|
|
// attribute
|
2004-05-22 22:15:22 +00:00
|
|
|
if (aAttribute.EqualsLiteral("style")) {
|
2002-01-09 13:51:37 +00:00
|
|
|
// if it is the style attribute, just add the new value to the existing style
|
|
|
|
// attribute's value
|
|
|
|
nsAutoString existingValue;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool wasSet = false;
|
2002-01-09 13:51:37 +00:00
|
|
|
res = GetAttributeValue(aElement, NS_LITERAL_STRING("style"), existingValue, &wasSet);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2014-05-22 03:48:51 +00:00
|
|
|
existingValue.Append(' ');
|
2002-01-09 13:51:37 +00:00
|
|
|
existingValue.Append(aValue);
|
2002-09-20 02:09:04 +00:00
|
|
|
if (aSuppressTransaction)
|
|
|
|
res = aElement->SetAttribute(aAttribute, existingValue);
|
|
|
|
else
|
|
|
|
res = SetAttribute(aElement, aAttribute, existingValue);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// we have no CSS equivalence for this attribute and it is not the style
|
|
|
|
// attribute; let's set it the good'n'old HTML way
|
2002-09-20 02:09:04 +00:00
|
|
|
if (aSuppressTransaction)
|
|
|
|
res = aElement->SetAttribute(aAttribute, aValue);
|
|
|
|
else
|
|
|
|
res = SetAttribute(aElement, aAttribute, aValue);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
// we are not in an HTML+CSS editor; let's set the attribute the HTML way
|
2002-09-20 02:09:04 +00:00
|
|
|
if (aSuppressTransaction)
|
|
|
|
res = aElement->SetAttribute(aAttribute, aValue);
|
|
|
|
else
|
|
|
|
res = SetAttribute(aElement, aAttribute, aValue);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-03-15 15:33:29 +00:00
|
|
|
nsresult
|
2013-03-10 07:58:34 +00:00
|
|
|
nsHTMLEditor::RemoveAttributeOrEquivalent(nsIDOMElement* aElement,
|
|
|
|
const nsAString& aAttribute,
|
2011-09-29 06:19:26 +00:00
|
|
|
bool aSuppressTransaction)
|
2002-03-15 15:33:29 +00:00
|
|
|
{
|
2013-03-10 07:58:34 +00:00
|
|
|
nsCOMPtr<dom::Element> element = do_QueryInterface(aElement);
|
|
|
|
NS_ENSURE_TRUE(element, NS_OK);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIAtom> attribute = do_GetAtom(aAttribute);
|
|
|
|
MOZ_ASSERT(attribute);
|
|
|
|
|
2002-03-15 15:33:29 +00:00
|
|
|
nsresult res = NS_OK;
|
2012-02-01 10:54:22 +00:00
|
|
|
if (IsCSSEnabled() && mHTMLCSSUtils) {
|
2013-03-10 07:58:34 +00:00
|
|
|
res = mHTMLCSSUtils->RemoveCSSEquivalentToHTMLStyle(
|
|
|
|
element, nullptr, &aAttribute, nullptr, aSuppressTransaction);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-15 15:33:29 +00:00
|
|
|
}
|
|
|
|
|
2013-03-10 07:58:34 +00:00
|
|
|
if (element->HasAttr(kNameSpaceID_None, attribute)) {
|
|
|
|
if (aSuppressTransaction) {
|
|
|
|
res = element->UnsetAttr(kNameSpaceID_None, attribute,
|
|
|
|
/* aNotify = */ true);
|
|
|
|
} else {
|
2002-09-20 02:09:04 +00:00
|
|
|
res = RemoveAttribute(aElement, aAttribute);
|
2013-03-10 07:58:34 +00:00
|
|
|
}
|
2002-03-15 15:33:29 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::SetIsCSSEnabled(bool aIsCSSPrefChecked)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
2012-02-10 10:04:46 +00:00
|
|
|
if (!mHTMLCSSUtils) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
2007-11-13 08:10:03 +00:00
|
|
|
|
2012-06-01 11:03:17 +00:00
|
|
|
mHTMLCSSUtils->SetCSSEnabled(aIsCSSPrefChecked);
|
2012-02-10 10:04:46 +00:00
|
|
|
|
|
|
|
// Disable the eEditorNoCSSMask flag if we're enabling StyleWithCSS.
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t flags = mFlags;
|
2012-02-10 10:04:46 +00:00
|
|
|
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 08:10:03 +00:00
|
|
|
}
|
2012-02-10 10:04:46 +00:00
|
|
|
|
|
|
|
return SetFlags(flags);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the block background color
|
2015-01-02 06:14:13 +00:00
|
|
|
nsresult
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::SetCSSBackgroundColor(const nsAString& aColor)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
ForceCompositionEnd();
|
|
|
|
|
2010-05-28 01:18:48 +00:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2012-06-08 14:20:55 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2002-01-09 13:51:37 +00:00
|
|
|
|
2012-05-18 08:29:39 +00:00
|
|
|
bool isCollapsed = selection->Collapsed();
|
2002-01-09 13:51:37 +00:00
|
|
|
|
|
|
|
nsAutoEditBatch batchIt(this);
|
2012-08-12 18:28:26 +00:00
|
|
|
nsAutoRules beginRulesSniffing(this, EditAction::insertElement, nsIEditor::eNext);
|
2002-01-09 13:51:37 +00:00
|
|
|
nsAutoSelectionReset selectionResetter(selection, this);
|
|
|
|
nsAutoTxnsConserveSelection dontSpazMySelection(this);
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool cancel, handled;
|
2012-08-12 18:28:26 +00:00
|
|
|
nsTextRulesInfo ruleInfo(EditAction::setTextProperty);
|
2012-05-22 09:37:17 +00:00
|
|
|
nsresult res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00:00
|
|
|
if (!cancel && !handled)
|
|
|
|
{
|
|
|
|
// loop thru the ranges in the selection
|
2004-06-17 00:13:25 +00:00
|
|
|
nsAutoString bgcolor; bgcolor.AssignLiteral("bgcolor");
|
2013-02-07 14:17:47 +00:00
|
|
|
uint32_t rangeCount = selection->GetRangeCount();
|
|
|
|
for (uint32_t rangeIdx = 0; rangeIdx < rangeCount; ++rangeIdx) {
|
|
|
|
nsCOMPtr<nsIDOMNode> cachedBlockParent = nullptr;
|
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(rangeIdx);
|
|
|
|
NS_ENSURE_TRUE(range, NS_ERROR_FAILURE);
|
2002-01-09 13:51:37 +00:00
|
|
|
|
|
|
|
// check for easy case: both range endpoints in same text node
|
|
|
|
nsCOMPtr<nsIDOMNode> startNode, endNode;
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t startOffset, endOffset;
|
2002-01-09 13:51:37 +00:00
|
|
|
res = range->GetStartContainer(getter_AddRefs(startNode));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00:00
|
|
|
res = range->GetEndContainer(getter_AddRefs(endNode));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00:00
|
|
|
res = range->GetStartOffset(&startOffset);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00:00
|
|
|
res = range->GetEndOffset(&endOffset);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00: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
|
2014-04-25 10:34:42 +00:00
|
|
|
if (blockParent && cachedBlockParent != blockParent) {
|
2002-01-09 13:51:37 +00:00
|
|
|
cachedBlockParent = blockParent;
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(blockParent);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t count;
|
2012-07-30 14:20:58 +00:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nullptr, &bgcolor, &aColor, &count, false);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
2002-02-18 10:06:07 +00:00
|
|
|
else if ((startNode == endNode) && nsTextEditUtils::IsBody(startNode) && isCollapsed)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
|
|
|
// we have no block in the document, let's apply the background to the body
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(startNode);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t count;
|
2012-07-30 14:20:58 +00:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nullptr, &bgcolor, &aColor, &count, false);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00: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 06:19:26 +00:00
|
|
|
bool isBlock =false;
|
2002-01-09 13:51:37 +00:00
|
|
|
res = NodeIsBlockStatic(selectedNode, &isBlock);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> blockParent = selectedNode;
|
|
|
|
if (!isBlock) {
|
|
|
|
blockParent = GetBlockNodeParent(selectedNode);
|
|
|
|
}
|
2014-04-25 10:34:42 +00:00
|
|
|
if (blockParent && cachedBlockParent != blockParent) {
|
2002-01-09 13:51:37 +00:00
|
|
|
cachedBlockParent = blockParent;
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(blockParent);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t count;
|
2012-07-30 14:20:58 +00:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nullptr, &bgcolor, &aColor, &count, false);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00: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-08 20:22:56 +00:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/subtree-content-iterator;1", &res);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(iter, NS_ERROR_FAILURE);
|
2002-01-09 13:51:37 +00:00
|
|
|
|
2002-11-12 19:40:11 +00:00
|
|
|
nsCOMArray<nsIDOMNode> arrayOfNodes;
|
2002-01-09 13:51:37 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> node;
|
2002-11-12 19:40:11 +00:00
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
// iterate range and build up array
|
|
|
|
res = iter->Init(range);
|
|
|
|
// init returns an error if no nodes in range.
|
|
|
|
// this can easily happen with the subtree
|
|
|
|
// iterator if the selection doesn't contain
|
|
|
|
// any *whole* nodes.
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
2004-01-24 00:46:17 +00:00
|
|
|
while (!iter->IsDone())
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
2004-01-24 00:46:17 +00:00
|
|
|
node = do_QueryInterface(iter->GetCurrentNode());
|
2010-06-17 20:40:48 +00:00
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_FAILURE);
|
2004-01-24 00:46:17 +00:00
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
if (IsEditable(node))
|
2002-11-12 19:40:11 +00:00
|
|
|
{
|
|
|
|
arrayOfNodes.AppendObject(node);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
2004-01-24 00:46:17 +00:00
|
|
|
|
|
|
|
iter->Next();
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
// first check the start parent of the range to see if it needs to
|
2003-03-24 04:15:34 +00:00
|
|
|
// be separately handled (it does if it's a text node, due to how the
|
2002-01-09 13:51:37 +00:00
|
|
|
// subtree iterator works - it will not have reported it).
|
|
|
|
if (IsTextNode(startNode) && IsEditable(startNode))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> blockParent;
|
|
|
|
blockParent = GetBlockNodeParent(startNode);
|
2014-04-25 10:34:42 +00:00
|
|
|
if (blockParent && cachedBlockParent != blockParent) {
|
2002-01-09 13:51:37 +00:00
|
|
|
cachedBlockParent = blockParent;
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(blockParent);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t count;
|
2012-07-30 14:20:58 +00:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nullptr, &bgcolor, &aColor, &count, false);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// then loop through the list, set the property on each node
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t listCount = arrayOfNodes.Count();
|
|
|
|
int32_t j;
|
2002-01-09 13:51:37 +00:00
|
|
|
for (j = 0; j < listCount; j++)
|
|
|
|
{
|
2002-11-12 19:40:11 +00:00
|
|
|
node = arrayOfNodes[j];
|
2002-01-09 13:51:37 +00:00
|
|
|
// do we have a block here ?
|
2011-09-29 06:19:26 +00:00
|
|
|
bool isBlock =false;
|
2002-01-09 13:51:37 +00:00
|
|
|
res = NodeIsBlockStatic(node, &isBlock);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> blockParent = node;
|
|
|
|
if (!isBlock) {
|
|
|
|
// no we don't, let's find the block ancestor
|
|
|
|
blockParent = GetBlockNodeParent(node);
|
|
|
|
}
|
2014-04-25 10:34:42 +00:00
|
|
|
if (blockParent && cachedBlockParent != blockParent) {
|
2002-01-09 13:51:37 +00:00
|
|
|
cachedBlockParent = blockParent;
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(blockParent);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t count;
|
2002-01-09 13:51:37 +00:00
|
|
|
// and set the property on it
|
2012-07-30 14:20:58 +00:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nullptr, &bgcolor, &aColor, &count, false);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
2002-11-12 19:40:11 +00:00
|
|
|
arrayOfNodes.Clear();
|
2002-01-09 13:51:37 +00:00
|
|
|
|
|
|
|
// last check the end parent of the range to see if it needs to
|
2003-03-24 04:15:34 +00:00
|
|
|
// be separately handled (it does if it's a text node, due to how the
|
2002-01-09 13:51:37 +00:00
|
|
|
// subtree iterator works - it will not have reported it).
|
|
|
|
if (IsTextNode(endNode) && IsEditable(endNode))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> blockParent;
|
|
|
|
blockParent = GetBlockNodeParent(endNode);
|
2014-04-25 10:34:42 +00:00
|
|
|
if (blockParent && cachedBlockParent != blockParent) {
|
2002-01-09 13:51:37 +00:00
|
|
|
cachedBlockParent = blockParent;
|
|
|
|
nsCOMPtr<nsIDOMElement> element = do_QueryInterface(blockParent);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t count;
|
2012-07-30 14:20:58 +00:00
|
|
|
res = mHTMLCSSUtils->SetCSSEquivalentToHTMLStyle(element, nullptr, &bgcolor, &aColor, &count, false);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!cancel)
|
|
|
|
{
|
|
|
|
// post-process
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsHTMLEditor::SetBackgroundColor(const nsAString& aColor)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
|
|
|
nsresult res;
|
2012-02-01 10:54:22 +00:00
|
|
|
if (IsCSSEnabled()) {
|
2002-01-09 13:51:37 +00: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?
|
|
|
|
//
|
2012-05-18 08:29:38 +00:00
|
|
|
/* virtual */
|
|
|
|
bool
|
|
|
|
nsHTMLEditor::AreNodesSameType(nsIContent* aNode1, nsIContent* aNode2)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
2012-05-18 08:29:38 +00:00
|
|
|
MOZ_ASSERT(aNode1);
|
|
|
|
MOZ_ASSERT(aNode2);
|
|
|
|
|
|
|
|
if (aNode1->Tag() != aNode2->Tag()) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
|
2012-05-18 08:29:38 +00:00
|
|
|
if (!IsCSSEnabled() || !aNode1->IsHTML(nsGkAtoms::span)) {
|
|
|
|
return true;
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
2012-05-18 08:29:38 +00:00
|
|
|
|
|
|
|
// If CSS is enabled, we are stricter about span nodes.
|
|
|
|
return mHTMLCSSUtils->ElementsSameStyle(aNode1->AsDOMNode(),
|
|
|
|
aNode2->AsDOMNode());
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|
|
|
|
|
2015-01-02 06:14:13 +00:00
|
|
|
nsresult
|
2002-03-11 13:11:48 +00:00
|
|
|
nsHTMLEditor::CopyLastEditableChildStyles(nsIDOMNode * aPreviousBlock, nsIDOMNode * aNewBlock,
|
|
|
|
nsIDOMNode **aOutBrNode)
|
|
|
|
{
|
2014-08-20 12:25:16 +00:00
|
|
|
nsCOMPtr<nsINode> newBlock = do_QueryInterface(aNewBlock);
|
|
|
|
NS_ENSURE_STATE(newBlock || !aNewBlock);
|
2012-07-30 14:20:58 +00:00
|
|
|
*aOutBrNode = nullptr;
|
2002-04-06 19:07:47 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> child, tmp;
|
2002-03-11 13:11:48 +00:00
|
|
|
nsresult res;
|
2002-04-06 19:07:47 +00:00
|
|
|
// first, clear out aNewBlock. Contract is that we want only the styles from previousBlock.
|
|
|
|
res = aNewBlock->GetFirstChild(getter_AddRefs(child));
|
|
|
|
while (NS_SUCCEEDED(res) && child)
|
|
|
|
{
|
|
|
|
res = DeleteNode(child);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-04-06 19:07:47 +00:00
|
|
|
res = aNewBlock->GetFirstChild(getter_AddRefs(child));
|
|
|
|
}
|
|
|
|
// now find and clone the styles
|
|
|
|
child = aPreviousBlock;
|
|
|
|
tmp = aPreviousBlock;
|
2002-03-11 13:11:48 +00:00
|
|
|
while (tmp) {
|
|
|
|
child = tmp;
|
2014-11-02 12:04:13 +00:00
|
|
|
nsCOMPtr<nsINode> child_ = do_QueryInterface(child);
|
|
|
|
NS_ENSURE_STATE(child_ || !child);
|
|
|
|
tmp = GetAsDOMNode(GetLastEditableChild(*child_));
|
2002-03-11 13:11:48 +00:00
|
|
|
}
|
|
|
|
while (child && nsTextEditUtils::IsBreak(child)) {
|
|
|
|
nsCOMPtr<nsIDOMNode> priorNode;
|
|
|
|
res = GetPriorHTMLNode(child, address_of(priorNode));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2002-03-11 13:11:48 +00:00
|
|
|
child = priorNode;
|
|
|
|
}
|
2014-08-20 12:25:16 +00:00
|
|
|
nsCOMPtr<Element> newStyles, deepestStyle;
|
|
|
|
nsCOMPtr<nsINode> childNode = do_QueryInterface(child);
|
|
|
|
nsCOMPtr<Element> childElement;
|
|
|
|
if (childNode) {
|
|
|
|
childElement = childNode->IsElement() ? childNode->AsElement()
|
|
|
|
: childNode->GetParentElement();
|
|
|
|
}
|
|
|
|
while (childElement && (childElement->AsDOMNode() != aPreviousBlock)) {
|
|
|
|
if (nsHTMLEditUtils::IsInlineStyle(childElement) ||
|
|
|
|
childElement->Tag() == nsGkAtoms::span) {
|
2002-03-11 13:11:48 +00:00
|
|
|
if (newStyles) {
|
2014-08-20 12:25:16 +00:00
|
|
|
newStyles = InsertContainerAbove(newStyles, childElement->Tag());
|
|
|
|
NS_ENSURE_STATE(newStyles);
|
|
|
|
} else {
|
2014-08-20 12:25:16 +00:00
|
|
|
deepestStyle = newStyles = CreateNode(childElement->Tag(), newBlock,
|
|
|
|
0);
|
|
|
|
NS_ENSURE_STATE(newStyles);
|
2002-03-11 13:11:48 +00:00
|
|
|
}
|
2014-08-20 12:25:16 +00:00
|
|
|
CloneAttributes(newStyles, childElement);
|
2002-03-11 13:11:48 +00:00
|
|
|
}
|
2014-08-20 12:25:16 +00:00
|
|
|
childElement = childElement->GetParentElement();
|
2002-03-11 13:11:48 +00:00
|
|
|
}
|
|
|
|
if (deepestStyle) {
|
2014-08-20 12:25:16 +00:00
|
|
|
*aOutBrNode = GetAsDOMNode(CreateBR(deepestStyle, 0).take());
|
|
|
|
NS_ENSURE_STATE(*aOutBrNode);
|
2002-03-11 13:11:48 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-02-24 09:37:18 +00:00
|
|
|
|
|
|
|
nsresult
|
2012-08-22 15:56:38 +00:00
|
|
|
nsHTMLEditor::GetElementOrigin(nsIDOMElement * aElement, int32_t & aX, int32_t & aY)
|
2003-02-24 09:37:18 +00:00
|
|
|
{
|
2008-01-22 07:39:06 +00:00
|
|
|
aX = 0;
|
|
|
|
aY = 0;
|
|
|
|
|
2011-03-11 05:40:30 +00:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, NS_ERROR_NOT_INITIALIZED);
|
2011-05-22 12:43:13 +00:00
|
|
|
nsCOMPtr<nsIPresShell> ps = GetPresShell();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(ps, NS_ERROR_NOT_INITIALIZED);
|
2003-02-24 09:37:18 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
|
2009-12-24 21:20:06 +00:00
|
|
|
nsIFrame *frame = content->GetPrimaryFrame();
|
2010-08-26 00:20:32 +00:00
|
|
|
NS_ENSURE_TRUE(frame, NS_OK);
|
2003-02-24 09:37:18 +00:00
|
|
|
|
2008-01-22 07:39:06 +00:00
|
|
|
nsIFrame *container = ps->GetAbsoluteContainingBlock(frame);
|
2010-08-26 00:20:32 +00:00
|
|
|
NS_ENSURE_TRUE(container, NS_OK);
|
2008-01-22 07:39:06 +00:00
|
|
|
nsPoint off = frame->GetOffsetTo(container);
|
|
|
|
aX = nsPresContext::AppUnitsToIntCSSPixels(off.x);
|
|
|
|
aY = nsPresContext::AppUnitsToIntCSSPixels(off.y);
|
2003-02-24 09:37:18 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-25 15:24:08 +00:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::EndUpdateViewBatch()
|
|
|
|
{
|
|
|
|
nsresult res = nsEditor::EndUpdateViewBatch();
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-03-25 15:24:08 +00: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) {
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NOT_INITIALIZED);
|
2003-06-25 08:50:48 +00:00
|
|
|
res = CheckSelectionStateForAnonymousButtons(selection);
|
2003-03-25 15:24:08 +00:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2003-06-25 08:50:48 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::GetSelectionContainer(nsIDOMElement ** aReturn)
|
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
2003-06-25 08:50:48 +00:00
|
|
|
// if we don't get the selection, just skip this
|
2014-11-02 12:04:13 +00:00
|
|
|
if (!selection) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2003-06-25 08:50:48 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> focusNode;
|
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
nsresult res;
|
2012-05-18 08:29:39 +00:00
|
|
|
if (selection->Collapsed()) {
|
2003-06-25 08:50:48 +00:00
|
|
|
res = selection->GetFocusNode(getter_AddRefs(focusNode));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2012-05-18 08:29:39 +00:00
|
|
|
} else {
|
2003-06-25 08:50:48 +00:00
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t rangeCount;
|
2003-06-25 08:50:48 +00:00
|
|
|
res = selection->GetRangeCount(&rangeCount);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 08:50:48 +00:00
|
|
|
|
|
|
|
if (rangeCount == 1) {
|
|
|
|
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<nsRange> range = selection->GetRangeAt(0);
|
2010-06-17 19:41:16 +00:00
|
|
|
NS_ENSURE_TRUE(range, NS_ERROR_NULL_POINTER);
|
2003-06-25 08:50:48 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> startContainer, endContainer;
|
|
|
|
res = range->GetStartContainer(getter_AddRefs(startContainer));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 08:50:48 +00:00
|
|
|
res = range->GetEndContainer(getter_AddRefs(endContainer));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t startOffset, endOffset;
|
2003-06-25 08:50:48 +00:00
|
|
|
res = range->GetStartOffset(&startOffset);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 08:50:48 +00:00
|
|
|
res = range->GetEndOffset(&endOffset);
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 08:50:48 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> focusElement;
|
|
|
|
if (startContainer == endContainer && startOffset + 1 == endOffset) {
|
2004-05-22 20:02:43 +00:00
|
|
|
res = GetSelectedElement(EmptyString(), getter_AddRefs(focusElement));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 08:50:48 +00:00
|
|
|
if (focusElement)
|
|
|
|
focusNode = do_QueryInterface(focusElement);
|
|
|
|
}
|
|
|
|
if (!focusNode) {
|
|
|
|
res = range->GetCommonAncestorContainer(getter_AddRefs(focusNode));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 08:50:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t i;
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<nsRange> range;
|
2003-06-25 08:50:48 +00:00
|
|
|
for (i = 0; i < rangeCount; i++)
|
|
|
|
{
|
2014-11-02 12:04:13 +00:00
|
|
|
range = selection->GetRangeAt(i);
|
|
|
|
NS_ENSURE_STATE(range);
|
2003-06-25 08:50:48 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> startContainer;
|
2009-03-30 09:58:42 +00:00
|
|
|
res = range->GetStartContainer(getter_AddRefs(startContainer));
|
|
|
|
if (NS_FAILED(res)) continue;
|
2003-06-25 08:50:48 +00:00
|
|
|
if (!focusNode)
|
|
|
|
focusNode = startContainer;
|
|
|
|
else if (focusNode != startContainer) {
|
|
|
|
res = startContainer->GetParentNode(getter_AddRefs(focusNode));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 08:50:48 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (focusNode) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint16_t nodeType;
|
2003-06-25 08:50:48 +00:00
|
|
|
focusNode->GetNodeType(&nodeType);
|
|
|
|
if (nsIDOMNode::TEXT_NODE == nodeType) {
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
res = focusNode->GetParentNode(getter_AddRefs(parent));
|
2010-06-17 19:27:24 +00:00
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
2003-06-25 08:50:48 +00:00
|
|
|
focusNode = parent;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMElement> focusElement = do_QueryInterface(focusNode);
|
2012-02-10 10:04:46 +00:00
|
|
|
focusElement.forget(aReturn);
|
2003-06-25 08:50:48 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-06-19 14:06:05 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::IsAnonymousElement(nsIDOMElement * aElement, bool * aReturn)
|
2003-06-19 14:06:05 +00:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aElement, NS_ERROR_NULL_POINTER);
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aElement);
|
2008-07-23 04:50:20 +00:00
|
|
|
*aReturn = content->IsRootOfNativeAnonymousSubtree();
|
2003-06-19 14:06:05 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2003-06-25 08:50:48 +00:00
|
|
|
|
2005-01-12 19:11:48 +00:00
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::SetReturnInParagraphCreatesNewParagraph(bool aCreatesNewParagraph)
|
2005-01-12 19:11:48 +00:00
|
|
|
{
|
|
|
|
mCRInParagraphCreatesParagraph = aCreatesNewParagraph;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-01 11:03:17 +00:00
|
|
|
bool
|
|
|
|
nsHTMLEditor::GetReturnInParagraphCreatesNewParagraph()
|
|
|
|
{
|
|
|
|
return mCRInParagraphCreatesParagraph;
|
|
|
|
}
|
|
|
|
|
2005-01-12 19:11:48 +00:00
|
|
|
nsresult
|
2011-09-29 06:19:26 +00:00
|
|
|
nsHTMLEditor::GetReturnInParagraphCreatesNewParagraph(bool *aCreatesNewParagraph)
|
2005-01-12 19:11:48 +00:00
|
|
|
{
|
|
|
|
*aCreatesNewParagraph = mCRInParagraphCreatesParagraph;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-05-04 17:40:39 +00:00
|
|
|
|
2010-10-29 16:30:52 +00:00
|
|
|
already_AddRefed<nsIContent>
|
|
|
|
nsHTMLEditor::GetFocusedContent()
|
2010-05-04 17:40:39 +00:00
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, nullptr);
|
2010-05-04 17:40:39 +00:00
|
|
|
|
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ENSURE_TRUE(fm, nullptr);
|
2010-05-04 17:40:39 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> focusedContent = fm->GetFocusedContent();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
2011-09-29 06:19:26 +00:00
|
|
|
bool inDesignMode = doc->HasFlag(NODE_IS_EDITABLE);
|
2010-05-04 17:40:39 +00:00
|
|
|
if (!focusedContent) {
|
|
|
|
// in designMode, nobody gets focus in most cases.
|
2010-10-29 16:30:52 +00:00
|
|
|
if (inDesignMode && OurWindowHasFocus()) {
|
|
|
|
nsCOMPtr<nsIContent> docRoot = doc->GetRootElement();
|
|
|
|
return docRoot.forget();
|
|
|
|
}
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2010-05-04 17:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (inDesignMode) {
|
2010-10-29 16:30:52 +00:00
|
|
|
return OurWindowHasFocus() &&
|
|
|
|
nsContentUtils::ContentIsDescendantOf(focusedContent, doc) ?
|
2012-07-30 14:20:58 +00:00
|
|
|
focusedContent.forget() : nullptr;
|
2010-05-04 17:40:39 +00: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 16:22:44 +00:00
|
|
|
focusedContent->HasIndependentSelection()) {
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2010-05-04 17:40:39 +00:00
|
|
|
}
|
2010-05-28 03:02:59 +00:00
|
|
|
// If our window is focused, we're focused.
|
2012-07-30 14:20:58 +00:00
|
|
|
return OurWindowHasFocus() ? focusedContent.forget() : nullptr;
|
2010-05-04 17:40:39 +00:00
|
|
|
}
|
|
|
|
|
2012-11-09 08:40:39 +00:00
|
|
|
already_AddRefed<nsIContent>
|
|
|
|
nsHTMLEditor::GetFocusedContentForIME()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> focusedContent = GetFocusedContent();
|
|
|
|
if (!focusedContent) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
|
|
|
NS_ENSURE_TRUE(doc, nullptr);
|
|
|
|
return doc->HasFlag(NODE_IS_EDITABLE) ? nullptr : focusedContent.forget();
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2010-07-22 16:22:44 +00:00
|
|
|
nsHTMLEditor::IsActiveInDOMWindow()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, false);
|
2010-07-22 16:22:44 +00:00
|
|
|
|
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ENSURE_TRUE(fm, false);
|
2010-07-22 16:22:44 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
2011-09-29 06:19:26 +00:00
|
|
|
bool inDesignMode = doc->HasFlag(NODE_IS_EDITABLE);
|
2010-07-22 16:22:44 +00:00
|
|
|
|
|
|
|
// If we're in designMode, we're always active in the DOM window.
|
|
|
|
if (inDesignMode) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2010-07-22 16:22:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindow* ourWindow = doc->GetWindow();
|
|
|
|
nsCOMPtr<nsPIDOMWindow> win;
|
|
|
|
nsIContent* content =
|
2011-10-17 14:59:28 +00:00
|
|
|
nsFocusManager::GetFocusedDescendant(ourWindow, false,
|
2010-07-22 16:22:44 +00:00
|
|
|
getter_AddRefs(win));
|
|
|
|
if (!content) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-07-22 16:22:44 +00: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 14:59:28 +00:00
|
|
|
return false;
|
2010-07-22 16:22:44 +00:00
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2010-07-22 16:22:44 +00:00
|
|
|
}
|
|
|
|
|
2012-05-06 07:53:11 +00:00
|
|
|
dom::Element*
|
2011-07-14 15:25:07 +00:00
|
|
|
nsHTMLEditor::GetActiveEditingHost()
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, nullptr);
|
2011-07-14 15:25:07 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ENSURE_TRUE(doc, nullptr);
|
2011-07-14 15:25:07 +00:00
|
|
|
if (doc->HasFlag(NODE_IS_EDITABLE)) {
|
|
|
|
return doc->GetBodyElement();
|
|
|
|
}
|
|
|
|
|
|
|
|
// We're HTML editor for contenteditable
|
2014-11-02 12:04:13 +00:00
|
|
|
nsRefPtr<Selection> selection = GetSelection();
|
|
|
|
NS_ENSURE_TRUE(selection, nullptr);
|
2011-07-14 15:25:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> focusNode;
|
2014-11-02 12:04:13 +00:00
|
|
|
nsresult rv = selection->GetFocusNode(getter_AddRefs(focusNode));
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2011-07-14 15:25:07 +00:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(focusNode);
|
|
|
|
if (!content) {
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2011-07-14 15:25:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the active content isn't editable, or it has independent selection,
|
|
|
|
// we're not active.
|
|
|
|
if (!content->HasFlag(NODE_IS_EDITABLE) ||
|
|
|
|
content->HasIndependentSelection()) {
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2011-07-14 15:25:07 +00:00
|
|
|
}
|
|
|
|
return content->GetEditingHost();
|
|
|
|
}
|
|
|
|
|
2013-10-22 23:32:04 +00:00
|
|
|
already_AddRefed<mozilla::dom::EventTarget>
|
2011-06-24 02:18:01 +00:00
|
|
|
nsHTMLEditor::GetDOMEventTarget()
|
2010-06-17 05:30:10 +00: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");
|
2013-10-22 23:32:04 +00:00
|
|
|
nsCOMPtr<mozilla::dom::EventTarget> target = do_QueryReferent(mDocWeak);
|
2011-06-24 02:18:01 +00:00
|
|
|
return target.forget();
|
2010-06-17 05:30:10 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2010-06-17 05:32:05 +00:00
|
|
|
nsHTMLEditor::ShouldReplaceRootElement()
|
|
|
|
{
|
|
|
|
if (!mRootElement) {
|
|
|
|
// If we don't know what is our root element, we should find our root.
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2010-06-17 05:32:05 +00: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 15:33:32 +00:00
|
|
|
nsCOMPtr<nsIMutationObserver> kungFuDeathGrip(this);
|
|
|
|
|
2010-06-17 05:32:05 +00: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();
|
2012-07-30 14:20:58 +00:00
|
|
|
mRootElement = nullptr;
|
2010-06-17 05:32:05 +00:00
|
|
|
nsresult rv = InstallEventListeners();
|
2012-01-11 08:22:16 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-19 10:39:39 +00:00
|
|
|
|
|
|
|
// We must have mRootElement now.
|
|
|
|
nsCOMPtr<nsIDOMElement> root;
|
|
|
|
rv = GetRootElement(getter_AddRefs(root));
|
2012-01-11 08:22:16 +00:00
|
|
|
if (NS_FAILED(rv) || !mRootElement) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-17 05:32:05 +00:00
|
|
|
|
|
|
|
rv = BeginningOfDocument();
|
2012-01-11 08:22:16 +00:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
2010-06-17 05:32:05 +00: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 05:29:40 +00: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 05:32:05 +00:00
|
|
|
already_AddRefed<nsINode>
|
|
|
|
nsHTMLEditor::GetFocusedNode()
|
|
|
|
{
|
2010-10-29 16:30:52 +00:00
|
|
|
nsCOMPtr<nsIContent> focusedContent = GetFocusedContent();
|
|
|
|
if (!focusedContent) {
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2010-06-17 05:32:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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 10:04:46 +00:00
|
|
|
return doc.forget();
|
2010-06-17 05:32:05 +00:00
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2010-05-04 17:40:39 +00:00
|
|
|
nsHTMLEditor::OurWindowHasFocus()
|
|
|
|
{
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, false);
|
2010-05-04 17:40:39 +00:00
|
|
|
nsIFocusManager* fm = nsFocusManager::GetFocusManager();
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ENSURE_TRUE(fm, false);
|
2010-05-04 17:40:39 +00:00
|
|
|
nsCOMPtr<nsIDOMWindow> focusedWindow;
|
|
|
|
fm->GetFocusedWindow(getter_AddRefs(focusedWindow));
|
|
|
|
if (!focusedWindow) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-05-04 17:40:39 +00:00
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryReferent(mDocWeak);
|
|
|
|
nsCOMPtr<nsIDOMWindow> ourWindow = do_QueryInterface(doc->GetWindow());
|
|
|
|
return ourWindow == focusedWindow;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2010-07-22 16:22:44 +00:00
|
|
|
nsHTMLEditor::IsAcceptableInputEvent(nsIDOMEvent* aEvent)
|
2010-05-04 17:40:39 +00:00
|
|
|
{
|
2010-07-22 16:22:44 +00:00
|
|
|
if (!nsEditor::IsAcceptableInputEvent(aEvent)) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-07-22 16:22:44 +00:00
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ENSURE_TRUE(mDocWeak, false);
|
2010-07-22 16:22:44 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMEventTarget> target;
|
|
|
|
aEvent->GetTarget(getter_AddRefs(target));
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ENSURE_TRUE(target, false);
|
2010-07-22 16:22:44 +00: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 14:59:28 +00:00
|
|
|
NS_ENSURE_TRUE(targetContent, false);
|
2010-07-22 16:22:44 +00:00
|
|
|
return document == targetContent->GetCurrentDoc();
|
|
|
|
}
|
|
|
|
|
2011-11-26 04:51:48 +00:00
|
|
|
// This HTML editor is for contenteditable. We need to check the validity of
|
|
|
|
// the target.
|
2010-07-22 16:22:44 +00:00
|
|
|
nsCOMPtr<nsIContent> targetContent = do_QueryInterface(target);
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ENSURE_TRUE(targetContent, false);
|
2011-11-26 04:51:48 +00: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 16:22:44 +00:00
|
|
|
if (!targetContent->HasFlag(NODE_IS_EDITABLE) ||
|
|
|
|
targetContent->HasIndependentSelection()) {
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-07-22 16:22:44 +00: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 17:40:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-11-27 11:51:53 +00:00
|
|
|
nsHTMLEditor::GetPreferredIMEState(IMEState *aState)
|
2010-05-04 17:40:39 +00:00
|
|
|
{
|
2011-11-27 11:51:53 +00: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 17:40:39 +00:00
|
|
|
if (IsReadonly() || IsDisabled()) {
|
2011-11-27 11:51:53 +00:00
|
|
|
aState->mEnabled = IMEState::DISABLED;
|
|
|
|
} else {
|
|
|
|
aState->mEnabled = IMEState::ENABLED;
|
2010-05-04 17:40:39 +00:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-27 01:36:44 +00:00
|
|
|
|
|
|
|
already_AddRefed<nsIContent>
|
|
|
|
nsHTMLEditor::GetInputEventTargetContent()
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> target = GetActiveEditingHost();
|
|
|
|
return target.forget();
|
|
|
|
}
|
2012-05-06 07:53:11 +00:00
|
|
|
|
2010-11-16 20:45:49 +00:00
|
|
|
bool
|
2014-04-28 16:03:12 +00:00
|
|
|
nsHTMLEditor::IsEditable(nsINode* aNode) {
|
2010-11-16 20:45:49 +00:00
|
|
|
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 07:53:11 +00:00
|
|
|
// virtual MOZ_OVERRIDE
|
|
|
|
dom::Element*
|
|
|
|
nsHTMLEditor::GetEditorRoot()
|
|
|
|
{
|
|
|
|
return GetActiveEditingHost();
|
|
|
|
}
|