2001-09-25 22:53:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
2004-04-18 14:21:17 +00:00
|
|
|
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
|
2001-01-28 20:13:07 +00:00
|
|
|
*
|
2004-04-18 14:21:17 +00:00
|
|
|
* The contents of this file are subject to the Mozilla Public License Version
|
|
|
|
* 1.1 (the "License"); you may not use this file except in compliance with
|
|
|
|
* the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/MPL/
|
2001-01-28 20:13:07 +00:00
|
|
|
*
|
2001-09-25 22:53:13 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
2001-01-28 20:13:07 +00:00
|
|
|
*
|
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2004-04-18 14:21:17 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
2001-09-25 22:53:13 +00:00
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
2001-01-28 20:13:07 +00:00
|
|
|
*
|
2001-09-25 22:53:13 +00:00
|
|
|
* Contributor(s):
|
2002-01-09 13:51:37 +00:00
|
|
|
* Daniel Glazman <glazman@netscape.com>
|
2005-10-17 00:54:02 +00:00
|
|
|
* Mats Palmgren <mats.palmgren@bredband.net>
|
2001-09-25 22:53:13 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
2004-04-18 14:21:17 +00:00
|
|
|
* either of the GNU General Public License Version 2 or later (the "GPL"),
|
|
|
|
* or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
2001-09-25 22:53:13 +00:00
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
2004-04-18 14:21:17 +00:00
|
|
|
* use your version of this file under the terms of the MPL, indicate your
|
2001-09-25 22:53:13 +00:00
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
2004-04-18 14:21:17 +00:00
|
|
|
* the terms of any one of the MPL, the GPL or the LGPL.
|
2001-09-25 22:53:13 +00:00
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
#include "nsPlaintextEditor.h"
|
|
|
|
#include "nsICaret.h"
|
2001-04-05 23:48:01 +00:00
|
|
|
#include "nsTextEditUtils.h"
|
2001-01-28 20:13:07 +00:00
|
|
|
#include "nsTextEditRules.h"
|
|
|
|
#include "nsEditorEventListeners.h"
|
|
|
|
#include "nsIEditActionListener.h"
|
|
|
|
#include "nsIDOMNodeList.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDocument.h"
|
2007-05-14 09:11:38 +00:00
|
|
|
#include "nsIDOMEventTarget.h"
|
2002-11-09 00:25:25 +00:00
|
|
|
#include "nsIDOM3EventTarget.h"
|
2001-01-28 20:13:07 +00:00
|
|
|
#include "nsIDOMKeyEvent.h"
|
|
|
|
#include "nsIDOMMouseListener.h"
|
|
|
|
#include "nsISelection.h"
|
|
|
|
#include "nsISelectionPrivate.h"
|
|
|
|
#include "nsISelectionController.h"
|
2001-07-16 02:40:48 +00:00
|
|
|
#include "nsGUIEvent.h"
|
2002-11-09 00:25:25 +00:00
|
|
|
#include "nsIDOMEventGroup.h"
|
2003-03-15 01:04:32 +00:00
|
|
|
#include "nsCRT.h"
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
#include "nsIEnumerator.h"
|
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIContentIterator.h"
|
|
|
|
#include "nsIDOMRange.h"
|
|
|
|
#include "nsISupportsArray.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIDocumentEncoder.h"
|
|
|
|
#include "nsIPresShell.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
2003-07-28 13:13:50 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
// Misc
|
2003-07-28 13:13:50 +00:00
|
|
|
#include "nsEditorUtils.h" // nsAutoEditBatch, nsAutoRules
|
2002-11-23 01:26:58 +00:00
|
|
|
#include "nsIPrefBranch.h"
|
|
|
|
#include "nsIPrefService.h"
|
2002-03-19 06:46:56 +00:00
|
|
|
#include "nsUnicharUtils.h"
|
2006-03-14 17:12:41 +00:00
|
|
|
#include "nsContentCID.h"
|
2001-04-05 23:48:01 +00:00
|
|
|
#include "nsAOLCiter.h"
|
|
|
|
#include "nsInternetCiter.h"
|
2007-07-26 04:14:33 +00:00
|
|
|
#include "nsEventDispatcher.h"
|
2001-04-05 23:48:01 +00:00
|
|
|
|
|
|
|
// Drag & Drop, Clipboard
|
|
|
|
#include "nsIClipboard.h"
|
|
|
|
#include "nsITransferable.h"
|
2007-07-26 04:14:33 +00:00
|
|
|
#include "nsCopySupport.h"
|
2001-04-05 23:48:01 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// prototype for rules creation shortcut
|
|
|
|
nsresult NS_NewTextEditRules(nsIEditRules** aInstancePtrResult);
|
|
|
|
|
|
|
|
nsPlaintextEditor::nsPlaintextEditor()
|
|
|
|
: nsEditor()
|
|
|
|
, mIgnoreSpuriousDragEvent(PR_FALSE)
|
|
|
|
, mRules(nsnull)
|
2002-04-16 23:11:33 +00:00
|
|
|
, mWrapToWindow(PR_FALSE)
|
|
|
|
, mWrapColumn(0)
|
2001-01-28 20:13:07 +00:00
|
|
|
, mMaxTextLength(-1)
|
|
|
|
, mInitTriggerCounter(0)
|
2005-12-20 20:12:54 +00:00
|
|
|
, mNewlineHandling(nsIPlaintextEditor::eNewlinesPasteToFirst)
|
2006-10-10 16:50:50 +00:00
|
|
|
#ifdef XP_WIN
|
|
|
|
, mCaretStyle(1)
|
|
|
|
#else
|
|
|
|
, mCaretStyle(0)
|
|
|
|
#endif
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPlaintextEditor::~nsPlaintextEditor()
|
|
|
|
{
|
|
|
|
// remove the rules as an action listener. Else we get a bad ownership loop later on.
|
|
|
|
// it's ok if the rules aren't a listener; we ignore the error.
|
|
|
|
nsCOMPtr<nsIEditActionListener> mListener = do_QueryInterface(mRules);
|
|
|
|
RemoveEditActionListener(mListener);
|
|
|
|
|
2002-11-12 23:30:29 +00:00
|
|
|
// Remove event listeners. Note that if we had an HTML editor,
|
|
|
|
// it installed its own instead of these
|
2005-12-11 09:26:29 +00:00
|
|
|
RemoveEventListeners();
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ADDREF_INHERITED(nsPlaintextEditor, nsEditor)
|
|
|
|
NS_IMPL_RELEASE_INHERITED(nsPlaintextEditor, nsEditor)
|
|
|
|
|
2003-06-25 19:21:59 +00:00
|
|
|
NS_INTERFACE_MAP_BEGIN(nsPlaintextEditor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIPlaintextEditor)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIEditorMailSupport)
|
|
|
|
NS_INTERFACE_MAP_END_INHERITING(nsEditor)
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPlaintextEditor::Init(nsIDOMDocument *aDoc,
|
|
|
|
nsIPresShell *aPresShell, nsIContent *aRoot, nsISelectionController *aSelCon, PRUint32 aFlags)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aDoc && aPresShell, "bad arg");
|
|
|
|
if (!aDoc || !aPresShell)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult res = NS_OK, rulesRes = NS_OK;
|
|
|
|
|
|
|
|
if (1)
|
|
|
|
{
|
|
|
|
// block to scope nsAutoEditInitRulesTrigger
|
|
|
|
nsAutoEditInitRulesTrigger rulesTrigger(this, rulesRes);
|
|
|
|
|
|
|
|
// Init the base editor
|
|
|
|
res = nsEditor::Init(aDoc, aPresShell, aRoot, aSelCon, aFlags);
|
|
|
|
}
|
2005-12-20 20:12:54 +00:00
|
|
|
|
2006-10-10 16:50:50 +00:00
|
|
|
// check the "single line editor newline handling"
|
|
|
|
// and "caret behaviour in selection" prefs
|
2005-12-20 20:12:54 +00:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch(do_GetService(NS_PREFSERVICE_CONTRACTID));
|
|
|
|
if (prefBranch)
|
2006-10-10 16:50:50 +00:00
|
|
|
{
|
2005-12-20 20:12:54 +00:00
|
|
|
prefBranch->GetIntPref("editor.singleLine.pasteNewlines",
|
|
|
|
&mNewlineHandling);
|
2006-10-10 16:50:50 +00:00
|
|
|
prefBranch->GetIntPref("layout.selection.caret_style", &mCaretStyle);
|
|
|
|
#ifdef XP_WIN
|
|
|
|
if (mCaretStyle == 0)
|
|
|
|
mCaretStyle = 1;
|
|
|
|
#endif
|
|
|
|
}
|
2005-12-20 20:12:54 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(rulesRes)) return rulesRes;
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsPlaintextEditor::BeginEditorInit()
|
|
|
|
{
|
|
|
|
mInitTriggerCounter++;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPlaintextEditor::EndEditorInit()
|
|
|
|
{
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
NS_PRECONDITION(mInitTriggerCounter > 0, "ended editor init before we began?");
|
|
|
|
mInitTriggerCounter--;
|
|
|
|
if (mInitTriggerCounter == 0)
|
|
|
|
{
|
|
|
|
res = InitRules();
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
EnableUndo(PR_TRUE);
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2003-06-17 16:40:34 +00:00
|
|
|
nsPlaintextEditor::SetDocumentCharacterSet(const nsACString & characterSet)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
nsresult result;
|
|
|
|
|
|
|
|
result = nsEditor::SetDocumentCharacterSet(characterSet);
|
|
|
|
|
|
|
|
// update META charset tag
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
nsCOMPtr<nsIDOMDocument>domdoc;
|
|
|
|
result = GetDocument(getter_AddRefs(domdoc));
|
|
|
|
if (NS_SUCCEEDED(result) && domdoc) {
|
|
|
|
nsCOMPtr<nsIDOMNodeList>metaList;
|
|
|
|
nsCOMPtr<nsIDOMElement>metaElement;
|
|
|
|
PRBool newMetaCharset = PR_TRUE;
|
|
|
|
|
|
|
|
// get a list of META tags
|
|
|
|
result = domdoc->GetElementsByTagName(NS_LITERAL_STRING("meta"), getter_AddRefs(metaList));
|
|
|
|
if (NS_SUCCEEDED(result) && metaList) {
|
|
|
|
PRUint32 listLength = 0;
|
|
|
|
(void) metaList->GetLength(&listLength);
|
|
|
|
|
2003-07-28 13:13:50 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>metaNode;
|
2001-01-28 20:13:07 +00:00
|
|
|
for (PRUint32 i = 0; i < listLength; i++) {
|
|
|
|
metaList->Item(i, getter_AddRefs(metaNode));
|
|
|
|
if (!metaNode) continue;
|
|
|
|
metaElement = do_QueryInterface(metaNode);
|
|
|
|
if (!metaElement) continue;
|
|
|
|
|
2002-03-19 06:46:56 +00:00
|
|
|
nsAutoString currentValue;
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(metaElement->GetAttribute(NS_LITERAL_STRING("http-equiv"), currentValue))) continue;
|
|
|
|
|
2002-03-19 06:46:56 +00:00
|
|
|
if (FindInReadable(NS_LITERAL_STRING("content-type"),
|
|
|
|
currentValue,
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
2001-12-17 15:23:51 +00:00
|
|
|
NS_NAMED_LITERAL_STRING(content, "content");
|
2001-11-14 01:33:42 +00:00
|
|
|
if (NS_FAILED(metaElement->GetAttribute(content, currentValue))) continue;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2002-03-19 06:46:56 +00:00
|
|
|
NS_NAMED_LITERAL_STRING(charsetEquals, "charset=");
|
|
|
|
nsAString::const_iterator originalStart, start, end;
|
2002-03-19 19:52:45 +00:00
|
|
|
originalStart = currentValue.BeginReading(start);
|
2002-03-19 06:46:56 +00:00
|
|
|
currentValue.EndReading(end);
|
|
|
|
if (FindInReadable(charsetEquals, start, end,
|
|
|
|
nsCaseInsensitiveStringComparator())) {
|
|
|
|
|
|
|
|
// set attribute to <original prefix> charset=text/html
|
|
|
|
result = nsEditor::SetAttribute(metaElement, content,
|
|
|
|
Substring(originalStart, start) +
|
2006-02-03 14:18:39 +00:00
|
|
|
charsetEquals + NS_ConvertASCIItoUTF16(characterSet));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_SUCCEEDED(result))
|
|
|
|
newMetaCharset = PR_FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newMetaCharset) {
|
|
|
|
nsCOMPtr<nsIDOMNodeList>headList;
|
|
|
|
result = domdoc->GetElementsByTagName(NS_LITERAL_STRING("head"),getter_AddRefs(headList));
|
|
|
|
if (NS_SUCCEEDED(result) && headList) {
|
2001-12-17 15:23:51 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>headNode;
|
2001-01-28 20:13:07 +00:00
|
|
|
headList->Item(0, getter_AddRefs(headNode));
|
|
|
|
if (headNode) {
|
2001-12-17 15:23:51 +00:00
|
|
|
nsCOMPtr<nsIDOMNode>resultNode;
|
2001-01-28 20:13:07 +00:00
|
|
|
// Create a new meta charset tag
|
2001-11-14 01:33:42 +00:00
|
|
|
result = CreateNode(NS_LITERAL_STRING("meta"), headNode, 0, getter_AddRefs(resultNode));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(result))
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Set attributes to the created element
|
2003-05-23 21:34:47 +00:00
|
|
|
if (resultNode && !characterSet.IsEmpty()) {
|
2001-01-28 20:13:07 +00:00
|
|
|
metaElement = do_QueryInterface(resultNode);
|
|
|
|
if (metaElement) {
|
|
|
|
// not undoable, undo should undo CreateNode
|
|
|
|
result = metaElement->SetAttribute(NS_LITERAL_STRING("http-equiv"), NS_LITERAL_STRING("Content-Type"));
|
|
|
|
if (NS_SUCCEEDED(result)) {
|
|
|
|
// not undoable, undo should undo CreateNode
|
2002-03-19 06:46:56 +00:00
|
|
|
result = metaElement->SetAttribute(NS_LITERAL_STRING("content"),
|
2006-02-03 14:18:39 +00:00
|
|
|
NS_LITERAL_STRING("text/html;charset=") + NS_ConvertASCIItoUTF16(characterSet));
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2005-03-24 19:00:01 +00:00
|
|
|
nsresult
|
|
|
|
nsPlaintextEditor::CreateEventListeners()
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2005-03-24 19:00:01 +00:00
|
|
|
nsresult rv = NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2005-03-24 19:00:01 +00:00
|
|
|
if (!mMouseListenerP) {
|
|
|
|
// get a mouse listener
|
|
|
|
rv |= NS_NewEditorMouseListener(getter_AddRefs(mMouseListenerP), this);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2005-03-24 19:00:01 +00:00
|
|
|
if (!mKeyListenerP) {
|
|
|
|
// get a key listener
|
|
|
|
rv |= NS_NewEditorKeyListener(getter_AddRefs(mKeyListenerP), this);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2005-03-24 19:00:01 +00:00
|
|
|
if (!mTextListenerP) {
|
|
|
|
// get a text listener
|
|
|
|
rv |= NS_NewEditorTextListener(getter_AddRefs(mTextListenerP), this);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2005-03-24 19:00:01 +00:00
|
|
|
if (!mCompositionListenerP) {
|
|
|
|
// get a composition listener
|
|
|
|
rv |=
|
|
|
|
NS_NewEditorCompositionListener(getter_AddRefs(mCompositionListenerP),
|
|
|
|
this);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2007-07-10 15:38:30 +00:00
|
|
|
nsCOMPtr<nsIPresShell> presShell = do_QueryReferent(mPresShellWeak);
|
2005-03-24 19:00:01 +00:00
|
|
|
if (!mDragListenerP) {
|
|
|
|
// get a drag listener
|
|
|
|
rv |= NS_NewEditorDragListener(getter_AddRefs(mDragListenerP), presShell,
|
|
|
|
this);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2005-03-24 19:00:01 +00:00
|
|
|
if (!mFocusListenerP) {
|
|
|
|
// get a focus listener
|
2007-07-10 15:38:30 +00:00
|
|
|
rv |= NS_NewEditorFocusListener(getter_AddRefs(mFocusListenerP),
|
|
|
|
this, presShell);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2005-03-24 19:00:01 +00:00
|
|
|
return rv;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::GetFlags(PRUint32 *aFlags)
|
|
|
|
{
|
|
|
|
if (!mRules || !aFlags) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
return mRules->GetFlags(aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::SetFlags(PRUint32 aFlags)
|
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NULL_POINTER; }
|
|
|
|
return mRules->SetFlags(aFlags);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPlaintextEditor::InitRules()
|
|
|
|
{
|
|
|
|
// instantiate the rules for this text editor
|
2003-07-28 13:13:50 +00:00
|
|
|
nsresult res = NS_NewTextEditRules(getter_AddRefs(mRules));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!mRules) return NS_ERROR_UNEXPECTED;
|
2003-07-28 13:13:50 +00:00
|
|
|
return mRules->Init(this, mFlags);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-09-18 19:42:38 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::GetIsDocumentEditable(PRBool *aIsDocumentEditable)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aIsDocumentEditable);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMDocument> doc;
|
|
|
|
GetDocument(getter_AddRefs(doc));
|
|
|
|
*aIsDocumentEditable = doc ? IsModifiable() : PR_FALSE;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
PRBool nsPlaintextEditor::IsModifiable()
|
|
|
|
{
|
|
|
|
PRUint32 flags;
|
|
|
|
if (NS_SUCCEEDED(GetFlags(&flags)))
|
|
|
|
return ((flags & eEditorReadonlyMask) == 0);
|
2003-07-28 13:13:50 +00:00
|
|
|
|
|
|
|
return PR_FALSE;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark nsIHTMLEditor methods
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPlaintextEditor::HandleKeyPress(nsIDOMKeyEvent* aKeyEvent)
|
|
|
|
{
|
|
|
|
PRUint32 keyCode, character;
|
2002-11-07 03:34:13 +00:00
|
|
|
PRBool ctrlKey, altKey, metaKey;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
if (!aKeyEvent) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(aKeyEvent->GetKeyCode(&keyCode)) &&
|
|
|
|
NS_SUCCEEDED(aKeyEvent->GetCtrlKey(&ctrlKey)) &&
|
|
|
|
NS_SUCCEEDED(aKeyEvent->GetAltKey(&altKey)) &&
|
|
|
|
NS_SUCCEEDED(aKeyEvent->GetMetaKey(&metaKey)))
|
|
|
|
{
|
|
|
|
aKeyEvent->GetCharCode(&character);
|
|
|
|
if (keyCode == nsIDOMKeyEvent::DOM_VK_RETURN
|
|
|
|
|| keyCode == nsIDOMKeyEvent::DOM_VK_ENTER)
|
|
|
|
{
|
|
|
|
nsString empty;
|
2001-04-07 00:45:26 +00:00
|
|
|
return TypedText(empty, eTypedBreak);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
else if (keyCode == nsIDOMKeyEvent::DOM_VK_ESCAPE)
|
|
|
|
{
|
|
|
|
// pass escape keypresses through as empty strings: needed for ime support
|
|
|
|
nsString empty;
|
2001-04-07 00:45:26 +00:00
|
|
|
return TypedText(empty, eTypedText);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2002-11-07 03:34:13 +00:00
|
|
|
if (character && !altKey && !ctrlKey && !metaKey)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2002-08-07 14:45:08 +00:00
|
|
|
aKeyEvent->PreventDefault();
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString key(character);
|
2001-04-07 00:45:26 +00:00
|
|
|
return TypedText(key, eTypedText);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This routine is needed to provide a bottleneck for typing for logging
|
2004-04-30 23:55:16 +00:00
|
|
|
purposes. Can't use HandleKeyPress() (above) for that since it takes
|
|
|
|
a nsIDOMKeyEvent* parameter. So instead we pass enough info through
|
2001-01-28 20:13:07 +00:00
|
|
|
to TypedText() to determine what action to take, but without passing
|
|
|
|
an event.
|
|
|
|
*/
|
2002-03-23 22:08:20 +00:00
|
|
|
NS_IMETHODIMP nsPlaintextEditor::TypedText(const nsAString& aString,
|
2001-01-28 20:13:07 +00:00
|
|
|
PRInt32 aAction)
|
|
|
|
{
|
|
|
|
nsAutoPlaceHolderBatch batch(this, gTypingTxnName);
|
|
|
|
|
|
|
|
switch (aAction)
|
|
|
|
{
|
|
|
|
case eTypedText:
|
|
|
|
{
|
|
|
|
return InsertText(aString);
|
|
|
|
}
|
|
|
|
case eTypedBreak:
|
|
|
|
{
|
|
|
|
return InsertLineBreak();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPlaintextEditor::CreateBRImpl(nsCOMPtr<nsIDOMNode> *aInOutParent, PRInt32 *aInOutOffset, nsCOMPtr<nsIDOMNode> *outBRNode, EDirection aSelect)
|
|
|
|
{
|
|
|
|
if (!aInOutParent || !*aInOutParent || !aInOutOffset || !outBRNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
*outBRNode = nsnull;
|
|
|
|
nsresult res;
|
|
|
|
|
|
|
|
// we need to insert a br. unfortunately, we may have to split a text node to do it.
|
|
|
|
nsCOMPtr<nsIDOMNode> node = *aInOutParent;
|
|
|
|
PRInt32 theOffset = *aInOutOffset;
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> nodeAsText = do_QueryInterface(node);
|
2001-12-17 15:23:51 +00:00
|
|
|
NS_NAMED_LITERAL_STRING(brType, "br");
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> brNode;
|
|
|
|
if (nodeAsText)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
PRInt32 offset;
|
|
|
|
PRUint32 len;
|
|
|
|
nodeAsText->GetLength(&len);
|
2001-02-04 19:39:56 +00:00
|
|
|
GetNodeLocation(node, address_of(tmp), &offset);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!tmp) return NS_ERROR_FAILURE;
|
|
|
|
if (!theOffset)
|
|
|
|
{
|
|
|
|
// we are already set to go
|
|
|
|
}
|
|
|
|
else if (theOffset == (PRInt32)len)
|
|
|
|
{
|
|
|
|
// update offset to point AFTER the text node
|
|
|
|
offset++;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// split the text node
|
|
|
|
res = SplitNode(node, theOffset, getter_AddRefs(tmp));
|
|
|
|
if (NS_FAILED(res)) return res;
|
2001-02-04 19:39:56 +00:00
|
|
|
res = GetNodeLocation(node, address_of(tmp), &offset);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
// create br
|
|
|
|
res = CreateNode(brType, tmp, offset, getter_AddRefs(brNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
*aInOutParent = tmp;
|
|
|
|
*aInOutOffset = offset+1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
res = CreateNode(brType, node, theOffset, getter_AddRefs(brNode));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
(*aInOutOffset)++;
|
|
|
|
}
|
|
|
|
|
|
|
|
*outBRNode = brNode;
|
|
|
|
if (*outBRNode && (aSelect != eNone))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
|
|
|
PRInt32 offset;
|
2003-07-28 13:13:50 +00:00
|
|
|
res = GetNodeLocation(*outBRNode, address_of(parent), &offset);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
2001-01-28 20:13:07 +00:00
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
|
|
|
|
if (aSelect == eNext)
|
|
|
|
{
|
|
|
|
// position selection after br
|
|
|
|
selPriv->SetInterlinePosition(PR_TRUE);
|
|
|
|
res = selection->Collapse(parent, offset+1);
|
|
|
|
}
|
|
|
|
else if (aSelect == ePrevious)
|
|
|
|
{
|
|
|
|
// position selection before br
|
|
|
|
selPriv->SetInterlinePosition(PR_TRUE);
|
|
|
|
res = selection->Collapse(parent, offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPlaintextEditor::CreateBR(nsIDOMNode *aNode, PRInt32 aOffset, nsCOMPtr<nsIDOMNode> *outBRNode, EDirection aSelect)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = aNode;
|
|
|
|
PRInt32 offset = aOffset;
|
2001-02-04 19:39:56 +00:00
|
|
|
return CreateBRImpl(address_of(parent), &offset, outBRNode, aSelect);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPlaintextEditor::InsertBR(nsCOMPtr<nsIDOMNode> *outBRNode)
|
|
|
|
{
|
|
|
|
if (!outBRNode) return NS_ERROR_NULL_POINTER;
|
|
|
|
*outBRNode = nsnull;
|
|
|
|
|
|
|
|
// calling it text insertion to trigger moz br treatment by rules
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpInsertText, nsIEditor::eNext);
|
|
|
|
|
2003-07-28 13:13:50 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
2003-07-28 13:13:50 +00:00
|
|
|
PRBool bCollapsed;
|
2001-01-28 20:13:07 +00:00
|
|
|
res = selection->GetIsCollapsed(&bCollapsed);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!bCollapsed)
|
|
|
|
{
|
|
|
|
res = DeleteSelection(nsIEditor::eNone);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIDOMNode> selNode;
|
|
|
|
PRInt32 selOffset;
|
2001-02-04 19:39:56 +00:00
|
|
|
res = GetStartNodeAndOffset(selection, address_of(selNode), &selOffset);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
res = CreateBR(selNode, selOffset, outBRNode);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// position selection after br
|
2001-02-04 19:39:56 +00:00
|
|
|
res = GetNodeLocation(*outBRNode, address_of(selNode), &selOffset);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
2003-07-28 13:13:50 +00:00
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
|
2001-01-28 20:13:07 +00:00
|
|
|
selPriv->SetInterlinePosition(PR_TRUE);
|
2003-07-28 13:13:50 +00:00
|
|
|
return selection->Collapse(selNode, selOffset+1);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2003-07-28 13:13:50 +00:00
|
|
|
|
2005-10-17 00:54:02 +00:00
|
|
|
nsresult
|
|
|
|
nsPlaintextEditor::GetTextSelectionOffsets(nsISelection *aSelection,
|
|
|
|
PRUint32 &aOutStartOffset,
|
|
|
|
PRUint32 &aOutEndOffset)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2005-10-17 00:54:02 +00:00
|
|
|
NS_ASSERTION(aSelection, "null selection");
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2005-10-17 00:54:02 +00:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIDOMNode> startNode, endNode;
|
|
|
|
PRInt32 startNodeOffset, endNodeOffset;
|
2001-01-28 20:13:07 +00:00
|
|
|
aSelection->GetAnchorNode(getter_AddRefs(startNode));
|
2005-10-17 00:54:02 +00:00
|
|
|
aSelection->GetAnchorOffset(&startNodeOffset);
|
2001-01-28 20:13:07 +00:00
|
|
|
aSelection->GetFocusNode(getter_AddRefs(endNode));
|
2005-10-17 00:54:02 +00:00
|
|
|
aSelection->GetFocusOffset(&endNodeOffset);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2005-10-17 00:54:02 +00:00
|
|
|
nsIDOMElement* rootNode = GetRoot();
|
|
|
|
NS_ENSURE_TRUE(rootNode, NS_ERROR_NULL_POINTER);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2005-10-17 00:54:02 +00:00
|
|
|
PRInt32 startOffset = -1;
|
|
|
|
PRInt32 endOffset = -1;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2005-10-17 00:54:02 +00:00
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2005-10-17 00:54:02 +00:00
|
|
|
#ifdef NS_DEBUG
|
|
|
|
PRInt32 nodeCount = 0; // only needed for the assertions below
|
|
|
|
#endif
|
|
|
|
PRUint32 totalLength = 0;
|
|
|
|
nsCOMPtr<nsIContent> rootContent = do_QueryInterface(rootNode);
|
|
|
|
iter->Init(rootContent);
|
|
|
|
for (; !iter->IsDone() && (startOffset == -1 || endOffset == -1); iter->Next()) {
|
|
|
|
nsCOMPtr<nsIDOMNode> currentNode = do_QueryInterface(iter->GetCurrentNode());
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> textNode = do_QueryInterface(currentNode);
|
|
|
|
if (textNode) {
|
|
|
|
// Note that sometimes we have an empty #text-node as start/endNode,
|
|
|
|
// which we regard as not editable because the frame width == 0,
|
|
|
|
// see nsEditor::IsEditable().
|
|
|
|
PRBool editable = IsEditable(currentNode);
|
|
|
|
if (currentNode == startNode) {
|
|
|
|
startOffset = totalLength + (editable ? startNodeOffset : 0);
|
|
|
|
}
|
|
|
|
if (currentNode == endNode) {
|
|
|
|
endOffset = totalLength + (editable ? endNodeOffset : 0);
|
|
|
|
}
|
|
|
|
if (editable) {
|
2001-01-28 20:13:07 +00:00
|
|
|
PRUint32 length;
|
|
|
|
textNode->GetLength(&length);
|
|
|
|
totalLength += length;
|
|
|
|
}
|
|
|
|
}
|
2005-10-17 00:54:02 +00:00
|
|
|
#ifdef NS_DEBUG
|
|
|
|
++nodeCount;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
if (endOffset == -1) {
|
|
|
|
NS_ASSERTION(endNode == rootNode, "failed to find the end node");
|
|
|
|
NS_ASSERTION(endNodeOffset == nodeCount-1 || endNodeOffset == 0,
|
|
|
|
"invalid end node offset");
|
|
|
|
endOffset = endNodeOffset == 0 ? 0 : totalLength;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2005-10-17 00:54:02 +00:00
|
|
|
if (startOffset == -1) {
|
|
|
|
NS_ASSERTION(startNode == rootNode, "failed to find the start node");
|
|
|
|
NS_ASSERTION(startNodeOffset == nodeCount-1 || startNodeOffset == 0,
|
|
|
|
"invalid start node offset");
|
|
|
|
startOffset = startNodeOffset == 0 ? 0 : totalLength;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2005-10-17 00:54:02 +00:00
|
|
|
// Make sure aOutStartOffset <= aOutEndOffset.
|
|
|
|
if (startOffset <= endOffset) {
|
|
|
|
aOutStartOffset = startOffset;
|
|
|
|
aOutEndOffset = endOffset;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2005-10-17 00:54:02 +00:00
|
|
|
else {
|
|
|
|
aOutStartOffset = endOffset;
|
|
|
|
aOutEndOffset = startOffset;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPlaintextEditor::DeleteSelection(nsIEditor::EDirection aAction)
|
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
|
|
|
nsresult result;
|
|
|
|
|
|
|
|
// delete placeholder txns merge.
|
|
|
|
nsAutoPlaceHolderBatch batch(this, gDeleteTxnName);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpDeleteSelection, aAction);
|
|
|
|
|
2006-10-10 16:50:50 +00:00
|
|
|
// pre-process
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
result = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
// If there is an existing selection when an extended delete is requested,
|
|
|
|
// platforms that use "caret-style" caret positioning collapse the
|
|
|
|
// selection to the start and then create a new selection.
|
|
|
|
// Platforms that use "selection-style" caret positioning just delete the
|
|
|
|
// existing selection without extending it.
|
|
|
|
PRBool bCollapsed;
|
|
|
|
result = selection->GetIsCollapsed(&bCollapsed);
|
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!bCollapsed &&
|
|
|
|
(aAction == eNextWord || aAction == ePreviousWord ||
|
|
|
|
aAction == eToBeginningOfLine || aAction == eToEndOfLine))
|
|
|
|
if (mCaretStyle == 1)
|
|
|
|
{
|
|
|
|
result = selection->CollapseToStart();
|
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
aAction = eNone;
|
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// If it's one of these modes,
|
|
|
|
// we have to extend the selection first.
|
|
|
|
// This needs to happen inside selection batching,
|
|
|
|
// otherwise the deleted text is autocopied to the clipboard.
|
|
|
|
if (aAction == eNextWord || aAction == ePreviousWord
|
|
|
|
|| aAction == eToBeginningOfLine || aAction == eToEndOfLine)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelectionController> selCont (do_QueryReferent(mSelConWeak));
|
|
|
|
if (!selCont)
|
|
|
|
return NS_ERROR_NO_INTERFACE;
|
|
|
|
|
|
|
|
switch (aAction)
|
|
|
|
{
|
|
|
|
case eNextWord:
|
2006-07-18 07:28:38 +00:00
|
|
|
result = selCont->WordExtendForDelete(PR_TRUE);
|
2001-01-28 20:13:07 +00:00
|
|
|
// DeleteSelectionImpl doesn't handle these actions
|
|
|
|
// because it's inside batching, so don't confuse it:
|
|
|
|
aAction = eNone;
|
|
|
|
break;
|
|
|
|
case ePreviousWord:
|
2006-07-18 07:28:38 +00:00
|
|
|
result = selCont->WordExtendForDelete(PR_FALSE);
|
2001-01-28 20:13:07 +00:00
|
|
|
aAction = eNone;
|
|
|
|
break;
|
|
|
|
case eToBeginningOfLine:
|
|
|
|
selCont->IntraLineMove(PR_TRUE, PR_FALSE); // try to move to end
|
|
|
|
result = selCont->IntraLineMove(PR_FALSE, PR_TRUE); // select to beginning
|
|
|
|
aAction = eNone;
|
|
|
|
break;
|
|
|
|
case eToEndOfLine:
|
|
|
|
result = selCont->IntraLineMove(PR_TRUE, PR_TRUE);
|
2001-06-12 22:21:12 +00:00
|
|
|
aAction = eNext;
|
2001-01-28 20:13:07 +00:00
|
|
|
break;
|
|
|
|
default: // avoid several compiler warnings
|
|
|
|
result = NS_OK;
|
|
|
|
break;
|
|
|
|
}
|
2005-10-17 00:54:02 +00:00
|
|
|
NS_ENSURE_SUCCESS(result, result);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
nsTextRulesInfo ruleInfo(nsTextEditRules::kDeleteSelection);
|
|
|
|
ruleInfo.collapsedAction = aAction;
|
2003-07-28 13:13:50 +00:00
|
|
|
PRBool cancel, handled;
|
2001-01-28 20:13:07 +00:00
|
|
|
result = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!cancel && !handled)
|
|
|
|
{
|
|
|
|
result = DeleteSelectionImpl(aAction);
|
|
|
|
}
|
|
|
|
if (!cancel)
|
|
|
|
{
|
|
|
|
// post-process
|
|
|
|
result = mRules->DidDoAction(selection, &ruleInfo, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2002-03-23 22:08:20 +00:00
|
|
|
NS_IMETHODIMP nsPlaintextEditor::InsertText(const nsAString &aStringToInsert)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
|
|
|
PRInt32 theAction = nsTextEditRules::kInsertText;
|
|
|
|
PRInt32 opID = kOpInsertText;
|
|
|
|
if (mInIMEMode)
|
|
|
|
{
|
|
|
|
theAction = nsTextEditRules::kInsertTextIME;
|
|
|
|
opID = kOpInsertIMEText;
|
|
|
|
}
|
|
|
|
nsAutoPlaceHolderBatch batch(this, nsnull);
|
|
|
|
nsAutoRules beginRulesSniffing(this, opID, nsIEditor::eNext);
|
|
|
|
|
|
|
|
// pre-process
|
2003-07-28 13:13:50 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult result = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
nsAutoString resultString;
|
|
|
|
// XXX can we trust instring to outlive ruleInfo,
|
|
|
|
// XXX and ruleInfo not to refer to instring in its dtor?
|
2001-04-07 00:45:26 +00:00
|
|
|
//nsAutoString instring(aStringToInsert);
|
2001-01-28 20:13:07 +00:00
|
|
|
nsTextRulesInfo ruleInfo(theAction);
|
2001-04-07 00:45:26 +00:00
|
|
|
ruleInfo.inString = &aStringToInsert;
|
2001-01-28 20:13:07 +00:00
|
|
|
ruleInfo.outString = &resultString;
|
|
|
|
ruleInfo.maxLength = mMaxTextLength;
|
|
|
|
|
2003-07-28 13:13:50 +00:00
|
|
|
PRBool cancel, handled;
|
2001-01-28 20:13:07 +00:00
|
|
|
result = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
if (!cancel && !handled)
|
|
|
|
{
|
|
|
|
// we rely on rules code for now - no default implementation
|
|
|
|
}
|
|
|
|
if (!cancel)
|
|
|
|
{
|
|
|
|
// post-process
|
|
|
|
result = mRules->DidDoAction(selection, &ruleInfo, result);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPlaintextEditor::InsertLineBreak()
|
|
|
|
{
|
|
|
|
if (!mRules) { return NS_ERROR_NOT_INITIALIZED; }
|
|
|
|
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpInsertBreak, nsIEditor::eNext);
|
|
|
|
|
|
|
|
// pre-process
|
2003-07-28 13:13:50 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult res;
|
2001-01-28 20:13:07 +00:00
|
|
|
res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2006-11-07 01:35:36 +00:00
|
|
|
// Batching the selection and moving nodes out from under the caret causes
|
|
|
|
// caret turds. Ask the shell to invalidate the caret now to avoid the turds.
|
|
|
|
nsCOMPtr<nsIPresShell> shell;
|
|
|
|
res = GetPresShell(getter_AddRefs(shell));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
shell->MaybeInvalidateCaretPosition();
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsTextRulesInfo ruleInfo(nsTextEditRules::kInsertBreak);
|
2003-07-28 13:13:50 +00:00
|
|
|
PRBool cancel, handled;
|
2001-01-28 20:13:07 +00:00
|
|
|
res = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!cancel && !handled)
|
|
|
|
{
|
|
|
|
// create the new BR node
|
|
|
|
nsCOMPtr<nsIDOMNode> newNode;
|
2003-07-28 13:13:50 +00:00
|
|
|
res = DeleteSelectionAndCreateNode(NS_LITERAL_STRING("br"), getter_AddRefs(newNode));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!newNode) res = NS_ERROR_NULL_POINTER; // don't return here, so DidDoAction is called
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
// set the selection to the new node
|
|
|
|
nsCOMPtr<nsIDOMNode>parent;
|
|
|
|
res = newNode->GetParentNode(getter_AddRefs(parent));
|
|
|
|
if (!parent) res = NS_ERROR_NULL_POINTER; // don't return here, so DidDoAction is called
|
|
|
|
if (NS_SUCCEEDED(res))
|
|
|
|
{
|
|
|
|
PRInt32 offsetInParent=-1; // we use the -1 as a marker to see if we need to compute this or not
|
|
|
|
nsCOMPtr<nsIDOMNode>nextNode;
|
|
|
|
newNode->GetNextSibling(getter_AddRefs(nextNode));
|
|
|
|
if (nextNode)
|
|
|
|
{
|
2003-07-28 13:13:50 +00:00
|
|
|
nsCOMPtr<nsIDOMCharacterData>nextTextNode = do_QueryInterface(nextNode);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (!nextTextNode) {
|
2003-07-28 13:13:50 +00:00
|
|
|
nextNode = do_QueryInterface(newNode); // is this QI needed?
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
offsetInParent=0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2003-07-28 13:13:50 +00:00
|
|
|
nextNode = do_QueryInterface(newNode); // is this QI needed?
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2003-07-28 13:13:50 +00:00
|
|
|
|
|
|
|
if (-1==offsetInParent)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2003-07-28 13:13:50 +00:00
|
|
|
nextNode->GetParentNode(getter_AddRefs(parent));
|
|
|
|
res = GetChildOffset(nextNode, parent, offsetInParent);
|
|
|
|
if (NS_SUCCEEDED(res)) {
|
|
|
|
// SetInterlinePosition(PR_TRUE) means we want the caret to stick to the content on the "right".
|
|
|
|
// We want the caret to stick to whatever is past the break. This is
|
|
|
|
// because the break is on the same line we were on, but the next content
|
|
|
|
// will be on the following line.
|
|
|
|
nsCOMPtr<nsISelectionPrivate> selPriv(do_QueryInterface(selection));
|
|
|
|
selPriv->SetInterlinePosition(PR_TRUE);
|
|
|
|
res = selection->Collapse(parent, offsetInParent+1); // +1 to insert just after the break
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
2003-07-28 13:13:50 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
res = selection->Collapse(nextNode, offsetInParent);
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!cancel)
|
|
|
|
{
|
|
|
|
// post-process, always called if WillInsertBreak didn't return cancel==PR_TRUE
|
|
|
|
res = mRules->DidDoAction(selection, &ruleInfo, res);
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2001-06-06 01:21:05 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::BeginComposition(nsTextEventReply* aReply)
|
|
|
|
{
|
2005-03-07 12:25:26 +00:00
|
|
|
NS_ENSURE_TRUE(!mInIMEMode, NS_OK);
|
|
|
|
|
2001-06-06 01:21:05 +00:00
|
|
|
if(mFlags & nsIPlaintextEditor::eEditorPasswordMask) {
|
|
|
|
if (mRules) {
|
|
|
|
nsIEditRules *p = mRules.get();
|
2007-07-08 07:08:04 +00:00
|
|
|
nsTextEditRules *textEditRules = static_cast<nsTextEditRules *>(p);
|
2001-06-06 01:21:05 +00:00
|
|
|
textEditRules->ResetIMETextPWBuf();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nsEditor::BeginComposition(aReply);
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::GetDocumentIsEmpty(PRBool *aDocumentIsEmpty)
|
|
|
|
{
|
|
|
|
if (!aDocumentIsEmpty)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
if (!mRules)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
return mRules->DocumentIsEmpty(aDocumentIsEmpty);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::GetTextLength(PRInt32 *aCount)
|
|
|
|
{
|
2005-10-17 00:54:02 +00:00
|
|
|
NS_ASSERTION(aCount, "null pointer");
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// initialize out params
|
|
|
|
*aCount = 0;
|
|
|
|
|
2005-10-17 00:54:02 +00:00
|
|
|
// special-case for empty document, to account for the bogus node
|
2001-01-28 20:13:07 +00:00
|
|
|
PRBool docEmpty;
|
2005-10-17 00:54:02 +00:00
|
|
|
nsresult rv = GetDocumentIsEmpty(&docEmpty);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (docEmpty)
|
|
|
|
return NS_OK;
|
|
|
|
|
2005-10-17 00:54:02 +00:00
|
|
|
nsIDOMElement* rootNode = GetRoot();
|
|
|
|
NS_ENSURE_TRUE(rootNode, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentIterator> iter =
|
|
|
|
do_CreateInstance("@mozilla.org/content/post-content-iterator;1", &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
PRUint32 totalLength = 0;
|
|
|
|
nsCOMPtr<nsIContent> rootContent = do_QueryInterface(rootNode);
|
|
|
|
iter->Init(rootContent);
|
|
|
|
for (; !iter->IsDone(); iter->Next()) {
|
|
|
|
nsCOMPtr<nsIDOMNode> currentNode = do_QueryInterface(iter->GetCurrentNode());
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> textNode = do_QueryInterface(currentNode);
|
|
|
|
if (textNode && IsEditable(currentNode)) {
|
|
|
|
PRUint32 length;
|
|
|
|
textNode->GetLength(&length);
|
|
|
|
totalLength += length;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
}
|
2005-10-17 00:54:02 +00:00
|
|
|
|
|
|
|
*aCount = totalLength;
|
|
|
|
return NS_OK;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::SetMaxTextLength(PRInt32 aMaxTextLength)
|
|
|
|
{
|
|
|
|
mMaxTextLength = aMaxTextLength;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::GetMaxTextLength(PRInt32* aMaxTextLength)
|
|
|
|
{
|
|
|
|
if (!aMaxTextLength)
|
|
|
|
return NS_ERROR_INVALID_POINTER;
|
|
|
|
*aMaxTextLength = mMaxTextLength;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
2002-04-16 23:11:33 +00:00
|
|
|
// Get the wrap width
|
2001-01-28 20:13:07 +00:00
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::GetWrapWidth(PRInt32 *aWrapColumn)
|
|
|
|
{
|
|
|
|
if (! aWrapColumn)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2002-04-16 23:11:33 +00:00
|
|
|
*aWrapColumn = mWrapColumn;
|
2001-01-28 20:13:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// See if the style value includes this attribute, and if it does,
|
|
|
|
// cut out everything from the attribute to the next semicolon.
|
|
|
|
//
|
|
|
|
static void CutStyle(const char* stylename, nsString& styleValue)
|
|
|
|
{
|
|
|
|
// Find the current wrapping type:
|
|
|
|
PRInt32 styleStart = styleValue.Find(stylename, PR_TRUE);
|
|
|
|
if (styleStart >= 0)
|
|
|
|
{
|
|
|
|
PRInt32 styleEnd = styleValue.Find(";", PR_FALSE, styleStart);
|
|
|
|
if (styleEnd > styleStart)
|
|
|
|
styleValue.Cut(styleStart, styleEnd - styleStart + 1);
|
|
|
|
else
|
|
|
|
styleValue.Cut(styleStart, styleValue.Length() - styleStart);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Change the wrap width on the root of this document.
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::SetWrapWidth(PRInt32 aWrapColumn)
|
|
|
|
{
|
2007-07-25 00:11:22 +00:00
|
|
|
SetWrapColumn(aWrapColumn);
|
2002-04-16 23:11:33 +00:00
|
|
|
|
|
|
|
// Make sure we're a plaintext editor, otherwise we shouldn't
|
|
|
|
// do the rest of this.
|
|
|
|
PRUint32 flags = 0;
|
|
|
|
GetFlags(&flags);
|
|
|
|
if (!(flags & eEditorPlaintextMask))
|
|
|
|
return NS_OK;
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// Ought to set a style sheet here ...
|
|
|
|
// Probably should keep around an mPlaintextStyleSheet for this purpose.
|
2005-03-24 19:00:01 +00:00
|
|
|
nsIDOMElement *rootElement = GetRoot();
|
|
|
|
if (!rootElement)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2005-03-24 19:00:01 +00:00
|
|
|
// Get the current style for this root element:
|
2001-12-17 15:23:51 +00:00
|
|
|
NS_NAMED_LITERAL_STRING(styleName, "style");
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoString styleValue;
|
2005-03-24 19:00:01 +00:00
|
|
|
nsresult res = rootElement->GetAttribute(styleName, styleValue);
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
|
|
|
|
// We'll replace styles for these values:
|
|
|
|
CutStyle("white-space", styleValue);
|
|
|
|
CutStyle("width", styleValue);
|
|
|
|
CutStyle("font-family", styleValue);
|
|
|
|
|
|
|
|
// If we have other style left, trim off any existing semicolons
|
|
|
|
// or whitespace, then add a known semicolon-space:
|
2003-05-23 21:34:47 +00:00
|
|
|
if (!styleValue.IsEmpty())
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
styleValue.Trim("; \t", PR_FALSE, PR_TRUE);
|
2004-06-17 00:13:25 +00:00
|
|
|
styleValue.AppendLiteral("; ");
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we have fixed-width font. This should be done for us,
|
|
|
|
// but it isn't, see bug 22502, so we have to add "font: -moz-fixed;".
|
|
|
|
// Only do this if we're wrapping.
|
|
|
|
if ((flags & eEditorEnableWrapHackMask) && aWrapColumn >= 0)
|
2004-06-17 00:13:25 +00:00
|
|
|
styleValue.AppendLiteral("font-family: -moz-fixed; ");
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2002-04-16 23:11:33 +00:00
|
|
|
// If "mail.compose.wrap_to_window_width" is set, and we're a mail editor,
|
|
|
|
// then remember our wrap width (for output purposes) but set the visual
|
|
|
|
// wrapping to window width.
|
|
|
|
// We may reset mWrapToWindow here, based on the pref's current value.
|
|
|
|
if (flags & eEditorMailMask)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
2002-11-23 01:26:58 +00:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch =
|
|
|
|
do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
|
2002-04-16 23:11:33 +00:00
|
|
|
if (NS_SUCCEEDED(rv))
|
2002-11-23 01:26:58 +00:00
|
|
|
prefBranch->GetBoolPref("mail.compose.wrap_to_window_width",
|
|
|
|
&mWrapToWindow);
|
2002-04-16 23:11:33 +00:00
|
|
|
}
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// and now we're ready to set the new whitespace/wrapping style.
|
2002-04-16 23:11:33 +00:00
|
|
|
if (aWrapColumn > 0 && !mWrapToWindow) // Wrap to a fixed column
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2004-06-17 00:13:25 +00:00
|
|
|
styleValue.AppendLiteral("white-space: -moz-pre-wrap; width: ");
|
2001-01-28 20:13:07 +00:00
|
|
|
styleValue.AppendInt(aWrapColumn);
|
2004-06-17 00:13:25 +00:00
|
|
|
styleValue.AppendLiteral("ch;");
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2002-04-16 23:11:33 +00:00
|
|
|
else if (mWrapToWindow || aWrapColumn == 0)
|
2004-06-17 00:13:25 +00:00
|
|
|
styleValue.AppendLiteral("white-space: -moz-pre-wrap;");
|
2001-01-28 20:13:07 +00:00
|
|
|
else
|
2004-06-17 00:13:25 +00:00
|
|
|
styleValue.AppendLiteral("white-space: pre;");
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2005-03-24 19:00:01 +00:00
|
|
|
return rootElement->SetAttribute(styleName, styleValue);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2007-07-25 00:11:22 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::SetWrapColumn(PRInt32 aWrapColumn)
|
|
|
|
{
|
|
|
|
mWrapColumn = aWrapColumn;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2005-12-20 20:12:54 +00:00
|
|
|
//
|
|
|
|
// Get the newline handling for this editor
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::GetNewlineHandling(PRInt32 *aNewlineHandling)
|
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aNewlineHandling);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2005-12-20 20:12:54 +00:00
|
|
|
*aNewlineHandling = mNewlineHandling;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Change the newline handling for this editor
|
|
|
|
//
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::SetNewlineHandling(PRInt32 aNewlineHandling)
|
|
|
|
{
|
|
|
|
mNewlineHandling = aNewlineHandling;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark nsIEditor overrides
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::Undo(PRUint32 aCount)
|
|
|
|
{
|
2001-05-11 13:59:11 +00:00
|
|
|
nsAutoUpdateViewBatch beginViewBatching(this);
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
ForceCompositionEnd();
|
|
|
|
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpUndo, nsIEditor::eNone);
|
|
|
|
|
|
|
|
nsTextRulesInfo ruleInfo(nsTextEditRules::kUndo);
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
GetSelection(getter_AddRefs(selection));
|
|
|
|
PRBool cancel, handled;
|
2003-07-28 13:13:50 +00:00
|
|
|
nsresult result = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
if (!cancel && NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
result = nsEditor::Undo(aCount);
|
|
|
|
result = mRules->DidDoAction(selection, &ruleInfo, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::Redo(PRUint32 aCount)
|
|
|
|
{
|
2001-05-11 13:59:11 +00:00
|
|
|
nsAutoUpdateViewBatch beginViewBatching(this);
|
2003-07-28 13:13:50 +00:00
|
|
|
|
|
|
|
ForceCompositionEnd();
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsAutoRules beginRulesSniffing(this, kOpRedo, nsIEditor::eNone);
|
|
|
|
|
|
|
|
nsTextRulesInfo ruleInfo(nsTextEditRules::kRedo);
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
GetSelection(getter_AddRefs(selection));
|
|
|
|
PRBool cancel, handled;
|
2003-07-28 13:13:50 +00:00
|
|
|
nsresult result = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
if (!cancel && NS_SUCCEEDED(result))
|
|
|
|
{
|
|
|
|
result = nsEditor::Redo(aCount);
|
|
|
|
result = mRules->DidDoAction(selection, &ruleInfo, result);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2007-07-26 04:14:33 +00:00
|
|
|
nsresult nsPlaintextEditor::GetClipboardEventTarget(nsIDOMNode** aEventTarget)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2007-07-26 04:14:33 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aEventTarget);
|
|
|
|
*aEventTarget = nsnull;
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
2001-11-19 06:45:00 +00:00
|
|
|
if (NS_FAILED(res))
|
2001-01-28 20:13:07 +00:00
|
|
|
return res;
|
|
|
|
|
2007-07-26 04:14:33 +00:00
|
|
|
return nsCopySupport::GetClipboardEventTarget(selection, aEventTarget);
|
|
|
|
}
|
|
|
|
|
2007-09-29 17:56:04 +00:00
|
|
|
nsresult nsPlaintextEditor::FireClipboardEvent(PRUint32 msg,
|
|
|
|
PRBool* aPreventDefault)
|
2007-07-26 04:14:33 +00:00
|
|
|
{
|
2007-09-29 17:56:04 +00:00
|
|
|
*aPreventDefault = PR_FALSE;
|
|
|
|
|
2007-07-26 04:14:33 +00:00
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!ps)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2007-09-29 17:56:04 +00:00
|
|
|
// Unsafe to fire event during reflow (bug 396108)
|
|
|
|
PRBool isReflowing = PR_TRUE;
|
|
|
|
nsresult rv = ps->IsReflowLocked(&isReflowing);
|
|
|
|
if (NS_FAILED(rv) || isReflowing)
|
|
|
|
return NS_OK;
|
|
|
|
|
2007-07-26 04:14:33 +00:00
|
|
|
nsCOMPtr<nsIDOMNode> eventTarget;
|
2007-09-29 17:56:04 +00:00
|
|
|
rv = GetClipboardEventTarget(getter_AddRefs(eventTarget));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
// On failure to get event target, just forget about it and don't fire.
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
nsEventStatus status = nsEventStatus_eIgnore;
|
|
|
|
nsEvent evt(PR_TRUE, msg);
|
|
|
|
nsEventDispatcher::Dispatch(eventTarget, ps->GetPresContext(), &evt,
|
|
|
|
nsnull, &status);
|
|
|
|
// if event handler return'd false (PreventDefault)
|
|
|
|
if (status == nsEventStatus_eConsumeNoDefault)
|
|
|
|
*aPreventDefault = PR_TRUE;
|
|
|
|
|
|
|
|
// Did the event handler cause the editor to be destroyed? (ie. the input
|
|
|
|
// element was removed from the document) Don't proceed with command,
|
|
|
|
// could crash, definitely does during paste.
|
|
|
|
if (mDidPreDestroy)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPlaintextEditor::Cut()
|
|
|
|
{
|
|
|
|
PRBool preventDefault;
|
|
|
|
nsresult rv = FireClipboardEvent(NS_CUT, &preventDefault);
|
|
|
|
if (NS_FAILED(rv) || preventDefault)
|
|
|
|
return rv;
|
2007-07-26 04:14:33 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
rv = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
PRBool isCollapsed;
|
|
|
|
if (NS_SUCCEEDED(selection->GetIsCollapsed(&isCollapsed)) && isCollapsed)
|
2002-11-13 21:21:28 +00:00
|
|
|
return NS_OK; // just return ok so no JS error is thrown
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2007-09-29 17:56:04 +00:00
|
|
|
// ps should be guaranteed by FireClipboardEvent not failing
|
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
2007-07-26 04:14:33 +00:00
|
|
|
rv = ps->DoCopy();
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
rv = DeleteSelection(eNone);
|
|
|
|
return rv;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2001-04-07 00:45:26 +00:00
|
|
|
NS_IMETHODIMP nsPlaintextEditor::CanCut(PRBool *aCanCut)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2007-07-26 04:14:33 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aCanCut);
|
|
|
|
*aCanCut = PR_FALSE;
|
|
|
|
|
2007-09-29 17:56:04 +00:00
|
|
|
nsresult rv = FireClipboardEvent(NS_BEFORECUT, aCanCut);
|
|
|
|
if (NS_FAILED(rv) || *aCanCut)
|
|
|
|
return rv;
|
2007-07-26 04:14:33 +00:00
|
|
|
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
rv = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2007-07-26 04:14:33 +00:00
|
|
|
PRBool isCollapsed;
|
|
|
|
rv = selection->GetIsCollapsed(&isCollapsed);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
*aCanCut = !isCollapsed && IsModifiable();
|
2001-01-28 20:13:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPlaintextEditor::Copy()
|
|
|
|
{
|
2007-09-29 17:56:04 +00:00
|
|
|
PRBool preventDefault;
|
|
|
|
nsresult rv = FireClipboardEvent(NS_COPY, &preventDefault);
|
|
|
|
if (NS_FAILED(rv) || preventDefault)
|
|
|
|
return rv;
|
2007-07-26 04:14:33 +00:00
|
|
|
|
2007-09-29 17:56:04 +00:00
|
|
|
// ps should be guaranteed by FireClipboardEvent not failing
|
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
2001-01-28 20:13:07 +00:00
|
|
|
return ps->DoCopy();
|
|
|
|
}
|
|
|
|
|
2001-04-07 00:45:26 +00:00
|
|
|
NS_IMETHODIMP nsPlaintextEditor::CanCopy(PRBool *aCanCopy)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2007-07-26 04:14:33 +00:00
|
|
|
NS_ENSURE_ARG_POINTER(aCanCopy);
|
2001-04-07 00:45:26 +00:00
|
|
|
*aCanCopy = PR_FALSE;
|
2007-07-26 04:14:33 +00:00
|
|
|
|
2007-09-29 17:56:04 +00:00
|
|
|
nsresult rv = FireClipboardEvent(NS_BEFORECOPY, aCanCopy);
|
|
|
|
if (NS_FAILED(rv) || *aCanCopy)
|
|
|
|
return rv;
|
2007-07-26 04:14:33 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
2007-07-26 04:14:33 +00:00
|
|
|
rv = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
PRBool isCollapsed;
|
2007-07-26 04:14:33 +00:00
|
|
|
rv = selection->GetIsCollapsed(&isCollapsed);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2001-04-07 00:45:26 +00:00
|
|
|
*aCanCopy = !isCollapsed;
|
2001-01-28 20:13:07 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// Shared between OutputToString and OutputToStream
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsPlaintextEditor::GetAndInitDocEncoder(const nsAString& aFormatType,
|
2001-04-05 23:48:01 +00:00
|
|
|
PRUint32 aFlags,
|
2003-06-17 16:40:34 +00:00
|
|
|
const nsACString& aCharset,
|
2001-04-05 23:48:01 +00:00
|
|
|
nsIDocumentEncoder** encoder)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPresShell> presShell;
|
|
|
|
nsresult rv = GetPresShell(getter_AddRefs(presShell));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (!presShell) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
nsCAutoString formatType(NS_DOC_ENCODER_CONTRACTID_BASE);
|
|
|
|
formatType.AppendWithConversion(aFormatType);
|
2001-10-27 11:28:06 +00:00
|
|
|
nsCOMPtr<nsIDocumentEncoder> docEncoder (do_CreateInstance(formatType.get(), &rv));
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2004-08-02 04:52:55 +00:00
|
|
|
nsIDocument *doc = presShell->GetDocument();
|
2006-05-01 05:25:52 +00:00
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(doc);
|
|
|
|
NS_ASSERTION(domDoc, "Need a document");
|
|
|
|
|
|
|
|
rv = docEncoder->Init(domDoc, aFormatType, aFlags);
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2003-05-23 21:34:47 +00:00
|
|
|
if (!aCharset.IsEmpty()
|
2004-05-22 22:15:22 +00:00
|
|
|
&& !(aCharset.EqualsLiteral("null")))
|
2001-04-07 00:45:26 +00:00
|
|
|
docEncoder->SetCharset(aCharset);
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
PRInt32 wc;
|
|
|
|
(void) GetWrapWidth(&wc);
|
|
|
|
if (wc >= 0)
|
|
|
|
(void) docEncoder->SetWrapColumn(wc);
|
|
|
|
|
|
|
|
// Set the selection, if appropriate.
|
|
|
|
// We do this either if the OutputSelectionOnly flag is set,
|
|
|
|
// in which case we use our existing selection ...
|
|
|
|
if (aFlags & nsIDocumentEncoder::OutputSelectionOnly)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
rv = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_SUCCEEDED(rv) && selection)
|
|
|
|
rv = docEncoder->SetSelection(selection);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
// ... or if the root element is not a body,
|
|
|
|
// in which case we set the selection to encompass the root.
|
|
|
|
else
|
|
|
|
{
|
2005-03-24 19:00:01 +00:00
|
|
|
nsIDOMElement *rootElement = GetRoot();
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_ENSURE_TRUE(rootElement, NS_ERROR_FAILURE);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (!nsTextEditUtils::IsBody(rootElement))
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2006-11-09 08:43:26 +00:00
|
|
|
rv = docEncoder->SetContainerNode(rootElement);
|
2001-01-28 20:13:07 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ADDREF(*encoder = docEncoder);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-25 22:39:19 +00:00
|
|
|
nsPlaintextEditor::OutputToString(const nsAString& aFormatType,
|
|
|
|
PRUint32 aFlags,
|
|
|
|
nsAString& aOutputString)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
nsString resultString;
|
|
|
|
nsTextRulesInfo ruleInfo(nsTextEditRules::kOutputText);
|
|
|
|
ruleInfo.outString = &resultString;
|
|
|
|
// XXX Struct should store a nsAReadable*
|
|
|
|
nsAutoString str(aFormatType);
|
|
|
|
ruleInfo.outputFormat = &str;
|
2003-07-28 13:13:50 +00:00
|
|
|
PRBool cancel, handled;
|
2001-01-28 20:13:07 +00:00
|
|
|
nsresult rv = mRules->WillDoAction(nsnull, &ruleInfo, &cancel, &handled);
|
|
|
|
if (cancel || NS_FAILED(rv)) { return rv; }
|
|
|
|
if (handled)
|
|
|
|
{ // this case will get triggered by password fields
|
|
|
|
aOutputString.Assign(*(ruleInfo.outString));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2003-06-17 16:40:34 +00:00
|
|
|
nsCAutoString charsetStr;
|
2001-07-14 00:50:21 +00:00
|
|
|
rv = GetDocumentCharacterSet(charsetStr);
|
|
|
|
if(NS_FAILED(rv) || charsetStr.IsEmpty())
|
2004-06-17 00:13:25 +00:00
|
|
|
charsetStr.AssignLiteral("ISO-8859-1");
|
2001-07-14 00:50:21 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder;
|
2001-07-14 00:50:21 +00:00
|
|
|
rv = GetAndInitDocEncoder(aFormatType, aFlags, charsetStr, getter_AddRefs(encoder));
|
2001-01-28 20:13:07 +00:00
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
2003-07-28 13:13:50 +00:00
|
|
|
return encoder->EncodeToString(aOutputString);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::OutputToStream(nsIOutputStream* aOutputStream,
|
2002-03-23 22:08:20 +00:00
|
|
|
const nsAString& aFormatType,
|
2003-06-17 16:40:34 +00:00
|
|
|
const nsACString& aCharset,
|
2001-01-28 20:13:07 +00:00
|
|
|
PRUint32 aFlags)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
// special-case for empty document when requesting plain text,
|
|
|
|
// to account for the bogus text node.
|
|
|
|
// XXX Should there be a similar test in OutputToString?
|
2004-05-22 22:15:22 +00:00
|
|
|
if (aFormatType.EqualsLiteral("text/plain"))
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
|
|
|
PRBool docEmpty;
|
|
|
|
rv = GetDocumentIsEmpty(&docEmpty);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (docEmpty)
|
|
|
|
return NS_OK; // output nothing
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder;
|
|
|
|
rv = GetAndInitDocEncoder(aFormatType, aFlags, aCharset,
|
|
|
|
getter_AddRefs(encoder));
|
|
|
|
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
return encoder->EncodeToStream(aOutputStream);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-04-05 23:48:01 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark nsIEditorMailSupport overrides
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
2002-10-08 20:24:23 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::InsertTextWithQuotations(const nsAString &aStringToInsert)
|
|
|
|
{
|
|
|
|
return InsertText(aStringToInsert);
|
|
|
|
}
|
|
|
|
|
2001-04-05 23:48:01 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::PasteAsQuotation(PRInt32 aSelectionType)
|
|
|
|
{
|
|
|
|
// Get Clipboard Service
|
|
|
|
nsresult rv;
|
2003-07-08 20:22:56 +00:00
|
|
|
nsCOMPtr<nsIClipboard> clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
|
2001-04-05 23:48:01 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Create generic Transferable for getting the data
|
2003-07-08 20:22:56 +00:00
|
|
|
nsCOMPtr<nsITransferable> trans = do_CreateInstance("@mozilla.org/widget/transferable;1", &rv);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && trans)
|
|
|
|
{
|
|
|
|
// We only handle plaintext pastes here
|
|
|
|
trans->AddDataFlavor(kUnicodeMime);
|
|
|
|
|
|
|
|
// Get the Data from the clipboard
|
|
|
|
clipboard->GetData(trans, aSelectionType);
|
|
|
|
|
|
|
|
// Now we ask the transferable for the data
|
|
|
|
// it still owns the data, we just have a pointer to it.
|
|
|
|
// If it can't support a "text" output of the data the call will fail
|
|
|
|
nsCOMPtr<nsISupports> genericDataObj;
|
2003-07-28 13:13:50 +00:00
|
|
|
PRUint32 len;
|
|
|
|
char* flav = nsnull;
|
2001-04-05 23:48:01 +00:00
|
|
|
rv = trans->GetAnyTransferData(&flav, getter_AddRefs(genericDataObj),
|
|
|
|
&len);
|
2003-07-28 13:13:50 +00:00
|
|
|
if (NS_FAILED(rv) || !flav)
|
2001-04-05 23:48:01 +00:00
|
|
|
{
|
|
|
|
#ifdef DEBUG_akkana
|
|
|
|
printf("PasteAsPlaintextQuotation: GetAnyTransferData failed, %d\n", rv);
|
|
|
|
#endif
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG_clipboard
|
|
|
|
printf("Got flavor [%s]\n", flav);
|
|
|
|
#endif
|
2003-07-28 13:13:50 +00:00
|
|
|
if (0 == nsCRT::strcmp(flav, kUnicodeMime))
|
2001-04-05 23:48:01 +00:00
|
|
|
{
|
2002-08-06 00:53:19 +00:00
|
|
|
nsCOMPtr<nsISupportsString> textDataObj ( do_QueryInterface(genericDataObj) );
|
2001-04-05 23:48:01 +00:00
|
|
|
if (textDataObj && len > 0)
|
|
|
|
{
|
2003-07-28 13:13:50 +00:00
|
|
|
nsAutoString stuffToPaste;
|
|
|
|
textDataObj->GetData ( stuffToPaste );
|
2001-04-05 23:48:01 +00:00
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
rv = InsertAsQuotation(stuffToPaste, 0);
|
|
|
|
}
|
|
|
|
}
|
2006-09-08 04:35:17 +00:00
|
|
|
NS_Free(flav);
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Utility routine to make a new citer. This addrefs, of course.
|
|
|
|
static nsICiter* MakeACiter()
|
|
|
|
{
|
|
|
|
// Make a citer of an appropriate type
|
|
|
|
nsICiter* citer = 0;
|
|
|
|
nsresult rv;
|
2002-11-23 01:26:58 +00:00
|
|
|
nsCOMPtr<nsIPrefBranch> prefBranch =
|
|
|
|
do_GetService(NS_PREFSERVICE_CONTRACTID, &rv);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (NS_FAILED(rv)) return 0;
|
|
|
|
|
|
|
|
char *citationType = 0;
|
2002-11-23 01:26:58 +00:00
|
|
|
rv = prefBranch->GetCharPref("mail.compose.citationType", &citationType);
|
2001-04-05 23:48:01 +00:00
|
|
|
|
2003-07-28 13:13:50 +00:00
|
|
|
if (NS_SUCCEEDED(rv) && citationType[0] && !strncmp(citationType, "aol", 3))
|
|
|
|
citer = new nsAOLCiter;
|
2001-04-05 23:48:01 +00:00
|
|
|
else
|
|
|
|
citer = new nsInternetCiter;
|
|
|
|
|
2003-07-28 13:13:50 +00:00
|
|
|
if (citationType)
|
|
|
|
PL_strfree(citationType);
|
|
|
|
|
2001-04-05 23:48:01 +00:00
|
|
|
if (citer)
|
|
|
|
NS_ADDREF(citer);
|
|
|
|
return citer;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsPlaintextEditor::InsertAsQuotation(const nsAString& aQuotedText,
|
2001-04-05 23:48:01 +00:00
|
|
|
nsIDOMNode **aNodeInserted)
|
|
|
|
{
|
|
|
|
// We have the text. Cite it appropriately:
|
|
|
|
nsCOMPtr<nsICiter> citer = dont_AddRef(MakeACiter());
|
|
|
|
|
|
|
|
// Let the citer quote it for us:
|
|
|
|
nsString quotedStuff;
|
|
|
|
nsresult rv = citer->GetCiteString(aQuotedText, quotedStuff);
|
2001-11-19 06:45:00 +00:00
|
|
|
if (NS_FAILED(rv))
|
2001-04-05 23:48:01 +00:00
|
|
|
return rv;
|
|
|
|
|
|
|
|
// It's best to put a blank line after the quoted text so that mails
|
|
|
|
// written without thinking won't be so ugly.
|
2002-06-12 14:12:32 +00:00
|
|
|
if (!aQuotedText.IsEmpty() && (aQuotedText.Last() != PRUnichar('\n')))
|
|
|
|
quotedStuff.Append(PRUnichar('\n'));
|
2001-04-05 23:48:01 +00:00
|
|
|
|
|
|
|
// get selection
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
rv = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (!selection) return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsAutoEditBatch beginBatching(this);
|
|
|
|
nsAutoRules beginRulesSniffing(this, kOpInsertText, nsIEditor::eNext);
|
|
|
|
|
|
|
|
// give rules a chance to handle or cancel
|
|
|
|
nsTextRulesInfo ruleInfo(nsTextEditRules::kInsertElement);
|
|
|
|
PRBool cancel, handled;
|
|
|
|
rv = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
if (cancel) return NS_OK; // rules canceled the operation
|
|
|
|
if (!handled)
|
|
|
|
{
|
2001-04-07 00:45:26 +00:00
|
|
|
rv = InsertText(quotedStuff);
|
2001-04-05 23:48:01 +00:00
|
|
|
|
|
|
|
// XXX Should set *aNodeInserted to the first node inserted
|
|
|
|
if (aNodeInserted && NS_SUCCEEDED(rv))
|
|
|
|
{
|
|
|
|
*aNodeInserted = 0;
|
|
|
|
//NS_IF_ADDREF(*aNodeInserted);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsPlaintextEditor::PasteAsCitedQuotation(const nsAString& aCitation,
|
2001-04-05 23:48:01 +00:00
|
|
|
PRInt32 aSelectionType)
|
|
|
|
{
|
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsPlaintextEditor::InsertAsCitedQuotation(const nsAString& aQuotedText,
|
|
|
|
const nsAString& aCitation,
|
2001-04-05 23:48:01 +00:00
|
|
|
PRBool aInsertHTML,
|
|
|
|
nsIDOMNode **aNodeInserted)
|
|
|
|
{
|
|
|
|
return InsertAsQuotation(aQuotedText, aNodeInserted);
|
|
|
|
}
|
|
|
|
|
2003-07-28 13:13:50 +00:00
|
|
|
nsresult
|
2003-09-03 04:17:45 +00:00
|
|
|
nsPlaintextEditor::SharedOutputString(PRUint32 aFlags,
|
|
|
|
PRBool* aIsCollapsed,
|
|
|
|
nsAString& aResult)
|
2003-07-28 13:13:50 +00:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult rv = GetSelection(getter_AddRefs(selection));
|
2003-09-03 04:17:45 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-07-28 13:13:50 +00:00
|
|
|
if (!selection)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
|
|
|
rv = selection->GetIsCollapsed(aIsCollapsed);
|
2003-09-03 04:17:45 +00:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-07-28 13:13:50 +00:00
|
|
|
|
2003-09-03 04:17:45 +00:00
|
|
|
if (!*aIsCollapsed)
|
2003-07-28 13:13:50 +00:00
|
|
|
aFlags |= nsIDocumentEncoder::OutputSelectionOnly;
|
2003-09-03 04:17:45 +00:00
|
|
|
// If the selection isn't collapsed, we'll use the whole document.
|
2003-07-28 13:13:50 +00:00
|
|
|
|
|
|
|
return OutputToString(NS_LITERAL_STRING("text/plain"), aFlags, aResult);
|
|
|
|
}
|
|
|
|
|
2001-04-05 23:48:01 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::Rewrap(PRBool aRespectNewlines)
|
|
|
|
{
|
|
|
|
PRInt32 wrapCol;
|
|
|
|
nsresult rv = GetWrapWidth(&wrapCol);
|
|
|
|
if (NS_FAILED(rv))
|
|
|
|
return NS_OK;
|
2001-05-09 13:05:05 +00:00
|
|
|
|
|
|
|
// Rewrap makes no sense if there's no wrap column; default to 72.
|
|
|
|
if (wrapCol <= 0)
|
|
|
|
wrapCol = 72;
|
|
|
|
|
2001-04-05 23:48:01 +00:00
|
|
|
#ifdef DEBUG_akkana
|
|
|
|
printf("nsPlaintextEditor::Rewrap to %ld columns\n", (long)wrapCol);
|
|
|
|
#endif
|
|
|
|
|
2003-07-28 13:13:50 +00:00
|
|
|
nsAutoString current;
|
|
|
|
PRBool isCollapsed;
|
|
|
|
rv = SharedOutputString(nsIDocumentEncoder::OutputFormatted
|
|
|
|
| nsIDocumentEncoder::OutputLFLineBreak,
|
|
|
|
&isCollapsed, current);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2003-07-28 13:13:50 +00:00
|
|
|
nsCOMPtr<nsICiter> citer = dont_AddRef(MakeACiter());
|
2001-04-05 23:48:01 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2003-07-28 13:13:50 +00:00
|
|
|
if (!citer) return NS_ERROR_UNEXPECTED;
|
2001-04-05 23:48:01 +00:00
|
|
|
|
|
|
|
nsString wrapped;
|
2003-07-28 13:13:50 +00:00
|
|
|
PRUint32 firstLineOffset = 0; // XXX need to reset this if there is a selection
|
|
|
|
rv = citer->Rewrap(current, wrapCol, firstLineOffset, aRespectNewlines,
|
|
|
|
wrapped);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-04-05 23:48:01 +00:00
|
|
|
|
|
|
|
if (isCollapsed) // rewrap the whole document
|
2003-07-28 13:13:50 +00:00
|
|
|
SelectAll();
|
2001-04-05 23:48:01 +00:00
|
|
|
|
2003-07-28 13:13:50 +00:00
|
|
|
return InsertTextWithQuotations(wrapped);
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::StripCites()
|
|
|
|
{
|
|
|
|
#ifdef DEBUG_akkana
|
|
|
|
printf("nsPlaintextEditor::StripCites()\n");
|
|
|
|
#endif
|
|
|
|
|
2003-07-28 13:13:50 +00:00
|
|
|
nsAutoString current;
|
|
|
|
PRBool isCollapsed;
|
|
|
|
nsresult rv = SharedOutputString(nsIDocumentEncoder::OutputFormatted,
|
|
|
|
&isCollapsed, current);
|
2001-04-05 23:48:01 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2003-07-28 13:13:50 +00:00
|
|
|
nsCOMPtr<nsICiter> citer = dont_AddRef(MakeACiter());
|
|
|
|
if (!citer) return NS_ERROR_UNEXPECTED;
|
2001-04-05 23:48:01 +00:00
|
|
|
|
|
|
|
nsString stripped;
|
2003-07-28 13:13:50 +00:00
|
|
|
rv = citer->StripCites(current, stripped);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
2001-04-05 23:48:01 +00:00
|
|
|
|
|
|
|
if (isCollapsed) // rewrap the whole document
|
|
|
|
{
|
|
|
|
rv = SelectAll();
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
}
|
|
|
|
|
2003-07-28 13:13:50 +00:00
|
|
|
return InsertText(stripped);
|
2001-04-05 23:48:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::GetEmbeddedObjects(nsISupportsArray** aNodeList)
|
|
|
|
{
|
|
|
|
*aNodeList = 0;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark nsIEditorIMESupport overrides
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2002-03-23 22:08:20 +00:00
|
|
|
nsPlaintextEditor::SetCompositionString(const nsAString& aCompositionString, nsIPrivateTextRangeList* aTextRangeList,nsTextEventReply* aReply)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2006-06-19 15:00:58 +00:00
|
|
|
if (!aTextRangeList && !aCompositionString.IsEmpty())
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2006-06-19 15:00:58 +00:00
|
|
|
NS_ERROR("aTextRangeList is null but the composition string is not null");
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
2002-06-13 20:35:12 +00:00
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!ps)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2003-07-28 13:13:50 +00:00
|
|
|
nsCOMPtr<nsISelection> selection;
|
|
|
|
nsresult result = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(result)) return result;
|
|
|
|
|
|
|
|
nsCOMPtr<nsICaret> caretP;
|
2006-06-19 15:00:58 +00:00
|
|
|
ps->GetCaret(getter_AddRefs(caretP));
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2006-06-19 15:00:58 +00:00
|
|
|
// We should return caret position if it is possible. Because this event
|
|
|
|
// dispatcher always expects to be returned the correct caret position.
|
|
|
|
// But in following cases, we don't need to process the composition string,
|
|
|
|
// so, we only need to return the caret position.
|
2002-12-22 01:51:14 +00:00
|
|
|
|
2006-06-19 15:00:58 +00:00
|
|
|
// aCompositionString.IsEmpty() && !mIMETextNode:
|
|
|
|
// Workaround for Windows IME bug 23558: We get every IME event twice.
|
|
|
|
// For escape keypress, this causes an empty string to be passed
|
|
|
|
// twice, which freaks out the editor.
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2006-06-19 15:00:58 +00:00
|
|
|
// aCompositionString.IsEmpty() && !aTextRangeList:
|
|
|
|
// Some Chinese IMEs for Linux are always composition string and text range
|
|
|
|
// list are empty when listing the Chinese characters. In this case,
|
|
|
|
// we don't need to process composition string too. See bug 271815.
|
2001-04-13 22:27:18 +00:00
|
|
|
|
2006-06-19 15:00:58 +00:00
|
|
|
if (!aCompositionString.IsEmpty() || (mIMETextNode && aTextRangeList))
|
|
|
|
{
|
|
|
|
mIMETextRangeList = aTextRangeList;
|
|
|
|
|
|
|
|
// XXX_kin: BEGIN HACK! HACK! HACK!
|
|
|
|
// XXX_kin:
|
|
|
|
// XXX_kin: This is lame! The IME stuff needs caret coordinates
|
|
|
|
// XXX_kin: synchronously, but the editor could be using async
|
|
|
|
// XXX_kin: updates (reflows and paints) for performance reasons.
|
|
|
|
// XXX_kin: In order to give IME what it needs, we have to temporarily
|
|
|
|
// XXX_kin: switch to sync updating during this call so that the
|
|
|
|
// XXX_kin: nsAutoPlaceHolderBatch can force sync reflows, paints,
|
|
|
|
// XXX_kin: and selection scrolling, so that we get back accurate
|
|
|
|
// XXX_kin: caret coordinates.
|
|
|
|
|
|
|
|
PRUint32 flags = 0;
|
|
|
|
PRBool restoreFlags = PR_FALSE;
|
|
|
|
|
|
|
|
if (NS_SUCCEEDED(GetFlags(&flags)) &&
|
|
|
|
(flags & nsIPlaintextEditor::eEditorUseAsyncUpdatesMask))
|
|
|
|
{
|
|
|
|
if (NS_SUCCEEDED(SetFlags(
|
|
|
|
flags & (~nsIPlaintextEditor::eEditorUseAsyncUpdatesMask))))
|
|
|
|
restoreFlags = PR_TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// XXX_kin: END HACK! HACK! HACK!
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2006-06-19 15:00:58 +00:00
|
|
|
// we need the nsAutoPlaceHolderBatch destructor called before hitting
|
|
|
|
// GetCaretCoordinates so the states in Frame system sync with content
|
|
|
|
// therefore, we put the nsAutoPlaceHolderBatch into a inner block
|
2002-03-07 14:34:35 +00:00
|
|
|
{
|
2006-06-19 15:00:58 +00:00
|
|
|
nsAutoPlaceHolderBatch batch(this, gIMETxnName);
|
|
|
|
|
|
|
|
SetIsIMEComposing(); // We set mIsIMEComposing properly.
|
|
|
|
|
|
|
|
result = InsertText(aCompositionString);
|
|
|
|
|
|
|
|
mIMEBufferLength = aCompositionString.Length();
|
|
|
|
|
|
|
|
if (caretP)
|
|
|
|
caretP->SetCaretDOMSelection(selection);
|
|
|
|
|
|
|
|
// second part of 23558 fix:
|
|
|
|
if (aCompositionString.IsEmpty())
|
|
|
|
mIMETextNode = nsnull;
|
2002-03-07 14:34:35 +00:00
|
|
|
}
|
|
|
|
|
2006-06-19 15:00:58 +00:00
|
|
|
// XXX_kin: BEGIN HACK! HACK! HACK!
|
|
|
|
// XXX_kin:
|
|
|
|
// XXX_kin: Restore the previous set of flags!
|
2002-06-13 20:35:12 +00:00
|
|
|
|
2006-06-19 15:00:58 +00:00
|
|
|
if (restoreFlags)
|
|
|
|
SetFlags(flags);
|
2002-06-13 20:35:12 +00:00
|
|
|
|
2006-06-19 15:00:58 +00:00
|
|
|
// XXX_kin: END HACK! HACK! HACK!
|
|
|
|
}
|
2002-06-13 20:35:12 +00:00
|
|
|
|
2003-07-28 13:13:50 +00:00
|
|
|
if (caretP)
|
|
|
|
{
|
2006-06-19 15:00:58 +00:00
|
|
|
result = caretP->GetCaretCoordinates(nsICaret::eIMECoordinates,
|
|
|
|
selection,
|
|
|
|
&(aReply->mCursorPosition),
|
|
|
|
&(aReply->mCursorIsCollapsed),
|
|
|
|
nsnull);
|
2003-07-28 13:13:50 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(result), "cannot get caret position");
|
|
|
|
}
|
2006-06-19 15:00:58 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::GetReconversionString(nsReconversionEventReply* aReply)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISelection> selection;
|
2003-07-28 13:13:50 +00:00
|
|
|
nsresult res = GetSelection(getter_AddRefs(selection));
|
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!selection) return NS_ERROR_FAILURE;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
2005-10-17 00:54:02 +00:00
|
|
|
// XXX get the first range in the selection. Since it is
|
2001-01-28 20:13:07 +00:00
|
|
|
// unclear what to do if reconversion happens with a
|
|
|
|
// multirange selection, we will ignore any additional ranges.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMRange> range;
|
|
|
|
res = selection->GetRangeAt(0, getter_AddRefs(range));
|
2003-07-28 13:13:50 +00:00
|
|
|
if (NS_FAILED(res)) return res;
|
|
|
|
if (!range) return NS_ERROR_FAILURE;
|
2001-01-28 20:13:07 +00:00
|
|
|
|
|
|
|
nsAutoString textValue;
|
|
|
|
res = range->ToString(textValue);
|
|
|
|
if (NS_FAILED(res))
|
|
|
|
return res;
|
|
|
|
|
2001-06-30 11:02:25 +00:00
|
|
|
aReply->mReconversionString = (PRUnichar*) nsMemory::Clone(textValue.get(),
|
2001-01-28 20:13:07 +00:00
|
|
|
(textValue.Length() + 1) * sizeof(PRUnichar));
|
|
|
|
if (!aReply->mReconversionString)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
2005-11-26 22:10:03 +00:00
|
|
|
if (textValue.IsEmpty())
|
|
|
|
return NS_OK;
|
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// delete the selection
|
2003-07-28 13:13:50 +00:00
|
|
|
return DeleteSelection(eNone);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark nsEditor overrides
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
/** All editor operations which alter the doc should be prefaced
|
|
|
|
* with a call to StartOperation, naming the action and direction */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::StartOperation(PRInt32 opID, nsIEditor::EDirection aDirection)
|
|
|
|
{
|
|
|
|
nsEditor::StartOperation(opID, aDirection); // will set mAction, mDirection
|
|
|
|
if (mRules) return mRules->BeforeEdit(mAction, mDirection);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/** All editor operations which alter the doc should be followed
|
|
|
|
* with a call to EndOperation */
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::EndOperation()
|
|
|
|
{
|
|
|
|
// post processing
|
|
|
|
nsresult res = NS_OK;
|
|
|
|
if (mRules) res = mRules->AfterEdit(mAction, mDirection);
|
|
|
|
nsEditor::EndOperation(); // will clear mAction, mDirection
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsPlaintextEditor::SelectEntireDocument(nsISelection *aSelection)
|
|
|
|
{
|
|
|
|
if (!aSelection || !mRules) { return NS_ERROR_NULL_POINTER; }
|
2005-03-24 19:00:01 +00:00
|
|
|
|
2001-01-28 20:13:07 +00:00
|
|
|
// is doc empty?
|
|
|
|
PRBool bDocIsEmpty;
|
2003-07-28 13:13:50 +00:00
|
|
|
if (NS_SUCCEEDED(mRules->DocumentIsEmpty(&bDocIsEmpty)) && bDocIsEmpty)
|
2001-01-28 20:13:07 +00:00
|
|
|
{
|
2005-03-24 19:00:01 +00:00
|
|
|
// get root node
|
|
|
|
nsIDOMElement *rootElement = GetRoot();
|
|
|
|
if (!rootElement)
|
|
|
|
return NS_ERROR_FAILURE;
|
2003-07-28 13:13:50 +00:00
|
|
|
|
|
|
|
// if it's empty don't select entire doc - that would select the bogus node
|
2005-03-24 19:00:01 +00:00
|
|
|
return aSelection->Collapse(rootElement, 0);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
2003-07-28 13:13:50 +00:00
|
|
|
|
|
|
|
return nsEditor::SelectEntireDocument(aSelection);
|
2001-01-28 20:13:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#pragma mark Random methods
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsPlaintextEditor::GetLayoutObject(nsIDOMNode *aNode, nsISupports **aLayoutObject)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIPresShell> ps = do_QueryReferent(mPresShellWeak);
|
|
|
|
if (!ps) return NS_ERROR_NOT_INITIALIZED;
|
|
|
|
|
2003-07-28 22:18:42 +00:00
|
|
|
nsresult result = NS_ERROR_NULL_POINTER;
|
2003-07-28 13:13:50 +00:00
|
|
|
if (aNode)
|
2001-01-28 20:13:07 +00:00
|
|
|
{ // get the content interface
|
|
|
|
nsCOMPtr<nsIContent> nodeAsContent( do_QueryInterface(aNode) );
|
|
|
|
if (nodeAsContent)
|
|
|
|
{ // get the frame from the content interface
|
|
|
|
//Note: frames are not ref counted, so don't use an nsCOMPtr
|
|
|
|
*aLayoutObject = nsnull;
|
|
|
|
result = ps->GetLayoutObjectFor(nodeAsContent, aLayoutObject);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#pragma mark -
|
|
|
|
#endif
|
|
|
|
|
2002-01-09 13:51:37 +00:00
|
|
|
nsresult
|
2002-01-25 10:16:52 +00:00
|
|
|
nsPlaintextEditor::SetAttributeOrEquivalent(nsIDOMElement * aElement,
|
2002-03-23 22:08:20 +00:00
|
|
|
const nsAString & aAttribute,
|
2002-09-17 12:04:59 +00:00
|
|
|
const nsAString & aValue,
|
|
|
|
PRBool aSuppressTransaction)
|
2002-01-09 13:51:37 +00:00
|
|
|
{
|
2002-03-15 15:33:29 +00:00
|
|
|
return nsEditor::SetAttribute(aElement, aAttribute, aValue);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsPlaintextEditor::RemoveAttributeOrEquivalent(nsIDOMElement * aElement,
|
2002-09-17 12:04:59 +00:00
|
|
|
const nsAString & aAttribute,
|
|
|
|
PRBool aSuppressTransaction)
|
2002-03-15 15:33:29 +00:00
|
|
|
{
|
|
|
|
return nsEditor::RemoveAttribute(aElement, aAttribute);
|
2002-01-09 13:51:37 +00:00
|
|
|
}
|