2017-10-27 01:08:41 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
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/. */
|
2000-03-22 02:58:02 +03:00
|
|
|
|
|
|
|
/*
|
2017-06-19 01:07:36 +03:00
|
|
|
* Implementation of mozilla::dom::Selection
|
2000-03-22 02:58:02 +03:00
|
|
|
*/
|
|
|
|
|
2014-04-10 20:09:40 +04:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2012-04-07 01:54:38 +04:00
|
|
|
|
2011-11-21 10:21:16 +04:00
|
|
|
#include "mozilla/Attributes.h"
|
2017-03-10 10:55:12 +03:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2014-04-03 08:18:36 +04:00
|
|
|
#include "mozilla/EventStates.h"
|
2017-08-07 10:33:59 +03:00
|
|
|
#include "mozilla/HTMLEditor.h"
|
2017-11-07 09:29:15 +03:00
|
|
|
#include "mozilla/RangeBoundary.h"
|
2011-11-21 10:21:16 +04:00
|
|
|
|
2000-05-04 12:35:42 +04:00
|
|
|
#include "nsCOMPtr.h"
|
2001-09-29 12:28:41 +04:00
|
|
|
#include "nsString.h"
|
2006-04-26 06:01:07 +04:00
|
|
|
#include "nsFrameSelection.h"
|
2000-09-14 15:45:01 +04:00
|
|
|
#include "nsISelectionListener.h"
|
2001-02-19 15:55:42 +03:00
|
|
|
#include "nsContentCID.h"
|
2016-04-26 15:43:55 +03:00
|
|
|
#include "nsDeviceContext.h"
|
2000-03-22 02:58:02 +03:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsIDOMNode.h"
|
|
|
|
#include "nsRange.h"
|
|
|
|
#include "nsITableCellLayout.h"
|
2006-07-28 01:46:34 +04:00
|
|
|
#include "nsTArray.h"
|
2016-06-16 15:14:08 +03:00
|
|
|
#include "nsTableWrapperFrame.h"
|
2012-08-08 17:05:17 +04:00
|
|
|
#include "nsTableCellFrame.h"
|
2009-09-01 15:29:27 +04:00
|
|
|
#include "nsIScrollableFrame.h"
|
2012-01-06 23:08:08 +04:00
|
|
|
#include "nsCCUncollectableMarker.h"
|
2000-03-22 02:58:02 +03:00
|
|
|
#include "nsIContentIterator.h"
|
2000-04-28 17:58:08 +04:00
|
|
|
#include "nsIDocumentEncoder.h"
|
2012-07-02 03:45:59 +04:00
|
|
|
#include "nsTextFragment.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2016-09-27 23:05:43 +03:00
|
|
|
#include "nsContentUtils.h"
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2007-01-30 03:06:41 +03:00
|
|
|
#include "nsGkAtoms.h"
|
2004-08-06 19:55:17 +04:00
|
|
|
#include "nsLayoutUtils.h"
|
2006-03-23 11:30:30 +03:00
|
|
|
#include "nsBidiPresUtils.h"
|
2009-07-27 02:05:41 +04:00
|
|
|
#include "nsTextFrame.h"
|
2001-03-21 04:16:22 +03:00
|
|
|
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 20:46:42 +04:00
|
|
|
#include "nsContentUtils.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadUtils.h"
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 20:46:42 +04:00
|
|
|
|
2004-08-01 03:15:21 +04:00
|
|
|
#include "nsPresContext.h"
|
2000-03-22 02:58:02 +03:00
|
|
|
#include "nsIPresShell.h"
|
2008-07-16 14:52:01 +04:00
|
|
|
#include "nsCaret.h"
|
2000-03-22 02:58:02 +03:00
|
|
|
|
|
|
|
#include "nsITimer.h"
|
2000-04-14 15:21:19 +04:00
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDocument.h"
|
2017-07-26 21:18:20 +03:00
|
|
|
#include "nsINamed.h"
|
2000-04-14 15:21:19 +04:00
|
|
|
|
2017-11-13 12:54:18 +03:00
|
|
|
#include "nsISelectionController.h" //for the enums
|
2005-02-01 02:47:26 +03:00
|
|
|
#include "nsAutoCopyListener.h"
|
2015-08-12 20:26:01 +03:00
|
|
|
#include "SelectionChangeListener.h"
|
2005-02-01 02:47:26 +03:00
|
|
|
#include "nsCopySupport.h"
|
|
|
|
#include "nsIClipboard.h"
|
2013-10-02 01:00:38 +04:00
|
|
|
#include "nsIFrameInlines.h"
|
2017-06-25 00:38:42 +03:00
|
|
|
#include "nsRefreshDriver.h"
|
2006-08-08 09:14:52 +04:00
|
|
|
#include "nsIBidiKeyboard.h"
|
|
|
|
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2011-07-20 23:18:54 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2013-12-02 14:26:11 +04:00
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2013-12-17 18:12:33 +04:00
|
|
|
#include "mozilla/ErrorResult.h"
|
|
|
|
#include "mozilla/dom/SelectionBinding.h"
|
2015-08-12 20:26:01 +03:00
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2016-02-04 03:51:03 +03:00
|
|
|
#include "mozilla/Telemetry.h"
|
2016-04-26 15:43:55 +03:00
|
|
|
#include "nsViewManager.h"
|
2010-11-06 12:40:04 +03:00
|
|
|
|
2016-08-04 02:08:33 +03:00
|
|
|
#include "nsFocusManager.h"
|
2017-03-14 04:36:21 +03:00
|
|
|
#include "nsPIDOMWindow.h"
|
2016-07-09 05:42:33 +03:00
|
|
|
|
2011-05-25 10:31:59 +04:00
|
|
|
using namespace mozilla;
|
2014-04-10 20:09:40 +04:00
|
|
|
using namespace mozilla::dom;
|
2011-05-25 10:31:59 +04:00
|
|
|
|
2002-03-22 18:26:18 +03:00
|
|
|
//#define DEBUG_TABLE 1
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static bool IsValidSelectionPoint(nsFrameSelection *aFrameSel, nsINode *aNode);
|
2002-03-26 02:44:46 +03:00
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
#ifdef PRINT_RANGE
|
2012-01-10 18:19:54 +04:00
|
|
|
static void printRange(nsRange *aDomRange);
|
2000-03-22 02:58:02 +03:00
|
|
|
#define DEBUG_OUT_RANGE(x) printRange(x)
|
|
|
|
#else
|
2017-07-06 15:00:35 +03:00
|
|
|
#define DEBUG_OUT_RANGE(x)
|
2013-09-05 00:43:12 +04:00
|
|
|
#endif // PRINT_RANGE
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2016-06-11 05:06:37 +03:00
|
|
|
/******************************************************************************
|
|
|
|
* Utility methods defined in nsISelectionController.idl
|
|
|
|
******************************************************************************/
|
2011-08-03 07:39:02 +04:00
|
|
|
|
2016-06-11 05:06:37 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
const char*
|
|
|
|
ToChar(SelectionType aSelectionType)
|
|
|
|
{
|
|
|
|
switch (aSelectionType) {
|
|
|
|
case SelectionType::eInvalid:
|
|
|
|
return "SelectionType::eInvalid";
|
2016-06-09 12:07:18 +03:00
|
|
|
case SelectionType::eNone:
|
2016-06-11 05:06:37 +03:00
|
|
|
return "SelectionType::eNone";
|
2016-06-09 12:35:22 +03:00
|
|
|
case SelectionType::eNormal:
|
2016-06-11 05:06:37 +03:00
|
|
|
return "SelectionType::eNormal";
|
2016-06-09 12:37:36 +03:00
|
|
|
case SelectionType::eSpellCheck:
|
2016-06-11 05:06:37 +03:00
|
|
|
return "SelectionType::eSpellCheck";
|
2016-06-09 12:51:49 +03:00
|
|
|
case SelectionType::eIMERawClause:
|
2016-06-11 05:06:37 +03:00
|
|
|
return "SelectionType::eIMERawClause";
|
2016-06-09 13:03:40 +03:00
|
|
|
case SelectionType::eIMESelectedRawClause:
|
2016-06-11 05:06:37 +03:00
|
|
|
return "SelectionType::eIMESelectedRawClause";
|
2016-06-09 13:14:17 +03:00
|
|
|
case SelectionType::eIMEConvertedClause:
|
2016-06-11 05:06:37 +03:00
|
|
|
return "SelectionType::eIMEConvertedClause";
|
2016-06-09 13:29:29 +03:00
|
|
|
case SelectionType::eIMESelectedClause:
|
2016-06-11 05:06:37 +03:00
|
|
|
return "SelectionType::eIMESelectedClause";
|
2016-06-09 14:32:36 +03:00
|
|
|
case SelectionType::eAccessibility:
|
2016-06-11 05:06:37 +03:00
|
|
|
return "SelectionType::eAccessibility";
|
2016-06-09 14:35:20 +03:00
|
|
|
case SelectionType::eFind:
|
2016-06-11 05:06:37 +03:00
|
|
|
return "SelectionType::eFind";
|
2016-06-09 14:47:48 +03:00
|
|
|
case SelectionType::eURLSecondary:
|
2016-06-11 05:06:37 +03:00
|
|
|
return "SelectionType::eURLSecondary";
|
2016-06-09 14:58:56 +03:00
|
|
|
case SelectionType::eURLStrikeout:
|
2016-06-11 05:06:37 +03:00
|
|
|
return "SelectionType::eURLStrikeout";
|
|
|
|
default:
|
|
|
|
return "Invalid SelectionType";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace mozilla
|
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
//#define DEBUG_SELECTION // uncomment for printf describing every collapse and extend.
|
|
|
|
//#define DEBUG_NAVIGATION
|
|
|
|
|
2000-03-31 11:17:22 +04:00
|
|
|
|
2000-06-03 03:05:06 +04:00
|
|
|
//#define DEBUG_TABLE_SELECTION 1
|
2000-03-31 11:17:22 +04:00
|
|
|
|
2003-04-20 05:03:40 +04:00
|
|
|
struct CachedOffsetForFrame {
|
|
|
|
CachedOffsetForFrame()
|
|
|
|
: mCachedFrameOffset(0, 0) // nsPoint ctor
|
2012-07-30 18:20:58 +04:00
|
|
|
, mLastCaretFrame(nullptr)
|
2003-04-20 05:03:40 +04:00
|
|
|
, mLastContentOffset(0)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mCanCacheFrameOffset(false)
|
2003-04-20 05:03:40 +04:00
|
|
|
{}
|
|
|
|
|
|
|
|
nsPoint mCachedFrameOffset; // cached frame offset
|
|
|
|
nsIFrame* mLastCaretFrame; // store the frame the caret was last drawn in.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t mLastContentOffset; // store last content offset
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mCanCacheFrameOffset; // cached frame offset is valid?
|
2003-04-20 05:03:40 +04:00
|
|
|
};
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
class nsAutoScrollTimer final : public nsITimerCallback
|
2017-07-26 21:18:20 +03:00
|
|
|
, public nsINamed
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
nsAutoScrollTimer()
|
2006-06-16 05:54:31 +04:00
|
|
|
: mFrameSelection(0), mSelection(0), mPresContext(0), mPoint(0,0), mDelay(30)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-09-10 09:16:18 +04:00
|
|
|
// aPoint is relative to aPresContext's root frame
|
2011-08-03 07:39:02 +04:00
|
|
|
nsresult Start(nsPresContext *aPresContext, nsPoint &aPoint)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2006-06-16 05:54:31 +04:00
|
|
|
mPoint = aPoint;
|
|
|
|
|
|
|
|
// Store the presentation context. The timer will be
|
|
|
|
// stopped by the selection if the prescontext is destroyed.
|
2000-03-22 02:58:02 +03:00
|
|
|
mPresContext = aPresContext;
|
2006-06-16 05:54:31 +04:00
|
|
|
|
2011-08-03 07:39:02 +04:00
|
|
|
mContent = nsIPresShell::GetCapturingContent();
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2017-03-18 19:40:30 +03:00
|
|
|
if (!mTimer) {
|
2017-10-16 09:15:40 +03:00
|
|
|
mTimer = NS_NewTimer(
|
2017-03-18 19:40:30 +03:00
|
|
|
mPresContext->Document()->EventTargetFor(TaskCategory::Other));
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2017-10-16 09:15:40 +03:00
|
|
|
if (!mTimer) {
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2017-03-18 19:40:30 +03:00
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2002-09-07 09:38:16 +04:00
|
|
|
return mTimer->InitWithCallback(this, mDelay, nsITimer::TYPE_ONE_SHOT);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult Stop()
|
|
|
|
{
|
2017-03-18 19:40:30 +03:00
|
|
|
if (mTimer) {
|
2000-03-22 02:58:02 +03:00
|
|
|
mTimer->Cancel();
|
2016-11-10 06:11:27 +03:00
|
|
|
mTimer = nullptr;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mContent = nullptr;
|
2006-06-16 05:54:31 +04:00
|
|
|
return NS_OK;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
nsresult Init(nsFrameSelection* aFrameSelection, Selection* aSelection)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
|
|
|
mFrameSelection = aFrameSelection;
|
|
|
|
mSelection = aSelection;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
nsresult SetDelay(uint32_t aDelay)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
|
|
|
mDelay = aDelay;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-21 19:28:04 +03:00
|
|
|
NS_IMETHOD Notify(nsITimer *timer) override
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2006-06-16 05:54:31 +04:00
|
|
|
if (mSelection && mPresContext)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2017-03-01 20:03:14 +03:00
|
|
|
AutoWeakFrame frame =
|
2012-07-30 18:20:58 +04:00
|
|
|
mContent ? mPresContext->GetPrimaryFrameFor(mContent) : nullptr;
|
2017-03-18 19:40:30 +03:00
|
|
|
if (!frame) {
|
2010-10-25 18:06:30 +04:00
|
|
|
return NS_OK;
|
2017-03-18 19:40:30 +03:00
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
mContent = nullptr;
|
2000-08-30 23:42:47 +04:00
|
|
|
|
2010-10-25 18:06:30 +04:00
|
|
|
nsPoint pt = mPoint -
|
2018-03-06 12:25:21 +03:00
|
|
|
frame->GetOffsetTo(mPresContext->PresShell()->GetRootFrame());
|
2017-04-06 07:11:52 +03:00
|
|
|
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
|
|
|
|
frameSelection->HandleDrag(frame, pt);
|
2017-03-18 19:40:30 +03:00
|
|
|
if (!frame.IsAlive()) {
|
2009-09-18 00:33:29 +04:00
|
|
|
return NS_OK;
|
2017-03-18 19:40:30 +03:00
|
|
|
}
|
2009-09-18 00:33:29 +04:00
|
|
|
|
2010-07-19 06:23:48 +04:00
|
|
|
NS_ASSERTION(frame->PresContext() == mPresContext, "document mismatch?");
|
2009-09-10 09:16:18 +04:00
|
|
|
mSelection->DoAutoScroll(frame, pt);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2002-09-07 09:38:16 +04:00
|
|
|
return NS_OK;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2014-06-24 02:40:01 +04:00
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
NS_IMETHOD GetName(nsACString& aName) override
|
|
|
|
{
|
|
|
|
aName.AssignLiteral("nsAutoScrollTimer");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-24 02:40:01 +04:00
|
|
|
protected:
|
|
|
|
virtual ~nsAutoScrollTimer()
|
|
|
|
{
|
2017-03-18 19:40:30 +03:00
|
|
|
if (mTimer) {
|
|
|
|
mTimer->Cancel();
|
|
|
|
}
|
2014-06-24 02:40:01 +04:00
|
|
|
}
|
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
private:
|
2006-06-16 05:54:31 +04:00
|
|
|
nsFrameSelection *mFrameSelection;
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection* mSelection;
|
2004-08-01 03:15:21 +04:00
|
|
|
nsPresContext *mPresContext;
|
2009-09-10 09:16:18 +04:00
|
|
|
// relative to mPresContext's root frame
|
2006-06-16 05:54:31 +04:00
|
|
|
nsPoint mPoint;
|
|
|
|
nsCOMPtr<nsITimer> mTimer;
|
|
|
|
nsCOMPtr<nsIContent> mContent;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t mDelay;
|
2000-03-22 02:58:02 +03:00
|
|
|
};
|
|
|
|
|
2017-07-26 21:18:20 +03:00
|
|
|
NS_IMPL_ISUPPORTS(nsAutoScrollTimer, nsITimerCallback, nsINamed)
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2002-03-26 02:44:46 +03:00
|
|
|
/*
|
|
|
|
The limiter is used specifically for the text areas and textfields
|
|
|
|
In that case it is the DIV tag that is anonymously created for the text
|
2017-07-06 15:00:35 +03:00
|
|
|
areas/fields. Text nodes and BR nodes fall beneath it. In the case of a
|
2002-03-26 02:44:46 +03:00
|
|
|
BR node the limiter will be the parent and the offset will point before or
|
2017-07-06 15:00:35 +03:00
|
|
|
after the BR node. In the case of the text node the parent content is
|
2002-03-26 02:44:46 +03:00
|
|
|
the text node itself and the offset will be the exact character position.
|
2017-07-06 15:00:35 +03:00
|
|
|
The offset is not important to check for validity. Simply look at the
|
2002-03-26 02:44:46 +03:00
|
|
|
passed in content. If it equals the limiter then the selection point is valid.
|
2017-07-06 15:00:35 +03:00
|
|
|
If its parent it the limiter then the point is also valid. In the case of
|
2002-03-26 02:44:46 +03:00
|
|
|
NO limiter all points are valid since you are in a topmost iframe. (browser
|
|
|
|
or composer)
|
|
|
|
*/
|
2017-07-06 15:00:35 +03:00
|
|
|
bool
|
2009-04-06 21:17:37 +04:00
|
|
|
IsValidSelectionPoint(nsFrameSelection *aFrameSel, nsINode *aNode)
|
2002-03-26 02:44:46 +03:00
|
|
|
{
|
2009-04-06 21:17:37 +04:00
|
|
|
if (!aFrameSel || !aNode)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-04-06 21:17:37 +04:00
|
|
|
|
|
|
|
nsIContent *limiter = aFrameSel->GetLimiter();
|
|
|
|
if (limiter && limiter != aNode && limiter != aNode->GetParent()) {
|
|
|
|
//if newfocus == the limiter. that's ok. but if not there and not parent bad
|
2011-10-17 18:59:28 +04:00
|
|
|
return false; //not in the right content. tLimiter said so
|
2002-03-26 02:44:46 +03:00
|
|
|
}
|
2009-04-06 21:17:37 +04:00
|
|
|
|
|
|
|
limiter = aFrameSel->GetAncestorLimiter();
|
|
|
|
return !limiter || nsContentUtils::ContentIsDescendantOf(aNode, limiter);
|
2002-03-26 02:44:46 +03:00
|
|
|
}
|
2000-03-31 11:17:22 +04:00
|
|
|
|
2015-02-21 07:27:59 +03:00
|
|
|
namespace mozilla {
|
2015-09-03 19:15:23 +03:00
|
|
|
struct MOZ_RAII AutoPrepareFocusRange
|
2015-02-21 07:27:59 +03:00
|
|
|
{
|
|
|
|
AutoPrepareFocusRange(Selection* aSelection,
|
|
|
|
bool aContinueSelection,
|
|
|
|
bool aMultipleSelection
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_PARAM)
|
|
|
|
{
|
|
|
|
MOZ_GUARD_OBJECT_NOTIFIER_INIT;
|
|
|
|
|
|
|
|
if (aSelection->mRanges.Length() <= 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aSelection->mFrameSelection->IsUserSelectionReason()) {
|
|
|
|
mUserSelect.emplace(aSelection);
|
|
|
|
}
|
2015-09-22 20:24:50 +03:00
|
|
|
bool userSelection = aSelection->mUserInitiated;
|
2015-02-21 07:27:59 +03:00
|
|
|
|
|
|
|
nsTArray<RangeData>& ranges = aSelection->mRanges;
|
|
|
|
if (!userSelection ||
|
|
|
|
(!aContinueSelection && aMultipleSelection)) {
|
|
|
|
// Scripted command or the user is starting a new explicit multi-range
|
|
|
|
// selection.
|
|
|
|
for (RangeData& entry : ranges) {
|
|
|
|
entry.mRange->SetIsGenerated(false);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
int16_t reason = aSelection->mFrameSelection->mSelectionChangeReason;
|
|
|
|
bool isAnchorRelativeOp = (reason & (nsISelectionListener::DRAG_REASON |
|
|
|
|
nsISelectionListener::MOUSEDOWN_REASON |
|
|
|
|
nsISelectionListener::MOUSEUP_REASON |
|
|
|
|
nsISelectionListener::COLLAPSETOSTART_REASON));
|
|
|
|
if (!isAnchorRelativeOp) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// This operation is against the anchor but our current mAnchorFocusRange
|
|
|
|
// represents the focus in a multi-range selection. The anchor from a user
|
|
|
|
// perspective is the most distant generated range on the opposite side.
|
|
|
|
// Find that range and make it the mAnchorFocusRange.
|
|
|
|
const size_t len = ranges.Length();
|
|
|
|
size_t newAnchorFocusIndex = size_t(-1);
|
|
|
|
if (aSelection->GetDirection() == eDirNext) {
|
|
|
|
for (size_t i = 0; i < len; ++i) {
|
|
|
|
if (ranges[i].mRange->IsGenerated()) {
|
|
|
|
newAnchorFocusIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
size_t i = len;
|
|
|
|
while (i--) {
|
|
|
|
if (ranges[i].mRange->IsGenerated()) {
|
|
|
|
newAnchorFocusIndex = i;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (newAnchorFocusIndex == size_t(-1)) {
|
|
|
|
// There are no generated ranges - that's fine.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Setup the new mAnchorFocusRange and mark the old one as generated.
|
|
|
|
if (aSelection->mAnchorFocusRange) {
|
|
|
|
aSelection->mAnchorFocusRange->SetIsGenerated(true);
|
|
|
|
}
|
|
|
|
nsRange* range = ranges[newAnchorFocusIndex].mRange;
|
|
|
|
range->SetIsGenerated(false);
|
|
|
|
aSelection->mAnchorFocusRange = range;
|
|
|
|
|
|
|
|
// Remove all generated ranges (including the old mAnchorFocusRange).
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPresContext> presContext = aSelection->GetPresContext();
|
2015-02-21 07:27:59 +03:00
|
|
|
size_t i = len;
|
|
|
|
while (i--) {
|
|
|
|
range = aSelection->mRanges[i].mRange;
|
|
|
|
if (range->IsGenerated()) {
|
2015-08-12 20:26:01 +03:00
|
|
|
range->SetSelection(nullptr);
|
2017-05-29 06:27:09 +03:00
|
|
|
aSelection->SelectFrames(presContext, range, false);
|
2015-02-21 07:27:59 +03:00
|
|
|
aSelection->mRanges.RemoveElementAt(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aSelection->mFrameSelection) {
|
|
|
|
aSelection->mFrameSelection->InvalidateDesiredPos();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-22 20:24:50 +03:00
|
|
|
Maybe<Selection::AutoUserInitiated> mUserSelect;
|
2015-02-21 07:27:59 +03:00
|
|
|
MOZ_DECL_USE_GUARD_OBJECT_NOTIFIER
|
|
|
|
};
|
2015-07-13 18:25:42 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|
2004-01-06 03:36:01 +03:00
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
|
|
|
|
#ifdef PRINT_RANGE
|
2012-01-10 18:19:54 +04:00
|
|
|
void printRange(nsRange *aDomRange)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
|
|
|
if (!aDomRange)
|
|
|
|
{
|
2000-10-29 02:17:53 +04:00
|
|
|
printf("NULL nsIDOMRange\n");
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2017-07-11 14:53:04 +03:00
|
|
|
nsINode* startNode = aDomRange->GetStartContainer();
|
2017-07-11 15:11:37 +03:00
|
|
|
nsINode* endNode = aDomRange->GetEndContainer();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startOffset = aDomRange->StartOffset();
|
|
|
|
int32_t endOffset = aDomRange->EndOffset();
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2000-10-29 02:17:53 +04:00
|
|
|
printf("range: 0x%lx\t start: 0x%lx %ld, \t end: 0x%lx,%ld\n",
|
2000-03-22 02:58:02 +03:00
|
|
|
(unsigned long)aDomRange,
|
2009-04-06 21:21:30 +04:00
|
|
|
(unsigned long)startNode, (long)startOffset,
|
|
|
|
(unsigned long)endNode, (long)endOffset);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
#endif /* PRINT_RANGE */
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::ToString(nsAString& aReturn)
|
2000-09-14 15:45:01 +04:00
|
|
|
{
|
2017-01-06 10:21:19 +03:00
|
|
|
// We need FlushType::Frames here to make sure frames have been created for
|
2012-05-30 17:03:19 +04:00
|
|
|
// the selected content. Use mFrameSelection->GetShell() which returns
|
|
|
|
// null if the Selection has been disconnected (the shell is Destroyed).
|
|
|
|
nsCOMPtr<nsIPresShell> shell =
|
2012-07-30 18:20:58 +04:00
|
|
|
mFrameSelection ? mFrameSelection->GetShell() : nullptr;
|
2012-05-30 17:03:19 +04:00
|
|
|
if (!shell) {
|
2012-06-10 02:15:12 +04:00
|
|
|
aReturn.Truncate();
|
2017-06-19 01:07:36 +03:00
|
|
|
return NS_OK;
|
2016-01-22 18:58:49 +03:00
|
|
|
}
|
2017-06-19 01:07:36 +03:00
|
|
|
shell->FlushPendingNotifications(FlushType::Frames);
|
|
|
|
|
|
|
|
return ToStringWithFormat("text/plain",
|
|
|
|
nsIDocumentEncoder::SkipInvisibleContent,
|
|
|
|
0, aReturn);
|
2000-03-24 05:52:07 +03:00
|
|
|
}
|
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
void
|
|
|
|
Selection::Stringify(nsAString& aResult)
|
2009-09-10 07:06:54 +04:00
|
|
|
{
|
2017-06-19 01:07:36 +03:00
|
|
|
// Eat the error code
|
|
|
|
ToString(aResult);
|
2009-09-10 07:06:54 +04:00
|
|
|
}
|
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Selection::ToStringWithFormat(const char* aFormatType, uint32_t aFlags,
|
|
|
|
int32_t aWrapCol, nsAString& aReturn)
|
2009-09-10 07:06:54 +04:00
|
|
|
{
|
2017-06-19 01:07:36 +03:00
|
|
|
ErrorResult result;
|
|
|
|
NS_ConvertUTF8toUTF16 format(aFormatType);
|
|
|
|
ToStringWithFormat(format, aFlags, aWrapCol, aReturn, result);
|
|
|
|
if (result.Failed()) {
|
|
|
|
return result.StealNSResult();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2009-09-10 07:06:54 +04:00
|
|
|
}
|
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
void
|
|
|
|
Selection::ToStringWithFormat(const nsAString& aFormatType, uint32_t aFlags,
|
|
|
|
int32_t aWrapCol, nsAString& aReturn,
|
|
|
|
ErrorResult& aRv)
|
2000-05-16 07:03:13 +04:00
|
|
|
{
|
2017-06-19 01:07:36 +03:00
|
|
|
nsresult rv = NS_OK;
|
|
|
|
NS_ConvertUTF8toUTF16 formatType( NS_DOC_ENCODER_CONTRACTID_BASE );
|
|
|
|
formatType.Append(aFormatType);
|
|
|
|
nsCOMPtr<nsIDocumentEncoder> encoder =
|
|
|
|
do_CreateInstance(NS_ConvertUTF16toUTF8(formatType).get(), &rv);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2000-05-16 07:03:13 +04:00
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
nsIPresShell* shell = GetPresShell();
|
|
|
|
if (!shell) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2000-05-16 07:03:13 +04:00
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
nsIDocument *doc = shell->GetDocument();
|
2000-05-16 07:03:13 +04:00
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
nsCOMPtr<nsIDOMDocument> domDoc = do_QueryInterface(doc);
|
|
|
|
NS_ASSERTION(domDoc, "Need a document");
|
2012-08-08 17:05:17 +04:00
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
// Flags should always include OutputSelectionOnly if we're coming from here:
|
|
|
|
aFlags |= nsIDocumentEncoder::OutputSelectionOnly;
|
|
|
|
nsAutoString readstring;
|
|
|
|
readstring.Assign(aFormatType);
|
|
|
|
rv = encoder->Init(domDoc, readstring, aFlags);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
2000-05-16 07:03:13 +04:00
|
|
|
}
|
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
encoder->SetSelection(this);
|
|
|
|
if (aWrapCol != 0)
|
|
|
|
encoder->SetWrapColumn(aWrapCol);
|
2000-05-16 07:03:13 +04:00
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
rv = encoder->EncodeToString(aReturn);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
2000-05-16 07:03:13 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Selection::SetInterlinePosition(bool aHintRight)
|
2000-03-24 05:52:07 +03:00
|
|
|
{
|
2017-06-19 01:07:36 +03:00
|
|
|
ErrorResult result;
|
|
|
|
SetInterlinePosition(aHintRight, result);
|
|
|
|
if (result.Failed()) {
|
|
|
|
return result.StealNSResult();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2000-03-24 05:52:07 +03:00
|
|
|
}
|
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
void
|
|
|
|
Selection::SetInterlinePosition(bool aHintRight, ErrorResult& aRv)
|
2000-03-24 05:52:07 +03:00
|
|
|
{
|
2017-06-19 01:07:36 +03:00
|
|
|
if (!mFrameSelection) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED); // Can't do selection
|
|
|
|
return;
|
2000-05-31 04:07:58 +04:00
|
|
|
}
|
2017-06-19 01:07:36 +03:00
|
|
|
mFrameSelection->SetHint(aHintRight ? CARET_ASSOCIATE_AFTER : CARET_ASSOCIATE_BEFORE);
|
2000-03-24 05:52:07 +03:00
|
|
|
}
|
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Selection::GetInterlinePosition(bool* aHintRight)
|
2000-03-24 05:52:07 +03:00
|
|
|
{
|
2017-06-19 01:07:36 +03:00
|
|
|
ErrorResult result;
|
|
|
|
*aHintRight = GetInterlinePosition(result);
|
|
|
|
if (result.Failed()) {
|
|
|
|
return result.StealNSResult();
|
2000-05-31 04:07:58 +04:00
|
|
|
}
|
2017-06-19 01:07:36 +03:00
|
|
|
return NS_OK;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
bool
|
|
|
|
Selection::GetInterlinePosition(ErrorResult& aRv)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2017-06-19 01:07:36 +03:00
|
|
|
if (!mFrameSelection) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED); // Can't do selection
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return mFrameSelection->GetHint() == CARET_ASSOCIATE_AFTER;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
Nullable<int16_t>
|
|
|
|
Selection::GetCaretBidiLevel(mozilla::ErrorResult& aRv) const
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2017-06-19 01:07:36 +03:00
|
|
|
if (!mFrameSelection) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return Nullable<int16_t>();
|
|
|
|
}
|
|
|
|
nsBidiLevel caretBidiLevel = mFrameSelection->GetCaretBidiLevel();
|
|
|
|
return (caretBidiLevel & BIDI_LEVEL_UNDEFINED) ?
|
|
|
|
Nullable<int16_t>() : Nullable<int16_t>(caretBidiLevel);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2017-06-19 01:07:36 +03:00
|
|
|
void
|
|
|
|
Selection::SetCaretBidiLevel(const Nullable<int16_t>& aCaretBidiLevel, mozilla::ErrorResult& aRv)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2017-06-19 01:07:36 +03:00
|
|
|
if (!mFrameSelection) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return;
|
2003-06-14 00:10:01 +04:00
|
|
|
}
|
2017-06-19 01:07:36 +03:00
|
|
|
if (aCaretBidiLevel.IsNull()) {
|
|
|
|
mFrameSelection->UndefineCaretBidiLevel();
|
|
|
|
} else {
|
|
|
|
mFrameSelection->SetCaretBidiLevel(aCaretBidiLevel.Value());
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2000-05-16 07:03:13 +04:00
|
|
|
}
|
|
|
|
|
2000-12-21 01:13:54 +03:00
|
|
|
nsresult
|
2017-05-29 07:11:57 +03:00
|
|
|
Selection::GetTableCellLocationFromRange(nsRange* aRange,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t* aSelectionType,
|
|
|
|
int32_t* aRow, int32_t* aCol)
|
2000-12-21 01:13:54 +03:00
|
|
|
{
|
|
|
|
if (!aRange || !aSelectionType || !aRow || !aCol)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*aSelectionType = nsISelectionPrivate::TABLESELECTION_NONE;
|
|
|
|
*aRow = 0;
|
|
|
|
*aCol = 0;
|
|
|
|
|
|
|
|
// Must have access to frame selection to get cell info
|
|
|
|
if (!mFrameSelection) return NS_OK;
|
|
|
|
|
2009-04-06 21:20:30 +04:00
|
|
|
nsresult result = GetTableSelectionType(aRange, aSelectionType);
|
2000-12-21 01:13:54 +03:00
|
|
|
if (NS_FAILED(result)) return result;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2000-12-21 01:13:54 +03:00
|
|
|
// Don't fail if range does not point to a single table cell,
|
|
|
|
// let aSelectionType tell user if we don't have a cell
|
|
|
|
if (*aSelectionType != nsISelectionPrivate::TABLESELECTION_CELL)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
// Get the child content (the cell) pointed to by starting node of range
|
|
|
|
// We do minimal checking since GetTableSelectionType assures
|
|
|
|
// us that this really is a table cell
|
2018-01-12 17:33:13 +03:00
|
|
|
nsCOMPtr<nsIContent> child = aRange->GetChildAtStartOffset();
|
2003-09-27 08:18:26 +04:00
|
|
|
if (!child)
|
|
|
|
return NS_ERROR_FAILURE;
|
2000-12-21 01:13:54 +03:00
|
|
|
|
2017-07-18 10:54:31 +03:00
|
|
|
// GetCellLayout depends on current frame, we need flush frame to get
|
|
|
|
// nsITableCellLayout
|
|
|
|
nsCOMPtr<nsIPresShell> presShell = mFrameSelection->GetShell();
|
|
|
|
if (presShell) {
|
|
|
|
presShell->FlushPendingNotifications(FlushType::Frames);
|
|
|
|
|
|
|
|
// Since calling FlushPendingNotifications, so check whether disconnected.
|
|
|
|
if (!mFrameSelection || !mFrameSelection->GetShell()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-12-21 01:13:54 +03:00
|
|
|
//Note: This is a non-ref-counted pointer to the frame
|
|
|
|
nsITableCellLayout *cellLayout = mFrameSelection->GetCellLayout(child);
|
2003-09-27 08:18:26 +04:00
|
|
|
if (!cellLayout)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2000-12-21 01:13:54 +03:00
|
|
|
return cellLayout->GetCellIndexes(*aRow, *aCol);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-05-29 07:31:11 +03:00
|
|
|
Selection::AddTableCellRange(nsRange* aRange, bool* aDidAddRange,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t* aOutIndex)
|
2017-07-06 15:00:35 +03:00
|
|
|
{
|
2008-03-31 21:47:42 +04:00
|
|
|
if (!aDidAddRange || !aOutIndex)
|
2000-12-21 01:13:54 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*aDidAddRange = false;
|
2008-03-31 21:47:42 +04:00
|
|
|
*aOutIndex = -1;
|
2000-12-21 01:13:54 +03:00
|
|
|
|
|
|
|
if (!mFrameSelection)
|
|
|
|
return NS_OK;
|
|
|
|
|
|
|
|
if (!aRange)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
nsresult result;
|
|
|
|
|
2001-01-04 03:24:39 +03:00
|
|
|
// Get if we are adding a cell selection and the row, col of cell if we are
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t newRow, newCol, tableMode;
|
2017-05-29 07:11:57 +03:00
|
|
|
result = GetTableCellLocationFromRange(aRange, &tableMode, &newRow, &newCol);
|
2000-12-21 01:13:54 +03:00
|
|
|
if (NS_FAILED(result)) return result;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2001-01-04 03:24:39 +03:00
|
|
|
// If not adding a cell range, we are done here
|
|
|
|
if (tableMode != nsISelectionPrivate::TABLESELECTION_CELL)
|
|
|
|
{
|
|
|
|
mFrameSelection->mSelectingTableCellMode = tableMode;
|
|
|
|
// Don't fail if range isn't a selected cell, aDidAddRange tells caller if we didn't proceed
|
2000-12-21 01:13:54 +03:00
|
|
|
return NS_OK;
|
2001-01-04 03:24:39 +03:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2001-01-04 03:24:39 +03:00
|
|
|
// Set frame selection mode only if not already set to a table mode
|
2009-07-27 12:47:02 +04:00
|
|
|
// so we don't lose the select row and column flags (not detected by getTableCellLocation)
|
2001-01-04 03:24:39 +03:00
|
|
|
if (mFrameSelection->mSelectingTableCellMode == TABLESELECTION_NONE)
|
|
|
|
mFrameSelection->mSelectingTableCellMode = tableMode;
|
2000-12-21 01:13:54 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
*aDidAddRange = true;
|
2008-03-31 21:47:42 +04:00
|
|
|
return AddItem(aRange, aOutIndex);
|
2000-12-21 01:13:54 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: Figure out TABLESELECTION_COLUMN and TABLESELECTION_ALLCELLS
|
2009-04-06 21:20:30 +04:00
|
|
|
nsresult
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::GetTableSelectionType(nsIDOMRange* aDOMRange,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t* aTableSelectionType)
|
2000-12-21 01:13:54 +03:00
|
|
|
{
|
2012-01-10 18:19:54 +04:00
|
|
|
if (!aDOMRange || !aTableSelectionType)
|
2000-12-21 01:13:54 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange* range = static_cast<nsRange*>(aDOMRange);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2000-12-21 01:13:54 +03:00
|
|
|
*aTableSelectionType = nsISelectionPrivate::TABLESELECTION_NONE;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2000-12-21 01:13:54 +03:00
|
|
|
// Must have access to frame selection to get cell info
|
|
|
|
if(!mFrameSelection) return NS_OK;
|
|
|
|
|
2017-07-11 14:53:04 +03:00
|
|
|
nsINode* startNode = range->GetStartContainer();
|
2000-12-21 01:13:54 +03:00
|
|
|
if (!startNode) return NS_ERROR_FAILURE;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2017-07-11 15:11:37 +03:00
|
|
|
nsINode* endNode = range->GetEndContainer();
|
2000-12-21 01:13:54 +03:00
|
|
|
if (!endNode) return NS_ERROR_FAILURE;
|
|
|
|
|
|
|
|
// Not a single selected node
|
|
|
|
if (startNode != endNode) return NS_OK;
|
|
|
|
|
2018-01-12 17:33:13 +03:00
|
|
|
nsIContent* child = range->GetChildAtStartOffset();
|
2000-12-21 01:13:54 +03:00
|
|
|
|
|
|
|
// Not a single selected node
|
2018-01-17 14:52:51 +03:00
|
|
|
if (!child || child->GetNextSibling() != range->GetChildAtEndOffset()) {
|
2000-12-21 01:13:54 +03:00
|
|
|
return NS_OK;
|
2018-01-12 17:33:13 +03:00
|
|
|
}
|
2000-12-21 01:13:54 +03:00
|
|
|
|
2009-08-25 00:02:07 +04:00
|
|
|
nsIContent* startContent = static_cast<nsIContent*>(startNode);
|
2015-03-03 14:08:59 +03:00
|
|
|
if (!(startNode->IsElement() && startContent->IsHTMLElement())) {
|
2009-04-06 21:20:30 +04:00
|
|
|
// Implies a check for being an element; if we ever make this work
|
|
|
|
// for non-HTML, need to keep checking for elements.
|
2003-11-19 04:20:56 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-03-03 14:09:00 +03:00
|
|
|
if (startContent->IsHTMLElement(nsGkAtoms::tr))
|
2000-12-21 01:13:54 +03:00
|
|
|
{
|
|
|
|
*aTableSelectionType = nsISelectionPrivate::TABLESELECTION_CELL;
|
|
|
|
}
|
|
|
|
else //check to see if we are selecting a table or row (column and all cells not done yet)
|
|
|
|
{
|
2015-03-03 14:09:00 +03:00
|
|
|
if (child->IsHTMLElement(nsGkAtoms::table))
|
2000-12-21 01:13:54 +03:00
|
|
|
*aTableSelectionType = nsISelectionPrivate::TABLESELECTION_TABLE;
|
2015-03-03 14:09:00 +03:00
|
|
|
else if (child->IsHTMLElement(nsGkAtoms::tr))
|
2000-12-21 01:13:54 +03:00
|
|
|
*aTableSelectionType = nsISelectionPrivate::TABLESELECTION_ROW;
|
|
|
|
}
|
2003-11-19 04:20:56 +03:00
|
|
|
|
2009-04-06 21:20:30 +04:00
|
|
|
return NS_OK;
|
2000-12-21 01:13:54 +03:00
|
|
|
}
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::Selection()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mCachedOffsetForFrame(nullptr)
|
2008-02-26 18:04:14 +03:00
|
|
|
, mDirection(eDirNext)
|
2016-06-09 12:35:22 +03:00
|
|
|
, mSelectionType(SelectionType::eNormal)
|
2017-05-10 17:48:50 +03:00
|
|
|
, mCustomColors(nullptr)
|
2015-09-22 20:24:50 +03:00
|
|
|
, mUserInitiated(false)
|
2017-03-10 10:55:12 +03:00
|
|
|
, mCalledByJS(false)
|
2015-08-12 20:26:01 +03:00
|
|
|
, mSelectionChangeBlockerCount(0)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::Selection(nsFrameSelection* aList)
|
2008-02-26 18:04:14 +03:00
|
|
|
: mFrameSelection(aList)
|
2012-07-30 18:20:58 +04:00
|
|
|
, mCachedOffsetForFrame(nullptr)
|
2008-02-26 18:04:14 +03:00
|
|
|
, mDirection(eDirNext)
|
2016-06-09 12:35:22 +03:00
|
|
|
, mSelectionType(SelectionType::eNormal)
|
2017-05-10 17:48:50 +03:00
|
|
|
, mCustomColors(nullptr)
|
2015-09-22 20:24:50 +03:00
|
|
|
, mUserInitiated(false)
|
2017-03-10 10:55:12 +03:00
|
|
|
, mCalledByJS(false)
|
2015-08-12 20:26:01 +03:00
|
|
|
, mSelectionChangeBlockerCount(0)
|
2000-05-11 08:25:43 +04:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::~Selection()
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2017-05-30 07:26:09 +03:00
|
|
|
SetAnchorFocusRange(-1);
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t count = mRanges.Length();
|
|
|
|
for (uint32_t i = 0; i < count; ++i) {
|
2015-08-12 20:26:01 +03:00
|
|
|
mRanges[i].mRange->SetSelection(nullptr);
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
|
|
|
|
2000-07-28 07:48:26 +04:00
|
|
|
if (mAutoScrollTimer) {
|
|
|
|
mAutoScrollTimer->Stop();
|
2012-07-30 18:20:58 +04:00
|
|
|
mAutoScrollTimer = nullptr;
|
2000-07-28 07:48:26 +04:00
|
|
|
}
|
2002-06-14 00:43:32 +04:00
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
mScrollEvent.Revoke();
|
2003-04-20 05:03:40 +04:00
|
|
|
|
2006-04-14 04:28:22 +04:00
|
|
|
if (mCachedOffsetForFrame) {
|
|
|
|
delete mCachedOffsetForFrame;
|
2012-07-30 18:20:58 +04:00
|
|
|
mCachedOffsetForFrame = nullptr;
|
2006-04-14 04:28:22 +04:00
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
nsIDocument*
|
|
|
|
Selection::GetParentObject() const
|
|
|
|
{
|
|
|
|
nsIPresShell* shell = GetPresShell();
|
|
|
|
if (shell) {
|
|
|
|
return shell->GetDocument();
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2017-11-27 11:10:27 +03:00
|
|
|
DocGroup*
|
|
|
|
Selection::GetDocGroup() const
|
|
|
|
{
|
|
|
|
nsIPresShell* shell = GetPresShell();
|
|
|
|
if (!shell) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIDocument* doc = shell->GetDocument();
|
|
|
|
return doc ? doc->GetDocGroup() : nullptr;
|
|
|
|
}
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(Selection)
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(Selection)
|
2009-05-09 08:59:26 +04:00
|
|
|
// Unlink the selection listeners *before* we do RemoveAllRanges since
|
|
|
|
// we don't want to notify the listeners during JS GC (they could be
|
|
|
|
// in JS!).
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSelectionListeners)
|
Bug 1393816 - part1: Cache a range until new range is created in Selection r=smaug
When setting value of <input type="text">, nsTextEditorState removes all
ranges of normal selection first. Then, TextEditor sets the value. Finally,
TextEditor collapses the selection at the end of the text.
In bug 1386471, we got that there are some problems to remove the call of
Selection::RemoveAllRanges() in nsTextEditorState. Therefore, we need another
approach to improve Selection::Collapse().
The approach of this patch is, when removing all ranges from normal selection,
Selection can cache an nsRange instance if there is an instance which is not
referenced from other than the Selection (i.e., it'll be removed when
Selection::Clear() is called). Then, Selection::Collapse() can reuse it. With
this fix, Selection::Collapse() can reduce allocation cost and may reduce some
other cost like adding it to mutation observer.
However, keeping nsRange instance may cause increasing mutation observer's cost
since nsRange will be adjusted its start node/offset and end node/offset with
mutation observer to guarantee that the range is always valid. So, we can
cache such range only when the caller (or its callee) will set selection range
later. Therefore, this patch adds Selection::RemoveAllRangesTemporarily()
and make only nsTextEditorState::SetValue() and
ContentEventHandler::OnSelectionEvent() use it.
MozReview-Commit-ID: FjWrbz4S1ld
--HG--
extra : rebase_source : 83677640525e0b1a84bdd7fce63ff4704b9cc22b
2017-08-25 13:21:39 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mCachedRange)
|
2018-03-27 07:35:23 +03:00
|
|
|
tmp->RemoveAllRanges(IgnoreErrors());
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mFrameSelection)
|
2013-12-17 18:12:33 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
2009-01-14 15:24:10 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
2012-06-08 18:20:55 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(Selection)
|
2009-01-14 15:24:10 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i, count = tmp->mRanges.Length();
|
2009-01-14 15:24:10 +03:00
|
|
|
for (i = 0; i < count; ++i) {
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRanges[i].mRange)
|
2009-01-14 15:24:10 +03:00
|
|
|
}
|
|
|
|
}
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mAnchorFocusRange)
|
Bug 1393816 - part1: Cache a range until new range is created in Selection r=smaug
When setting value of <input type="text">, nsTextEditorState removes all
ranges of normal selection first. Then, TextEditor sets the value. Finally,
TextEditor collapses the selection at the end of the text.
In bug 1386471, we got that there are some problems to remove the call of
Selection::RemoveAllRanges() in nsTextEditorState. Therefore, we need another
approach to improve Selection::Collapse().
The approach of this patch is, when removing all ranges from normal selection,
Selection can cache an nsRange instance if there is an instance which is not
referenced from other than the Selection (i.e., it'll be removed when
Selection::Clear() is called). Then, Selection::Collapse() can reuse it. With
this fix, Selection::Collapse() can reduce allocation cost and may reduce some
other cost like adding it to mutation observer.
However, keeping nsRange instance may cause increasing mutation observer's cost
since nsRange will be adjusted its start node/offset and end node/offset with
mutation observer to guarantee that the range is always valid. So, we can
cache such range only when the caller (or its callee) will set selection range
later. Therefore, this patch adds Selection::RemoveAllRangesTemporarily()
and make only nsTextEditorState::SetValue() and
ContentEventHandler::OnSelectionEvent() use it.
MozReview-Commit-ID: FjWrbz4S1ld
--HG--
extra : rebase_source : 83677640525e0b1a84bdd7fce63ff4704b9cc22b
2017-08-25 13:21:39 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mCachedRange)
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mFrameSelection)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSelectionListeners)
|
2009-01-14 15:24:10 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
2013-12-17 18:12:33 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(Selection)
|
2009-01-14 15:24:10 +03:00
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
// QueryInterface implementation for Selection
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Selection)
|
2013-12-17 18:12:33 +04:00
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
Landing the XPCDOM_20010329_BRANCH branch, changes mostly done by jband@netscape.com and jst@netscape.com, also some changes done by shaver@mozilla.org, peterv@netscape.com and markh@activestate.com. r= and sr= by vidur@netscape.com, jband@netscape.com, jst@netscpae.com, danm@netscape.com, hyatt@netscape.com, shaver@mozilla.org, dbradley@netscape.com, rpotts@netscape.com.
2001-05-08 20:46:42 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISelection)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISelectionPrivate)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupportsWeakReference)
|
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsISelection)
|
|
|
|
NS_INTERFACE_MAP_END
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2018-01-10 10:47:07 +03:00
|
|
|
NS_IMPL_MAIN_THREAD_ONLY_CYCLE_COLLECTING_ADDREF(Selection)
|
|
|
|
NS_IMPL_MAIN_THREAD_ONLY_CYCLE_COLLECTING_RELEASE(Selection)
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2017-12-05 10:36:57 +03:00
|
|
|
const RangeBoundary&
|
|
|
|
Selection::AnchorRef()
|
|
|
|
{
|
|
|
|
if (!mAnchorFocusRange) {
|
|
|
|
static RangeBoundary sEmpty;
|
|
|
|
return sEmpty;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetDirection() == eDirNext) {
|
|
|
|
return mAnchorFocusRange->StartRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
return mAnchorFocusRange->EndRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
const RangeBoundary&
|
|
|
|
Selection::FocusRef()
|
|
|
|
{
|
|
|
|
if (!mAnchorFocusRange) {
|
|
|
|
static RangeBoundary sEmpty;
|
|
|
|
return sEmpty;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (GetDirection() == eDirNext){
|
|
|
|
return mAnchorFocusRange->EndRef();
|
|
|
|
}
|
|
|
|
|
|
|
|
return mAnchorFocusRange->StartRef();
|
|
|
|
}
|
2000-05-13 12:07:34 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::GetAnchorNode(nsIDOMNode** aAnchorNode)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2009-04-06 21:17:37 +04:00
|
|
|
nsINode* anchorNode = GetAnchorNode();
|
|
|
|
if (anchorNode) {
|
|
|
|
return CallQueryInterface(anchorNode, aAnchorNode);
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*aAnchorNode = nullptr;
|
2009-04-06 21:17:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-05-13 12:07:34 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Selection::GetAnchorOffset(int32_t* aAnchorOffset)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2013-12-17 18:12:33 +04:00
|
|
|
*aAnchorOffset = static_cast<int32_t>(AnchorOffset());
|
2009-04-06 21:17:37 +04:00
|
|
|
return NS_OK;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// note: this can return a nil focus node
|
2000-05-13 12:07:34 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::GetFocusNode(nsIDOMNode** aFocusNode)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2009-04-06 21:17:37 +04:00
|
|
|
nsINode* focusNode = GetFocusNode();
|
|
|
|
if (focusNode) {
|
|
|
|
return CallQueryInterface(focusNode, aFocusNode);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*aFocusNode = nullptr;
|
2009-04-06 21:17:37 +04:00
|
|
|
return NS_OK;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Selection::GetFocusOffset(int32_t* aFocusOffset)
|
2009-04-06 21:17:37 +04:00
|
|
|
{
|
2013-12-17 18:12:33 +04:00
|
|
|
*aFocusOffset = static_cast<int32_t>(FocusOffset());
|
2009-04-06 21:17:37 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
void
|
2017-05-30 07:26:09 +03:00
|
|
|
Selection::SetAnchorFocusRange(int32_t indx)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
if (indx >= (int32_t)mRanges.Length())
|
2000-03-22 02:58:02 +03:00
|
|
|
return;
|
|
|
|
if (indx < 0) //release all
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
mAnchorFocusRange = nullptr;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
else{
|
2006-07-27 21:57:15 +04:00
|
|
|
mAnchorFocusRange = mRanges[indx].mRange;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-10-05 14:10:31 +04:00
|
|
|
static nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
CompareToRangeStart(nsINode* aCompareNode, int32_t aCompareOffset,
|
|
|
|
nsRange* aRange, int32_t* aCmp)
|
2006-07-27 21:57:15 +04:00
|
|
|
{
|
2017-07-11 14:53:04 +03:00
|
|
|
nsINode* start = aRange->GetStartContainer();
|
2009-10-05 14:10:31 +04:00
|
|
|
NS_ENSURE_STATE(aCompareNode && start);
|
2018-03-06 03:34:40 +03:00
|
|
|
// If the nodes that we're comparing are not in the same document or in the
|
|
|
|
// same subtree, assume that aCompareNode will fall at the end of the ranges.
|
2014-08-06 03:23:02 +04:00
|
|
|
if (aCompareNode->GetComposedDoc() != start->GetComposedDoc() ||
|
2018-03-06 03:34:40 +03:00
|
|
|
!start->GetComposedDoc() ||
|
|
|
|
aCompareNode->SubtreeRoot() != start->SubtreeRoot()) {
|
2010-09-23 22:44:01 +04:00
|
|
|
*aCmp = 1;
|
|
|
|
} else {
|
|
|
|
*aCmp = nsContentUtils::ComparePoints(aCompareNode, aCompareOffset,
|
|
|
|
start, aRange->StartOffset());
|
|
|
|
}
|
2009-10-05 14:10:31 +04:00
|
|
|
return NS_OK;
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
|
|
|
|
2009-10-05 14:10:31 +04:00
|
|
|
static nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
CompareToRangeEnd(nsINode* aCompareNode, int32_t aCompareOffset,
|
|
|
|
nsRange* aRange, int32_t* aCmp)
|
2006-07-27 21:57:15 +04:00
|
|
|
{
|
2017-07-11 15:11:37 +03:00
|
|
|
nsINode* end = aRange->GetEndContainer();
|
2009-10-05 14:10:31 +04:00
|
|
|
NS_ENSURE_STATE(aCompareNode && end);
|
2018-03-06 03:34:40 +03:00
|
|
|
// If the nodes that we're comparing are not in the same document or in the
|
|
|
|
// same subtree, assume that aCompareNode will fall at the end of the ranges.
|
2014-08-06 03:23:02 +04:00
|
|
|
if (aCompareNode->GetComposedDoc() != end->GetComposedDoc() ||
|
2018-03-06 03:34:40 +03:00
|
|
|
!end->GetComposedDoc() ||
|
|
|
|
aCompareNode->SubtreeRoot() != end->SubtreeRoot()) {
|
2010-09-23 22:44:01 +04:00
|
|
|
*aCmp = 1;
|
|
|
|
} else {
|
|
|
|
*aCmp = nsContentUtils::ComparePoints(aCompareNode, aCompareOffset,
|
|
|
|
end, aRange->EndOffset());
|
|
|
|
}
|
2009-10-05 14:10:31 +04:00
|
|
|
return NS_OK;
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
// Selection::FindInsertionPoint
|
2006-07-27 21:57:15 +04:00
|
|
|
//
|
2009-05-10 01:30:19 +04:00
|
|
|
// Binary searches the given sorted array of ranges for the insertion point
|
|
|
|
// for the given node/offset. The given comparator is used, and the index
|
|
|
|
// where the point should appear in the array is placed in *aInsertionPoint.
|
2006-07-27 21:57:15 +04:00
|
|
|
//
|
2009-05-10 01:30:19 +04:00
|
|
|
// If there is an item in the array equal to the input point, we will return
|
|
|
|
// the index of this item.
|
2006-07-27 21:57:15 +04:00
|
|
|
|
2009-10-05 14:10:31 +04:00
|
|
|
nsresult
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::FindInsertionPoint(
|
2009-05-10 01:30:19 +04:00
|
|
|
nsTArray<RangeData>* aElementArray,
|
2012-08-22 19:56:38 +04:00
|
|
|
nsINode* aPointNode, int32_t aPointOffset,
|
|
|
|
nsresult (*aComparator)(nsINode*,int32_t,nsRange*,int32_t*),
|
|
|
|
int32_t* aPoint)
|
2006-07-27 21:57:15 +04:00
|
|
|
{
|
2009-10-05 14:10:31 +04:00
|
|
|
*aPoint = 0;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t beginSearch = 0;
|
|
|
|
int32_t endSearch = aElementArray->Length(); // one beyond what to check
|
2006-07-27 21:57:15 +04:00
|
|
|
|
2012-07-25 00:25:36 +04:00
|
|
|
if (endSearch) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t center = endSearch - 1; // Check last index, then binary search
|
2012-07-25 00:25:36 +04:00
|
|
|
do {
|
|
|
|
nsRange* range = (*aElementArray)[center].mRange;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t cmp;
|
2012-07-25 00:25:36 +04:00
|
|
|
nsresult rv = aComparator(aPointNode, aPointOffset, range, &cmp);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
if (cmp < 0) { // point < cur
|
|
|
|
endSearch = center;
|
|
|
|
} else if (cmp > 0) { // point > cur
|
|
|
|
beginSearch = center + 1;
|
|
|
|
} else { // found match, done
|
|
|
|
beginSearch = center;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
center = (endSearch - beginSearch) / 2 + beginSearch;
|
|
|
|
} while (endSearch - beginSearch > 0);
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
2012-07-25 00:25:36 +04:00
|
|
|
|
2009-10-05 14:10:31 +04:00
|
|
|
*aPoint = beginSearch;
|
|
|
|
return NS_OK;
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
// Selection::SubtractRange
|
2009-05-10 01:30:19 +04:00
|
|
|
//
|
|
|
|
// A helper function that subtracts aSubtract from aRange, and adds
|
2009-05-10 01:36:17 +04:00
|
|
|
// 1 or 2 RangeData objects representing the remaining non-overlapping
|
|
|
|
// difference to aOutput. It is assumed that the caller has checked that
|
|
|
|
// aRange and aSubtract do indeed overlap
|
2009-05-10 01:30:19 +04:00
|
|
|
|
|
|
|
nsresult
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::SubtractRange(RangeData* aRange, nsRange* aSubtract,
|
|
|
|
nsTArray<RangeData>* aOutput)
|
2009-05-10 01:30:19 +04:00
|
|
|
{
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange* range = aRange->mRange;
|
2009-05-10 01:30:19 +04:00
|
|
|
|
|
|
|
// First we want to compare to the range start
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t cmp;
|
2017-07-11 14:53:04 +03:00
|
|
|
nsresult rv = CompareToRangeStart(range->GetStartContainer(),
|
2009-05-10 01:30:19 +04:00
|
|
|
range->StartOffset(),
|
2009-10-05 14:10:31 +04:00
|
|
|
aSubtract, &cmp);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2009-05-10 01:30:19 +04:00
|
|
|
// Also, make a comparison to the range end
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t cmp2;
|
2017-07-11 15:11:37 +03:00
|
|
|
rv = CompareToRangeEnd(range->GetEndContainer(),
|
2009-10-05 14:10:31 +04:00
|
|
|
range->EndOffset(),
|
|
|
|
aSubtract, &cmp2);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2009-05-10 01:30:19 +04:00
|
|
|
|
|
|
|
// If the existing range left overlaps the new range (aSubtract) then
|
|
|
|
// cmp < 0, and cmp2 < 0
|
|
|
|
// If it right overlaps the new range then cmp > 0 and cmp2 > 0
|
|
|
|
// If it fully contains the new range, then cmp < 0 and cmp2 > 0
|
|
|
|
|
|
|
|
if (cmp2 > 0) {
|
|
|
|
// We need to add a new RangeData to the output, running from
|
|
|
|
// the end of aSubtract to the end of range
|
2017-07-11 15:11:37 +03:00
|
|
|
RefPtr<nsRange> postOverlap = new nsRange(aSubtract->GetEndContainer());
|
2017-05-30 07:18:25 +03:00
|
|
|
rv = postOverlap->SetStartAndEnd(
|
2017-07-11 15:11:37 +03:00
|
|
|
aSubtract->GetEndContainer(), aSubtract->EndOffset(),
|
|
|
|
range->GetEndContainer(), range->EndOffset());
|
2017-05-30 07:18:25 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2009-05-10 01:30:19 +04:00
|
|
|
if (!postOverlap->Collapsed()) {
|
|
|
|
if (!aOutput->InsertElementAt(0, RangeData(postOverlap)))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
(*aOutput)[0].mTextRangeStyle = aRange->mTextRangeStyle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (cmp < 0) {
|
|
|
|
// We need to add a new RangeData to the output, running from
|
|
|
|
// the start of the range to the start of aSubtract
|
2017-07-11 14:53:04 +03:00
|
|
|
RefPtr<nsRange> preOverlap = new nsRange(range->GetStartContainer());
|
|
|
|
rv = preOverlap->SetStartAndEnd(range->GetStartContainer(),
|
|
|
|
range->StartOffset(),
|
|
|
|
aSubtract->GetStartContainer(),
|
|
|
|
aSubtract->StartOffset());
|
2017-05-30 07:18:25 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
2009-05-10 01:30:19 +04:00
|
|
|
if (!preOverlap->Collapsed()) {
|
|
|
|
if (!aOutput->InsertElementAt(0, RangeData(preOverlap)))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
(*aOutput)[0].mTextRangeStyle = aRange->mTextRangeStyle;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:26:01 +03:00
|
|
|
void
|
2015-10-18 08:24:48 +03:00
|
|
|
Selection::UserSelectRangesToAdd(nsRange* aItem, nsTArray<RefPtr<nsRange>>& aRangesToAdd)
|
2015-08-12 20:26:01 +03:00
|
|
|
{
|
|
|
|
aItem->ExcludeNonSelectableNodes(&aRangesToAdd);
|
|
|
|
if (aRangesToAdd.IsEmpty()) {
|
|
|
|
ErrorResult err;
|
|
|
|
nsINode* node = aItem->GetStartContainer(err);
|
|
|
|
if (node && node->IsContent() && node->AsContent()->GetEditingHost()) {
|
|
|
|
// A contenteditable node with user-select:none, for example.
|
|
|
|
// Allow it to have a collapsed selection (for the caret).
|
|
|
|
aItem->Collapse(GetDirection() == eDirPrevious);
|
|
|
|
aRangesToAdd.AppendElement(aItem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
nsresult
|
2015-08-12 20:26:01 +03:00
|
|
|
Selection::AddItem(nsRange* aItem, int32_t* aOutIndex, bool aNoStartSelect)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
|
|
|
if (!aItem)
|
2009-04-06 21:17:37 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
if (!aItem->IsPositioned())
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
2012-09-17 17:54:01 +04:00
|
|
|
|
|
|
|
NS_ASSERTION(aOutIndex, "aOutIndex can't be null");
|
|
|
|
|
2015-09-22 20:24:50 +03:00
|
|
|
if (mUserInitiated) {
|
2016-02-02 18:36:30 +03:00
|
|
|
AutoTArray<RefPtr<nsRange>, 4> rangesToAdd;
|
2017-05-23 20:09:26 +03:00
|
|
|
*aOutIndex = int32_t(mRanges.Length()) - 1;
|
2015-08-12 20:26:01 +03:00
|
|
|
|
2016-09-27 23:05:43 +03:00
|
|
|
nsIDocument* doc = GetParentObject();
|
|
|
|
bool selectEventsEnabled =
|
|
|
|
nsFrameSelection::sSelectionEventsEnabled ||
|
|
|
|
(doc && nsContentUtils::IsSystemPrincipal(doc->NodePrincipal()));
|
|
|
|
|
2016-06-07 16:42:06 +03:00
|
|
|
if (!aNoStartSelect &&
|
2016-06-09 12:35:22 +03:00
|
|
|
mSelectionType == SelectionType::eNormal &&
|
2016-09-27 23:05:43 +03:00
|
|
|
selectEventsEnabled && Collapsed() &&
|
2015-08-12 20:26:01 +03:00
|
|
|
!IsBlockingSelectionChangeEvents()) {
|
|
|
|
// First, we generate the ranges to add with a scratch range, which is a
|
|
|
|
// clone of the original range passed in. We do this seperately, because the
|
|
|
|
// selectstart event could have caused the world to change, and required
|
|
|
|
// ranges to be re-generated
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRange> scratchRange = aItem->CloneRange();
|
2015-08-12 20:26:01 +03:00
|
|
|
UserSelectRangesToAdd(scratchRange, rangesToAdd);
|
|
|
|
bool newRangesNonEmpty = rangesToAdd.Length() > 1 ||
|
|
|
|
(rangesToAdd.Length() == 1 && !rangesToAdd[0]->Collapsed());
|
|
|
|
|
|
|
|
MOZ_ASSERT(!newRangesNonEmpty || nsContentUtils::IsSafeToRunScript());
|
|
|
|
if (newRangesNonEmpty && nsContentUtils::IsSafeToRunScript()) {
|
|
|
|
// We consider a selection to be starting if we are currently collapsed,
|
|
|
|
// and the selection is becoming uncollapsed, and this is caused by a user
|
|
|
|
// initiated event.
|
|
|
|
bool defaultAction = true;
|
2015-09-02 20:39:39 +03:00
|
|
|
|
2016-10-13 06:36:29 +03:00
|
|
|
// The spec currently doesn't say that we should dispatch this event
|
|
|
|
// on text controls, so for now we only support doing that under a
|
|
|
|
// pref, disabled by default.
|
|
|
|
// See https://github.com/w3c/selection-api/issues/53.
|
|
|
|
bool dispatchEvent = true;
|
2017-07-11 14:53:04 +03:00
|
|
|
nsCOMPtr<nsINode> target = aItem->GetStartContainer();
|
2016-10-13 06:36:29 +03:00
|
|
|
if (nsFrameSelection::sSelectionEventsOnTextControlsEnabled) {
|
|
|
|
// Get the first element which isn't in a native anonymous subtree
|
|
|
|
while (target && target->IsInNativeAnonymousSubtree()) {
|
|
|
|
target = target->GetParent();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (target->IsInNativeAnonymousSubtree()) {
|
|
|
|
// This is a selection under a text control, so don't dispatch the
|
|
|
|
// event.
|
|
|
|
dispatchEvent = false;
|
|
|
|
}
|
2015-09-02 20:39:39 +03:00
|
|
|
}
|
|
|
|
|
2016-10-13 06:36:29 +03:00
|
|
|
if (dispatchEvent) {
|
|
|
|
nsContentUtils::DispatchTrustedEvent(GetParentObject(), target,
|
|
|
|
NS_LITERAL_STRING("selectstart"),
|
|
|
|
true, true, &defaultAction);
|
2015-08-12 20:26:01 +03:00
|
|
|
|
2016-10-13 06:36:29 +03:00
|
|
|
if (!defaultAction) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2015-08-12 20:26:01 +03:00
|
|
|
|
2016-10-13 06:36:29 +03:00
|
|
|
// As we just dispatched an event to the DOM, something could have
|
|
|
|
// changed under our feet. Re-generate the rangesToAdd array, and ensure
|
|
|
|
// that the range we are about to add is still valid.
|
|
|
|
if (!aItem->IsPositioned()) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2015-08-12 20:26:01 +03:00
|
|
|
}
|
2015-02-04 02:54:31 +03:00
|
|
|
}
|
2015-08-12 20:26:01 +03:00
|
|
|
|
|
|
|
// The scratch ranges we generated may be invalid now, throw them out
|
|
|
|
rangesToAdd.ClearAndRetainStorage();
|
2015-02-04 02:54:31 +03:00
|
|
|
}
|
2015-08-12 20:26:01 +03:00
|
|
|
|
|
|
|
// Generate the ranges to add
|
|
|
|
UserSelectRangesToAdd(aItem, rangesToAdd);
|
2015-02-21 07:27:59 +03:00
|
|
|
size_t newAnchorFocusIndex =
|
|
|
|
GetDirection() == eDirPrevious ? 0 : rangesToAdd.Length() - 1;
|
2014-09-10 21:07:36 +04:00
|
|
|
for (size_t i = 0; i < rangesToAdd.Length(); ++i) {
|
2015-02-21 07:27:59 +03:00
|
|
|
int32_t index;
|
|
|
|
nsresult rv = AddItemInternal(rangesToAdd[i], &index);
|
2014-09-10 21:07:36 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2015-02-21 07:27:59 +03:00
|
|
|
if (i == newAnchorFocusIndex) {
|
|
|
|
*aOutIndex = index;
|
|
|
|
rangesToAdd[i]->SetIsGenerated(false);
|
|
|
|
} else {
|
|
|
|
rangesToAdd[i]->SetIsGenerated(true);
|
|
|
|
}
|
2014-09-10 21:07:36 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
return AddItemInternal(aItem, aOutIndex);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Selection::AddItemInternal(nsRange* aItem, int32_t* aOutIndex)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aItem);
|
|
|
|
MOZ_ASSERT(aItem->IsPositioned());
|
|
|
|
MOZ_ASSERT(aOutIndex);
|
|
|
|
|
2012-09-17 17:54:01 +04:00
|
|
|
*aOutIndex = -1;
|
2006-07-27 21:57:15 +04:00
|
|
|
|
|
|
|
// a common case is that we have no ranges yet
|
|
|
|
if (mRanges.Length() == 0) {
|
2009-05-10 01:30:19 +04:00
|
|
|
if (!mRanges.AppendElement(RangeData(aItem)))
|
2006-07-27 21:57:15 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2015-08-12 20:26:01 +03:00
|
|
|
aItem->SetSelection(this);
|
2011-12-20 13:15:41 +04:00
|
|
|
|
2012-09-17 17:54:01 +04:00
|
|
|
*aOutIndex = 0;
|
2006-07-27 21:57:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startIndex, endIndex;
|
2017-07-11 14:53:04 +03:00
|
|
|
nsresult rv = GetIndicesForInterval(aItem->GetStartContainer(),
|
2012-07-12 19:56:30 +04:00
|
|
|
aItem->StartOffset(),
|
2017-07-11 15:11:37 +03:00
|
|
|
aItem->GetEndContainer(),
|
2012-07-12 19:56:30 +04:00
|
|
|
aItem->EndOffset(), false,
|
|
|
|
&startIndex, &endIndex);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-27 21:57:15 +04:00
|
|
|
|
2009-05-10 01:30:19 +04:00
|
|
|
if (endIndex == -1) {
|
|
|
|
// All ranges start after the given range. We can insert our range at
|
|
|
|
// position 0, knowing there are no overlaps (handled below)
|
|
|
|
startIndex = 0;
|
|
|
|
endIndex = 0;
|
|
|
|
} else if (startIndex == -1) {
|
|
|
|
// All ranges end before the given range. We can insert our range at
|
|
|
|
// the end of the array, knowing there are no overlaps (handled below)
|
|
|
|
startIndex = mRanges.Length();
|
2009-09-25 14:11:42 +04:00
|
|
|
endIndex = startIndex;
|
2006-08-10 23:56:43 +04:00
|
|
|
}
|
|
|
|
|
2009-05-10 01:30:19 +04:00
|
|
|
// If the range is already contained in mRanges, silently succeed
|
2017-07-11 14:53:04 +03:00
|
|
|
bool sameRange = EqualsRangeAtPoint(aItem->GetStartContainer(),
|
|
|
|
aItem->StartOffset(),
|
2017-07-11 15:11:37 +03:00
|
|
|
aItem->GetEndContainer(),
|
2017-07-11 14:53:04 +03:00
|
|
|
aItem->EndOffset(), startIndex);
|
2009-05-10 01:30:19 +04:00
|
|
|
if (sameRange) {
|
2012-09-17 17:54:01 +04:00
|
|
|
*aOutIndex = startIndex;
|
2009-05-10 01:30:19 +04:00
|
|
|
return NS_OK;
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
2009-05-10 01:30:19 +04:00
|
|
|
|
2009-09-25 14:11:42 +04:00
|
|
|
if (startIndex == endIndex) {
|
|
|
|
// The new range doesn't overlap any existing ranges
|
|
|
|
if (!mRanges.InsertElementAt(startIndex, RangeData(aItem)))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2015-08-12 20:26:01 +03:00
|
|
|
aItem->SetSelection(this);
|
2012-09-17 17:54:01 +04:00
|
|
|
*aOutIndex = startIndex;
|
2009-09-25 14:11:42 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-05-10 01:30:19 +04:00
|
|
|
// We now know that at least 1 existing range overlaps with the range that
|
|
|
|
// we are trying to add. In fact, the only ranges of interest are those at
|
|
|
|
// the two end points, startIndex and endIndex - 1 (which may point to the
|
|
|
|
// same range) as these may partially overlap the new range. Any ranges
|
|
|
|
// between these indices are fully overlapped by the new range, and so can be
|
|
|
|
// removed
|
|
|
|
nsTArray<RangeData> overlaps;
|
|
|
|
if (!overlaps.InsertElementAt(0, mRanges[startIndex]))
|
2006-07-27 21:57:15 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2009-05-10 01:30:19 +04:00
|
|
|
|
|
|
|
if (endIndex - 1 != startIndex) {
|
|
|
|
if (!overlaps.InsertElementAt(1, mRanges[endIndex - 1]))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
2009-05-10 01:30:19 +04:00
|
|
|
|
|
|
|
// Remove all the overlapping ranges
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = startIndex; i < endIndex; ++i) {
|
2015-08-12 20:26:01 +03:00
|
|
|
mRanges[i].mRange->SetSelection(nullptr);
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
2009-05-10 01:30:19 +04:00
|
|
|
mRanges.RemoveElementsAt(startIndex, endIndex - startIndex);
|
|
|
|
|
|
|
|
nsTArray<RangeData> temp;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = overlaps.Length() - 1; i >= 0; i--) {
|
2009-05-10 01:30:19 +04:00
|
|
|
nsresult rv = SubtractRange(&overlaps[i], aItem, &temp);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
|
|
|
|
2009-05-10 01:30:19 +04:00
|
|
|
// Insert the new element into our "leftovers" array
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t insertionPoint;
|
2017-07-11 14:53:04 +03:00
|
|
|
rv = FindInsertionPoint(&temp, aItem->GetStartContainer(),
|
2012-07-12 19:56:30 +04:00
|
|
|
aItem->StartOffset(), CompareToRangeStart,
|
|
|
|
&insertionPoint);
|
2009-10-05 14:10:31 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2006-07-27 21:57:15 +04:00
|
|
|
|
2009-05-10 01:30:19 +04:00
|
|
|
if (!temp.InsertElementAt(insertionPoint, RangeData(aItem)))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
// Merge the leftovers back in to mRanges
|
|
|
|
if (!mRanges.InsertElementsAt(startIndex, temp))
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
2006-07-27 21:57:15 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < temp.Length(); ++i) {
|
2015-08-12 20:26:01 +03:00
|
|
|
temp[i].mRange->SetSelection(this);
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
|
|
|
|
2009-09-22 22:05:39 +04:00
|
|
|
*aOutIndex = startIndex + insertionPoint;
|
2006-07-27 21:57:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
|
|
|
nsresult
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::RemoveItem(nsRange* aItem)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2002-12-11 03:02:49 +03:00
|
|
|
if (!aItem)
|
2000-03-22 02:58:02 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2006-07-27 21:57:15 +04:00
|
|
|
|
|
|
|
// Find the range's index & remove it. We could use FindInsertionPoint to
|
|
|
|
// get O(log n) time, but that requires many expensive DOM comparisons.
|
|
|
|
// For even several thousand items, this is probably faster because the
|
|
|
|
// comparisons are so fast.
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t idx = -1;
|
|
|
|
uint32_t i;
|
2006-07-27 21:57:15 +04:00
|
|
|
for (i = 0; i < mRanges.Length(); i ++) {
|
|
|
|
if (mRanges[i].mRange == aItem) {
|
2012-08-22 19:56:38 +04:00
|
|
|
idx = (int32_t)i;
|
2006-07-27 21:57:15 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-07-27 22:39:18 +04:00
|
|
|
if (idx < 0)
|
2017-04-26 15:50:36 +03:00
|
|
|
return NS_ERROR_DOM_NOT_FOUND_ERR;
|
2006-07-27 21:57:15 +04:00
|
|
|
|
2009-05-10 01:30:19 +04:00
|
|
|
mRanges.RemoveElementAt(idx);
|
2015-08-12 20:26:01 +03:00
|
|
|
aItem->SetSelection(nullptr);
|
2006-07-27 21:57:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2008-10-17 23:46:33 +04:00
|
|
|
nsresult
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::RemoveCollapsedRanges()
|
2008-10-17 23:46:33 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t i = 0;
|
2008-10-17 23:46:33 +04:00
|
|
|
while (i < mRanges.Length()) {
|
2009-04-06 21:17:37 +04:00
|
|
|
if (mRanges[i].mRange->Collapsed()) {
|
|
|
|
nsresult rv = RemoveItem(mRanges[i].mRange);
|
2008-10-17 23:46:33 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
} else {
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
|
|
|
nsresult
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::Clear(nsPresContext* aPresContext)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2017-05-30 07:26:09 +03:00
|
|
|
SetAnchorFocusRange(-1);
|
2006-07-27 21:57:15 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mRanges.Length(); ++i) {
|
2015-08-12 20:26:01 +03:00
|
|
|
mRanges[i].mRange->SetSelection(nullptr);
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(aPresContext, mRanges[i].mRange, false);
|
2009-04-06 21:17:37 +04:00
|
|
|
}
|
2006-07-27 21:57:15 +04:00
|
|
|
mRanges.Clear();
|
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
// Reset direction so for more dependable table selection range handling
|
|
|
|
SetDirection(eDirNext);
|
2006-06-20 04:30:18 +04:00
|
|
|
|
|
|
|
// If this was an ATTENTION selection, change it back to normal now
|
2009-01-14 15:24:10 +03:00
|
|
|
if (mFrameSelection &&
|
|
|
|
mFrameSelection->GetDisplaySelection() ==
|
2006-06-20 04:30:18 +04:00
|
|
|
nsISelectionController::SELECTION_ATTENTION) {
|
|
|
|
mFrameSelection->SetDisplaySelection(nsISelectionController::SELECTION_ON);
|
|
|
|
}
|
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-07-17 16:06:24 +04:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Selection::GetType(int16_t* aType)
|
2008-07-17 16:06:24 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aType);
|
2016-06-11 05:06:37 +03:00
|
|
|
*aType = ToRawSelectionType(Type());
|
2008-07-17 16:06:24 +04:00
|
|
|
return NS_OK;
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
|
|
|
|
2009-09-25 14:11:42 +04:00
|
|
|
// RangeMatches*Point
|
|
|
|
//
|
|
|
|
// Compares the range beginning or ending point, and returns true if it
|
|
|
|
// exactly matches the given DOM point.
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static inline bool
|
2012-08-22 19:56:38 +04:00
|
|
|
RangeMatchesBeginPoint(nsRange* aRange, nsINode* aNode, int32_t aOffset)
|
2009-09-25 14:11:42 +04:00
|
|
|
{
|
2017-07-11 14:53:04 +03:00
|
|
|
return aRange->GetStartContainer() == aNode &&
|
2017-07-19 16:49:52 +03:00
|
|
|
static_cast<int32_t>(aRange->StartOffset()) == aOffset;
|
2009-09-25 14:11:42 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
static inline bool
|
2012-08-22 19:56:38 +04:00
|
|
|
RangeMatchesEndPoint(nsRange* aRange, nsINode* aNode, int32_t aOffset)
|
2009-09-25 14:11:42 +04:00
|
|
|
{
|
2017-07-19 16:49:52 +03:00
|
|
|
return aRange->GetEndContainer() == aNode &&
|
|
|
|
static_cast<int32_t>(aRange->EndOffset()) == aOffset;
|
2009-09-25 14:11:42 +04:00
|
|
|
}
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
// Selection::EqualsRangeAtPoint
|
2009-09-25 14:11:42 +04:00
|
|
|
//
|
|
|
|
// Utility method for checking equivalence of two ranges.
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::EqualsRangeAtPoint(
|
2012-08-22 19:56:38 +04:00
|
|
|
nsINode* aBeginNode, int32_t aBeginOffset,
|
|
|
|
nsINode* aEndNode, int32_t aEndOffset,
|
|
|
|
int32_t aRangeIndex)
|
2009-09-25 14:11:42 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
if (aRangeIndex >=0 && aRangeIndex < (int32_t) mRanges.Length()) {
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange* range = mRanges[aRangeIndex].mRange;
|
2009-09-25 14:11:42 +04:00
|
|
|
if (RangeMatchesBeginPoint(range, aBeginNode, aBeginOffset) &&
|
|
|
|
RangeMatchesEndPoint(range, aEndNode, aEndOffset))
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2009-09-25 14:11:42 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2009-09-25 14:11:42 +04:00
|
|
|
}
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
// Selection::GetRangesForInterval
|
2006-07-27 21:57:15 +04:00
|
|
|
//
|
2012-01-10 18:19:54 +04:00
|
|
|
// XPCOM wrapper for the nsTArray version
|
2006-07-27 21:57:15 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Selection::GetRangesForInterval(nsIDOMNode* aBeginNode, int32_t aBeginOffset,
|
|
|
|
nsIDOMNode* aEndNode, int32_t aEndOffset,
|
2012-06-08 18:20:55 +04:00
|
|
|
bool aAllowAdjacent,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t* aResultCount,
|
2012-06-08 18:20:55 +04:00
|
|
|
nsIDOMRange*** aResults)
|
2006-07-27 21:57:15 +04:00
|
|
|
{
|
2009-05-10 01:30:19 +04:00
|
|
|
if (!aBeginNode || ! aEndNode || ! aResultCount || ! aResults)
|
2006-07-27 21:57:15 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
2006-09-24 10:04:00 +04:00
|
|
|
*aResultCount = 0;
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResults = nullptr;
|
2013-12-17 18:12:33 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<nsRange>> results;
|
2013-12-17 18:12:33 +04:00
|
|
|
ErrorResult result;
|
2012-01-10 18:19:54 +04:00
|
|
|
nsCOMPtr<nsINode> beginNode = do_QueryInterface(aBeginNode);
|
|
|
|
nsCOMPtr<nsINode> endNode = do_QueryInterface(aEndNode);
|
2013-12-17 18:12:33 +04:00
|
|
|
NS_ENSURE_TRUE(beginNode && endNode, NS_ERROR_NULL_POINTER);
|
|
|
|
GetRangesForInterval(*beginNode, aBeginOffset, *endNode, aEndOffset,
|
|
|
|
aAllowAdjacent, results, result);
|
|
|
|
if (result.Failed()) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2013-12-17 18:12:33 +04:00
|
|
|
}
|
2012-01-10 18:19:54 +04:00
|
|
|
*aResultCount = results.Length();
|
|
|
|
if (*aResultCount == 0) {
|
2006-07-27 21:57:15 +04:00
|
|
|
return NS_OK;
|
2012-01-10 18:19:54 +04:00
|
|
|
}
|
2006-07-27 21:57:15 +04:00
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
*aResults = static_cast<nsIDOMRange**>
|
2015-03-27 03:01:12 +03:00
|
|
|
(moz_xmalloc(sizeof(nsIDOMRange*) * *aResultCount));
|
2006-07-27 21:57:15 +04:00
|
|
|
NS_ENSURE_TRUE(*aResults, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
for (uint32_t i = 0; i < *aResultCount; i++) {
|
2014-03-15 23:00:15 +04:00
|
|
|
(*aResults)[i] = results[i].forget().take();
|
2013-12-17 18:12:33 +04:00
|
|
|
}
|
2006-07-27 21:57:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
|
|
|
|
void
|
|
|
|
Selection::GetRangesForInterval(nsINode& aBeginNode, int32_t aBeginOffset,
|
|
|
|
nsINode& aEndNode, int32_t aEndOffset,
|
|
|
|
bool aAllowAdjacent,
|
2015-10-18 08:24:48 +03:00
|
|
|
nsTArray<RefPtr<nsRange>>& aReturn,
|
2013-12-17 18:12:33 +04:00
|
|
|
mozilla::ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsTArray<nsRange*> results;
|
|
|
|
nsresult rv = GetRangesForIntervalArray(&aBeginNode, aBeginOffset,
|
|
|
|
&aEndNode, aEndOffset,
|
|
|
|
aAllowAdjacent, &results);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aReturn.SetLength(results.Length());
|
|
|
|
for (uint32_t i = 0; i < results.Length(); ++i) {
|
|
|
|
aReturn[i] = results[i]; // AddRefs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
// Selection::GetRangesForIntervalArray
|
2006-07-27 21:57:15 +04:00
|
|
|
//
|
2012-01-10 18:19:54 +04:00
|
|
|
// Fills a nsTArray with the ranges overlapping the range specified by
|
2009-05-10 01:30:19 +04:00
|
|
|
// the given endpoints. Ranges in the selection exactly adjacent to the
|
|
|
|
// input range are not returned unless aAllowAdjacent is set.
|
|
|
|
//
|
|
|
|
// For example, if the following ranges were in the selection
|
|
|
|
// (assume everything is within the same node)
|
|
|
|
//
|
|
|
|
// Start Offset: 0 2 7 9
|
|
|
|
// End Offset: 2 5 9 10
|
|
|
|
//
|
|
|
|
// and passed aBeginOffset of 2 and aEndOffset of 9, then with
|
|
|
|
// aAllowAdjacent set, all the ranges should be returned. If
|
|
|
|
// aAllowAdjacent was false, the ranges [2, 5] and [7, 9] only
|
|
|
|
// should be returned
|
|
|
|
//
|
|
|
|
// Now that overlapping ranges are disallowed, there can be a maximum of
|
|
|
|
// 2 adjacent ranges
|
|
|
|
|
2009-04-06 21:17:37 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
Selection::GetRangesForIntervalArray(nsINode* aBeginNode, int32_t aBeginOffset,
|
|
|
|
nsINode* aEndNode, int32_t aEndOffset,
|
2012-06-08 18:20:55 +04:00
|
|
|
bool aAllowAdjacent,
|
|
|
|
nsTArray<nsRange*>* aRanges)
|
2009-04-06 21:17:37 +04:00
|
|
|
{
|
2006-07-27 21:57:15 +04:00
|
|
|
aRanges->Clear();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startIndex, endIndex;
|
2012-07-12 19:56:30 +04:00
|
|
|
nsresult res = GetIndicesForInterval(aBeginNode, aBeginOffset,
|
|
|
|
aEndNode, aEndOffset, aAllowAdjacent,
|
|
|
|
&startIndex, &endIndex);
|
|
|
|
NS_ENSURE_SUCCESS(res, res);
|
|
|
|
|
2009-05-10 01:30:19 +04:00
|
|
|
if (startIndex == -1 || endIndex == -1)
|
2006-07-27 21:57:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (int32_t i = startIndex; i < endIndex; i++) {
|
2012-01-10 18:19:54 +04:00
|
|
|
if (!aRanges->AppendElement(mRanges[i].mRange))
|
2009-05-10 01:30:19 +04:00
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
// Selection::GetIndicesForInterval
|
2009-05-10 01:30:19 +04:00
|
|
|
//
|
2012-01-10 18:19:54 +04:00
|
|
|
// Works on the same principle as GetRangesForIntervalArray above, however
|
2009-05-10 01:30:19 +04:00
|
|
|
// instead this returns the indices into mRanges between which the
|
|
|
|
// overlapping ranges lie.
|
|
|
|
|
2012-07-12 19:56:30 +04:00
|
|
|
nsresult
|
2012-08-22 19:56:38 +04:00
|
|
|
Selection::GetIndicesForInterval(nsINode* aBeginNode, int32_t aBeginOffset,
|
|
|
|
nsINode* aEndNode, int32_t aEndOffset,
|
2012-06-08 18:20:55 +04:00
|
|
|
bool aAllowAdjacent,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t* aStartIndex, int32_t* aEndIndex)
|
2009-05-10 01:30:19 +04:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startIndex;
|
|
|
|
int32_t endIndex;
|
2010-12-20 12:19:00 +03:00
|
|
|
|
|
|
|
if (!aStartIndex)
|
|
|
|
aStartIndex = &startIndex;
|
|
|
|
if (!aEndIndex)
|
|
|
|
aEndIndex = &endIndex;
|
|
|
|
|
|
|
|
*aStartIndex = -1;
|
|
|
|
*aEndIndex = -1;
|
2009-05-10 01:30:19 +04:00
|
|
|
|
|
|
|
if (mRanges.Length() == 0)
|
2012-07-12 19:56:30 +04:00
|
|
|
return NS_OK;
|
2009-05-10 01:30:19 +04:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool intervalIsCollapsed = aBeginNode == aEndNode &&
|
2009-09-25 14:11:42 +04:00
|
|
|
aBeginOffset == aEndOffset;
|
|
|
|
|
2009-05-10 01:30:19 +04:00
|
|
|
// Ranges that end before the given interval and begin after the given
|
|
|
|
// interval can be discarded
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t endsBeforeIndex;
|
2012-07-04 08:36:45 +04:00
|
|
|
if (NS_FAILED(FindInsertionPoint(&mRanges, aEndNode, aEndOffset,
|
2009-10-05 14:10:31 +04:00
|
|
|
&CompareToRangeStart,
|
|
|
|
&endsBeforeIndex))) {
|
2012-07-12 19:56:30 +04:00
|
|
|
return NS_OK;
|
2009-10-05 14:10:31 +04:00
|
|
|
}
|
2009-09-25 14:11:42 +04:00
|
|
|
|
|
|
|
if (endsBeforeIndex == 0) {
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange* endRange = mRanges[endsBeforeIndex].mRange;
|
2009-09-25 14:11:42 +04:00
|
|
|
|
|
|
|
// If the interval is strictly before the range at index 0, we can optimize
|
|
|
|
// by returning now - all ranges start after the given interval
|
|
|
|
if (!RangeMatchesBeginPoint(endRange, aEndNode, aEndOffset))
|
2012-07-12 19:56:30 +04:00
|
|
|
return NS_OK;
|
2009-09-25 14:11:42 +04:00
|
|
|
|
|
|
|
// We now know that the start point of mRanges[0].mRange equals the end of
|
|
|
|
// the interval. Thus, when aAllowadjacent is true, the caller is always
|
|
|
|
// interested in this range. However, when excluding adjacencies, we must
|
|
|
|
// remember to include the range when both it and the given interval are
|
|
|
|
// collapsed to the same point
|
|
|
|
if (!aAllowAdjacent && !(endRange->Collapsed() && intervalIsCollapsed))
|
2012-07-12 19:56:30 +04:00
|
|
|
return NS_OK;
|
2009-09-25 14:11:42 +04:00
|
|
|
}
|
2009-05-10 01:30:19 +04:00
|
|
|
*aEndIndex = endsBeforeIndex;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t beginsAfterIndex;
|
2012-07-04 08:36:45 +04:00
|
|
|
if (NS_FAILED(FindInsertionPoint(&mRanges, aBeginNode, aBeginOffset,
|
2009-10-05 14:10:31 +04:00
|
|
|
&CompareToRangeEnd,
|
|
|
|
&beginsAfterIndex))) {
|
2012-07-12 19:56:30 +04:00
|
|
|
return NS_OK;
|
2009-10-05 14:10:31 +04:00
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
if (beginsAfterIndex == (int32_t) mRanges.Length())
|
2012-07-12 19:56:30 +04:00
|
|
|
return NS_OK; // optimization: all ranges are strictly before us
|
2009-05-10 01:30:19 +04:00
|
|
|
|
2006-07-27 21:57:15 +04:00
|
|
|
if (aAllowAdjacent) {
|
2009-09-25 14:11:42 +04:00
|
|
|
// At this point, one of the following holds:
|
|
|
|
// endsBeforeIndex == mRanges.Length(),
|
|
|
|
// endsBeforeIndex points to a range whose start point does not equal the
|
|
|
|
// given interval's start point
|
|
|
|
// endsBeforeIndex points to a range whose start point equals the given
|
|
|
|
// interval's start point
|
|
|
|
// In the final case, there can be two such ranges, a collapsed range, and
|
|
|
|
// an adjacent range (they will appear in mRanges in that order). For this
|
|
|
|
// final case, we need to increment endsBeforeIndex, until one of the
|
|
|
|
// first two possibilites hold
|
2012-08-22 19:56:38 +04:00
|
|
|
while (endsBeforeIndex < (int32_t) mRanges.Length()) {
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange* endRange = mRanges[endsBeforeIndex].mRange;
|
2009-09-25 14:11:42 +04:00
|
|
|
if (!RangeMatchesBeginPoint(endRange, aEndNode, aEndOffset))
|
|
|
|
break;
|
|
|
|
endsBeforeIndex++;
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
2009-05-10 01:30:19 +04:00
|
|
|
|
2009-09-25 14:11:42 +04:00
|
|
|
// Likewise, one of the following holds:
|
|
|
|
// beginsAfterIndex == 0,
|
|
|
|
// beginsAfterIndex points to a range whose end point does not equal
|
|
|
|
// the given interval's end point
|
|
|
|
// beginsOnOrAfter points to a range whose end point equals the given
|
|
|
|
// interval's end point
|
|
|
|
// In the final case, there can be two such ranges, an adjacent range, and
|
|
|
|
// a collapsed range (they will appear in mRanges in that order). For this
|
|
|
|
// final case, we only need to take action if both those ranges exist, and
|
|
|
|
// we are pointing to the collapsed range - we need to point to the
|
|
|
|
// adjacent range
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange* beginRange = mRanges[beginsAfterIndex].mRange;
|
2009-09-25 14:11:42 +04:00
|
|
|
if (beginsAfterIndex > 0 && beginRange->Collapsed() &&
|
|
|
|
RangeMatchesEndPoint(beginRange, aBeginNode, aBeginOffset)) {
|
|
|
|
beginRange = mRanges[beginsAfterIndex - 1].mRange;
|
|
|
|
if (RangeMatchesEndPoint(beginRange, aBeginNode, aBeginOffset))
|
|
|
|
beginsAfterIndex--;
|
|
|
|
}
|
2006-07-27 21:57:15 +04:00
|
|
|
} else {
|
2009-09-25 14:11:42 +04:00
|
|
|
// See above for the possibilities at this point. The only case where we
|
|
|
|
// need to take action is when the range at beginsAfterIndex ends on
|
|
|
|
// the given interval's start point, but that range isn't collapsed (a
|
|
|
|
// collapsed range should be included in the returned results).
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange* beginRange = mRanges[beginsAfterIndex].mRange;
|
2009-09-25 14:11:42 +04:00
|
|
|
if (RangeMatchesEndPoint(beginRange, aBeginNode, aBeginOffset) &&
|
|
|
|
!beginRange->Collapsed())
|
2009-05-10 01:30:19 +04:00
|
|
|
beginsAfterIndex++;
|
|
|
|
|
2009-09-25 14:11:42 +04:00
|
|
|
// Again, see above for the meaning of endsBeforeIndex at this point.
|
|
|
|
// In particular, endsBeforeIndex may point to a collaped range which
|
|
|
|
// represents the point at the end of the interval - this range should be
|
|
|
|
// included
|
2012-08-22 19:56:38 +04:00
|
|
|
if (endsBeforeIndex < (int32_t) mRanges.Length()) {
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange* endRange = mRanges[endsBeforeIndex].mRange;
|
2009-09-25 14:11:42 +04:00
|
|
|
if (RangeMatchesBeginPoint(endRange, aEndNode, aEndOffset) &&
|
|
|
|
endRange->Collapsed())
|
|
|
|
endsBeforeIndex++;
|
|
|
|
}
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
2009-05-10 01:30:19 +04:00
|
|
|
|
2012-07-12 19:56:30 +04:00
|
|
|
NS_ASSERTION(beginsAfterIndex <= endsBeforeIndex,
|
|
|
|
"Is mRanges not ordered?");
|
|
|
|
NS_ENSURE_STATE(beginsAfterIndex <= endsBeforeIndex);
|
|
|
|
|
2009-05-10 01:30:19 +04:00
|
|
|
*aStartIndex = beginsAfterIndex;
|
|
|
|
*aEndIndex = endsBeforeIndex;
|
2012-07-12 19:56:30 +04:00
|
|
|
return NS_OK;
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
|
|
|
|
2017-08-23 13:09:07 +03:00
|
|
|
nsresult
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::GetPrimaryFrameForAnchorNode(nsIFrame** aReturnFrame)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
|
|
|
if (!aReturnFrame)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t frameOffset = 0;
|
2000-03-22 02:58:02 +03:00
|
|
|
*aReturnFrame = 0;
|
2009-04-06 21:17:38 +04:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(GetAnchorNode());
|
2000-05-11 08:25:43 +04:00
|
|
|
if (content && mFrameSelection)
|
2000-06-28 08:00:12 +04:00
|
|
|
{
|
2006-04-26 06:01:07 +04:00
|
|
|
*aReturnFrame = mFrameSelection->
|
2013-12-17 18:12:33 +04:00
|
|
|
GetFrameForNodeOffset(content, AnchorOffset(),
|
2006-04-26 06:01:07 +04:00
|
|
|
mFrameSelection->GetHint(), &frameOffset);
|
|
|
|
if (*aReturnFrame)
|
|
|
|
return NS_OK;
|
2000-06-28 08:00:12 +04:00
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2017-08-23 13:09:07 +03:00
|
|
|
nsresult
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::GetPrimaryFrameForFocusNode(nsIFrame** aReturnFrame,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t* aOffsetUsed,
|
2012-06-08 18:20:55 +04:00
|
|
|
bool aVisual)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2017-08-23 13:09:07 +03:00
|
|
|
if (!aReturnFrame) {
|
2000-03-22 02:58:02 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2017-08-23 13:09:07 +03:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2017-08-23 13:09:07 +03:00
|
|
|
*aReturnFrame = nullptr;
|
|
|
|
nsINode* focusNode = GetFocusNode();
|
2017-08-25 17:54:46 +03:00
|
|
|
if (!focusNode || !focusNode->IsContent() || !mFrameSelection) {
|
2005-11-16 12:37:01 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
2017-08-23 13:09:07 +03:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2017-08-23 13:09:07 +03:00
|
|
|
nsCOMPtr<nsIContent> content = focusNode->AsContent();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t frameOffset = 0;
|
2017-08-23 13:09:07 +03:00
|
|
|
if (!aOffsetUsed) {
|
2003-04-18 01:08:44 +04:00
|
|
|
aOffsetUsed = &frameOffset;
|
2017-08-23 13:09:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
GetPrimaryOrCaretFrameForNodeOffset(content, FocusOffset(), aReturnFrame,
|
|
|
|
aOffsetUsed, aVisual);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If content is whitespace only, we promote focus node to parent because
|
|
|
|
// whitespace only node might have no frame.
|
|
|
|
|
|
|
|
if (!content->TextIsOnlyWhitespace()) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> parent = content->GetParent();
|
|
|
|
if (NS_WARN_IF(!parent)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2018-01-23 16:30:18 +03:00
|
|
|
int32_t offset = parent->ComputeIndexOf(content);
|
2017-08-23 13:09:07 +03:00
|
|
|
|
|
|
|
return GetPrimaryOrCaretFrameForNodeOffset(parent, offset, aReturnFrame,
|
|
|
|
aOffsetUsed, aVisual);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
Selection::GetPrimaryOrCaretFrameForNodeOffset(nsIContent* aContent,
|
|
|
|
uint32_t aOffset,
|
|
|
|
nsIFrame** aReturnFrame,
|
|
|
|
int32_t* aOffsetUsed,
|
|
|
|
bool aVisual) const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aReturnFrame);
|
|
|
|
MOZ_ASSERT(aOffsetUsed);
|
|
|
|
|
|
|
|
*aReturnFrame = nullptr;
|
|
|
|
|
|
|
|
if (!mFrameSelection) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2014-08-06 09:19:27 +04:00
|
|
|
CaretAssociationHint hint = mFrameSelection->GetHint();
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2006-08-30 13:26:39 +04:00
|
|
|
if (aVisual) {
|
2014-11-20 13:45:22 +03:00
|
|
|
nsBidiLevel caretBidiLevel = mFrameSelection->GetCaretBidiLevel();
|
2005-11-16 12:37:01 +03:00
|
|
|
|
2014-08-06 09:19:24 +04:00
|
|
|
return nsCaret::GetCaretFrameForNodeOffset(mFrameSelection,
|
2017-08-23 13:09:07 +03:00
|
|
|
aContent, aOffset, hint,
|
|
|
|
caretBidiLevel, aReturnFrame,
|
|
|
|
aOffsetUsed);
|
2006-08-30 13:26:39 +04:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2017-08-23 13:09:07 +03:00
|
|
|
*aReturnFrame =
|
|
|
|
mFrameSelection->GetFrameForNodeOffset(aContent, aOffset,
|
|
|
|
hint, aOffsetUsed);
|
|
|
|
if (!*aReturnFrame) {
|
2006-08-30 13:26:39 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
2017-08-23 13:09:07 +03:00
|
|
|
}
|
2006-08-30 13:26:39 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2017-05-28 03:37:10 +03:00
|
|
|
void
|
|
|
|
Selection::SelectFramesForContent(nsIContent* aContent,
|
|
|
|
bool aSelected)
|
|
|
|
{
|
|
|
|
nsIFrame* frame = aContent->GetPrimaryFrame();
|
|
|
|
if (!frame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// The frame could be an SVG text frame, in which case we don't treat it
|
|
|
|
// as a text frame.
|
|
|
|
if (frame->IsTextFrame()) {
|
|
|
|
nsTextFrame* textFrame = static_cast<nsTextFrame*>(frame);
|
|
|
|
textFrame->SetSelectedRange(0, aContent->GetText()->GetLength(),
|
|
|
|
aSelected, mSelectionType);
|
|
|
|
} else {
|
|
|
|
frame->InvalidateFrameSubtree(); // frame continuations?
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
//select all content children of aContent
|
2009-04-06 21:17:37 +04:00
|
|
|
nsresult
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::SelectAllFramesForContent(nsIContentIterator* aInnerIter,
|
|
|
|
nsIContent* aContent,
|
|
|
|
bool aSelected)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2017-05-28 03:37:10 +03:00
|
|
|
// If aContent doesn't have children, we should avoid to use the content
|
|
|
|
// iterator for performance reason.
|
|
|
|
if (!aContent->HasChildren()) {
|
|
|
|
SelectFramesForContent(aContent, aSelected);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2017-05-28 03:37:10 +03:00
|
|
|
if (NS_WARN_IF(NS_FAILED(aInnerIter->Init(aContent)))) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2017-05-28 03:37:10 +03:00
|
|
|
for (; !aInnerIter->IsDone(); aInnerIter->Next()) {
|
|
|
|
nsINode* node = aInnerIter->GetCurrentNode();
|
|
|
|
MOZ_ASSERT(node);
|
2017-05-29 08:25:11 +03:00
|
|
|
nsIContent* innercontent = node->IsContent() ? node->AsContent() : nullptr;
|
2017-05-28 03:37:10 +03:00
|
|
|
SelectFramesForContent(innercontent, aSelected);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2017-05-28 03:37:10 +03:00
|
|
|
return NS_OK;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2012-06-28 15:29:56 +04:00
|
|
|
/**
|
2012-07-06 10:39:01 +04:00
|
|
|
* The idea of this helper method is to select or deselect "top to bottom",
|
2012-06-28 15:29:56 +04:00
|
|
|
* traversing through the frames
|
|
|
|
*/
|
2009-04-06 22:39:02 +04:00
|
|
|
nsresult
|
2017-05-29 06:27:09 +03:00
|
|
|
Selection::SelectFrames(nsPresContext* aPresContext, nsRange* aRange,
|
2012-07-06 10:39:01 +04:00
|
|
|
bool aSelect)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2011-12-24 08:08:23 +04:00
|
|
|
if (!mFrameSelection || !aPresContext || !aPresContext->GetPresShell()) {
|
2012-06-28 15:29:56 +04:00
|
|
|
// nothing to do
|
|
|
|
return NS_OK;
|
2011-12-24 08:08:23 +04:00
|
|
|
}
|
2017-05-29 08:25:11 +03:00
|
|
|
MOZ_ASSERT(aRange && aRange->IsPositioned());
|
2009-11-16 01:52:31 +03:00
|
|
|
|
2011-12-20 13:15:41 +04:00
|
|
|
if (mFrameSelection->GetTableCellSelection()) {
|
|
|
|
nsINode* node = aRange->GetCommonAncestor();
|
2012-06-28 15:29:56 +04:00
|
|
|
nsIFrame* frame = node->IsContent() ? node->AsContent()->GetPrimaryFrame()
|
2018-03-06 12:25:21 +03:00
|
|
|
: aPresContext->PresShell()->GetRootFrame();
|
2011-12-20 13:15:41 +04:00
|
|
|
if (frame) {
|
|
|
|
frame->InvalidateFrameSubtree();
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-12-11 17:24:49 +03:00
|
|
|
|
|
|
|
|
2012-06-28 15:29:56 +04:00
|
|
|
// Loop through the content iterator for each content node; for each text
|
|
|
|
// node, call SetSelected on it:
|
2017-07-11 14:53:04 +03:00
|
|
|
nsINode* startNode = aRange->GetStartContainer();
|
2017-05-28 03:37:10 +03:00
|
|
|
nsIContent* startContent =
|
2017-05-29 08:25:11 +03:00
|
|
|
startNode->IsContent() ? startNode->AsContent() : nullptr;
|
2017-05-28 03:37:10 +03:00
|
|
|
if (!startContent) {
|
2014-10-22 16:45:30 +04:00
|
|
|
// Don't warn, bug 1055722
|
2017-05-29 08:25:11 +03:00
|
|
|
// XXX The range can start from a document node and such range can be
|
|
|
|
// added to Selection with JS. Therefore, even in such cases,
|
|
|
|
// shouldn't we handle selection in the range?
|
2014-10-22 16:45:30 +04:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2000-03-31 11:17:22 +04:00
|
|
|
|
2012-06-28 15:29:56 +04:00
|
|
|
// We must call first one explicitly
|
2018-04-13 01:41:00 +03:00
|
|
|
bool isFirstContentTextNode = startContent->IsText();
|
2017-07-11 15:11:37 +03:00
|
|
|
nsINode* endNode = aRange->GetEndContainer();
|
2017-05-28 03:37:10 +03:00
|
|
|
if (isFirstContentTextNode) {
|
|
|
|
nsIFrame* frame = startContent->GetPrimaryFrame();
|
|
|
|
// The frame could be an SVG text frame, in which case we don't treat it
|
|
|
|
// as a text frame.
|
|
|
|
if (frame) {
|
|
|
|
if (frame->IsTextFrame()) {
|
|
|
|
nsTextFrame* textFrame = static_cast<nsTextFrame*>(frame);
|
|
|
|
uint32_t startOffset = aRange->StartOffset();
|
|
|
|
uint32_t endOffset;
|
|
|
|
if (endNode == startContent) {
|
|
|
|
endOffset = aRange->EndOffset();
|
|
|
|
} else {
|
|
|
|
endOffset = startContent->Length();
|
|
|
|
}
|
|
|
|
textFrame->SetSelectedRange(startOffset, endOffset, aSelect,
|
|
|
|
mSelectionType);
|
2012-06-28 15:29:56 +04:00
|
|
|
} else {
|
2017-05-28 03:37:10 +03:00
|
|
|
frame->InvalidateFrameSubtree();
|
2012-06-28 15:29:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2000-04-02 09:05:31 +04:00
|
|
|
|
2017-05-28 03:37:10 +03:00
|
|
|
// If the range is in a node and the node is a leaf node, we don't need to
|
|
|
|
// walk the subtree.
|
|
|
|
if (aRange->Collapsed() ||
|
|
|
|
(startNode == endNode && !startNode->HasChildren())) {
|
|
|
|
if (!isFirstContentTextNode) {
|
|
|
|
SelectFramesForContent(startContent, aSelect);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContentIterator> iter = NS_NewContentSubtreeIterator();
|
|
|
|
iter->Init(aRange);
|
2017-05-31 09:57:53 +03:00
|
|
|
if (isFirstContentTextNode && !iter->IsDone() &&
|
|
|
|
iter->GetCurrentNode() == startNode) {
|
2017-05-28 03:37:10 +03:00
|
|
|
iter->Next(); // first content has already been handled.
|
|
|
|
}
|
2012-06-28 15:29:56 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> inneriter = NS_NewContentIterator();
|
2017-05-28 03:37:10 +03:00
|
|
|
for (; !iter->IsDone(); iter->Next()) {
|
|
|
|
nsINode* node = iter->GetCurrentNode();
|
|
|
|
MOZ_ASSERT(node);
|
2017-05-29 08:25:11 +03:00
|
|
|
nsIContent* content = node->IsContent() ? node->AsContent() : nullptr;
|
2012-07-06 10:39:01 +04:00
|
|
|
SelectAllFramesForContent(inneriter, content, aSelect);
|
2012-06-28 15:29:56 +04:00
|
|
|
}
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2012-06-28 15:29:56 +04:00
|
|
|
// We must now do the last one if it is not the same as the first
|
2017-05-28 03:37:10 +03:00
|
|
|
if (endNode != startNode) {
|
|
|
|
nsIContent* endContent =
|
2017-05-29 08:25:11 +03:00
|
|
|
endNode->IsContent() ? endNode->AsContent() : nullptr;
|
|
|
|
// XXX The range can end at a document node and such range can be
|
|
|
|
// added to Selection with JS. Therefore, even in such cases,
|
|
|
|
// shouldn't we handle selection in the range?
|
2017-05-28 03:37:10 +03:00
|
|
|
if (NS_WARN_IF(!endContent)) {
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
2018-04-13 01:41:00 +03:00
|
|
|
if (endContent->IsText()) {
|
2017-05-28 03:37:10 +03:00
|
|
|
nsIFrame* frame = endContent->GetPrimaryFrame();
|
2012-06-28 15:29:56 +04:00
|
|
|
// The frame could be an SVG text frame, in which case we'll ignore it.
|
2017-04-30 18:30:08 +03:00
|
|
|
if (frame && frame->IsTextFrame()) {
|
2009-07-27 02:05:41 +04:00
|
|
|
nsTextFrame* textFrame = static_cast<nsTextFrame*>(frame);
|
2016-06-07 16:42:06 +03:00
|
|
|
textFrame->SetSelectedRange(0, aRange->EndOffset(), aSelect,
|
2016-06-11 05:06:37 +03:00
|
|
|
mSelectionType);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-28 15:29:56 +04:00
|
|
|
return NS_OK;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2011-12-20 13:15:41 +04:00
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
// Selection::LookUpSelection
|
2006-07-27 21:57:15 +04:00
|
|
|
//
|
|
|
|
// This function is called when a node wants to know where the selection is
|
|
|
|
// over itself.
|
|
|
|
//
|
|
|
|
// Usually, this is called when we already know there is a selection over
|
|
|
|
// the node in question, and we only need to find the boundaries of it on
|
|
|
|
// that node. This is when slowCheck is false--a strict test is not needed.
|
|
|
|
// Other times, the caller has no idea, and wants us to test everything,
|
|
|
|
// so we are supposed to determine whether there is a selection over the
|
|
|
|
// node at all.
|
|
|
|
//
|
|
|
|
// A previous version of this code used this flag to do less work when
|
|
|
|
// inclusion was already known (slowCheck=false). However, our tree
|
|
|
|
// structure allows us to quickly determine ranges overlapping the node,
|
|
|
|
// so we just ignore the slowCheck flag and do the full test every time.
|
|
|
|
//
|
|
|
|
// PERFORMANCE: a common case is that we are doing a fast check with exactly
|
|
|
|
// one range in the selection. In this case, this function is slower than
|
|
|
|
// brute force because of the overhead of checking the tree. We can optimize
|
|
|
|
// this case to make it faster by doing the same thing the previous version
|
|
|
|
// of this function did in the case of 1 range. This would also mean that
|
|
|
|
// the aSlowCheck flag would have meaning again.
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2017-01-20 18:28:02 +03:00
|
|
|
UniquePtr<SelectionDetails>
|
2012-08-22 19:56:38 +04:00
|
|
|
Selection::LookUpSelection(nsIContent* aContent, int32_t aContentOffset,
|
|
|
|
int32_t aContentLength,
|
2017-01-20 18:28:02 +03:00
|
|
|
UniquePtr<SelectionDetails> aDetailsHead,
|
2016-06-11 05:06:37 +03:00
|
|
|
SelectionType aSelectionType,
|
2016-06-07 16:42:06 +03:00
|
|
|
bool aSlowCheck)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2017-01-20 18:28:02 +03:00
|
|
|
if (!aContent) {
|
|
|
|
return aDetailsHead;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2006-07-27 21:57:15 +04:00
|
|
|
// it is common to have no ranges, to optimize that
|
2017-01-20 18:28:02 +03:00
|
|
|
if (mRanges.Length() == 0) {
|
|
|
|
return aDetailsHead;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2012-01-10 18:19:54 +04:00
|
|
|
nsTArray<nsRange*> overlappingRanges;
|
2017-01-20 18:28:02 +03:00
|
|
|
nsresult rv = GetRangesForIntervalArray(aContent, aContentOffset,
|
|
|
|
aContent, aContentOffset + aContentLength,
|
|
|
|
false,
|
|
|
|
&overlappingRanges);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return aDetailsHead;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (overlappingRanges.Length() == 0) {
|
|
|
|
return aDetailsHead;
|
|
|
|
}
|
|
|
|
|
|
|
|
UniquePtr<SelectionDetails> detailsHead = Move(aDetailsHead);
|
2006-07-27 21:57:15 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < overlappingRanges.Length(); i++) {
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange* range = overlappingRanges[i];
|
2017-07-11 14:53:04 +03:00
|
|
|
nsINode* startNode = range->GetStartContainer();
|
2017-07-11 15:11:37 +03:00
|
|
|
nsINode* endNode = range->GetEndContainer();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startOffset = range->StartOffset();
|
|
|
|
int32_t endOffset = range->EndOffset();
|
2006-07-27 21:57:15 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t start = -1, end = -1;
|
2009-04-06 21:17:37 +04:00
|
|
|
if (startNode == aContent && endNode == aContent) {
|
2006-07-27 21:57:15 +04:00
|
|
|
if (startOffset < (aContentOffset + aContentLength) &&
|
|
|
|
endOffset > aContentOffset) {
|
|
|
|
// this range is totally inside the requested content range
|
2013-01-15 16:22:03 +04:00
|
|
|
start = std::max(0, startOffset - aContentOffset);
|
|
|
|
end = std::min(aContentLength, endOffset - aContentOffset);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2006-07-27 21:57:15 +04:00
|
|
|
// otherwise, range is inside the requested node, but does not intersect
|
|
|
|
// the requested content range, so ignore it
|
2009-04-06 21:17:37 +04:00
|
|
|
} else if (startNode == aContent) {
|
2006-07-27 21:57:15 +04:00
|
|
|
if (startOffset < (aContentOffset + aContentLength)) {
|
|
|
|
// the beginning of the range is inside the requested node, but the
|
|
|
|
// end is outside, select everything from there to the end
|
2013-01-15 16:22:03 +04:00
|
|
|
start = std::max(0, startOffset - aContentOffset);
|
2006-07-27 21:57:15 +04:00
|
|
|
end = aContentLength;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2009-04-06 21:17:37 +04:00
|
|
|
} else if (endNode == aContent) {
|
2006-07-27 21:57:15 +04:00
|
|
|
if (endOffset > aContentOffset) {
|
|
|
|
// the end of the range is inside the requested node, but the beginning
|
|
|
|
// is outside, select everything from the beginning to there
|
|
|
|
start = 0;
|
2013-01-15 16:22:03 +04:00
|
|
|
end = std::min(aContentLength, endOffset - aContentOffset);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2006-07-27 21:57:15 +04:00
|
|
|
} else {
|
|
|
|
// this range does not begin or end in the requested node, but since
|
|
|
|
// GetRangesForInterval returned this range, we know it overlaps.
|
|
|
|
// Therefore, this node is enclosed in the range, and we select all
|
|
|
|
// of it.
|
|
|
|
start = 0;
|
|
|
|
end = aContentLength;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2006-07-27 21:57:15 +04:00
|
|
|
if (start < 0)
|
|
|
|
continue; // the ranges do not overlap the input range
|
|
|
|
|
2017-01-20 18:28:02 +03:00
|
|
|
auto newHead = MakeUnique<SelectionDetails>();
|
2006-07-27 21:57:15 +04:00
|
|
|
|
2017-01-20 18:28:02 +03:00
|
|
|
newHead->mNext = Move(detailsHead);
|
|
|
|
newHead->mStart = start;
|
|
|
|
newHead->mEnd = end;
|
|
|
|
newHead->mSelectionType = aSelectionType;
|
2009-04-23 07:27:12 +04:00
|
|
|
RangeData *rd = FindRangeData(range);
|
|
|
|
if (rd) {
|
2017-01-20 18:28:02 +03:00
|
|
|
newHead->mTextRangeStyle = rd->mTextRangeStyle;
|
2009-04-23 07:27:12 +04:00
|
|
|
}
|
2017-01-20 18:28:02 +03:00
|
|
|
detailsHead = Move(newHead);
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
2017-01-20 18:28:02 +03:00
|
|
|
return detailsHead;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::Repaint(nsPresContext* aPresContext)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t arrCount = (int32_t)mRanges.Length();
|
2000-03-22 02:58:02 +03:00
|
|
|
|
|
|
|
if (arrCount < 1)
|
|
|
|
return NS_OK;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t i;
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
for (i = 0; i < arrCount; i++)
|
|
|
|
{
|
2017-05-29 06:27:09 +03:00
|
|
|
nsresult rv = SelectFrames(aPresContext, mRanges[i].mRange, true);
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2002-12-11 03:02:49 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-04-20 05:03:40 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::GetCanCacheFrameOffset(bool* aCanCacheFrameOffset)
|
2017-07-06 15:00:35 +03:00
|
|
|
{
|
2003-04-20 05:03:40 +04:00
|
|
|
NS_ENSURE_ARG_POINTER(aCanCacheFrameOffset);
|
|
|
|
|
|
|
|
if (mCachedOffsetForFrame)
|
|
|
|
*aCanCacheFrameOffset = mCachedOffsetForFrame->mCanCacheFrameOffset;
|
|
|
|
else
|
2011-10-17 18:59:28 +04:00
|
|
|
*aCanCacheFrameOffset = false;
|
2003-04-20 05:03:40 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::SetCanCacheFrameOffset(bool aCanCacheFrameOffset)
|
2003-04-20 05:03:40 +04:00
|
|
|
{
|
|
|
|
if (!mCachedOffsetForFrame) {
|
|
|
|
mCachedOffsetForFrame = new CachedOffsetForFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCachedOffsetForFrame->mCanCacheFrameOffset = aCanCacheFrameOffset;
|
|
|
|
|
2003-06-11 07:29:59 +04:00
|
|
|
// clean up cached frame when turn off cache
|
|
|
|
// fix bug 207936
|
|
|
|
if (!aCanCacheFrameOffset) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mCachedOffsetForFrame->mLastCaretFrame = nullptr;
|
2003-06-11 07:29:59 +04:00
|
|
|
}
|
|
|
|
|
2003-04-20 05:03:40 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
Selection::GetCachedFrameOffset(nsIFrame* aFrame, int32_t inOffset,
|
2012-06-08 18:20:55 +04:00
|
|
|
nsPoint& aPoint)
|
2003-04-20 05:03:40 +04:00
|
|
|
{
|
|
|
|
if (!mCachedOffsetForFrame) {
|
|
|
|
mCachedOffsetForFrame = new CachedOffsetForFrame;
|
|
|
|
}
|
|
|
|
|
2006-04-28 08:33:41 +04:00
|
|
|
nsresult rv = NS_OK;
|
2003-06-11 07:29:59 +04:00
|
|
|
if (mCachedOffsetForFrame->mCanCacheFrameOffset &&
|
|
|
|
mCachedOffsetForFrame->mLastCaretFrame &&
|
2003-04-20 05:03:40 +04:00
|
|
|
(aFrame == mCachedOffsetForFrame->mLastCaretFrame) &&
|
|
|
|
(inOffset == mCachedOffsetForFrame->mLastContentOffset))
|
|
|
|
{
|
|
|
|
// get cached frame offset
|
|
|
|
aPoint = mCachedOffsetForFrame->mCachedFrameOffset;
|
2017-07-06 15:00:35 +03:00
|
|
|
}
|
2003-04-20 05:03:40 +04:00
|
|
|
else
|
|
|
|
{
|
2006-04-25 01:18:44 +04:00
|
|
|
// Recalculate frame offset and cache it. Don't cache a frame offset if
|
|
|
|
// GetPointFromOffset fails, though.
|
2009-09-10 09:16:18 +04:00
|
|
|
rv = aFrame->GetPointFromOffset(inOffset, &aPoint);
|
2006-04-25 01:18:44 +04:00
|
|
|
if (NS_SUCCEEDED(rv) && mCachedOffsetForFrame->mCanCacheFrameOffset) {
|
2003-04-20 05:03:40 +04:00
|
|
|
mCachedOffsetForFrame->mCachedFrameOffset = aPoint;
|
|
|
|
mCachedOffsetForFrame->mLastCaretFrame = aFrame;
|
2017-07-06 15:00:35 +03:00
|
|
|
mCachedOffsetForFrame->mLastContentOffset = inOffset;
|
2003-04-20 05:03:40 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-04-28 08:33:41 +04:00
|
|
|
return rv;
|
2003-04-20 05:03:40 +04:00
|
|
|
}
|
|
|
|
|
2014-09-10 03:27:56 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Selection::GetAncestorLimiter(nsIContent** aContent)
|
|
|
|
{
|
|
|
|
if (mFrameSelection) {
|
|
|
|
nsCOMPtr<nsIContent> c = mFrameSelection->GetAncestorLimiter();
|
|
|
|
c.forget(aContent);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2007-06-28 06:48:16 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::SetAncestorLimiter(nsIContent* aContent)
|
2007-06-28 06:48:16 +04:00
|
|
|
{
|
2017-04-06 07:11:52 +03:00
|
|
|
if (mFrameSelection) {
|
|
|
|
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
|
|
|
|
frameSelection->SetAncestorLimiter(aContent);
|
|
|
|
}
|
2007-06-28 06:48:16 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2009-04-23 07:27:12 +04:00
|
|
|
RangeData*
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::FindRangeData(nsIDOMRange* aRange)
|
2009-04-23 07:27:12 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(aRange, nullptr);
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < mRanges.Length(); i++) {
|
2009-04-23 07:27:12 +04:00
|
|
|
if (mRanges[i].mRange == aRange)
|
|
|
|
return &mRanges[i];
|
|
|
|
}
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2009-04-23 07:27:12 +04:00
|
|
|
}
|
|
|
|
|
2017-08-24 10:56:12 +03:00
|
|
|
nsresult
|
|
|
|
Selection::SetTextRangeStyle(nsRange* aRange,
|
2013-10-01 11:22:59 +04:00
|
|
|
const TextRangeStyle& aTextRangeStyle)
|
2009-04-23 07:27:12 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRange);
|
|
|
|
RangeData *rd = FindRangeData(aRange);
|
|
|
|
if (rd) {
|
|
|
|
rd->mTextRangeStyle = aTextRangeStyle;
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
nsresult
|
2017-11-15 13:26:10 +03:00
|
|
|
Selection::StartAutoScrollTimer(nsIFrame* aFrame, const nsPoint& aPoint,
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t aDelay)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2009-09-10 09:16:18 +04:00
|
|
|
NS_PRECONDITION(aFrame, "Need a frame");
|
2006-06-16 05:54:31 +04:00
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
nsresult result;
|
2017-03-18 19:40:30 +03:00
|
|
|
if (!mFrameSelection) {
|
2000-05-11 08:25:43 +04:00
|
|
|
return NS_OK;//nothing to do
|
2017-03-18 19:40:30 +03:00
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2017-03-18 19:40:30 +03:00
|
|
|
if (!mAutoScrollTimer) {
|
2006-06-16 05:54:31 +04:00
|
|
|
mAutoScrollTimer = new nsAutoScrollTimer();
|
2000-03-22 02:58:02 +03:00
|
|
|
|
|
|
|
result = mAutoScrollTimer->Init(mFrameSelection, this);
|
|
|
|
|
2017-03-18 19:40:30 +03:00
|
|
|
if (NS_FAILED(result)) {
|
2000-03-22 02:58:02 +03:00
|
|
|
return result;
|
2017-03-18 19:40:30 +03:00
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
result = mAutoScrollTimer->SetDelay(aDelay);
|
|
|
|
|
2017-03-18 19:40:30 +03:00
|
|
|
if (NS_FAILED(result)) {
|
2000-03-22 02:58:02 +03:00
|
|
|
return result;
|
2017-03-18 19:40:30 +03:00
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2009-09-10 09:16:18 +04:00
|
|
|
return DoAutoScroll(aFrame, aPoint);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::StopAutoScrollTimer()
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2016-04-26 15:43:55 +03:00
|
|
|
if (mAutoScrollTimer) {
|
2000-03-22 02:58:02 +03:00
|
|
|
return mAutoScrollTimer->Stop();
|
2016-04-26 15:43:55 +03:00
|
|
|
}
|
2017-03-18 19:40:30 +03:00
|
|
|
return NS_OK;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2017-11-16 05:32:22 +03:00
|
|
|
Selection::DoAutoScroll(nsIFrame* aFrame, nsPoint aPoint)
|
2000-08-30 23:42:47 +04:00
|
|
|
{
|
2009-09-10 09:16:18 +04:00
|
|
|
NS_PRECONDITION(aFrame, "Need a frame");
|
2000-08-30 23:42:47 +04:00
|
|
|
|
2017-03-18 19:40:30 +03:00
|
|
|
if (mAutoScrollTimer) {
|
2011-12-20 07:46:39 +04:00
|
|
|
(void)mAutoScrollTimer->Stop();
|
2017-03-18 19:40:30 +03:00
|
|
|
}
|
2000-08-30 23:42:47 +04:00
|
|
|
|
2009-09-10 09:16:18 +04:00
|
|
|
nsPresContext* presContext = aFrame->PresContext();
|
2016-04-26 15:43:55 +03:00
|
|
|
nsCOMPtr<nsIPresShell> shell = presContext->PresShell();
|
2010-01-26 16:10:12 +03:00
|
|
|
nsRootPresContext* rootPC = presContext->GetRootPresContext();
|
|
|
|
if (!rootPC)
|
|
|
|
return NS_OK;
|
2018-03-06 12:25:21 +03:00
|
|
|
nsIFrame* rootmostFrame = rootPC->PresShell()->GetRootFrame();
|
2017-03-01 20:03:14 +03:00
|
|
|
AutoWeakFrame weakRootFrame(rootmostFrame);
|
|
|
|
AutoWeakFrame weakFrame(aFrame);
|
2010-07-19 06:23:48 +04:00
|
|
|
// Get the point relative to the root most frame because the scroll we are
|
|
|
|
// about to do will change the coordinates of aFrame.
|
|
|
|
nsPoint globalPoint = aPoint + aFrame->GetOffsetToCrossDoc(rootmostFrame);
|
2000-08-30 23:42:47 +04:00
|
|
|
|
2016-04-26 15:43:55 +03:00
|
|
|
bool done = false;
|
|
|
|
bool didScroll;
|
|
|
|
while (true) {
|
|
|
|
didScroll = shell->ScrollFrameRectIntoView(
|
|
|
|
aFrame, nsRect(aPoint, nsSize(0, 0)),
|
|
|
|
nsIPresShell::ScrollAxis(), nsIPresShell::ScrollAxis(),
|
|
|
|
0);
|
|
|
|
if (!weakFrame || !weakRootFrame) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
if (!didScroll && !done) {
|
|
|
|
// If aPoint is at the screen edge then try to scroll anyway, once.
|
|
|
|
RefPtr<nsDeviceContext> dx = shell->GetViewManager()->GetDeviceContext();
|
|
|
|
nsRect screen;
|
|
|
|
dx->GetRect(screen);
|
|
|
|
nsPoint screenPoint = globalPoint +
|
|
|
|
rootmostFrame->GetScreenRectInAppUnits().TopLeft();
|
|
|
|
nscoord onePx = nsPresContext::AppUnitsPerCSSPixel();
|
|
|
|
nscoord scrollAmount = 10 * onePx;
|
2018-02-19 23:15:23 +03:00
|
|
|
if (std::abs(screen.x - screenPoint.x) <= onePx) {
|
2016-04-26 15:43:55 +03:00
|
|
|
aPoint.x -= scrollAmount;
|
|
|
|
} else if (std::abs(screen.XMost() - screenPoint.x) <= onePx) {
|
|
|
|
aPoint.x += scrollAmount;
|
2018-02-19 23:15:23 +03:00
|
|
|
} else if (std::abs(screen.y - screenPoint.y) <= onePx) {
|
2016-04-26 15:43:55 +03:00
|
|
|
aPoint.y -= scrollAmount;
|
|
|
|
} else if (std::abs(screen.YMost() - screenPoint.y) <= onePx) {
|
|
|
|
aPoint.y += scrollAmount;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
done = true;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2000-08-30 23:42:47 +04:00
|
|
|
|
2004-11-10 10:27:11 +03:00
|
|
|
// Start the AutoScroll timer if necessary.
|
2016-04-26 15:43:55 +03:00
|
|
|
if (didScroll && mAutoScrollTimer) {
|
2009-09-10 09:16:18 +04:00
|
|
|
nsPoint presContextPoint = globalPoint -
|
2018-03-06 12:25:21 +03:00
|
|
|
shell->GetRootFrame()->GetOffsetToCrossDoc(rootmostFrame);
|
2011-08-03 07:39:02 +04:00
|
|
|
mAutoScrollTimer->Start(presContext, presContextPoint);
|
2000-08-30 23:42:47 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
void
|
|
|
|
Selection::RemoveAllRanges(ErrorResult& aRv)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2000-05-11 08:25:43 +04:00
|
|
|
if (!mFrameSelection)
|
2013-12-17 18:12:33 +04:00
|
|
|
return; // nothing to do
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPresContext> presContext = GetPresContext();
|
2003-03-12 06:22:11 +03:00
|
|
|
nsresult result = Clear(presContext);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
aRv.Throw(result);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-03-12 06:22:11 +03:00
|
|
|
// Turn off signal for table selection
|
2017-04-06 07:11:52 +03:00
|
|
|
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
|
|
|
|
frameSelection->ClearTableCellSelection();
|
2000-06-03 03:05:06 +04:00
|
|
|
|
2017-03-14 04:36:21 +03:00
|
|
|
// Be aware, this instance may be destroyed after this call.
|
|
|
|
// XXX Why doesn't this call Selection::NotifySelectionListener() directly?
|
2017-04-06 07:11:52 +03:00
|
|
|
result = frameSelection->NotifySelectionListeners(GetType());
|
2017-03-14 04:36:21 +03:00
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
// Also need to notify the frames!
|
2004-02-21 00:38:31 +03:00
|
|
|
// PresShell::CharacterDataChanged should do that on DocumentChanged
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
aRv.Throw(result);
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
Bug 1393816 - part1: Cache a range until new range is created in Selection r=smaug
When setting value of <input type="text">, nsTextEditorState removes all
ranges of normal selection first. Then, TextEditor sets the value. Finally,
TextEditor collapses the selection at the end of the text.
In bug 1386471, we got that there are some problems to remove the call of
Selection::RemoveAllRanges() in nsTextEditorState. Therefore, we need another
approach to improve Selection::Collapse().
The approach of this patch is, when removing all ranges from normal selection,
Selection can cache an nsRange instance if there is an instance which is not
referenced from other than the Selection (i.e., it'll be removed when
Selection::Clear() is called). Then, Selection::Collapse() can reuse it. With
this fix, Selection::Collapse() can reduce allocation cost and may reduce some
other cost like adding it to mutation observer.
However, keeping nsRange instance may cause increasing mutation observer's cost
since nsRange will be adjusted its start node/offset and end node/offset with
mutation observer to guarantee that the range is always valid. So, we can
cache such range only when the caller (or its callee) will set selection range
later. Therefore, this patch adds Selection::RemoveAllRangesTemporarily()
and make only nsTextEditorState::SetValue() and
ContentEventHandler::OnSelectionEvent() use it.
MozReview-Commit-ID: FjWrbz4S1ld
--HG--
extra : rebase_source : 83677640525e0b1a84bdd7fce63ff4704b9cc22b
2017-08-25 13:21:39 +03:00
|
|
|
nsresult
|
|
|
|
Selection::RemoveAllRangesTemporarily()
|
|
|
|
{
|
|
|
|
if (!mCachedRange) {
|
|
|
|
// Look for a range which isn't referred by other than this instance.
|
|
|
|
// If there is, it'll be released by calling Clear(). So, we can reuse it
|
|
|
|
// when we need to create a range.
|
|
|
|
for (auto& rangeData : mRanges) {
|
|
|
|
auto& range = rangeData.mRange;
|
|
|
|
if (range->GetRefCount() == 1 ||
|
|
|
|
(range->GetRefCount() == 2 && range == mAnchorFocusRange)) {
|
|
|
|
mCachedRange = range;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Then, remove all ranges.
|
|
|
|
ErrorResult result;
|
|
|
|
RemoveAllRanges(result);
|
|
|
|
if (result.Failed()) {
|
|
|
|
mCachedRange = nullptr;
|
|
|
|
}
|
|
|
|
return result.StealNSResult();
|
|
|
|
}
|
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
|
|
|
Selection::AddRangeJS(nsRange& aRange, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AutoRestore<bool> calledFromJSRestorer(mCalledByJS);
|
|
|
|
mCalledByJS = true;
|
|
|
|
AddRange(aRange, aRv);
|
|
|
|
}
|
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
void
|
|
|
|
Selection::AddRange(nsRange& aRange, ErrorResult& aRv)
|
2017-02-25 13:33:34 +03:00
|
|
|
{
|
|
|
|
return AddRangeInternal(aRange, GetParentObject(), aRv);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Selection::AddRangeInternal(nsRange& aRange, nsIDocument* aDocument,
|
|
|
|
ErrorResult& aRv)
|
2017-02-25 02:26:18 +03:00
|
|
|
{
|
2017-02-25 13:33:34 +03:00
|
|
|
nsINode* rangeRoot = aRange.GetRoot();
|
2017-02-25 13:33:34 +03:00
|
|
|
if (aDocument != rangeRoot && (!rangeRoot ||
|
|
|
|
aDocument != rangeRoot->GetComposedDoc())) {
|
2017-02-25 13:33:34 +03:00
|
|
|
// http://w3c.github.io/selection-api/#dom-selection-addrange
|
|
|
|
// "... if the root of the range's boundary points are the document
|
|
|
|
// associated with context object. Otherwise, this method must do nothing."
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
Bug 1393816 - part1: Cache a range until new range is created in Selection r=smaug
When setting value of <input type="text">, nsTextEditorState removes all
ranges of normal selection first. Then, TextEditor sets the value. Finally,
TextEditor collapses the selection at the end of the text.
In bug 1386471, we got that there are some problems to remove the call of
Selection::RemoveAllRanges() in nsTextEditorState. Therefore, we need another
approach to improve Selection::Collapse().
The approach of this patch is, when removing all ranges from normal selection,
Selection can cache an nsRange instance if there is an instance which is not
referenced from other than the Selection (i.e., it'll be removed when
Selection::Clear() is called). Then, Selection::Collapse() can reuse it. With
this fix, Selection::Collapse() can reduce allocation cost and may reduce some
other cost like adding it to mutation observer.
However, keeping nsRange instance may cause increasing mutation observer's cost
since nsRange will be adjusted its start node/offset and end node/offset with
mutation observer to guarantee that the range is always valid. So, we can
cache such range only when the caller (or its callee) will set selection range
later. Therefore, this patch adds Selection::RemoveAllRangesTemporarily()
and make only nsTextEditorState::SetValue() and
ContentEventHandler::OnSelectionEvent() use it.
MozReview-Commit-ID: FjWrbz4S1ld
--HG--
extra : rebase_source : 83677640525e0b1a84bdd7fce63ff4704b9cc22b
2017-08-25 13:21:39 +03:00
|
|
|
// If a range is being added, we don't need cached range because Collapse()
|
|
|
|
// won't use it.
|
|
|
|
mCachedRange = nullptr;
|
|
|
|
|
2017-07-18 10:54:31 +03:00
|
|
|
// AddTableCellRange might flush frame.
|
|
|
|
RefPtr<Selection> kungFuDeathGrip(this);
|
|
|
|
|
2000-12-21 01:13:54 +03:00
|
|
|
// This inserts a table cell range in proper document order
|
2006-04-18 02:06:25 +04:00
|
|
|
// and returns NS_OK if range doesn't contain just one table cell
|
2011-09-29 10:19:26 +04:00
|
|
|
bool didAddRange;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t rangeIndex;
|
2017-05-29 07:31:11 +03:00
|
|
|
nsresult result = AddTableCellRange(&aRange, &didAddRange, &rangeIndex);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
aRv.Throw(result);
|
|
|
|
return;
|
|
|
|
}
|
2000-12-21 01:13:54 +03:00
|
|
|
|
2014-09-10 21:07:36 +04:00
|
|
|
if (!didAddRange) {
|
2013-12-17 18:12:33 +04:00
|
|
|
result = AddItem(&aRange, &rangeIndex);
|
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
aRv.Throw(result);
|
|
|
|
return;
|
|
|
|
}
|
2000-12-21 01:13:54 +03:00
|
|
|
}
|
|
|
|
|
2014-09-10 21:07:36 +04:00
|
|
|
if (rangeIndex < 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-05-30 07:26:09 +03:00
|
|
|
SetAnchorFocusRange(rangeIndex);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2006-05-11 20:28:01 +04:00
|
|
|
// Make sure the caret appears on the next line, if at a newline
|
2016-06-09 12:35:22 +03:00
|
|
|
if (mSelectionType == SelectionType::eNormal) {
|
2011-10-17 18:59:28 +04:00
|
|
|
SetInterlinePosition(true);
|
2016-06-07 16:42:06 +03:00
|
|
|
}
|
2006-05-11 20:28:01 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPresContext> presContext = GetPresContext();
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, &aRange, true);
|
2000-12-21 01:13:54 +03:00
|
|
|
|
2000-05-11 08:25:43 +04:00
|
|
|
if (!mFrameSelection)
|
2013-12-17 18:12:33 +04:00
|
|
|
return;//nothing to do
|
2000-12-21 01:13:54 +03:00
|
|
|
|
2017-03-14 04:36:21 +03:00
|
|
|
// Be aware, this instance may be destroyed after this call.
|
|
|
|
// XXX Why doesn't this call Selection::NotifySelectionListener() directly?
|
2017-04-06 07:11:52 +03:00
|
|
|
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
|
|
|
|
result = frameSelection->NotifySelectionListeners(GetType());
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
aRv.Throw(result);
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
// Selection::RemoveRange
|
2006-07-31 22:25:22 +04:00
|
|
|
//
|
|
|
|
// Removes the given range from the selection. The tricky part is updating
|
|
|
|
// the flags on the frames that indicate whether they have a selection or
|
|
|
|
// not. There could be several selection ranges on the frame, and clearing
|
|
|
|
// the bit would cause the selection to not be drawn, even when there is
|
|
|
|
// another range on the frame (bug 346185).
|
|
|
|
//
|
|
|
|
// We therefore find any ranges that intersect the same nodes as the range
|
|
|
|
// being removed, and cause them to set the selected bits back on their
|
|
|
|
// selected frames after we've cleared the bit from ours.
|
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
void
|
|
|
|
Selection::RemoveRange(nsRange& aRange, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsresult rv = RemoveItem(&aRange);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-11 14:53:04 +03:00
|
|
|
nsINode* beginNode = aRange.GetStartContainer();
|
2017-07-11 15:11:37 +03:00
|
|
|
nsINode* endNode = aRange.GetEndContainer();
|
2010-10-21 04:24:15 +04:00
|
|
|
|
|
|
|
if (!beginNode || !endNode) {
|
|
|
|
// Detached range; nothing else to do here.
|
2013-12-17 18:12:33 +04:00
|
|
|
return;
|
2010-10-21 04:24:15 +04:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2006-07-31 22:25:22 +04:00
|
|
|
// find out the length of the end node, so we can select all of it
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t beginOffset, endOffset;
|
2018-04-13 01:41:00 +03:00
|
|
|
if (endNode->IsText()) {
|
2006-07-31 22:25:22 +04:00
|
|
|
// Get the length of the text. We can't just use the offset because
|
|
|
|
// another range could be touching this text node but not intersect our
|
|
|
|
// range.
|
|
|
|
beginOffset = 0;
|
2018-04-13 01:41:00 +03:00
|
|
|
endOffset = endNode->AsText()->TextLength();
|
2006-07-31 22:25:22 +04:00
|
|
|
} else {
|
|
|
|
// For non-text nodes, the given offsets should be sufficient.
|
2013-12-17 18:12:33 +04:00
|
|
|
beginOffset = aRange.StartOffset();
|
|
|
|
endOffset = aRange.EndOffset();
|
2006-07-31 22:25:22 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// clear the selected bit from the removed range's frames
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPresContext> presContext = GetPresContext();
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, &aRange, false);
|
2006-07-31 22:25:22 +04:00
|
|
|
|
|
|
|
// add back the selected bit for each range touching our nodes
|
2012-01-10 18:19:54 +04:00
|
|
|
nsTArray<nsRange*> affectedRanges;
|
|
|
|
rv = GetRangesForIntervalArray(beginNode, beginOffset,
|
|
|
|
endNode, endOffset,
|
|
|
|
true, &affectedRanges);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < affectedRanges.Length(); i++) {
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, affectedRanges[i], true);
|
2009-04-06 21:17:37 +04:00
|
|
|
}
|
2006-07-31 22:25:22 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t cnt = mRanges.Length();
|
2013-12-17 18:12:33 +04:00
|
|
|
if (&aRange == mAnchorFocusRange) {
|
2008-10-08 16:53:14 +04:00
|
|
|
// Reset anchor to LAST range or clear it if there are no ranges.
|
2017-05-30 07:26:09 +03:00
|
|
|
SetAnchorFocusRange(cnt - 1);
|
2008-10-08 16:53:14 +04:00
|
|
|
|
|
|
|
// When the selection is user-created it makes sense to scroll the range
|
|
|
|
// into view. The spell-check selection, however, is created and destroyed
|
|
|
|
// in the background. We don't want to scroll in this case or the view
|
|
|
|
// might appear to be moving randomly (bug 337871).
|
2016-06-09 12:37:36 +03:00
|
|
|
if (mSelectionType != SelectionType::eSpellCheck && cnt > 0) {
|
2012-01-12 05:19:19 +04:00
|
|
|
ScrollIntoView(nsISelectionController::SELECTION_FOCUS_REGION);
|
2016-06-07 16:42:06 +03:00
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2008-10-08 16:53:14 +04:00
|
|
|
|
2000-05-11 08:25:43 +04:00
|
|
|
if (!mFrameSelection)
|
2013-12-17 18:12:33 +04:00
|
|
|
return;//nothing to do
|
2017-03-14 04:36:21 +03:00
|
|
|
|
|
|
|
// Be aware, this instance may be destroyed after this call.
|
|
|
|
// XXX Why doesn't this call Selection::NotifySelectionListener() directly?
|
2017-04-06 07:11:52 +03:00
|
|
|
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
|
|
|
|
rv = frameSelection->NotifySelectionListeners(GetType());
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-03-26 02:44:46 +03:00
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
/*
|
|
|
|
* Collapse sets the whole selection to be one point.
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2017-07-11 17:33:04 +03:00
|
|
|
Selection::Collapse(nsIDOMNode* aContainer, int32_t aOffset)
|
2009-04-06 21:17:37 +04:00
|
|
|
{
|
2017-07-11 17:52:39 +03:00
|
|
|
nsCOMPtr<nsINode> container = do_QueryInterface(aContainer);
|
2017-11-07 09:29:15 +03:00
|
|
|
return Collapse(RawRangeBoundary(container, aOffset));
|
2009-04-06 21:17:37 +04:00
|
|
|
}
|
|
|
|
|
2012-02-01 14:54:22 +04:00
|
|
|
NS_IMETHODIMP
|
2017-07-11 17:33:04 +03:00
|
|
|
Selection::CollapseNative(nsINode* aContainer, int32_t aOffset)
|
2012-02-01 14:54:22 +04:00
|
|
|
{
|
2017-11-07 09:29:15 +03:00
|
|
|
return Collapse(RawRangeBoundary(aContainer, aOffset));
|
2012-02-01 14:54:22 +04:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
2017-07-11 17:33:04 +03:00
|
|
|
Selection::CollapseJS(nsINode* aContainer, uint32_t aOffset, ErrorResult& aRv)
|
2017-03-10 10:55:12 +03:00
|
|
|
{
|
|
|
|
AutoRestore<bool> calledFromJSRestorer(mCalledByJS);
|
|
|
|
mCalledByJS = true;
|
2017-07-11 17:33:04 +03:00
|
|
|
if (!aContainer) {
|
2017-04-25 14:55:31 +03:00
|
|
|
RemoveAllRanges(aRv);
|
|
|
|
return;
|
|
|
|
}
|
2017-11-07 09:29:15 +03:00
|
|
|
Collapse(RawRangeBoundary(aContainer, aOffset), aRv);
|
2013-12-17 18:12:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-11-07 09:29:15 +03:00
|
|
|
Selection::Collapse(const RawRangeBoundary& aPoint, ErrorResult& aRv)
|
2013-12-17 18:12:33 +04:00
|
|
|
{
|
|
|
|
if (!mFrameSelection) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED); // Can't do selection
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-07 09:29:15 +03:00
|
|
|
if (!aPoint.IsSet()) {
|
|
|
|
aRv.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-01-30 07:10:53 +03:00
|
|
|
if (aPoint.Container()->NodeType() == nsINode::DOCUMENT_TYPE_NODE) {
|
2017-08-10 15:02:08 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-07 09:29:15 +03:00
|
|
|
// RawRangeBoundary::IsSetAndValid() checks if the point actually refers
|
|
|
|
// a child of the container when IsSet() is true. If its offset hasn't been
|
|
|
|
// computed yet, this just checks it with its mRef. So, we can avoid
|
|
|
|
// computing offset here.
|
|
|
|
if (!aPoint.IsSetAndValid()) {
|
2017-08-10 15:02:08 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-11-07 09:29:15 +03:00
|
|
|
if (!HasSameRoot(*aPoint.Container())) {
|
2017-08-10 15:02:08 +03:00
|
|
|
// Return with no error
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-04-06 07:11:52 +03:00
|
|
|
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
|
|
|
|
frameSelection->InvalidateDesiredPos();
|
2017-11-07 09:29:15 +03:00
|
|
|
if (!IsValidSelectionPoint(frameSelection, aPoint.Container())) {
|
2013-12-17 18:12:33 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
nsresult result;
|
2013-08-29 18:14:15 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPresContext> presContext = GetPresContext();
|
2017-11-07 09:29:15 +03:00
|
|
|
if (!presContext ||
|
|
|
|
presContext->Document() != aPoint.Container()->OwnerDoc()) {
|
2013-12-17 18:12:33 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2013-08-29 18:14:15 +04:00
|
|
|
|
2017-05-25 12:04:55 +03:00
|
|
|
// Cache current range is if there is because it may be reusable.
|
|
|
|
RefPtr<nsRange> oldRange = !mRanges.IsEmpty() ? mRanges[0].mRange : nullptr;
|
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
// Delete all of the current ranges
|
|
|
|
Clear(presContext);
|
|
|
|
|
2003-03-12 06:22:11 +03:00
|
|
|
// Turn off signal for table selection
|
2017-04-06 07:11:52 +03:00
|
|
|
frameSelection->ClearTableCellSelection();
|
2000-06-03 03:05:06 +04:00
|
|
|
|
2016-03-17 19:27:56 +03:00
|
|
|
// Hack to display the caret on the right line (bug 1237236).
|
2017-04-06 07:11:52 +03:00
|
|
|
if (frameSelection->GetHint() != CARET_ASSOCIATE_AFTER &&
|
2017-11-07 09:29:15 +03:00
|
|
|
aPoint.Container()->IsContent()) {
|
2016-04-01 03:08:15 +03:00
|
|
|
int32_t frameOffset;
|
|
|
|
nsTextFrame* f =
|
2017-11-07 09:29:15 +03:00
|
|
|
do_QueryFrame(nsCaret::GetFrameAndOffset(this, aPoint.Container(),
|
|
|
|
aPoint.Offset(), &frameOffset));
|
2016-03-17 19:27:56 +03:00
|
|
|
if (f && f->IsAtEndOfLine() && f->HasSignificantTerminalNewline()) {
|
2017-11-07 09:29:15 +03:00
|
|
|
// RawRangeBounary::Offset() causes computing offset if it's not been
|
|
|
|
// done yet. However, it's called only when the container is a text
|
|
|
|
// node. In such case, offset has always been set since it cannot have
|
|
|
|
// any children. So, this doesn't cause computing offset with expensive
|
2018-01-23 16:30:18 +03:00
|
|
|
// method, nsINode::ComputeIndexOf().
|
2017-11-07 09:29:15 +03:00
|
|
|
if ((aPoint.Container()->AsContent() == f->GetContent() &&
|
|
|
|
f->GetContentEnd() == static_cast<int32_t>(aPoint.Offset())) ||
|
|
|
|
(aPoint.Container() == f->GetContent()->GetParentNode() &&
|
|
|
|
f->GetContent() == aPoint.GetPreviousSiblingOfChildAtOffset())) {
|
2017-04-06 07:11:52 +03:00
|
|
|
frameSelection->SetHint(CARET_ASSOCIATE_AFTER);
|
2016-03-17 19:27:56 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-25 12:04:55 +03:00
|
|
|
RefPtr<nsRange> range;
|
|
|
|
// If the old range isn't referred by anybody other than this method,
|
|
|
|
// we should reuse it for reducing the recreation cost.
|
|
|
|
if (oldRange && oldRange->GetRefCount() == 1) {
|
Bug 1393816 - part1: Cache a range until new range is created in Selection r=smaug
When setting value of <input type="text">, nsTextEditorState removes all
ranges of normal selection first. Then, TextEditor sets the value. Finally,
TextEditor collapses the selection at the end of the text.
In bug 1386471, we got that there are some problems to remove the call of
Selection::RemoveAllRanges() in nsTextEditorState. Therefore, we need another
approach to improve Selection::Collapse().
The approach of this patch is, when removing all ranges from normal selection,
Selection can cache an nsRange instance if there is an instance which is not
referenced from other than the Selection (i.e., it'll be removed when
Selection::Clear() is called). Then, Selection::Collapse() can reuse it. With
this fix, Selection::Collapse() can reduce allocation cost and may reduce some
other cost like adding it to mutation observer.
However, keeping nsRange instance may cause increasing mutation observer's cost
since nsRange will be adjusted its start node/offset and end node/offset with
mutation observer to guarantee that the range is always valid. So, we can
cache such range only when the caller (or its callee) will set selection range
later. Therefore, this patch adds Selection::RemoveAllRangesTemporarily()
and make only nsTextEditorState::SetValue() and
ContentEventHandler::OnSelectionEvent() use it.
MozReview-Commit-ID: FjWrbz4S1ld
--HG--
extra : rebase_source : 83677640525e0b1a84bdd7fce63ff4704b9cc22b
2017-08-25 13:21:39 +03:00
|
|
|
range = Move(oldRange);
|
|
|
|
} else if (mCachedRange) {
|
|
|
|
range = Move(mCachedRange);
|
2017-05-25 12:04:55 +03:00
|
|
|
} else {
|
2017-11-07 09:29:15 +03:00
|
|
|
range = new nsRange(aPoint.Container());
|
2017-05-25 12:04:55 +03:00
|
|
|
}
|
2017-11-07 09:29:15 +03:00
|
|
|
result = range->CollapseTo(aPoint);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
aRv.Throw(result);
|
|
|
|
return;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
|
|
|
#ifdef DEBUG_SELECTION
|
2017-11-07 09:29:15 +03:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aPoint.Container());
|
|
|
|
nsCOMPtr<nsIDocument> doc = do_QueryInterface(aPoint.Container());
|
2017-07-11 17:52:39 +03:00
|
|
|
printf ("Sel. Collapse to %p %s %d\n", container.get(),
|
2015-03-03 14:09:00 +03:00
|
|
|
content ? nsAtomCString(content->NodeInfo()->NameAtom()).get()
|
2013-12-17 18:12:33 +04:00
|
|
|
: (doc ? "DOCUMENT" : "???"),
|
2017-11-07 09:29:15 +03:00
|
|
|
aPoint.Offset());
|
2000-03-22 02:58:02 +03:00
|
|
|
#endif
|
|
|
|
|
2012-09-17 17:54:01 +04:00
|
|
|
int32_t rangeIndex = -1;
|
|
|
|
result = AddItem(range, &rangeIndex);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
aRv.Throw(result);
|
|
|
|
return;
|
|
|
|
}
|
2017-05-30 07:26:09 +03:00
|
|
|
SetAnchorFocusRange(0);
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, range, true);
|
2017-03-14 04:36:21 +03:00
|
|
|
|
|
|
|
// Be aware, this instance may be destroyed after this call.
|
|
|
|
// XXX Why doesn't this call Selection::NotifySelectionListener() directly?
|
2017-04-06 07:11:52 +03:00
|
|
|
result = frameSelection->NotifySelectionListeners(GetType());
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
aRv.Throw(result);
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets the whole selection to be one point
|
|
|
|
* at the start of the current selection
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::CollapseToStart()
|
2013-12-17 18:12:33 +04:00
|
|
|
{
|
|
|
|
ErrorResult result;
|
|
|
|
CollapseToStart(result);
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2013-12-17 18:12:33 +04:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
|
|
|
Selection::CollapseToStartJS(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AutoRestore<bool> calledFromJSRestorer(mCalledByJS);
|
|
|
|
mCalledByJS = true;
|
|
|
|
CollapseToStart(aRv);
|
|
|
|
}
|
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
void
|
|
|
|
Selection::CollapseToStart(ErrorResult& aRv)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2018-03-27 07:35:22 +03:00
|
|
|
if (RangeCount() == 0) {
|
2013-12-17 18:12:33 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2000-12-21 01:13:54 +03:00
|
|
|
// Get the first range
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange* firstRange = mRanges[0].mRange;
|
2013-12-17 18:12:33 +04:00
|
|
|
if (!firstRange) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2013-08-17 05:51:41 +04:00
|
|
|
if (mFrameSelection) {
|
|
|
|
int16_t reason = mFrameSelection->PopReason() | nsISelectionListener::COLLAPSETOSTART_REASON;
|
|
|
|
mFrameSelection->PostReason(reason);
|
|
|
|
}
|
2017-07-11 17:52:39 +03:00
|
|
|
nsINode* container = firstRange->GetStartContainer();
|
|
|
|
if (!container) {
|
2013-12-17 18:12:33 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2017-07-11 17:52:39 +03:00
|
|
|
Collapse(*container, firstRange->StartOffset(), aRv);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Sets the whole selection to be one point
|
|
|
|
* at the end of the current selection
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::CollapseToEnd()
|
2013-12-17 18:12:33 +04:00
|
|
|
{
|
|
|
|
ErrorResult result;
|
|
|
|
CollapseToEnd(result);
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2013-12-17 18:12:33 +04:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
|
|
|
Selection::CollapseToEndJS(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AutoRestore<bool> calledFromJSRestorer(mCalledByJS);
|
|
|
|
mCalledByJS = true;
|
|
|
|
CollapseToEnd(aRv);
|
|
|
|
}
|
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
void
|
|
|
|
Selection::CollapseToEnd(ErrorResult& aRv)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2018-03-27 07:35:22 +03:00
|
|
|
uint32_t cnt = RangeCount();
|
|
|
|
if (cnt == 0) {
|
2013-12-17 18:12:33 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2000-12-21 01:13:54 +03:00
|
|
|
// Get the last range
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange* lastRange = mRanges[cnt - 1].mRange;
|
2013-12-17 18:12:33 +04:00
|
|
|
if (!lastRange) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2013-08-17 05:51:41 +04:00
|
|
|
if (mFrameSelection) {
|
|
|
|
int16_t reason = mFrameSelection->PopReason() | nsISelectionListener::COLLAPSETOEND_REASON;
|
|
|
|
mFrameSelection->PostReason(reason);
|
|
|
|
}
|
2017-07-11 17:52:39 +03:00
|
|
|
nsINode* container = lastRange->GetEndContainer();
|
|
|
|
if (!container) {
|
2013-12-17 18:12:33 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2017-07-11 17:52:39 +03:00
|
|
|
Collapse(*container, lastRange->EndOffset(), aRv);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2012-05-18 12:29:39 +04:00
|
|
|
/* virtual */
|
|
|
|
bool
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::Collapsed()
|
2012-05-18 12:29:39 +04:00
|
|
|
{
|
|
|
|
return IsCollapsed();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::GetIsCollapsed(bool* aIsCollapsed)
|
2012-05-18 12:29:39 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_TRUE(aIsCollapsed, NS_ERROR_NULL_POINTER);
|
|
|
|
|
|
|
|
*aIsCollapsed = IsCollapsed();
|
2009-04-06 21:17:37 +04:00
|
|
|
return NS_OK;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2017-08-03 17:13:24 +03:00
|
|
|
void
|
|
|
|
Selection::GetType(nsAString& aOutType) const
|
|
|
|
{
|
|
|
|
if (!RangeCount()) {
|
|
|
|
aOutType.AssignLiteral("None");
|
|
|
|
} else if (IsCollapsed()) {
|
|
|
|
aOutType.AssignLiteral("Caret");
|
|
|
|
} else {
|
|
|
|
aOutType.AssignLiteral("Range");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
nsRange*
|
|
|
|
Selection::GetRangeAt(uint32_t aIndex, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsRange* range = GetRangeAt(aIndex);
|
|
|
|
if (!range) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2003-03-12 06:22:11 +03:00
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
return range;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange*
|
2016-09-30 16:36:10 +03:00
|
|
|
Selection::GetRangeAt(int32_t aIndex) const
|
2009-04-06 21:20:30 +04:00
|
|
|
{
|
2013-08-16 23:01:04 +04:00
|
|
|
RangeData empty(nullptr);
|
|
|
|
return mRanges.SafeElementAt(aIndex, empty).mRange;
|
2009-04-06 21:20:30 +04:00
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
utility function
|
|
|
|
*/
|
2009-04-06 21:17:37 +04:00
|
|
|
nsresult
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::SetAnchorFocusToRange(nsRange* aRange)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2009-02-26 19:36:34 +03:00
|
|
|
NS_ENSURE_STATE(mAnchorFocusRange);
|
2012-03-15 22:26:32 +04:00
|
|
|
|
2015-08-12 20:26:01 +03:00
|
|
|
bool collapsed = Collapsed();
|
|
|
|
|
2012-03-15 22:26:32 +04:00
|
|
|
nsresult res = RemoveItem(mAnchorFocusRange);
|
|
|
|
if (NS_FAILED(res))
|
|
|
|
return res;
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aOutIndex = -1;
|
2015-08-12 20:26:01 +03:00
|
|
|
res = AddItem(aRange, &aOutIndex, !collapsed);
|
2012-03-15 22:26:32 +04:00
|
|
|
if (NS_FAILED(res))
|
|
|
|
return res;
|
2017-05-30 07:26:09 +03:00
|
|
|
SetAnchorFocusRange(aOutIndex);
|
2012-03-15 22:26:32 +04:00
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2008-10-17 23:46:33 +04:00
|
|
|
void
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::ReplaceAnchorFocusRange(nsRange* aRange)
|
2008-10-17 23:46:33 +04:00
|
|
|
{
|
2012-09-14 14:00:31 +04:00
|
|
|
NS_ENSURE_TRUE_VOID(mAnchorFocusRange);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPresContext> presContext = GetPresContext();
|
2008-10-17 23:46:33 +04:00
|
|
|
if (presContext) {
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, mAnchorFocusRange, false);
|
2012-03-15 22:26:32 +04:00
|
|
|
SetAnchorFocusToRange(aRange);
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, mAnchorFocusRange, true);
|
2008-10-17 23:46:33 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-18 10:26:00 +03:00
|
|
|
void
|
|
|
|
Selection::AdjustAnchorFocusForMultiRange(nsDirection aDirection)
|
|
|
|
{
|
|
|
|
if (aDirection == mDirection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
SetDirection(aDirection);
|
|
|
|
|
|
|
|
if (RangeCount() <= 1) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRange* firstRange = GetRangeAt(0);
|
|
|
|
nsRange* lastRange = GetRangeAt(RangeCount() - 1);
|
|
|
|
|
|
|
|
if (mDirection == eDirPrevious) {
|
|
|
|
firstRange->SetIsGenerated(false);
|
|
|
|
lastRange->SetIsGenerated(true);
|
2017-05-30 07:26:09 +03:00
|
|
|
SetAnchorFocusRange(0);
|
2015-03-18 10:26:00 +03:00
|
|
|
} else { // aDir == eDirNext
|
|
|
|
firstRange->SetIsGenerated(true);
|
|
|
|
lastRange->SetIsGenerated(false);
|
2017-05-30 07:26:09 +03:00
|
|
|
SetAnchorFocusRange(RangeCount() - 1);
|
2015-03-18 10:26:00 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
/*
|
|
|
|
Notes which might come in handy for extend:
|
|
|
|
|
|
|
|
We can tell the direction of the selection by asking for the anchors selection
|
|
|
|
if the begin is less than the end then we know the selection is to the "right".
|
|
|
|
else it is a backwards selection.
|
|
|
|
a = anchor
|
|
|
|
1 = old cursor
|
|
|
|
2 = new cursor
|
|
|
|
|
|
|
|
if (a <= 1 && 1 <=2) a,1,2 or (a1,2)
|
|
|
|
if (a < 2 && 1 > 2) a,2,1
|
|
|
|
if (1 < a && a <2) 1,a,2
|
|
|
|
if (a > 2 && 2 >1) 1,2,a
|
|
|
|
if (2 < a && a <1) 2,a,1
|
|
|
|
if (a > 1 && 1 >2) 2,1,a
|
|
|
|
then execute
|
|
|
|
a 1 2 select from 1 to 2
|
|
|
|
a 2 1 deselect from 2 to 1
|
|
|
|
1 a 2 deselect from 1 to a select from a to 2
|
|
|
|
1 2 a deselect from 1 to 2
|
|
|
|
2 1 a = continue selection from 2 to 1
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Extend extends the selection away from the anchor.
|
|
|
|
* We don't need to know the direction, because we always change the focus.
|
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2017-07-11 17:33:04 +03:00
|
|
|
Selection::Extend(nsIDOMNode* aContainer, int32_t aOffset)
|
2009-04-06 21:17:37 +04:00
|
|
|
{
|
2017-07-11 17:52:39 +03:00
|
|
|
nsCOMPtr<nsINode> container = do_QueryInterface(aContainer);
|
|
|
|
return Extend(container, aOffset);
|
2009-04-06 21:17:37 +04:00
|
|
|
}
|
|
|
|
|
2012-05-18 12:29:39 +04:00
|
|
|
NS_IMETHODIMP
|
2017-07-11 17:33:04 +03:00
|
|
|
Selection::ExtendNative(nsINode* aContainer, int32_t aOffset)
|
2012-05-18 12:29:39 +04:00
|
|
|
{
|
2017-07-11 17:33:04 +03:00
|
|
|
return Extend(aContainer, aOffset);
|
2012-05-18 12:29:39 +04:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
2017-07-11 17:33:04 +03:00
|
|
|
Selection::ExtendJS(nsINode& aContainer, uint32_t aOffset, ErrorResult& aRv)
|
2017-03-10 10:55:12 +03:00
|
|
|
{
|
|
|
|
AutoRestore<bool> calledFromJSRestorer(mCalledByJS);
|
|
|
|
mCalledByJS = true;
|
2017-07-11 17:33:04 +03:00
|
|
|
Extend(aContainer, aOffset, aRv);
|
2017-03-10 10:55:12 +03:00
|
|
|
}
|
|
|
|
|
2009-04-06 21:17:37 +04:00
|
|
|
nsresult
|
2017-07-11 17:33:04 +03:00
|
|
|
Selection::Extend(nsINode* aContainer, int32_t aOffset)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2017-07-11 17:33:04 +03:00
|
|
|
if (!aContainer) {
|
2000-03-22 02:58:02 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2017-07-11 17:33:04 +03:00
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
ErrorResult result;
|
2017-07-11 17:33:04 +03:00
|
|
|
Extend(*aContainer, static_cast<uint32_t>(aOffset), result);
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2013-12-17 18:12:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-07-11 17:33:04 +03:00
|
|
|
Selection::Extend(nsINode& aContainer, uint32_t aOffset, ErrorResult& aRv)
|
2013-12-17 18:12:33 +04:00
|
|
|
{
|
2000-03-22 02:58:02 +03:00
|
|
|
// First, find the range containing the old focus point:
|
2013-12-17 18:12:33 +04:00
|
|
|
if (!mAnchorFocusRange) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2002-03-26 02:44:46 +03:00
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
if (!mFrameSelection) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED); // Can't do selection
|
|
|
|
return;
|
|
|
|
}
|
2006-04-14 04:28:22 +04:00
|
|
|
|
2017-08-10 15:02:08 +03:00
|
|
|
if (!HasSameRoot(aContainer)) {
|
|
|
|
// Return with no error
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2002-03-26 02:44:46 +03:00
|
|
|
nsresult res;
|
2017-07-11 17:33:04 +03:00
|
|
|
if (!IsValidSelectionPoint(mFrameSelection, &aContainer)) {
|
2013-12-17 18:12:33 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2002-03-26 02:44:46 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPresContext> presContext = GetPresContext();
|
2017-07-11 17:33:04 +03:00
|
|
|
if (!presContext || presContext->Document() != aContainer.OwnerDoc()) {
|
2013-12-17 18:12:33 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2013-08-29 18:14:15 +04:00
|
|
|
|
2015-02-21 07:27:59 +03:00
|
|
|
#ifdef DEBUG_SELECTION
|
|
|
|
nsDirection oldDirection = GetDirection();
|
|
|
|
#endif
|
2009-04-06 21:17:37 +04:00
|
|
|
nsINode* anchorNode = GetAnchorNode();
|
|
|
|
nsINode* focusNode = GetFocusNode();
|
2013-12-17 18:12:33 +04:00
|
|
|
uint32_t anchorOffset = AnchorOffset();
|
|
|
|
uint32_t focusOffset = FocusOffset();
|
2000-04-12 14:34:19 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRange> range = mAnchorFocusRange->CloneRange();
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2017-07-11 14:53:04 +03:00
|
|
|
nsINode* startNode = range->GetStartContainer();
|
2017-07-11 15:11:37 +03:00
|
|
|
nsINode* endNode = range->GetEndContainer();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startOffset = range->StartOffset();
|
|
|
|
int32_t endOffset = range->EndOffset();
|
2000-03-22 02:58:02 +03:00
|
|
|
|
|
|
|
//compare anchor to old cursor.
|
|
|
|
|
2010-10-19 07:24:39 +04:00
|
|
|
// We pass |disconnected| to the following ComparePoints calls in order
|
2013-12-02 14:26:11 +04:00
|
|
|
// to avoid assertions. ComparePoints returns 1 in the disconnected case
|
|
|
|
// and we can end up in various cases below, but it is assumed that in
|
|
|
|
// any of the cases we end up, the nsRange implementation will collapse
|
|
|
|
// the range to the new point because we can not make a valid range with
|
|
|
|
// a disconnected point. This means that whatever range is currently
|
|
|
|
// selected will be cleared.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool disconnected = false;
|
2013-12-02 14:26:11 +04:00
|
|
|
bool shouldClearRange = false;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t result1 = nsContentUtils::ComparePoints(anchorNode, anchorOffset,
|
2010-10-19 07:24:39 +04:00
|
|
|
focusNode, focusOffset,
|
|
|
|
&disconnected);
|
2000-03-22 02:58:02 +03:00
|
|
|
//compare old cursor to new cursor
|
2013-12-02 14:26:11 +04:00
|
|
|
shouldClearRange |= disconnected;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t result2 = nsContentUtils::ComparePoints(focusNode, focusOffset,
|
2017-07-11 17:33:04 +03:00
|
|
|
&aContainer, aOffset,
|
2010-10-19 07:24:39 +04:00
|
|
|
&disconnected);
|
2000-03-22 02:58:02 +03:00
|
|
|
//compare anchor to new cursor
|
2013-12-02 14:26:11 +04:00
|
|
|
shouldClearRange |= disconnected;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t result3 = nsContentUtils::ComparePoints(anchorNode, anchorOffset,
|
2017-07-11 17:33:04 +03:00
|
|
|
&aContainer, aOffset,
|
2010-10-19 07:24:39 +04:00
|
|
|
&disconnected);
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2013-12-02 14:26:11 +04:00
|
|
|
// If the points are disconnected, the range will be collapsed below,
|
|
|
|
// resulting in a range that selects nothing.
|
|
|
|
if (shouldClearRange) {
|
|
|
|
// Repaint the current range with the selection removed.
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, range, false);
|
2013-12-02 14:26:11 +04:00
|
|
|
}
|
|
|
|
|
2017-07-11 17:33:04 +03:00
|
|
|
RefPtr<nsRange> difRange = new nsRange(&aContainer);
|
2000-03-22 02:58:02 +03:00
|
|
|
if ((result1 == 0 && result3 < 0) || (result1 <= 0 && result2 < 0)){//a1,2 a,1,2
|
|
|
|
//select from 1 to 2 unless they are collapsed
|
2017-07-11 17:33:04 +03:00
|
|
|
range->SetEnd(aContainer, aOffset, aRv);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-21 07:27:59 +03:00
|
|
|
SetDirection(eDirNext);
|
2017-05-30 07:18:25 +03:00
|
|
|
res = difRange->SetStartAndEnd(focusNode, focusOffset,
|
2017-07-11 15:11:37 +03:00
|
|
|
range->GetEndContainer(),
|
|
|
|
range->EndOffset());
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, difRange , true);
|
2012-03-15 22:26:32 +04:00
|
|
|
res = SetAnchorFocusToRange(range);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
else if (result1 == 0 && result3 > 0){//2, a1
|
|
|
|
//select from 2 to 1a
|
2015-02-21 07:27:59 +03:00
|
|
|
SetDirection(eDirPrevious);
|
2017-07-11 17:33:04 +03:00
|
|
|
range->SetStart(aContainer, aOffset, aRv);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, range, true);
|
2012-03-15 22:26:32 +04:00
|
|
|
res = SetAnchorFocusToRange(range);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
else if (result3 <= 0 && result2 >= 0) {//a,2,1 or a2,1 or a,21 or a21
|
|
|
|
//deselect from 2 to 1
|
2017-07-11 17:33:04 +03:00
|
|
|
res = difRange->SetStartAndEnd(&aContainer, aOffset,
|
2017-05-30 07:18:25 +03:00
|
|
|
focusNode, focusOffset);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
2012-07-27 18:03:08 +04:00
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2017-07-11 17:33:04 +03:00
|
|
|
range->SetEnd(aContainer, aOffset, aRv);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-15 22:26:32 +04:00
|
|
|
res = SetAnchorFocusToRange(range);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, difRange, false); // deselect now
|
2017-07-11 15:11:37 +03:00
|
|
|
difRange->SetEnd(range->GetEndContainer(), range->EndOffset());
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, difRange, true); // must reselect last node
|
|
|
|
// maybe more
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
else if (result1 >= 0 && result3 <= 0) {//1,a,2 or 1a,2 or 1,a2 or 1a2
|
|
|
|
if (GetDirection() == eDirPrevious){
|
2009-04-06 21:17:37 +04:00
|
|
|
res = range->SetStart(endNode, endOffset);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2015-02-21 07:27:59 +03:00
|
|
|
SetDirection(eDirNext);
|
2017-07-11 17:33:04 +03:00
|
|
|
range->SetEnd(aContainer, aOffset, aRv);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2009-04-06 21:17:37 +04:00
|
|
|
if (focusNode != anchorNode || focusOffset != anchorOffset) {//if collapsed diff dont do anything
|
|
|
|
res = difRange->SetStart(focusNode, focusOffset);
|
2012-07-27 18:03:08 +04:00
|
|
|
nsresult tmp = difRange->SetEnd(anchorNode, anchorOffset);
|
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
res = tmp;
|
|
|
|
}
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2012-03-15 22:26:32 +04:00
|
|
|
res = SetAnchorFocusToRange(range);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2008-10-13 02:11:51 +04:00
|
|
|
//deselect from 1 to a
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, difRange , false);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2008-10-13 02:11:51 +04:00
|
|
|
else
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2012-03-15 22:26:32 +04:00
|
|
|
res = SetAnchorFocusToRange(range);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2008-10-13 02:11:51 +04:00
|
|
|
//select from a to 2
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, range , true);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
else if (result2 <= 0 && result3 >= 0) {//1,2,a or 12,a or 1,2a or 12a
|
|
|
|
//deselect from 1 to 2
|
2017-05-30 07:18:25 +03:00
|
|
|
res = difRange->SetStartAndEnd(focusNode, focusOffset,
|
2017-07-11 17:33:04 +03:00
|
|
|
&aContainer, aOffset);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
2012-07-27 18:03:08 +04:00
|
|
|
}
|
2015-02-21 07:27:59 +03:00
|
|
|
SetDirection(eDirPrevious);
|
2017-07-11 17:33:04 +03:00
|
|
|
range->SetStart(aContainer, aOffset, aRv);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2012-03-15 22:26:32 +04:00
|
|
|
res = SetAnchorFocusToRange(range);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, difRange , false);
|
2017-07-11 14:53:04 +03:00
|
|
|
difRange->SetStart(range->GetStartContainer(), range->StartOffset());
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, difRange, true); // must reselect last node
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
else if (result3 >= 0 && result1 <= 0) {//2,a,1 or 2a,1 or 2,a1 or 2a1
|
|
|
|
if (GetDirection() == eDirNext){
|
2009-04-06 21:17:37 +04:00
|
|
|
range->SetEnd(startNode, startOffset);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2015-02-21 07:27:59 +03:00
|
|
|
SetDirection(eDirPrevious);
|
2017-07-11 17:33:04 +03:00
|
|
|
range->SetStart(aContainer, aOffset, aRv);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2008-10-13 02:11:51 +04:00
|
|
|
//deselect from a to 1
|
2009-04-06 21:17:37 +04:00
|
|
|
if (focusNode != anchorNode || focusOffset!= anchorOffset) {//if collapsed diff dont do anything
|
2017-05-30 07:18:25 +03:00
|
|
|
res = difRange->SetStartAndEnd(anchorNode, anchorOffset,
|
|
|
|
focusNode, focusOffset);
|
|
|
|
nsresult tmp = SetAnchorFocusToRange(range);
|
2012-07-27 18:03:08 +04:00
|
|
|
if (NS_FAILED(tmp)) {
|
|
|
|
res = tmp;
|
|
|
|
}
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, difRange, false);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2012-03-15 22:26:32 +04:00
|
|
|
res = SetAnchorFocusToRange(range);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2008-10-13 02:11:51 +04:00
|
|
|
//select from 2 to a
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, range , true);
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
else if (result2 >= 0 && result1 >= 0) {//2,1,a or 21,a or 2,1a or 21a
|
|
|
|
//select from 2 to 1
|
2017-07-11 17:33:04 +03:00
|
|
|
range->SetStart(aContainer, aOffset, aRv);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2015-02-21 07:27:59 +03:00
|
|
|
SetDirection(eDirPrevious);
|
2017-05-30 07:18:25 +03:00
|
|
|
res = difRange->SetStartAndEnd(
|
2017-07-11 14:53:04 +03:00
|
|
|
range->GetStartContainer(), range->StartOffset(),
|
2017-05-30 07:18:25 +03:00
|
|
|
focusNode, focusOffset);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, difRange, true);
|
2012-03-15 22:26:32 +04:00
|
|
|
res = SetAnchorFocusToRange(range);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
return;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2014-09-10 21:07:36 +04:00
|
|
|
if (mRanges.Length() > 1) {
|
|
|
|
for (size_t i = 0; i < mRanges.Length(); ++i) {
|
|
|
|
nsRange* range = mRanges[i].mRange;
|
|
|
|
MOZ_ASSERT(range->IsInSelection());
|
2017-05-29 06:27:09 +03:00
|
|
|
SelectFrames(presContext, range, range->IsInSelection());
|
2014-09-10 21:07:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
DEBUG_OUT_RANGE(range);
|
2009-04-06 21:17:37 +04:00
|
|
|
#ifdef DEBUG_SELECTION
|
2015-02-21 07:27:59 +03:00
|
|
|
if (GetDirection() != oldDirection) {
|
|
|
|
printf(" direction changed to %s\n",
|
|
|
|
GetDirection() == eDirNext? "eDirNext":"eDirPrevious");
|
|
|
|
}
|
2017-07-11 17:33:04 +03:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(&aContainer);
|
2013-12-17 18:12:33 +04:00
|
|
|
printf ("Sel. Extend to %p %s %d\n", content.get(),
|
2015-03-03 14:09:00 +03:00
|
|
|
nsAtomCString(content->NodeInfo()->NameAtom()).get(), aOffset);
|
2000-03-22 02:58:02 +03:00
|
|
|
#endif
|
2017-03-14 04:36:21 +03:00
|
|
|
|
|
|
|
// Be aware, this instance may be destroyed after this call.
|
|
|
|
// XXX Why doesn't this call Selection::NotifySelectionListener() directly?
|
2017-04-06 07:11:52 +03:00
|
|
|
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
|
|
|
|
res = frameSelection->NotifySelectionListeners(GetType());
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(res)) {
|
|
|
|
aRv.Throw(res);
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2000-09-13 00:54:41 +04:00
|
|
|
NS_IMETHODIMP
|
2017-07-11 17:33:04 +03:00
|
|
|
Selection::SelectAllChildren(nsIDOMNode* aNode)
|
2000-09-13 00:54:41 +04:00
|
|
|
{
|
2013-12-17 18:12:33 +04:00
|
|
|
ErrorResult result;
|
2017-07-11 17:33:04 +03:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
2013-12-17 18:12:33 +04:00
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_INVALID_ARG);
|
|
|
|
SelectAllChildren(*node, result);
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2013-12-17 18:12:33 +04:00
|
|
|
}
|
2012-03-16 20:56:54 +04:00
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
|
|
|
Selection::SelectAllChildrenJS(nsINode& aNode, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AutoRestore<bool> calledFromJSRestorer(mCalledByJS);
|
|
|
|
mCalledByJS = true;
|
|
|
|
SelectAllChildren(aNode, aRv);
|
|
|
|
}
|
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
void
|
|
|
|
Selection::SelectAllChildren(nsINode& aNode, ErrorResult& aRv)
|
|
|
|
{
|
2018-01-30 07:10:53 +03:00
|
|
|
if (aNode.NodeType() == nsINode::DOCUMENT_TYPE_NODE) {
|
2017-08-10 15:02:08 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!HasSameRoot(aNode)) {
|
|
|
|
// Return with no error
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:26:01 +03:00
|
|
|
if (mFrameSelection) {
|
2000-09-14 15:45:01 +04:00
|
|
|
mFrameSelection->PostReason(nsISelectionListener::SELECTALL_REASON);
|
2000-09-13 00:54:41 +04:00
|
|
|
}
|
2015-08-12 20:26:01 +03:00
|
|
|
SelectionBatcher batch(this);
|
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
Collapse(aNode, 0, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-16 20:56:54 +04:00
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
Extend(aNode, aNode.GetChildCount(), aRv);
|
2000-09-13 00:54:41 +04:00
|
|
|
}
|
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::ContainsNode(nsIDOMNode* aNode, bool aAllowPartial, bool* aYes)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2014-09-16 23:11:52 +04:00
|
|
|
if (!aYes) {
|
2000-03-22 02:58:02 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2014-09-16 23:11:52 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
*aYes = false;
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2009-04-06 21:17:37 +04:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
2014-09-16 23:11:52 +04:00
|
|
|
if (!node) {
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2013-12-17 18:12:33 +04:00
|
|
|
ErrorResult result;
|
2014-09-16 23:11:52 +04:00
|
|
|
*aYes = ContainsNode(*node, aAllowPartial, result);
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2013-12-17 18:12:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2014-09-16 23:11:52 +04:00
|
|
|
Selection::ContainsNode(nsINode& aNode, bool aAllowPartial, ErrorResult& aRv)
|
2013-12-17 18:12:33 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
2014-09-16 23:11:52 +04:00
|
|
|
if (mRanges.Length() == 0) {
|
2013-12-17 18:12:33 +04:00
|
|
|
return false;
|
2014-09-16 23:11:52 +04:00
|
|
|
}
|
2009-04-06 21:17:37 +04:00
|
|
|
|
|
|
|
// XXXbz this duplicates the GetNodeLength code in nsRange.cpp
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t nodeLength;
|
2018-04-15 13:46:24 +03:00
|
|
|
bool isData = aNode.IsCharacterData();
|
2009-04-06 21:17:37 +04:00
|
|
|
if (isData) {
|
2018-04-13 01:41:00 +03:00
|
|
|
nodeLength = aNode.AsText()->TextLength();
|
2006-07-27 21:57:15 +04:00
|
|
|
} else {
|
2014-09-16 23:11:52 +04:00
|
|
|
nodeLength = aNode.GetChildCount();
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
|
|
|
|
2012-01-10 18:19:54 +04:00
|
|
|
nsTArray<nsRange*> overlappingRanges;
|
2014-09-16 23:11:52 +04:00
|
|
|
rv = GetRangesForIntervalArray(&aNode, 0, &aNode, nodeLength,
|
2012-01-10 18:19:54 +04:00
|
|
|
false, &overlappingRanges);
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return false;
|
|
|
|
}
|
2012-01-10 18:19:54 +04:00
|
|
|
if (overlappingRanges.Length() == 0)
|
2013-12-17 18:12:33 +04:00
|
|
|
return false; // no ranges overlap
|
|
|
|
|
2006-07-27 21:57:15 +04:00
|
|
|
// if the caller said partial intersections are OK, we're done
|
|
|
|
if (aAllowPartial) {
|
2013-12-17 18:12:33 +04:00
|
|
|
return true;
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
2000-08-29 10:37:12 +04:00
|
|
|
|
2006-07-27 21:57:15 +04:00
|
|
|
// text nodes always count as inside
|
2009-04-06 21:17:37 +04:00
|
|
|
if (isData) {
|
2013-12-17 18:12:33 +04:00
|
|
|
return true;
|
2006-07-27 21:57:15 +04:00
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2006-07-27 21:57:15 +04:00
|
|
|
// The caller wants to know if the node is entirely within the given range,
|
|
|
|
// so we have to check all intersecting ranges.
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = 0; i < overlappingRanges.Length(); i++) {
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nodeStartsBeforeRange, nodeEndsAfterRange;
|
2014-09-16 23:11:52 +04:00
|
|
|
if (NS_SUCCEEDED(nsRange::CompareNodeToRange(&aNode, overlappingRanges[i],
|
2006-07-27 21:57:15 +04:00
|
|
|
&nodeStartsBeforeRange,
|
|
|
|
&nodeEndsAfterRange))) {
|
|
|
|
if (!nodeStartsBeforeRange && !nodeEndsAfterRange) {
|
2013-12-17 18:12:33 +04:00
|
|
|
return true;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2013-12-17 18:12:33 +04:00
|
|
|
return false;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2016-09-30 16:36:10 +03:00
|
|
|
class PointInRectChecker : public nsLayoutUtils::RectCallback {
|
|
|
|
public:
|
|
|
|
explicit PointInRectChecker(const nsPoint& aPoint)
|
|
|
|
: mPoint(aPoint)
|
|
|
|
, mMatchFound(false)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddRect(const nsRect& aRect) override
|
|
|
|
{
|
|
|
|
mMatchFound = mMatchFound || aRect.Contains(mPoint);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool MatchFound()
|
|
|
|
{
|
|
|
|
return mMatchFound;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsPoint mPoint;
|
|
|
|
bool mMatchFound;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool
|
|
|
|
Selection::ContainsPoint(const nsPoint& aPoint)
|
|
|
|
{
|
|
|
|
if (IsCollapsed()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
PointInRectChecker checker(aPoint);
|
|
|
|
for (uint32_t i = 0; i < RangeCount(); i++) {
|
|
|
|
nsRange* range = GetRangeAt(i);
|
2016-11-02 23:49:43 +03:00
|
|
|
nsRange::CollectClientRectsAndText(&checker, nullptr, range,
|
2017-07-11 14:53:04 +03:00
|
|
|
range->GetStartContainer(),
|
|
|
|
range->StartOffset(),
|
2017-07-11 15:11:37 +03:00
|
|
|
range->GetEndContainer(),
|
2017-07-11 14:53:04 +03:00
|
|
|
range->EndOffset(),
|
|
|
|
true, false);
|
2016-09-30 16:36:10 +03:00
|
|
|
if (checker.MatchFound()) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
2000-08-29 03:35:57 +04:00
|
|
|
|
2012-12-19 22:44:58 +04:00
|
|
|
nsPresContext*
|
|
|
|
Selection::GetPresContext() const
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2012-12-19 22:44:58 +04:00
|
|
|
nsIPresShell *shell = GetPresShell();
|
|
|
|
if (!shell) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2012-12-19 22:44:58 +04:00
|
|
|
return shell->GetPresContext();
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2012-12-19 04:55:11 +04:00
|
|
|
nsIPresShell*
|
|
|
|
Selection::GetPresShell() const
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2000-05-11 08:25:43 +04:00
|
|
|
if (!mFrameSelection)
|
2012-12-19 04:55:11 +04:00
|
|
|
return nullptr;//nothing to do
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2012-12-19 04:55:11 +04:00
|
|
|
return mFrameSelection->GetShell();
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2017-03-14 04:36:21 +03:00
|
|
|
nsIDocument*
|
|
|
|
Selection::GetDocument() const
|
|
|
|
{
|
|
|
|
nsIPresShell* presShell = GetPresShell();
|
|
|
|
return presShell ? presShell->GetDocument() : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsPIDOMWindowOuter*
|
|
|
|
Selection::GetWindow() const
|
|
|
|
{
|
|
|
|
nsIDocument* document = GetDocument();
|
|
|
|
return document ? document->GetWindow() : nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-07 10:33:59 +03:00
|
|
|
HTMLEditor*
|
|
|
|
Selection::GetHTMLEditor() const
|
2017-03-14 04:36:21 +03:00
|
|
|
{
|
|
|
|
nsPresContext* presContext = GetPresContext();
|
|
|
|
if (!presContext) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return nsContentUtils::GetHTMLEditor(presContext);
|
|
|
|
}
|
|
|
|
|
2009-09-10 09:16:18 +04:00
|
|
|
nsIFrame *
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::GetSelectionAnchorGeometry(SelectionRegion aRegion, nsRect* aRect)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2000-05-11 08:25:43 +04:00
|
|
|
if (!mFrameSelection)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr; // nothing to do
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(aRect, nullptr);
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2006-11-09 19:37:10 +03:00
|
|
|
aRect->SetRect(0, 0, 0, 0);
|
2010-11-25 04:35:02 +03:00
|
|
|
|
|
|
|
switch (aRegion) {
|
|
|
|
case nsISelectionController::SELECTION_ANCHOR_REGION:
|
|
|
|
case nsISelectionController::SELECTION_FOCUS_REGION:
|
|
|
|
return GetSelectionEndPointGeometry(aRegion, aRect);
|
|
|
|
case nsISelectionController::SELECTION_WHOLE_SELECTION:
|
|
|
|
break;
|
|
|
|
default:
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-11-25 04:35:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(aRegion == nsISelectionController::SELECTION_WHOLE_SELECTION,
|
|
|
|
"should only be SELECTION_WHOLE_SELECTION here");
|
|
|
|
|
|
|
|
nsRect anchorRect;
|
|
|
|
nsIFrame* anchorFrame = GetSelectionEndPointGeometry(
|
|
|
|
nsISelectionController::SELECTION_ANCHOR_REGION, &anchorRect);
|
|
|
|
if (!anchorFrame)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-11-25 04:35:02 +03:00
|
|
|
|
|
|
|
nsRect focusRect;
|
|
|
|
nsIFrame* focusFrame = GetSelectionEndPointGeometry(
|
|
|
|
nsISelectionController::SELECTION_FOCUS_REGION, &focusRect);
|
|
|
|
if (!focusFrame)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2010-11-25 04:35:02 +03:00
|
|
|
|
|
|
|
NS_ASSERTION(anchorFrame->PresContext() == focusFrame->PresContext(),
|
|
|
|
"points of selection in different documents?");
|
|
|
|
// make focusRect relative to anchorFrame
|
|
|
|
focusRect += focusFrame->GetOffsetTo(anchorFrame);
|
|
|
|
|
2011-04-19 07:07:23 +04:00
|
|
|
aRect->UnionRectEdges(anchorRect, focusRect);
|
2010-11-25 04:35:02 +03:00
|
|
|
return anchorFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame *
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::GetSelectionEndPointGeometry(SelectionRegion aRegion, nsRect* aRect)
|
2010-11-25 04:35:02 +03:00
|
|
|
{
|
|
|
|
if (!mFrameSelection)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr; // nothing to do
|
2010-11-25 04:35:02 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(aRect, nullptr);
|
2010-11-25 04:35:02 +03:00
|
|
|
|
|
|
|
aRect->SetRect(0, 0, 0, 0);
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsINode *node = nullptr;
|
2013-12-17 18:12:33 +04:00
|
|
|
uint32_t nodeOffset = 0;
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIFrame *frame = nullptr;
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2006-11-09 19:37:10 +03:00
|
|
|
switch (aRegion) {
|
|
|
|
case nsISelectionController::SELECTION_ANCHOR_REGION:
|
2009-04-06 21:17:38 +04:00
|
|
|
node = GetAnchorNode();
|
2013-12-17 18:12:33 +04:00
|
|
|
nodeOffset = AnchorOffset();
|
2006-11-09 19:37:10 +03:00
|
|
|
break;
|
|
|
|
case nsISelectionController::SELECTION_FOCUS_REGION:
|
2009-04-06 21:17:38 +04:00
|
|
|
node = GetFocusNode();
|
2013-12-17 18:12:33 +04:00
|
|
|
nodeOffset = FocusOffset();
|
2006-11-09 19:37:10 +03:00
|
|
|
break;
|
|
|
|
default:
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!node)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2000-03-22 02:58:02 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(node);
|
2012-07-30 18:20:58 +04:00
|
|
|
NS_ENSURE_TRUE(content.get(), nullptr);
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t frameOffset = 0;
|
2006-11-09 19:37:10 +03:00
|
|
|
frame = mFrameSelection->GetFrameForNodeOffset(content, nodeOffset,
|
|
|
|
mFrameSelection->GetHint(),
|
|
|
|
&frameOffset);
|
2000-03-22 02:58:02 +03:00
|
|
|
if (!frame)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2000-08-30 23:42:47 +04:00
|
|
|
|
|
|
|
// Figure out what node type we have, then get the
|
|
|
|
// appropriate rect for it's nodeOffset.
|
2018-04-13 01:41:00 +03:00
|
|
|
bool isText = node->IsText();
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2006-11-09 19:37:10 +03:00
|
|
|
nsPoint pt(0, 0);
|
2009-04-06 21:17:38 +04:00
|
|
|
if (isText) {
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIFrame* childFrame = nullptr;
|
2006-11-09 19:37:10 +03:00
|
|
|
frameOffset = 0;
|
2009-04-06 21:17:38 +04:00
|
|
|
nsresult rv =
|
|
|
|
frame->GetChildFrameContainingOffset(nodeOffset,
|
|
|
|
mFrameSelection->GetHint(),
|
|
|
|
&frameOffset, &childFrame);
|
2006-11-09 19:37:10 +03:00
|
|
|
if (NS_FAILED(rv))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2000-03-22 02:58:02 +03:00
|
|
|
if (!childFrame)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2000-03-22 02:58:02 +03:00
|
|
|
|
|
|
|
frame = childFrame;
|
|
|
|
|
|
|
|
// Get the x coordinate of the offset into the text frame.
|
2006-11-09 19:37:10 +03:00
|
|
|
rv = GetCachedFrameOffset(frame, nodeOffset, pt);
|
|
|
|
if (NS_FAILED(rv))
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2006-11-09 19:37:10 +03:00
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2009-09-10 09:16:18 +04:00
|
|
|
// Return the rect relative to the frame, with zero width.
|
2009-04-06 21:17:38 +04:00
|
|
|
if (isText) {
|
2018-02-19 23:15:23 +03:00
|
|
|
aRect->x = pt.x;
|
2014-08-06 09:19:27 +04:00
|
|
|
} else if (mFrameSelection->GetHint() == CARET_ASSOCIATE_BEFORE) {
|
2006-11-09 19:37:10 +03:00
|
|
|
// It's the frame's right edge we're interested in.
|
2018-02-19 23:15:23 +03:00
|
|
|
aRect->x = frame->GetRect().Width();
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
2017-08-14 15:30:10 +03:00
|
|
|
aRect->SetHeight(frame->GetRect().Height());
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2009-09-10 09:16:18 +04:00
|
|
|
return frame;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2006-05-10 21:30:15 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::ScrollSelectionIntoViewEvent::Run()
|
2002-06-14 00:43:32 +04:00
|
|
|
{
|
2012-06-08 18:20:55 +04:00
|
|
|
if (!mSelection)
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK; // event revoked
|
2002-06-14 00:43:32 +04:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t flags = Selection::SCROLL_DO_FLUSH |
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::SCROLL_SYNCHRONOUS;
|
2010-10-23 02:41:55 +04:00
|
|
|
|
2017-03-21 04:12:41 +03:00
|
|
|
Selection* sel = mSelection; // workaround to satisfy static analysis
|
|
|
|
RefPtr<Selection> kungFuDeathGrip(sel);
|
2012-06-08 18:20:55 +04:00
|
|
|
mSelection->mScrollEvent.Forget();
|
|
|
|
mSelection->ScrollIntoView(mRegion, mVerticalScroll,
|
2012-10-07 22:45:51 +04:00
|
|
|
mHorizontalScroll, mFlags | flags);
|
2006-05-10 21:30:15 +04:00
|
|
|
return NS_OK;
|
2002-06-14 00:43:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::PostScrollSelectionIntoViewEvent(
|
2012-03-20 06:09:50 +04:00
|
|
|
SelectionRegion aRegion,
|
2012-10-07 22:45:51 +04:00
|
|
|
int32_t aFlags,
|
2012-03-20 06:09:50 +04:00
|
|
|
nsIPresShell::ScrollAxis aVertical,
|
|
|
|
nsIPresShell::ScrollAxis aHorizontal)
|
2002-06-14 00:43:32 +04:00
|
|
|
{
|
2006-05-10 21:30:15 +04:00
|
|
|
// If we've already posted an event, revoke it and place a new one at the
|
|
|
|
// end of the queue to make sure that any new pending reflow events are
|
|
|
|
// processed before we scroll. This will insure that we scroll to the
|
|
|
|
// correct place on screen.
|
|
|
|
mScrollEvent.Revoke();
|
2017-06-25 00:38:42 +03:00
|
|
|
nsPresContext* presContext = GetPresContext();
|
|
|
|
NS_ENSURE_STATE(presContext);
|
|
|
|
nsRefreshDriver* refreshDriver = presContext->RefreshDriver();
|
|
|
|
NS_ENSURE_STATE(refreshDriver);
|
2002-06-14 00:43:32 +04:00
|
|
|
|
2017-07-19 10:03:59 +03:00
|
|
|
mScrollEvent =
|
2017-03-16 10:33:00 +03:00
|
|
|
new ScrollSelectionIntoViewEvent(this, aRegion, aVertical, aHorizontal,
|
|
|
|
aFlags);
|
2017-07-19 10:03:59 +03:00
|
|
|
refreshDriver->AddEarlyRunner(mScrollEvent.get());
|
2017-06-25 00:38:42 +03:00
|
|
|
return NS_OK;
|
2002-06-14 00:43:32 +04:00
|
|
|
}
|
|
|
|
|
2007-08-24 08:54:45 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::ScrollIntoView(SelectionRegion aRegion, bool aIsSynchronous,
|
2012-08-22 19:56:38 +04:00
|
|
|
int16_t aVPercent, int16_t aHPercent)
|
2012-03-20 06:09:50 +04:00
|
|
|
{
|
2013-12-17 18:12:33 +04:00
|
|
|
ErrorResult result;
|
|
|
|
ScrollIntoView(aRegion, aIsSynchronous, aVPercent, aHPercent, result);
|
|
|
|
if (result.Failed()) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2013-12-17 18:12:33 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Selection::ScrollIntoView(int16_t aRegion, bool aIsSynchronous,
|
|
|
|
int16_t aVPercent, int16_t aHPercent,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
nsresult rv = ScrollIntoViewInternal(aRegion, aIsSynchronous,
|
|
|
|
nsIPresShell::ScrollAxis(aVPercent),
|
|
|
|
nsIPresShell::ScrollAxis(aHPercent));
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
}
|
2012-03-20 06:09:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::ScrollIntoViewInternal(SelectionRegion aRegion, bool aIsSynchronous,
|
|
|
|
nsIPresShell::ScrollAxis aVertical,
|
|
|
|
nsIPresShell::ScrollAxis aHorizontal)
|
2007-08-24 08:54:45 +04:00
|
|
|
{
|
2012-03-20 06:09:50 +04:00
|
|
|
return ScrollIntoView(aRegion, aVertical, aHorizontal,
|
2012-06-08 18:20:55 +04:00
|
|
|
aIsSynchronous ? Selection::SCROLL_SYNCHRONOUS : 0);
|
2007-08-24 08:54:45 +04:00
|
|
|
}
|
|
|
|
|
2007-08-22 06:57:06 +04:00
|
|
|
nsresult
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::ScrollIntoView(SelectionRegion aRegion,
|
|
|
|
nsIPresShell::ScrollAxis aVertical,
|
|
|
|
nsIPresShell::ScrollAxis aHorizontal,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aFlags)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2000-05-11 08:25:43 +04:00
|
|
|
if (!mFrameSelection)
|
|
|
|
return NS_OK;//nothing to do
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2017-07-29 07:31:46 +03:00
|
|
|
nsIPresShell* presShell = mFrameSelection->GetShell();
|
2012-11-04 10:41:42 +04:00
|
|
|
if (!presShell)
|
|
|
|
return NS_OK;
|
|
|
|
|
2000-03-22 02:58:02 +03:00
|
|
|
if (mFrameSelection->GetBatching())
|
|
|
|
return NS_OK;
|
|
|
|
|
2012-06-08 18:20:55 +04:00
|
|
|
if (!(aFlags & Selection::SCROLL_SYNCHRONOUS))
|
2012-10-07 22:45:51 +04:00
|
|
|
return PostScrollSelectionIntoViewEvent(aRegion, aFlags,
|
2012-03-20 06:09:50 +04:00
|
|
|
aVertical, aHorizontal);
|
2002-06-14 00:43:32 +04:00
|
|
|
|
2017-07-29 07:31:46 +03:00
|
|
|
// From this point on, the presShell may get destroyed by the calls below, so
|
|
|
|
// hold on to it using a strong reference to ensure the safety of the
|
|
|
|
// accesses to frame pointers in the callees.
|
|
|
|
nsCOMPtr<nsIPresShell> kungFuDeathGrip(presShell);
|
|
|
|
|
2012-10-29 08:31:45 +04:00
|
|
|
// Now that text frame character offsets are always valid (though not
|
|
|
|
// necessarily correct), the worst that will happen if we don't flush here
|
|
|
|
// is that some callers might scroll to the wrong place. Those should
|
|
|
|
// either manually flush if they're in a safe position for it or use the
|
|
|
|
// async version of this method.
|
|
|
|
if (aFlags & Selection::SCROLL_DO_FLUSH) {
|
2017-01-05 10:31:56 +03:00
|
|
|
presShell->FlushPendingNotifications(FlushType::Layout);
|
2012-10-29 08:31:45 +04:00
|
|
|
|
2012-11-04 10:41:42 +04:00
|
|
|
// Reget the presshell, since it might have been Destroy'ed.
|
|
|
|
presShell = mFrameSelection ? mFrameSelection->GetShell() : nullptr;
|
|
|
|
if (!presShell)
|
|
|
|
return NS_OK;
|
2012-10-29 08:31:45 +04:00
|
|
|
}
|
2000-08-30 23:42:47 +04:00
|
|
|
|
2012-10-29 08:31:45 +04:00
|
|
|
//
|
|
|
|
// Scroll the selection region into view.
|
|
|
|
//
|
2012-09-24 08:38:16 +04:00
|
|
|
|
2012-10-29 08:31:45 +04:00
|
|
|
nsRect rect;
|
|
|
|
nsIFrame* frame = GetSelectionAnchorGeometry(aRegion, &rect);
|
|
|
|
if (!frame)
|
|
|
|
return NS_ERROR_FAILURE;
|
2012-10-07 22:45:51 +04:00
|
|
|
|
2012-10-29 08:31:45 +04:00
|
|
|
// Scroll vertically to get the caret into view, but only if the container
|
|
|
|
// is perceived to be scrollable in that direction (i.e. there is a visible
|
|
|
|
// vertical scrollbar or the scroll range is at least one device pixel)
|
|
|
|
aVertical.mOnlyIfPerceivedScrollableDirection = true;
|
2012-10-07 22:45:51 +04:00
|
|
|
|
2012-10-29 08:31:45 +04:00
|
|
|
uint32_t flags = 0;
|
|
|
|
if (aFlags & Selection::SCROLL_FIRST_ANCESTOR_ONLY) {
|
|
|
|
flags |= nsIPresShell::SCROLL_FIRST_ANCESTOR_ONLY;
|
2000-04-27 11:37:12 +04:00
|
|
|
}
|
2012-10-29 08:31:45 +04:00
|
|
|
if (aFlags & Selection::SCROLL_OVERFLOW_HIDDEN) {
|
|
|
|
flags |= nsIPresShell::SCROLL_OVERFLOW_HIDDEN;
|
|
|
|
}
|
|
|
|
|
|
|
|
presShell->ScrollFrameRectIntoView(frame, rect, aVertical, aHorizontal,
|
|
|
|
flags);
|
|
|
|
return NS_OK;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::AddSelectionListener(nsISelectionListener* aNewListener)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2000-05-04 12:35:42 +04:00
|
|
|
if (!aNewListener)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
2013-12-17 18:12:33 +04:00
|
|
|
ErrorResult result;
|
|
|
|
AddSelectionListener(aNewListener, result);
|
|
|
|
if (result.Failed()) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2013-12-17 18:12:33 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
void
|
|
|
|
Selection::AddSelectionListener(nsISelectionListener* aNewListener,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2017-06-09 07:15:49 +03:00
|
|
|
bool result = mSelectionListeners.AppendElement(aNewListener, fallible); // AddRefs
|
2013-12-17 18:12:33 +04:00
|
|
|
if (!result) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::RemoveSelectionListener(nsISelectionListener* aListenerToRemove)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2013-12-17 18:12:33 +04:00
|
|
|
if (!aListenerToRemove)
|
2000-05-04 12:35:42 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2013-12-17 18:12:33 +04:00
|
|
|
ErrorResult result;
|
|
|
|
RemoveSelectionListener(aListenerToRemove, result);
|
|
|
|
if (result.Failed()) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2013-12-17 18:12:33 +04:00
|
|
|
}
|
|
|
|
return NS_OK;
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
void
|
|
|
|
Selection::RemoveSelectionListener(nsISelectionListener* aListenerToRemove,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
2017-06-09 07:15:49 +03:00
|
|
|
bool result = mSelectionListeners.RemoveElement(aListenerToRemove); // Releases
|
2013-12-17 18:12:33 +04:00
|
|
|
if (!result) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2017-03-14 04:36:21 +03:00
|
|
|
Element*
|
|
|
|
Selection::GetCommonEditingHostForAllRanges()
|
|
|
|
{
|
|
|
|
Element* editingHost = nullptr;
|
|
|
|
for (RangeData& rangeData : mRanges) {
|
|
|
|
nsRange* range = rangeData.mRange;
|
|
|
|
MOZ_ASSERT(range);
|
|
|
|
nsINode* commonAncestorNode = range->GetCommonAncestor();
|
|
|
|
if (!commonAncestorNode || !commonAncestorNode->IsContent()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsIContent* commonAncestor = commonAncestorNode->AsContent();
|
|
|
|
Element* foundEditingHost = commonAncestor->GetEditingHost();
|
|
|
|
// Even when common ancestor is a non-editable element in a contenteditable
|
|
|
|
// element, we don't need to move focus to the contenteditable element
|
|
|
|
// because Chromium doesn't set focus to it.
|
|
|
|
if (!foundEditingHost) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if (!editingHost) {
|
|
|
|
editingHost = foundEditingHost;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (editingHost == foundEditingHost) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (nsContentUtils::ContentIsDescendantOf(foundEditingHost, editingHost)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (nsContentUtils::ContentIsDescendantOf(editingHost, foundEditingHost)) {
|
|
|
|
editingHost = foundEditingHost;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
// editingHost and foundEditingHost are not a descendant of the other.
|
|
|
|
// So, there is no common editing host.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return editingHost;
|
|
|
|
}
|
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
nsresult
|
|
|
|
Selection::NotifySelectionListeners(bool aCalledByJS)
|
|
|
|
{
|
|
|
|
AutoRestore<bool> calledFromJSRestorer(mCalledByJS);
|
|
|
|
mCalledByJS = aCalledByJS;
|
|
|
|
return NotifySelectionListeners();
|
|
|
|
}
|
|
|
|
|
2000-05-04 12:35:42 +04:00
|
|
|
nsresult
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::NotifySelectionListeners()
|
2000-05-04 12:35:42 +04:00
|
|
|
{
|
2000-05-11 08:25:43 +04:00
|
|
|
if (!mFrameSelection)
|
|
|
|
return NS_OK;//nothing to do
|
2017-03-14 04:36:21 +03:00
|
|
|
|
2017-03-16 11:15:20 +03:00
|
|
|
// Our internal code should not move focus with using this class while
|
|
|
|
// this moves focus nor from selection listeners.
|
|
|
|
AutoRestore<bool> calledByJSRestorer(mCalledByJS);
|
|
|
|
mCalledByJS = false;
|
|
|
|
|
2017-03-14 04:36:21 +03:00
|
|
|
// When normal selection is changed by Selection API, we need to move focus
|
|
|
|
// if common ancestor of all ranges are in an editing host. Note that we
|
|
|
|
// don't need to move focus *to* the other focusable node because other
|
|
|
|
// browsers don't do it either.
|
2017-03-16 11:15:20 +03:00
|
|
|
if (mSelectionType == SelectionType::eNormal &&
|
|
|
|
calledByJSRestorer.SavedValue()) {
|
2017-03-14 04:36:21 +03:00
|
|
|
nsPIDOMWindowOuter* window = GetWindow();
|
|
|
|
nsIDocument* document = GetDocument();
|
|
|
|
// If the document is in design mode or doesn't have contenteditable
|
|
|
|
// element, we don't need to move focus.
|
|
|
|
if (window && document && !document->HasFlag(NODE_IS_EDITABLE) &&
|
2017-08-07 10:33:59 +03:00
|
|
|
GetHTMLEditor()) {
|
2017-03-14 04:36:21 +03:00
|
|
|
RefPtr<Element> newEditingHost = GetCommonEditingHostForAllRanges();
|
|
|
|
nsFocusManager* fm = nsFocusManager::GetFocusManager();
|
|
|
|
nsCOMPtr<nsPIDOMWindowOuter> focusedWindow;
|
|
|
|
nsIContent* focusedContent =
|
2017-09-07 16:54:49 +03:00
|
|
|
nsFocusManager::GetFocusedDescendant(window,
|
|
|
|
nsFocusManager::eOnlyCurrentWindow,
|
|
|
|
getter_AddRefs(focusedWindow));
|
2017-03-14 04:36:21 +03:00
|
|
|
nsCOMPtr<Element> focusedElement = do_QueryInterface(focusedContent);
|
|
|
|
// When all selected ranges are in an editing host, it should take focus.
|
2017-05-30 12:07:22 +03:00
|
|
|
// But otherwise, we shouldn't move focus since Chromium doesn't move
|
|
|
|
// focus but only selection range is updated.
|
2017-03-14 04:36:21 +03:00
|
|
|
if (newEditingHost && newEditingHost != focusedElement) {
|
|
|
|
MOZ_ASSERT(!newEditingHost->IsInNativeAnonymousSubtree());
|
|
|
|
// Note that don't steal focus from focused window if the window doesn't
|
2017-07-05 15:35:18 +03:00
|
|
|
// have focus and if the window isn't focused window, shouldn't be
|
|
|
|
// scrolled to the new focused element.
|
|
|
|
uint32_t flags = nsIFocusManager::FLAG_NOSWITCHFRAME;
|
|
|
|
if (focusedWindow != fm->GetFocusedWindow()) {
|
|
|
|
flags |= nsIFocusManager::FLAG_NOSCROLL;
|
|
|
|
}
|
2018-04-26 17:37:46 +03:00
|
|
|
fm->SetFocus(newEditingHost, flags);
|
2017-03-14 04:36:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-06 07:11:52 +03:00
|
|
|
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
|
|
|
|
if (frameSelection->GetBatching()) {
|
|
|
|
frameSelection->SetDirty();
|
2000-05-04 12:35:42 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-08-15 23:47:30 +03:00
|
|
|
if (mSelectionListeners.IsEmpty()) {
|
|
|
|
// If there are no selection listeners, we're done!
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-06-09 07:15:49 +03:00
|
|
|
AutoTArray<nsCOMPtr<nsISelectionListener>, 8>
|
|
|
|
selectionListeners(mSelectionListeners);
|
2012-12-19 04:55:11 +04:00
|
|
|
|
2000-05-04 12:35:42 +04:00
|
|
|
nsCOMPtr<nsIDOMDocument> domdoc;
|
2012-12-19 04:55:11 +04:00
|
|
|
nsIPresShell* ps = GetPresShell();
|
|
|
|
if (ps) {
|
2017-08-15 23:48:42 +03:00
|
|
|
// Avoid using QueryInterface() here because it can be expensive.
|
|
|
|
domdoc = static_cast<nsIDOMDocument*>(ps->GetDocument()->AsDOMNode());
|
2012-12-19 04:55:11 +04:00
|
|
|
}
|
|
|
|
|
2017-04-06 07:11:52 +03:00
|
|
|
short reason = frameSelection->PopReason();
|
2017-06-09 07:15:49 +03:00
|
|
|
for (auto& listener : selectionListeners) {
|
|
|
|
listener->NotifySelectionChanged(domdoc, this, reason);
|
2000-05-04 12:35:42 +04:00
|
|
|
}
|
2003-03-12 06:22:11 +03:00
|
|
|
return NS_OK;
|
2000-05-04 12:35:42 +04:00
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2017-09-08 06:10:44 +03:00
|
|
|
void
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::StartBatchChanges()
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2017-04-06 07:11:52 +03:00
|
|
|
if (mFrameSelection) {
|
|
|
|
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
|
|
|
|
frameSelection->StartBatchChanges();
|
|
|
|
}
|
2015-12-01 23:25:06 +03:00
|
|
|
}
|
2006-04-26 06:01:07 +04:00
|
|
|
|
2017-09-08 06:10:44 +03:00
|
|
|
void
|
|
|
|
Selection::EndBatchChanges(int16_t aReason)
|
2015-12-01 23:25:06 +03:00
|
|
|
{
|
|
|
|
if (mFrameSelection) {
|
2017-04-06 07:11:52 +03:00
|
|
|
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
|
|
|
|
frameSelection->EndBatchChanges(aReason);
|
2015-12-01 23:25:06 +03:00
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2015-08-12 20:26:01 +03:00
|
|
|
void
|
|
|
|
Selection::AddSelectionChangeBlocker()
|
|
|
|
{
|
|
|
|
mSelectionChangeBlockerCount++;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
2015-08-12 20:26:01 +03:00
|
|
|
void
|
|
|
|
Selection::RemoveSelectionChangeBlocker()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mSelectionChangeBlockerCount > 0,
|
|
|
|
"mSelectionChangeBlockerCount has an invalid value - "
|
|
|
|
"maybe you have a mismatched RemoveSelectionChangeBlocker?");
|
|
|
|
mSelectionChangeBlockerCount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Selection::IsBlockingSelectionChangeEvents() const
|
|
|
|
{
|
|
|
|
return mSelectionChangeBlockerCount > 0;
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::DeleteFromDocument()
|
2013-12-17 18:12:33 +04:00
|
|
|
{
|
|
|
|
ErrorResult result;
|
|
|
|
DeleteFromDocument(result);
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2013-12-17 18:12:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Selection::DeleteFromDocument(ErrorResult& aRv)
|
2000-03-22 02:58:02 +03:00
|
|
|
{
|
2000-05-11 08:25:43 +04:00
|
|
|
if (!mFrameSelection)
|
2013-12-17 18:12:33 +04:00
|
|
|
return;//nothing to do
|
2017-04-06 07:11:52 +03:00
|
|
|
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
|
|
|
|
nsresult rv = frameSelection->DeleteFromDocument();
|
2013-12-17 18:12:33 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
}
|
2000-03-22 02:58:02 +03:00
|
|
|
}
|
|
|
|
|
2010-04-28 21:47:23 +04:00
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::Modify(const nsAString& aAlter, const nsAString& aDirection,
|
|
|
|
const nsAString& aGranularity)
|
2013-12-17 18:12:33 +04:00
|
|
|
{
|
|
|
|
ErrorResult result;
|
|
|
|
Modify(aAlter, aDirection, aGranularity, result);
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2013-12-17 18:12:33 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Selection::Modify(const nsAString& aAlter, const nsAString& aDirection,
|
|
|
|
const nsAString& aGranularity, ErrorResult& aRv)
|
2010-04-28 21:47:23 +04:00
|
|
|
{
|
|
|
|
// Silently exit if there's no selection or no focus node.
|
|
|
|
if (!mFrameSelection || !GetAnchorFocusRange() || !GetFocusNode()) {
|
2013-12-17 18:12:33 +04:00
|
|
|
return;
|
2010-04-28 21:47:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!aAlter.LowerCaseEqualsLiteral("move") &&
|
|
|
|
!aAlter.LowerCaseEqualsLiteral("extend")) {
|
2013-12-17 18:12:33 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
2010-04-28 21:47:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!aDirection.LowerCaseEqualsLiteral("forward") &&
|
|
|
|
!aDirection.LowerCaseEqualsLiteral("backward") &&
|
|
|
|
!aDirection.LowerCaseEqualsLiteral("left") &&
|
|
|
|
!aDirection.LowerCaseEqualsLiteral("right")) {
|
2013-12-17 18:12:33 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
2010-04-28 21:47:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Line moves are always visual.
|
2011-09-29 10:19:26 +04:00
|
|
|
bool visual = aDirection.LowerCaseEqualsLiteral("left") ||
|
2010-04-28 21:47:23 +04:00
|
|
|
aDirection.LowerCaseEqualsLiteral("right") ||
|
|
|
|
aGranularity.LowerCaseEqualsLiteral("line");
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool forward = aDirection.LowerCaseEqualsLiteral("forward") ||
|
2010-04-28 21:47:23 +04:00
|
|
|
aDirection.LowerCaseEqualsLiteral("right");
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool extend = aAlter.LowerCaseEqualsLiteral("extend");
|
2010-04-28 21:47:23 +04:00
|
|
|
|
|
|
|
nsSelectionAmount amount;
|
|
|
|
if (aGranularity.LowerCaseEqualsLiteral("character")) {
|
2010-12-17 01:17:41 +03:00
|
|
|
amount = eSelectCluster;
|
2014-11-22 17:39:02 +03:00
|
|
|
} else if (aGranularity.LowerCaseEqualsLiteral("word")) {
|
2011-03-20 22:49:06 +03:00
|
|
|
amount = eSelectWordNoSpace;
|
2014-11-22 17:39:02 +03:00
|
|
|
} else if (aGranularity.LowerCaseEqualsLiteral("line")) {
|
2010-04-28 21:47:23 +04:00
|
|
|
amount = eSelectLine;
|
2014-11-22 17:39:02 +03:00
|
|
|
} else if (aGranularity.LowerCaseEqualsLiteral("lineboundary")) {
|
|
|
|
amount = forward ? eSelectEndLine : eSelectBeginLine;
|
|
|
|
} else if (aGranularity.LowerCaseEqualsLiteral("sentence") ||
|
|
|
|
aGranularity.LowerCaseEqualsLiteral("sentenceboundary") ||
|
|
|
|
aGranularity.LowerCaseEqualsLiteral("paragraph") ||
|
|
|
|
aGranularity.LowerCaseEqualsLiteral("paragraphboundary") ||
|
|
|
|
aGranularity.LowerCaseEqualsLiteral("documentboundary")) {
|
2013-12-17 18:12:33 +04:00
|
|
|
aRv.Throw(NS_ERROR_NOT_IMPLEMENTED);
|
2014-06-15 03:43:46 +04:00
|
|
|
return;
|
2014-11-22 17:39:02 +03:00
|
|
|
} else {
|
2013-12-17 18:12:33 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SYNTAX_ERR);
|
|
|
|
return;
|
2010-04-28 21:47:23 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the anchor doesn't equal the focus and we try to move without first
|
|
|
|
// collapsing the selection, MoveCaret will collapse the selection and quit.
|
|
|
|
// To avoid this, we need to collapse the selection first.
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (!extend) {
|
|
|
|
nsINode* focusNode = GetFocusNode();
|
|
|
|
// We should have checked earlier that there was a focus node.
|
2013-12-17 18:12:33 +04:00
|
|
|
if (!focusNode) {
|
|
|
|
aRv.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
uint32_t focusOffset = FocusOffset();
|
2010-04-28 21:47:23 +04:00
|
|
|
Collapse(focusNode, focusOffset);
|
|
|
|
}
|
|
|
|
|
2014-11-20 13:45:23 +03:00
|
|
|
// If the paragraph direction of the focused frame is right-to-left,
|
2014-11-22 17:39:02 +03:00
|
|
|
// we may have to swap the direction of movement.
|
2010-04-28 21:47:23 +04:00
|
|
|
nsIFrame *frame;
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t offset;
|
2010-04-28 21:47:23 +04:00
|
|
|
rv = GetPrimaryFrameForFocusNode(&frame, &offset, visual);
|
|
|
|
if (NS_SUCCEEDED(rv) && frame) {
|
2014-11-20 13:45:23 +03:00
|
|
|
nsBidiDirection paraDir = nsBidiPresUtils::ParagraphDirection(frame);
|
2010-04-28 21:47:23 +04:00
|
|
|
|
2014-11-22 17:39:02 +03:00
|
|
|
if (paraDir == NSBIDI_RTL && visual) {
|
|
|
|
if (amount == eSelectBeginLine) {
|
|
|
|
amount = eSelectEndLine;
|
|
|
|
forward = !forward;
|
|
|
|
} else if (amount == eSelectEndLine) {
|
|
|
|
amount = eSelectBeginLine;
|
|
|
|
forward = !forward;
|
2010-04-28 21:47:23 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MoveCaret will return an error if it can't move in the specified
|
|
|
|
// direction, but we just ignore this error unless it's a line move, in which
|
|
|
|
// case we call nsISelectionController::CompleteMove to move the cursor to
|
|
|
|
// the beginning/end of the line.
|
2017-04-06 07:11:52 +03:00
|
|
|
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
|
|
|
|
rv = frameSelection->MoveCaret(forward ? eDirNext : eDirPrevious,
|
|
|
|
extend, amount,
|
|
|
|
visual ? nsFrameSelection::eVisual
|
|
|
|
: nsFrameSelection::eLogical);
|
2010-04-28 21:47:23 +04:00
|
|
|
|
|
|
|
if (aGranularity.LowerCaseEqualsLiteral("line") && NS_FAILED(rv)) {
|
|
|
|
nsCOMPtr<nsISelectionController> shell =
|
2017-04-06 07:11:52 +03:00
|
|
|
do_QueryInterface(frameSelection->GetShell());
|
2010-04-28 21:47:23 +04:00
|
|
|
if (!shell)
|
2013-12-17 18:12:33 +04:00
|
|
|
return;
|
2010-04-28 21:47:23 +04:00
|
|
|
shell->CompleteMove(forward, extend);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
|
|
|
Selection::SetBaseAndExtentJS(nsINode& aAnchorNode,
|
|
|
|
uint32_t aAnchorOffset,
|
|
|
|
nsINode& aFocusNode,
|
|
|
|
uint32_t aFocusOffset,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
AutoRestore<bool> calledFromJSRestorer(mCalledByJS);
|
|
|
|
mCalledByJS = true;
|
|
|
|
SetBaseAndExtent(aAnchorNode, aAnchorOffset,
|
|
|
|
aFocusNode, aFocusOffset, aRv);
|
|
|
|
}
|
|
|
|
|
2016-12-19 18:48:37 +03:00
|
|
|
void
|
|
|
|
Selection::SetBaseAndExtent(nsINode& aAnchorNode, uint32_t aAnchorOffset,
|
|
|
|
nsINode& aFocusNode, uint32_t aFocusOffset,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!mFrameSelection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-10 15:02:08 +03:00
|
|
|
if (!HasSameRoot(aAnchorNode) ||
|
|
|
|
!HasSameRoot(aFocusNode)) {
|
|
|
|
// Return with no error
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-12-19 18:48:37 +03:00
|
|
|
SelectionBatcher batch(this);
|
|
|
|
|
|
|
|
int32_t relativePosition =
|
|
|
|
nsContentUtils::ComparePoints(&aAnchorNode, aAnchorOffset,
|
|
|
|
&aFocusNode, aFocusOffset);
|
|
|
|
nsINode* start = &aAnchorNode;
|
|
|
|
nsINode* end = &aFocusNode;
|
|
|
|
uint32_t startOffset = aAnchorOffset;
|
|
|
|
uint32_t endOffset = aFocusOffset;
|
|
|
|
if (relativePosition > 0) {
|
|
|
|
start = &aFocusNode;
|
|
|
|
end = &aAnchorNode;
|
|
|
|
startOffset = aFocusOffset;
|
|
|
|
endOffset = aAnchorOffset;
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:12:38 +03:00
|
|
|
// If there is cached range, we should reuse it for saving the allocation
|
|
|
|
// const (and some other cost in nsRange::DoSetRange().
|
|
|
|
RefPtr<nsRange> newRange = Move(mCachedRange);
|
|
|
|
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (newRange) {
|
|
|
|
rv = newRange->SetStartAndEnd(start, startOffset, end, endOffset);
|
|
|
|
} else {
|
|
|
|
rv = nsRange::CreateRange(start, startOffset, end, endOffset,
|
|
|
|
getter_AddRefs(newRange));
|
|
|
|
}
|
|
|
|
|
|
|
|
// nsRange::SetStartAndEnd() and nsRange::CreateRange() returns
|
|
|
|
// IndexSizeError if any offset is out of bounds.
|
2016-12-19 18:48:37 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aRv.Throw(rv);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-08-25 18:12:38 +03:00
|
|
|
RemoveAllRanges(aRv);
|
|
|
|
if (aRv.Failed()) {
|
2016-12-19 18:48:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2018-03-27 07:35:22 +03:00
|
|
|
AddRange(*newRange, aRv);
|
|
|
|
if (aRv.Failed()) {
|
2016-12-19 18:48:37 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetDirection(relativePosition > 0 ? eDirPrevious : eDirNext);
|
|
|
|
}
|
|
|
|
|
2001-03-21 04:16:22 +03:00
|
|
|
/** SelectionLanguageChange modifies the cursor Bidi level after a change in keyboard direction
|
2011-10-17 18:59:28 +04:00
|
|
|
* @param aLangRTL is true if the new language is right-to-left or false if the new language is left-to-right
|
2001-03-21 04:16:22 +03:00
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::SelectionLanguageChange(bool aLangRTL)
|
2001-03-21 04:16:22 +03:00
|
|
|
{
|
2006-04-14 04:28:22 +04:00
|
|
|
if (!mFrameSelection)
|
|
|
|
return NS_ERROR_NOT_INITIALIZED; // Can't do selection
|
2015-01-09 12:39:38 +03:00
|
|
|
|
2017-04-06 07:11:52 +03:00
|
|
|
RefPtr<nsFrameSelection> frameSelection = mFrameSelection;
|
|
|
|
|
2015-01-09 12:39:38 +03:00
|
|
|
// if the direction of the language hasn't changed, nothing to do
|
|
|
|
nsBidiLevel kbdBidiLevel = aLangRTL ? NSBIDI_RTL : NSBIDI_LTR;
|
2017-04-06 07:11:52 +03:00
|
|
|
if (kbdBidiLevel == frameSelection->mKbdBidiLevel) {
|
2015-01-09 12:39:38 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-04-06 07:11:52 +03:00
|
|
|
frameSelection->mKbdBidiLevel = kbdBidiLevel;
|
2015-01-09 12:39:38 +03:00
|
|
|
|
2001-03-21 04:16:22 +03:00
|
|
|
nsresult result;
|
|
|
|
nsIFrame *focusFrame = 0;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
result = GetPrimaryFrameForFocusNode(&focusFrame, nullptr, false);
|
2009-04-06 21:17:38 +04:00
|
|
|
if (NS_FAILED(result)) {
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
if (!focusFrame) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2001-03-21 04:16:22 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t frameStart, frameEnd;
|
2001-03-21 04:16:22 +03:00
|
|
|
focusFrame->GetOffsets(frameStart, frameEnd);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsPresContext> context = GetPresContext();
|
2014-11-20 13:45:22 +03:00
|
|
|
nsBidiLevel levelBefore, levelAfter;
|
2012-12-19 22:44:58 +04:00
|
|
|
if (!context) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2001-03-21 04:16:22 +03:00
|
|
|
|
2016-10-06 11:49:14 +03:00
|
|
|
nsBidiLevel level = focusFrame->GetEmbeddingLevel();
|
2013-12-17 18:12:33 +04:00
|
|
|
int32_t focusOffset = static_cast<int32_t>(FocusOffset());
|
2001-03-21 04:16:22 +03:00
|
|
|
if ((focusOffset != frameStart) && (focusOffset != frameEnd))
|
|
|
|
// the cursor is not at a frame boundary, so the level of both the characters (logically) before and after the cursor
|
|
|
|
// is equal to the frame level
|
|
|
|
levelBefore = levelAfter = level;
|
|
|
|
else {
|
|
|
|
// the cursor is at a frame boundary, so use GetPrevNextBidiLevels to find the level of the characters
|
|
|
|
// before and after the cursor
|
2009-04-06 21:17:38 +04:00
|
|
|
nsCOMPtr<nsIContent> focusContent = do_QueryInterface(GetFocusNode());
|
2017-04-06 07:11:52 +03:00
|
|
|
nsPrevNextBidiLevels levels = frameSelection->
|
2011-10-17 18:59:28 +04:00
|
|
|
GetPrevNextBidiLevels(focusContent, focusOffset, false);
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2006-04-26 06:01:07 +04:00
|
|
|
levelBefore = levels.mLevelBefore;
|
|
|
|
levelAfter = levels.mLevelAfter;
|
2001-03-21 04:16:22 +03:00
|
|
|
}
|
|
|
|
|
2014-11-20 13:45:22 +03:00
|
|
|
if (IS_SAME_DIRECTION(levelBefore, levelAfter)) {
|
2001-03-21 04:16:22 +03:00
|
|
|
// if cursor is between two characters with the same orientation, changing the keyboard language
|
|
|
|
// must toggle the cursor level between the level of the character with the lowest level
|
|
|
|
// (if the new language corresponds to the orientation of that character) and this level plus 1
|
|
|
|
// (if the new language corresponds to the opposite orientation)
|
|
|
|
if ((level != levelBefore) && (level != levelAfter))
|
2013-01-15 16:22:03 +04:00
|
|
|
level = std::min(levelBefore, levelAfter);
|
2015-01-09 12:39:38 +03:00
|
|
|
if (IS_SAME_DIRECTION(level, kbdBidiLevel))
|
2017-04-06 07:11:52 +03:00
|
|
|
frameSelection->SetCaretBidiLevel(level);
|
2001-03-21 04:16:22 +03:00
|
|
|
else
|
2017-04-06 07:11:52 +03:00
|
|
|
frameSelection->SetCaretBidiLevel(level + 1);
|
2001-03-21 04:16:22 +03:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
// if cursor is between characters with opposite orientations, changing the keyboard language must change
|
|
|
|
// the cursor level to that of the adjacent character with the orientation corresponding to the new language.
|
2015-01-09 12:39:38 +03:00
|
|
|
if (IS_SAME_DIRECTION(levelBefore, kbdBidiLevel))
|
2017-04-06 07:11:52 +03:00
|
|
|
frameSelection->SetCaretBidiLevel(levelBefore);
|
2001-03-21 04:16:22 +03:00
|
|
|
else
|
2017-04-06 07:11:52 +03:00
|
|
|
frameSelection->SetCaretBidiLevel(levelAfter);
|
2001-03-21 04:16:22 +03:00
|
|
|
}
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2014-11-22 17:39:03 +03:00
|
|
|
// The caret might have moved, so invalidate the desired position
|
2005-09-18 18:18:28 +04:00
|
|
|
// for future usages of up-arrow or down-arrow
|
2017-04-06 07:11:52 +03:00
|
|
|
frameSelection->InvalidateDesiredPos();
|
2017-07-06 15:00:35 +03:00
|
|
|
|
2001-03-21 04:16:22 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2005-02-01 02:47:26 +03:00
|
|
|
|
2017-05-10 17:48:50 +03:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Selection::SetColors(const nsAString& aForegroundColor,
|
|
|
|
const nsAString& aBackgroundColor,
|
|
|
|
const nsAString& aAltForegroundColor,
|
|
|
|
const nsAString& aAltBackgroundColor)
|
|
|
|
{
|
|
|
|
ErrorResult result;
|
|
|
|
SetColors(aForegroundColor, aBackgroundColor,
|
|
|
|
aAltForegroundColor, aAltBackgroundColor, result);
|
|
|
|
return result.StealNSResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Selection::SetColors(const nsAString& aForegroundColor,
|
|
|
|
const nsAString& aBackgroundColor,
|
|
|
|
const nsAString& aAltForegroundColor,
|
|
|
|
const nsAString& aAltBackgroundColor,
|
|
|
|
ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (mSelectionType != SelectionType::eFind) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mCustomColors.reset(new SelectionCustomColors);
|
|
|
|
|
|
|
|
NS_NAMED_LITERAL_STRING(currentColorStr, "currentColor");
|
|
|
|
NS_NAMED_LITERAL_STRING(transparentStr, "transparent");
|
|
|
|
|
|
|
|
if (!aForegroundColor.Equals(currentColorStr)) {
|
|
|
|
nscolor foregroundColor;
|
|
|
|
nsAttrValue aForegroundColorValue;
|
|
|
|
aForegroundColorValue.ParseColor(aForegroundColor);
|
|
|
|
if (!aForegroundColorValue.GetColorValue(foregroundColor)) {
|
|
|
|
aRv.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCustomColors->mForegroundColor = Some(foregroundColor);
|
|
|
|
} else {
|
|
|
|
mCustomColors->mForegroundColor = Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aBackgroundColor.Equals(transparentStr)) {
|
|
|
|
nscolor backgroundColor;
|
|
|
|
nsAttrValue aBackgroundColorValue;
|
|
|
|
aBackgroundColorValue.ParseColor(aBackgroundColor);
|
|
|
|
if (!aBackgroundColorValue.GetColorValue(backgroundColor)) {
|
|
|
|
aRv.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCustomColors->mBackgroundColor = Some(backgroundColor);
|
|
|
|
} else {
|
|
|
|
mCustomColors->mBackgroundColor = Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aAltForegroundColor.Equals(currentColorStr)) {
|
|
|
|
nscolor altForegroundColor;
|
|
|
|
nsAttrValue aAltForegroundColorValue;
|
|
|
|
aAltForegroundColorValue.ParseColor(aAltForegroundColor);
|
|
|
|
if (!aAltForegroundColorValue.GetColorValue(altForegroundColor)) {
|
|
|
|
aRv.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCustomColors->mAltForegroundColor = Some(altForegroundColor);
|
|
|
|
} else {
|
|
|
|
mCustomColors->mAltForegroundColor = Nothing();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!aAltBackgroundColor.Equals(transparentStr)) {
|
|
|
|
nscolor altBackgroundColor;
|
|
|
|
nsAttrValue aAltBackgroundColorValue;
|
|
|
|
aAltBackgroundColorValue.ParseColor(aAltBackgroundColor);
|
|
|
|
if (!aAltBackgroundColorValue.GetColorValue(altBackgroundColor)) {
|
|
|
|
aRv.Throw(NS_ERROR_INVALID_ARG);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mCustomColors->mAltBackgroundColor = Some(altBackgroundColor);
|
|
|
|
} else {
|
|
|
|
mCustomColors->mAltBackgroundColor = Nothing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Selection::ResetColors()
|
|
|
|
{
|
|
|
|
ErrorResult result;
|
|
|
|
ResetColors(result);
|
|
|
|
return result.StealNSResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Selection::ResetColors(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
mCustomColors = nullptr;
|
|
|
|
}
|
|
|
|
|
2011-07-28 21:51:22 +04:00
|
|
|
NS_IMETHODIMP_(nsDirection)
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::GetSelectionDirection() {
|
2011-07-28 21:51:22 +04:00
|
|
|
return mDirection;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
2012-06-08 18:20:55 +04:00
|
|
|
Selection::SetSelectionDirection(nsDirection aDirection) {
|
2011-07-28 21:51:22 +04:00
|
|
|
mDirection = aDirection;
|
|
|
|
}
|
|
|
|
|
2013-12-17 18:12:33 +04:00
|
|
|
JSObject*
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
Selection::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-12-17 18:12:33 +04:00
|
|
|
{
|
Bug 1117172 part 3. Change the wrappercached WrapObject methods to allow passing in aGivenProto. r=peterv
The only manual changes here are to BindingUtils.h, BindingUtils.cpp,
Codegen.py, Element.cpp, IDBFileRequest.cpp, IDBObjectStore.cpp,
dom/workers/Navigator.cpp, WorkerPrivate.cpp, DeviceStorageRequestChild.cpp,
Notification.cpp, nsGlobalWindow.cpp, MessagePort.cpp, nsJSEnvironment.cpp,
Sandbox.cpp, XPCConvert.cpp, ExportHelpers.cpp, and DataStoreService.cpp. The
rest of this diff was generated by running the following commands:
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObjectInternal\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapNode\((?:aCx|cx|aContext|aCtx|js))\)/\1, aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(WrapObject\(JSContext *\* *(?:aCx|cx|aContext|aCtx|js))\)/\1, JS::Handle<JSObject*> aGivenProto)/g'
find . -name "*.h" -o -name "*.cpp" | xargs perl -pi -e 'BEGIN { $/ = undef } s/(Binding(?:_workers)?::Wrap\((?:aCx|cx|aContext|aCtx|js), [^,)]+)\)/\1, aGivenProto)/g'
2015-03-19 17:13:33 +03:00
|
|
|
return mozilla::dom::SelectionBinding::Wrap(aCx, this, aGivenProto);
|
2013-12-17 18:12:33 +04:00
|
|
|
}
|
2005-02-01 02:47:26 +03:00
|
|
|
|
2015-08-12 20:26:01 +03:00
|
|
|
// AutoHideSelectionChanges
|
|
|
|
AutoHideSelectionChanges::AutoHideSelectionChanges(const nsFrameSelection* aFrame)
|
2016-06-11 05:06:37 +03:00
|
|
|
: AutoHideSelectionChanges(
|
2016-06-09 12:35:22 +03:00
|
|
|
aFrame ? aFrame->GetSelection(SelectionType::eNormal) : nullptr)
|
2015-08-12 20:26:01 +03:00
|
|
|
{}
|
2017-08-10 15:02:08 +03:00
|
|
|
|
|
|
|
bool
|
|
|
|
Selection::HasSameRoot(nsINode& aNode)
|
|
|
|
{
|
|
|
|
nsINode* root = aNode.SubtreeRoot();
|
|
|
|
nsIDocument* doc = GetParentObject();
|
|
|
|
return doc == root || (root && doc == root->GetComposedDoc());
|
|
|
|
}
|