2010-04-21 20:17:41 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set sw=2 ts=2 et tw=80: */
|
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/. */
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2011-08-08 21:28:04 +00:00
|
|
|
#include "nsTextEditorState.h"
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIPresShell.h"
|
2013-01-03 13:23:11 +00:00
|
|
|
#include "nsView.h"
|
2010-04-21 20:17:41 +00:00
|
|
|
#include "nsCaret.h"
|
|
|
|
#include "nsEditorCID.h"
|
|
|
|
#include "nsLayoutCID.h"
|
|
|
|
#include "nsITextControlFrame.h"
|
|
|
|
#include "nsIPlaintextEditor.h"
|
2012-07-13 06:33:42 +00:00
|
|
|
#include "nsIDOMCharacterData.h"
|
2010-04-21 20:17:41 +00:00
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsContentCreatorFunctions.h"
|
|
|
|
#include "nsTextControlFrame.h"
|
|
|
|
#include "nsIControllers.h"
|
2010-08-05 02:40:18 +00:00
|
|
|
#include "nsIDOMHTMLInputElement.h"
|
2011-06-11 07:52:24 +00:00
|
|
|
#include "nsIDOMHTMLTextAreaElement.h"
|
2010-04-21 20:17:41 +00:00
|
|
|
#include "nsITransactionManager.h"
|
|
|
|
#include "nsIControllerContext.h"
|
|
|
|
#include "nsAttrValue.h"
|
2012-09-30 16:40:24 +00:00
|
|
|
#include "nsAttrValueInlines.h"
|
2010-04-21 20:17:41 +00:00
|
|
|
#include "nsGenericHTMLElement.h"
|
2011-06-25 05:56:13 +00:00
|
|
|
#include "nsIDOMEventListener.h"
|
2012-11-14 19:23:35 +00:00
|
|
|
#include "nsIEditorObserver.h"
|
2014-03-14 13:13:31 +00:00
|
|
|
#include "nsIWidget.h"
|
2010-04-21 20:17:41 +00:00
|
|
|
#include "nsIDocumentEncoder.h"
|
|
|
|
#include "nsISelectionPrivate.h"
|
|
|
|
#include "nsPIDOMWindow.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsIEditor.h"
|
|
|
|
#include "nsTextEditRules.h"
|
2014-04-10 16:09:40 +00:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2014-03-17 06:56:53 +00:00
|
|
|
#include "mozilla/EventListenerManager.h"
|
2011-08-11 13:29:50 +00:00
|
|
|
#include "nsContentUtils.h"
|
2012-11-09 10:32:58 +00:00
|
|
|
#include "mozilla/Preferences.h"
|
2013-04-04 12:01:08 +00:00
|
|
|
#include "nsTextNode.h"
|
2013-09-22 23:23:30 +00:00
|
|
|
#include "nsIController.h"
|
2013-09-25 11:21:19 +00:00
|
|
|
#include "mozilla/TextEvents.h"
|
2013-12-12 01:51:58 +00:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2014-08-01 00:25:31 +00:00
|
|
|
#include "mozilla/dom/HTMLInputElement.h"
|
|
|
|
#include "nsNumberControlFrame.h"
|
2014-08-06 05:19:27 +00:00
|
|
|
#include "nsFrameSelection.h"
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2012-06-10 23:44:50 +00:00
|
|
|
using namespace mozilla;
|
2010-10-25 12:17:38 +00:00
|
|
|
using namespace mozilla::dom;
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
static NS_DEFINE_CID(kTextEditorCID, NS_TEXTEDITOR_CID);
|
|
|
|
|
2013-08-28 19:06:00 +00:00
|
|
|
class MOZ_STACK_CLASS ValueSetter
|
|
|
|
{
|
|
|
|
public:
|
2014-09-02 00:49:25 +00:00
|
|
|
explicit ValueSetter(nsIEditor* aEditor)
|
2013-08-28 19:06:00 +00:00
|
|
|
: mEditor(aEditor)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aEditor);
|
|
|
|
|
|
|
|
// To protect against a reentrant call to SetValue, we check whether
|
|
|
|
// another SetValue is already happening for this editor. If it is,
|
|
|
|
// we must wait until we unwind to re-enable oninput events.
|
|
|
|
mEditor->GetSuppressDispatchingInputEvent(&mOuterTransaction);
|
|
|
|
}
|
|
|
|
~ValueSetter()
|
|
|
|
{
|
|
|
|
mEditor->SetSuppressDispatchingInputEvent(mOuterTransaction);
|
|
|
|
}
|
|
|
|
void Init()
|
|
|
|
{
|
|
|
|
mEditor->SetSuppressDispatchingInputEvent(true);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIEditor> mEditor;
|
|
|
|
bool mOuterTransaction;
|
|
|
|
};
|
|
|
|
|
2010-10-22 22:42:07 +00:00
|
|
|
class RestoreSelectionState : public nsRunnable {
|
|
|
|
public:
|
2011-07-29 21:31:57 +00:00
|
|
|
RestoreSelectionState(nsTextEditorState *aState, nsTextControlFrame *aFrame)
|
2010-10-22 22:42:07 +00:00
|
|
|
: mFrame(aFrame),
|
2011-04-12 02:03:50 +00:00
|
|
|
mTextEditorState(aState)
|
2010-10-22 22:42:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
2012-04-30 21:05:53 +00:00
|
|
|
if (!mTextEditorState) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-21 08:25:27 +00:00
|
|
|
if (mFrame) {
|
2011-04-04 11:41:02 +00:00
|
|
|
// SetSelectionRange leads to Selection::AddRange which flushes Layout -
|
|
|
|
// need to block script to avoid nested PrepareEditor calls (bug 642800).
|
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
2011-07-29 21:31:57 +00:00
|
|
|
nsTextEditorState::SelectionProperties& properties =
|
|
|
|
mTextEditorState->GetSelectionProperties();
|
|
|
|
mFrame->SetSelectionRange(properties.mStart,
|
|
|
|
properties.mEnd,
|
|
|
|
properties.mDirection);
|
|
|
|
if (!mTextEditorState->mSelectionRestoreEagerInit) {
|
|
|
|
mTextEditorState->HideSelectionIfBlurred();
|
|
|
|
}
|
2011-10-17 14:59:28 +00:00
|
|
|
mTextEditorState->mSelectionRestoreEagerInit = false;
|
2010-10-22 22:42:07 +00:00
|
|
|
}
|
2011-05-21 08:25:27 +00:00
|
|
|
mTextEditorState->FinishedRestoringSelection();
|
2010-10-22 22:42:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-05-21 08:25:27 +00:00
|
|
|
// Let the text editor tell us we're no longer relevant - avoids use of nsWeakFrame
|
|
|
|
void Revoke() {
|
2012-07-30 14:20:58 +00:00
|
|
|
mFrame = nullptr;
|
|
|
|
mTextEditorState = nullptr;
|
2011-05-21 08:25:27 +00:00
|
|
|
}
|
|
|
|
|
2010-10-22 22:42:07 +00:00
|
|
|
private:
|
|
|
|
nsTextControlFrame* mFrame;
|
2011-04-12 02:03:50 +00:00
|
|
|
nsTextEditorState* mTextEditorState;
|
2010-10-22 22:42:07 +00:00
|
|
|
};
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
/*static*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2010-04-21 20:17:41 +00:00
|
|
|
nsITextControlElement::GetWrapPropertyEnum(nsIContent* aContent,
|
|
|
|
nsITextControlElement::nsHTMLTextWrap& aWrapProp)
|
|
|
|
{
|
|
|
|
// soft is the default; "physical" defaults to soft as well because all other
|
|
|
|
// browsers treat it that way and there is no real reason to maintain physical
|
|
|
|
// and virtual as separate entities if no one else does. Only hard and off
|
|
|
|
// do anything different.
|
|
|
|
aWrapProp = eHTMLTextWrap_Soft; // the default
|
|
|
|
|
|
|
|
nsAutoString wrap;
|
|
|
|
if (aContent->IsHTML()) {
|
|
|
|
static nsIContent::AttrValuesArray strings[] =
|
2012-07-30 14:20:58 +00:00
|
|
|
{&nsGkAtoms::HARD, &nsGkAtoms::OFF, nullptr};
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
switch (aContent->FindAttrValueIn(kNameSpaceID_None, nsGkAtoms::wrap,
|
|
|
|
strings, eIgnoreCase)) {
|
|
|
|
case 0: aWrapProp = eHTMLTextWrap_Hard; break;
|
|
|
|
case 1: aWrapProp = eHTMLTextWrap_Off; break;
|
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
2010-11-16 20:45:49 +00:00
|
|
|
/*static*/
|
|
|
|
already_AddRefed<nsITextControlElement>
|
|
|
|
nsITextControlElement::GetTextControlElementFromEditingHost(nsIContent* aHost)
|
|
|
|
{
|
|
|
|
if (!aHost) {
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2010-11-16 20:45:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsITextControlElement> parent =
|
|
|
|
do_QueryInterface(aHost->GetParent());
|
|
|
|
|
|
|
|
return parent.forget();
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
static bool
|
2010-04-21 20:17:41 +00:00
|
|
|
SuppressEventHandlers(nsPresContext* aPresContext)
|
|
|
|
{
|
2011-09-29 06:19:26 +00:00
|
|
|
bool suppressHandlers = false;
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
if (aPresContext)
|
|
|
|
{
|
|
|
|
// Right now we only suppress event handlers and controller manipulation
|
|
|
|
// when in a print preview or print context!
|
|
|
|
|
|
|
|
// In the current implementation, we only paginate when
|
|
|
|
// printing or in print preview.
|
|
|
|
|
|
|
|
suppressHandlers = aPresContext->IsPaginated();
|
|
|
|
}
|
|
|
|
|
|
|
|
return suppressHandlers;
|
|
|
|
}
|
|
|
|
|
2012-02-02 16:15:18 +00:00
|
|
|
class nsAnonDivObserver MOZ_FINAL : public nsStubMutationObserver
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-09-02 00:49:25 +00:00
|
|
|
explicit nsAnonDivObserver(nsTextEditorState* aTextEditorState)
|
2010-04-21 20:17:41 +00:00
|
|
|
: mTextEditorState(aTextEditorState) {}
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED
|
|
|
|
NS_DECL_NSIMUTATIONOBSERVER_CONTENTAPPENDED
|
|
|
|
NS_DECL_NSIMUTATIONOBSERVER_CONTENTINSERTED
|
|
|
|
NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED
|
|
|
|
|
|
|
|
private:
|
2014-06-25 02:09:15 +00:00
|
|
|
~nsAnonDivObserver() {}
|
2010-04-21 20:17:41 +00:00
|
|
|
nsTextEditorState* mTextEditorState;
|
|
|
|
};
|
|
|
|
|
2012-02-02 16:15:18 +00:00
|
|
|
class nsTextInputSelectionImpl MOZ_FINAL : public nsSupportsWeakReference
|
|
|
|
, public nsISelectionController
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
2014-06-25 02:09:15 +00:00
|
|
|
~nsTextInputSelectionImpl(){}
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
public:
|
|
|
|
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
|
|
|
|
NS_DECL_CYCLE_COLLECTION_CLASS_AMBIGUOUS(nsTextInputSelectionImpl, nsISelectionController)
|
|
|
|
|
|
|
|
nsTextInputSelectionImpl(nsFrameSelection *aSel, nsIPresShell *aShell, nsIContent *aLimiter);
|
|
|
|
|
|
|
|
void SetScrollableFrame(nsIScrollableFrame *aScrollableFrame);
|
|
|
|
nsFrameSelection* GetConstFrameSelection()
|
|
|
|
{ return mFrameSelection; }
|
|
|
|
|
|
|
|
//NSISELECTIONCONTROLLER INTERFACES
|
2015-01-06 20:13:36 +00:00
|
|
|
NS_IMETHOD SetDisplaySelection(int16_t toggle) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD GetDisplaySelection(int16_t* _retval) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD SetSelectionFlags(int16_t aInEnable) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD GetSelectionFlags(int16_t *aOutEnable) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD GetSelection(int16_t type, nsISelection** _retval) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD ScrollSelectionIntoView(int16_t aType, int16_t aRegion, int16_t aFlags) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD RepaintSelection(int16_t type) MOZ_OVERRIDE;
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_IMETHOD RepaintSelection(nsPresContext* aPresContext, SelectionType aSelectionType);
|
2015-01-06 20:13:36 +00:00
|
|
|
NS_IMETHOD SetCaretEnabled(bool enabled) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD SetCaretReadOnly(bool aReadOnly) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD GetCaretEnabled(bool* _retval) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD GetCaretVisible(bool* _retval) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD SetCaretVisibilityDuringSelection(bool aVisibility) MOZ_OVERRIDE;
|
2014-11-22 14:39:03 +00:00
|
|
|
NS_IMETHOD PhysicalMove(int16_t aDirection, int16_t aAmount, bool aExtend) MOZ_OVERRIDE;
|
2015-01-06 20:13:36 +00:00
|
|
|
NS_IMETHOD CharacterMove(bool aForward, bool aExtend) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD CharacterExtendForDelete() MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD CharacterExtendForBackspace() MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD WordMove(bool aForward, bool aExtend) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD WordExtendForDelete(bool aForward) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD LineMove(bool aForward, bool aExtend) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD IntraLineMove(bool aForward, bool aExtend) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD PageMove(bool aForward, bool aExtend) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD CompleteScroll(bool aForward) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD CompleteMove(bool aForward, bool aExtend) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD ScrollPage(bool aForward) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD ScrollLine(bool aForward) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD ScrollCharacter(bool aRight) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD SelectAll(void) MOZ_OVERRIDE;
|
|
|
|
NS_IMETHOD CheckVisibility(nsIDOMNode *node, int16_t startOffset, int16_t EndOffset, bool* _retval) MOZ_OVERRIDE;
|
|
|
|
virtual nsresult CheckVisibilityContent(nsIContent* aNode, int16_t aStartOffset, int16_t aEndOffset, bool* aRetval) MOZ_OVERRIDE;
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
private:
|
2011-05-19 03:10:49 +00:00
|
|
|
nsRefPtr<nsFrameSelection> mFrameSelection;
|
2010-04-21 20:17:41 +00:00
|
|
|
nsCOMPtr<nsIContent> mLimiter;
|
|
|
|
nsIScrollableFrame *mScrollFrame;
|
|
|
|
nsWeakPtr mPresShellWeak;
|
|
|
|
};
|
|
|
|
|
2011-03-06 11:11:31 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsTextInputSelectionImpl)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(nsTextInputSelectionImpl)
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_INTERFACE_TABLE_HEAD(nsTextInputSelectionImpl)
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_INTERFACE_TABLE(nsTextInputSelectionImpl,
|
|
|
|
nsISelectionController,
|
|
|
|
nsISelectionDisplay,
|
|
|
|
nsISupportsWeakReference)
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_INTERFACE_TABLE_TO_MAP_SEGUE_CYCLE_COLLECTION(nsTextInputSelectionImpl)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
2014-04-25 16:49:00 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(nsTextInputSelectionImpl, mFrameSelection, mLimiter)
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
|
|
|
|
// BEGIN nsTextInputSelectionImpl
|
|
|
|
|
|
|
|
nsTextInputSelectionImpl::nsTextInputSelectionImpl(nsFrameSelection *aSel,
|
|
|
|
nsIPresShell *aShell,
|
|
|
|
nsIContent *aLimiter)
|
2012-07-30 14:20:58 +00:00
|
|
|
: mScrollFrame(nullptr)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (aSel && aShell)
|
|
|
|
{
|
|
|
|
mFrameSelection = aSel;//we are the owner now!
|
|
|
|
mLimiter = aLimiter;
|
|
|
|
mFrameSelection->Init(aShell, mLimiter);
|
|
|
|
mPresShellWeak = do_GetWeakReference(aShell);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsTextInputSelectionImpl::SetScrollableFrame(nsIScrollableFrame *aScrollableFrame)
|
|
|
|
{
|
|
|
|
mScrollFrame = aScrollableFrame;
|
|
|
|
if (!mScrollFrame && mFrameSelection) {
|
|
|
|
mFrameSelection->DisconnectFromPresShell();
|
2012-07-30 14:20:58 +00:00
|
|
|
mFrameSelection = nullptr;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTextInputSelectionImpl::SetDisplaySelection(int16_t aToggle)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (!mFrameSelection)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
mFrameSelection->SetDisplaySelection(aToggle);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTextInputSelectionImpl::GetDisplaySelection(int16_t *aToggle)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (!mFrameSelection)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*aToggle = mFrameSelection->GetDisplaySelection();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTextInputSelectionImpl::SetSelectionFlags(int16_t aToggle)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
return NS_OK;//stub this out. not used in input
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTextInputSelectionImpl::GetSelectionFlags(int16_t *aOutEnable)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
*aOutEnable = nsISelectionDisplay::DISPLAY_TEXT;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTextInputSelectionImpl::GetSelection(int16_t type, nsISelection **_retval)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (!mFrameSelection)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*_retval = mFrameSelection->GetSelection(type);
|
|
|
|
|
|
|
|
if (!(*_retval))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
NS_ADDREF(*_retval);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTextInputSelectionImpl::ScrollSelectionIntoView(int16_t aType, int16_t aRegion, int16_t aFlags)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (!mFrameSelection)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2010-10-21 20:56:20 +00:00
|
|
|
return mFrameSelection->ScrollSelectionIntoView(aType, aRegion, aFlags);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTextInputSelectionImpl::RepaintSelection(int16_t type)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (!mFrameSelection)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return mFrameSelection->RepaintSelection(type);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextInputSelectionImpl::RepaintSelection(nsPresContext* aPresContext, SelectionType aSelectionType)
|
|
|
|
{
|
|
|
|
if (!mFrameSelection)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
return mFrameSelection->RepaintSelection(aSelectionType);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::SetCaretEnabled(bool enabled)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIPresShell> shell = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!shell) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// tell the pres shell to enable the caret, rather than settings its visibility directly.
|
|
|
|
// this way the presShell's idea of caret visibility is maintained.
|
|
|
|
nsCOMPtr<nsISelectionController> selCon = do_QueryInterface(shell);
|
|
|
|
if (!selCon) return NS_ERROR_NO_INTERFACE;
|
|
|
|
selCon->SetCaretEnabled(enabled);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::SetCaretReadOnly(bool aReadOnly)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsresult result;
|
|
|
|
nsCOMPtr<nsIPresShell> shell = do_QueryReferent(mPresShellWeak, &result);
|
|
|
|
if (shell)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsCaret> caret = shell->GetCaret();
|
|
|
|
if (caret) {
|
|
|
|
nsISelection* domSel = mFrameSelection->
|
|
|
|
GetSelection(nsISelectionController::SELECTION_NORMAL);
|
|
|
|
if (domSel)
|
|
|
|
caret->SetCaretReadOnly(aReadOnly);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::GetCaretEnabled(bool *_retval)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
return GetCaretVisible(_retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::GetCaretVisible(bool *_retval)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsresult result;
|
|
|
|
nsCOMPtr<nsIPresShell> shell = do_QueryReferent(mPresShellWeak, &result);
|
|
|
|
if (shell)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsCaret> caret = shell->GetCaret();
|
|
|
|
if (caret) {
|
2014-08-06 05:19:29 +00:00
|
|
|
*_retval = caret->IsVisible();
|
|
|
|
return NS_OK;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::SetCaretVisibilityDuringSelection(bool aVisibility)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsresult result;
|
|
|
|
nsCOMPtr<nsIPresShell> shell = do_QueryReferent(mPresShellWeak, &result);
|
|
|
|
if (shell)
|
|
|
|
{
|
|
|
|
nsRefPtr<nsCaret> caret = shell->GetCaret();
|
|
|
|
if (caret) {
|
|
|
|
nsISelection* domSel = mFrameSelection->
|
|
|
|
GetSelection(nsISelectionController::SELECTION_NORMAL);
|
|
|
|
if (domSel)
|
|
|
|
caret->SetVisibilityDuringSelection(aVisibility);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2014-11-22 14:39:03 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextInputSelectionImpl::PhysicalMove(int16_t aDirection, int16_t aAmount,
|
|
|
|
bool aExtend)
|
|
|
|
{
|
|
|
|
if (mFrameSelection)
|
|
|
|
return mFrameSelection->PhysicalMove(aDirection, aAmount, aExtend);
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::CharacterMove(bool aForward, bool aExtend)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (mFrameSelection)
|
|
|
|
return mFrameSelection->CharacterMove(aForward, aExtend);
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextInputSelectionImpl::CharacterExtendForDelete()
|
|
|
|
{
|
|
|
|
if (mFrameSelection)
|
|
|
|
return mFrameSelection->CharacterExtendForDelete();
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
2010-06-04 17:28:19 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextInputSelectionImpl::CharacterExtendForBackspace()
|
|
|
|
{
|
|
|
|
if (mFrameSelection)
|
|
|
|
return mFrameSelection->CharacterExtendForBackspace();
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::WordMove(bool aForward, bool aExtend)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (mFrameSelection)
|
|
|
|
return mFrameSelection->WordMove(aForward, aExtend);
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::WordExtendForDelete(bool aForward)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (mFrameSelection)
|
|
|
|
return mFrameSelection->WordExtendForDelete(aForward);
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::LineMove(bool aForward, bool aExtend)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (mFrameSelection)
|
|
|
|
{
|
|
|
|
nsresult result = mFrameSelection->LineMove(aForward, aExtend);
|
|
|
|
if (NS_FAILED(result))
|
|
|
|
result = CompleteMove(aForward,aExtend);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::IntraLineMove(bool aForward, bool aExtend)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (mFrameSelection)
|
|
|
|
return mFrameSelection->IntraLineMove(aForward, aExtend);
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::PageMove(bool aForward, bool aExtend)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
// expected behavior for PageMove is to scroll AND move the caret
|
|
|
|
// and to remain relative position of the caret in view. see Bug 4302.
|
|
|
|
if (mScrollFrame)
|
|
|
|
{
|
|
|
|
mFrameSelection->CommonPageMove(aForward, aExtend, mScrollFrame);
|
|
|
|
}
|
|
|
|
// After ScrollSelectionIntoView(), the pending notifications might be
|
|
|
|
// flushed and PresShell/PresContext/Frames may be dead. See bug 418470.
|
2010-10-21 20:56:20 +00:00
|
|
|
return ScrollSelectionIntoView(nsISelectionController::SELECTION_NORMAL, nsISelectionController::SELECTION_FOCUS_REGION,
|
|
|
|
nsISelectionController::SCROLL_SYNCHRONOUS);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::CompleteScroll(bool aForward)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (!mScrollFrame)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
mScrollFrame->ScrollBy(nsIntPoint(0, aForward ? 1 : -1),
|
|
|
|
nsIScrollableFrame::WHOLE,
|
|
|
|
nsIScrollableFrame::INSTANT);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::CompleteMove(bool aForward, bool aExtend)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
// grab the parent / root DIV for this text widget
|
|
|
|
nsIContent* parentDIV = mFrameSelection->GetLimiter();
|
|
|
|
if (!parentDIV)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
// make the caret be either at the very beginning (0) or the very end
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t offset = 0;
|
2014-08-06 05:19:27 +00:00
|
|
|
CaretAssociationHint hint = CARET_ASSOCIATE_BEFORE;
|
2010-04-21 20:17:41 +00:00
|
|
|
if (aForward)
|
|
|
|
{
|
|
|
|
offset = parentDIV->GetChildCount();
|
|
|
|
|
|
|
|
// Prevent the caret from being placed after the last
|
|
|
|
// BR node in the content tree!
|
|
|
|
|
|
|
|
if (offset > 0)
|
|
|
|
{
|
2011-09-27 07:54:58 +00:00
|
|
|
nsIContent *child = parentDIV->GetLastChild();
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
if (child->Tag() == nsGkAtoms::br)
|
|
|
|
{
|
|
|
|
--offset;
|
2014-08-06 05:19:27 +00:00
|
|
|
hint = CARET_ASSOCIATE_AFTER; // for Bug 106855
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mFrameSelection->HandleClick(parentDIV, offset, offset, aExtend,
|
2011-10-17 14:59:28 +00:00
|
|
|
false, hint);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
// if we got this far, attempt to scroll no matter what the above result is
|
|
|
|
return CompleteScroll(aForward);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::ScrollPage(bool aForward)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (!mScrollFrame)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
mScrollFrame->ScrollBy(nsIntPoint(0, aForward ? 1 : -1),
|
|
|
|
nsIScrollableFrame::PAGES,
|
|
|
|
nsIScrollableFrame::SMOOTH);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextInputSelectionImpl::ScrollLine(bool aForward)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (!mScrollFrame)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
mScrollFrame->ScrollBy(nsIntPoint(0, aForward ? 1 : -1),
|
|
|
|
nsIScrollableFrame::LINES,
|
|
|
|
nsIScrollableFrame::SMOOTH);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-01-05 21:33:41 +00:00
|
|
|
nsTextInputSelectionImpl::ScrollCharacter(bool aRight)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (!mScrollFrame)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2012-01-05 21:33:41 +00:00
|
|
|
mScrollFrame->ScrollBy(nsIntPoint(aRight ? 1 : -1, 0),
|
2010-04-21 20:17:41 +00:00
|
|
|
nsIScrollableFrame::LINES,
|
|
|
|
nsIScrollableFrame::SMOOTH);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextInputSelectionImpl::SelectAll()
|
|
|
|
{
|
|
|
|
if (mFrameSelection)
|
|
|
|
return mFrameSelection->SelectAll();
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTextInputSelectionImpl::CheckVisibility(nsIDOMNode *node, int16_t startOffset, int16_t EndOffset, bool *_retval)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (!mPresShellWeak) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
nsresult result;
|
|
|
|
nsCOMPtr<nsISelectionController> shell = do_QueryReferent(mPresShellWeak, &result);
|
|
|
|
if (shell)
|
|
|
|
{
|
|
|
|
return shell->CheckVisibility(node,startOffset,EndOffset, _retval);
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2012-01-25 07:50:05 +00:00
|
|
|
nsresult
|
|
|
|
nsTextInputSelectionImpl::CheckVisibilityContent(nsIContent* aNode,
|
2012-08-22 15:56:38 +00:00
|
|
|
int16_t aStartOffset,
|
|
|
|
int16_t aEndOffset,
|
2012-01-25 07:50:05 +00:00
|
|
|
bool* aRetval)
|
|
|
|
{
|
|
|
|
if (!mPresShellWeak) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelectionController> shell = do_QueryReferent(mPresShellWeak);
|
|
|
|
NS_ENSURE_TRUE(shell, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return shell->CheckVisibilityContent(aNode, aStartOffset, aEndOffset, aRetval);
|
|
|
|
}
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
class nsTextInputListener : public nsISelectionListener,
|
2011-06-25 05:56:13 +00:00
|
|
|
public nsIDOMEventListener,
|
2012-11-14 19:23:35 +00:00
|
|
|
public nsIEditorObserver,
|
2010-04-21 20:17:41 +00:00
|
|
|
public nsSupportsWeakReference
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/** the default constructor
|
|
|
|
*/
|
|
|
|
explicit nsTextInputListener(nsITextControlElement* aTxtCtrlElement);
|
|
|
|
|
|
|
|
/** SetEditor gives an address to the editor that will be accessed
|
|
|
|
* @param aEditor the editor this listener calls for editing operations
|
|
|
|
*/
|
|
|
|
void SetFrame(nsTextControlFrame *aFrame){mFrame = aFrame;}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
void SettingValue(bool aValue) { mSettingValue = aValue; }
|
2012-05-31 16:01:30 +00:00
|
|
|
void SetValueChanged(bool aSetValueChanged) { mSetValueChanged = aSetValueChanged; }
|
2010-10-06 20:31:09 +00:00
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
NS_DECL_NSISELECTIONLISTENER
|
|
|
|
|
2011-06-25 05:56:13 +00:00
|
|
|
NS_DECL_NSIDOMEVENTLISTENER
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2012-11-14 19:23:35 +00:00
|
|
|
NS_DECL_NSIEDITOROBSERVER
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
protected:
|
2014-06-25 02:09:15 +00:00
|
|
|
/** the default destructor. virtual due to the possibility of derivation.
|
|
|
|
*/
|
|
|
|
virtual ~nsTextInputListener();
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2014-07-27 23:21:00 +00:00
|
|
|
nsresult UpdateTextInputCommands(const nsAString& commandsToUpdate,
|
|
|
|
nsISelection* sel = nullptr,
|
|
|
|
int16_t reason = 0);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
protected:
|
|
|
|
|
2011-05-21 08:25:27 +00:00
|
|
|
nsIFrame* mFrame;
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
nsITextControlElement* const mTxtCtrlElement;
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mSelectionWasCollapsed;
|
2010-04-21 20:17:41 +00:00
|
|
|
/**
|
|
|
|
* Whether we had undo items or not the last time we got EditAction()
|
|
|
|
* notification (when this state changes we update undo and redo menus)
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mHadUndoItems;
|
2010-04-21 20:17:41 +00:00
|
|
|
/**
|
|
|
|
* Whether we had redo items or not the last time we got EditAction()
|
|
|
|
* notification (when this state changes we update undo and redo menus)
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mHadRedoItems;
|
2010-10-06 20:31:09 +00:00
|
|
|
/**
|
|
|
|
* Whether we're in the process of a SetValue call, and should therefore
|
|
|
|
* refrain from calling OnValueChanged.
|
|
|
|
*/
|
2011-09-29 06:19:26 +00:00
|
|
|
bool mSettingValue;
|
2012-05-31 16:01:30 +00:00
|
|
|
/**
|
|
|
|
* Whether we are in the process of a SetValue call that doesn't want
|
|
|
|
* |SetValueChanged| to be called.
|
|
|
|
*/
|
|
|
|
bool mSetValueChanged;
|
2010-04-21 20:17:41 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* nsTextInputListener implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsTextInputListener::nsTextInputListener(nsITextControlElement* aTxtCtrlElement)
|
2012-07-30 14:20:58 +00:00
|
|
|
: mFrame(nullptr)
|
2011-05-21 08:25:27 +00:00
|
|
|
, mTxtCtrlElement(aTxtCtrlElement)
|
2011-10-17 14:59:28 +00:00
|
|
|
, mSelectionWasCollapsed(true)
|
|
|
|
, mHadUndoItems(false)
|
|
|
|
, mHadRedoItems(false)
|
|
|
|
, mSettingValue(false)
|
2012-05-31 16:01:30 +00:00
|
|
|
, mSetValueChanged(true)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTextInputListener::~nsTextInputListener()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsTextInputListener,
|
|
|
|
nsISelectionListener,
|
|
|
|
nsIEditorObserver,
|
|
|
|
nsISupportsWeakReference,
|
|
|
|
nsIDOMEventListener)
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
// BEGIN nsIDOMSelectionListener
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTextInputListener::NotifySelectionChanged(nsIDOMDocument* aDoc, nsISelection* aSel, int16_t aReason)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
2011-09-29 06:19:26 +00:00
|
|
|
bool collapsed;
|
2011-05-21 08:25:27 +00:00
|
|
|
nsWeakFrame weakFrame = mFrame;
|
|
|
|
|
|
|
|
if (!aDoc || !aSel || NS_FAILED(aSel->GetIsCollapsed(&collapsed)))
|
2010-04-21 20:17:41 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Fire the select event
|
|
|
|
// The specs don't exactly say when we should fire the select event.
|
|
|
|
// IE: Whenever you add/remove a character to/from the selection. Also
|
|
|
|
// each time for select all. Also if you get to the end of the text
|
|
|
|
// field you will get new event for each keypress or a continuous
|
|
|
|
// stream of events if you use the mouse. IE will fire select event
|
|
|
|
// when the selection collapses to nothing if you are holding down
|
|
|
|
// the shift or mouse button.
|
|
|
|
// Mozilla: If we have non-empty selection we will fire a new event for each
|
|
|
|
// keypress (or mouseup) if the selection changed. Mozilla will also
|
|
|
|
// create the event each time select all is called, even if everything
|
|
|
|
// was previously selected, becase technically select all will first collapse
|
|
|
|
// and then extend. Mozilla will never create an event if the selection
|
|
|
|
// collapses to nothing.
|
|
|
|
if (!collapsed && (aReason & (nsISelectionListener::MOUSEUP_REASON |
|
|
|
|
nsISelectionListener::KEYPRESS_REASON |
|
|
|
|
nsISelectionListener::SELECTALL_REASON)))
|
|
|
|
{
|
|
|
|
nsIContent* content = mFrame->GetContent();
|
|
|
|
if (content)
|
|
|
|
{
|
2014-08-05 23:23:02 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = content->GetComposedDoc();
|
2010-04-21 20:17:41 +00:00
|
|
|
if (doc)
|
|
|
|
{
|
2010-06-25 13:59:57 +00:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = doc->GetShell();
|
2010-04-21 20:17:41 +00:00
|
|
|
if (presShell)
|
|
|
|
{
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
2013-10-02 03:46:04 +00:00
|
|
|
WidgetEvent event(true, NS_FORM_SELECTED);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
presShell->HandleEventWithTarget(&event, mFrame, content, &status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if the collapsed state did not change, don't fire notifications
|
|
|
|
if (collapsed == mSelectionWasCollapsed)
|
|
|
|
return NS_OK;
|
2014-07-27 23:21:00 +00:00
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
mSelectionWasCollapsed = collapsed;
|
|
|
|
|
2011-05-21 08:25:27 +00:00
|
|
|
if (!weakFrame.IsAlive() || !nsContentUtils::IsFocusedContent(mFrame->GetContent()))
|
2010-04-21 20:17:41 +00:00
|
|
|
return NS_OK;
|
|
|
|
|
2014-07-27 23:21:00 +00:00
|
|
|
return UpdateTextInputCommands(NS_LITERAL_STRING("select"), aSel, aReason);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// END nsIDOMSelectionListener
|
|
|
|
|
|
|
|
static void
|
2014-03-14 13:13:30 +00:00
|
|
|
DoCommandCallback(Command aCommand, void* aData)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
nsTextControlFrame *frame = static_cast<nsTextControlFrame*>(aData);
|
|
|
|
nsIContent *content = frame->GetContent();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIControllers> controllers;
|
2010-08-05 02:40:18 +00:00
|
|
|
nsCOMPtr<nsIDOMHTMLInputElement> input = do_QueryInterface(content);
|
2010-04-21 20:17:41 +00:00
|
|
|
if (input) {
|
|
|
|
input->GetControllers(getter_AddRefs(controllers));
|
|
|
|
} else {
|
2011-06-11 07:52:24 +00:00
|
|
|
nsCOMPtr<nsIDOMHTMLTextAreaElement> textArea =
|
2010-04-21 20:17:41 +00:00
|
|
|
do_QueryInterface(content);
|
|
|
|
|
|
|
|
if (textArea) {
|
|
|
|
textArea->GetControllers(getter_AddRefs(controllers));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!controllers) {
|
|
|
|
NS_WARNING("Could not get controllers");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-03-14 13:13:30 +00:00
|
|
|
const char* commandStr = WidgetKeyboardEvent::GetCommandStr(aCommand);
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
nsCOMPtr<nsIController> controller;
|
2014-03-14 13:13:30 +00:00
|
|
|
controllers->GetControllerForCommand(commandStr, getter_AddRefs(controller));
|
2013-07-10 14:14:21 +00:00
|
|
|
if (!controller) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool commandEnabled;
|
2014-03-14 13:13:30 +00:00
|
|
|
nsresult rv = controller->IsCommandEnabled(commandStr, &commandEnabled);
|
2013-07-10 14:14:21 +00:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
if (commandEnabled) {
|
2014-03-14 13:13:30 +00:00
|
|
|
controller->DoCommand(commandStr);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2011-06-25 05:56:13 +00:00
|
|
|
nsTextInputListener::HandleEvent(nsIDOMEvent* aEvent)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
2013-08-24 07:24:32 +00:00
|
|
|
bool defaultPrevented = false;
|
|
|
|
nsresult rv = aEvent->GetDefaultPrevented(&defaultPrevented);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (defaultPrevented) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2013-08-24 07:24:32 +00:00
|
|
|
bool isTrusted = false;
|
|
|
|
rv = aEvent->GetIsTrusted(&isTrusted);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!isTrusted) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-06-25 05:56:13 +00:00
|
|
|
|
2013-10-01 07:22:58 +00:00
|
|
|
WidgetKeyboardEvent* keyEvent =
|
2013-10-18 06:10:24 +00:00
|
|
|
aEvent->GetInternalNSEvent()->AsKeyboardEvent();
|
|
|
|
if (!keyEvent) {
|
2013-08-24 07:24:32 +00:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2014-03-14 13:13:31 +00:00
|
|
|
if (keyEvent->message != NS_KEY_PRESS) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-03-14 13:13:31 +00:00
|
|
|
nsIWidget::NativeKeyBindingsType nativeKeyBindingsType =
|
|
|
|
mTxtCtrlElement->IsTextArea() ?
|
|
|
|
nsIWidget::NativeKeyBindingsForMultiLineEditor :
|
|
|
|
nsIWidget::NativeKeyBindingsForSingleLineEditor;
|
|
|
|
nsIWidget* widget = keyEvent->widget;
|
|
|
|
// If the event is created by chrome script, the widget is nullptr.
|
|
|
|
if (!widget) {
|
|
|
|
widget = mFrame->GetNearestWidget();
|
|
|
|
NS_ENSURE_TRUE(widget, NS_OK);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
2014-03-14 13:13:31 +00:00
|
|
|
|
|
|
|
if (widget->ExecuteNativeKeyBinding(nativeKeyBindingsType,
|
|
|
|
*keyEvent, DoCommandCallback, mFrame)) {
|
2014-03-14 13:13:31 +00:00
|
|
|
aEvent->PreventDefault();
|
|
|
|
}
|
2010-04-21 20:17:41 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-11-14 19:23:35 +00:00
|
|
|
// BEGIN nsIEditorObserver
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2010-04-21 20:17:41 +00:00
|
|
|
nsTextInputListener::EditAction()
|
|
|
|
{
|
2011-05-21 08:25:27 +00:00
|
|
|
nsWeakFrame weakFrame = mFrame;
|
|
|
|
|
|
|
|
nsITextControlFrame* frameBase = do_QueryFrame(mFrame);
|
2011-01-12 15:40:21 +00:00
|
|
|
nsTextControlFrame* frame = static_cast<nsTextControlFrame*> (frameBase);
|
|
|
|
NS_ASSERTION(frame, "Where is our frame?");
|
2010-04-21 20:17:41 +00:00
|
|
|
//
|
|
|
|
// Update the undo / redo menus
|
|
|
|
//
|
|
|
|
nsCOMPtr<nsIEditor> editor;
|
2011-01-12 15:40:21 +00:00
|
|
|
frame->GetEditor(getter_AddRefs(editor));
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
// Get the number of undo / redo items
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t numUndoItems = 0;
|
|
|
|
int32_t numRedoItems = 0;
|
2012-02-23 20:47:37 +00:00
|
|
|
editor->GetNumberOfUndoItems(&numUndoItems);
|
|
|
|
editor->GetNumberOfRedoItems(&numRedoItems);
|
2010-04-21 20:17:41 +00:00
|
|
|
if ((numUndoItems && !mHadUndoItems) || (!numUndoItems && mHadUndoItems) ||
|
|
|
|
(numRedoItems && !mHadRedoItems) || (!numRedoItems && mHadRedoItems)) {
|
|
|
|
// Modify the menu if undo or redo items are different
|
|
|
|
UpdateTextInputCommands(NS_LITERAL_STRING("undo"));
|
|
|
|
|
|
|
|
mHadUndoItems = numUndoItems != 0;
|
|
|
|
mHadRedoItems = numRedoItems != 0;
|
|
|
|
}
|
|
|
|
|
2011-05-21 08:25:27 +00:00
|
|
|
if (!weakFrame.IsAlive()) {
|
2012-11-14 19:23:35 +00:00
|
|
|
return NS_OK;
|
2011-01-12 15:40:21 +00:00
|
|
|
}
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
// Make sure we know we were changed (do NOT set this to false if there are
|
|
|
|
// no undo items; JS could change the value and we'd still need to save it)
|
2012-05-31 16:01:30 +00:00
|
|
|
if (mSetValueChanged) {
|
|
|
|
frame->SetValueChanged(true);
|
|
|
|
}
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2010-10-06 20:31:09 +00:00
|
|
|
if (!mSettingValue) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mTxtCtrlElement->OnValueChanged(true);
|
2010-10-06 20:31:09 +00:00
|
|
|
}
|
2012-11-14 19:23:35 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
2014-07-31 04:37:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextInputListener::BeforeEditAction()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsTextInputListener::CancelEditAction()
|
|
|
|
{
|
|
|
|
return NS_OK;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
2012-11-14 19:23:35 +00:00
|
|
|
// END nsIEditorObserver
|
|
|
|
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
nsresult
|
2014-07-27 23:21:00 +00:00
|
|
|
nsTextInputListener::UpdateTextInputCommands(const nsAString& commandsToUpdate,
|
|
|
|
nsISelection* sel,
|
|
|
|
int16_t reason)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
nsIContent* content = mFrame->GetContent();
|
|
|
|
NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);
|
|
|
|
|
2014-08-05 23:23:02 +00:00
|
|
|
nsCOMPtr<nsIDocument> doc = content->GetComposedDoc();
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsPIDOMWindow *domWindow = doc->GetWindow();
|
|
|
|
NS_ENSURE_TRUE(domWindow, NS_ERROR_FAILURE);
|
|
|
|
|
2014-07-27 23:21:00 +00:00
|
|
|
return domWindow->UpdateCommands(commandsToUpdate, sel, reason);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// END nsTextInputListener
|
|
|
|
|
|
|
|
// nsTextEditorState
|
|
|
|
|
|
|
|
nsTextEditorState::nsTextEditorState(nsITextControlElement* aOwningElement)
|
|
|
|
: mTextCtrlElement(aOwningElement),
|
2012-07-30 14:20:58 +00:00
|
|
|
mRestoringSelection(nullptr),
|
|
|
|
mBoundFrame(nullptr),
|
2011-10-17 14:59:28 +00:00
|
|
|
mEverInited(false),
|
|
|
|
mEditorInitialized(false),
|
|
|
|
mInitializing(false),
|
|
|
|
mValueTransferInProgress(false),
|
|
|
|
mSelectionCached(true),
|
2012-11-09 10:22:29 +00:00
|
|
|
mSelectionRestoreEagerInit(false),
|
|
|
|
mPlaceholderVisibility(false)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsTextEditorState);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTextEditorState::~nsTextEditorState()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsTextEditorState);
|
|
|
|
Clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsTextEditorState::Clear()
|
|
|
|
{
|
|
|
|
if (mBoundFrame) {
|
|
|
|
// Oops, we still have a frame!
|
|
|
|
// This should happen when the type of a text input control is being changed
|
|
|
|
// to something which is not a text control. In this case, we should pretend
|
|
|
|
// that a frame is being destroyed, and clean up after ourselves properly.
|
|
|
|
UnbindFromFrame(mBoundFrame);
|
2012-07-30 14:20:58 +00:00
|
|
|
mEditor = nullptr;
|
2010-04-21 20:17:41 +00:00
|
|
|
} else {
|
|
|
|
// If we have a bound frame around, UnbindFromFrame will call DestroyEditor
|
|
|
|
// for us.
|
|
|
|
DestroyEditor();
|
|
|
|
}
|
2015-01-16 18:40:13 +00:00
|
|
|
mTextListener = nullptr;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
2012-04-24 04:19:26 +00:00
|
|
|
void nsTextEditorState::Unlink()
|
|
|
|
{
|
|
|
|
nsTextEditorState* tmp = this;
|
2010-04-21 20:17:41 +00:00
|
|
|
tmp->Clear();
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSelCon)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mEditor)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mRootNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mPlaceholderDiv)
|
2012-04-24 04:19:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsTextEditorState::Traverse(nsCycleCollectionTraversalCallback& cb)
|
|
|
|
{
|
|
|
|
nsTextEditorState* tmp = this;
|
2012-11-15 07:32:40 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSelCon)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEditor)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRootNode)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mPlaceholderDiv)
|
2012-04-24 04:19:26 +00:00
|
|
|
}
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
nsFrameSelection*
|
|
|
|
nsTextEditorState::GetConstFrameSelection() {
|
|
|
|
if (mSelCon)
|
|
|
|
return mSelCon->GetConstFrameSelection();
|
2012-07-30 14:20:58 +00:00
|
|
|
return nullptr;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsIEditor*
|
|
|
|
nsTextEditorState::GetEditor()
|
|
|
|
{
|
|
|
|
if (!mEditor) {
|
|
|
|
nsresult rv = PrepareEditor();
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, nullptr);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
return mEditor;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsISelectionController*
|
|
|
|
nsTextEditorState::GetSelectionController() const
|
|
|
|
{
|
|
|
|
return mSelCon;
|
|
|
|
}
|
|
|
|
|
2010-07-18 22:07:53 +00:00
|
|
|
// Helper class, used below in BindToFrame().
|
|
|
|
class PrepareEditorEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
PrepareEditorEvent(nsTextEditorState &aState,
|
|
|
|
nsIContent *aOwnerContent,
|
|
|
|
const nsAString &aCurrentValue)
|
2014-07-30 19:52:05 +00:00
|
|
|
: mState(&aState)
|
2010-07-18 22:07:53 +00:00
|
|
|
, mOwnerContent(aOwnerContent)
|
|
|
|
, mCurrentValue(aCurrentValue)
|
|
|
|
{
|
2012-10-10 19:22:27 +00:00
|
|
|
aState.mValueTransferInProgress = true;
|
2010-07-18 22:07:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
2012-10-10 19:22:27 +00:00
|
|
|
NS_ENSURE_TRUE(mState, NS_ERROR_NULL_POINTER);
|
|
|
|
|
2010-07-18 22:07:53 +00:00
|
|
|
// Transfer the saved value to the editor if we have one
|
2012-07-30 14:20:58 +00:00
|
|
|
const nsAString *value = nullptr;
|
2010-07-18 22:07:53 +00:00
|
|
|
if (!mCurrentValue.IsEmpty()) {
|
|
|
|
value = &mCurrentValue;
|
|
|
|
}
|
|
|
|
|
2012-11-04 12:27:49 +00:00
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
|
|
|
|
2012-10-10 19:22:27 +00:00
|
|
|
mState->PrepareEditor(value);
|
2010-07-18 22:07:53 +00:00
|
|
|
|
2012-10-10 19:22:27 +00:00
|
|
|
mState->mValueTransferInProgress = false;
|
2011-07-19 18:28:50 +00:00
|
|
|
|
2010-07-18 22:07:53 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2012-10-10 19:22:27 +00:00
|
|
|
WeakPtr<nsTextEditorState> mState;
|
2010-07-18 22:07:53 +00:00
|
|
|
nsCOMPtr<nsIContent> mOwnerContent; // strong reference
|
|
|
|
nsAutoString mCurrentValue;
|
|
|
|
};
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
nsresult
|
|
|
|
nsTextEditorState::BindToFrame(nsTextControlFrame* aFrame)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(aFrame, "The frame to bind to should be valid");
|
|
|
|
NS_ENSURE_ARG_POINTER(aFrame);
|
|
|
|
|
|
|
|
NS_ASSERTION(!mBoundFrame, "Cannot bind twice, need to unbind first");
|
|
|
|
NS_ENSURE_TRUE(!mBoundFrame, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// If we'll need to transfer our current value to the editor, save it before
|
|
|
|
// binding to the frame.
|
|
|
|
nsAutoString currentValue;
|
|
|
|
if (mEditor) {
|
2011-10-17 14:59:28 +00:00
|
|
|
GetValue(currentValue, true);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mBoundFrame = aFrame;
|
|
|
|
|
|
|
|
nsIContent *rootNode = GetRootNode();
|
|
|
|
|
2011-09-13 19:09:51 +00:00
|
|
|
nsresult rv = InitializeRootNode();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
nsIPresShell *shell = mBoundFrame->PresContext()->GetPresShell();
|
|
|
|
NS_ENSURE_TRUE(shell, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// Create selection
|
2011-05-19 05:26:45 +00:00
|
|
|
nsRefPtr<nsFrameSelection> frameSel = new nsFrameSelection();
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
// Create a SelectionController
|
|
|
|
mSelCon = new nsTextInputSelectionImpl(frameSel, shell, rootNode);
|
2015-01-16 18:40:13 +00:00
|
|
|
MOZ_ASSERT(!mTextListener, "Should not overwrite the object");
|
2010-04-21 20:17:41 +00:00
|
|
|
mTextListener = new nsTextInputListener(mTextCtrlElement);
|
|
|
|
|
|
|
|
mTextListener->SetFrame(mBoundFrame);
|
|
|
|
mSelCon->SetDisplaySelection(nsISelectionController::SELECTION_ON);
|
|
|
|
|
|
|
|
// Get the caret and make it a selection listener.
|
|
|
|
nsRefPtr<nsISelection> domSelection;
|
|
|
|
if (NS_SUCCEEDED(mSelCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
|
|
|
|
getter_AddRefs(domSelection))) &&
|
|
|
|
domSelection) {
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(domSelection));
|
|
|
|
nsRefPtr<nsCaret> caret = shell->GetCaret();
|
|
|
|
nsCOMPtr<nsISelectionListener> listener;
|
|
|
|
if (caret) {
|
|
|
|
listener = do_QueryInterface(caret);
|
|
|
|
if (listener) {
|
|
|
|
selPriv->AddSelectionListener(listener);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
selPriv->AddSelectionListener(static_cast<nsISelectionListener*>
|
|
|
|
(mTextListener));
|
|
|
|
}
|
|
|
|
|
|
|
|
// If an editor exists from before, prepare it for usage
|
|
|
|
if (mEditor) {
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(mTextCtrlElement);
|
|
|
|
NS_ENSURE_TRUE(content, NS_ERROR_FAILURE);
|
|
|
|
|
2011-01-27 05:26:33 +00:00
|
|
|
// Set the correct direction on the newly created root node
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t flags;
|
2011-05-19 05:26:45 +00:00
|
|
|
nsresult rv = mEditor->GetFlags(&flags);
|
2011-01-27 05:26:33 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (flags & nsIPlaintextEditor::eEditorRightToLeft) {
|
2011-10-17 14:59:28 +00:00
|
|
|
rootNode->SetAttr(kNameSpaceID_None, nsGkAtoms::dir, NS_LITERAL_STRING("rtl"), false);
|
2011-01-27 05:26:33 +00:00
|
|
|
} else if (flags & nsIPlaintextEditor::eEditorLeftToRight) {
|
2011-10-17 14:59:28 +00:00
|
|
|
rootNode->SetAttr(kNameSpaceID_None, nsGkAtoms::dir, NS_LITERAL_STRING("ltr"), false);
|
2011-01-27 05:26:33 +00:00
|
|
|
} else {
|
|
|
|
// otherwise, inherit the content node's direction
|
|
|
|
}
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
if (!nsContentUtils::AddScriptRunner(
|
|
|
|
new PrepareEditorEvent(*this, content, currentValue)))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-10-20 18:48:34 +00:00
|
|
|
struct PreDestroyer
|
|
|
|
{
|
|
|
|
void Init(nsIEditor* aEditor)
|
|
|
|
{
|
|
|
|
mNewEditor = aEditor;
|
|
|
|
}
|
|
|
|
~PreDestroyer()
|
|
|
|
{
|
|
|
|
if (mNewEditor) {
|
|
|
|
mNewEditor->PreDestroy(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Swap(nsCOMPtr<nsIEditor>& aEditor)
|
|
|
|
{
|
|
|
|
return mNewEditor.swap(aEditor);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsIEditor> mNewEditor;
|
|
|
|
};
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
nsresult
|
|
|
|
nsTextEditorState::PrepareEditor(const nsAString *aValue)
|
|
|
|
{
|
|
|
|
if (!mBoundFrame) {
|
|
|
|
// Cannot create an editor without a bound frame.
|
|
|
|
// Don't return a failure code, because js callers can't handle that.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mEditorInitialized) {
|
|
|
|
// Do not initialize the editor multiple times.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-04 18:59:35 +00:00
|
|
|
// Don't attempt to initialize recursively!
|
|
|
|
InitializationGuard guard(*this);
|
|
|
|
if (guard.IsInitializingRecursively()) {
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
}
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
// Note that we don't check mEditor here, because we might already have one
|
|
|
|
// around, in which case we don't create a new one, and we'll just tie the
|
|
|
|
// required machinery to it.
|
|
|
|
|
|
|
|
nsPresContext *presContext = mBoundFrame->PresContext();
|
|
|
|
nsIPresShell *shell = presContext->GetPresShell();
|
|
|
|
|
|
|
|
// Setup the editor flags
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t editorFlags = 0;
|
2010-04-21 20:17:41 +00:00
|
|
|
if (IsPlainTextControl())
|
|
|
|
editorFlags |= nsIPlaintextEditor::eEditorPlaintextMask;
|
|
|
|
if (IsSingleLineTextControl())
|
|
|
|
editorFlags |= nsIPlaintextEditor::eEditorSingleLineMask;
|
|
|
|
if (IsPasswordTextControl())
|
|
|
|
editorFlags |= nsIPlaintextEditor::eEditorPasswordMask;
|
|
|
|
|
|
|
|
// All nsTextControlFrames are widgets
|
|
|
|
editorFlags |= nsIPlaintextEditor::eEditorWidgetMask;
|
|
|
|
|
2011-10-17 02:06:24 +00:00
|
|
|
// Spell check is diabled at creation time. It is enabled once
|
|
|
|
// the editor comes into focus.
|
|
|
|
editorFlags |= nsIPlaintextEditor::eEditorSkipSpellCheck;
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool shouldInitializeEditor = false;
|
2010-04-21 20:17:41 +00:00
|
|
|
nsCOMPtr<nsIEditor> newEditor; // the editor that we might create
|
2012-01-04 00:28:45 +00:00
|
|
|
nsresult rv = NS_OK;
|
2012-10-20 18:48:34 +00:00
|
|
|
PreDestroyer preDestroyer;
|
2010-04-21 20:17:41 +00:00
|
|
|
if (!mEditor) {
|
2011-10-17 14:59:28 +00:00
|
|
|
shouldInitializeEditor = true;
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
// Create an editor
|
|
|
|
newEditor = do_CreateInstance(kTextEditorCID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-10-20 18:48:34 +00:00
|
|
|
preDestroyer.Init(newEditor);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
// Make sure we clear out the non-breaking space before we initialize the editor
|
2012-11-03 12:08:09 +00:00
|
|
|
rv = mBoundFrame->UpdateValueDisplay(true, true);
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
2010-10-14 19:36:35 +00:00
|
|
|
if (aValue || !mEditorInitialized) {
|
2010-04-21 20:17:41 +00:00
|
|
|
// Set the correct value in the root node
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = mBoundFrame->UpdateValueDisplay(true, !mEditorInitialized, aValue);
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
newEditor = mEditor; // just pretend that we have a new editor!
|
|
|
|
}
|
|
|
|
|
2014-04-14 12:33:47 +00:00
|
|
|
// Get the current value of the textfield from the content.
|
|
|
|
// Note that if we've created a new editor, mEditor is null at this stage,
|
|
|
|
// so we will get the real value from the content.
|
|
|
|
nsAutoString defaultValue;
|
|
|
|
if (aValue) {
|
|
|
|
defaultValue = *aValue;
|
|
|
|
} else {
|
|
|
|
GetValue(defaultValue, true);
|
|
|
|
}
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
if (!mEditorInitialized) {
|
|
|
|
// Now initialize the editor.
|
|
|
|
//
|
|
|
|
// NOTE: Conversion of '\n' to <BR> happens inside the
|
|
|
|
// editor's Init() call.
|
|
|
|
|
|
|
|
// Get the DOM document
|
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc = do_QueryInterface(shell->GetDocument());
|
|
|
|
if (!domdoc)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// What follows is a bit of a hack. The editor uses the public DOM APIs
|
|
|
|
// for its content manipulations, and it causes it to fail some security
|
2013-12-12 01:51:58 +00:00
|
|
|
// checks deep inside when initializing. So we explictly make it clear that
|
|
|
|
// we're native code.
|
2010-04-21 20:17:41 +00:00
|
|
|
// Note that any script that's directly trying to access our value
|
|
|
|
// has to be going through some scriptable object to do that and that
|
|
|
|
// already does the relevant security checks.
|
2014-04-15 03:27:00 +00:00
|
|
|
AutoNoJSAPI nojsapi;
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2014-04-14 12:33:47 +00:00
|
|
|
rv = newEditor->Init(domdoc, GetRootNode(), mSelCon, editorFlags,
|
|
|
|
defaultValue);
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize the controller for the editor
|
|
|
|
|
|
|
|
if (!SuppressEventHandlers(presContext)) {
|
|
|
|
nsCOMPtr<nsIControllers> controllers;
|
2010-08-05 02:40:18 +00:00
|
|
|
nsCOMPtr<nsIDOMHTMLInputElement> inputElement =
|
2010-04-21 20:17:41 +00:00
|
|
|
do_QueryInterface(mTextCtrlElement);
|
|
|
|
if (inputElement) {
|
|
|
|
rv = inputElement->GetControllers(getter_AddRefs(controllers));
|
|
|
|
} else {
|
2011-06-11 07:52:24 +00:00
|
|
|
nsCOMPtr<nsIDOMHTMLTextAreaElement> textAreaElement =
|
2010-04-21 20:17:41 +00:00
|
|
|
do_QueryInterface(mTextCtrlElement);
|
|
|
|
|
|
|
|
if (!textAreaElement)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
rv = textAreaElement->GetControllers(getter_AddRefs(controllers));
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (controllers) {
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t numControllers;
|
2011-09-29 06:19:26 +00:00
|
|
|
bool found = false;
|
2010-04-21 20:17:41 +00:00
|
|
|
rv = controllers->GetControllerCount(&numControllers);
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < numControllers; i ++) {
|
2010-04-21 20:17:41 +00:00
|
|
|
nsCOMPtr<nsIController> controller;
|
|
|
|
rv = controllers->GetControllerAt(i, getter_AddRefs(controller));
|
|
|
|
if (NS_SUCCEEDED(rv) && controller) {
|
|
|
|
nsCOMPtr<nsIControllerContext> editController =
|
|
|
|
do_QueryInterface(controller);
|
|
|
|
if (editController) {
|
|
|
|
editController->SetCommandContext(newEditor);
|
2011-10-17 14:59:28 +00:00
|
|
|
found = true;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!found)
|
|
|
|
rv = NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldInitializeEditor) {
|
|
|
|
// Initialize the plaintext editor
|
|
|
|
nsCOMPtr<nsIPlaintextEditor> textEditor(do_QueryInterface(newEditor));
|
|
|
|
if (textEditor) {
|
|
|
|
// Set up wrapping
|
|
|
|
textEditor->SetWrapColumn(GetWrapCols());
|
|
|
|
|
|
|
|
// Set max text field length
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t maxLength;
|
2010-04-21 20:17:41 +00:00
|
|
|
if (GetMaxLength(&maxLength)) {
|
|
|
|
textEditor->SetMaxTextLength(maxLength);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(mTextCtrlElement);
|
|
|
|
if (content) {
|
|
|
|
rv = newEditor->GetFlags(&editorFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Check if the readonly attribute is set.
|
|
|
|
if (content->HasAttr(kNameSpaceID_None, nsGkAtoms::readonly))
|
|
|
|
editorFlags |= nsIPlaintextEditor::eEditorReadonlyMask;
|
|
|
|
|
|
|
|
// Check if the disabled attribute is set.
|
2010-09-18 21:33:16 +00:00
|
|
|
// TODO: call IsDisabled() here!
|
2010-04-21 20:17:41 +00:00
|
|
|
if (content->HasAttr(kNameSpaceID_None, nsGkAtoms::disabled))
|
|
|
|
editorFlags |= nsIPlaintextEditor::eEditorDisabledMask;
|
|
|
|
|
|
|
|
// Disable the selection if necessary.
|
|
|
|
if (editorFlags & nsIPlaintextEditor::eEditorDisabledMask)
|
|
|
|
mSelCon->SetDisplaySelection(nsISelectionController::SELECTION_OFF);
|
|
|
|
|
|
|
|
newEditor->SetFlags(editorFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (shouldInitializeEditor) {
|
|
|
|
// Hold on to the newly created editor
|
2012-10-20 18:48:34 +00:00
|
|
|
preDestroyer.Swap(mEditor);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// If we have a default value, insert it under the div we created
|
|
|
|
// above, but be sure to use the editor so that '*' characters get
|
|
|
|
// displayed for password fields, etc. SetValue() will call the
|
|
|
|
// editor for us.
|
|
|
|
|
|
|
|
if (!defaultValue.IsEmpty()) {
|
2011-12-24 03:52:21 +00:00
|
|
|
rv = newEditor->SetFlags(editorFlags);
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Now call SetValue() which will make the necessary editor calls to set
|
|
|
|
// the default value. Make sure to turn off undo before setting the default
|
|
|
|
// value, and turn it back on afterwards. This will make sure we can't undo
|
|
|
|
// past the default value.
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = newEditor->EnableUndo(false);
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2015-01-08 01:14:01 +00:00
|
|
|
bool success = SetValue(defaultValue, false, false);
|
|
|
|
NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = newEditor->EnableUndo(true);
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv),"Transaction Manager must have failed");
|
|
|
|
|
|
|
|
// Now restore the original editor flags.
|
|
|
|
rv = newEditor->SetFlags(editorFlags);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransactionManager> transMgr;
|
|
|
|
newEditor->GetTransactionManager(getter_AddRefs(transMgr));
|
|
|
|
NS_ENSURE_TRUE(transMgr, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
transMgr->SetMaxTransactionCount(nsITextControlElement::DEFAULT_UNDO_CAP);
|
|
|
|
|
|
|
|
if (IsPasswordTextControl()) {
|
|
|
|
// Disable undo for password textfields. Note that we want to do this at
|
|
|
|
// the very end of InitEditor, so the calls to EnableUndo when setting the
|
|
|
|
// default value don't screw us up.
|
|
|
|
// Since changing the control type does a reframe, we don't have to worry
|
|
|
|
// about dynamic type changes here.
|
2011-10-17 14:59:28 +00:00
|
|
|
newEditor->EnableUndo(false);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!mEditorInitialized) {
|
|
|
|
newEditor->PostCreate();
|
2011-10-17 14:59:28 +00:00
|
|
|
mEverInited = true;
|
|
|
|
mEditorInitialized = true;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mTextListener)
|
2012-11-14 19:23:35 +00:00
|
|
|
newEditor->AddEditorObserver(mTextListener);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2010-10-13 05:34:05 +00:00
|
|
|
// Restore our selection after being bound to a new frame
|
2014-08-01 00:25:31 +00:00
|
|
|
HTMLInputElement* number = GetParentNumberControl(mBoundFrame);
|
|
|
|
if (number ? number->IsSelectionCached() : mSelectionCached) {
|
2011-05-21 08:25:27 +00:00
|
|
|
if (mRestoringSelection) // paranoia
|
|
|
|
mRestoringSelection->Revoke();
|
2011-07-29 21:31:57 +00:00
|
|
|
mRestoringSelection = new RestoreSelectionState(this, mBoundFrame);
|
2011-05-21 08:25:27 +00:00
|
|
|
if (mRestoringSelection) {
|
|
|
|
nsContentUtils::AddScriptRunner(mRestoringSelection);
|
|
|
|
}
|
2010-10-13 05:34:05 +00:00
|
|
|
}
|
|
|
|
|
2011-07-29 21:31:57 +00:00
|
|
|
// The selection cache is no longer going to be valid
|
2014-08-01 00:25:31 +00:00
|
|
|
if (number) {
|
|
|
|
number->ClearSelectionCached();
|
|
|
|
} else {
|
|
|
|
mSelectionCached = false;
|
|
|
|
}
|
2011-07-29 21:31:57 +00:00
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-08-01 00:25:31 +00:00
|
|
|
bool
|
|
|
|
nsTextEditorState::IsSelectionCached() const
|
|
|
|
{
|
|
|
|
if (mBoundFrame) {
|
|
|
|
HTMLInputElement* number = GetParentNumberControl(mBoundFrame);
|
|
|
|
if (number) {
|
|
|
|
return number->IsSelectionCached();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mSelectionCached;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTextEditorState::SelectionProperties&
|
|
|
|
nsTextEditorState::GetSelectionProperties()
|
|
|
|
{
|
|
|
|
if (mBoundFrame) {
|
|
|
|
HTMLInputElement* number = GetParentNumberControl(mBoundFrame);
|
|
|
|
if (number) {
|
|
|
|
return number->GetSelectionProperties();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return mSelectionProperties;
|
|
|
|
}
|
|
|
|
|
|
|
|
HTMLInputElement*
|
|
|
|
nsTextEditorState::GetParentNumberControl(nsFrame* aFrame) const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aFrame);
|
|
|
|
nsIContent* content = aFrame->GetContent();
|
|
|
|
MOZ_ASSERT(content);
|
|
|
|
nsIContent* parent = content->GetParent();
|
|
|
|
if (!parent) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsIContent* parentOfParent = parent->GetParent();
|
|
|
|
if (!parentOfParent) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
HTMLInputElement* input = HTMLInputElement::FromContent(parentOfParent);
|
|
|
|
if (input) {
|
|
|
|
// This function might be called during frame reconstruction as a result
|
|
|
|
// of changing the input control's type from number to something else. In
|
|
|
|
// that situation, the type of the control has changed, but its frame has
|
|
|
|
// not been reconstructed yet. So we need to check the type of the input
|
|
|
|
// control in addition to the type of the frame.
|
|
|
|
return (input->GetType() == NS_FORM_INPUT_NUMBER) ? input : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
void
|
|
|
|
nsTextEditorState::DestroyEditor()
|
|
|
|
{
|
|
|
|
// notify the editor that we are going away
|
|
|
|
if (mEditorInitialized) {
|
|
|
|
if (mTextListener)
|
2012-11-14 19:23:35 +00:00
|
|
|
mEditor->RemoveEditorObserver(mTextListener);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mEditor->PreDestroy(true);
|
|
|
|
mEditorInitialized = false;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
2014-04-27 13:33:24 +00:00
|
|
|
ClearValueCache();
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsTextEditorState::UnbindFromFrame(nsTextControlFrame* aFrame)
|
|
|
|
{
|
2012-09-14 10:00:31 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(mBoundFrame);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
// If it was, however, it should be unbounded from the same frame.
|
2010-06-11 16:24:28 +00:00
|
|
|
NS_ASSERTION(!aFrame || aFrame == mBoundFrame, "Unbinding from the wrong frame");
|
2012-12-24 04:45:57 +00:00
|
|
|
NS_ENSURE_TRUE_VOID(!aFrame || aFrame == mBoundFrame);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2014-08-19 11:54:08 +00:00
|
|
|
// If the editor is modified but nsIEditorObserver::EditAction() hasn't been
|
|
|
|
// called yet, we need to notify it here because editor may be destroyed
|
|
|
|
// before EditAction() is called if selection listener causes flushing layout.
|
2014-08-28 14:33:35 +00:00
|
|
|
bool isInEditAction = false;
|
2014-08-19 11:54:08 +00:00
|
|
|
if (mTextListener && mEditor && mEditorInitialized &&
|
2014-08-28 14:33:35 +00:00
|
|
|
NS_SUCCEEDED(mEditor->GetIsInEditAction(&isInEditAction)) &&
|
|
|
|
isInEditAction) {
|
2014-08-19 11:54:08 +00:00
|
|
|
mTextListener->EditAction();
|
|
|
|
}
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
// We need to start storing the value outside of the editor if we're not
|
|
|
|
// going to use it anymore, so retrieve it for now.
|
|
|
|
nsAutoString value;
|
2011-10-17 14:59:28 +00:00
|
|
|
GetValue(value, true);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2011-05-21 08:25:27 +00:00
|
|
|
if (mRestoringSelection) {
|
|
|
|
mRestoringSelection->Revoke();
|
2012-07-30 14:20:58 +00:00
|
|
|
mRestoringSelection = nullptr;
|
2011-05-21 08:25:27 +00:00
|
|
|
}
|
|
|
|
|
2010-10-13 05:34:05 +00:00
|
|
|
// Save our selection state if needed.
|
|
|
|
// Note that nsTextControlFrame::GetSelectionRange attempts to initialize the
|
|
|
|
// editor before grabbing the range, and because this is not an acceptable
|
|
|
|
// side effect for unbinding from a text control frame, we need to call
|
|
|
|
// GetSelectionRange before calling DestroyEditor, and only if
|
|
|
|
// mEditorInitialized indicates that we actually have an editor available.
|
|
|
|
if (mEditorInitialized) {
|
2014-08-01 00:25:31 +00:00
|
|
|
HTMLInputElement* number = GetParentNumberControl(aFrame);
|
|
|
|
if (number) {
|
|
|
|
// If we are inside a number control, cache the selection on the
|
|
|
|
// parent control, because this text editor state will be destroyed
|
|
|
|
// together with the native anonymous text control.
|
|
|
|
SelectionProperties props;
|
|
|
|
mBoundFrame->GetSelectionRange(&props.mStart, &props.mEnd,
|
|
|
|
&props.mDirection);
|
|
|
|
number->SetSelectionProperties(props);
|
|
|
|
} else {
|
|
|
|
mBoundFrame->GetSelectionRange(&mSelectionProperties.mStart,
|
|
|
|
&mSelectionProperties.mEnd,
|
|
|
|
&mSelectionProperties.mDirection);
|
|
|
|
mSelectionCached = true;
|
|
|
|
}
|
2010-10-13 05:34:05 +00:00
|
|
|
}
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
// Destroy our editor
|
|
|
|
DestroyEditor();
|
|
|
|
|
|
|
|
// Clean up the controller
|
|
|
|
if (!SuppressEventHandlers(mBoundFrame->PresContext()))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIControllers> controllers;
|
2010-08-05 02:40:18 +00:00
|
|
|
nsCOMPtr<nsIDOMHTMLInputElement> inputElement =
|
2010-04-21 20:17:41 +00:00
|
|
|
do_QueryInterface(mTextCtrlElement);
|
|
|
|
if (inputElement)
|
|
|
|
inputElement->GetControllers(getter_AddRefs(controllers));
|
|
|
|
else
|
|
|
|
{
|
2011-06-11 07:52:24 +00:00
|
|
|
nsCOMPtr<nsIDOMHTMLTextAreaElement> textAreaElement =
|
2010-04-21 20:17:41 +00:00
|
|
|
do_QueryInterface(mTextCtrlElement);
|
|
|
|
if (textAreaElement) {
|
|
|
|
textAreaElement->GetControllers(getter_AddRefs(controllers));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (controllers)
|
|
|
|
{
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t numControllers;
|
2010-04-21 20:17:41 +00:00
|
|
|
nsresult rv = controllers->GetControllerCount(&numControllers);
|
|
|
|
NS_ASSERTION((NS_SUCCEEDED(rv)), "bad result in gfx text control destructor");
|
2012-08-22 15:56:38 +00:00
|
|
|
for (uint32_t i = 0; i < numControllers; i ++)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIController> controller;
|
|
|
|
rv = controllers->GetControllerAt(i, getter_AddRefs(controller));
|
|
|
|
if (NS_SUCCEEDED(rv) && controller)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIControllerContext> editController = do_QueryInterface(controller);
|
|
|
|
if (editController)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
editController->SetCommandContext(nullptr);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mSelCon) {
|
|
|
|
if (mTextListener) {
|
|
|
|
nsRefPtr<nsISelection> domSelection;
|
|
|
|
if (NS_SUCCEEDED(mSelCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
|
|
|
|
getter_AddRefs(domSelection))) &&
|
|
|
|
domSelection) {
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(domSelection));
|
|
|
|
|
|
|
|
selPriv->RemoveSelectionListener(static_cast<nsISelectionListener*>
|
|
|
|
(mTextListener));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mSelCon->SetScrollableFrame(nullptr);
|
|
|
|
mSelCon = nullptr;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mTextListener)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
mTextListener->SetFrame(nullptr);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2013-04-06 00:44:15 +00:00
|
|
|
nsCOMPtr<EventTarget> target = do_QueryInterface(mTextCtrlElement);
|
2014-03-17 06:56:53 +00:00
|
|
|
EventListenerManager* manager = target->GetExistingListenerManager();
|
2011-06-24 02:18:02 +00:00
|
|
|
if (manager) {
|
2011-06-25 05:56:13 +00:00
|
|
|
manager->RemoveEventListenerByType(mTextListener,
|
2012-12-16 01:26:05 +00:00
|
|
|
NS_LITERAL_STRING("keydown"),
|
2014-03-17 06:56:52 +00:00
|
|
|
TrustedEventsAtSystemGroupBubble());
|
2011-06-25 05:56:13 +00:00
|
|
|
manager->RemoveEventListenerByType(mTextListener,
|
2012-12-16 01:26:05 +00:00
|
|
|
NS_LITERAL_STRING("keypress"),
|
2014-03-17 06:56:52 +00:00
|
|
|
TrustedEventsAtSystemGroupBubble());
|
2011-06-25 05:56:13 +00:00
|
|
|
manager->RemoveEventListenerByType(mTextListener,
|
2012-12-16 01:26:05 +00:00
|
|
|
NS_LITERAL_STRING("keyup"),
|
2014-03-17 06:56:52 +00:00
|
|
|
TrustedEventsAtSystemGroupBubble());
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mTextListener = nullptr;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
2012-07-30 14:20:58 +00:00
|
|
|
mBoundFrame = nullptr;
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
// Now that we don't have a frame any more, store the value in the text buffer.
|
2011-07-19 18:28:50 +00:00
|
|
|
// The only case where we don't do this is if a value transfer is in progress.
|
|
|
|
if (!mValueTransferInProgress) {
|
2015-01-08 01:14:01 +00:00
|
|
|
bool success = SetValue(value, false, false);
|
|
|
|
// TODO Find something better to do if this fails...
|
|
|
|
NS_ENSURE_TRUE_VOID(success);
|
2011-07-19 18:28:50 +00:00
|
|
|
}
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
if (mRootNode && mMutationObserver) {
|
|
|
|
mRootNode->RemoveMutationObserver(mMutationObserver);
|
2012-07-30 14:20:58 +00:00
|
|
|
mMutationObserver = nullptr;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Unbind the anonymous content from the tree.
|
|
|
|
// We actually hold a reference to the content nodes so that
|
|
|
|
// they're not actually destroyed.
|
|
|
|
nsContentUtils::DestroyAnonymousContent(&mRootNode);
|
|
|
|
nsContentUtils::DestroyAnonymousContent(&mPlaceholderDiv);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextEditorState::CreateRootNode()
|
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(!mRootNode, NS_ERROR_UNEXPECTED);
|
|
|
|
NS_ENSURE_ARG_POINTER(mBoundFrame);
|
|
|
|
|
|
|
|
nsIPresShell *shell = mBoundFrame->PresContext()->GetPresShell();
|
|
|
|
NS_ENSURE_TRUE(shell, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsIDocument *doc = shell->GetDocument();
|
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
// Now create a DIV and add it to the anonymous content child list.
|
2014-06-20 02:01:40 +00:00
|
|
|
nsRefPtr<mozilla::dom::NodeInfo> nodeInfo;
|
2012-07-30 14:20:58 +00:00
|
|
|
nodeInfo = doc->NodeInfoManager()->GetNodeInfo(nsGkAtoms::div, nullptr,
|
2011-06-14 07:56:49 +00:00
|
|
|
kNameSpaceID_XHTML,
|
|
|
|
nsIDOMNode::ELEMENT_NODE);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2010-07-23 09:49:57 +00:00
|
|
|
nsresult rv = NS_NewHTMLElement(getter_AddRefs(mRootNode), nodeInfo.forget(),
|
2010-10-25 12:17:38 +00:00
|
|
|
NOT_FROM_PARSER);
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-09-13 19:09:51 +00:00
|
|
|
if (!IsSingleLineTextControl()) {
|
|
|
|
mMutationObserver = new nsAnonDivObserver(this);
|
|
|
|
mRootNode->AddMutationObserver(mMutationObserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextEditorState::InitializeRootNode()
|
|
|
|
{
|
2010-11-16 20:45:49 +00:00
|
|
|
// Make our root node editable
|
|
|
|
mRootNode->SetFlags(NODE_IS_EDITABLE);
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
// Set the necessary classes on the text control. We use class values
|
|
|
|
// instead of a 'style' attribute so that the style comes from a user-agent
|
|
|
|
// style sheet and is still applied even if author styles are disabled.
|
|
|
|
nsAutoString classValue;
|
|
|
|
classValue.AppendLiteral("anonymous-div");
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t wrapCols = GetWrapCols();
|
2010-04-21 20:17:41 +00:00
|
|
|
if (wrapCols >= 0) {
|
|
|
|
classValue.AppendLiteral(" wrap");
|
|
|
|
}
|
|
|
|
if (!IsSingleLineTextControl()) {
|
|
|
|
// We can't just inherit the overflow because setting visible overflow will
|
|
|
|
// crash when the number of lines exceeds the height of the textarea and
|
|
|
|
// setting -moz-hidden-unscrollable overflow (NS_STYLE_OVERFLOW_CLIP)
|
|
|
|
// doesn't paint the caret for some reason.
|
2013-02-16 21:51:02 +00:00
|
|
|
const nsStyleDisplay* disp = mBoundFrame->StyleDisplay();
|
2010-04-21 20:17:41 +00:00
|
|
|
if (disp->mOverflowX != NS_STYLE_OVERFLOW_VISIBLE &&
|
|
|
|
disp->mOverflowX != NS_STYLE_OVERFLOW_CLIP) {
|
|
|
|
classValue.AppendLiteral(" inherit-overflow");
|
|
|
|
}
|
|
|
|
}
|
2011-09-13 19:09:51 +00:00
|
|
|
nsresult rv = mRootNode->SetAttr(kNameSpaceID_None, nsGkAtoms::_class,
|
2011-10-17 14:59:28 +00:00
|
|
|
classValue, false);
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return mBoundFrame->UpdateValueDisplay(false);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsTextEditorState::CreatePlaceholderNode()
|
|
|
|
{
|
2010-10-19 13:11:07 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(mTextCtrlElement);
|
|
|
|
if (content) {
|
|
|
|
nsAutoString placeholderTxt;
|
|
|
|
content->GetAttr(kNameSpaceID_None, nsGkAtoms::placeholder,
|
|
|
|
placeholderTxt);
|
|
|
|
nsContentUtils::RemoveNewlines(placeholderTxt);
|
|
|
|
NS_ASSERTION(!placeholderTxt.IsEmpty(), "CreatePlaceholderNode() shouldn't \
|
|
|
|
be called if @placeholder is the empty string when trimmed from line breaks");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif // DEBUG
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_ENSURE_TRUE(!mPlaceholderDiv, NS_ERROR_UNEXPECTED);
|
|
|
|
NS_ENSURE_ARG_POINTER(mBoundFrame);
|
|
|
|
|
|
|
|
nsIPresShell *shell = mBoundFrame->PresContext()->GetPresShell();
|
|
|
|
NS_ENSURE_TRUE(shell, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsIDocument *doc = shell->GetDocument();
|
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsNodeInfoManager* pNodeInfoManager = doc->NodeInfoManager();
|
|
|
|
NS_ENSURE_TRUE(pNodeInfoManager, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// Create a DIV for the placeholder
|
|
|
|
// and add it to the anonymous content child list
|
2014-06-20 02:01:40 +00:00
|
|
|
nsRefPtr<mozilla::dom::NodeInfo> nodeInfo;
|
2012-07-30 14:20:58 +00:00
|
|
|
nodeInfo = pNodeInfoManager->GetNodeInfo(nsGkAtoms::div, nullptr,
|
2011-06-14 07:56:49 +00:00
|
|
|
kNameSpaceID_XHTML,
|
|
|
|
nsIDOMNode::ELEMENT_NODE);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2010-07-23 09:49:57 +00:00
|
|
|
rv = NS_NewHTMLElement(getter_AddRefs(mPlaceholderDiv), nodeInfo.forget(),
|
2010-10-25 12:17:38 +00:00
|
|
|
NOT_FROM_PARSER);
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// Create the text node for the placeholder text before doing anything else
|
2013-04-04 12:01:08 +00:00
|
|
|
nsRefPtr<nsTextNode> placeholderText = new nsTextNode(pNodeInfoManager);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
rv = mPlaceholderDiv->AppendChildTo(placeholderText, false);
|
2010-04-21 20:17:41 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
// initialize the text
|
2011-10-17 14:59:28 +00:00
|
|
|
UpdatePlaceholderText(false);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 06:19:26 +00:00
|
|
|
bool
|
2012-08-22 15:56:38 +00:00
|
|
|
nsTextEditorState::GetMaxLength(int32_t* aMaxLength)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(mTextCtrlElement);
|
2012-09-26 14:17:51 +00:00
|
|
|
nsGenericHTMLElement* element =
|
|
|
|
nsGenericHTMLElement::FromContentOrNull(content);
|
2011-10-17 14:59:28 +00:00
|
|
|
NS_ENSURE_TRUE(element, false);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
const nsAttrValue* attr = element->GetParsedAttr(nsGkAtoms::maxlength);
|
|
|
|
if (attr && attr->Type() == nsAttrValue::eInteger) {
|
|
|
|
*aMaxLength = attr->GetIntegerValue();
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return true;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
return false;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextEditorState::GetValue(nsAString& aValue, bool aIgnoreWrap) const
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
2010-11-22 08:12:56 +00:00
|
|
|
if (mEditor && mBoundFrame && (mEditorInitialized || !IsSingleLineTextControl())) {
|
2011-09-29 06:19:26 +00:00
|
|
|
bool canCache = aIgnoreWrap && !IsSingleLineTextControl();
|
2010-04-21 20:17:41 +00:00
|
|
|
if (canCache && !mCachedValue.IsEmpty()) {
|
|
|
|
aValue = mCachedValue;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aValue.Truncate(); // initialize out param
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t flags = (nsIDocumentEncoder::OutputLFLineBreak |
|
2010-04-21 20:17:41 +00:00
|
|
|
nsIDocumentEncoder::OutputPreformatted |
|
|
|
|
nsIDocumentEncoder::OutputPersistNBSP);
|
|
|
|
|
|
|
|
if (IsPlainTextControl())
|
|
|
|
{
|
|
|
|
flags |= nsIDocumentEncoder::OutputBodyOnly;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aIgnoreWrap) {
|
|
|
|
nsITextControlElement::nsHTMLTextWrap wrapProp;
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(mTextCtrlElement);
|
|
|
|
if (content &&
|
|
|
|
nsITextControlElement::GetWrapPropertyEnum(content, wrapProp) &&
|
|
|
|
wrapProp == nsITextControlElement::eHTMLTextWrap_Hard) {
|
|
|
|
flags |= nsIDocumentEncoder::OutputWrap;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// What follows is a bit of a hack. The problem is that we could be in
|
|
|
|
// this method because we're being destroyed for whatever reason while
|
|
|
|
// script is executing. If that happens, editor will run with the
|
|
|
|
// privileges of the executing script, which means it may not be able to
|
|
|
|
// access its own DOM nodes! Let's try to deal with that by pushing a null
|
|
|
|
// JSContext on the JSContext stack to make it clear that we're native
|
|
|
|
// code. Note that any script that's directly trying to access our value
|
|
|
|
// has to be going through some scriptable object to do that and that
|
|
|
|
// already does the relevant security checks.
|
|
|
|
// XXXbz if we could just get the textContent of our anonymous content (eg
|
|
|
|
// if plaintext editor didn't create <br> nodes all over), we wouldn't need
|
|
|
|
// this.
|
2014-04-15 03:27:00 +00:00
|
|
|
{ /* Scope for AutoNoJSAPI. */
|
|
|
|
AutoNoJSAPI nojsapi;
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
mEditor->OutputToString(NS_LITERAL_STRING("text/plain"), flags,
|
|
|
|
aValue);
|
|
|
|
}
|
|
|
|
if (canCache) {
|
|
|
|
mCachedValue = aValue;
|
|
|
|
} else {
|
|
|
|
mCachedValue.Truncate();
|
|
|
|
}
|
|
|
|
} else {
|
2013-03-25 19:35:55 +00:00
|
|
|
if (!mTextCtrlElement->ValueChanged() || !mValue) {
|
|
|
|
mTextCtrlElement->GetDefaultValueFromContent(aValue);
|
|
|
|
} else {
|
2010-04-21 20:17:41 +00:00
|
|
|
aValue = NS_ConvertUTF8toUTF16(*mValue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-08 01:14:01 +00:00
|
|
|
bool
|
2012-05-31 16:01:30 +00:00
|
|
|
nsTextEditorState::SetValue(const nsAString& aValue, bool aUserInput,
|
|
|
|
bool aSetValueChanged)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
if (mEditor && mBoundFrame) {
|
|
|
|
// The InsertText call below might flush pending notifications, which
|
|
|
|
// could lead into a scheduled PrepareEditor to be called. That will
|
|
|
|
// lead to crashes (or worse) because we'd be initializing the editor
|
|
|
|
// before InsertText returns. This script blocker makes sure that
|
|
|
|
// PrepareEditor cannot be called prematurely.
|
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
|
|
|
|
2010-11-26 20:48:45 +00:00
|
|
|
#ifdef DEBUG
|
|
|
|
if (IsSingleLineTextControl()) {
|
|
|
|
NS_ASSERTION(mEditorInitialized || mInitializing,
|
|
|
|
"We should never try to use the editor if we're not initialized unless we're being initialized");
|
|
|
|
}
|
|
|
|
#endif
|
2010-11-22 08:12:56 +00:00
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
nsAutoString currentValue;
|
2014-04-14 12:33:47 +00:00
|
|
|
mBoundFrame->GetText(currentValue);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
nsWeakFrame weakFrame(mBoundFrame);
|
|
|
|
|
|
|
|
// this is necessary to avoid infinite recursion
|
|
|
|
if (!currentValue.Equals(aValue))
|
|
|
|
{
|
2013-08-28 19:06:00 +00:00
|
|
|
ValueSetter valueSetter(mEditor);
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
// \r is an illegal character in the dom, but people use them,
|
|
|
|
// so convert windows and mac platform linebreaks to \n:
|
|
|
|
// Unfortunately aValue is declared const, so we have to copy
|
|
|
|
// in order to do this substitution.
|
2015-01-08 01:14:01 +00:00
|
|
|
nsString newValue;
|
|
|
|
if (!newValue.Assign(aValue, fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-01-04 15:02:17 +00:00
|
|
|
if (aValue.FindChar(char16_t('\r')) != -1) {
|
2015-01-08 01:14:01 +00:00
|
|
|
if (!nsContentUtils::PlatformToDOMLineBreaks(newValue, fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc;
|
|
|
|
mEditor->GetDocument(getter_AddRefs(domDoc));
|
|
|
|
if (!domDoc) {
|
|
|
|
NS_WARNING("Why don't we have a document?");
|
2015-01-08 01:14:01 +00:00
|
|
|
return true;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Time to mess with our security context... See comments in GetValue()
|
|
|
|
// for why this is needed. Note that we have to do this up here, because
|
|
|
|
// otherwise SelectAll() will fail.
|
2013-12-12 01:51:58 +00:00
|
|
|
{
|
2014-04-15 03:27:00 +00:00
|
|
|
AutoNoJSAPI nojsapi;
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> domSel;
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv;
|
|
|
|
mSelCon->GetSelection(nsISelectionController::SELECTION_NORMAL,
|
|
|
|
getter_AddRefs(domSel));
|
|
|
|
if (domSel)
|
|
|
|
{
|
|
|
|
selPriv = do_QueryInterface(domSel);
|
|
|
|
if (selPriv)
|
|
|
|
selPriv->StartBatchChanges();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelectionController> kungFuDeathGrip = mSelCon.get();
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t currentLength = currentValue.Length();
|
|
|
|
uint32_t newlength = newValue.Length();
|
2010-04-21 20:17:41 +00:00
|
|
|
if (!currentLength ||
|
|
|
|
!StringBeginsWith(newValue, currentValue)) {
|
|
|
|
// Replace the whole text.
|
|
|
|
currentLength = 0;
|
|
|
|
mSelCon->SelectAll();
|
|
|
|
} else {
|
|
|
|
// Collapse selection to the end so that we can append data.
|
2011-10-17 14:59:28 +00:00
|
|
|
mBoundFrame->SelectAllOrCollapseToEndOfText(false);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
const nsAString& insertValue =
|
|
|
|
StringTail(newValue, newlength - currentLength);
|
|
|
|
nsCOMPtr<nsIPlaintextEditor> plaintextEditor = do_QueryInterface(mEditor);
|
|
|
|
if (!plaintextEditor || !weakFrame.IsAlive()) {
|
|
|
|
NS_WARNING("Somehow not a plaintext editor?");
|
2015-01-08 01:14:01 +00:00
|
|
|
return true;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
valueSetter.Init();
|
|
|
|
|
|
|
|
// get the flags, remove readonly and disabled, set the value,
|
|
|
|
// restore flags
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t flags, savedFlags;
|
2010-04-21 20:17:41 +00:00
|
|
|
mEditor->GetFlags(&savedFlags);
|
|
|
|
flags = savedFlags;
|
|
|
|
flags &= ~(nsIPlaintextEditor::eEditorDisabledMask);
|
|
|
|
flags &= ~(nsIPlaintextEditor::eEditorReadonlyMask);
|
|
|
|
flags |= nsIPlaintextEditor::eEditorDontEchoPassword;
|
|
|
|
mEditor->SetFlags(flags);
|
|
|
|
|
2011-10-17 14:59:28 +00:00
|
|
|
mTextListener->SettingValue(true);
|
2012-05-31 16:01:30 +00:00
|
|
|
mTextListener->SetValueChanged(aSetValueChanged);
|
2010-10-06 20:31:09 +00:00
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
// Also don't enforce max-length here
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t savedMaxLength;
|
2010-04-21 20:17:41 +00:00
|
|
|
plaintextEditor->GetMaxTextLength(&savedMaxLength);
|
|
|
|
plaintextEditor->SetMaxTextLength(-1);
|
|
|
|
|
|
|
|
if (insertValue.IsEmpty()) {
|
2012-05-10 14:54:33 +00:00
|
|
|
mEditor->DeleteSelection(nsIEditor::eNone, nsIEditor::eStrip);
|
2010-04-21 20:17:41 +00:00
|
|
|
} else {
|
|
|
|
plaintextEditor->InsertText(insertValue);
|
|
|
|
}
|
2010-10-06 20:31:09 +00:00
|
|
|
|
2012-05-31 16:01:30 +00:00
|
|
|
mTextListener->SetValueChanged(true);
|
2011-10-17 14:59:28 +00:00
|
|
|
mTextListener->SettingValue(false);
|
2010-10-06 20:31:09 +00:00
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
if (!weakFrame.IsAlive()) {
|
2010-06-28 18:04:37 +00:00
|
|
|
// If the frame was destroyed because of a flush somewhere inside
|
|
|
|
// InsertText, mBoundFrame here will be false. But it's also possible
|
|
|
|
// for the frame to go away because of another reason (such as deleting
|
|
|
|
// the existing selection -- see bug 574558), in which case we don't
|
|
|
|
// need to reset the value here.
|
|
|
|
if (!mBoundFrame) {
|
2015-01-08 01:14:01 +00:00
|
|
|
return SetValue(newValue, false, aSetValueChanged);
|
2010-06-28 18:04:37 +00:00
|
|
|
}
|
2015-01-08 01:14:01 +00:00
|
|
|
return true;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!IsSingleLineTextControl()) {
|
2015-01-08 01:14:01 +00:00
|
|
|
if (!mCachedValue.Assign(newValue, fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
plaintextEditor->SetMaxTextLength(savedMaxLength);
|
|
|
|
mEditor->SetFlags(savedFlags);
|
|
|
|
if (selPriv)
|
|
|
|
selPriv->EndBatchChanges();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!mValue) {
|
|
|
|
mValue = new nsCString;
|
|
|
|
}
|
2015-01-08 01:14:01 +00:00
|
|
|
nsString value;
|
|
|
|
if (!value.Assign(aValue, fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!nsContentUtils::PlatformToDOMLineBreaks(value, fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (!CopyUTF16toUTF8(value, *mValue, fallible)) {
|
|
|
|
return false;
|
|
|
|
}
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
// Update the frame display if needed
|
|
|
|
if (mBoundFrame) {
|
2011-10-17 14:59:28 +00:00
|
|
|
mBoundFrame->UpdateValueDisplay(true);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we've reached the point where the root node has been created, we
|
|
|
|
// can assume that it's safe to notify.
|
|
|
|
ValueWasChanged(!!mRootNode);
|
2010-08-21 17:52:57 +00:00
|
|
|
|
|
|
|
mTextCtrlElement->OnValueChanged(!!mRootNode);
|
2015-01-08 01:14:01 +00:00
|
|
|
|
|
|
|
return true;
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsTextEditorState::InitializeKeyboardEventListeners()
|
|
|
|
{
|
|
|
|
//register key listeners
|
2013-04-06 00:44:15 +00:00
|
|
|
nsCOMPtr<EventTarget> target = do_QueryInterface(mTextCtrlElement);
|
2014-03-17 06:56:53 +00:00
|
|
|
EventListenerManager* manager = target->GetOrCreateListenerManager();
|
2011-06-24 02:18:02 +00:00
|
|
|
if (manager) {
|
2011-06-25 05:56:13 +00:00
|
|
|
manager->AddEventListenerByType(mTextListener,
|
|
|
|
NS_LITERAL_STRING("keydown"),
|
2014-03-17 06:56:52 +00:00
|
|
|
TrustedEventsAtSystemGroupBubble());
|
2011-06-25 05:56:13 +00:00
|
|
|
manager->AddEventListenerByType(mTextListener,
|
|
|
|
NS_LITERAL_STRING("keypress"),
|
2014-03-17 06:56:52 +00:00
|
|
|
TrustedEventsAtSystemGroupBubble());
|
2011-06-25 05:56:13 +00:00
|
|
|
manager->AddEventListenerByType(mTextListener,
|
|
|
|
NS_LITERAL_STRING("keyup"),
|
2014-03-17 06:56:52 +00:00
|
|
|
TrustedEventsAtSystemGroupBubble());
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
2011-08-24 20:54:30 +00:00
|
|
|
mSelCon->SetScrollableFrame(do_QueryFrame(mBoundFrame->GetFirstPrincipalChild()));
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextEditorState::ValueWasChanged(bool aNotify)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
2012-11-09 10:31:34 +00:00
|
|
|
UpdatePlaceholderVisibility(aNotify);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-09-29 06:19:26 +00:00
|
|
|
nsTextEditorState::UpdatePlaceholderText(bool aNotify)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
2010-10-19 13:11:07 +00:00
|
|
|
NS_ASSERTION(mPlaceholderDiv, "This function should not be called if "
|
|
|
|
"mPlaceholderDiv isn't set");
|
|
|
|
|
2010-08-06 02:13:07 +00:00
|
|
|
// If we don't have a placeholder div, there's nothing to do.
|
|
|
|
if (!mPlaceholderDiv)
|
|
|
|
return;
|
|
|
|
|
2010-04-21 20:17:41 +00:00
|
|
|
nsAutoString placeholderValue;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(mTextCtrlElement);
|
|
|
|
content->GetAttr(kNameSpaceID_None, nsGkAtoms::placeholder, placeholderValue);
|
|
|
|
nsContentUtils::RemoveNewlines(placeholderValue);
|
2011-09-27 07:54:58 +00:00
|
|
|
NS_ASSERTION(mPlaceholderDiv->GetFirstChild(), "placeholder div has no child");
|
|
|
|
mPlaceholderDiv->GetFirstChild()->SetText(placeholderValue, aNotify);
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-11-09 10:31:34 +00:00
|
|
|
nsTextEditorState::UpdatePlaceholderVisibility(bool aNotify)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
2012-11-09 10:31:34 +00:00
|
|
|
nsAutoString value;
|
|
|
|
GetValue(value, true);
|
|
|
|
|
|
|
|
mPlaceholderVisibility = value.IsEmpty();
|
2010-04-21 20:17:41 +00:00
|
|
|
|
2012-11-09 10:32:58 +00:00
|
|
|
if (mPlaceholderVisibility &&
|
|
|
|
!Preferences::GetBool("dom.placeholder.show_on_focus", true)) {
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(mTextCtrlElement);
|
|
|
|
mPlaceholderVisibility = !nsContentUtils::IsFocusedContent(content);
|
|
|
|
}
|
|
|
|
|
2012-11-09 10:22:29 +00:00
|
|
|
if (mBoundFrame && aNotify) {
|
|
|
|
mBoundFrame->InvalidateFrame();
|
|
|
|
}
|
2010-04-21 20:17:41 +00:00
|
|
|
}
|
|
|
|
|
2011-04-12 02:03:50 +00:00
|
|
|
void
|
|
|
|
nsTextEditorState::HideSelectionIfBlurred()
|
|
|
|
{
|
2015-02-09 22:34:50 +00:00
|
|
|
MOZ_ASSERT(mSelCon, "Should have a selection controller if we have a frame!");
|
2011-04-12 02:03:50 +00:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(mTextCtrlElement);
|
|
|
|
if (!nsContentUtils::IsFocusedContent(content)) {
|
|
|
|
mSelCon->SetDisplaySelection(nsISelectionController::SELECTION_HIDDEN);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(nsAnonDivObserver, nsIMutationObserver)
|
2010-04-21 20:17:41 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
nsAnonDivObserver::CharacterDataChanged(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
|
|
|
mTextEditorState->ClearValueCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsAnonDivObserver::ContentAppended(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aFirstNewContent,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t /* unused */)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
mTextEditorState->ClearValueCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsAnonDivObserver::ContentInserted(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t /* unused */)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
mTextEditorState->ClearValueCache();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsAnonDivObserver::ContentRemoved(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t aIndexInContainer,
|
2010-07-21 22:05:17 +00:00
|
|
|
nsIContent* aPreviousSibling)
|
2010-04-21 20:17:41 +00:00
|
|
|
{
|
|
|
|
mTextEditorState->ClearValueCache();
|
|
|
|
}
|