2001-09-26 02:53:13 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
2006-04-07 02:48:55 +04:00
|
|
|
/* vim: set ts=2 sw=2 et tw=78: */
|
2012-05-21 15:12:37 +04: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/. */
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-07-13 10:33:42 +04:00
|
|
|
#include <string.h>
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2016-07-07 05:49:42 +03:00
|
|
|
#include "EditorUtils.h"
|
|
|
|
#include "mozilla/dom/DataTransfer.h"
|
2013-04-10 18:15:54 +04:00
|
|
|
#include "mozilla/dom/DocumentFragment.h"
|
2016-07-07 05:49:42 +03:00
|
|
|
#include "mozilla/dom/DOMStringList.h"
|
2015-08-05 15:28:27 +03:00
|
|
|
#include "mozilla/OwningNonNull.h"
|
2013-12-09 06:52:54 +04:00
|
|
|
#include "mozilla/ArrayUtils.h"
|
2012-10-04 10:45:16 +04:00
|
|
|
#include "mozilla/Base64.h"
|
2013-10-22 17:27:36 +04:00
|
|
|
#include "mozilla/BasicEvents.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "mozilla/Preferences.h"
|
2014-04-10 20:09:40 +04:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsAString.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsCRTGlue.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
2016-05-20 02:47:44 +03:00
|
|
|
#include "nsIScriptError.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsDebug.h"
|
|
|
|
#include "nsDependentSubstring.h"
|
|
|
|
#include "nsEditRules.h"
|
|
|
|
#include "nsEditor.h"
|
|
|
|
#include "nsError.h"
|
|
|
|
#include "nsGkAtoms.h"
|
2001-01-28 23:13:07 +03:00
|
|
|
#include "nsHTMLEditUtils.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsHTMLEditor.h"
|
|
|
|
#include "nsIClipboard.h"
|
2001-01-28 23:13:07 +03:00
|
|
|
#include "nsIContent.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsIContentFilter.h"
|
|
|
|
#include "nsIDOMComment.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
2001-01-28 23:13:07 +03:00
|
|
|
#include "nsIDOMDocumentFragment.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsIDOMElement.h"
|
|
|
|
#include "nsIDOMHTMLAnchorElement.h"
|
|
|
|
#include "nsIDOMHTMLEmbedElement.h"
|
2004-03-04 21:47:49 +03:00
|
|
|
#include "nsIDOMHTMLFrameElement.h"
|
|
|
|
#include "nsIDOMHTMLIFrameElement.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsIDOMHTMLImageElement.h"
|
2004-03-04 21:47:49 +03:00
|
|
|
#include "nsIDOMHTMLInputElement.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsIDOMHTMLLinkElement.h"
|
|
|
|
#include "nsIDOMHTMLObjectElement.h"
|
2004-03-04 21:47:49 +03:00
|
|
|
#include "nsIDOMHTMLScriptElement.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIEditor.h"
|
|
|
|
#include "nsIEditorIMESupport.h"
|
|
|
|
#include "nsIEditorMailSupport.h"
|
|
|
|
#include "nsIFile.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIMIMEService.h"
|
2014-02-28 03:04:46 +04:00
|
|
|
#include "nsNameSpaceManager.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsINode.h"
|
2012-03-19 12:16:20 +04:00
|
|
|
#include "nsIParserUtils.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
#include "nsIPlaintextEditor.h"
|
|
|
|
#include "nsISupportsImpl.h"
|
|
|
|
#include "nsISupportsPrimitives.h"
|
|
|
|
#include "nsISupportsUtils.h"
|
|
|
|
#include "nsITransferable.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsIVariant.h"
|
|
|
|
#include "nsLinebreakConverter.h"
|
|
|
|
#include "nsLiteralString.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
#include "nsPlaintextEditor.h"
|
|
|
|
#include "nsRange.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsSelectionState.h"
|
|
|
|
#include "nsServiceManagerUtils.h"
|
|
|
|
#include "nsStreamUtils.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsStringFwd.h"
|
|
|
|
#include "nsStringIterator.h"
|
|
|
|
#include "nsSubstringTuple.h"
|
|
|
|
#include "nsTextEditRules.h"
|
|
|
|
#include "nsTextEditUtils.h"
|
|
|
|
#include "nsTreeSanitizer.h"
|
|
|
|
#include "nsWSRunObject.h"
|
|
|
|
#include "nsXPCOM.h"
|
|
|
|
#include "nscore.h"
|
2014-09-21 20:46:30 +04:00
|
|
|
#include "nsContentUtils.h"
|
2012-07-13 10:33:42 +04:00
|
|
|
|
|
|
|
class nsIAtom;
|
|
|
|
class nsILoadContext;
|
|
|
|
class nsISupports;
|
2011-06-17 04:59:29 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2013-04-10 18:15:54 +04:00
|
|
|
using namespace mozilla::dom;
|
2003-04-15 18:07:30 +04:00
|
|
|
|
2006-04-07 02:48:55 +04:00
|
|
|
#define kInsertCookie "_moz_Insert Here_moz_"
|
|
|
|
|
2002-11-22 06:30:59 +03:00
|
|
|
// some little helpers
|
2015-05-28 18:58:42 +03:00
|
|
|
static bool FindIntegerAfterString(const char *aLeadingString,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCString &aCStr, int32_t &foundNumber);
|
2002-11-22 06:30:59 +03:00
|
|
|
static nsresult RemoveFragComments(nsCString &theStr);
|
2015-05-13 18:58:25 +03:00
|
|
|
static void RemoveBodyAndHead(nsINode& aNode);
|
2006-04-26 00:13:46 +04:00
|
|
|
static nsresult FindTargetNode(nsIDOMNode *aStart, nsCOMPtr<nsIDOMNode> &aResult);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2015-01-02 09:14:13 +03:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::LoadHTML(const nsAString & aInputString)
|
2001-10-22 10:22:52 +04:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(mRules, NS_ERROR_NOT_INITIALIZED);
|
2001-10-22 10:22:52 +04:00
|
|
|
|
|
|
|
// force IME commit; set up rules sniffing and batching
|
|
|
|
ForceCompositionEnd();
|
2016-06-23 12:46:58 +03:00
|
|
|
AutoEditBatch beginBatching(this);
|
2016-06-23 12:15:42 +03:00
|
|
|
AutoRules beginRulesSniffing(this, EditAction::loadHTML, nsIEditor::eNext);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-10-22 10:22:52 +04:00
|
|
|
// Get selection
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
2012-05-22 13:37:17 +04:00
|
|
|
NS_ENSURE_STATE(selection);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2012-08-12 22:28:26 +04:00
|
|
|
nsTextRulesInfo ruleInfo(EditAction::loadHTML);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
2013-03-23 07:47:57 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
2012-05-22 13:37:17 +04:00
|
|
|
nsresult rv = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (cancel) {
|
|
|
|
return NS_OK; // rules canceled the operation
|
|
|
|
}
|
|
|
|
|
2001-11-09 18:16:03 +03:00
|
|
|
if (!handled)
|
2001-10-27 01:21:04 +04:00
|
|
|
{
|
2001-11-09 18:16:03 +03:00
|
|
|
// Delete Selection, but only if it isn't collapsed, see bug #106269
|
2012-05-18 12:29:39 +04:00
|
|
|
if (!selection->Collapsed()) {
|
2012-05-10 18:54:33 +04:00
|
|
|
rv = DeleteSelection(eNone, eStrip);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-11-09 18:16:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the first range in the selection, for context:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRange> range = selection->GetRangeAt(0);
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(range, NS_ERROR_NULL_POINTER);
|
2001-11-09 18:16:03 +03:00
|
|
|
|
|
|
|
// create fragment for pasted html
|
|
|
|
nsCOMPtr<nsIDOMDocumentFragment> docfrag;
|
|
|
|
{
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = range->CreateContextualFragment(aInputString, getter_AddRefs(docfrag));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-11-09 18:16:03 +03:00
|
|
|
}
|
|
|
|
// put the fragment into the document
|
|
|
|
nsCOMPtr<nsIDOMNode> parent, junk;
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = range->GetStartContainer(getter_AddRefs(parent));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(parent, NS_ERROR_NULL_POINTER);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t childOffset;
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = range->GetStartOffset(&childOffset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-11-09 18:16:03 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> nodeToInsert;
|
2001-10-22 10:22:52 +04:00
|
|
|
docfrag->GetFirstChild(getter_AddRefs(nodeToInsert));
|
2001-11-09 18:16:03 +03:00
|
|
|
while (nodeToInsert)
|
|
|
|
{
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = InsertNode(nodeToInsert, parent, childOffset++);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-11-09 18:16:03 +03:00
|
|
|
docfrag->GetFirstChild(getter_AddRefs(nodeToInsert));
|
|
|
|
}
|
2001-10-22 10:22:52 +04:00
|
|
|
}
|
2001-11-09 18:16:03 +03:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
return mRules->DidDoAction(selection, &ruleInfo, rv);
|
2001-10-22 10:22:52 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-15 01:11:14 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::InsertHTML(const nsAString & aInString)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2004-01-30 01:04:45 +03:00
|
|
|
const nsAFlatString& empty = EmptyString();
|
|
|
|
|
|
|
|
return InsertHTMLWithContext(aInString, empty, empty, empty,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr, nullptr, 0, true);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-15 01:11:14 +03:00
|
|
|
nsresult
|
2003-06-12 18:51:37 +04:00
|
|
|
nsHTMLEditor::InsertHTMLWithContext(const nsAString & aInputString,
|
|
|
|
const nsAString & aContextStr,
|
|
|
|
const nsAString & aInfoStr,
|
|
|
|
const nsAString & aFlavor,
|
|
|
|
nsIDOMDocument *aSourceDoc,
|
|
|
|
nsIDOMNode *aDestNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aDestOffset,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aDeleteSelection)
|
2010-09-27 21:21:34 +04:00
|
|
|
{
|
|
|
|
return DoInsertHTMLWithContext(aInputString, aContextStr, aInfoStr,
|
|
|
|
aFlavor, aSourceDoc, aDestNode, aDestOffset, aDeleteSelection,
|
2013-04-04 18:27:41 +04:00
|
|
|
/* trusted input */ true, /* clear style */ false);
|
2010-09-27 21:21:34 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::DoInsertHTMLWithContext(const nsAString & aInputString,
|
|
|
|
const nsAString & aContextStr,
|
|
|
|
const nsAString & aInfoStr,
|
|
|
|
const nsAString & aFlavor,
|
|
|
|
nsIDOMDocument *aSourceDoc,
|
|
|
|
nsIDOMNode *aDestNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aDestOffset,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aDeleteSelection,
|
2013-04-04 18:27:41 +04:00
|
|
|
bool aTrustedInput,
|
|
|
|
bool aClearStyle)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(mRules, NS_ERROR_NOT_INITIALIZED);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-09-16 14:13:31 +04:00
|
|
|
// Prevent the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// force IME commit; set up rules sniffing and batching
|
|
|
|
ForceCompositionEnd();
|
2016-06-23 12:46:58 +03:00
|
|
|
AutoEditBatch beginBatching(this);
|
2016-06-23 12:15:42 +03:00
|
|
|
AutoRules beginRulesSniffing(this, EditAction::htmlPaste, nsIEditor::eNext);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Get selection
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
2012-05-10 16:00:14 +04:00
|
|
|
NS_ENSURE_STATE(selection);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// create a dom document fragment that represents the structure to paste
|
2006-04-26 00:13:46 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> fragmentAsNode, streamStartParent, streamEndParent;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t streamStartOffset = 0, streamEndOffset = 0;
|
2005-02-02 00:12:53 +03:00
|
|
|
|
2012-05-10 16:00:14 +04:00
|
|
|
nsresult rv = CreateDOMFragmentFromPaste(aInputString, aContextStr, aInfoStr,
|
|
|
|
address_of(fragmentAsNode),
|
|
|
|
address_of(streamStartParent),
|
|
|
|
address_of(streamEndParent),
|
|
|
|
&streamStartOffset,
|
|
|
|
&streamEndOffset,
|
|
|
|
aTrustedInput);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2006-04-26 00:13:46 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> targetNode, tempNode;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t targetOffset=0;
|
2006-04-07 02:48:55 +04:00
|
|
|
|
2003-04-15 18:07:30 +04:00
|
|
|
if (!aDestNode)
|
|
|
|
{
|
|
|
|
// if caller didn't provide the destination/target node,
|
|
|
|
// fetch the paste insertion point from our selection
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = GetStartNodeAndOffset(selection, getter_AddRefs(targetNode), &targetOffset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-11-16 23:45:49 +03:00
|
|
|
if (!targetNode || !IsEditable(targetNode)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2003-04-15 18:07:30 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
targetNode = aDestNode;
|
|
|
|
targetOffset = aDestOffset;
|
|
|
|
}
|
2006-04-07 02:48:55 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool doContinue = true;
|
2003-04-15 18:07:30 +04:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = DoContentFilterCallback(aFlavor, aSourceDoc, aDeleteSelection,
|
|
|
|
(nsIDOMNode **)address_of(fragmentAsNode),
|
|
|
|
(nsIDOMNode **)address_of(streamStartParent),
|
|
|
|
&streamStartOffset,
|
|
|
|
(nsIDOMNode **)address_of(streamEndParent),
|
|
|
|
&streamEndOffset,
|
|
|
|
(nsIDOMNode **)address_of(targetNode),
|
|
|
|
&targetOffset, &doContinue);
|
2003-04-15 18:07:30 +04:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(doContinue, NS_OK);
|
2003-04-15 18:07:30 +04:00
|
|
|
|
|
|
|
// if we have a destination / target node, we want to insert there
|
|
|
|
// rather than in place of the selection
|
|
|
|
// ignore aDeleteSelection here if no aDestNode since deletion will
|
|
|
|
// also occur later; this block is intended to cover the various
|
|
|
|
// scenarios where we are dropping in an editor (and may want to delete
|
|
|
|
// the selection before collapsing the selection in the new destination)
|
|
|
|
if (aDestNode)
|
|
|
|
{
|
|
|
|
if (aDeleteSelection)
|
|
|
|
{
|
|
|
|
// Use an auto tracker so that our drop point is correctly
|
|
|
|
// positioned after the delete.
|
|
|
|
nsAutoTrackDOMPoint tracker(mRangeUpdater, &targetNode, &targetOffset);
|
2012-05-10 18:54:33 +04:00
|
|
|
rv = DeleteSelection(eNone, eStrip);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-04-15 18:07:30 +04:00
|
|
|
}
|
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = selection->Collapse(targetNode, targetOffset);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2003-04-15 18:07:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// we need to recalculate various things based on potentially new offsets
|
|
|
|
// this is work to be completed at a later date (probably by jfrancis)
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// make a list of what nodes in docFrag we need to move
|
2015-04-24 14:27:35 +03:00
|
|
|
nsTArray<OwningNonNull<nsINode>> nodeList;
|
|
|
|
nsCOMPtr<nsINode> fragmentAsNodeNode = do_QueryInterface(fragmentAsNode);
|
|
|
|
NS_ENSURE_STATE(fragmentAsNodeNode || !fragmentAsNode);
|
|
|
|
nsCOMPtr<nsINode> streamStartParentNode =
|
|
|
|
do_QueryInterface(streamStartParent);
|
|
|
|
NS_ENSURE_STATE(streamStartParentNode || !streamStartParent);
|
|
|
|
nsCOMPtr<nsINode> streamEndParentNode =
|
|
|
|
do_QueryInterface(streamEndParent);
|
|
|
|
NS_ENSURE_STATE(streamEndParentNode || !streamEndParent);
|
|
|
|
CreateListOfNodesToPaste(*static_cast<DocumentFragment*>(fragmentAsNodeNode.get()),
|
|
|
|
nodeList,
|
|
|
|
streamStartParentNode, streamStartOffset,
|
|
|
|
streamEndParentNode, streamEndOffset);
|
|
|
|
|
|
|
|
if (nodeList.Length() == 0) {
|
2015-05-14 15:02:00 +03:00
|
|
|
// We aren't inserting anything, but if aDeleteSelection is set, we do want
|
|
|
|
// to delete everything.
|
|
|
|
if (aDeleteSelection) {
|
|
|
|
return DeleteSelection(eNone, eStrip);
|
|
|
|
}
|
2006-03-24 04:52:07 +03:00
|
|
|
return NS_OK;
|
2012-03-27 01:58:43 +04:00
|
|
|
}
|
2005-06-02 02:39:35 +04:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
// Are there any table elements in the list?
|
2001-01-28 23:13:07 +03:00
|
|
|
// node and offset for insertion
|
|
|
|
nsCOMPtr<nsIDOMNode> parentNode;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t offsetOfNewNode;
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// check for table cell selection mode
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cellSelectionMode = false;
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMElement> cell;
|
2012-07-30 18:20:58 +04:00
|
|
|
rv = GetFirstSelectedCell(nullptr, getter_AddRefs(cell));
|
2012-03-27 01:58:43 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && cell)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
cellSelectionMode = true;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
if (cellSelectionMode)
|
|
|
|
{
|
|
|
|
// do we have table content to paste? If so, we want to delete
|
|
|
|
// the selected table cells and replace with new table elements;
|
|
|
|
// but if not we want to delete _contents_ of cells and replace
|
2015-05-28 18:58:42 +03:00
|
|
|
// with non-table elements. Use cellSelectionMode bool to
|
2001-01-28 23:13:07 +03:00
|
|
|
// indicate results.
|
2015-04-24 14:27:35 +03:00
|
|
|
if (!nsHTMLEditUtils::IsTableElement(nodeList[0])) {
|
2011-10-17 18:59:28 +04:00
|
|
|
cellSelectionMode = false;
|
2015-04-24 14:27:35 +03:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!cellSelectionMode)
|
|
|
|
{
|
2012-06-21 16:54:26 +04:00
|
|
|
rv = DeleteSelectionAndPrepareToCreateNode();
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2013-04-04 18:27:41 +04:00
|
|
|
if (aClearStyle) {
|
|
|
|
// pasting does not inherit local inline styles
|
2016-05-01 16:11:40 +03:00
|
|
|
nsCOMPtr<nsINode> tmpNode = selection->GetAnchorNode();
|
2013-12-17 18:12:33 +04:00
|
|
|
int32_t tmpOffset = static_cast<int32_t>(selection->AnchorOffset());
|
2013-04-04 18:27:41 +04:00
|
|
|
rv = ClearStyle(address_of(tmpNode), &tmpOffset, nullptr, nullptr);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2016-07-07 07:27:31 +03:00
|
|
|
// Delete whole cells: we will replace with new table content.
|
|
|
|
|
|
|
|
// Braces for artificial block to scope AutoSelectionRestorer.
|
|
|
|
// Save current selection since DeleteTableCell() perturbs it.
|
|
|
|
{
|
|
|
|
AutoSelectionRestorer selectionRestorer(selection, this);
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = DeleteTableCell(1);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2004-03-04 21:47:49 +03:00
|
|
|
// collapse selection to beginning of deleted table content
|
2001-01-28 23:13:07 +03:00
|
|
|
selection->CollapseToStart();
|
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// give rules a chance to handle or cancel
|
2012-08-12 22:28:26 +04:00
|
|
|
nsTextRulesInfo ruleInfo(EditAction::insertElement);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (cancel) {
|
|
|
|
return NS_OK; // rules canceled the operation
|
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
if (!handled)
|
|
|
|
{
|
2012-03-27 01:58:43 +04:00
|
|
|
// The rules code (WillDoAction above) might have changed the selection.
|
2001-01-28 23:13:07 +03:00
|
|
|
// refresh our memory...
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = GetStartNodeAndOffset(selection, getter_AddRefs(parentNode), &offsetOfNewNode);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(parentNode, NS_ERROR_FAILURE);
|
2003-04-16 00:53:15 +04:00
|
|
|
|
|
|
|
// Adjust position based on the first node we are going to insert.
|
2016-05-01 17:59:05 +03:00
|
|
|
NormalizeEOLInsertPosition(nodeList[0], address_of(parentNode),
|
|
|
|
&offsetOfNewNode);
|
2003-04-16 00:53:15 +04:00
|
|
|
|
2002-05-14 04:05:37 +04:00
|
|
|
// if there are any invisible br's after our insertion point, remove them.
|
|
|
|
// this is because if there is a br at end of what we paste, it will make
|
|
|
|
// the invisible br visible.
|
|
|
|
nsWSRunObject wsObj(this, parentNode, offsetOfNewNode);
|
2014-08-26 17:13:22 +04:00
|
|
|
if (wsObj.mEndReasonNode &&
|
|
|
|
nsTextEditUtils::IsBreak(wsObj.mEndReasonNode) &&
|
|
|
|
!IsVisBreak(wsObj.mEndReasonNode)) {
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = DeleteNode(wsObj.mEndReasonNode);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-05-14 04:05:37 +04:00
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
|
|
|
|
// Remember if we are in a link.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool bStartedInLink = IsInLink(parentNode);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
|
|
|
// Are we in a text node? If so, split it.
|
2001-01-28 23:13:07 +03:00
|
|
|
if (IsTextNode(parentNode))
|
|
|
|
{
|
2015-08-05 18:10:09 +03:00
|
|
|
nsCOMPtr<nsIContent> parentContent = do_QueryInterface(parentNode);
|
|
|
|
NS_ENSURE_STATE(parentContent || !parentNode);
|
|
|
|
offsetOfNewNode = SplitNodeDeep(*parentContent, *parentContent,
|
|
|
|
offsetOfNewNode);
|
|
|
|
NS_ENSURE_STATE(offsetOfNewNode != -1);
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> temp;
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = parentNode->GetParentNode(getter_AddRefs(temp));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
parentNode = temp;
|
|
|
|
}
|
|
|
|
|
2001-06-16 02:29:07 +04:00
|
|
|
// build up list of parents of first node in list that are either
|
|
|
|
// lists or tables. First examine front of paste node list.
|
2015-04-24 14:27:35 +03:00
|
|
|
nsTArray<OwningNonNull<Element>> startListAndTableArray;
|
|
|
|
GetListAndTableParents(StartOrEnd::start, nodeList,
|
|
|
|
startListAndTableArray);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// remember number of lists and tables above us
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t highWaterMark = -1;
|
2015-04-24 14:27:35 +03:00
|
|
|
if (startListAndTableArray.Length() > 0) {
|
|
|
|
highWaterMark = DiscoverPartialListsAndTables(nodeList,
|
|
|
|
startListAndTableArray);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2001-06-16 02:29:07 +04:00
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
// if we have pieces of tables or lists to be inserted, let's force the paste
|
|
|
|
// to deal with table elements right away, so that it doesn't orphan some
|
2001-01-28 23:13:07 +03:00
|
|
|
// table or list contents outside the table or list.
|
|
|
|
if (highWaterMark >= 0)
|
|
|
|
{
|
2015-04-24 14:27:35 +03:00
|
|
|
ReplaceOrphanedStructure(StartOrEnd::start, nodeList,
|
|
|
|
startListAndTableArray, highWaterMark);
|
2001-06-16 02:29:07 +04:00
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-06-16 02:29:07 +04:00
|
|
|
// Now go through the same process again for the end of the paste node list.
|
2015-04-24 14:27:35 +03:00
|
|
|
nsTArray<OwningNonNull<Element>> endListAndTableArray;
|
|
|
|
GetListAndTableParents(StartOrEnd::end, nodeList, endListAndTableArray);
|
2001-06-16 02:29:07 +04:00
|
|
|
highWaterMark = -1;
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-06-16 02:29:07 +04:00
|
|
|
// remember number of lists and tables above us
|
2015-04-24 14:27:35 +03:00
|
|
|
if (endListAndTableArray.Length() > 0) {
|
|
|
|
highWaterMark = DiscoverPartialListsAndTables(nodeList,
|
|
|
|
endListAndTableArray);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-06-16 02:29:07 +04:00
|
|
|
// don't orphan partial list or table structure
|
|
|
|
if (highWaterMark >= 0)
|
|
|
|
{
|
2015-04-24 14:27:35 +03:00
|
|
|
ReplaceOrphanedStructure(StartOrEnd::end, nodeList,
|
|
|
|
endListAndTableArray, highWaterMark);
|
2001-06-16 02:29:07 +04:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Loop over the node list and paste the nodes:
|
2001-06-20 06:05:37 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> parentBlock, lastInsertNode, insertedContextParent;
|
2015-04-24 14:27:35 +03:00
|
|
|
int32_t listCount = nodeList.Length();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t j;
|
2016-05-01 17:59:05 +03:00
|
|
|
nsCOMPtr<nsINode> parentNodeNode = do_QueryInterface(parentNode);
|
|
|
|
NS_ENSURE_STATE(parentNodeNode || !parentNode);
|
|
|
|
if (IsBlockNode(parentNodeNode))
|
2001-06-20 06:05:37 +04:00
|
|
|
parentBlock = parentNode;
|
|
|
|
else
|
|
|
|
parentBlock = GetBlockNodeParent(parentNode);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
for (j=0; j<listCount; j++)
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool bDidInsert = false;
|
2015-04-24 14:27:35 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> curNode = nodeList[j]->AsDOMNode();
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
NS_ENSURE_TRUE(curNode, NS_ERROR_FAILURE);
|
|
|
|
NS_ENSURE_TRUE(curNode != fragmentAsNode, NS_ERROR_FAILURE);
|
2001-04-06 03:48:01 +04:00
|
|
|
NS_ENSURE_TRUE(!nsTextEditUtils::IsBody(curNode), NS_ERROR_FAILURE);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
if (insertedContextParent)
|
|
|
|
{
|
2015-05-28 18:58:42 +03:00
|
|
|
// if we had to insert something higher up in the paste hierarchy, we want to
|
2001-01-28 23:13:07 +03:00
|
|
|
// skip any further paste nodes that descend from that. Else we will paste twice.
|
2016-07-07 05:49:42 +03:00
|
|
|
if (EditorUtils::IsDescendantOf(curNode, insertedContextParent)) {
|
2001-01-28 23:13:07 +03:00
|
|
|
continue;
|
2016-07-07 05:49:42 +03:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// give the user a hand on table element insertion. if they have
|
|
|
|
// a table or table row on the clipboard, and are trying to insert
|
|
|
|
// into a table or table row, insert the appropriate children instead.
|
|
|
|
if ( (nsHTMLEditUtils::IsTableRow(curNode) && nsHTMLEditUtils::IsTableRow(parentNode))
|
|
|
|
&& (nsHTMLEditUtils::IsTable(curNode) || nsHTMLEditUtils::IsTable(parentNode)) )
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> child;
|
|
|
|
curNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
while (child)
|
|
|
|
{
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = InsertNodeAtPoint(child, address_of(parentNode), &offsetOfNewNode, true);
|
|
|
|
if (NS_FAILED(rv))
|
2009-02-21 05:51:52 +03:00
|
|
|
break;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
bDidInsert = true;
|
2009-02-21 05:51:52 +03:00
|
|
|
lastInsertNode = child;
|
|
|
|
offsetOfNewNode++;
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
curNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
}
|
|
|
|
}
|
2001-06-16 02:29:07 +04:00
|
|
|
// give the user a hand on list insertion. if they have
|
|
|
|
// a list on the clipboard, and are trying to insert
|
|
|
|
// into a list or list item, insert the appropriate children instead,
|
|
|
|
// ie, merge the lists instead of pasting in a sublist.
|
2015-05-28 18:58:42 +03:00
|
|
|
else if (nsHTMLEditUtils::IsList(curNode) &&
|
2001-06-16 02:29:07 +04:00
|
|
|
(nsHTMLEditUtils::IsList(parentNode) || nsHTMLEditUtils::IsListItem(parentNode)) )
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIDOMNode> child, tmp;
|
|
|
|
curNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
while (child)
|
|
|
|
{
|
|
|
|
if (nsHTMLEditUtils::IsListItem(child) || nsHTMLEditUtils::IsList(child))
|
|
|
|
{
|
2012-03-27 01:58:43 +04:00
|
|
|
// Check if we are pasting into empty list item. If so
|
2001-08-22 09:32:44 +04:00
|
|
|
// delete it and paste into parent list instead.
|
|
|
|
if (nsHTMLEditUtils::IsListItem(parentNode))
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool isEmpty;
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = IsEmptyNode(parentNode, &isEmpty, true);
|
|
|
|
if (NS_SUCCEEDED(rv) && isEmpty)
|
2001-08-22 09:32:44 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t newOffset;
|
2012-07-09 04:45:12 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> listNode = GetNodeLocation(parentNode, &newOffset);
|
2001-08-22 09:32:44 +04:00
|
|
|
if (listNode)
|
|
|
|
{
|
|
|
|
DeleteNode(parentNode);
|
|
|
|
parentNode = listNode;
|
|
|
|
offsetOfNewNode = newOffset;
|
|
|
|
}
|
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
}
|
|
|
|
rv = InsertNodeAtPoint(child, address_of(parentNode), &offsetOfNewNode, true);
|
|
|
|
if (NS_FAILED(rv))
|
2009-02-21 05:51:52 +03:00
|
|
|
break;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
bDidInsert = true;
|
2009-02-21 05:51:52 +03:00
|
|
|
lastInsertNode = child;
|
|
|
|
offsetOfNewNode++;
|
2001-06-16 02:29:07 +04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
curNode->RemoveChild(child, getter_AddRefs(tmp));
|
|
|
|
}
|
|
|
|
curNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2014-04-25 14:34:42 +04:00
|
|
|
} else if (parentBlock && nsHTMLEditUtils::IsPre(parentBlock) &&
|
|
|
|
nsHTMLEditUtils::IsPre(curNode)) {
|
|
|
|
// Check for pre's going into pre's.
|
2001-06-20 06:05:37 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> child, tmp;
|
|
|
|
curNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
while (child)
|
|
|
|
{
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = InsertNodeAtPoint(child, address_of(parentNode), &offsetOfNewNode, true);
|
|
|
|
if (NS_FAILED(rv))
|
2009-02-21 05:51:52 +03:00
|
|
|
break;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
bDidInsert = true;
|
2009-02-21 05:51:52 +03:00
|
|
|
lastInsertNode = child;
|
|
|
|
offsetOfNewNode++;
|
|
|
|
|
2001-06-20 06:05:37 +04:00
|
|
|
curNode->GetFirstChild(getter_AddRefs(child));
|
|
|
|
}
|
|
|
|
}
|
2009-02-21 05:51:52 +03:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
if (!bDidInsert || NS_FAILED(rv))
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
// try to insert
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = InsertNodeAtPoint(curNode, address_of(parentNode), &offsetOfNewNode, true);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
bDidInsert = true;
|
2001-01-28 23:13:07 +03:00
|
|
|
lastInsertNode = curNode;
|
|
|
|
}
|
2009-04-01 05:05:44 +04:00
|
|
|
|
2009-02-21 05:51:52 +03:00
|
|
|
// Assume failure means no legal parent in the document hierarchy,
|
|
|
|
// try again with the parent of curNode in the paste hierarchy.
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> parent;
|
2012-03-27 01:58:43 +04:00
|
|
|
while (NS_FAILED(rv) && curNode)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
curNode->GetParentNode(getter_AddRefs(parent));
|
2001-04-06 03:48:01 +04:00
|
|
|
if (parent && !nsTextEditUtils::IsBody(parent))
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = InsertNodeAtPoint(parent, address_of(parentNode), &offsetOfNewNode, true);
|
|
|
|
if (NS_SUCCEEDED(rv))
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
bDidInsert = true;
|
2001-01-28 23:13:07 +03:00
|
|
|
insertedContextParent = parent;
|
2011-07-26 21:59:50 +04:00
|
|
|
lastInsertNode = GetChildAt(parentNode, offsetOfNewNode);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
curNode = parent;
|
|
|
|
}
|
|
|
|
}
|
2009-04-01 05:05:44 +04:00
|
|
|
if (lastInsertNode)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2012-07-09 04:45:12 +04:00
|
|
|
parentNode = GetNodeLocation(lastInsertNode, &offsetOfNewNode);
|
2001-01-28 23:13:07 +03:00
|
|
|
offsetOfNewNode++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Now collapse the selection to the end of what we just inserted:
|
2015-05-28 18:58:42 +03:00
|
|
|
if (lastInsertNode)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2002-05-14 04:05:37 +04:00
|
|
|
// set selection to the end of what we just pasted.
|
2014-05-02 16:15:27 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> selNode, tmp, highTable;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t selOffset;
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2005-11-25 11:16:51 +03:00
|
|
|
// but don't cross tables
|
2002-05-14 04:05:37 +04:00
|
|
|
if (!nsHTMLEditUtils::IsTable(lastInsertNode))
|
|
|
|
{
|
2014-11-02 15:04:13 +03:00
|
|
|
nsCOMPtr<nsINode> lastInsertNode_ = do_QueryInterface(lastInsertNode);
|
|
|
|
NS_ENSURE_STATE(lastInsertNode_ || !lastInsertNode);
|
|
|
|
selNode = GetAsDOMNode(GetLastEditableLeaf(*lastInsertNode_));
|
2002-05-14 04:05:37 +04:00
|
|
|
tmp = selNode;
|
|
|
|
while (tmp && (tmp != lastInsertNode))
|
|
|
|
{
|
|
|
|
if (nsHTMLEditUtils::IsTable(tmp))
|
|
|
|
highTable = tmp;
|
|
|
|
nsCOMPtr<nsIDOMNode> parent = tmp;
|
|
|
|
tmp->GetParentNode(getter_AddRefs(parent));
|
|
|
|
tmp = parent;
|
|
|
|
}
|
|
|
|
if (highTable)
|
|
|
|
selNode = highTable;
|
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
if (!selNode)
|
2002-05-14 04:05:37 +04:00
|
|
|
selNode = lastInsertNode;
|
2012-03-27 01:58:43 +04:00
|
|
|
if (IsTextNode(selNode) || (IsContainer(selNode) && !nsHTMLEditUtils::IsTable(selNode)))
|
2002-05-14 04:05:37 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
rv = GetLengthOfDOMNode(selNode, (uint32_t&)selOffset);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-05-14 04:05:37 +04:00
|
|
|
}
|
|
|
|
else // we need to find a container for selection. Look up.
|
|
|
|
{
|
|
|
|
tmp = selNode;
|
2012-07-09 04:45:12 +04:00
|
|
|
selNode = GetNodeLocation(tmp, &selOffset);
|
2014-11-11 20:47:36 +03:00
|
|
|
// selNode might be null in case a mutation listener removed
|
|
|
|
// the stuff we just inserted from the DOM.
|
|
|
|
NS_ENSURE_STATE(selNode);
|
2002-05-14 04:05:37 +04:00
|
|
|
++selOffset; // want to be *after* last leaf node in paste
|
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2005-11-25 11:16:51 +03:00
|
|
|
// make sure we don't end up with selection collapsed after an invisible break node
|
2002-05-14 04:05:37 +04:00
|
|
|
nsWSRunObject wsRunObj(this, selNode, selOffset);
|
2014-05-02 16:15:27 +04:00
|
|
|
nsCOMPtr<nsINode> visNode;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t outVisOffset=0;
|
2012-07-13 10:33:43 +04:00
|
|
|
WSType visType;
|
2014-05-02 16:15:27 +04:00
|
|
|
nsCOMPtr<nsINode> selNode_(do_QueryInterface(selNode));
|
|
|
|
wsRunObj.PriorVisibleNode(selNode_, selOffset, address_of(visNode),
|
2012-06-01 15:03:17 +04:00
|
|
|
&outVisOffset, &visType);
|
2012-07-13 10:33:43 +04:00
|
|
|
if (visType == WSType::br) {
|
2015-05-28 18:58:42 +03:00
|
|
|
// we are after a break. Is it visible? Despite the name,
|
2002-05-14 04:05:37 +04:00
|
|
|
// PriorVisibleNode does not make that determination for breaks.
|
|
|
|
// It also may not return the break in visNode. We have to pull it
|
|
|
|
// out of the nsWSRunObject's state.
|
|
|
|
if (!IsVisBreak(wsRunObj.mStartReasonNode))
|
|
|
|
{
|
2005-11-25 11:16:51 +03:00
|
|
|
// don't leave selection past an invisible break;
|
2002-05-14 04:05:37 +04:00
|
|
|
// reset {selNode,selOffset} to point before break
|
2014-05-02 15:11:26 +04:00
|
|
|
selNode = GetNodeLocation(GetAsDOMNode(wsRunObj.mStartReasonNode), &selOffset);
|
2002-05-14 04:05:37 +04:00
|
|
|
// we want to be inside any inline style prior to break
|
|
|
|
nsWSRunObject wsRunObj(this, selNode, selOffset);
|
2014-05-02 16:15:27 +04:00
|
|
|
selNode_ = do_QueryInterface(selNode);
|
|
|
|
wsRunObj.PriorVisibleNode(selNode_, selOffset, address_of(visNode),
|
2012-06-01 15:03:17 +04:00
|
|
|
&outVisOffset, &visType);
|
2012-07-13 10:33:43 +04:00
|
|
|
if (visType == WSType::text || visType == WSType::normalWS) {
|
2014-05-02 16:15:27 +04:00
|
|
|
selNode = GetAsDOMNode(visNode);
|
2002-05-14 04:05:37 +04:00
|
|
|
selOffset = outVisOffset; // PriorVisibleNode already set offset to _after_ the text or ws
|
2012-07-13 10:33:43 +04:00
|
|
|
} else if (visType == WSType::special) {
|
2015-05-28 18:58:42 +03:00
|
|
|
// prior visible thing is an image or some other non-text thingy.
|
2002-05-14 04:05:37 +04:00
|
|
|
// We want to be right after it.
|
2014-05-02 15:11:26 +04:00
|
|
|
selNode = GetNodeLocation(GetAsDOMNode(wsRunObj.mStartReasonNode), &selOffset);
|
2002-05-14 04:05:37 +04:00
|
|
|
++selOffset;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
selection->Collapse(selNode, selOffset);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2002-05-14 04:05:37 +04:00
|
|
|
// if we just pasted a link, discontinue link style
|
|
|
|
nsCOMPtr<nsIDOMNode> link;
|
|
|
|
if (!bStartedInLink && IsInLink(selNode, address_of(link)))
|
|
|
|
{
|
|
|
|
// so, if we just pasted a link, I split it. Why do that instead of just
|
|
|
|
// nudging selection point beyond it? Because it might have ended in a BR
|
|
|
|
// that is not visible. If so, the code above just placed selection
|
|
|
|
// inside that. So I split it instead.
|
2015-08-05 18:10:09 +03:00
|
|
|
nsCOMPtr<nsIContent> linkContent = do_QueryInterface(link);
|
|
|
|
NS_ENSURE_STATE(linkContent || !link);
|
|
|
|
nsCOMPtr<nsIContent> selContent = do_QueryInterface(selNode);
|
|
|
|
NS_ENSURE_STATE(selContent || !selNode);
|
|
|
|
nsCOMPtr<nsIContent> leftLink;
|
|
|
|
SplitNodeDeep(*linkContent, *selContent, selOffset,
|
|
|
|
EmptyContainers::no, getter_AddRefs(leftLink));
|
2015-02-03 01:34:17 +03:00
|
|
|
if (leftLink) {
|
2015-08-05 18:10:09 +03:00
|
|
|
selNode = GetNodeLocation(GetAsDOMNode(leftLink), &selOffset);
|
2015-02-03 01:34:17 +03:00
|
|
|
selection->Collapse(selNode, selOffset+1);
|
|
|
|
}
|
2002-05-14 04:05:37 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
|
|
|
|
return mRules->DidDoAction(selection, &ruleInfo, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
NS_IMETHODIMP
|
2003-04-15 18:07:30 +04:00
|
|
|
nsHTMLEditor::AddInsertionListener(nsIContentFilter *aListener)
|
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(aListener, NS_ERROR_NULL_POINTER);
|
2003-04-15 18:07:30 +04:00
|
|
|
|
|
|
|
// don't let a listener be added more than once
|
2015-04-24 14:27:35 +03:00
|
|
|
if (!mContentFilters.Contains(aListener)) {
|
|
|
|
mContentFilters.AppendElement(*aListener);
|
2003-04-15 18:07:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
NS_IMETHODIMP
|
2003-04-15 18:07:30 +04:00
|
|
|
nsHTMLEditor::RemoveInsertionListener(nsIContentFilter *aListener)
|
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(aListener, NS_ERROR_FAILURE);
|
2003-04-15 18:07:30 +04:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
mContentFilters.RemoveElement(aListener);
|
2003-04-15 18:07:30 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2003-04-15 18:07:30 +04:00
|
|
|
nsresult
|
2015-05-28 18:58:42 +03:00
|
|
|
nsHTMLEditor::DoContentFilterCallback(const nsAString &aFlavor,
|
2003-06-10 22:53:11 +04:00
|
|
|
nsIDOMDocument *sourceDoc,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aWillDeleteSelection,
|
2015-05-28 18:58:42 +03:00
|
|
|
nsIDOMNode **aFragmentAsNode,
|
|
|
|
nsIDOMNode **aFragStartNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t *aFragStartOffset,
|
2015-05-28 18:58:42 +03:00
|
|
|
nsIDOMNode **aFragEndNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t *aFragEndOffset,
|
2003-07-28 17:13:50 +04:00
|
|
|
nsIDOMNode **aTargetNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t *aTargetOffset,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *aDoContinue)
|
2003-04-15 18:07:30 +04:00
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
*aDoContinue = true;
|
2003-04-15 18:07:30 +04:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
for (auto& listener : mContentFilters) {
|
|
|
|
if (!*aDoContinue) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
listener->NotifyOfInsertion(aFlavor, nullptr, sourceDoc,
|
|
|
|
aWillDeleteSelection, aFragmentAsNode,
|
|
|
|
aFragStartNode, aFragStartOffset,
|
|
|
|
aFragEndNode, aFragEndOffset, aTargetNode,
|
|
|
|
aTargetOffset, aDoContinue);
|
2003-04-15 18:07:30 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-05-14 04:05:37 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2002-05-14 04:05:37 +04:00
|
|
|
nsHTMLEditor::IsInLink(nsIDOMNode *aNode, nsCOMPtr<nsIDOMNode> *outLink)
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(aNode, false);
|
2002-05-14 04:05:37 +04:00
|
|
|
if (outLink)
|
2012-07-30 18:20:58 +04:00
|
|
|
*outLink = nullptr;
|
2002-05-14 04:05:37 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> tmp, node = aNode;
|
|
|
|
while (node)
|
|
|
|
{
|
2015-05-28 18:58:42 +03:00
|
|
|
if (nsHTMLEditUtils::IsLink(node))
|
2002-05-14 04:05:37 +04:00
|
|
|
{
|
|
|
|
if (outLink)
|
|
|
|
*outLink = node;
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-05-14 04:05:37 +04:00
|
|
|
}
|
|
|
|
tmp = node;
|
|
|
|
tmp->GetParentNode(getter_AddRefs(node));
|
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-05-14 04:05:37 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
nsresult
|
2015-05-13 18:58:25 +03:00
|
|
|
nsHTMLEditor::StripFormattingNodes(nsIContent& aNode, bool aListOnly)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2015-05-13 18:58:25 +03:00
|
|
|
if (aNode.TextIsOnlyWhitespace()) {
|
|
|
|
nsCOMPtr<nsINode> parent = aNode.GetParentNode();
|
|
|
|
if (parent) {
|
2012-03-27 01:58:43 +04:00
|
|
|
if (!aListOnly || nsHTMLEditUtils::IsList(parent)) {
|
2015-05-13 18:58:25 +03:00
|
|
|
ErrorResult rv;
|
|
|
|
parent->RemoveChild(aNode, rv);
|
|
|
|
return rv.StealNSResult();
|
2012-03-27 01:58:43 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2015-05-13 18:58:25 +03:00
|
|
|
if (!aNode.IsHTMLElement(nsGkAtoms::pre)) {
|
|
|
|
nsCOMPtr<nsIContent> child = aNode.GetLastChild();
|
|
|
|
while (child) {
|
|
|
|
nsCOMPtr<nsIContent> previous = child->GetPreviousSibling();
|
|
|
|
nsresult rv = StripFormattingNodes(*child, aListOnly);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-05-13 18:58:25 +03:00
|
|
|
child = previous.forget();
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP nsHTMLEditor::PrepareTransferable(nsITransferable **transferable)
|
2002-11-22 06:30:59 +03:00
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-02 09:14:13 +03:00
|
|
|
nsresult
|
2015-07-22 09:56:57 +03:00
|
|
|
nsHTMLEditor::PrepareHTMLTransferable(nsITransferable **aTransferable)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
// Create generic Transferable for getting the data
|
2004-11-01 21:50:36 +03:00
|
|
|
nsresult rv = CallCreateInstance("@mozilla.org/widget/transferable;1", aTransferable);
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// Get the nsITransferable interface for getting the data from the clipboard
|
2002-11-22 06:30:59 +03:00
|
|
|
if (aTransferable)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2012-04-17 06:14:01 +04:00
|
|
|
nsCOMPtr<nsIDocument> destdoc = GetDocument();
|
2012-07-30 18:20:58 +04:00
|
|
|
nsILoadContext* loadContext = destdoc ? destdoc->GetLoadContext() : nullptr;
|
2012-04-17 06:14:01 +04:00
|
|
|
(*aTransferable)->Init(loadContext);
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Create the desired DataFlavor for the type of data
|
|
|
|
// we want to get out of the transferable
|
2010-04-12 06:35:18 +04:00
|
|
|
// This should only happen in html editors, not plaintext
|
|
|
|
if (!IsPlaintextEditor())
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2015-07-22 09:56:57 +03:00
|
|
|
(*aTransferable)->AddDataFlavor(kNativeHTMLMime);
|
2002-11-22 06:30:59 +03:00
|
|
|
(*aTransferable)->AddDataFlavor(kHTMLMime);
|
2010-11-07 01:49:26 +03:00
|
|
|
(*aTransferable)->AddDataFlavor(kFileMime);
|
2008-07-12 14:16:18 +04:00
|
|
|
|
2011-06-17 04:59:29 +04:00
|
|
|
switch (Preferences::GetInt("clipboard.paste_image_type", 1))
|
2008-07-12 14:16:18 +04:00
|
|
|
{
|
2011-06-17 04:59:29 +04:00
|
|
|
case 0: // prefer JPEG over PNG over GIF encoding
|
|
|
|
(*aTransferable)->AddDataFlavor(kJPEGImageMime);
|
2012-06-08 03:27:07 +04:00
|
|
|
(*aTransferable)->AddDataFlavor(kJPGImageMime);
|
2011-06-17 04:59:29 +04:00
|
|
|
(*aTransferable)->AddDataFlavor(kPNGImageMime);
|
|
|
|
(*aTransferable)->AddDataFlavor(kGIFImageMime);
|
|
|
|
break;
|
|
|
|
case 1: // prefer PNG over JPEG over GIF encoding (default)
|
|
|
|
default:
|
|
|
|
(*aTransferable)->AddDataFlavor(kPNGImageMime);
|
|
|
|
(*aTransferable)->AddDataFlavor(kJPEGImageMime);
|
2012-06-08 03:27:07 +04:00
|
|
|
(*aTransferable)->AddDataFlavor(kJPGImageMime);
|
2011-06-17 04:59:29 +04:00
|
|
|
(*aTransferable)->AddDataFlavor(kGIFImageMime);
|
|
|
|
break;
|
|
|
|
case 2: // prefer GIF over JPEG over PNG encoding
|
|
|
|
(*aTransferable)->AddDataFlavor(kGIFImageMime);
|
|
|
|
(*aTransferable)->AddDataFlavor(kJPEGImageMime);
|
2012-06-08 03:27:07 +04:00
|
|
|
(*aTransferable)->AddDataFlavor(kJPGImageMime);
|
2011-06-17 04:59:29 +04:00
|
|
|
(*aTransferable)->AddDataFlavor(kPNGImageMime);
|
|
|
|
break;
|
2008-07-12 14:16:18 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2002-11-22 06:30:59 +03:00
|
|
|
(*aTransferable)->AddDataFlavor(kUnicodeMime);
|
2009-03-27 09:50:03 +03:00
|
|
|
(*aTransferable)->AddDataFlavor(kMozTextInternal);
|
2002-11-22 06:30:59 +03:00
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2002-11-22 06:30:59 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2015-05-28 18:58:42 +03:00
|
|
|
FindIntegerAfterString(const char *aLeadingString,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsCString &aCStr, int32_t &foundNumber)
|
2002-11-22 06:30:59 +03:00
|
|
|
{
|
|
|
|
// first obtain offsets from cfhtml str
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t numFront = aCStr.Find(aLeadingString);
|
2002-11-22 06:30:59 +03:00
|
|
|
if (numFront == -1)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-03-27 01:58:43 +04:00
|
|
|
numFront += strlen(aLeadingString);
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t numBack = aCStr.FindCharInSet(CRLF, numFront);
|
2002-11-22 06:30:59 +03:00
|
|
|
if (numBack == -1)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString numStr(Substring(aCStr, numFront, numBack-numFront));
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult errorCode;
|
2011-09-21 21:22:53 +04:00
|
|
|
foundNumber = numStr.ToInteger(&errorCode);
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2002-11-22 06:30:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
RemoveFragComments(nsCString & aStr)
|
|
|
|
{
|
|
|
|
// remove the StartFragment/EndFragment comments from the str, if present
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startCommentIndx = aStr.Find("<!--StartFragment");
|
2002-11-22 06:30:59 +03:00
|
|
|
if (startCommentIndx >= 0)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startCommentEnd = aStr.Find("-->", false, startCommentIndx);
|
2002-11-22 06:30:59 +03:00
|
|
|
if (startCommentEnd > startCommentIndx)
|
|
|
|
aStr.Cut(startCommentIndx, (startCommentEnd+3)-startCommentIndx);
|
2012-03-27 01:58:43 +04:00
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t endCommentIndx = aStr.Find("<!--EndFragment");
|
2002-11-22 06:30:59 +03:00
|
|
|
if (endCommentIndx >= 0)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t endCommentEnd = aStr.Find("-->", false, endCommentIndx);
|
2002-11-22 06:30:59 +03:00
|
|
|
if (endCommentEnd > endCommentIndx)
|
|
|
|
aStr.Cut(endCommentIndx, (endCommentEnd+3)-endCommentIndx);
|
2012-03-27 01:58:43 +04:00
|
|
|
}
|
2002-11-22 06:30:59 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2014-01-04 19:02:17 +04:00
|
|
|
nsHTMLEditor::ParseCFHTML(nsCString & aCfhtml, char16_t **aStuffToPaste, char16_t **aCfcontext)
|
2002-11-22 06:30:59 +03:00
|
|
|
{
|
2011-09-21 21:22:53 +04:00
|
|
|
// First obtain offsets from cfhtml str.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startHTML, endHTML, startFragment, endFragment;
|
2015-05-28 18:58:42 +03:00
|
|
|
if (!FindIntegerAfterString("StartHTML:", aCfhtml, startHTML) ||
|
2011-09-21 21:22:53 +04:00
|
|
|
startHTML < -1)
|
2002-11-22 06:30:59 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-05-28 18:58:42 +03:00
|
|
|
if (!FindIntegerAfterString("EndHTML:", aCfhtml, endHTML) ||
|
2012-03-27 01:58:43 +04:00
|
|
|
endHTML < -1)
|
2011-09-21 21:22:53 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-05-28 18:58:42 +03:00
|
|
|
if (!FindIntegerAfterString("StartFragment:", aCfhtml, startFragment) ||
|
2012-03-27 01:58:43 +04:00
|
|
|
startFragment < 0)
|
2011-09-21 21:22:53 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2015-05-28 18:58:42 +03:00
|
|
|
if (!FindIntegerAfterString("EndFragment:", aCfhtml, endFragment) ||
|
2011-09-21 21:22:53 +04:00
|
|
|
startFragment < 0)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// The StartHTML and EndHTML markers are allowed to be -1 to include everything.
|
|
|
|
// See Reference: MSDN doc entitled "HTML Clipboard Format"
|
|
|
|
// http://msdn.microsoft.com/en-us/library/aa767917(VS.85).aspx#unknown_854
|
|
|
|
if (startHTML == -1) {
|
|
|
|
startHTML = aCfhtml.Find("<!--StartFragment-->");
|
|
|
|
if (startHTML == -1)
|
2012-03-27 01:58:43 +04:00
|
|
|
return NS_OK;
|
2011-09-21 21:22:53 +04:00
|
|
|
}
|
|
|
|
if (endHTML == -1) {
|
|
|
|
const char endFragmentMarker[] = "<!--EndFragment-->";
|
|
|
|
endHTML = aCfhtml.Find(endFragmentMarker);
|
|
|
|
if (endHTML == -1)
|
2012-03-27 01:58:43 +04:00
|
|
|
return NS_OK;
|
2011-10-11 09:50:08 +04:00
|
|
|
endHTML += ArrayLength(endFragmentMarker) - 1;
|
2011-09-21 21:22:53 +04:00
|
|
|
}
|
|
|
|
|
2002-11-22 06:30:59 +03:00
|
|
|
// create context string
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString contextUTF8(Substring(aCfhtml, startHTML, startFragment - startHTML) +
|
2006-04-07 02:48:55 +04:00
|
|
|
NS_LITERAL_CSTRING("<!--" kInsertCookie "-->") +
|
2002-11-22 06:30:59 +03:00
|
|
|
Substring(aCfhtml, endFragment, endHTML - endFragment));
|
2006-04-07 02:48:55 +04:00
|
|
|
|
2004-03-02 19:03:56 +03:00
|
|
|
// validate startFragment
|
|
|
|
// make sure it's not in the middle of a HTML tag
|
|
|
|
// see bug #228879 for more details
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t curPos = startFragment;
|
2004-03-02 19:03:56 +03:00
|
|
|
while (curPos > startHTML)
|
|
|
|
{
|
|
|
|
if (aCfhtml[curPos] == '>')
|
|
|
|
{
|
|
|
|
// working backwards, the first thing we see is the end of a tag
|
|
|
|
// so StartFragment is good, so do nothing.
|
|
|
|
break;
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
else if (aCfhtml[curPos] == '<')
|
2004-03-02 19:03:56 +03:00
|
|
|
{
|
2004-03-02 20:34:22 +03:00
|
|
|
// if we are at the start, then we want to see the '<'
|
2015-05-28 18:58:42 +03:00
|
|
|
if (curPos != startFragment)
|
2004-03-02 20:34:22 +03:00
|
|
|
{
|
|
|
|
// working backwards, the first thing we see is the start of a tag
|
|
|
|
// so StartFragment is bad, so we need to update it.
|
2009-08-14 18:09:00 +04:00
|
|
|
NS_ERROR("StartFragment byte count in the clipboard looks bad, see bug #228879");
|
2004-03-02 20:34:22 +03:00
|
|
|
startFragment = curPos - 1;
|
|
|
|
}
|
2004-03-02 19:03:56 +03:00
|
|
|
break;
|
|
|
|
}
|
2015-05-28 18:58:42 +03:00
|
|
|
else
|
2004-03-02 19:03:56 +03:00
|
|
|
{
|
|
|
|
curPos--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-11-22 06:30:59 +03:00
|
|
|
// create fragment string
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString fragmentUTF8(Substring(aCfhtml, startFragment, endFragment-startFragment));
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2002-11-22 06:30:59 +03:00
|
|
|
// remove the StartFragment/EndFragment comments from the fragment, if present
|
|
|
|
RemoveFragComments(fragmentUTF8);
|
|
|
|
|
|
|
|
// remove the StartFragment/EndFragment comments from the context, if present
|
|
|
|
RemoveFragComments(contextUTF8);
|
|
|
|
|
|
|
|
// convert both strings to usc2
|
2006-02-03 17:18:39 +03:00
|
|
|
const nsAFlatString& fragUcs2Str = NS_ConvertUTF8toUTF16(fragmentUTF8);
|
|
|
|
const nsAFlatString& cntxtUcs2Str = NS_ConvertUTF8toUTF16(contextUTF8);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2002-11-22 06:30:59 +03:00
|
|
|
// translate platform linebreaks for fragment
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t oldLengthInChars = fragUcs2Str.Length() + 1; // +1 to include null terminator
|
|
|
|
int32_t newLengthInChars = 0;
|
2002-11-22 06:30:59 +03:00
|
|
|
*aStuffToPaste = nsLinebreakConverter::ConvertUnicharLineBreaks(fragUcs2Str.get(),
|
2015-05-28 18:58:42 +03:00
|
|
|
nsLinebreakConverter::eLinebreakAny,
|
|
|
|
nsLinebreakConverter::eLinebreakContent,
|
2002-11-22 06:30:59 +03:00
|
|
|
oldLengthInChars, &newLengthInChars);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_TRUE(*aStuffToPaste, NS_ERROR_FAILURE);
|
|
|
|
|
2002-11-22 06:30:59 +03:00
|
|
|
// translate platform linebreaks for context
|
2002-12-12 04:05:53 +03:00
|
|
|
oldLengthInChars = cntxtUcs2Str.Length() + 1; // +1 to include null terminator
|
2002-11-22 06:30:59 +03:00
|
|
|
newLengthInChars = 0;
|
|
|
|
*aCfcontext = nsLinebreakConverter::ConvertUnicharLineBreaks(cntxtUcs2Str.get(),
|
2015-05-28 18:58:42 +03:00
|
|
|
nsLinebreakConverter::eLinebreakAny,
|
|
|
|
nsLinebreakConverter::eLinebreakContent,
|
2002-11-22 06:30:59 +03:00
|
|
|
oldLengthInChars, &newLengthInChars);
|
2012-02-15 13:18:24 +04:00
|
|
|
// it's ok for context to be empty. frag might be whole doc and contain all its context.
|
2012-03-27 01:58:43 +04:00
|
|
|
|
|
|
|
// we're done!
|
2001-01-28 23:13:07 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-05-20 02:47:44 +03:00
|
|
|
static nsresult
|
|
|
|
ImgFromData(const nsACString& aType, const nsACString& aData, nsString& aOutput)
|
|
|
|
{
|
|
|
|
nsAutoCString data64;
|
|
|
|
nsresult rv = Base64Encode(aData, data64);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
aOutput.AssignLiteral("<IMG src=\"data:");
|
|
|
|
AppendUTF8toUTF16(aType, aOutput);
|
|
|
|
aOutput.AppendLiteral(";base64,");
|
|
|
|
AppendUTF8toUTF16(data64, aOutput);
|
|
|
|
aOutput.AppendLiteral("\" alt=\"\" >");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMPL_ISUPPORTS(nsHTMLEditor::BlobReader, nsIEditorBlobListener)
|
|
|
|
|
|
|
|
nsHTMLEditor::BlobReader::BlobReader(BlobImpl* aBlob, nsHTMLEditor* aEditor,
|
|
|
|
bool aIsSafe, nsIDOMDocument* aSourceDoc,
|
|
|
|
nsIDOMNode* aDestinationNode,
|
|
|
|
int32_t aDestOffset,
|
|
|
|
bool aDoDeleteSelection)
|
|
|
|
: mBlob(aBlob)
|
|
|
|
, mEditor(aEditor)
|
|
|
|
, mIsSafe(aIsSafe)
|
|
|
|
, mSourceDoc(aSourceDoc)
|
|
|
|
, mDestinationNode(aDestinationNode)
|
|
|
|
, mDestOffset(aDestOffset)
|
|
|
|
, mDoDeleteSelection(aDoDeleteSelection)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mBlob);
|
|
|
|
MOZ_ASSERT(mEditor);
|
|
|
|
MOZ_ASSERT(mDestinationNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::BlobReader::OnResult(const nsACString& aResult)
|
|
|
|
{
|
|
|
|
nsString blobType;
|
|
|
|
mBlob->GetType(blobType);
|
|
|
|
|
|
|
|
NS_ConvertUTF16toUTF8 type(blobType);
|
|
|
|
nsAutoString stuffToPaste;
|
|
|
|
nsresult rv = ImgFromData(type, aResult, stuffToPaste);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2016-06-23 12:46:58 +03:00
|
|
|
AutoEditBatch beginBatching(mEditor);
|
2016-05-20 02:47:44 +03:00
|
|
|
rv = mEditor->DoInsertHTMLWithContext(stuffToPaste, EmptyString(), EmptyString(),
|
|
|
|
NS_LITERAL_STRING(kFileMime),
|
|
|
|
mSourceDoc,
|
|
|
|
mDestinationNode, mDestOffset,
|
|
|
|
mDoDeleteSelection,
|
|
|
|
mIsSafe, false);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::BlobReader::OnError(const nsAString& aError)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsINode> destNode = do_QueryInterface(mDestinationNode);
|
|
|
|
const nsPromiseFlatString& flat = PromiseFlatString(aError);
|
|
|
|
const char16_t* error = flat.get();
|
|
|
|
nsContentUtils::ReportToConsole(nsIScriptError::warningFlag,
|
|
|
|
NS_LITERAL_CSTRING("Editor"),
|
|
|
|
destNode->OwnerDoc(),
|
|
|
|
nsContentUtils::eDOM_PROPERTIES,
|
|
|
|
"EditorFileDropFailed",
|
|
|
|
&error, 1);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-02-17 18:16:59 +04:00
|
|
|
nsresult nsHTMLEditor::InsertObject(const char* aType, nsISupports* aObject, bool aIsSafe,
|
|
|
|
nsIDOMDocument *aSourceDoc,
|
|
|
|
nsIDOMNode *aDestinationNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aDestOffset,
|
2012-02-17 18:16:59 +04:00
|
|
|
bool aDoDeleteSelection)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
2016-05-20 02:47:44 +03:00
|
|
|
if (nsCOMPtr<BlobImpl> blob = do_QueryInterface(aObject)) {
|
|
|
|
RefPtr<BlobReader> br = new BlobReader(blob, this, aIsSafe, aSourceDoc,
|
|
|
|
aDestinationNode, aDestOffset,
|
|
|
|
aDoDeleteSelection);
|
|
|
|
nsCOMPtr<nsIEditorUtils> utils =
|
|
|
|
do_GetService("@mozilla.org/editor-utils;1");
|
|
|
|
NS_ENSURE_TRUE(utils, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aDestinationNode);
|
|
|
|
MOZ_ASSERT(node);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMBlob> domBlob = Blob::Create(node->GetOwnerGlobal(), blob);
|
|
|
|
NS_ENSURE_TRUE(domBlob, NS_ERROR_FAILURE);
|
|
|
|
|
|
|
|
return utils->SlurpBlob(domBlob, node->OwnerDoc()->GetWindow(), br);
|
|
|
|
}
|
|
|
|
|
2015-10-28 00:41:58 +03:00
|
|
|
nsAutoCString type(aType);
|
2012-02-17 18:16:59 +04:00
|
|
|
|
|
|
|
// Check to see if we can insert an image file
|
|
|
|
bool insertAsImage = false;
|
2015-10-28 00:41:58 +03:00
|
|
|
nsCOMPtr<nsIFile> fileObj;
|
|
|
|
if (type.EqualsLiteral(kFileMime))
|
2012-02-17 18:16:59 +04:00
|
|
|
{
|
2015-10-28 00:41:58 +03:00
|
|
|
fileObj = do_QueryInterface(aObject);
|
2012-02-17 18:16:59 +04:00
|
|
|
if (fileObj)
|
|
|
|
{
|
|
|
|
// Accept any image type fed to us
|
2015-10-28 00:41:58 +03:00
|
|
|
if (nsContentUtils::IsFileImage(fileObj, type))
|
|
|
|
{
|
2012-02-17 18:16:59 +04:00
|
|
|
insertAsImage = true;
|
2015-10-28 00:41:58 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Reset type.
|
|
|
|
type.AssignLiteral(kFileMime);
|
2012-02-17 18:16:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-10-28 00:41:58 +03:00
|
|
|
if (type.EqualsLiteral(kJPEGImageMime) ||
|
|
|
|
type.EqualsLiteral(kJPGImageMime) ||
|
|
|
|
type.EqualsLiteral(kPNGImageMime) ||
|
|
|
|
type.EqualsLiteral(kGIFImageMime) ||
|
2012-02-17 18:16:59 +04:00
|
|
|
insertAsImage)
|
|
|
|
{
|
2015-10-28 00:41:58 +03:00
|
|
|
nsCString imageData;
|
|
|
|
if (insertAsImage)
|
|
|
|
{
|
|
|
|
rv = nsContentUtils::SlurpFileToString(fileObj, imageData);
|
2012-02-17 18:16:59 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2015-10-28 00:41:58 +03:00
|
|
|
else
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsIInputStream> imageStream = do_QueryInterface(aObject);
|
|
|
|
NS_ENSURE_TRUE(imageStream, NS_ERROR_FAILURE);
|
2012-02-17 18:16:59 +04:00
|
|
|
|
2015-10-28 00:41:58 +03:00
|
|
|
rv = NS_ConsumeStream(imageStream, UINT32_MAX, imageData);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-02-17 18:16:59 +04:00
|
|
|
|
2015-10-28 00:41:58 +03:00
|
|
|
rv = imageStream->Close();
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
}
|
2012-02-17 18:16:59 +04:00
|
|
|
|
2016-05-20 02:47:44 +03:00
|
|
|
nsAutoString stuffToPaste;
|
|
|
|
rv = ImgFromData(type, imageData, stuffToPaste);
|
2012-10-04 10:45:16 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-02-17 18:16:59 +04:00
|
|
|
|
2016-06-23 12:46:58 +03:00
|
|
|
AutoEditBatch beginBatching(this);
|
2015-05-28 18:58:42 +03:00
|
|
|
rv = DoInsertHTMLWithContext(stuffToPaste, EmptyString(), EmptyString(),
|
2012-02-17 18:16:59 +04:00
|
|
|
NS_LITERAL_STRING(kFileMime),
|
|
|
|
aSourceDoc,
|
|
|
|
aDestinationNode, aDestOffset,
|
|
|
|
aDoDeleteSelection,
|
2015-05-06 17:15:00 +03:00
|
|
|
aIsSafe, false);
|
2012-02-17 18:16:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-02 09:14:13 +03:00
|
|
|
nsresult
|
|
|
|
nsHTMLEditor::InsertFromTransferable(nsITransferable *transferable,
|
|
|
|
nsIDOMDocument *aSourceDoc,
|
|
|
|
const nsAString & aContextStr,
|
|
|
|
const nsAString & aInfoStr,
|
2015-07-22 09:56:57 +03:00
|
|
|
bool havePrivateHTMLFlavor,
|
2015-01-02 09:14:13 +03:00
|
|
|
nsIDOMNode *aDestinationNode,
|
|
|
|
int32_t aDestOffset,
|
|
|
|
bool aDoDeleteSelection)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
nsresult rv = NS_OK;
|
2006-10-12 00:30:06 +04:00
|
|
|
nsXPIDLCString bestFlavor;
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsISupports> genericDataObj;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = 0;
|
2012-03-27 01:58:43 +04:00
|
|
|
if (NS_SUCCEEDED(transferable->GetAnyTransferData(getter_Copies(bestFlavor), getter_AddRefs(genericDataObj), &len)))
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2016-06-23 12:01:23 +03:00
|
|
|
AutoTransactionsConserveSelection dontSpazMySelection(this);
|
2003-07-28 18:27:49 +04:00
|
|
|
nsAutoString flavor;
|
|
|
|
flavor.AssignWithConversion(bestFlavor);
|
2003-07-28 17:13:50 +04:00
|
|
|
nsAutoString stuffToPaste;
|
2012-02-17 18:16:59 +04:00
|
|
|
bool isSafe = IsSafeToInsertData(aSourceDoc);
|
2010-11-07 01:49:26 +03:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
if (0 == nsCRT::strcmp(bestFlavor, kFileMime) ||
|
2012-02-17 18:16:59 +04:00
|
|
|
0 == nsCRT::strcmp(bestFlavor, kJPEGImageMime) ||
|
2012-06-08 03:27:07 +04:00
|
|
|
0 == nsCRT::strcmp(bestFlavor, kJPGImageMime) ||
|
2012-02-17 18:16:59 +04:00
|
|
|
0 == nsCRT::strcmp(bestFlavor, kPNGImageMime) ||
|
|
|
|
0 == nsCRT::strcmp(bestFlavor, kGIFImageMime)) {
|
|
|
|
rv = InsertObject(bestFlavor, genericDataObj, isSafe,
|
|
|
|
aSourceDoc, aDestinationNode, aDestOffset, aDoDeleteSelection);
|
2010-11-07 01:49:26 +03:00
|
|
|
}
|
2012-02-17 18:16:59 +04:00
|
|
|
else if (0 == nsCRT::strcmp(bestFlavor, kNativeHTMLMime))
|
2002-11-22 06:30:59 +03:00
|
|
|
{
|
|
|
|
// note cf_html uses utf8, hence use length = len, not len/2 as in flavors below
|
2012-03-27 01:58:43 +04:00
|
|
|
nsCOMPtr<nsISupportsCString> textDataObj = do_QueryInterface(genericDataObj);
|
2002-11-22 06:30:59 +03:00
|
|
|
if (textDataObj && len > 0)
|
|
|
|
{
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString cfhtml;
|
2002-11-22 06:30:59 +03:00
|
|
|
textDataObj->GetData(cfhtml);
|
|
|
|
NS_ASSERTION(cfhtml.Length() <= (len), "Invalid length!");
|
|
|
|
nsXPIDLString cfcontext, cffragment, cfselection; // cfselection left emtpy for now
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2002-11-22 06:30:59 +03:00
|
|
|
rv = ParseCFHTML(cfhtml, getter_Copies(cffragment), getter_Copies(cfcontext));
|
|
|
|
if (NS_SUCCEEDED(rv) && !cffragment.IsEmpty())
|
|
|
|
{
|
2016-06-23 12:46:58 +03:00
|
|
|
AutoEditBatch beginBatching(this);
|
2015-07-22 09:56:57 +03:00
|
|
|
// If we have our private HTML flavor, we will only use the fragment
|
|
|
|
// from the CF_HTML. The rest comes from the clipboard.
|
|
|
|
if (havePrivateHTMLFlavor) {
|
|
|
|
rv = DoInsertHTMLWithContext(cffragment,
|
|
|
|
aContextStr, aInfoStr, flavor,
|
|
|
|
aSourceDoc,
|
|
|
|
aDestinationNode, aDestOffset,
|
|
|
|
aDoDeleteSelection,
|
|
|
|
isSafe);
|
|
|
|
} else {
|
|
|
|
rv = DoInsertHTMLWithContext(cffragment,
|
|
|
|
cfcontext, cfselection, flavor,
|
|
|
|
aSourceDoc,
|
|
|
|
aDestinationNode, aDestOffset,
|
|
|
|
aDoDeleteSelection,
|
|
|
|
isSafe);
|
|
|
|
|
|
|
|
}
|
2012-07-04 04:44:46 +04:00
|
|
|
} else {
|
|
|
|
// In some platforms (like Linux), the clipboard might return data
|
|
|
|
// requested for unknown flavors (for example:
|
|
|
|
// application/x-moz-nativehtml). In this case, treat the data
|
|
|
|
// to be pasted as mere HTML to get the best chance of pasting it
|
|
|
|
// correctly.
|
|
|
|
bestFlavor.AssignLiteral(kHTMLMime);
|
|
|
|
// Fall through the next case
|
2002-11-22 06:30:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-04 04:44:46 +04:00
|
|
|
if (0 == nsCRT::strcmp(bestFlavor, kHTMLMime) ||
|
|
|
|
0 == nsCRT::strcmp(bestFlavor, kUnicodeMime) ||
|
|
|
|
0 == nsCRT::strcmp(bestFlavor, kMozTextInternal)) {
|
2012-03-27 01:58:43 +04:00
|
|
|
nsCOMPtr<nsISupportsString> textDataObj = do_QueryInterface(genericDataObj);
|
2012-07-04 04:44:46 +04:00
|
|
|
if (textDataObj && len > 0) {
|
2002-08-27 01:20:34 +04:00
|
|
|
nsAutoString text;
|
2002-11-22 06:30:59 +03:00
|
|
|
textDataObj->GetData(text);
|
2002-08-27 01:20:34 +04:00
|
|
|
NS_ASSERTION(text.Length() <= (len/2), "Invalid length!");
|
2002-11-22 06:30:59 +03:00
|
|
|
stuffToPaste.Assign(text.get(), len / 2);
|
2012-07-04 04:44:46 +04:00
|
|
|
} else {
|
|
|
|
nsCOMPtr<nsISupportsCString> textDataObj(do_QueryInterface(genericDataObj));
|
|
|
|
if (textDataObj && len > 0) {
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString text;
|
2012-07-04 04:44:46 +04:00
|
|
|
textDataObj->GetData(text);
|
|
|
|
NS_ASSERTION(text.Length() <= len, "Invalid length!");
|
|
|
|
stuffToPaste.Assign(NS_ConvertUTF8toUTF16(Substring(text, 0, len)));
|
|
|
|
}
|
|
|
|
}
|
2012-02-17 18:16:59 +04:00
|
|
|
|
2012-07-04 04:44:46 +04:00
|
|
|
if (!stuffToPaste.IsEmpty()) {
|
2016-06-23 12:46:58 +03:00
|
|
|
AutoEditBatch beginBatching(this);
|
2012-02-17 18:16:59 +04:00
|
|
|
if (0 == nsCRT::strcmp(bestFlavor, kHTMLMime)) {
|
|
|
|
rv = DoInsertHTMLWithContext(stuffToPaste,
|
|
|
|
aContextStr, aInfoStr, flavor,
|
|
|
|
aSourceDoc,
|
|
|
|
aDestinationNode, aDestOffset,
|
|
|
|
aDoDeleteSelection,
|
|
|
|
isSafe);
|
|
|
|
} else {
|
|
|
|
rv = InsertTextAt(stuffToPaste, aDestinationNode, aDestOffset, aDoDeleteSelection);
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
2012-02-17 18:16:59 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Try to scroll the selection into view if the paste succeeded
|
|
|
|
if (NS_SUCCEEDED(rv))
|
|
|
|
ScrollSelectionIntoView(false);
|
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
GetStringFromDataTransfer(nsIDOMDataTransfer *aDataTransfer, const nsAString& aType,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aIndex, nsAString& aOutputString)
|
2012-02-17 18:16:59 +04:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIVariant> variant;
|
2015-10-23 02:28:14 +03:00
|
|
|
DataTransfer::Cast(aDataTransfer)->GetDataAtNoSecurityCheck(aType, aIndex, getter_AddRefs(variant));
|
2012-02-17 18:16:59 +04:00
|
|
|
if (variant)
|
|
|
|
variant->GetAsAString(aOutputString);
|
|
|
|
}
|
|
|
|
|
2013-07-09 21:54:21 +04:00
|
|
|
nsresult nsHTMLEditor::InsertFromDataTransfer(DataTransfer *aDataTransfer,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aIndex,
|
2012-02-17 18:16:59 +04:00
|
|
|
nsIDOMDocument *aSourceDoc,
|
|
|
|
nsIDOMNode *aDestinationNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aDestOffset,
|
2012-02-17 18:16:59 +04:00
|
|
|
bool aDoDeleteSelection)
|
|
|
|
{
|
2013-07-09 21:54:21 +04:00
|
|
|
ErrorResult rv;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMStringList> types = aDataTransfer->MozTypesAt(aIndex, rv);
|
2013-07-09 21:54:21 +04:00
|
|
|
if (rv.Failed()) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-07-09 21:54:21 +04:00
|
|
|
}
|
2012-02-17 18:16:59 +04:00
|
|
|
|
2013-07-09 21:54:21 +04:00
|
|
|
bool hasPrivateHTMLFlavor = types->Contains(NS_LITERAL_STRING(kHTMLContext));
|
2012-02-17 18:16:59 +04:00
|
|
|
|
|
|
|
bool isText = IsPlaintextEditor();
|
|
|
|
bool isSafe = IsSafeToInsertData(aSourceDoc);
|
2012-09-07 23:57:10 +04:00
|
|
|
|
2013-07-09 21:54:21 +04:00
|
|
|
uint32_t length = types->Length();
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t t = 0; t < length; t++) {
|
2012-02-17 18:16:59 +04:00
|
|
|
nsAutoString type;
|
|
|
|
types->Item(t, type);
|
|
|
|
|
|
|
|
if (!isText) {
|
|
|
|
if (type.EqualsLiteral(kFileMime) ||
|
|
|
|
type.EqualsLiteral(kJPEGImageMime) ||
|
2012-06-08 03:27:07 +04:00
|
|
|
type.EqualsLiteral(kJPGImageMime) ||
|
2012-02-17 18:16:59 +04:00
|
|
|
type.EqualsLiteral(kPNGImageMime) ||
|
|
|
|
type.EqualsLiteral(kGIFImageMime)) {
|
|
|
|
nsCOMPtr<nsIVariant> variant;
|
2015-10-23 02:28:14 +03:00
|
|
|
DataTransfer::Cast(aDataTransfer)->GetDataAtNoSecurityCheck(type, aIndex, getter_AddRefs(variant));
|
2012-02-17 18:16:59 +04:00
|
|
|
if (variant) {
|
|
|
|
nsCOMPtr<nsISupports> object;
|
|
|
|
variant->GetAsISupports(getter_AddRefs(object));
|
2012-09-07 23:57:10 +04:00
|
|
|
return InsertObject(NS_ConvertUTF16toUTF8(type).get(), object, isSafe,
|
|
|
|
aSourceDoc, aDestinationNode, aDestOffset, aDoDeleteSelection);
|
2012-02-17 18:16:59 +04:00
|
|
|
}
|
|
|
|
}
|
2015-07-22 09:56:57 +03:00
|
|
|
else if (type.EqualsLiteral(kNativeHTMLMime)) {
|
|
|
|
// Windows only clipboard parsing.
|
2002-08-27 01:20:34 +04:00
|
|
|
nsAutoString text;
|
2015-07-22 09:56:57 +03:00
|
|
|
GetStringFromDataTransfer(aDataTransfer, type, aIndex, text);
|
2012-02-17 18:16:59 +04:00
|
|
|
NS_ConvertUTF16toUTF8 cfhtml(text);
|
|
|
|
|
|
|
|
nsXPIDLString cfcontext, cffragment, cfselection; // cfselection left emtpy for now
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2012-09-07 23:57:10 +04:00
|
|
|
nsresult rv = ParseCFHTML(cfhtml, getter_Copies(cffragment), getter_Copies(cfcontext));
|
2012-02-17 18:16:59 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && !cffragment.IsEmpty())
|
|
|
|
{
|
2016-06-23 12:46:58 +03:00
|
|
|
AutoEditBatch beginBatching(this);
|
2015-07-22 09:56:57 +03:00
|
|
|
|
|
|
|
if (hasPrivateHTMLFlavor) {
|
|
|
|
// If we have our private HTML flavor, we will only use the fragment
|
|
|
|
// from the CF_HTML. The rest comes from the clipboard.
|
|
|
|
nsAutoString contextString, infoString;
|
|
|
|
GetStringFromDataTransfer(aDataTransfer, NS_LITERAL_STRING(kHTMLContext), aIndex, contextString);
|
|
|
|
GetStringFromDataTransfer(aDataTransfer, NS_LITERAL_STRING(kHTMLInfo), aIndex, infoString);
|
|
|
|
return DoInsertHTMLWithContext(cffragment,
|
|
|
|
contextString, infoString, type,
|
|
|
|
aSourceDoc,
|
|
|
|
aDestinationNode, aDestOffset,
|
|
|
|
aDoDeleteSelection,
|
|
|
|
isSafe);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return DoInsertHTMLWithContext(cffragment,
|
|
|
|
cfcontext, cfselection, type,
|
|
|
|
aSourceDoc,
|
|
|
|
aDestinationNode, aDestOffset,
|
|
|
|
aDoDeleteSelection,
|
|
|
|
isSafe);
|
|
|
|
}
|
2012-02-17 18:16:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (type.EqualsLiteral(kHTMLMime)) {
|
2012-02-17 18:16:59 +04:00
|
|
|
nsAutoString text, contextString, infoString;
|
2012-02-17 18:16:59 +04:00
|
|
|
GetStringFromDataTransfer(aDataTransfer, type, aIndex, text);
|
2012-02-17 18:16:59 +04:00
|
|
|
GetStringFromDataTransfer(aDataTransfer, NS_LITERAL_STRING(kHTMLContext), aIndex, contextString);
|
|
|
|
GetStringFromDataTransfer(aDataTransfer, NS_LITERAL_STRING(kHTMLInfo), aIndex, infoString);
|
2012-02-17 18:16:59 +04:00
|
|
|
|
2016-06-23 12:46:58 +03:00
|
|
|
AutoEditBatch beginBatching(this);
|
2012-02-17 18:16:59 +04:00
|
|
|
if (type.EqualsLiteral(kHTMLMime)) {
|
2012-09-07 23:57:10 +04:00
|
|
|
return DoInsertHTMLWithContext(text,
|
|
|
|
contextString, infoString, type,
|
|
|
|
aSourceDoc,
|
|
|
|
aDestinationNode, aDestOffset,
|
|
|
|
aDoDeleteSelection,
|
|
|
|
isSafe);
|
2012-02-17 18:16:59 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
2010-11-07 01:49:26 +03:00
|
|
|
|
2012-02-17 18:16:59 +04:00
|
|
|
if (type.EqualsLiteral(kTextMime) ||
|
|
|
|
type.EqualsLiteral(kMozTextInternal)) {
|
|
|
|
nsAutoString text;
|
|
|
|
GetStringFromDataTransfer(aDataTransfer, type, aIndex, text);
|
2009-05-15 12:44:16 +04:00
|
|
|
|
2016-06-23 12:46:58 +03:00
|
|
|
AutoEditBatch beginBatching(this);
|
2012-09-07 23:57:10 +04:00
|
|
|
return InsertTextAt(text, aDestinationNode, aDestOffset, aDoDeleteSelection);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-09-07 23:57:10 +04:00
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nsHTMLEditor::HavePrivateHTMLFlavor(nsIClipboard *aClipboard)
|
2002-11-22 06:30:59 +03:00
|
|
|
{
|
|
|
|
// check the clipboard for our special kHTMLContext flavor. If that is there, we know
|
|
|
|
// we have our own internal html format on clipboard.
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ENSURE_TRUE(aClipboard, false);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool bHavePrivateHTMLFlavor = false;
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2007-11-20 22:06:30 +03:00
|
|
|
const char* flavArray[] = { kHTMLContext };
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2007-11-20 22:06:30 +03:00
|
|
|
if (NS_SUCCEEDED(aClipboard->HasDataMatchingFlavors(flavArray,
|
2011-10-11 09:50:08 +04:00
|
|
|
ArrayLength(flavArray), nsIClipboard::kGlobalClipboard,
|
2012-03-27 01:58:43 +04:00
|
|
|
&bHavePrivateHTMLFlavor)))
|
2002-11-22 06:30:59 +03:00
|
|
|
return bHavePrivateHTMLFlavor;
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2002-11-22 06:30:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::Paste(int32_t aSelectionType)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2015-09-08 17:33:35 +03:00
|
|
|
if (!FireClipboardEvent(ePaste, aSelectionType)) {
|
2010-03-19 21:32:13 +03:00
|
|
|
return NS_OK;
|
2015-09-08 17:33:35 +03:00
|
|
|
}
|
2007-07-26 08:14:33 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Get Clipboard Service
|
2010-03-19 21:32:13 +03:00
|
|
|
nsresult rv;
|
2003-07-09 00:22:56 +04:00
|
|
|
nsCOMPtr<nsIClipboard> clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Get the nsITransferable interface for getting the data from the clipboard
|
|
|
|
nsCOMPtr<nsITransferable> trans;
|
2015-07-22 09:56:57 +03:00
|
|
|
rv = PrepareHTMLTransferable(getter_AddRefs(trans));
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
|
|
|
|
// Get the Data from the clipboard
|
|
|
|
rv = clipboard->GetData(trans, aSelectionType);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (!IsModifiable()) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-11-22 06:30:59 +03:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
// also get additional html copy hints, if present
|
|
|
|
nsAutoString contextStr, infoStr;
|
2003-04-26 01:05:32 +04:00
|
|
|
|
2015-07-22 09:56:57 +03:00
|
|
|
// If we have our internal html flavor on the clipboard, there is special
|
|
|
|
// context to use instead of cfhtml context.
|
|
|
|
bool bHavePrivateHTMLFlavor = HavePrivateHTMLFlavor(clipboard);
|
2012-03-27 01:58:43 +04:00
|
|
|
if (bHavePrivateHTMLFlavor)
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupports> contextDataObj, infoDataObj;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t contextLen, infoLen;
|
2012-03-27 01:58:43 +04:00
|
|
|
nsCOMPtr<nsISupportsString> textDataObj;
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransferable> contextTrans =
|
|
|
|
do_CreateInstance("@mozilla.org/widget/transferable;1");
|
|
|
|
NS_ENSURE_TRUE(contextTrans, NS_ERROR_NULL_POINTER);
|
2012-07-30 18:20:58 +04:00
|
|
|
contextTrans->Init(nullptr);
|
2012-03-27 01:58:43 +04:00
|
|
|
contextTrans->AddDataFlavor(kHTMLContext);
|
|
|
|
clipboard->GetData(contextTrans, aSelectionType);
|
|
|
|
contextTrans->GetTransferData(kHTMLContext, getter_AddRefs(contextDataObj), &contextLen);
|
|
|
|
|
|
|
|
nsCOMPtr<nsITransferable> infoTrans =
|
|
|
|
do_CreateInstance("@mozilla.org/widget/transferable;1");
|
|
|
|
NS_ENSURE_TRUE(infoTrans, NS_ERROR_NULL_POINTER);
|
2012-07-30 18:20:58 +04:00
|
|
|
infoTrans->Init(nullptr);
|
2012-03-27 01:58:43 +04:00
|
|
|
infoTrans->AddDataFlavor(kHTMLInfo);
|
|
|
|
clipboard->GetData(infoTrans, aSelectionType);
|
|
|
|
infoTrans->GetTransferData(kHTMLInfo, getter_AddRefs(infoDataObj), &infoLen);
|
|
|
|
|
|
|
|
if (contextDataObj)
|
|
|
|
{
|
|
|
|
nsAutoString text;
|
|
|
|
textDataObj = do_QueryInterface(contextDataObj);
|
|
|
|
textDataObj->GetData(text);
|
|
|
|
NS_ASSERTION(text.Length() <= (contextLen/2), "Invalid length!");
|
|
|
|
contextStr.Assign(text.get(), contextLen / 2);
|
|
|
|
}
|
2003-04-26 01:05:32 +04:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
if (infoDataObj)
|
|
|
|
{
|
|
|
|
nsAutoString text;
|
|
|
|
textDataObj = do_QueryInterface(infoDataObj);
|
|
|
|
textDataObj->GetData(text);
|
|
|
|
NS_ASSERTION(text.Length() <= (infoLen/2), "Invalid length!");
|
|
|
|
infoStr.Assign(text.get(), infoLen / 2);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
// handle transferable hooks
|
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc;
|
|
|
|
GetDocument(getter_AddRefs(domdoc));
|
2016-06-23 10:59:15 +03:00
|
|
|
if (!EditorHookUtils::DoInsertionHook(domdoc, nullptr, trans)) {
|
2012-03-27 01:58:43 +04:00
|
|
|
return NS_OK;
|
2016-06-23 10:59:15 +03:00
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2015-07-22 09:56:57 +03:00
|
|
|
return InsertFromTransferable(trans, nullptr, contextStr, infoStr, bHavePrivateHTMLFlavor,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr, 0, true);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2010-01-11 04:45:45 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::PasteTransferable(nsITransferable *aTransferable)
|
|
|
|
{
|
2013-08-07 22:52:38 +04:00
|
|
|
// Use an invalid value for the clipboard type as data comes from aTransferable
|
|
|
|
// and we don't currently implement a way to put that in the data transfer yet.
|
2015-09-08 17:33:35 +03:00
|
|
|
if (!FireClipboardEvent(ePaste, nsIClipboard::kGlobalClipboard)) {
|
2010-03-19 21:32:13 +03:00
|
|
|
return NS_OK;
|
2015-09-08 17:33:35 +03:00
|
|
|
}
|
2010-01-11 04:45:45 +03:00
|
|
|
|
|
|
|
// handle transferable hooks
|
2012-03-24 12:19:14 +04:00
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc = GetDOMDocument();
|
2016-06-23 10:59:15 +03:00
|
|
|
if (!EditorHookUtils::DoInsertionHook(domdoc, nullptr, aTransferable)) {
|
2010-01-11 04:45:45 +03:00
|
|
|
return NS_OK;
|
2016-06-23 10:59:15 +03:00
|
|
|
}
|
2010-01-11 04:45:45 +03:00
|
|
|
|
|
|
|
nsAutoString contextStr, infoStr;
|
2015-07-22 09:56:57 +03:00
|
|
|
return InsertFromTransferable(aTransferable, nullptr, contextStr, infoStr, false,
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr, 0, true);
|
2010-01-11 04:45:45 +03:00
|
|
|
}
|
|
|
|
|
2015-05-28 18:58:42 +03:00
|
|
|
//
|
2002-10-06 05:23:18 +04:00
|
|
|
// HTML PasteNoFormatting. Ignore any HTML styles and formating in paste source
|
|
|
|
//
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::PasteNoFormatting(int32_t aSelectionType)
|
2002-10-06 05:23:18 +04:00
|
|
|
{
|
2015-09-08 17:33:35 +03:00
|
|
|
if (!FireClipboardEvent(ePaste, aSelectionType)) {
|
2012-04-03 21:41:04 +04:00
|
|
|
return NS_OK;
|
2015-09-08 17:33:35 +03:00
|
|
|
}
|
2012-04-03 21:41:04 +04:00
|
|
|
|
2002-11-07 16:04:37 +03:00
|
|
|
ForceCompositionEnd();
|
|
|
|
|
|
|
|
// Get Clipboard Service
|
|
|
|
nsresult rv;
|
2003-07-09 00:22:56 +04:00
|
|
|
nsCOMPtr<nsIClipboard> clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2002-11-07 16:04:37 +03:00
|
|
|
// Get the nsITransferable interface for getting the data from the clipboard.
|
|
|
|
// use nsPlaintextEditor::PrepareTransferable() to force unicode plaintext data.
|
|
|
|
nsCOMPtr<nsITransferable> trans;
|
|
|
|
rv = nsPlaintextEditor::PrepareTransferable(getter_AddRefs(trans));
|
|
|
|
if (NS_SUCCEEDED(rv) && trans)
|
|
|
|
{
|
2015-05-28 18:58:42 +03:00
|
|
|
// Get the Data from the clipboard
|
2002-11-07 16:04:37 +03:00
|
|
|
if (NS_SUCCEEDED(clipboard->GetData(trans, aSelectionType)) && IsModifiable())
|
|
|
|
{
|
2004-01-30 01:04:45 +03:00
|
|
|
const nsAFlatString& empty = EmptyString();
|
2015-07-22 09:56:57 +03:00
|
|
|
rv = InsertFromTransferable(trans, nullptr, empty, empty, false, nullptr, 0,
|
2011-10-17 18:59:28 +04:00
|
|
|
true);
|
2002-11-07 16:04:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rv;
|
2002-10-06 05:23:18 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2002-11-07 16:04:37 +03:00
|
|
|
|
2010-01-11 04:45:45 +03:00
|
|
|
// The following arrays contain the MIME types that we can paste. The arrays
|
|
|
|
// are used by CanPaste() and CanPasteTransferable() below.
|
|
|
|
|
|
|
|
static const char* textEditorFlavors[] = { kUnicodeMime };
|
|
|
|
static const char* textHtmlEditorFlavors[] = { kUnicodeMime, kHTMLMime,
|
2012-06-08 03:27:07 +04:00
|
|
|
kJPEGImageMime, kJPGImageMime,
|
|
|
|
kPNGImageMime, kGIFImageMime };
|
2010-01-11 04:45:45 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::CanPaste(int32_t aSelectionType, bool *aCanPaste)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2007-07-26 08:14:33 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aCanPaste);
|
2011-10-17 18:59:28 +04:00
|
|
|
*aCanPaste = false;
|
2007-07-26 08:14:33 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// can't paste if readonly
|
2012-03-27 01:58:43 +04:00
|
|
|
if (!IsModifiable()) {
|
2001-01-28 23:13:07 +03:00
|
|
|
return NS_OK;
|
2012-03-27 01:58:43 +04:00
|
|
|
}
|
2008-02-24 13:55:37 +03:00
|
|
|
|
|
|
|
nsresult rv;
|
2003-07-09 00:22:56 +04:00
|
|
|
nsCOMPtr<nsIClipboard> clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2010-04-12 06:35:18 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool haveFlavors;
|
2007-11-20 22:06:30 +03:00
|
|
|
|
|
|
|
// Use the flavors depending on the current editor mask
|
2010-04-12 06:35:18 +04:00
|
|
|
if (IsPlaintextEditor())
|
2007-11-20 22:06:30 +03:00
|
|
|
rv = clipboard->HasDataMatchingFlavors(textEditorFlavors,
|
2011-10-11 09:50:08 +04:00
|
|
|
ArrayLength(textEditorFlavors),
|
2007-11-20 22:06:30 +03:00
|
|
|
aSelectionType, &haveFlavors);
|
|
|
|
else
|
|
|
|
rv = clipboard->HasDataMatchingFlavors(textHtmlEditorFlavors,
|
2011-10-11 09:50:08 +04:00
|
|
|
ArrayLength(textHtmlEditorFlavors),
|
2007-11-20 22:06:30 +03:00
|
|
|
aSelectionType, &haveFlavors);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-04-07 04:45:26 +04:00
|
|
|
*aCanPaste = haveFlavors;
|
2001-01-28 23:13:07 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::CanPasteTransferable(nsITransferable *aTransferable, bool *aCanPaste)
|
2010-01-11 04:45:45 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aCanPaste);
|
|
|
|
|
|
|
|
// can't paste if readonly
|
|
|
|
if (!IsModifiable()) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aCanPaste = false;
|
2010-01-11 04:45:45 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If |aTransferable| is null, assume that a paste will succeed.
|
|
|
|
if (!aTransferable) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aCanPaste = true;
|
2010-01-11 04:45:45 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Peek in |aTransferable| to see if it contains a supported MIME type.
|
|
|
|
|
|
|
|
// Use the flavors depending on the current editor mask
|
|
|
|
const char ** flavors;
|
|
|
|
unsigned length;
|
2010-04-12 06:35:18 +04:00
|
|
|
if (IsPlaintextEditor()) {
|
2010-01-11 04:45:45 +03:00
|
|
|
flavors = textEditorFlavors;
|
2011-10-11 09:50:08 +04:00
|
|
|
length = ArrayLength(textEditorFlavors);
|
2010-01-11 04:45:45 +03:00
|
|
|
} else {
|
|
|
|
flavors = textHtmlEditorFlavors;
|
2011-10-11 09:50:08 +04:00
|
|
|
length = ArrayLength(textHtmlEditorFlavors);
|
2010-01-11 04:45:45 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < length; i++, flavors++) {
|
|
|
|
nsCOMPtr<nsISupports> data;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t dataLen;
|
2010-01-11 04:45:45 +03:00
|
|
|
nsresult rv = aTransferable->GetTransferData(*flavors,
|
|
|
|
getter_AddRefs(data),
|
|
|
|
&dataLen);
|
|
|
|
if (NS_SUCCEEDED(rv) && data) {
|
2011-10-17 18:59:28 +04:00
|
|
|
*aCanPaste = true;
|
2010-01-11 04:45:45 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*aCanPaste = false;
|
2010-01-11 04:45:45 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
//
|
2001-01-28 23:13:07 +03:00
|
|
|
// HTML PasteAsQuotation: Paste in a blockquote type=cite
|
|
|
|
//
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::PasteAsQuotation(int32_t aSelectionType)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2010-04-12 06:35:18 +04:00
|
|
|
if (IsPlaintextEditor())
|
2001-01-28 23:13:07 +03:00
|
|
|
return PasteAsPlaintextQuotation(aSelectionType);
|
|
|
|
|
|
|
|
nsAutoString citation;
|
|
|
|
return PasteAsCitedQuotation(citation, aSelectionType);
|
|
|
|
}
|
|
|
|
|
2002-03-15 01:11:14 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::PasteAsCitedQuotation(const nsAString & aCitation,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aSelectionType)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2016-06-23 12:46:58 +03:00
|
|
|
AutoEditBatch beginBatching(this);
|
2016-06-23 12:15:42 +03:00
|
|
|
AutoRules beginRulesSniffing(this, EditAction::insertQuotation,
|
|
|
|
nsIEditor::eNext);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// get selection
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
2010-06-17 23:41:16 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// give rules a chance to handle or cancel
|
2012-08-12 22:28:26 +04:00
|
|
|
nsTextRulesInfo ruleInfo(EditAction::insertElement);
|
2011-09-29 10:19:26 +04:00
|
|
|
bool cancel, handled;
|
2013-03-23 07:47:57 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
2012-05-22 13:37:17 +04:00
|
|
|
nsresult rv = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (cancel || handled) {
|
|
|
|
return NS_OK; // rules canceled the operation
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2014-11-02 15:04:13 +03:00
|
|
|
nsCOMPtr<Element> newNode =
|
|
|
|
DeleteSelectionAndCreateElement(*nsGkAtoms::blockquote);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_TRUE(newNode, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
// Try to set type=cite. Ignore it if this fails.
|
2014-11-02 15:04:13 +03:00
|
|
|
newNode->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
|
|
|
|
NS_LITERAL_STRING("cite"), true);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
|
|
|
// Set the selection to the underneath the node we just inserted:
|
|
|
|
rv = selection->Collapse(newNode, 0);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
return Paste(aSelectionType);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Paste a plaintext quotation
|
|
|
|
//
|
2012-08-22 19:56:38 +04:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::PasteAsPlaintextQuotation(int32_t aSelectionType)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
|
|
|
// Get Clipboard Service
|
|
|
|
nsresult rv;
|
2003-07-09 00:22:56 +04:00
|
|
|
nsCOMPtr<nsIClipboard> clipboard(do_GetService("@mozilla.org/widget/clipboard;1", &rv));
|
2010-06-17 23:27:24 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
// Create generic Transferable for getting the data
|
2003-07-09 00:22:56 +04:00
|
|
|
nsCOMPtr<nsITransferable> trans =
|
|
|
|
do_CreateInstance("@mozilla.org/widget/transferable;1", &rv);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
NS_ENSURE_TRUE(trans, NS_ERROR_FAILURE);
|
2003-07-28 17:13:50 +04:00
|
|
|
|
2012-04-17 06:14:01 +04:00
|
|
|
nsCOMPtr<nsIDocument> destdoc = GetDocument();
|
2012-07-30 18:20:58 +04:00
|
|
|
nsILoadContext* loadContext = destdoc ? destdoc->GetLoadContext() : nullptr;
|
2012-04-17 06:14:01 +04:00
|
|
|
trans->Init(loadContext);
|
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
// 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;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = 0;
|
2012-03-27 01:58:43 +04:00
|
|
|
char* flav = 0;
|
|
|
|
rv = trans->GetAnyTransferData(&flav, getter_AddRefs(genericDataObj), &len);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (flav && 0 == nsCRT::strcmp(flav, kUnicodeMime))
|
|
|
|
{
|
|
|
|
nsCOMPtr<nsISupportsString> textDataObj = do_QueryInterface(genericDataObj);
|
|
|
|
if (textDataObj && len > 0)
|
|
|
|
{
|
|
|
|
nsAutoString stuffToPaste;
|
|
|
|
textDataObj->GetData(stuffToPaste);
|
|
|
|
NS_ASSERTION(stuffToPaste.Length() <= (len/2), "Invalid length!");
|
2016-06-23 12:46:58 +03:00
|
|
|
AutoEditBatch beginBatching(this);
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = InsertAsPlaintextQuotation(stuffToPaste, true, 0);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
}
|
2015-04-01 08:29:55 +03:00
|
|
|
free(flav);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-10-09 00:24:23 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsHTMLEditor::InsertTextWithQuotations(const nsAString &aStringToInsert)
|
|
|
|
{
|
2002-10-18 23:25:32 +04:00
|
|
|
// The whole operation should be undoable in one transaction:
|
|
|
|
BeginTransaction();
|
|
|
|
|
2002-10-09 00:24:23 +04:00
|
|
|
// We're going to loop over the string, collecting up a "hunk"
|
|
|
|
// that's all the same type (quoted or not),
|
|
|
|
// Whenever the quotedness changes (or we reach the string's end)
|
|
|
|
// we will insert the hunk all at once, quoted or non.
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
static const char16_t cite('>');
|
2011-09-29 10:19:26 +04:00
|
|
|
bool curHunkIsQuoted = (aStringToInsert.First() == cite);
|
2002-10-09 00:24:23 +04:00
|
|
|
|
|
|
|
nsAString::const_iterator hunkStart, strEnd;
|
|
|
|
aStringToInsert.BeginReading(hunkStart);
|
|
|
|
aStringToInsert.EndReading(strEnd);
|
|
|
|
|
|
|
|
// In the loop below, we only look for DOM newlines (\n),
|
|
|
|
// because we don't have a FindChars method that can look
|
|
|
|
// for both \r and \n. \r is illegal in the dom anyway,
|
|
|
|
// but in debug builds, let's take the time to verify that
|
|
|
|
// there aren't any there:
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsAString::const_iterator dbgStart (hunkStart);
|
|
|
|
if (FindCharInReadable('\r', dbgStart, strEnd))
|
2011-10-17 18:59:28 +04:00
|
|
|
NS_ASSERTION(false,
|
2002-10-09 00:24:23 +04:00
|
|
|
"Return characters in DOM! InsertTextWithQuotations may be wrong");
|
|
|
|
#endif /* DEBUG */
|
|
|
|
|
|
|
|
// Loop over lines:
|
2002-10-09 05:33:31 +04:00
|
|
|
nsresult rv = NS_OK;
|
2002-10-09 00:24:23 +04:00
|
|
|
nsAString::const_iterator lineStart (hunkStart);
|
2002-10-18 23:25:32 +04:00
|
|
|
while (1) // we will break from inside when we run out of newlines
|
2002-10-09 00:24:23 +04:00
|
|
|
{
|
|
|
|
// Search for the end of this line (dom newlines, see above):
|
2011-09-29 10:19:26 +04:00
|
|
|
bool found = FindCharInReadable('\n', lineStart, strEnd);
|
|
|
|
bool quoted = false;
|
2002-10-09 00:24:23 +04:00
|
|
|
if (found)
|
|
|
|
{
|
|
|
|
// if there's another newline, lineStart now points there.
|
|
|
|
// Loop over any consecutive newline chars:
|
|
|
|
nsAString::const_iterator firstNewline (lineStart);
|
|
|
|
while (*lineStart == '\n')
|
|
|
|
++lineStart;
|
|
|
|
quoted = (*lineStart == cite);
|
|
|
|
if (quoted == curHunkIsQuoted)
|
|
|
|
continue;
|
|
|
|
// else we're changing state, so we need to insert
|
|
|
|
// from curHunk to lineStart then loop around.
|
|
|
|
|
|
|
|
// But if the current hunk is quoted, then we want to make sure
|
|
|
|
// that any extra newlines on the end do not get included in
|
|
|
|
// the quoted section: blank lines flaking a quoted section
|
|
|
|
// should be considered unquoted, so that if the user clicks
|
|
|
|
// there and starts typing, the new text will be outside of
|
|
|
|
// the quoted block.
|
|
|
|
if (curHunkIsQuoted)
|
|
|
|
lineStart = firstNewline;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If no newline found, lineStart is now strEnd and we can finish up,
|
|
|
|
// inserting from curHunk to lineStart then returning.
|
2002-10-09 01:18:41 +04:00
|
|
|
const nsAString &curHunk = Substring(hunkStart, lineStart);
|
2002-10-09 00:24:23 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> dummyNode;
|
|
|
|
if (curHunkIsQuoted)
|
2011-10-17 18:59:28 +04:00
|
|
|
rv = InsertAsPlaintextQuotation(curHunk, false,
|
2002-10-09 00:24:23 +04:00
|
|
|
getter_AddRefs(dummyNode));
|
|
|
|
else
|
|
|
|
rv = InsertText(curHunk);
|
|
|
|
|
|
|
|
if (!found)
|
|
|
|
break;
|
|
|
|
|
|
|
|
curHunkIsQuoted = quoted;
|
|
|
|
hunkStart = lineStart;
|
|
|
|
}
|
|
|
|
|
2002-10-18 23:25:32 +04:00
|
|
|
EndTransaction();
|
|
|
|
|
2002-10-09 00:24:23 +04:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2002-03-15 01:11:14 +03:00
|
|
|
NS_IMETHODIMP nsHTMLEditor::InsertAsQuotation(const nsAString & aQuotedText,
|
2001-01-28 23:13:07 +03:00
|
|
|
nsIDOMNode **aNodeInserted)
|
|
|
|
{
|
2010-04-12 06:35:18 +04:00
|
|
|
if (IsPlaintextEditor())
|
2011-10-17 18:59:28 +04:00
|
|
|
return InsertAsPlaintextQuotation(aQuotedText, true, aNodeInserted);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
|
|
|
nsAutoString citation;
|
2011-10-17 18:59:28 +04:00
|
|
|
return InsertAsCitedQuotation(aQuotedText, citation, false,
|
2003-06-12 18:51:37 +04:00
|
|
|
aNodeInserted);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2001-04-06 03:48:01 +04:00
|
|
|
// Insert plaintext as a quotation, with cite marks (e.g. "> ").
|
|
|
|
// This differs from its corresponding method in nsPlaintextEditor
|
|
|
|
// in that here, quoted material is enclosed in a <pre> tag
|
|
|
|
// in order to preserve the original line wrapping.
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-15 01:11:14 +03:00
|
|
|
nsHTMLEditor::InsertAsPlaintextQuotation(const nsAString & aQuotedText,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aAddCites,
|
2001-01-28 23:13:07 +03:00
|
|
|
nsIDOMNode **aNodeInserted)
|
|
|
|
{
|
|
|
|
// get selection
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
2012-03-03 02:23:01 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2016-06-23 12:46:58 +03:00
|
|
|
AutoEditBatch beginBatching(this);
|
2016-06-23 12:15:42 +03:00
|
|
|
AutoRules beginRulesSniffing(this, EditAction::insertQuotation,
|
|
|
|
nsIEditor::eNext);
|
2012-03-03 02:23:01 +04:00
|
|
|
|
|
|
|
// give rules a chance to handle or cancel
|
2012-08-12 22:28:26 +04:00
|
|
|
nsTextRulesInfo ruleInfo(EditAction::insertElement);
|
2012-03-03 02:23:01 +04:00
|
|
|
bool cancel, handled;
|
2013-03-23 07:47:57 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
2012-05-22 13:37:17 +04:00
|
|
|
nsresult rv = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
2012-03-03 02:23:01 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-03-27 01:58:43 +04:00
|
|
|
if (cancel || handled) {
|
|
|
|
return NS_OK; // rules canceled the operation
|
|
|
|
}
|
|
|
|
|
2016-04-05 01:22:00 +03:00
|
|
|
// Wrap the inserted quote in a <span> so we can distinguish it.
|
2014-11-02 15:04:13 +03:00
|
|
|
nsCOMPtr<Element> newNode =
|
|
|
|
DeleteSelectionAndCreateElement(*nsGkAtoms::span);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
|
|
|
// If this succeeded, then set selection inside the pre
|
|
|
|
// so the inserted text will end up there.
|
|
|
|
// If it failed, we don't care what the return value was,
|
|
|
|
// but we'll fall through and try to insert the text anyway.
|
2014-11-02 15:04:13 +03:00
|
|
|
if (newNode) {
|
2012-03-27 01:58:43 +04:00
|
|
|
// Add an attribute on the pre node so we'll know it's a quotation.
|
2014-11-02 15:04:13 +03:00
|
|
|
newNode->SetAttr(kNameSpaceID_None, nsGkAtoms::mozquote,
|
|
|
|
NS_LITERAL_STRING("true"), true);
|
2016-04-05 01:22:00 +03:00
|
|
|
// Allow wrapping on spans so long lines get wrapped to the screen.
|
2014-11-02 15:04:13 +03:00
|
|
|
newNode->SetAttr(kNameSpaceID_None, nsGkAtoms::style,
|
2016-04-05 01:22:00 +03:00
|
|
|
NS_LITERAL_STRING("white-space: pre-wrap;"), true);
|
2014-11-02 15:04:13 +03:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
// and set the selection inside it:
|
|
|
|
selection->Collapse(newNode, 0);
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
if (aAddCites)
|
|
|
|
rv = nsPlaintextEditor::InsertAsQuotation(aQuotedText, aNodeInserted);
|
|
|
|
else
|
|
|
|
rv = nsPlaintextEditor::InsertText(aQuotedText);
|
|
|
|
// Note that if !aAddCites, aNodeInserted isn't set.
|
|
|
|
// That's okay because the routines that use aAddCites
|
|
|
|
// don't need to know the inserted node.
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
if (aNodeInserted && NS_SUCCEEDED(rv))
|
|
|
|
{
|
2014-11-02 15:04:13 +03:00
|
|
|
*aNodeInserted = GetAsDOMNode(newNode);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_IF_ADDREF(*aNodeInserted);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-03-03 02:23:01 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// Set the selection to just after the inserted node:
|
2012-03-27 01:58:43 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && newNode)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2014-11-02 15:04:13 +03:00
|
|
|
nsCOMPtr<nsINode> parent = newNode->GetParentNode();
|
|
|
|
int32_t offset = parent ? parent->IndexOf(newNode) : -1;
|
2012-07-02 16:07:57 +04:00
|
|
|
if (parent) {
|
|
|
|
selection->Collapse(parent, offset + 1);
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_IMETHODIMP
|
2003-07-28 17:13:50 +04:00
|
|
|
nsHTMLEditor::StripCites()
|
|
|
|
{
|
|
|
|
return nsPlaintextEditor::StripCites();
|
|
|
|
}
|
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsHTMLEditor::Rewrap(bool aRespectNewlines)
|
2003-07-28 17:13:50 +04:00
|
|
|
{
|
|
|
|
return nsPlaintextEditor::Rewrap(aRespectNewlines);
|
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
NS_IMETHODIMP
|
2002-03-15 01:11:14 +03:00
|
|
|
nsHTMLEditor::InsertAsCitedQuotation(const nsAString & aQuotedText,
|
|
|
|
const nsAString & aCitation,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aInsertHTML,
|
2001-01-28 23:13:07 +03:00
|
|
|
nsIDOMNode **aNodeInserted)
|
|
|
|
{
|
2002-10-17 04:07:28 +04:00
|
|
|
// Don't let anyone insert html into a "plaintext" editor:
|
2010-04-12 06:35:18 +04:00
|
|
|
if (IsPlaintextEditor())
|
2002-10-17 04:07:28 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(!aInsertHTML, "InsertAsCitedQuotation: trying to insert html into plaintext editor");
|
2011-10-17 18:59:28 +04:00
|
|
|
return InsertAsPlaintextQuotation(aQuotedText, true, aNodeInserted);
|
2002-10-17 04:07:28 +04:00
|
|
|
}
|
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// get selection
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Selection> selection = GetSelection();
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_TRUE(selection, NS_ERROR_NULL_POINTER);
|
|
|
|
|
2016-06-23 12:46:58 +03:00
|
|
|
AutoEditBatch beginBatching(this);
|
2016-06-23 12:15:42 +03:00
|
|
|
AutoRules beginRulesSniffing(this, EditAction::insertQuotation,
|
|
|
|
nsIEditor::eNext);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
|
|
|
// give rules a chance to handle or cancel
|
2012-08-12 22:28:26 +04:00
|
|
|
nsTextRulesInfo ruleInfo(EditAction::insertElement);
|
2012-03-27 01:58:43 +04:00
|
|
|
bool cancel, handled;
|
2013-03-23 07:47:57 +04:00
|
|
|
// Protect the edit rules object from dying
|
|
|
|
nsCOMPtr<nsIEditRules> kungFuDeathGrip(mRules);
|
2012-05-22 13:37:17 +04:00
|
|
|
nsresult rv = mRules->WillDoAction(selection, &ruleInfo, &cancel, &handled);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
if (cancel || handled) {
|
|
|
|
return NS_OK; // rules canceled the operation
|
2010-06-17 23:41:16 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2014-11-02 15:04:13 +03:00
|
|
|
nsCOMPtr<Element> newNode =
|
|
|
|
DeleteSelectionAndCreateElement(*nsGkAtoms::blockquote);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_TRUE(newNode, NS_ERROR_NULL_POINTER);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
// Try to set type=cite. Ignore it if this fails.
|
2014-11-02 15:04:13 +03:00
|
|
|
newNode->SetAttr(kNameSpaceID_None, nsGkAtoms::type,
|
|
|
|
NS_LITERAL_STRING("cite"), true);
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2014-11-02 15:04:13 +03:00
|
|
|
if (!aCitation.IsEmpty()) {
|
|
|
|
newNode->SetAttr(kNameSpaceID_None, nsGkAtoms::cite, aCitation, true);
|
2012-03-27 01:58:43 +04:00
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2014-11-02 15:04:13 +03:00
|
|
|
// Set the selection inside the blockquote so aQuotedText will go there:
|
|
|
|
selection->Collapse(newNode, 0);
|
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
if (aInsertHTML)
|
|
|
|
rv = LoadHTML(aQuotedText);
|
|
|
|
else
|
|
|
|
rv = InsertText(aQuotedText); // XXX ignore charset
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
if (aNodeInserted && NS_SUCCEEDED(rv))
|
|
|
|
{
|
2014-11-02 15:04:13 +03:00
|
|
|
*aNodeInserted = GetAsDOMNode(newNode);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_IF_ADDREF(*aNodeInserted);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Set the selection to just after the inserted node:
|
2012-03-27 01:58:43 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && newNode)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2014-11-02 15:04:13 +03:00
|
|
|
nsCOMPtr<nsINode> parent = newNode->GetParentNode();
|
|
|
|
int32_t offset = parent ? parent->IndexOf(newNode) : -1;
|
2012-07-02 16:07:57 +04:00
|
|
|
if (parent) {
|
|
|
|
selection->Collapse(parent, offset + 1);
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
return rv;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2002-11-22 06:30:59 +03:00
|
|
|
|
2015-05-13 18:58:25 +03:00
|
|
|
void RemoveBodyAndHead(nsINode& aNode)
|
2002-11-22 06:30:59 +03:00
|
|
|
{
|
2015-05-13 18:58:25 +03:00
|
|
|
nsCOMPtr<nsIContent> body, head;
|
2002-11-22 06:30:59 +03:00
|
|
|
// find the body and head nodes if any.
|
|
|
|
// look only at immediate children of aNode.
|
2015-05-13 18:58:25 +03:00
|
|
|
for (nsCOMPtr<nsIContent> child = aNode.GetFirstChild();
|
|
|
|
child;
|
|
|
|
child = child->GetNextSibling()) {
|
|
|
|
if (child->IsHTMLElement(nsGkAtoms::body)) {
|
2002-11-22 06:30:59 +03:00
|
|
|
body = child;
|
2015-05-13 18:58:25 +03:00
|
|
|
} else if (child->IsHTMLElement(nsGkAtoms::head)) {
|
2002-11-22 06:30:59 +03:00
|
|
|
head = child;
|
|
|
|
}
|
|
|
|
}
|
2015-05-13 18:58:25 +03:00
|
|
|
if (head) {
|
|
|
|
ErrorResult ignored;
|
|
|
|
aNode.RemoveChild(*head, ignored);
|
2002-11-22 06:30:59 +03:00
|
|
|
}
|
2015-05-13 18:58:25 +03:00
|
|
|
if (body) {
|
|
|
|
nsCOMPtr<nsIContent> child = body->GetFirstChild();
|
|
|
|
while (child) {
|
|
|
|
ErrorResult ignored;
|
|
|
|
aNode.InsertBefore(*child, body, ignored);
|
|
|
|
child = body->GetFirstChild();
|
2002-11-22 06:30:59 +03:00
|
|
|
}
|
2015-05-13 18:58:25 +03:00
|
|
|
|
|
|
|
ErrorResult ignored;
|
|
|
|
aNode.RemoveChild(*body, ignored);
|
2002-11-22 06:30:59 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-07 02:48:55 +04:00
|
|
|
/**
|
|
|
|
* This function finds the target node that we will be pasting into. aStart is
|
|
|
|
* the context that we're given and aResult will be the target. Initially,
|
2013-04-03 04:13:10 +04:00
|
|
|
* *aResult must be nullptr.
|
2006-04-07 02:48:55 +04:00
|
|
|
*
|
|
|
|
* The target for a paste is found by either finding the node that contains
|
|
|
|
* the magical comment node containing kInsertCookie or, failing that, the
|
|
|
|
* firstChild of the firstChild (until we reach a leaf).
|
|
|
|
*/
|
2006-04-26 00:13:46 +04:00
|
|
|
nsresult FindTargetNode(nsIDOMNode *aStart, nsCOMPtr<nsIDOMNode> &aResult)
|
2006-04-07 02:48:55 +04:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(aStart, NS_OK);
|
2006-04-07 02:48:55 +04:00
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> child, tmp;
|
|
|
|
|
|
|
|
nsresult rv = aStart->GetFirstChild(getter_AddRefs(child));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (!child)
|
|
|
|
{
|
2013-04-03 04:13:10 +04:00
|
|
|
// If the current result is nullptr, then aStart is a leaf, and is the
|
2006-04-07 02:48:55 +04:00
|
|
|
// fallback result.
|
2006-04-26 00:13:46 +04:00
|
|
|
if (!aResult)
|
|
|
|
aResult = aStart;
|
2006-04-07 02:48:55 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
do
|
|
|
|
{
|
|
|
|
// Is this child the magical cookie?
|
|
|
|
nsCOMPtr<nsIDOMComment> comment = do_QueryInterface(child);
|
|
|
|
if (comment)
|
|
|
|
{
|
|
|
|
nsAutoString data;
|
|
|
|
rv = comment->GetData(data);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (data.EqualsLiteral(kInsertCookie))
|
|
|
|
{
|
|
|
|
// Yes it is! Return an error so we bubble out and short-circuit the
|
|
|
|
// search.
|
2006-04-26 00:13:46 +04:00
|
|
|
aResult = aStart;
|
2006-04-07 02:48:55 +04:00
|
|
|
|
2006-04-26 00:13:46 +04:00
|
|
|
// Note: it doesn't matter if this fails.
|
|
|
|
aStart->RemoveChild(child, getter_AddRefs(tmp));
|
2006-04-07 02:48:55 +04:00
|
|
|
|
|
|
|
return NS_FOUND_TARGET;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note: Don't use NS_ENSURE_* here since we return a failure result to
|
|
|
|
// inicate that we found the magical cookie and we don't want to spam the
|
|
|
|
// console.
|
2006-04-26 00:13:46 +04:00
|
|
|
rv = FindTargetNode(child, aResult);
|
2010-06-18 00:44:35 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-04-07 02:48:55 +04:00
|
|
|
|
|
|
|
rv = child->GetNextSibling(getter_AddRefs(tmp));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
child = tmp;
|
|
|
|
} while (child);
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-11-22 06:30:59 +03:00
|
|
|
|
2003-04-15 18:07:30 +04:00
|
|
|
nsresult nsHTMLEditor::CreateDOMFragmentFromPaste(const nsAString &aInputString,
|
2002-03-24 01:08:20 +03:00
|
|
|
const nsAString & aContextStr,
|
|
|
|
const nsAString & aInfoStr,
|
2001-01-28 23:13:07 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> *outFragNode,
|
2006-04-26 00:13:46 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> *outStartNode,
|
|
|
|
nsCOMPtr<nsIDOMNode> *outEndNode,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t *outStartOffset,
|
|
|
|
int32_t *outEndOffset,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aTrustedInput)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(outFragNode && outStartNode && outEndNode, NS_ERROR_NULL_POINTER);
|
2003-04-15 05:09:09 +04:00
|
|
|
|
2012-03-24 12:19:14 +04:00
|
|
|
nsCOMPtr<nsIDocument> doc = GetDocument();
|
2009-07-10 10:17:46 +04:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_FAILURE);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2001-03-09 04:36:38 +03:00
|
|
|
// if we have context info, create a fragment for that
|
2015-05-13 18:58:25 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
nsCOMPtr<nsIDOMNode> contextLeaf;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DocumentFragment> contextAsNode;
|
2015-05-13 18:58:25 +03:00
|
|
|
if (!aContextStr.IsEmpty()) {
|
2015-05-13 18:58:25 +03:00
|
|
|
rv = ParseFragment(aContextStr, nullptr, doc, getter_AddRefs(contextAsNode),
|
2012-03-27 01:58:43 +04:00
|
|
|
aTrustedInput);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-11-22 06:30:59 +03:00
|
|
|
NS_ENSURE_TRUE(contextAsNode, NS_ERROR_FAILURE);
|
|
|
|
|
2015-05-13 18:58:25 +03:00
|
|
|
rv = StripFormattingNodes(*contextAsNode);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-04-26 00:13:46 +04:00
|
|
|
|
2015-05-13 18:58:25 +03:00
|
|
|
RemoveBodyAndHead(*contextAsNode);
|
2006-04-07 02:48:55 +04:00
|
|
|
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = FindTargetNode(contextAsNode, contextLeaf);
|
|
|
|
if (rv == NS_FOUND_TARGET) {
|
|
|
|
rv = NS_OK;
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2001-03-09 04:36:38 +03:00
|
|
|
}
|
2006-04-26 00:13:46 +04:00
|
|
|
|
2011-07-29 15:48:04 +04:00
|
|
|
nsCOMPtr<nsIContent> contextLeafAsContent = do_QueryInterface(contextLeaf);
|
2009-07-10 10:17:46 +04:00
|
|
|
|
|
|
|
// create fragment for pasted html
|
2011-07-29 15:48:04 +04:00
|
|
|
nsIAtom* contextAtom;
|
|
|
|
if (contextLeafAsContent) {
|
2015-03-03 14:08:59 +03:00
|
|
|
contextAtom = contextLeafAsContent->NodeInfo()->NameAtom();
|
|
|
|
if (contextLeafAsContent->IsHTMLElement(nsGkAtoms::html)) {
|
2011-07-29 15:48:04 +04:00
|
|
|
contextAtom = nsGkAtoms::body;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
contextAtom = nsGkAtoms::body;
|
|
|
|
}
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DocumentFragment> fragment;
|
2012-03-27 01:58:43 +04:00
|
|
|
rv = ParseFragment(aInputString,
|
|
|
|
contextAtom,
|
|
|
|
doc,
|
2015-05-13 18:58:25 +03:00
|
|
|
getter_AddRefs(fragment),
|
2012-03-27 01:58:43 +04:00
|
|
|
aTrustedInput);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-05-13 18:58:25 +03:00
|
|
|
NS_ENSURE_TRUE(fragment, NS_ERROR_FAILURE);
|
2006-04-07 02:48:55 +04:00
|
|
|
|
2015-05-13 18:58:25 +03:00
|
|
|
RemoveBodyAndHead(*fragment);
|
2006-04-07 02:48:55 +04:00
|
|
|
|
2015-05-13 18:58:25 +03:00
|
|
|
if (contextAsNode) {
|
2001-01-28 23:13:07 +03:00
|
|
|
// unite the two trees
|
2015-05-13 18:58:25 +03:00
|
|
|
nsCOMPtr<nsIDOMNode> junk;
|
2015-05-13 18:58:25 +03:00
|
|
|
contextLeaf->AppendChild(fragment, getter_AddRefs(junk));
|
|
|
|
fragment = contextAsNode;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2006-04-07 02:48:55 +04:00
|
|
|
|
2015-05-13 18:58:25 +03:00
|
|
|
rv = StripFormattingNodes(*fragment, true);
|
2012-03-27 01:58:43 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-04-27 04:20:27 +04:00
|
|
|
|
|
|
|
// If there was no context, then treat all of the data we did get as the
|
|
|
|
// pasted data.
|
2015-05-13 18:58:25 +03:00
|
|
|
if (contextLeaf) {
|
2006-04-27 04:20:27 +04:00
|
|
|
*outEndNode = *outStartNode = contextLeaf;
|
2015-05-13 18:58:25 +03:00
|
|
|
} else {
|
2015-05-13 18:58:25 +03:00
|
|
|
*outEndNode = *outStartNode = fragment;
|
2015-05-13 18:58:25 +03:00
|
|
|
}
|
2006-04-27 04:20:27 +04:00
|
|
|
|
2015-05-13 18:58:25 +03:00
|
|
|
*outFragNode = fragment.forget();
|
2006-04-26 00:13:46 +04:00
|
|
|
*outStartOffset = 0;
|
2006-04-07 02:48:55 +04:00
|
|
|
|
2001-01-28 23:13:07 +03:00
|
|
|
// get the infoString contents
|
2015-05-13 18:58:25 +03:00
|
|
|
if (!aInfoStr.IsEmpty()) {
|
|
|
|
int32_t sep = aInfoStr.FindChar((char16_t)',');
|
|
|
|
nsAutoString numstr1(Substring(aInfoStr, 0, sep));
|
|
|
|
nsAutoString numstr2(Substring(aInfoStr, sep+1, aInfoStr.Length() - (sep+1)));
|
2006-04-26 00:13:46 +04:00
|
|
|
|
|
|
|
// Move the start and end children.
|
2012-07-27 17:59:29 +04:00
|
|
|
nsresult err;
|
2015-05-13 18:58:25 +03:00
|
|
|
int32_t num = numstr1.ToInteger(&err);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMNode> tmp;
|
|
|
|
while (num--) {
|
2006-04-26 00:13:46 +04:00
|
|
|
(*outStartNode)->GetFirstChild(getter_AddRefs(tmp));
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(tmp, NS_ERROR_FAILURE);
|
2006-04-26 00:13:46 +04:00
|
|
|
tmp.swap(*outStartNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
num = numstr2.ToInteger(&err);
|
2015-05-13 18:58:25 +03:00
|
|
|
while (num--) {
|
2006-04-26 00:13:46 +04:00
|
|
|
(*outEndNode)->GetLastChild(getter_AddRefs(tmp));
|
2010-06-18 00:40:48 +04:00
|
|
|
NS_ENSURE_TRUE(tmp, NS_ERROR_FAILURE);
|
2006-04-26 00:13:46 +04:00
|
|
|
tmp.swap(*outEndNode);
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
2006-04-26 00:13:46 +04:00
|
|
|
|
2015-05-13 18:58:25 +03:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(*outEndNode);
|
|
|
|
*outEndOffset = node->Length();
|
2012-03-27 01:58:43 +04:00
|
|
|
return NS_OK;
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2009-07-10 10:17:46 +04:00
|
|
|
|
|
|
|
nsresult nsHTMLEditor::ParseFragment(const nsAString & aFragStr,
|
2011-07-29 15:48:04 +04:00
|
|
|
nsIAtom* aContextLocalName,
|
2009-07-10 10:17:46 +04:00
|
|
|
nsIDocument* aTargetDocument,
|
2015-05-13 18:58:25 +03:00
|
|
|
DocumentFragment** aFragment,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool aTrustedInput)
|
2009-07-10 10:17:46 +04:00
|
|
|
{
|
2012-06-14 10:14:47 +04:00
|
|
|
nsAutoScriptBlockerSuppressNodeRemoved autoBlocker;
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DocumentFragment> fragment =
|
2013-04-10 18:15:54 +04:00
|
|
|
new DocumentFragment(aTargetDocument->NodeInfoManager());
|
2012-03-27 01:58:43 +04:00
|
|
|
nsresult rv = nsContentUtils::ParseFragmentHTML(aFragStr,
|
|
|
|
fragment,
|
|
|
|
aContextLocalName ?
|
|
|
|
aContextLocalName : nsGkAtoms::body,
|
|
|
|
kNameSpaceID_XHTML,
|
|
|
|
false,
|
|
|
|
true);
|
2011-07-29 15:48:04 +04:00
|
|
|
if (!aTrustedInput) {
|
2012-03-19 12:16:20 +04:00
|
|
|
nsTreeSanitizer sanitizer(aContextLocalName ?
|
|
|
|
nsIParserUtils::SanitizerAllowStyle :
|
|
|
|
nsIParserUtils::SanitizerAllowComments);
|
2011-07-29 15:48:04 +04:00
|
|
|
sanitizer.Sanitize(fragment);
|
|
|
|
}
|
2015-05-13 18:58:25 +03:00
|
|
|
fragment.forget(aFragment);
|
2011-10-14 14:39:19 +04:00
|
|
|
return rv;
|
2009-07-10 10:17:46 +04:00
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
void
|
|
|
|
nsHTMLEditor::CreateListOfNodesToPaste(DocumentFragment& aFragment,
|
|
|
|
nsTArray<OwningNonNull<nsINode>>& outNodeList,
|
|
|
|
nsINode* aStartNode,
|
|
|
|
int32_t aStartOffset,
|
|
|
|
nsINode* aEndNode,
|
|
|
|
int32_t aEndOffset)
|
2001-01-28 23:13:07 +03:00
|
|
|
{
|
2015-04-24 14:27:35 +03:00
|
|
|
// If no info was provided about the boundary between context and stream,
|
2003-04-15 18:07:30 +04:00
|
|
|
// then assume all is stream.
|
2015-04-24 14:27:35 +03:00
|
|
|
if (!aStartNode) {
|
|
|
|
aStartNode = &aFragment;
|
2003-04-15 18:07:30 +04:00
|
|
|
aStartOffset = 0;
|
2015-04-24 14:27:35 +03:00
|
|
|
aEndNode = &aFragment;
|
|
|
|
aEndOffset = aFragment.Length();
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRange> docFragRange;
|
2015-04-24 14:27:35 +03:00
|
|
|
nsresult rv = nsRange::CreateRange(aStartNode, aStartOffset,
|
|
|
|
aEndNode, aEndOffset,
|
|
|
|
getter_AddRefs(docFragRange));
|
|
|
|
MOZ_ASSERT(NS_SUCCEEDED(rv));
|
|
|
|
NS_ENSURE_SUCCESS(rv, );
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
// Now use a subtree iterator over the range to create a list of nodes
|
2016-06-23 11:19:35 +03:00
|
|
|
TrivialFunctor functor;
|
2016-06-23 11:25:03 +03:00
|
|
|
DOMSubtreeIterator iter;
|
2015-05-22 16:58:30 +03:00
|
|
|
rv = iter.Init(*docFragRange);
|
|
|
|
NS_ENSURE_SUCCESS(rv, );
|
2015-04-24 14:27:34 +03:00
|
|
|
iter.AppendList(functor, outNodeList);
|
2001-01-28 23:13:07 +03:00
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
void
|
|
|
|
nsHTMLEditor::GetListAndTableParents(StartOrEnd aStartOrEnd,
|
|
|
|
nsTArray<OwningNonNull<nsINode>>& aNodeList,
|
|
|
|
nsTArray<OwningNonNull<Element>>& outArray)
|
2001-06-16 02:29:07 +04:00
|
|
|
{
|
2015-04-24 14:27:35 +03:00
|
|
|
MOZ_ASSERT(aNodeList.Length());
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
// Build up list of parents of first (or last) node in list that are either
|
|
|
|
// lists, or tables.
|
|
|
|
int32_t idx = aStartOrEnd == StartOrEnd::end ? aNodeList.Length() - 1 : 0;
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
for (nsCOMPtr<nsINode> node = aNodeList[idx]; node;
|
|
|
|
node = node->GetParentNode()) {
|
|
|
|
if (nsHTMLEditUtils::IsList(node) || nsHTMLEditUtils::IsTable(node)) {
|
|
|
|
outArray.AppendElement(*node->AsElement());
|
2001-06-16 02:29:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2001-01-28 23:13:07 +03:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
int32_t
|
|
|
|
nsHTMLEditor::DiscoverPartialListsAndTables(nsTArray<OwningNonNull<nsINode>>& aPasteNodes,
|
|
|
|
nsTArray<OwningNonNull<Element>>& aListsAndTables)
|
2001-06-16 02:29:07 +04:00
|
|
|
{
|
2015-04-24 14:27:35 +03:00
|
|
|
int32_t ret = -1;
|
|
|
|
int32_t listAndTableParents = aListsAndTables.Length();
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
// Scan insertion list for table elements (other than table).
|
|
|
|
for (auto& curNode : aPasteNodes) {
|
|
|
|
if (nsHTMLEditUtils::IsTableElement(curNode) &&
|
|
|
|
!curNode->IsHTMLElement(nsGkAtoms::table)) {
|
|
|
|
nsCOMPtr<Element> table = curNode->GetParentElement();
|
|
|
|
while (table && !table->IsHTMLElement(nsGkAtoms::table)) {
|
|
|
|
table = table->GetParentElement();
|
|
|
|
}
|
|
|
|
if (table) {
|
|
|
|
int32_t idx = aListsAndTables.IndexOf(table);
|
|
|
|
if (idx == -1) {
|
|
|
|
return ret;
|
2001-06-16 02:29:07 +04:00
|
|
|
}
|
2015-04-24 14:27:35 +03:00
|
|
|
ret = idx;
|
|
|
|
if (ret == listAndTableParents - 1) {
|
|
|
|
return ret;
|
2001-06-16 02:29:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 14:27:35 +03:00
|
|
|
if (nsHTMLEditUtils::IsListItem(curNode)) {
|
|
|
|
nsCOMPtr<Element> list = curNode->GetParentElement();
|
|
|
|
while (list && !nsHTMLEditUtils::IsList(list)) {
|
|
|
|
list = list->GetParentElement();
|
|
|
|
}
|
|
|
|
if (list) {
|
|
|
|
int32_t idx = aListsAndTables.IndexOf(list);
|
|
|
|
if (idx == -1) {
|
|
|
|
return ret;
|
2001-06-16 02:29:07 +04:00
|
|
|
}
|
2015-04-24 14:27:35 +03:00
|
|
|
ret = idx;
|
|
|
|
if (ret == listAndTableParents - 1) {
|
|
|
|
return ret;
|
2001-06-16 02:29:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 14:27:35 +03:00
|
|
|
return ret;
|
2001-06-16 02:29:07 +04:00
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
nsINode*
|
|
|
|
nsHTMLEditor::ScanForListAndTableStructure(StartOrEnd aStartOrEnd,
|
|
|
|
nsTArray<OwningNonNull<nsINode>>& aNodes,
|
|
|
|
Element& aListOrTable)
|
2001-06-16 02:29:07 +04:00
|
|
|
{
|
2015-04-24 14:27:35 +03:00
|
|
|
// Look upward from first/last paste node for a piece of this list/table
|
|
|
|
int32_t idx = aStartOrEnd == StartOrEnd::end ? aNodes.Length() - 1 : 0;
|
|
|
|
bool isList = nsHTMLEditUtils::IsList(&aListOrTable);
|
2015-05-28 18:58:42 +03:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
for (nsCOMPtr<nsINode> node = aNodes[idx]; node;
|
|
|
|
node = node->GetParentNode()) {
|
|
|
|
if ((isList && nsHTMLEditUtils::IsListItem(node)) ||
|
|
|
|
(!isList && nsHTMLEditUtils::IsTableElement(node) &&
|
|
|
|
!node->IsHTMLElement(nsGkAtoms::table))) {
|
|
|
|
nsCOMPtr<Element> structureNode = node->GetParentElement();
|
|
|
|
if (isList) {
|
|
|
|
while (structureNode && !nsHTMLEditUtils::IsList(structureNode)) {
|
|
|
|
structureNode = structureNode->GetParentElement();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
while (structureNode &&
|
|
|
|
!structureNode->IsHTMLElement(nsGkAtoms::table)) {
|
|
|
|
structureNode = structureNode->GetParentElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (structureNode == &aListOrTable) {
|
|
|
|
if (isList) {
|
|
|
|
return structureNode;
|
|
|
|
}
|
|
|
|
return node;
|
2001-06-16 02:29:07 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-24 14:27:35 +03:00
|
|
|
return nullptr;
|
2012-03-27 01:58:43 +04:00
|
|
|
}
|
2001-06-16 02:29:07 +04:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
void
|
|
|
|
nsHTMLEditor::ReplaceOrphanedStructure(StartOrEnd aStartOrEnd,
|
|
|
|
nsTArray<OwningNonNull<nsINode>>& aNodeArray,
|
|
|
|
nsTArray<OwningNonNull<Element>>& aListAndTableArray,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aHighWaterMark)
|
2001-06-16 02:29:07 +04:00
|
|
|
{
|
2015-04-24 14:27:35 +03:00
|
|
|
OwningNonNull<Element> curNode = aListAndTableArray[aHighWaterMark];
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
// Find substructure of list or table that must be included in paste.
|
|
|
|
nsCOMPtr<nsINode> replaceNode =
|
|
|
|
ScanForListAndTableStructure(aStartOrEnd, aNodeArray, curNode);
|
2012-03-27 01:58:43 +04:00
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
if (!replaceNode) {
|
|
|
|
return;
|
2001-06-16 02:29:07 +04:00
|
|
|
}
|
|
|
|
|
2015-04-24 14:27:35 +03:00
|
|
|
// If we found substructure, paste it instead of its descendants.
|
2016-02-27 04:34:40 +03:00
|
|
|
// Only replace with the substructure if all the nodes in the list are
|
|
|
|
// descendants.
|
|
|
|
bool shouldReplaceNodes = true;
|
|
|
|
for (uint32_t i = 0; i < aNodeArray.Length(); i++) {
|
|
|
|
uint32_t idx = aStartOrEnd == StartOrEnd::start ?
|
|
|
|
i : (aNodeArray.Length() - i - 1);
|
2015-04-24 14:27:35 +03:00
|
|
|
OwningNonNull<nsINode> endpoint = aNodeArray[idx];
|
2016-07-07 05:49:42 +03:00
|
|
|
if (!EditorUtils::IsDescendantOf(endpoint, replaceNode)) {
|
2016-02-27 04:34:40 +03:00
|
|
|
shouldReplaceNodes = false;
|
2015-04-24 14:27:35 +03:00
|
|
|
break;
|
|
|
|
}
|
2012-03-27 01:58:43 +04:00
|
|
|
}
|
2005-01-15 00:49:16 +03:00
|
|
|
|
2016-02-27 04:34:40 +03:00
|
|
|
if (shouldReplaceNodes) {
|
|
|
|
// Now replace the removed nodes with the structural parent
|
|
|
|
aNodeArray.Clear();
|
|
|
|
if (aStartOrEnd == StartOrEnd::end) {
|
|
|
|
aNodeArray.AppendElement(*replaceNode);
|
|
|
|
} else {
|
|
|
|
aNodeArray.InsertElementAt(0, *replaceNode);
|
|
|
|
}
|
2001-06-16 02:29:07 +04:00
|
|
|
}
|
|
|
|
}
|