2015-05-03 22:32:37 +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/. */
|
1998-11-25 00:20:11 +03:00
|
|
|
|
|
|
|
/*
|
2006-03-31 12:41:49 +04:00
|
|
|
* Implementation of the DOM nsIDOMRange object.
|
1998-11-25 00:20:11 +03:00
|
|
|
*/
|
1998-11-25 00:24:40 +03:00
|
|
|
|
2001-08-14 11:59:59 +04:00
|
|
|
#include "nscore.h"
|
1998-12-18 05:51:34 +03:00
|
|
|
#include "nsRange.h"
|
|
|
|
|
2001-09-29 12:28:41 +04:00
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsReadableUtils.h"
|
1998-12-01 22:18:52 +03:00
|
|
|
#include "nsIDOMNode.h"
|
1998-12-10 21:58:49 +03:00
|
|
|
#include "nsIDOMDocumentFragment.h"
|
1998-12-03 18:02:37 +03:00
|
|
|
#include "nsIContent.h"
|
1999-01-04 19:48:33 +03:00
|
|
|
#include "nsIDocument.h"
|
1998-12-10 01:07:00 +03:00
|
|
|
#include "nsIDOMText.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
1999-02-12 08:28:46 +03:00
|
|
|
#include "nsIContentIterator.h"
|
2018-01-30 07:10:51 +03:00
|
|
|
#include "nsINodeList.h"
|
2006-11-04 00:51:01 +03:00
|
|
|
#include "nsGkAtoms.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
|
|
|
#include "nsContentUtils.h"
|
2008-07-25 14:37:37 +04:00
|
|
|
#include "nsGenericDOMDataNode.h"
|
2009-09-25 01:01:48 +04:00
|
|
|
#include "nsTextFrame.h"
|
2013-02-07 16:09:41 +04:00
|
|
|
#include "mozilla/dom/DocumentFragment.h"
|
2013-07-12 07:29:24 +04:00
|
|
|
#include "mozilla/dom/DocumentType.h"
|
2013-02-07 16:09:41 +04:00
|
|
|
#include "mozilla/dom/RangeBinding.h"
|
2013-09-20 14:21:03 +04:00
|
|
|
#include "mozilla/dom/DOMRect.h"
|
2016-11-02 23:49:43 +03:00
|
|
|
#include "mozilla/dom/DOMStringList.h"
|
2013-12-02 14:26:11 +04:00
|
|
|
#include "mozilla/dom/ShadowRoot.h"
|
2015-08-12 20:26:01 +03:00
|
|
|
#include "mozilla/dom/Selection.h"
|
2012-04-17 18:36:22 +04:00
|
|
|
#include "mozilla/Telemetry.h"
|
2012-10-26 17:32:10 +04:00
|
|
|
#include "mozilla/Likely.h"
|
2013-09-19 08:23:48 +04:00
|
|
|
#include "nsCSSFrameConstructor.h"
|
2015-10-30 08:37:03 +03:00
|
|
|
#include "nsStyleStruct.h"
|
|
|
|
#include "nsStyleStructInlines.h"
|
|
|
|
#include "nsComputedDOMStyle.h"
|
2018-01-11 07:38:01 +03:00
|
|
|
#include "mozilla/dom/InspectorFontFace.h"
|
2012-04-17 18:36:22 +04:00
|
|
|
|
|
|
|
using namespace mozilla;
|
2013-07-12 07:29:24 +04:00
|
|
|
using namespace mozilla::dom;
|
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
|
|
|
|
2013-02-07 16:09:41 +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
|
|
|
nsRange::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
|
2013-02-07 16:09:41 +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 RangeBinding::Wrap(aCx, this, aGivenProto);
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2017-11-27 11:10:27 +03:00
|
|
|
DocGroup*
|
|
|
|
nsRange::GetDocGroup() const
|
|
|
|
{
|
|
|
|
return mOwner ? mOwner->GetDocGroup() : nullptr;
|
|
|
|
}
|
|
|
|
|
1999-07-03 15:14:08 +04:00
|
|
|
/******************************************************
|
|
|
|
* stack based utilty class for managing monitor
|
|
|
|
******************************************************/
|
|
|
|
|
2011-12-24 17:26:03 +04:00
|
|
|
static void InvalidateAllFrames(nsINode* aNode)
|
|
|
|
{
|
|
|
|
NS_PRECONDITION(aNode, "bad arg");
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIFrame* frame = nullptr;
|
2011-12-24 17:26:03 +04:00
|
|
|
switch (aNode->NodeType()) {
|
2018-01-30 07:10:53 +03:00
|
|
|
case nsINode::TEXT_NODE:
|
|
|
|
case nsINode::ELEMENT_NODE:
|
2011-12-24 17:26:03 +04:00
|
|
|
{
|
|
|
|
nsIContent* content = static_cast<nsIContent*>(aNode);
|
|
|
|
frame = content->GetPrimaryFrame();
|
|
|
|
break;
|
|
|
|
}
|
2018-01-30 07:10:53 +03:00
|
|
|
case nsINode::DOCUMENT_NODE:
|
2011-12-24 17:26:03 +04:00
|
|
|
{
|
|
|
|
nsIDocument* doc = static_cast<nsIDocument*>(aNode);
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIPresShell* shell = doc ? doc->GetShell() : nullptr;
|
|
|
|
frame = shell ? shell->GetRootFrame() : nullptr;
|
2011-12-24 17:26:03 +04:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (nsIFrame* f = frame; f; f = f->GetNextContinuation()) {
|
|
|
|
f->InvalidateFrameSubtree();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1999-02-14 12:14:50 +03:00
|
|
|
// Utility routine to detect if a content node is completely contained in a range
|
1999-02-16 18:43:28 +03:00
|
|
|
// If outNodeBefore is returned true, then the node starts before the range does.
|
|
|
|
// If outNodeAfter is returned true, then the node ends after the range does.
|
|
|
|
// Note that both of the above might be true.
|
|
|
|
// If neither are true, the node is contained inside of the range.
|
2017-07-06 15:00:35 +03:00
|
|
|
// XXX - callers responsibility to ensure node in same doc as range!
|
2003-01-15 02:05:52 +03:00
|
|
|
|
|
|
|
// static
|
|
|
|
nsresult
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange::CompareNodeToRange(nsINode* aNode, nsRange* aRange,
|
2011-09-29 10:19:26 +04:00
|
|
|
bool *outNodeBefore, bool *outNodeAfter)
|
1999-02-14 12:14:50 +03:00
|
|
|
{
|
2008-02-24 15:46:09 +03:00
|
|
|
NS_ENSURE_STATE(aNode);
|
1999-02-14 12:14:50 +03:00
|
|
|
// create a pair of dom points that expresses location of node:
|
|
|
|
// NODE(start), NODE(end)
|
|
|
|
// Let incoming range be:
|
|
|
|
// {RANGE(start), RANGE(end)}
|
|
|
|
// if (RANGE(start) <= NODE(start)) and (RANGE(end) => NODE(end))
|
|
|
|
// then the Node is contained (completely) by the Range.
|
2017-07-06 15:00:35 +03:00
|
|
|
|
|
|
|
if (!aRange || !aRange->IsPositioned())
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
1999-02-14 12:14:50 +03:00
|
|
|
// gather up the dom point info
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t nodeStart, nodeEnd;
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* parent = aNode->GetParentNode();
|
2006-10-26 01:53:03 +04:00
|
|
|
if (!parent) {
|
2017-07-06 15:00:35 +03:00
|
|
|
// can't make a parent/offset pair to represent start or
|
2008-10-15 13:40:28 +04:00
|
|
|
// end of the root node, because it has no parent.
|
2006-10-26 01:53:03 +04:00
|
|
|
// so instead represent it by (node,0) and (node,numChildren)
|
|
|
|
parent = aNode;
|
|
|
|
nodeStart = 0;
|
2017-07-19 16:49:52 +03:00
|
|
|
uint32_t childCount = aNode->GetChildCount();
|
|
|
|
MOZ_ASSERT(childCount <= INT32_MAX,
|
|
|
|
"There shouldn't be over INT32_MAX children");
|
|
|
|
nodeEnd = static_cast<int32_t>(childCount);
|
2006-10-26 01:53:03 +04:00
|
|
|
}
|
|
|
|
else {
|
2018-01-23 16:30:18 +03:00
|
|
|
nodeStart = parent->ComputeIndexOf(aNode);
|
2006-10-26 01:53:03 +04:00
|
|
|
nodeEnd = nodeStart + 1;
|
2017-07-19 16:49:52 +03:00
|
|
|
MOZ_ASSERT(nodeStart < nodeEnd, "nodeStart shouldn't be INT32_MAX");
|
2006-10-21 05:30:54 +04:00
|
|
|
}
|
1999-02-14 12:14:50 +03:00
|
|
|
|
2017-07-11 18:02:14 +03:00
|
|
|
nsINode* rangeStartContainer = aRange->GetStartContainer();
|
2017-07-11 18:08:37 +03:00
|
|
|
nsINode* rangeEndContainer = aRange->GetEndContainer();
|
2017-07-19 16:49:52 +03:00
|
|
|
uint32_t rangeStartOffset = aRange->StartOffset();
|
|
|
|
uint32_t rangeEndOffset = aRange->EndOffset();
|
1999-02-14 12:14:50 +03:00
|
|
|
|
|
|
|
// is RANGE(start) <= NODE(start) ?
|
2011-09-29 10:19:26 +04:00
|
|
|
bool disconnected = false;
|
2017-07-19 16:49:52 +03:00
|
|
|
*outNodeBefore =
|
|
|
|
nsContentUtils::ComparePoints(rangeStartContainer,
|
|
|
|
static_cast<int32_t>(rangeStartOffset),
|
|
|
|
parent, nodeStart,
|
|
|
|
&disconnected) > 0;
|
2008-02-24 15:46:09 +03:00
|
|
|
NS_ENSURE_TRUE(!disconnected, NS_ERROR_DOM_WRONG_DOCUMENT_ERR);
|
|
|
|
|
1999-02-14 12:14:50 +03:00
|
|
|
// is RANGE(end) >= NODE(end) ?
|
2017-07-19 16:49:52 +03:00
|
|
|
*outNodeAfter =
|
|
|
|
nsContentUtils::ComparePoints(rangeEndContainer,
|
|
|
|
static_cast<int32_t>(rangeEndOffset),
|
|
|
|
parent, nodeEnd,
|
|
|
|
&disconnected) < 0;
|
2008-02-24 15:46:09 +03:00
|
|
|
NS_ENSURE_TRUE(!disconnected, NS_ERROR_DOM_WRONG_DOCUMENT_ERR);
|
1999-02-16 18:43:28 +03:00
|
|
|
return NS_OK;
|
1999-02-14 12:14:50 +03:00
|
|
|
}
|
|
|
|
|
2011-12-20 13:15:41 +04:00
|
|
|
static nsINode*
|
|
|
|
GetNextRangeCommonAncestor(nsINode* aNode)
|
|
|
|
{
|
|
|
|
while (aNode && !aNode->IsCommonAncestorForRangeInSelection()) {
|
|
|
|
if (!aNode->IsDescendantOfCommonAncestorForRangeInSelection()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
2012-10-09 16:31:24 +04:00
|
|
|
aNode = aNode->GetParentNode();
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
|
|
|
return aNode;
|
|
|
|
}
|
|
|
|
|
2016-02-13 20:40:23 +03:00
|
|
|
/**
|
|
|
|
* A Comparator suitable for mozilla::BinarySearchIf for searching a collection
|
|
|
|
* of nsRange* for an overlap of (mNode, mStartOffset) .. (mNode, mEndOffset).
|
|
|
|
*/
|
|
|
|
struct IsItemInRangeComparator
|
|
|
|
{
|
|
|
|
nsINode* mNode;
|
|
|
|
uint32_t mStartOffset;
|
|
|
|
uint32_t mEndOffset;
|
|
|
|
|
|
|
|
int operator()(const nsRange* const aRange) const
|
|
|
|
{
|
2017-07-19 16:49:52 +03:00
|
|
|
int32_t cmp =
|
|
|
|
nsContentUtils::ComparePoints(
|
|
|
|
mNode, static_cast<int32_t>(mEndOffset),
|
|
|
|
aRange->GetStartContainer(),
|
|
|
|
static_cast<int32_t>(aRange->StartOffset()));
|
2016-02-13 20:40:23 +03:00
|
|
|
if (cmp == 1) {
|
2017-07-19 16:49:52 +03:00
|
|
|
cmp =
|
|
|
|
nsContentUtils::ComparePoints(
|
|
|
|
mNode, static_cast<int32_t>(mStartOffset),
|
|
|
|
aRange->GetEndContainer(),
|
|
|
|
static_cast<int32_t>(aRange->EndOffset()));
|
2016-02-13 20:40:23 +03:00
|
|
|
if (cmp == -1) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2011-12-20 13:15:41 +04:00
|
|
|
/* static */ bool
|
2012-08-22 19:56:38 +04:00
|
|
|
nsRange::IsNodeSelected(nsINode* aNode, uint32_t aStartOffset,
|
|
|
|
uint32_t aEndOffset)
|
2011-12-20 13:15:41 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aNode, "bad arg");
|
|
|
|
|
|
|
|
nsINode* n = GetNextRangeCommonAncestor(aNode);
|
|
|
|
NS_ASSERTION(n || !aNode->IsSelectionDescendant(),
|
|
|
|
"orphan selection descendant");
|
2016-02-13 20:40:23 +03:00
|
|
|
|
2017-12-02 01:53:20 +03:00
|
|
|
// Collect the selection objects for potential ranges.
|
2016-02-13 20:40:23 +03:00
|
|
|
nsTHashtable<nsPtrHashKey<Selection>> ancestorSelections;
|
2017-12-02 01:53:20 +03:00
|
|
|
Selection* prevSelection = nullptr;
|
2016-02-13 20:40:23 +03:00
|
|
|
uint32_t maxRangeCount = 0;
|
2012-10-09 16:31:24 +04:00
|
|
|
for (; n; n = GetNextRangeCommonAncestor(n->GetParentNode())) {
|
2017-09-01 18:13:47 +03:00
|
|
|
LinkedList<nsRange>* ranges = n->GetExistingCommonAncestorRanges();
|
2017-07-22 08:29:07 +03:00
|
|
|
if (!ranges) {
|
|
|
|
continue;
|
|
|
|
}
|
2017-09-01 18:13:47 +03:00
|
|
|
for (nsRange* range : *ranges) {
|
|
|
|
MOZ_ASSERT(range->IsInSelection(),
|
|
|
|
"Why is this range registeed with a node?");
|
2017-09-13 23:53:45 +03:00
|
|
|
// Looks like that IsInSelection() assert fails sometimes...
|
2017-12-02 01:53:20 +03:00
|
|
|
if (range->IsInSelection()) {
|
2016-02-13 20:40:23 +03:00
|
|
|
Selection* selection = range->mSelection;
|
2017-12-02 01:53:20 +03:00
|
|
|
if (prevSelection != selection) {
|
|
|
|
prevSelection = selection;
|
|
|
|
ancestorSelections.PutEntry(selection);
|
|
|
|
}
|
2016-02-13 20:40:23 +03:00
|
|
|
maxRangeCount = std::max(maxRangeCount, selection->RangeCount());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-02 01:53:20 +03:00
|
|
|
IsItemInRangeComparator comparator = { aNode, aStartOffset, aEndOffset };
|
|
|
|
if (!ancestorSelections.IsEmpty()) {
|
2016-02-13 20:40:23 +03:00
|
|
|
for (auto iter = ancestorSelections.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
Selection* selection = iter.Get()->GetKey();
|
2017-12-02 01:53:20 +03:00
|
|
|
// Binary search the sorted ranges in this selection.
|
2016-02-13 20:40:23 +03:00
|
|
|
// (Selection::GetRangeAt returns its ranges ordered).
|
2017-12-02 01:53:20 +03:00
|
|
|
size_t low = 0;
|
|
|
|
size_t high = selection->RangeCount();
|
|
|
|
|
|
|
|
while (high != low) {
|
|
|
|
size_t middle = low + (high - low) / 2;
|
|
|
|
|
|
|
|
const nsRange* const range = selection->GetRangeAt(middle);
|
|
|
|
int result = comparator(range);
|
|
|
|
if (result == 0) {
|
|
|
|
if (!range->Collapsed())
|
|
|
|
return true;
|
|
|
|
|
|
|
|
const nsRange* middlePlus1;
|
|
|
|
const nsRange* middleMinus1;
|
|
|
|
// if node end > start of middle+1, result = 1
|
|
|
|
if (middle + 1 < high &&
|
|
|
|
(middlePlus1 = selection->GetRangeAt(middle + 1)) &&
|
|
|
|
nsContentUtils::ComparePoints(
|
|
|
|
aNode, static_cast<int32_t>(aEndOffset),
|
|
|
|
middlePlus1->GetStartContainer(),
|
|
|
|
static_cast<int32_t>(middlePlus1->StartOffset())) > 0) {
|
|
|
|
result = 1;
|
|
|
|
// if node start < end of middle - 1, result = -1
|
|
|
|
} else if (middle >= 1 &&
|
|
|
|
(middleMinus1 = selection->GetRangeAt(middle - 1)) &&
|
|
|
|
nsContentUtils::ComparePoints(
|
|
|
|
aNode, static_cast<int32_t>(aStartOffset),
|
|
|
|
middleMinus1->GetEndContainer(),
|
|
|
|
static_cast<int32_t>(middleMinus1->EndOffset())) < 0) {
|
|
|
|
result = -1;
|
|
|
|
} else {
|
2017-12-14 23:29:59 +03:00
|
|
|
break;
|
2017-12-02 01:53:20 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result < 0) {
|
|
|
|
high = middle;
|
|
|
|
} else {
|
|
|
|
low = middle + 1;
|
2015-07-22 19:42:01 +03:00
|
|
|
}
|
|
|
|
}
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
1998-12-03 18:02:37 +03:00
|
|
|
/******************************************************
|
|
|
|
* constructor/destructor
|
|
|
|
******************************************************/
|
1999-10-09 00:41:19 +04:00
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
nsRange::~nsRange()
|
1998-12-04 01:59:07 +03:00
|
|
|
{
|
2011-12-20 13:15:41 +04:00
|
|
|
NS_ASSERTION(!IsInSelection(), "deleting nsRange that is in use");
|
|
|
|
|
1999-02-14 12:14:50 +03:00
|
|
|
// we want the side effects (releases and list removals)
|
2017-12-05 11:50:13 +03:00
|
|
|
DoSetRange(RawRangeBoundary(), RawRangeBoundary(), nullptr);
|
2013-07-12 07:29:24 +04:00
|
|
|
}
|
1998-11-25 04:21:42 +03:00
|
|
|
|
2015-08-12 20:26:01 +03:00
|
|
|
nsRange::nsRange(nsINode* aNode)
|
|
|
|
: mRoot(nullptr)
|
2017-09-01 18:13:25 +03:00
|
|
|
, mRegisteredCommonAncestor(nullptr)
|
2017-08-18 01:05:40 +03:00
|
|
|
, mNextStartRef(nullptr)
|
|
|
|
, mNextEndRef(nullptr)
|
2015-08-12 20:26:01 +03:00
|
|
|
, mIsPositioned(false)
|
|
|
|
, mMaySpanAnonymousSubtrees(false)
|
|
|
|
, mIsGenerated(false)
|
2017-03-10 10:55:12 +03:00
|
|
|
, mCalledByJS(false)
|
2015-08-12 20:26:01 +03:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNode, "range isn't in a document!");
|
|
|
|
mOwner = aNode->OwnerDoc();
|
|
|
|
}
|
|
|
|
|
2013-04-17 01:12:03 +04:00
|
|
|
/* static */
|
|
|
|
nsresult
|
2017-07-19 16:49:52 +03:00
|
|
|
nsRange::CreateRange(nsINode* aStartContainer, uint32_t aStartOffset,
|
|
|
|
nsINode* aEndParent, uint32_t aEndOffset,
|
2013-04-17 01:12:03 +04:00
|
|
|
nsRange** aRange)
|
|
|
|
{
|
2017-05-30 07:18:25 +03:00
|
|
|
MOZ_ASSERT(aRange);
|
|
|
|
*aRange = nullptr;
|
2013-04-17 01:12:03 +04:00
|
|
|
|
2017-07-11 17:10:42 +03:00
|
|
|
RefPtr<nsRange> range = new nsRange(aStartContainer);
|
|
|
|
nsresult rv = range->SetStartAndEnd(aStartContainer, aStartOffset,
|
2017-05-30 07:18:25 +03:00
|
|
|
aEndParent, aEndOffset);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
range.forget(aRange);
|
|
|
|
return NS_OK;
|
2013-04-17 01:12:03 +04:00
|
|
|
}
|
|
|
|
|
2012-01-15 12:13:12 +04:00
|
|
|
/* static */
|
|
|
|
nsresult
|
2017-07-19 16:49:52 +03:00
|
|
|
nsRange::CreateRange(nsIDOMNode* aStartContainer, uint32_t aStartOffset,
|
|
|
|
nsIDOMNode* aEndParent, uint32_t aEndOffset,
|
2012-01-15 12:13:12 +04:00
|
|
|
nsRange** aRange)
|
|
|
|
{
|
2017-07-11 18:02:14 +03:00
|
|
|
nsCOMPtr<nsINode> startContainer = do_QueryInterface(aStartContainer);
|
2017-07-11 18:08:37 +03:00
|
|
|
nsCOMPtr<nsINode> endContainer = do_QueryInterface(aEndParent);
|
2017-07-11 18:02:14 +03:00
|
|
|
return CreateRange(startContainer, aStartOffset,
|
2017-07-11 18:08:37 +03:00
|
|
|
endContainer, aEndOffset, aRange);
|
2012-01-15 12:13:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
nsresult
|
2017-07-19 16:49:52 +03:00
|
|
|
nsRange::CreateRange(nsIDOMNode* aStartContainer, uint32_t aStartOffset,
|
|
|
|
nsIDOMNode* aEndParent, uint32_t aEndOffset,
|
2012-01-15 12:13:12 +04:00
|
|
|
nsIDOMRange** aRange)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRange> range;
|
2017-07-11 17:10:42 +03:00
|
|
|
nsresult rv = nsRange::CreateRange(aStartContainer, aStartOffset, aEndParent,
|
2012-01-15 12:13:12 +04:00
|
|
|
aEndOffset, getter_AddRefs(range));
|
|
|
|
range.forget(aRange);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-10-02 17:58:26 +03:00
|
|
|
/* static */
|
|
|
|
nsresult
|
|
|
|
nsRange::CreateRange(const RawRangeBoundary& aStart,
|
|
|
|
const RawRangeBoundary& aEnd,
|
|
|
|
nsRange** aRange)
|
|
|
|
{
|
|
|
|
RefPtr<nsRange> range = new nsRange(aStart.Container());
|
|
|
|
nsresult rv = range->SetStartAndEnd(aStart, aEnd);
|
|
|
|
if (NS_WARN_IF(NS_FAILED(rv))) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
range.forget(aRange);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1998-12-03 18:02:37 +03:00
|
|
|
/******************************************************
|
1999-10-09 00:41:19 +04:00
|
|
|
* nsISupports
|
1998-12-03 18:02:37 +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
|
|
|
|
2017-08-22 17:47:00 +03:00
|
|
|
NS_IMPL_MAIN_THREAD_ONLY_CYCLE_COLLECTING_ADDREF(nsRange)
|
2017-08-18 01:05:40 +03:00
|
|
|
NS_IMPL_MAIN_THREAD_ONLY_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(
|
|
|
|
nsRange, DoSetRange(RawRangeBoundary(), RawRangeBoundary(), nullptr))
|
2008-12-03 13:03:10 +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
|
|
|
// QueryInterface implementation for nsRange
|
2008-12-03 13:03:10 +03:00
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsRange)
|
2013-02-07 16:09:41 +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(nsIDOMRange)
|
2006-11-04 00:51:01 +03:00
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIMutationObserver)
|
2012-01-10 18:19:54 +04:00
|
|
|
NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIDOMRange)
|
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_END
|
|
|
|
|
2013-08-02 05:29:05 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(nsRange)
|
|
|
|
|
2008-12-03 13:03:10 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(nsRange)
|
2013-02-07 16:09:41 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner);
|
2017-09-01 18:13:47 +03:00
|
|
|
|
|
|
|
// We _could_ just rely on Reset() to UnregisterCommonAncestor(),
|
|
|
|
// but it wouldn't know we're calling it from Unlink and so would do
|
|
|
|
// more work than it really needs to.
|
|
|
|
if (tmp->mRegisteredCommonAncestor) {
|
|
|
|
tmp->UnregisterCommonAncestor(tmp->mRegisteredCommonAncestor, true);
|
|
|
|
}
|
|
|
|
|
2008-12-03 13:03:10 +03:00
|
|
|
tmp->Reset();
|
2015-08-12 20:26:01 +03:00
|
|
|
|
|
|
|
// This needs to be unlinked after Reset() is called, as it controls
|
|
|
|
// the result of IsInSelection() which is used by tmp->Reset().
|
2017-09-13 23:53:45 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!tmp->isInList(),
|
|
|
|
"Shouldn't be registered now that we're unlinking");
|
2015-08-12 20:26:01 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mSelection);
|
2008-12-03 13:03:10 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(nsRange)
|
2013-02-07 16:09:41 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner)
|
2017-09-08 00:05:51 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStart)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEnd)
|
2012-11-15 11:32:40 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRoot)
|
2015-08-12 20:26:01 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mSelection)
|
2008-12-03 13:03:10 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
1998-11-25 04:21:42 +03:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(nsRange)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_END
|
|
|
|
|
2011-12-20 13:15:41 +04:00
|
|
|
static void MarkDescendants(nsINode* aNode)
|
|
|
|
{
|
|
|
|
// Set NodeIsDescendantOfCommonAncestorForRangeInSelection on aNode's
|
|
|
|
// descendants unless aNode is already marked as a range common ancestor
|
|
|
|
// or a descendant of one, in which case all of our descendants have the
|
|
|
|
// bit set already.
|
|
|
|
if (!aNode->IsSelectionDescendant()) {
|
|
|
|
// don't set the Descendant bit on |aNode| itself
|
|
|
|
nsINode* node = aNode->GetNextNode(aNode);
|
|
|
|
while (node) {
|
|
|
|
node->SetDescendantOfCommonAncestorForRangeInSelection();
|
|
|
|
if (!node->IsCommonAncestorForRangeInSelection()) {
|
|
|
|
node = node->GetNextNode(aNode);
|
|
|
|
} else {
|
|
|
|
// optimize: skip this sub-tree since it's marked already.
|
|
|
|
node = node->GetNextNonChildNode(aNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void UnmarkDescendants(nsINode* aNode)
|
|
|
|
{
|
|
|
|
// Unset NodeIsDescendantOfCommonAncestorForRangeInSelection on aNode's
|
|
|
|
// descendants unless aNode is a descendant of another range common ancestor.
|
|
|
|
// Also, exclude descendants of range common ancestors (but not the common
|
|
|
|
// ancestor itself).
|
|
|
|
if (!aNode->IsDescendantOfCommonAncestorForRangeInSelection()) {
|
|
|
|
// we know |aNode| doesn't have any bit set
|
|
|
|
nsINode* node = aNode->GetNextNode(aNode);
|
|
|
|
while (node) {
|
|
|
|
node->ClearDescendantOfCommonAncestorForRangeInSelection();
|
|
|
|
if (!node->IsCommonAncestorForRangeInSelection()) {
|
|
|
|
node = node->GetNextNode(aNode);
|
|
|
|
} else {
|
|
|
|
// We found an ancestor of an overlapping range, skip its descendants.
|
|
|
|
node = node->GetNextNonChildNode(aNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange::RegisterCommonAncestor(nsINode* aNode)
|
2011-12-20 13:15:41 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aNode, "bad arg");
|
2017-09-13 23:53:45 +03:00
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(IsInSelection(), "registering range not in selection");
|
2011-12-20 13:15:41 +04:00
|
|
|
|
2017-09-01 18:13:25 +03:00
|
|
|
mRegisteredCommonAncestor = aNode;
|
|
|
|
|
2011-12-20 13:15:41 +04:00
|
|
|
MarkDescendants(aNode);
|
|
|
|
|
2017-09-01 18:13:47 +03:00
|
|
|
UniquePtr<LinkedList<nsRange>>& ranges = aNode->GetCommonAncestorRangesPtr();
|
2011-12-20 13:15:41 +04:00
|
|
|
if (!ranges) {
|
2017-09-01 18:13:47 +03:00
|
|
|
ranges = MakeUnique<LinkedList<nsRange>>();
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
2017-10-09 16:13:18 +03:00
|
|
|
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!isInList());
|
2017-09-01 18:13:47 +03:00
|
|
|
ranges->insertBack(this);
|
2011-12-20 13:15:41 +04:00
|
|
|
aNode->SetCommonAncestorForRangeInSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2017-09-01 18:13:47 +03:00
|
|
|
nsRange::UnregisterCommonAncestor(nsINode* aNode, bool aIsUnlinking)
|
2011-12-20 13:15:41 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aNode, "bad arg");
|
|
|
|
NS_ASSERTION(aNode->IsCommonAncestorForRangeInSelection(), "wrong node");
|
2017-09-01 18:13:25 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(aNode == mRegisteredCommonAncestor, "wrong node");
|
2017-09-01 18:13:47 +03:00
|
|
|
LinkedList<nsRange>* ranges = aNode->GetExistingCommonAncestorRanges();
|
2017-07-22 08:29:07 +03:00
|
|
|
MOZ_ASSERT(ranges);
|
2011-12-20 13:15:41 +04:00
|
|
|
|
2017-09-01 18:13:25 +03:00
|
|
|
mRegisteredCommonAncestor = nullptr;
|
|
|
|
|
2017-09-01 18:13:47 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
bool found = false;
|
|
|
|
for (nsRange* range : *ranges) {
|
|
|
|
if (range == this) {
|
|
|
|
found = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(found,
|
|
|
|
"We should be in the list on our registered common ancestor");
|
|
|
|
#endif // DEBUG
|
|
|
|
|
|
|
|
remove();
|
2017-07-28 21:31:47 +03:00
|
|
|
|
2017-09-01 18:13:47 +03:00
|
|
|
// We don't want to waste time unmarking flags on nodes that are
|
|
|
|
// being unlinked anyway.
|
|
|
|
if (!aIsUnlinking && ranges->isEmpty()) {
|
2011-12-20 13:15:41 +04:00
|
|
|
aNode->ClearCommonAncestorForRangeInSelection();
|
|
|
|
UnmarkDescendants(aNode);
|
2017-07-28 21:31:47 +03:00
|
|
|
}
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
|
|
|
|
2006-11-04 00:51:01 +03:00
|
|
|
/******************************************************
|
|
|
|
* nsIMutationObserver implementation
|
|
|
|
******************************************************/
|
|
|
|
void
|
|
|
|
nsRange::CharacterDataChanged(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
2017-08-18 01:05:40 +03:00
|
|
|
MOZ_ASSERT(!mNextEndRef);
|
|
|
|
MOZ_ASSERT(!mNextStartRef);
|
|
|
|
MOZ_ASSERT(mIsPositioned, "shouldn't be notified if not positioned");
|
2006-11-04 00:51:01 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsINode* newRoot = nullptr;
|
2017-08-18 01:05:40 +03:00
|
|
|
RawRangeBoundary newStart;
|
|
|
|
RawRangeBoundary newEnd;
|
2011-09-24 04:56:38 +04:00
|
|
|
|
2012-11-21 00:14:15 +04:00
|
|
|
if (aInfo->mDetails &&
|
|
|
|
aInfo->mDetails->mType == CharacterDataChangeInfo::Details::eSplit) {
|
|
|
|
// If the splitted text node is immediately before a range boundary point
|
|
|
|
// that refers to a child index (i.e. its parent is the boundary container)
|
2017-08-18 01:05:40 +03:00
|
|
|
// then we need to adjust the corresponding boundary to account for the new
|
|
|
|
// text node that will be inserted. However, because the new sibling hasn't
|
|
|
|
// been inserted yet, that would result in an invalid boundary. Therefore,
|
|
|
|
// we store the new child in mNext*Ref to make sure we adjust the boundary
|
|
|
|
// in the next ContentInserted or ContentAppended call.
|
2012-11-21 00:14:15 +04:00
|
|
|
nsINode* parentNode = aContent->GetParentNode();
|
2017-08-18 01:05:40 +03:00
|
|
|
if (parentNode == mEnd.Container()) {
|
|
|
|
if (aContent == mEnd.Ref()) {
|
|
|
|
MOZ_ASSERT(aInfo->mDetails->mNextSibling);
|
|
|
|
mNextEndRef = aInfo->mDetails->mNextSibling;
|
2017-07-19 16:49:52 +03:00
|
|
|
}
|
2012-11-21 00:14:15 +04:00
|
|
|
}
|
2017-08-18 01:05:40 +03:00
|
|
|
|
|
|
|
if (parentNode == mStart.Container()) {
|
|
|
|
if (aContent == mStart.Ref()) {
|
|
|
|
MOZ_ASSERT(aInfo->mDetails->mNextSibling);
|
|
|
|
mNextStartRef = aInfo->mDetails->mNextSibling;
|
2017-07-19 16:49:52 +03:00
|
|
|
}
|
2012-11-21 00:14:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-10 16:04:19 +03:00
|
|
|
// If the changed node contains our start boundary and the change starts
|
|
|
|
// before the boundary we'll need to adjust the offset.
|
2017-08-18 01:05:40 +03:00
|
|
|
if (aContent == mStart.Container() && aInfo->mChangeStart < mStart.Offset()) {
|
2011-08-16 04:55:20 +04:00
|
|
|
if (aInfo->mDetails) {
|
|
|
|
// splitText(), aInfo->mDetails->mNextSibling is the new text node
|
|
|
|
NS_ASSERTION(aInfo->mDetails->mType ==
|
|
|
|
CharacterDataChangeInfo::Details::eSplit,
|
|
|
|
"only a split can start before the end");
|
2017-08-18 01:05:40 +03:00
|
|
|
NS_ASSERTION(mStart.Offset() <= aInfo->mChangeEnd + 1,
|
|
|
|
"mStart.Offset() is beyond the end of this node");
|
|
|
|
int32_t newStartOffset = mStart.Offset() - aInfo->mChangeStart;
|
|
|
|
newStart.Set(aInfo->mDetails->mNextSibling, newStartOffset);
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(aContent == mRoot)) {
|
2017-08-18 01:05:40 +03:00
|
|
|
newRoot = IsValidBoundary(newStart.Container());
|
2011-09-24 04:56:38 +04:00
|
|
|
}
|
2011-12-20 13:15:41 +04:00
|
|
|
|
2017-07-11 16:57:55 +03:00
|
|
|
bool isCommonAncestor =
|
2017-08-18 01:05:40 +03:00
|
|
|
IsInSelection() && mStart.Container() == mEnd.Container();
|
2011-12-20 13:15:41 +04:00
|
|
|
if (isCommonAncestor) {
|
2017-09-01 18:13:47 +03:00
|
|
|
UnregisterCommonAncestor(mStart.Container(), false);
|
2017-08-18 01:05:40 +03:00
|
|
|
RegisterCommonAncestor(newStart.Container());
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
2017-08-18 01:05:40 +03:00
|
|
|
if (mStart.Container()->IsDescendantOfCommonAncestorForRangeInSelection()) {
|
|
|
|
newStart.Container()->SetDescendantOfCommonAncestorForRangeInSelection();
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
2011-08-16 04:55:20 +04:00
|
|
|
} else {
|
|
|
|
// If boundary is inside changed text, position it before change
|
|
|
|
// else adjust start offset for the change in length.
|
2017-08-18 01:05:40 +03:00
|
|
|
int32_t newStartOffset = mStart.Offset() <= aInfo->mChangeEnd ?
|
2011-08-16 04:55:20 +04:00
|
|
|
aInfo->mChangeStart :
|
2017-08-18 01:05:40 +03:00
|
|
|
mStart.Offset() + aInfo->mChangeStart - aInfo->mChangeEnd +
|
2011-08-16 04:55:20 +04:00
|
|
|
aInfo->mReplaceLength;
|
2017-08-18 01:05:40 +03:00
|
|
|
newStart.Set(mStart.Container(), newStartOffset);
|
2011-08-16 04:55:20 +04:00
|
|
|
}
|
2006-11-04 00:51:01 +03:00
|
|
|
}
|
|
|
|
|
2011-09-24 04:56:38 +04:00
|
|
|
// Do the same thing for the end boundary, except for splitText of a node
|
|
|
|
// with no parent then only switch to the new node if the start boundary
|
|
|
|
// did so too (otherwise the range would end up with disconnected nodes).
|
2017-08-18 01:05:40 +03:00
|
|
|
if (aContent == mEnd.Container() && aInfo->mChangeStart < mEnd.Offset()) {
|
|
|
|
if (aInfo->mDetails && (aContent->GetParentNode() || newStart.Container())) {
|
2011-08-16 04:55:20 +04:00
|
|
|
// splitText(), aInfo->mDetails->mNextSibling is the new text node
|
|
|
|
NS_ASSERTION(aInfo->mDetails->mType ==
|
|
|
|
CharacterDataChangeInfo::Details::eSplit,
|
|
|
|
"only a split can start before the end");
|
2017-08-18 01:05:40 +03:00
|
|
|
NS_ASSERTION(mEnd.Offset() <= aInfo->mChangeEnd + 1,
|
|
|
|
"mEnd.Offset() is beyond the end of this node");
|
|
|
|
newEnd.Set(aInfo->mDetails->mNextSibling, mEnd.Offset() - aInfo->mChangeStart);
|
2011-12-20 13:15:41 +04:00
|
|
|
|
2017-07-11 16:57:55 +03:00
|
|
|
bool isCommonAncestor =
|
2017-08-18 01:05:40 +03:00
|
|
|
IsInSelection() && mStart.Container() == mEnd.Container();
|
|
|
|
if (isCommonAncestor && !newStart.Container()) {
|
2011-12-20 13:15:41 +04:00
|
|
|
// The split occurs inside the range.
|
2017-09-01 18:13:47 +03:00
|
|
|
UnregisterCommonAncestor(mStart.Container(), false);
|
2017-08-18 01:05:40 +03:00
|
|
|
RegisterCommonAncestor(mStart.Container()->GetParentNode());
|
|
|
|
newEnd.Container()->SetDescendantOfCommonAncestorForRangeInSelection();
|
|
|
|
} else if (mEnd.Container()->
|
2017-07-11 16:57:55 +03:00
|
|
|
IsDescendantOfCommonAncestorForRangeInSelection()) {
|
2017-08-18 01:05:40 +03:00
|
|
|
newEnd.Container()->SetDescendantOfCommonAncestorForRangeInSelection();
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
2011-08-16 04:55:20 +04:00
|
|
|
} else {
|
2017-08-18 01:05:40 +03:00
|
|
|
int32_t newEndOffset = mEnd.Offset() <= aInfo->mChangeEnd ?
|
2011-08-16 04:55:20 +04:00
|
|
|
aInfo->mChangeStart :
|
2017-08-18 01:05:40 +03:00
|
|
|
mEnd.Offset() + aInfo->mChangeStart - aInfo->mChangeEnd +
|
2011-08-16 04:55:20 +04:00
|
|
|
aInfo->mReplaceLength;
|
2017-08-18 01:05:40 +03:00
|
|
|
newEnd.Set(mEnd.Container(), newEndOffset);
|
2011-08-16 04:55:20 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aInfo->mDetails &&
|
|
|
|
aInfo->mDetails->mType == CharacterDataChangeInfo::Details::eMerge) {
|
|
|
|
// normalize(), aInfo->mDetails->mNextSibling is the merged text node
|
|
|
|
// that will be removed
|
|
|
|
nsIContent* removed = aInfo->mDetails->mNextSibling;
|
2017-08-18 01:05:40 +03:00
|
|
|
if (removed == mStart.Container()) {
|
|
|
|
newStart.Set(aContent, mStart.Offset() + aInfo->mChangeStart);
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(removed == mRoot)) {
|
2017-08-18 01:05:40 +03:00
|
|
|
newRoot = IsValidBoundary(newStart.Container());
|
2011-09-24 04:56:38 +04:00
|
|
|
}
|
2011-08-16 04:55:20 +04:00
|
|
|
}
|
2017-08-18 01:05:40 +03:00
|
|
|
if (removed == mEnd.Container()) {
|
|
|
|
newEnd.Set(aContent, mEnd.Offset() + aInfo->mChangeStart);
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(removed == mRoot)) {
|
2017-08-18 01:05:40 +03:00
|
|
|
newRoot = IsValidBoundary(newEnd.Container());
|
2011-09-24 04:56:38 +04:00
|
|
|
}
|
|
|
|
}
|
2012-11-21 00:14:15 +04:00
|
|
|
// When the removed text node's parent is one of our boundary nodes we may
|
|
|
|
// need to adjust the offset to account for the removed node. However,
|
|
|
|
// there will also be a ContentRemoved notification later so the only cases
|
|
|
|
// we need to handle here is when the removed node is the text node after
|
|
|
|
// the boundary. (The m*Offset > 0 check is an optimization - a boundary
|
|
|
|
// point before the first child is never affected by normalize().)
|
|
|
|
nsINode* parentNode = aContent->GetParentNode();
|
2017-08-18 01:05:40 +03:00
|
|
|
if (parentNode == mStart.Container() && mStart.Offset() > 0 &&
|
|
|
|
mStart.Offset() < parentNode->GetChildCount() &&
|
|
|
|
removed == mStart.GetChildAtOffset()) {
|
|
|
|
newStart.Set(aContent, aInfo->mChangeStart);
|
2012-11-21 00:14:15 +04:00
|
|
|
}
|
2017-08-18 01:05:40 +03:00
|
|
|
if (parentNode == mEnd.Container() && mEnd.Offset() > 0 &&
|
|
|
|
mEnd.Offset() < parentNode->GetChildCount() &&
|
|
|
|
removed == mEnd.GetChildAtOffset()) {
|
|
|
|
newEnd.Set(aContent, aInfo->mChangeEnd);
|
2012-11-21 00:14:15 +04:00
|
|
|
}
|
2011-09-24 04:56:38 +04:00
|
|
|
}
|
2012-11-21 00:14:15 +04:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
if (newStart.IsSet() || newEnd.IsSet()) {
|
|
|
|
if (!newStart.IsSet()) {
|
|
|
|
newStart = mStart;
|
2011-09-24 04:56:38 +04:00
|
|
|
}
|
2017-08-18 01:05:40 +03:00
|
|
|
if (!newEnd.IsSet()) {
|
|
|
|
newEnd = mEnd;
|
2011-08-16 04:55:20 +04:00
|
|
|
}
|
2017-08-18 01:05:40 +03:00
|
|
|
DoSetRange(newStart, newEnd,
|
2011-12-20 13:15:41 +04:00
|
|
|
newRoot ? newRoot : mRoot.get(),
|
2017-08-18 01:05:40 +03:00
|
|
|
!newEnd.Container()->GetParentNode() || !newStart.Container()->GetParentNode());
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRange::ContentAppended(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
2017-07-27 16:49:52 +03:00
|
|
|
nsIContent* aFirstNewContent)
|
2011-12-20 13:15:41 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mIsPositioned, "shouldn't be notified if not positioned");
|
|
|
|
|
|
|
|
nsINode* container = NODE_FROM(aContainer, aDocument);
|
2017-08-18 01:05:40 +03:00
|
|
|
MOZ_ASSERT(container);
|
|
|
|
MOZ_ASSERT(aFirstNewContent->GetParentNode() == container);
|
2011-12-20 13:15:41 +04:00
|
|
|
if (container->IsSelectionDescendant() && IsInSelection()) {
|
|
|
|
nsINode* child = aFirstNewContent;
|
|
|
|
while (child) {
|
|
|
|
if (!child->IsDescendantOfCommonAncestorForRangeInSelection()) {
|
|
|
|
MarkDescendants(child);
|
|
|
|
child->SetDescendantOfCommonAncestorForRangeInSelection();
|
|
|
|
}
|
|
|
|
child = child->GetNextSibling();
|
|
|
|
}
|
2006-11-04 00:51:01 +03:00
|
|
|
}
|
2012-11-21 00:14:15 +04:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
if (mNextStartRef || mNextEndRef) {
|
|
|
|
// A splitText has occurred, if any mNext*Ref was set, we need to adjust
|
|
|
|
// the range boundaries.
|
|
|
|
if (mNextStartRef) {
|
|
|
|
mStart.SetAfterRef(mStart.Container(), mNextStartRef);
|
|
|
|
MOZ_ASSERT(mNextStartRef == aFirstNewContent);
|
|
|
|
mNextStartRef = nullptr;
|
|
|
|
}
|
|
|
|
if (mNextEndRef) {
|
|
|
|
mEnd.SetAfterRef(mEnd.Container(), mNextEndRef);
|
|
|
|
MOZ_ASSERT(mNextEndRef == aFirstNewContent);
|
|
|
|
mNextEndRef = nullptr;
|
|
|
|
}
|
|
|
|
DoSetRange(mStart.AsRaw(), mEnd.AsRaw(), mRoot, true);
|
2012-11-21 00:14:15 +04:00
|
|
|
}
|
2006-11-04 00:51:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRange::ContentInserted(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
2017-07-27 16:49:52 +03:00
|
|
|
nsIContent* aChild)
|
2006-11-04 00:51:01 +03:00
|
|
|
{
|
2017-08-18 01:05:40 +03:00
|
|
|
MOZ_ASSERT(mIsPositioned, "shouldn't be notified if not positioned");
|
2006-11-04 00:51:01 +03:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
bool updateBoundaries = false;
|
2006-11-04 00:51:01 +03:00
|
|
|
nsINode* container = NODE_FROM(aContainer, aDocument);
|
2017-08-18 01:05:40 +03:00
|
|
|
MOZ_ASSERT(container);
|
|
|
|
RawRangeBoundary newStart(mStart);
|
|
|
|
RawRangeBoundary newEnd(mEnd);
|
|
|
|
MOZ_ASSERT(aChild->GetParentNode() == container);
|
2006-11-04 00:51:01 +03:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
// Invalidate boundary offsets if a child that may have moved them was
|
|
|
|
// inserted.
|
|
|
|
if (container == mStart.Container()) {
|
|
|
|
newStart.InvalidateOffset();
|
|
|
|
updateBoundaries = true;
|
2006-11-04 00:51:01 +03:00
|
|
|
}
|
2017-08-18 01:05:40 +03:00
|
|
|
|
|
|
|
if (container == mEnd.Container()) {
|
|
|
|
newEnd.InvalidateOffset();
|
|
|
|
updateBoundaries = true;
|
|
|
|
}
|
|
|
|
|
2011-12-20 13:15:41 +04:00
|
|
|
if (container->IsSelectionDescendant() &&
|
|
|
|
!aChild->IsDescendantOfCommonAncestorForRangeInSelection()) {
|
|
|
|
MarkDescendants(aChild);
|
|
|
|
aChild->SetDescendantOfCommonAncestorForRangeInSelection();
|
|
|
|
}
|
2012-11-21 00:14:15 +04:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
if (mNextStartRef || mNextEndRef) {
|
|
|
|
if (mNextStartRef) {
|
|
|
|
newStart.SetAfterRef(mStart.Container(), mNextStartRef);
|
|
|
|
MOZ_ASSERT(mNextStartRef == aChild);
|
|
|
|
mNextStartRef = nullptr;
|
|
|
|
}
|
|
|
|
if (mNextEndRef) {
|
|
|
|
newEnd.SetAfterRef(mEnd.Container(), mNextEndRef);
|
|
|
|
MOZ_ASSERT(mNextEndRef == aChild);
|
|
|
|
mNextEndRef = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
updateBoundaries = true;
|
2012-11-21 00:14:15 +04:00
|
|
|
}
|
2017-07-19 21:29:59 +03:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
if (updateBoundaries) {
|
|
|
|
DoSetRange(newStart, newEnd, mRoot);
|
2017-07-19 21:29:59 +03:00
|
|
|
}
|
2006-11-04 00:51:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRange::ContentRemoved(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
2010-07-22 02:05:17 +04:00
|
|
|
nsIContent* aPreviousSibling)
|
2006-11-04 00:51:01 +03:00
|
|
|
{
|
2017-08-18 01:05:40 +03:00
|
|
|
MOZ_ASSERT(mIsPositioned, "shouldn't be notified if not positioned");
|
2006-11-04 00:51:01 +03:00
|
|
|
nsINode* container = NODE_FROM(aContainer, aDocument);
|
2017-08-18 01:05:40 +03:00
|
|
|
RawRangeBoundary newStart;
|
|
|
|
RawRangeBoundary newEnd;
|
|
|
|
Maybe<bool> gravitateStart;
|
|
|
|
bool gravitateEnd;
|
2006-11-04 00:51:01 +03:00
|
|
|
|
|
|
|
// Adjust position if a sibling was removed...
|
2017-08-18 01:05:40 +03:00
|
|
|
if (container == mStart.Container()) {
|
|
|
|
// We're only interested if our boundary reference was removed, otherwise
|
|
|
|
// we can just invalidate the offset.
|
|
|
|
if (aChild == mStart.Ref()) {
|
|
|
|
newStart.SetAfterRef(container, aPreviousSibling);
|
|
|
|
} else {
|
|
|
|
newStart = mStart;
|
|
|
|
newStart.InvalidateOffset();
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
gravitateStart = Some(nsContentUtils::ContentIsDescendantOf(mStart.Container(), aChild));
|
|
|
|
if (gravitateStart.value()) {
|
|
|
|
newStart.SetAfterRef(container, aPreviousSibling);
|
2009-07-27 16:58:23 +04:00
|
|
|
}
|
2006-11-04 00:51:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do same thing for end boundry.
|
2017-08-18 01:05:40 +03:00
|
|
|
if (container == mEnd.Container()) {
|
|
|
|
if (aChild == mEnd.Ref()) {
|
|
|
|
newEnd.SetAfterRef(container, aPreviousSibling);
|
|
|
|
} else {
|
|
|
|
newEnd = mEnd;
|
|
|
|
newEnd.InvalidateOffset();
|
|
|
|
}
|
2013-08-13 19:40:42 +04:00
|
|
|
} else {
|
2017-08-18 01:05:40 +03:00
|
|
|
if (mStart.Container() == mEnd.Container() && gravitateStart.isSome()) {
|
|
|
|
gravitateEnd = gravitateStart.value();
|
|
|
|
} else {
|
|
|
|
gravitateEnd = nsContentUtils::ContentIsDescendantOf(mEnd.Container(), aChild);
|
|
|
|
}
|
|
|
|
if (gravitateEnd) {
|
|
|
|
newEnd.SetAfterRef(container, aPreviousSibling);
|
|
|
|
}
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
if (newStart.IsSet() || newEnd.IsSet()) {
|
|
|
|
DoSetRange(newStart.IsSet() ? newStart : mStart.AsRaw(),
|
|
|
|
newEnd.IsSet() ? newEnd : mEnd.AsRaw(), mRoot);
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
2017-08-18 01:05:40 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(mStart.Ref() != aChild);
|
|
|
|
MOZ_ASSERT(mEnd.Ref() != aChild);
|
|
|
|
|
2011-12-20 13:15:41 +04:00
|
|
|
if (container->IsSelectionDescendant() &&
|
|
|
|
aChild->IsDescendantOfCommonAncestorForRangeInSelection()) {
|
|
|
|
aChild->ClearDescendantOfCommonAncestorForRangeInSelection();
|
|
|
|
UnmarkDescendants(aChild);
|
2006-11-04 00:51:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-24 15:46:09 +03:00
|
|
|
void
|
|
|
|
nsRange::ParentChainChanged(nsIContent *aContent)
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mRoot == aContent, "Wrong ParentChainChanged notification?");
|
2017-08-18 01:05:40 +03:00
|
|
|
nsINode* newRoot = IsValidBoundary(mStart.Container());
|
2008-03-13 13:12:51 +03:00
|
|
|
NS_ASSERTION(newRoot, "No valid boundary or root found!");
|
2017-08-18 01:05:40 +03:00
|
|
|
if (newRoot != IsValidBoundary(mEnd.Container())) {
|
2013-03-04 20:37:48 +04:00
|
|
|
// Sometimes ordering involved in cycle collection can lead to our
|
|
|
|
// start parent and/or end parent being disconnected from our root
|
|
|
|
// without our getting a ContentRemoved notification.
|
|
|
|
// See bug 846096 for more details.
|
2017-08-18 01:05:40 +03:00
|
|
|
NS_ASSERTION(mEnd.Container()->IsInNativeAnonymousSubtree(),
|
2013-03-04 20:37:48 +04:00
|
|
|
"This special case should happen only with "
|
|
|
|
"native-anonymous content");
|
|
|
|
// When that happens, bail out and set pointers to null; since we're
|
|
|
|
// in cycle collection and unreachable it shouldn't matter.
|
|
|
|
Reset();
|
|
|
|
return;
|
|
|
|
}
|
2010-07-21 19:33:31 +04:00
|
|
|
// This is safe without holding a strong ref to self as long as the change
|
|
|
|
// of mRoot is the last thing in DoSetRange.
|
2017-08-18 01:05:40 +03:00
|
|
|
DoSetRange(mStart.AsRaw(), mEnd.AsRaw(), newRoot);
|
2008-02-24 15:46:09 +03:00
|
|
|
}
|
|
|
|
|
2012-01-10 18:19:54 +04:00
|
|
|
/******************************************************
|
|
|
|
* Utilities for comparing points: API from nsIDOMRange
|
|
|
|
******************************************************/
|
2000-06-08 02:57:36 +04:00
|
|
|
NS_IMETHODIMP
|
2017-07-19 16:49:52 +03:00
|
|
|
nsRange::IsPointInRange(nsIDOMNode* aContainer, uint32_t aOffset, bool* aResult)
|
2000-06-08 02:57:36 +04:00
|
|
|
{
|
2017-07-11 17:52:39 +03:00
|
|
|
nsCOMPtr<nsINode> container = do_QueryInterface(aContainer);
|
|
|
|
if (!container) {
|
2013-02-07 16:09:41 +04:00
|
|
|
return NS_ERROR_DOM_NOT_OBJECT_ERR;
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
2017-07-19 16:49:52 +03:00
|
|
|
if (NS_WARN_IF(!IsValidOffset(aOffset))) {
|
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
}
|
2013-02-07 16:09:41 +04:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
ErrorResult rv;
|
2017-12-05 11:50:13 +03:00
|
|
|
*aResult = IsPointInRange(RawRangeBoundary(container, aOffset), rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
2013-02-07 16:09:41 +04:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
bool
|
2017-12-05 11:50:13 +03:00
|
|
|
nsRange::IsPointInRange(const RawRangeBoundary& aPoint, ErrorResult& aRv)
|
2013-02-07 16:09:41 +04:00
|
|
|
{
|
2017-12-05 11:50:13 +03:00
|
|
|
uint16_t compareResult = ComparePoint(aPoint, aRv);
|
2013-02-07 16:09:41 +04:00
|
|
|
// If the node isn't in the range's document, it clearly isn't in the range.
|
2015-04-27 16:18:52 +03:00
|
|
|
if (aRv.ErrorCodeIs(NS_ERROR_DOM_WRONG_DOCUMENT_ERR)) {
|
|
|
|
aRv.SuppressException();
|
2013-02-07 16:09:41 +04:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return compareResult == 0;
|
2013-03-12 07:10:18 +04:00
|
|
|
}
|
2013-02-07 16:09:41 +04:00
|
|
|
|
2000-06-08 02:57:36 +04:00
|
|
|
// returns -1 if point is before range, 0 if point is in range,
|
|
|
|
// 1 if point is after range.
|
|
|
|
NS_IMETHODIMP
|
2017-07-19 16:49:52 +03:00
|
|
|
nsRange::ComparePoint(nsIDOMNode* aContainer, uint32_t aOffset,
|
|
|
|
int16_t* aResult)
|
2000-06-08 02:57:36 +04:00
|
|
|
{
|
2017-07-11 17:52:39 +03:00
|
|
|
nsCOMPtr<nsINode> container = do_QueryInterface(aContainer);
|
|
|
|
NS_ENSURE_TRUE(container, NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
|
2006-11-02 22:07:56 +03:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
ErrorResult rv;
|
2017-12-05 11:50:13 +03:00
|
|
|
*aResult = ComparePoint(RawRangeBoundary(container, aOffset), rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int16_t
|
2017-12-05 11:50:13 +03:00
|
|
|
nsRange::ComparePoint(const RawRangeBoundary& aPoint, ErrorResult& aRv)
|
2013-02-07 16:09:41 +04:00
|
|
|
{
|
2017-12-05 11:50:13 +03:00
|
|
|
if (NS_WARN_IF(!aPoint.IsSet())) {
|
|
|
|
// FYI: Shouldn't reach this case if it's called by JS. Therefore, it's
|
|
|
|
// okay to warn.
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
// our range is in a good state?
|
|
|
|
if (!mIsPositioned) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return 0;
|
2006-11-02 22:07:56 +03:00
|
|
|
}
|
2013-02-07 16:09:41 +04:00
|
|
|
|
2017-12-05 11:50:13 +03:00
|
|
|
if (!nsContentUtils::ContentIsDescendantOf(aPoint.Container(), mRoot)) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_WRONG_DOCUMENT_ERR);
|
|
|
|
return 0;
|
2012-03-20 23:54:38 +04:00
|
|
|
}
|
2013-02-07 16:09:41 +04:00
|
|
|
|
2018-01-30 07:10:53 +03:00
|
|
|
if (aPoint.Container()->NodeType() == nsINode::DOCUMENT_TYPE_NODE) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR);
|
|
|
|
return 0;
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
2013-02-07 16:09:41 +04:00
|
|
|
|
2017-12-05 11:50:13 +03:00
|
|
|
if (aPoint.Offset() > aPoint.Container()->Length()) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-12-05 11:50:13 +03:00
|
|
|
int32_t cmp = nsContentUtils::ComparePoints(aPoint, mStart.AsRaw());
|
2017-07-19 16:49:52 +03:00
|
|
|
if (cmp <= 0) {
|
2013-02-07 16:09:41 +04:00
|
|
|
return cmp;
|
2000-06-08 02:57:36 +04:00
|
|
|
}
|
2017-12-05 11:50:13 +03:00
|
|
|
if (nsContentUtils::ComparePoints(mEnd.AsRaw(), aPoint) == -1) {
|
2013-02-07 16:09:41 +04:00
|
|
|
return 1;
|
2013-03-12 07:10:18 +04:00
|
|
|
}
|
2013-02-07 16:09:41 +04:00
|
|
|
|
|
|
|
return 0;
|
2000-06-08 02:57:36 +04:00
|
|
|
}
|
2012-07-18 14:36:08 +04:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::IntersectsNode(nsIDOMNode* aNode, bool* aResult)
|
|
|
|
{
|
|
|
|
*aResult = false;
|
|
|
|
|
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
// TODO: This should throw a TypeError.
|
|
|
|
NS_ENSURE_ARG(node);
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
*aResult = IntersectsNode(*node, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
nsRange::IntersectsNode(nsINode& aNode, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!mIsPositioned) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return false;
|
|
|
|
}
|
2012-07-18 14:36:08 +04:00
|
|
|
|
|
|
|
// Step 3.
|
2013-02-07 16:09:41 +04:00
|
|
|
nsINode* parent = aNode.GetParentNode();
|
2012-07-18 14:36:08 +04:00
|
|
|
if (!parent) {
|
2017-07-06 15:00:35 +03:00
|
|
|
// Steps 2 and 4.
|
2012-07-18 14:36:08 +04:00
|
|
|
// |parent| is null, so |node|'s root is |node| itself.
|
2013-02-07 16:09:41 +04:00
|
|
|
return GetRoot() == &aNode;
|
2012-07-18 14:36:08 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Step 5.
|
2018-01-23 16:30:18 +03:00
|
|
|
int32_t nodeIndex = parent->ComputeIndexOf(&aNode);
|
2012-07-18 14:36:08 +04:00
|
|
|
|
|
|
|
// Steps 6-7.
|
|
|
|
// Note: if disconnected is true, ComparePoints returns 1.
|
|
|
|
bool disconnected = false;
|
2017-08-18 01:05:40 +03:00
|
|
|
bool result = nsContentUtils::ComparePoints(mStart.Container(), mStart.Offset(),
|
|
|
|
parent, nodeIndex + 1,
|
|
|
|
&disconnected) < 0 &&
|
|
|
|
nsContentUtils::ComparePoints(parent, nodeIndex,
|
|
|
|
mEnd.Container(), mEnd.Offset(),
|
|
|
|
&disconnected) < 0;
|
2012-07-18 14:36:08 +04:00
|
|
|
|
|
|
|
// Step 2.
|
|
|
|
if (disconnected) {
|
2013-02-07 16:09:41 +04:00
|
|
|
result = false;
|
2012-07-18 14:36:08 +04:00
|
|
|
}
|
2013-02-07 16:09:41 +04:00
|
|
|
return result;
|
2012-07-18 14:36:08 +04:00
|
|
|
}
|
|
|
|
|
2017-10-06 01:37:47 +03:00
|
|
|
void
|
|
|
|
nsRange::NotifySelectionListenersAfterRangeSet()
|
|
|
|
{
|
|
|
|
if (mSelection) {
|
|
|
|
// Our internal code should not move focus with using this instance while
|
|
|
|
// it's calling Selection::NotifySelectionListeners() which may move focus
|
|
|
|
// or calls selection listeners. So, let's set mCalledByJS to false here
|
|
|
|
// since non-*JS() methods don't set it to false.
|
|
|
|
AutoCalledByJSRestore calledByJSRestorer(*this);
|
|
|
|
mCalledByJS = false;
|
|
|
|
// Be aware, this range may be modified or stop being a range for selection
|
|
|
|
// after this call. Additionally, the selection instance may have gone.
|
|
|
|
RefPtr<Selection> selection = mSelection;
|
|
|
|
selection->NotifySelectionListeners(calledByJSRestorer.SavedValue());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
1998-12-03 18:02:37 +03:00
|
|
|
/******************************************************
|
|
|
|
* Private helper routines
|
|
|
|
******************************************************/
|
1998-12-04 01:59:07 +03:00
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
// It's important that all setting of the range start/end points
|
1998-12-18 05:51:34 +03:00
|
|
|
// go through this function, which will do all the right voodoo
|
2017-07-06 15:00:35 +03:00
|
|
|
// for content notification of range ownership.
|
1998-12-19 02:12:29 +03:00
|
|
|
// Calling DoSetRange with either parent argument null will collapse
|
|
|
|
// the range to have both endpoints point to the other node
|
2006-10-21 05:30:54 +04:00
|
|
|
void
|
2017-08-18 01:05:40 +03:00
|
|
|
nsRange::DoSetRange(const RawRangeBoundary& aStart,
|
|
|
|
const RawRangeBoundary& aEnd,
|
2011-12-20 13:15:41 +04:00
|
|
|
nsINode* aRoot, bool aNotInsertedYet)
|
1998-12-04 21:21:52 +03:00
|
|
|
{
|
2017-08-18 01:05:40 +03:00
|
|
|
NS_PRECONDITION((aStart.IsSet() && aEnd.IsSet() && aRoot) ||
|
|
|
|
(!aStart.IsSet() && !aEnd.IsSet() && !aRoot),
|
2006-11-04 00:51:01 +03:00
|
|
|
"Set all or none");
|
2011-09-24 04:56:38 +04:00
|
|
|
NS_PRECONDITION(!aRoot || aNotInsertedYet ||
|
2017-08-18 01:05:40 +03:00
|
|
|
(nsContentUtils::ContentIsDescendantOf(aStart.Container(), aRoot) &&
|
|
|
|
nsContentUtils::ContentIsDescendantOf(aEnd.Container(), aRoot) &&
|
|
|
|
aRoot == IsValidBoundary(aStart.Container()) &&
|
|
|
|
aRoot == IsValidBoundary(aEnd.Container())),
|
2006-11-04 00:51:01 +03:00
|
|
|
"Wrong root");
|
|
|
|
NS_PRECONDITION(!aRoot ||
|
2017-10-25 18:19:11 +03:00
|
|
|
(aStart.Container()->IsContent() &&
|
|
|
|
aEnd.Container()->IsContent() &&
|
2007-03-01 23:48:26 +03:00
|
|
|
aRoot ==
|
2017-08-18 01:05:40 +03:00
|
|
|
static_cast<nsIContent*>(aStart.Container())->GetBindingParent() &&
|
2007-03-01 23:48:26 +03:00
|
|
|
aRoot ==
|
2017-08-18 01:05:40 +03:00
|
|
|
static_cast<nsIContent*>(aEnd.Container())->GetBindingParent()) ||
|
2012-10-09 16:31:24 +04:00
|
|
|
(!aRoot->GetParentNode() &&
|
2006-11-04 00:51:01 +03:00
|
|
|
(aRoot->IsNodeOfType(nsINode::eDOCUMENT) ||
|
|
|
|
aRoot->IsNodeOfType(nsINode::eATTRIBUTE) ||
|
2008-02-24 15:46:09 +03:00
|
|
|
aRoot->IsNodeOfType(nsINode::eDOCUMENT_FRAGMENT) ||
|
|
|
|
/*For backward compatibility*/
|
2017-10-25 18:19:11 +03:00
|
|
|
aRoot->IsContent())),
|
2006-11-04 00:51:01 +03:00
|
|
|
"Bad root");
|
|
|
|
if (mRoot != aRoot) {
|
|
|
|
if (mRoot) {
|
|
|
|
mRoot->RemoveMutationObserver(this);
|
2006-10-21 05:30:54 +04:00
|
|
|
}
|
2006-11-04 00:51:01 +03:00
|
|
|
if (aRoot) {
|
|
|
|
aRoot->AddMutationObserver(this);
|
2006-10-21 05:30:54 +04:00
|
|
|
}
|
1999-08-06 14:33:09 +04:00
|
|
|
}
|
2017-07-11 16:46:11 +03:00
|
|
|
bool checkCommonAncestor =
|
2017-08-18 01:05:40 +03:00
|
|
|
(mStart.Container() != aStart.Container() || mEnd.Container() != aEnd.Container()) &&
|
2017-07-11 16:46:11 +03:00
|
|
|
IsInSelection() && !aNotInsertedYet;
|
2017-09-01 18:13:47 +03:00
|
|
|
|
|
|
|
// GetCommonAncestor is unreliable while we're unlinking (could
|
|
|
|
// return null if our start/end have already been unlinked), so make
|
|
|
|
// sure to not use it here to determine our "old" current ancestor.
|
2017-08-18 01:05:40 +03:00
|
|
|
mStart = aStart;
|
|
|
|
mEnd = aEnd;
|
2017-11-02 15:25:14 +03:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
mIsPositioned = !!mStart.Container();
|
2011-12-20 13:15:41 +04:00
|
|
|
if (checkCommonAncestor) {
|
2017-09-01 18:13:47 +03:00
|
|
|
nsINode* oldCommonAncestor = mRegisteredCommonAncestor;
|
2011-12-20 13:15:41 +04:00
|
|
|
nsINode* newCommonAncestor = GetCommonAncestor();
|
|
|
|
if (newCommonAncestor != oldCommonAncestor) {
|
|
|
|
if (oldCommonAncestor) {
|
2017-09-01 18:13:47 +03:00
|
|
|
UnregisterCommonAncestor(oldCommonAncestor, false);
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
|
|
|
if (newCommonAncestor) {
|
|
|
|
RegisterCommonAncestor(newCommonAncestor);
|
|
|
|
} else {
|
2012-04-25 16:03:48 +04:00
|
|
|
NS_ASSERTION(!mIsPositioned, "unexpected disconnected nodes");
|
2015-08-12 20:26:01 +03:00
|
|
|
mSelection = nullptr;
|
2017-09-13 23:53:45 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mRegisteredCommonAncestor,
|
|
|
|
"How can we have a registered common ancestor when we "
|
|
|
|
"didn't register ourselves?");
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!isInList(),
|
|
|
|
"Shouldn't be registered if we have no "
|
|
|
|
"mRegisteredCommonAncestor");
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:26:01 +03:00
|
|
|
// This needs to be the last thing this function does, other than notifying
|
|
|
|
// selection listeners. See comment in ParentChainChanged.
|
2006-11-04 00:51:01 +03:00
|
|
|
mRoot = aRoot;
|
2015-08-12 20:26:01 +03:00
|
|
|
|
|
|
|
// Notify any selection listeners. This has to occur last because otherwise the world
|
|
|
|
// could be observed by a selection listener while the range was in an invalid state.
|
2017-10-06 01:37:47 +03:00
|
|
|
// So we run it off of a script runner to ensure it runs after the mutation observers
|
|
|
|
// have finished running.
|
2015-08-12 20:26:01 +03:00
|
|
|
if (mSelection) {
|
2017-10-06 01:37:47 +03:00
|
|
|
nsContentUtils::AddScriptRunner(NewRunnableMethod(
|
|
|
|
"NotifySelectionListenersAfterRangeSet",
|
|
|
|
this,
|
|
|
|
&nsRange::NotifySelectionListenersAfterRangeSet));
|
2015-08-12 20:26:01 +03:00
|
|
|
}
|
1998-12-03 12:51:05 +03:00
|
|
|
}
|
1998-11-26 05:30:44 +03:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
static int32_t
|
|
|
|
IndexOf(nsINode* aChild)
|
|
|
|
{
|
|
|
|
nsINode* parent = aChild->GetParentNode();
|
|
|
|
|
2018-01-23 16:30:18 +03:00
|
|
|
return parent ? parent->ComputeIndexOf(aChild) : -1;
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2015-08-12 20:26:01 +03:00
|
|
|
void
|
|
|
|
nsRange::SetSelection(mozilla::dom::Selection* aSelection)
|
|
|
|
{
|
|
|
|
if (mSelection == aSelection) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-24 23:44:51 +03:00
|
|
|
|
2015-08-12 20:26:01 +03:00
|
|
|
// At least one of aSelection and mSelection must be null
|
|
|
|
// aSelection will be null when we are removing from a selection
|
|
|
|
// and a range can't be in more than one selection at a time,
|
|
|
|
// thus mSelection must be null too.
|
|
|
|
MOZ_ASSERT(!aSelection || !mSelection);
|
|
|
|
|
2017-09-24 23:44:51 +03:00
|
|
|
// Extra step in case our parent failed to ensure the above
|
|
|
|
// invariant.
|
|
|
|
if (aSelection && mSelection) {
|
|
|
|
mSelection->RemoveRange(this);
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:26:01 +03:00
|
|
|
mSelection = aSelection;
|
|
|
|
if (mSelection) {
|
2017-09-01 18:13:47 +03:00
|
|
|
nsINode* commonAncestor = GetCommonAncestor();
|
|
|
|
NS_ASSERTION(commonAncestor, "unexpected disconnected nodes");
|
2015-08-12 20:26:01 +03:00
|
|
|
RegisterCommonAncestor(commonAncestor);
|
|
|
|
} else {
|
2017-09-01 18:13:47 +03:00
|
|
|
UnregisterCommonAncestor(mRegisteredCommonAncestor, false);
|
2017-09-13 23:53:45 +03:00
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!mRegisteredCommonAncestor,
|
|
|
|
"How can we have a registered common ancestor when we "
|
|
|
|
"just unregistered?");
|
|
|
|
MOZ_DIAGNOSTIC_ASSERT(!isInList(),
|
|
|
|
"Shouldn't be registered if we have no "
|
|
|
|
"mRegisteredCommonAncestor after unregistering");
|
2015-08-12 20:26:01 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-10 18:19:54 +04:00
|
|
|
nsINode*
|
2009-04-06 21:17:38 +04:00
|
|
|
nsRange::GetCommonAncestor() const
|
2006-11-02 05:14:57 +03:00
|
|
|
{
|
|
|
|
return mIsPositioned ?
|
2017-08-18 01:05:40 +03:00
|
|
|
nsContentUtils::GetCommonAncestor(mStart.Container(), mEnd.Container()) :
|
2012-07-30 18:20:58 +04:00
|
|
|
nullptr;
|
2006-11-02 05:14:57 +03:00
|
|
|
}
|
|
|
|
|
2012-01-10 18:19:54 +04:00
|
|
|
void
|
2006-11-02 05:14:57 +03:00
|
|
|
nsRange::Reset()
|
|
|
|
{
|
2017-08-18 01:05:40 +03:00
|
|
|
DoSetRange(RawRangeBoundary(), RawRangeBoundary(), nullptr);
|
2006-11-02 05:14:57 +03:00
|
|
|
}
|
|
|
|
|
1998-12-03 18:02:37 +03:00
|
|
|
/******************************************************
|
|
|
|
* public functionality
|
|
|
|
******************************************************/
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
2017-07-11 17:10:42 +03:00
|
|
|
nsRange::GetStartContainer(nsIDOMNode** aStartContainer)
|
1998-12-03 12:51:05 +03:00
|
|
|
{
|
|
|
|
if (!mIsPositioned)
|
2001-01-07 18:11:50 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2003-04-15 05:09:09 +04:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
return CallQueryInterface(mStart.Container(), aStartContainer);
|
1998-12-03 12:51:05 +03:00
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
nsINode*
|
|
|
|
nsRange::GetStartContainer(ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
if (!mIsPositioned) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
return mStart.Container();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
2017-07-19 16:49:52 +03:00
|
|
|
nsRange::GetStartOffset(uint32_t* aStartOffset)
|
1998-12-03 12:51:05 +03:00
|
|
|
{
|
|
|
|
if (!mIsPositioned)
|
2001-01-07 18:11:50 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2006-10-21 05:30:54 +04:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
*aStartOffset = mStart.Offset();
|
2006-10-21 05:30:54 +04:00
|
|
|
|
1998-12-03 12:51:05 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
uint32_t
|
|
|
|
nsRange::GetStartOffset(ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
if (!mIsPositioned) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
return mStart.Offset();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
2017-07-11 18:09:37 +03:00
|
|
|
nsRange::GetEndContainer(nsIDOMNode** aEndContainer)
|
1998-12-03 12:51:05 +03:00
|
|
|
{
|
|
|
|
if (!mIsPositioned)
|
2001-01-07 18:11:50 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2006-10-21 05:30:54 +04:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
return CallQueryInterface(mEnd.Container(), aEndContainer);
|
1998-12-03 12:51:05 +03:00
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
nsINode*
|
|
|
|
nsRange::GetEndContainer(ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
if (!mIsPositioned) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
return mEnd.Container();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
2017-07-19 16:49:52 +03:00
|
|
|
nsRange::GetEndOffset(uint32_t* aEndOffset)
|
1998-12-03 12:51:05 +03:00
|
|
|
{
|
|
|
|
if (!mIsPositioned)
|
2001-01-07 18:11:50 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2006-10-21 05:30:54 +04:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
*aEndOffset = mEnd.Offset();
|
2006-10-21 05:30:54 +04:00
|
|
|
|
1998-12-03 12:51:05 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
uint32_t
|
|
|
|
nsRange::GetEndOffset(ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
if (!mIsPositioned) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
return mEnd.Offset();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsRange::GetCollapsed(bool* aIsCollapsed)
|
1998-12-03 12:51:05 +03:00
|
|
|
{
|
|
|
|
if (!mIsPositioned)
|
2001-01-07 18:11:50 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
2001-01-10 00:44:35 +03:00
|
|
|
|
2006-10-21 05:30:54 +04:00
|
|
|
*aIsCollapsed = Collapsed();
|
|
|
|
|
1998-12-03 12:51:05 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
nsINode*
|
|
|
|
nsRange::GetCommonAncestorContainer(ErrorResult& aRv) const
|
|
|
|
{
|
|
|
|
if (!mIsPositioned) {
|
|
|
|
aRv.Throw(NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
return nsContentUtils::GetCommonAncestor(mStart.Container(), mEnd.Container());
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::GetCommonAncestorContainer(nsIDOMNode** aCommonParent)
|
2006-10-21 05:30:54 +04:00
|
|
|
{
|
2013-02-07 16:09:41 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
nsINode* commonAncestor = GetCommonAncestorContainer(rv);
|
|
|
|
if (commonAncestor) {
|
|
|
|
NS_ADDREF(*aCommonParent = commonAncestor->AsDOMNode());
|
|
|
|
} else {
|
|
|
|
*aCommonParent = nullptr;
|
2006-10-21 05:30:54 +04:00
|
|
|
}
|
|
|
|
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
1998-12-03 18:02:37 +03:00
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2017-05-30 07:18:25 +03:00
|
|
|
/* static */
|
|
|
|
bool
|
2017-07-19 16:49:52 +03:00
|
|
|
nsRange::IsValidOffset(nsINode* aNode, uint32_t aOffset)
|
2017-05-30 07:18:25 +03:00
|
|
|
{
|
|
|
|
return aNode &&
|
2017-07-19 16:49:52 +03:00
|
|
|
IsValidOffset(aOffset) &&
|
2017-05-30 07:18:25 +03:00
|
|
|
static_cast<size_t>(aOffset) <= aNode->Length();
|
|
|
|
}
|
|
|
|
|
2017-06-23 07:21:47 +03:00
|
|
|
/* static */
|
2012-01-10 18:19:54 +04:00
|
|
|
nsINode*
|
2017-06-23 07:21:47 +03:00
|
|
|
nsRange::ComputeRootNode(nsINode* aNode, bool aMaySpanAnonymousSubtrees)
|
2006-05-15 23:35:12 +04:00
|
|
|
{
|
|
|
|
if (!aNode) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2006-05-15 23:35:12 +04:00
|
|
|
}
|
|
|
|
|
2017-10-25 18:19:11 +03:00
|
|
|
if (aNode->IsContent()) {
|
2015-03-03 14:09:00 +03:00
|
|
|
if (aNode->NodeInfo()->NameAtom() == nsGkAtoms::documentTypeNodeName) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2007-03-01 01:19:20 +03:00
|
|
|
}
|
|
|
|
|
2017-10-25 18:19:11 +03:00
|
|
|
nsIContent* content = aNode->AsContent();
|
2017-06-23 07:21:47 +03:00
|
|
|
if (!aMaySpanAnonymousSubtrees) {
|
2013-12-02 14:26:11 +04:00
|
|
|
// If the node is in a shadow tree then the ShadowRoot is the root.
|
|
|
|
ShadowRoot* containingShadow = content->GetContainingShadow();
|
|
|
|
if (containingShadow) {
|
|
|
|
return containingShadow;
|
|
|
|
}
|
|
|
|
|
2007-06-19 07:01:03 +04:00
|
|
|
// If the node has a binding parent, that should be the root.
|
|
|
|
// XXXbz maybe only for native anonymous content?
|
|
|
|
nsINode* root = content->GetBindingParent();
|
|
|
|
if (root) {
|
|
|
|
return root;
|
|
|
|
}
|
2007-03-01 01:19:20 +03:00
|
|
|
}
|
2006-05-15 23:35:12 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Elements etc. must be in document or in document fragment,
|
|
|
|
// text nodes in document, in document fragment or in attribute.
|
2014-10-02 22:45:44 +04:00
|
|
|
nsINode* root = aNode->GetUncomposedDoc();
|
2006-11-04 00:51:01 +03:00
|
|
|
if (root) {
|
|
|
|
return root;
|
2006-05-15 23:35:12 +04:00
|
|
|
}
|
|
|
|
|
2013-08-13 19:40:42 +04:00
|
|
|
root = aNode->SubtreeRoot();
|
2006-05-15 23:35:12 +04:00
|
|
|
|
2006-11-04 00:51:01 +03:00
|
|
|
NS_ASSERTION(!root->IsNodeOfType(nsINode::eDOCUMENT),
|
2014-10-02 22:45:44 +04:00
|
|
|
"GetUncomposedDoc should have returned a doc");
|
2006-11-04 00:51:01 +03:00
|
|
|
|
2008-02-24 15:46:09 +03:00
|
|
|
// We allow this because of backward compatibility.
|
|
|
|
return root;
|
2006-10-21 05:30:54 +04:00
|
|
|
}
|
|
|
|
|
2017-06-26 11:26:27 +03:00
|
|
|
/* static */
|
|
|
|
bool
|
|
|
|
nsRange::IsValidPoints(nsINode* aStartContainer, uint32_t aStartOffset,
|
|
|
|
nsINode* aEndContainer, uint32_t aEndOffset)
|
|
|
|
{
|
|
|
|
// Use NS_WARN_IF() only for the cases where the arguments are unexpected.
|
|
|
|
if (NS_WARN_IF(!aStartContainer) || NS_WARN_IF(!aEndContainer) ||
|
|
|
|
NS_WARN_IF(!IsValidOffset(aStartContainer, aStartOffset)) ||
|
|
|
|
NS_WARN_IF(!IsValidOffset(aEndContainer, aEndOffset))) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, don't use NS_WARN_IF() for preventing to make console messy.
|
|
|
|
// Instead, check one by one since it is easier to catch the error reason
|
|
|
|
// with debugger.
|
|
|
|
|
|
|
|
if (ComputeRootNode(aStartContainer) != ComputeRootNode(aEndContainer)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool disconnected = false;
|
|
|
|
int32_t order =
|
|
|
|
nsContentUtils::ComparePoints(aStartContainer,
|
|
|
|
static_cast<int32_t>(aStartOffset),
|
|
|
|
aEndContainer,
|
|
|
|
static_cast<int32_t>(aEndOffset),
|
|
|
|
&disconnected);
|
|
|
|
// FYI: disconnected should be false unless |order| is 1.
|
|
|
|
if (order == 1 || NS_WARN_IF(disconnected)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
|
|
|
nsRange::SetStartJS(nsINode& aNode, uint32_t aOffset, ErrorResult& aErr)
|
|
|
|
{
|
2017-03-17 07:32:51 +03:00
|
|
|
AutoCalledByJSRestore calledByJSRestorer(*this);
|
|
|
|
mCalledByJS = true;
|
2017-03-10 10:55:12 +03:00
|
|
|
SetStart(aNode, aOffset, aErr);
|
|
|
|
}
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
void
|
|
|
|
nsRange::SetStart(nsINode& aNode, uint32_t aOffset, ErrorResult& aRv)
|
|
|
|
{
|
2014-09-25 13:04:45 +04:00
|
|
|
if (!nsContentUtils::LegacyIsCallerNativeCode() &&
|
|
|
|
!nsContentUtils::CanCallerAccess(&aNode)) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoInvalidateSelection atEndOfBlock(this);
|
2017-12-05 11:50:13 +03:00
|
|
|
SetStart(RawRangeBoundary(&aNode, aOffset), aRv);
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
2017-07-19 16:49:52 +03:00
|
|
|
nsRange::SetStart(nsIDOMNode* aContainer, uint32_t aOffset)
|
1998-12-03 12:51:05 +03:00
|
|
|
{
|
2017-07-11 17:52:39 +03:00
|
|
|
nsCOMPtr<nsINode> container = do_QueryInterface(aContainer);
|
|
|
|
if (!container) {
|
2013-02-07 16:09:41 +04:00
|
|
|
return NS_ERROR_DOM_NOT_OBJECT_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorResult rv;
|
2017-07-11 17:52:39 +03:00
|
|
|
SetStart(*container, aOffset, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2009-04-06 21:17:37 +04:00
|
|
|
}
|
|
|
|
|
2017-12-05 11:50:13 +03:00
|
|
|
void
|
|
|
|
nsRange::SetStart(const RawRangeBoundary& aPoint, ErrorResult& aRv)
|
2009-04-06 21:17:37 +04:00
|
|
|
{
|
2017-12-05 11:50:13 +03:00
|
|
|
nsINode* newRoot = IsValidBoundary(aPoint.Container());
|
2015-07-15 03:52:25 +03:00
|
|
|
if (!newRoot) {
|
2017-12-05 11:50:13 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR);
|
|
|
|
return;
|
2015-07-15 03:52:25 +03:00
|
|
|
}
|
2006-10-21 05:30:54 +04:00
|
|
|
|
2017-12-05 11:50:13 +03:00
|
|
|
if (!aPoint.IsSetAndValid()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
2012-03-30 01:09:04 +04:00
|
|
|
}
|
2002-06-26 00:03:06 +04:00
|
|
|
|
2006-10-22 08:46:19 +04:00
|
|
|
// Collapse if not positioned yet, if positioned in another doc or
|
|
|
|
// if the new start is after end.
|
2006-11-04 00:51:01 +03:00
|
|
|
if (!mIsPositioned || newRoot != mRoot ||
|
2017-12-05 11:50:13 +03:00
|
|
|
nsContentUtils::ComparePoints(aPoint, mEnd.AsRaw()) == 1) {
|
|
|
|
DoSetRange(aPoint, aPoint, newRoot);
|
|
|
|
return;
|
1998-12-03 12:51:05 +03:00
|
|
|
}
|
2002-06-26 00:03:06 +04:00
|
|
|
|
2017-12-05 11:50:13 +03:00
|
|
|
DoSetRange(aPoint, mEnd.AsRaw(), mRoot);
|
1998-12-03 12:51:05 +03:00
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
|
|
|
nsRange::SetStartBeforeJS(nsINode& aNode, ErrorResult& aErr)
|
|
|
|
{
|
2017-03-17 07:32:51 +03:00
|
|
|
AutoCalledByJSRestore calledByJSRestorer(*this);
|
|
|
|
mCalledByJS = true;
|
2017-03-10 10:55:12 +03:00
|
|
|
SetStartBefore(aNode, aErr);
|
|
|
|
}
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
void
|
|
|
|
nsRange::SetStartBefore(nsINode& aNode, ErrorResult& aRv)
|
|
|
|
{
|
2014-09-25 13:04:45 +04:00
|
|
|
if (!nsContentUtils::LegacyIsCallerNativeCode() &&
|
|
|
|
!nsContentUtils::CanCallerAccess(&aNode)) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoInvalidateSelection atEndOfBlock(this);
|
2017-07-19 16:49:52 +03:00
|
|
|
// If the node is being removed from its parent, GetContainerAndOffsetBefore()
|
|
|
|
// returns nullptr. Then, SetStart() will throw
|
|
|
|
// NS_ERROR_DOM_INVALID_NODE_TYPE_ERR.
|
|
|
|
uint32_t offset = UINT32_MAX;
|
2017-07-11 17:52:39 +03:00
|
|
|
nsINode* container = GetContainerAndOffsetBefore(&aNode, &offset);
|
|
|
|
aRv = SetStart(container, offset);
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::SetStartBefore(nsIDOMNode* aSibling)
|
1998-12-09 21:44:26 +03:00
|
|
|
{
|
2013-02-07 16:09:41 +04:00
|
|
|
nsCOMPtr<nsINode> sibling = do_QueryInterface(aSibling);
|
|
|
|
if (!sibling) {
|
|
|
|
return NS_ERROR_DOM_NOT_OBJECT_ERR;
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
SetStartBefore(*sibling, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
|
|
|
nsRange::SetStartAfterJS(nsINode& aNode, ErrorResult& aErr)
|
|
|
|
{
|
2017-03-17 07:32:51 +03:00
|
|
|
AutoCalledByJSRestore calledByJSRestorer(*this);
|
|
|
|
mCalledByJS = true;
|
2017-03-10 10:55:12 +03:00
|
|
|
SetStartAfter(aNode, aErr);
|
|
|
|
}
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
void
|
|
|
|
nsRange::SetStartAfter(nsINode& aNode, ErrorResult& aRv)
|
|
|
|
{
|
2014-09-25 13:04:45 +04:00
|
|
|
if (!nsContentUtils::LegacyIsCallerNativeCode() &&
|
|
|
|
!nsContentUtils::CanCallerAccess(&aNode)) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoInvalidateSelection atEndOfBlock(this);
|
2017-07-19 16:49:52 +03:00
|
|
|
// If the node is being removed from its parent, GetContainerAndOffsetAfter()
|
|
|
|
// returns nullptr. Then, SetStart() will throw
|
|
|
|
// NS_ERROR_DOM_INVALID_NODE_TYPE_ERR.
|
|
|
|
uint32_t offset = UINT32_MAX;
|
2017-07-11 17:52:39 +03:00
|
|
|
nsINode* container = GetContainerAndOffsetAfter(&aNode, &offset);
|
|
|
|
aRv = SetStart(container, offset);
|
1998-12-09 21:44:26 +03:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::SetStartAfter(nsIDOMNode* aSibling)
|
1998-12-09 21:44:26 +03:00
|
|
|
{
|
2013-02-07 16:09:41 +04:00
|
|
|
nsCOMPtr<nsINode> sibling = do_QueryInterface(aSibling);
|
|
|
|
if (!sibling) {
|
|
|
|
return NS_ERROR_DOM_NOT_OBJECT_ERR;
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
2013-03-12 07:10:18 +04:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
SetStartAfter(*sibling, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
|
|
|
nsRange::SetEndJS(nsINode& aNode, uint32_t aOffset, ErrorResult& aErr)
|
|
|
|
{
|
2017-03-17 07:32:51 +03:00
|
|
|
AutoCalledByJSRestore calledByJSRestorer(*this);
|
|
|
|
mCalledByJS = true;
|
2017-03-10 10:55:12 +03:00
|
|
|
SetEnd(aNode, aOffset, aErr);
|
|
|
|
}
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
void
|
|
|
|
nsRange::SetEnd(nsINode& aNode, uint32_t aOffset, ErrorResult& aRv)
|
|
|
|
{
|
2014-09-25 13:04:45 +04:00
|
|
|
if (!nsContentUtils::LegacyIsCallerNativeCode() &&
|
|
|
|
!nsContentUtils::CanCallerAccess(&aNode)) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
AutoInvalidateSelection atEndOfBlock(this);
|
2017-12-05 11:50:13 +03:00
|
|
|
SetEnd(RawRangeBoundary(&aNode, aOffset), aRv);
|
1998-12-09 21:44:26 +03:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
2017-07-19 16:49:52 +03:00
|
|
|
nsRange::SetEnd(nsIDOMNode* aContainer, uint32_t aOffset)
|
1998-12-03 12:51:05 +03:00
|
|
|
{
|
2017-07-11 17:52:39 +03:00
|
|
|
nsCOMPtr<nsINode> container = do_QueryInterface(aContainer);
|
|
|
|
if (!container) {
|
2013-02-07 16:09:41 +04:00
|
|
|
return NS_ERROR_DOM_NOT_OBJECT_ERR;
|
|
|
|
}
|
2009-04-06 21:17:37 +04:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
ErrorResult rv;
|
2017-07-11 17:52:39 +03:00
|
|
|
SetEnd(*container, aOffset, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
2013-03-12 07:10:18 +04:00
|
|
|
|
2017-12-05 11:50:13 +03:00
|
|
|
void
|
|
|
|
nsRange::SetEnd(const RawRangeBoundary& aPoint, ErrorResult& aRv)
|
2009-04-06 21:17:37 +04:00
|
|
|
{
|
2017-12-05 11:50:13 +03:00
|
|
|
nsINode* newRoot = IsValidBoundary(aPoint.Container());
|
2015-07-15 03:54:58 +03:00
|
|
|
if (!newRoot) {
|
2017-12-05 11:50:13 +03:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR);
|
|
|
|
return;
|
2015-07-15 03:54:58 +03:00
|
|
|
}
|
2006-10-21 05:30:54 +04:00
|
|
|
|
2017-12-05 11:50:13 +03:00
|
|
|
if (!aPoint.IsSetAndValid()) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return;
|
2006-10-21 05:30:54 +04:00
|
|
|
}
|
2006-10-22 08:46:19 +04:00
|
|
|
|
|
|
|
// Collapse if not positioned yet, if positioned in another doc or
|
|
|
|
// if the new end is before start.
|
2006-11-04 00:51:01 +03:00
|
|
|
if (!mIsPositioned || newRoot != mRoot ||
|
2017-12-05 11:50:13 +03:00
|
|
|
nsContentUtils::ComparePoints(mStart.AsRaw(), aPoint) == 1) {
|
|
|
|
DoSetRange(aPoint, aPoint, newRoot);
|
|
|
|
return;
|
1998-12-04 21:21:52 +03:00
|
|
|
}
|
2006-10-21 05:30:54 +04:00
|
|
|
|
2017-12-05 11:50:13 +03:00
|
|
|
DoSetRange(mStart.AsRaw(), aPoint, mRoot);
|
1998-12-03 12:51:05 +03:00
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2017-09-01 15:29:47 +03:00
|
|
|
void
|
|
|
|
nsRange::SelectNodesInContainer(nsINode* aContainer,
|
|
|
|
nsIContent* aStartContent,
|
|
|
|
nsIContent* aEndContent)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aContainer);
|
2018-01-23 16:30:18 +03:00
|
|
|
MOZ_ASSERT(aContainer->ComputeIndexOf(aStartContent) <=
|
|
|
|
aContainer->ComputeIndexOf(aEndContent));
|
|
|
|
MOZ_ASSERT(aStartContent && aContainer->ComputeIndexOf(aStartContent) != -1);
|
|
|
|
MOZ_ASSERT(aEndContent && aContainer->ComputeIndexOf(aEndContent) != -1);
|
2017-09-01 15:29:47 +03:00
|
|
|
|
|
|
|
nsINode* newRoot = ComputeRootNode(aContainer, mMaySpanAnonymousSubtrees);
|
|
|
|
MOZ_ASSERT(newRoot);
|
|
|
|
if (!newRoot) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
RawRangeBoundary start(aContainer, aStartContent->GetPreviousSibling());
|
|
|
|
RawRangeBoundary end(aContainer, aEndContent);
|
|
|
|
DoSetRange(start, end, newRoot);
|
|
|
|
}
|
|
|
|
|
2017-05-30 07:18:25 +03:00
|
|
|
nsresult
|
2017-10-02 17:58:26 +03:00
|
|
|
nsRange::SetStartAndEnd(const RawRangeBoundary& aStart,
|
|
|
|
const RawRangeBoundary& aEnd)
|
2017-05-30 07:18:25 +03:00
|
|
|
{
|
2017-10-02 17:58:26 +03:00
|
|
|
if (NS_WARN_IF(!aStart.IsSet()) || NS_WARN_IF(!aEnd.IsSet())) {
|
2017-05-30 07:18:25 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2017-10-02 17:58:26 +03:00
|
|
|
nsINode* newStartRoot = IsValidBoundary(aStart.Container());
|
2017-05-30 07:18:25 +03:00
|
|
|
if (!newStartRoot) {
|
|
|
|
return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
|
|
|
|
}
|
2017-10-02 17:58:26 +03:00
|
|
|
if (!aStart.IsSetAndValid()) {
|
2017-05-30 07:18:25 +03:00
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
}
|
|
|
|
|
2017-10-02 17:58:26 +03:00
|
|
|
if (aStart.Container() == aEnd.Container()) {
|
|
|
|
if (!aEnd.IsSetAndValid()) {
|
2017-05-30 07:18:25 +03:00
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
}
|
2017-10-02 17:58:26 +03:00
|
|
|
// XXX: Offsets - handle this more efficiently.
|
2017-05-30 07:18:25 +03:00
|
|
|
// If the end offset is less than the start offset, this should be
|
|
|
|
// collapsed at the end offset.
|
2017-10-02 17:58:26 +03:00
|
|
|
if (aStart.Offset() > aEnd.Offset()) {
|
|
|
|
DoSetRange(aEnd, aEnd, newStartRoot);
|
2017-05-30 07:18:25 +03:00
|
|
|
} else {
|
2017-10-02 17:58:26 +03:00
|
|
|
DoSetRange(aStart, aEnd, newStartRoot);
|
2017-05-30 07:18:25 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-10-02 17:58:26 +03:00
|
|
|
nsINode* newEndRoot = IsValidBoundary(aEnd.Container());
|
2017-05-30 07:18:25 +03:00
|
|
|
if (!newEndRoot) {
|
|
|
|
return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
|
|
|
|
}
|
2017-10-02 17:58:26 +03:00
|
|
|
if (!aEnd.IsSetAndValid()) {
|
2017-05-30 07:18:25 +03:00
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If they have different root, this should be collapsed at the end point.
|
|
|
|
if (newStartRoot != newEndRoot) {
|
2017-10-02 17:58:26 +03:00
|
|
|
DoSetRange(aEnd, aEnd, newEndRoot);
|
2017-05-30 07:18:25 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the end point is before the start point, this should be collapsed at
|
|
|
|
// the end point.
|
2017-10-02 17:58:26 +03:00
|
|
|
if (nsContentUtils::ComparePoints(aStart, aEnd) == 1) {
|
|
|
|
DoSetRange(aEnd, aEnd, newEndRoot);
|
2017-05-30 07:18:25 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, set the range as specified.
|
2017-10-02 17:58:26 +03:00
|
|
|
DoSetRange(aStart, aEnd, newStartRoot);
|
2017-05-30 07:18:25 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
|
|
|
nsRange::SetEndBeforeJS(nsINode& aNode, ErrorResult& aErr)
|
|
|
|
{
|
2017-03-17 07:32:51 +03:00
|
|
|
AutoCalledByJSRestore calledByJSRestorer(*this);
|
|
|
|
mCalledByJS = true;
|
2017-03-10 10:55:12 +03:00
|
|
|
SetEndBefore(aNode, aErr);
|
|
|
|
}
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
void
|
|
|
|
nsRange::SetEndBefore(nsINode& aNode, ErrorResult& aRv)
|
|
|
|
{
|
2014-09-25 13:04:45 +04:00
|
|
|
if (!nsContentUtils::LegacyIsCallerNativeCode() &&
|
|
|
|
!nsContentUtils::CanCallerAccess(&aNode)) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoInvalidateSelection atEndOfBlock(this);
|
2017-07-19 16:49:52 +03:00
|
|
|
// If the node is being removed from its parent, GetContainerAndOffsetBefore()
|
|
|
|
// returns nullptr. Then, SetEnd() will throw
|
|
|
|
// NS_ERROR_DOM_INVALID_NODE_TYPE_ERR.
|
|
|
|
uint32_t offset = UINT32_MAX;
|
2017-07-11 17:52:39 +03:00
|
|
|
nsINode* container = GetContainerAndOffsetBefore(&aNode, &offset);
|
|
|
|
aRv = SetEnd(container, offset);
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::SetEndBefore(nsIDOMNode* aSibling)
|
1998-12-09 21:44:26 +03:00
|
|
|
{
|
2013-02-07 16:09:41 +04:00
|
|
|
nsCOMPtr<nsINode> sibling = do_QueryInterface(aSibling);
|
|
|
|
if (!sibling) {
|
|
|
|
return NS_ERROR_DOM_NOT_OBJECT_ERR;
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
SetEndBefore(*sibling, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
|
|
|
nsRange::SetEndAfterJS(nsINode& aNode, ErrorResult& aErr)
|
|
|
|
{
|
2017-03-17 07:32:51 +03:00
|
|
|
AutoCalledByJSRestore calledByJSRestorer(*this);
|
|
|
|
mCalledByJS = true;
|
2017-03-10 10:55:12 +03:00
|
|
|
SetEndAfter(aNode, aErr);
|
|
|
|
}
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
void
|
|
|
|
nsRange::SetEndAfter(nsINode& aNode, ErrorResult& aRv)
|
|
|
|
{
|
2014-09-25 13:04:45 +04:00
|
|
|
if (!nsContentUtils::LegacyIsCallerNativeCode() &&
|
|
|
|
!nsContentUtils::CanCallerAccess(&aNode)) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
AutoInvalidateSelection atEndOfBlock(this);
|
2017-07-19 16:49:52 +03:00
|
|
|
// If the node is being removed from its parent, GetContainerAndOffsetAfter()
|
|
|
|
// returns nullptr. Then, SetEnd() will throw
|
|
|
|
// NS_ERROR_DOM_INVALID_NODE_TYPE_ERR.
|
|
|
|
uint32_t offset = UINT32_MAX;
|
2017-07-11 17:52:39 +03:00
|
|
|
nsINode* container = GetContainerAndOffsetAfter(&aNode, &offset);
|
|
|
|
aRv = SetEnd(container, offset);
|
1998-12-09 21:44:26 +03:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::SetEndAfter(nsIDOMNode* aSibling)
|
1998-12-09 21:44:26 +03:00
|
|
|
{
|
2013-02-07 16:09:41 +04:00
|
|
|
nsCOMPtr<nsINode> sibling = do_QueryInterface(aSibling);
|
|
|
|
if (!sibling) {
|
|
|
|
return NS_ERROR_DOM_NOT_OBJECT_ERR;
|
2006-10-21 05:30:54 +04:00
|
|
|
}
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
SetEndAfter(*sibling, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
1998-12-09 21:44:26 +03:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
2011-09-29 10:19:26 +04:00
|
|
|
nsRange::Collapse(bool aToStart)
|
1998-12-03 12:51:05 +03:00
|
|
|
{
|
|
|
|
if (!mIsPositioned)
|
2001-01-07 18:11:50 +03:00
|
|
|
return NS_ERROR_NOT_INITIALIZED;
|
1998-12-03 12:51:05 +03:00
|
|
|
|
2011-12-24 17:26:03 +04:00
|
|
|
AutoInvalidateSelection atEndOfBlock(this);
|
2017-07-11 16:46:11 +03:00
|
|
|
if (aToStart) {
|
2017-08-18 01:05:40 +03:00
|
|
|
DoSetRange(mStart.AsRaw(), mStart.AsRaw(), mRoot);
|
2017-07-11 16:46:11 +03:00
|
|
|
} else {
|
2017-08-18 01:05:40 +03:00
|
|
|
DoSetRange(mEnd.AsRaw(), mEnd.AsRaw(), mRoot);
|
2017-07-11 16:46:11 +03:00
|
|
|
}
|
2006-10-21 05:30:54 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
1998-12-03 12:51:05 +03:00
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
|
|
|
nsRange::CollapseJS(bool aToStart)
|
|
|
|
{
|
2017-03-17 07:32:51 +03:00
|
|
|
AutoCalledByJSRestore calledByJSRestorer(*this);
|
|
|
|
mCalledByJS = true;
|
2017-03-10 10:55:12 +03:00
|
|
|
Unused << Collapse(aToStart);
|
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::SelectNode(nsIDOMNode* aN)
|
1998-12-03 12:51:05 +03:00
|
|
|
{
|
2006-10-21 05:30:54 +04:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aN);
|
2012-03-01 22:35:48 +04:00
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_DOM_INVALID_NODE_TYPE_ERR);
|
2006-10-21 05:30:54 +04:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
SelectNode(*node, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
|
|
|
nsRange::SelectNodeJS(nsINode& aNode, ErrorResult& aErr)
|
|
|
|
{
|
2017-03-17 07:32:51 +03:00
|
|
|
AutoCalledByJSRestore calledByJSRestorer(*this);
|
|
|
|
mCalledByJS = true;
|
2017-03-10 10:55:12 +03:00
|
|
|
SelectNode(aNode, aErr);
|
|
|
|
}
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
void
|
|
|
|
nsRange::SelectNode(nsINode& aNode, ErrorResult& aRv)
|
|
|
|
{
|
2014-09-25 13:04:45 +04:00
|
|
|
if (!nsContentUtils::LegacyIsCallerNativeCode() &&
|
|
|
|
!nsContentUtils::CanCallerAccess(&aNode)) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-11 17:52:39 +03:00
|
|
|
nsINode* container = aNode.GetParentNode();
|
|
|
|
nsINode* newRoot = IsValidBoundary(container);
|
2013-02-07 16:09:41 +04:00
|
|
|
if (!newRoot) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2006-10-21 05:30:54 +04:00
|
|
|
|
2018-01-23 16:30:18 +03:00
|
|
|
int32_t index = container->ComputeIndexOf(&aNode);
|
2017-08-18 01:05:40 +03:00
|
|
|
// MOZ_ASSERT(index != -1);
|
|
|
|
// We need to compute the index here unfortunately, because, while we have
|
|
|
|
// support for XBL, |container| may be the node's binding parent without
|
|
|
|
// actually containing it.
|
2017-07-19 16:49:52 +03:00
|
|
|
if (NS_WARN_IF(index < 0) ||
|
|
|
|
!IsValidOffset(static_cast<uint32_t>(index)) ||
|
|
|
|
!IsValidOffset(static_cast<uint32_t>(index) + 1)) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR);
|
|
|
|
return;
|
2006-10-21 05:30:54 +04:00
|
|
|
}
|
|
|
|
|
2011-12-24 17:26:03 +04:00
|
|
|
AutoInvalidateSelection atEndOfBlock(this);
|
2017-12-05 11:50:13 +03:00
|
|
|
DoSetRange(RawRangeBoundary(container, index),
|
|
|
|
RawRangeBoundary(container, index + 1), newRoot);
|
1998-12-03 12:51:05 +03:00
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::SelectNodeContents(nsIDOMNode* aN)
|
1998-12-03 12:51:05 +03:00
|
|
|
{
|
2013-03-12 07:10:18 +04:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aN);
|
2013-02-07 16:09:41 +04:00
|
|
|
NS_ENSURE_TRUE(node, NS_ERROR_DOM_INVALID_NODE_TYPE_ERR);
|
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
SelectNodeContents(*node, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2017-03-10 10:55:12 +03:00
|
|
|
void
|
|
|
|
nsRange::SelectNodeContentsJS(nsINode& aNode, ErrorResult& aErr)
|
|
|
|
{
|
2017-03-17 07:32:51 +03:00
|
|
|
AutoCalledByJSRestore calledByJSRestorer(*this);
|
|
|
|
mCalledByJS = true;
|
2017-03-10 10:55:12 +03:00
|
|
|
SelectNodeContents(aNode, aErr);
|
|
|
|
}
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
void
|
|
|
|
nsRange::SelectNodeContents(nsINode& aNode, ErrorResult& aRv)
|
|
|
|
{
|
2014-09-25 13:04:45 +04:00
|
|
|
if (!nsContentUtils::LegacyIsCallerNativeCode() &&
|
|
|
|
!nsContentUtils::CanCallerAccess(&aNode)) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsINode* newRoot = IsValidBoundary(&aNode);
|
|
|
|
if (!newRoot) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-12-24 17:26:03 +04:00
|
|
|
AutoInvalidateSelection atEndOfBlock(this);
|
2017-12-05 11:50:13 +03:00
|
|
|
DoSetRange(RawRangeBoundary(&aNode, 0),
|
|
|
|
RawRangeBoundary(&aNode, aNode.Length()), newRoot);
|
1998-12-03 12:51:05 +03:00
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2002-03-18 17:38:11 +03:00
|
|
|
// The Subtree Content Iterator only returns subtrees that are
|
|
|
|
// completely within a given range. It doesn't return a CharacterData
|
2008-10-11 23:46:05 +04:00
|
|
|
// node that contains either the start or end point of the range.,
|
|
|
|
// nor does it return element nodes when nothing in the element is selected.
|
2002-03-18 17:38:11 +03:00
|
|
|
// We need an iterator that will also include these start/end points
|
|
|
|
// so that our methods/algorithms aren't cluttered with special
|
|
|
|
// case code that tries to include these points while iterating.
|
|
|
|
//
|
|
|
|
// The RangeSubtreeIterator class mimics the nsIContentIterator
|
|
|
|
// methods we need, so should the Content Iterator support the
|
|
|
|
// start/end points in the future, we can switchover relatively
|
|
|
|
// easy.
|
|
|
|
|
2013-04-12 07:20:09 +04:00
|
|
|
class MOZ_STACK_CLASS RangeSubtreeIterator
|
2002-03-18 17:38:11 +03:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
|
|
|
|
enum RangeSubtreeIterState { eDone=0,
|
2008-10-11 23:46:05 +04:00
|
|
|
eUseStart,
|
2002-03-18 17:38:11 +03:00
|
|
|
eUseIterator,
|
2008-10-11 23:46:05 +04:00
|
|
|
eUseEnd };
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
nsCOMPtr<nsIContentIterator> mIter;
|
|
|
|
RangeSubtreeIterState mIterState;
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> mStart;
|
|
|
|
nsCOMPtr<nsINode> mEnd;
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
RangeSubtreeIterator()
|
|
|
|
: mIterState(eDone)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~RangeSubtreeIterator()
|
1998-12-08 05:26:15 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
nsresult Init(nsRange *aRange);
|
|
|
|
already_AddRefed<nsINode> GetCurrentNode();
|
2004-01-24 03:46:17 +03:00
|
|
|
void First();
|
|
|
|
void Last();
|
|
|
|
void Next();
|
|
|
|
void Prev();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool IsDone()
|
2004-01-24 03:46:17 +03:00
|
|
|
{
|
|
|
|
return mIterState == eDone;
|
|
|
|
}
|
|
|
|
};
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
nsresult
|
2013-07-12 07:29:24 +04:00
|
|
|
RangeSubtreeIterator::Init(nsRange *aRange)
|
2004-01-24 03:46:17 +03:00
|
|
|
{
|
|
|
|
mIterState = eDone;
|
2013-07-12 07:29:24 +04:00
|
|
|
if (aRange->Collapsed()) {
|
2008-10-11 23:46:05 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
// Grab the start point of the range and QI it to
|
|
|
|
// a CharacterData pointer. If it is CharacterData store
|
|
|
|
// a pointer to the node.
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
nsCOMPtr<nsINode> node = aRange->GetStartContainer(rv);
|
2004-01-24 03:46:17 +03:00
|
|
|
if (!node) return NS_ERROR_FAILURE;
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2008-10-11 23:46:05 +04:00
|
|
|
nsCOMPtr<nsIDOMCharacterData> startData = do_QueryInterface(node);
|
2013-07-12 07:29:24 +04:00
|
|
|
if (startData || (node->IsElement() &&
|
|
|
|
node->AsElement()->GetChildCount() == aRange->GetStartOffset(rv))) {
|
2008-10-11 23:46:05 +04:00
|
|
|
mStart = node;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
// Grab the end point of the range and QI it to
|
|
|
|
// a CharacterData pointer. If it is CharacterData store
|
|
|
|
// a pointer to the node.
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
node = aRange->GetEndContainer(rv);
|
2004-01-24 03:46:17 +03:00
|
|
|
if (!node) return NS_ERROR_FAILURE;
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2008-10-11 23:46:05 +04:00
|
|
|
nsCOMPtr<nsIDOMCharacterData> endData = do_QueryInterface(node);
|
2013-07-12 07:29:24 +04:00
|
|
|
if (endData || (node->IsElement() && aRange->GetEndOffset(rv) == 0)) {
|
2008-10-11 23:46:05 +04:00
|
|
|
mEnd = node;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2008-10-11 23:46:05 +04:00
|
|
|
if (mStart && mStart == mEnd)
|
2004-01-24 03:46:17 +03:00
|
|
|
{
|
|
|
|
// The range starts and stops in the same CharacterData
|
|
|
|
// node. Null out the end pointer so we only visit the
|
|
|
|
// node once!
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mEnd = nullptr;
|
2004-01-24 03:46:17 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Now create a Content Subtree Iterator to be used
|
|
|
|
// for the subtrees between the end points!
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2012-06-11 03:44:50 +04:00
|
|
|
mIter = NS_NewContentSubtreeIterator();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
nsresult res = mIter->Init(aRange);
|
2004-01-24 03:46:17 +03:00
|
|
|
if (NS_FAILED(res)) return res;
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
if (mIter->IsDone())
|
|
|
|
{
|
|
|
|
// The subtree iterator thinks there's nothing
|
|
|
|
// to iterate over, so just free it up so we
|
|
|
|
// don't accidentally call into it.
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mIter = nullptr;
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
// Initialize the iterator by calling First().
|
|
|
|
// Note that we are ignoring the return value on purpose!
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
First();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
already_AddRefed<nsINode>
|
2004-01-24 03:46:17 +03:00
|
|
|
RangeSubtreeIterator::GetCurrentNode()
|
|
|
|
{
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> node;
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2008-10-11 23:46:05 +04:00
|
|
|
if (mIterState == eUseStart && mStart) {
|
2013-04-22 15:15:59 +04:00
|
|
|
node = mStart;
|
|
|
|
} else if (mIterState == eUseEnd && mEnd) {
|
|
|
|
node = mEnd;
|
|
|
|
} else if (mIterState == eUseIterator && mIter) {
|
2013-07-12 07:29:24 +04:00
|
|
|
node = mIter->GetCurrentNode();
|
2004-01-24 03:46:17 +03:00
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2013-04-22 15:15:59 +04:00
|
|
|
return node.forget();
|
2004-01-24 03:46:17 +03:00
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
|
|
|
RangeSubtreeIterator::First()
|
|
|
|
{
|
2008-10-11 23:46:05 +04:00
|
|
|
if (mStart)
|
|
|
|
mIterState = eUseStart;
|
2004-01-24 03:46:17 +03:00
|
|
|
else if (mIter)
|
|
|
|
{
|
|
|
|
mIter->First();
|
|
|
|
|
|
|
|
mIterState = eUseIterator;
|
1998-12-08 05:26:15 +03:00
|
|
|
}
|
2008-10-11 23:46:05 +04:00
|
|
|
else if (mEnd)
|
|
|
|
mIterState = eUseEnd;
|
2004-01-24 03:46:17 +03:00
|
|
|
else
|
|
|
|
mIterState = eDone;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
|
|
|
RangeSubtreeIterator::Last()
|
|
|
|
{
|
2008-10-11 23:46:05 +04:00
|
|
|
if (mEnd)
|
|
|
|
mIterState = eUseEnd;
|
2004-01-24 03:46:17 +03:00
|
|
|
else if (mIter)
|
1998-12-08 05:26:15 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
mIter->Last();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
mIterState = eUseIterator;
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
2008-10-11 23:46:05 +04:00
|
|
|
else if (mStart)
|
|
|
|
mIterState = eUseStart;
|
2004-01-24 03:46:17 +03:00
|
|
|
else
|
|
|
|
mIterState = eDone;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
void
|
|
|
|
RangeSubtreeIterator::Next()
|
|
|
|
{
|
2008-10-11 23:46:05 +04:00
|
|
|
if (mIterState == eUseStart)
|
2002-03-18 17:38:11 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
if (mIter)
|
1998-12-08 05:26:15 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
mIter->First();
|
|
|
|
|
2002-03-18 17:38:11 +03:00
|
|
|
mIterState = eUseIterator;
|
1998-12-08 05:26:15 +03:00
|
|
|
}
|
2008-10-11 23:46:05 +04:00
|
|
|
else if (mEnd)
|
|
|
|
mIterState = eUseEnd;
|
2002-03-18 17:38:11 +03:00
|
|
|
else
|
2004-01-24 03:46:17 +03:00
|
|
|
mIterState = eDone;
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
else if (mIterState == eUseIterator)
|
2002-03-18 17:38:11 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
mIter->Next();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
if (mIter->IsDone())
|
1998-12-08 05:26:15 +03:00
|
|
|
{
|
2008-10-11 23:46:05 +04:00
|
|
|
if (mEnd)
|
|
|
|
mIterState = eUseEnd;
|
2002-03-18 17:38:11 +03:00
|
|
|
else
|
|
|
|
mIterState = eDone;
|
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
mIterState = eDone;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RangeSubtreeIterator::Prev()
|
|
|
|
{
|
2008-10-11 23:46:05 +04:00
|
|
|
if (mIterState == eUseEnd)
|
2004-01-24 03:46:17 +03:00
|
|
|
{
|
|
|
|
if (mIter)
|
2002-03-18 17:38:11 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
mIter->Last();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
mIterState = eUseIterator;
|
1998-12-08 05:26:15 +03:00
|
|
|
}
|
2008-10-11 23:46:05 +04:00
|
|
|
else if (mStart)
|
|
|
|
mIterState = eUseStart;
|
2002-03-18 17:38:11 +03:00
|
|
|
else
|
2004-01-24 03:46:17 +03:00
|
|
|
mIterState = eDone;
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
else if (mIterState == eUseIterator)
|
1998-12-15 02:17:52 +03:00
|
|
|
{
|
2004-01-24 03:46:17 +03:00
|
|
|
mIter->Prev();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
if (mIter->IsDone())
|
1998-12-15 02:17:52 +03:00
|
|
|
{
|
2008-10-11 23:46:05 +04:00
|
|
|
if (mStart)
|
|
|
|
mIterState = eUseStart;
|
2002-03-18 17:38:11 +03:00
|
|
|
else
|
|
|
|
mIterState = eDone;
|
1998-12-15 02:17:52 +03:00
|
|
|
}
|
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
else
|
|
|
|
mIterState = eDone;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
|
2006-08-29 00:12:37 +04:00
|
|
|
// CollapseRangeAfterDelete() is a utility method that is used by
|
2002-03-18 17:38:11 +03:00
|
|
|
// DeleteContents() and ExtractContents() to collapse the range
|
|
|
|
// in the correct place, under the range's root container (the
|
|
|
|
// range end points common container) as outlined by the Range spec:
|
|
|
|
//
|
|
|
|
// http://www.w3.org/TR/2000/REC-DOM-Level-2-Traversal-Range-20001113/ranges.html
|
|
|
|
// The assumption made by this method is that the delete or extract
|
|
|
|
// has been done already, and left the range in a state where there is
|
|
|
|
// no content between the 2 end points.
|
|
|
|
|
2006-10-21 05:30:54 +04:00
|
|
|
static nsresult
|
2013-07-12 07:29:24 +04:00
|
|
|
CollapseRangeAfterDelete(nsRange* aRange)
|
2002-03-18 17:38:11 +03:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aRange);
|
|
|
|
|
|
|
|
// Check if range gravity took care of collapsing the range for us!
|
2013-07-12 07:29:24 +04:00
|
|
|
if (aRange->Collapsed())
|
2002-03-18 17:38:11 +03:00
|
|
|
{
|
|
|
|
// aRange is collapsed so there's nothing for us to do.
|
|
|
|
//
|
|
|
|
// There are 2 possible scenarios here:
|
|
|
|
//
|
|
|
|
// 1. aRange could've been collapsed prior to the delete/extract,
|
|
|
|
// which would've resulted in nothing being removed, so aRange
|
|
|
|
// is already where it should be.
|
|
|
|
//
|
|
|
|
// 2. Prior to the delete/extract, aRange's start and end were in
|
|
|
|
// the same container which would mean everything between them
|
|
|
|
// was removed, causing range gravity to collapse the range.
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// aRange isn't collapsed so figure out the appropriate place to collapse!
|
|
|
|
// First get both end points and their common ancestor.
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
nsCOMPtr<nsINode> commonAncestor = aRange->GetCommonAncestorContainer(rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
if (rv.Failed()) return rv.StealNSResult();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> startContainer = aRange->GetStartContainer(rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
if (rv.Failed()) return rv.StealNSResult();
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> endContainer = aRange->GetEndContainer(rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
if (rv.Failed()) return rv.StealNSResult();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
// Collapse to one of the end points if they are already in the
|
|
|
|
// commonAncestor. This should work ok since this method is called
|
|
|
|
// immediately after a delete or extract that leaves no content
|
|
|
|
// between the 2 end points!
|
|
|
|
|
|
|
|
if (startContainer == commonAncestor)
|
2011-10-17 18:59:28 +04:00
|
|
|
return aRange->Collapse(true);
|
2002-03-18 17:38:11 +03:00
|
|
|
if (endContainer == commonAncestor)
|
2011-10-17 18:59:28 +04:00
|
|
|
return aRange->Collapse(false);
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
// End points are at differing levels. We want to collapse to the
|
|
|
|
// point that is between the 2 subtrees that contain each point,
|
|
|
|
// under the common ancestor.
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> nodeToSelect(startContainer);
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
while (nodeToSelect)
|
|
|
|
{
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> parent = nodeToSelect->GetParentNode();
|
2002-03-18 17:38:11 +03:00
|
|
|
if (parent == commonAncestor)
|
|
|
|
break; // We found the nodeToSelect!
|
|
|
|
|
|
|
|
nodeToSelect = parent;
|
1998-12-15 02:17:52 +03:00
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
if (!nodeToSelect)
|
|
|
|
return NS_ERROR_FAILURE; // This should never happen!
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
aRange->SelectNode(*nodeToSelect, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
if (rv.Failed()) return rv.StealNSResult();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return aRange->Collapse(false);
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
2017-07-11 17:33:04 +03:00
|
|
|
PrependChild(nsINode* aContainer, nsINode* aChild)
|
2008-10-11 23:46:05 +04:00
|
|
|
{
|
2017-07-11 17:33:04 +03:00
|
|
|
nsCOMPtr<nsINode> first = aContainer->GetFirstChild();
|
2013-07-12 07:29:24 +04:00
|
|
|
ErrorResult rv;
|
2017-07-11 17:33:04 +03:00
|
|
|
aContainer->InsertBefore(*aChild, first, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2008-10-11 23:46:05 +04:00
|
|
|
}
|
|
|
|
|
2012-06-28 15:29:56 +04:00
|
|
|
// Helper function for CutContents, making sure that the current node wasn't
|
|
|
|
// removed by mutation events (bug 766426)
|
|
|
|
static bool
|
|
|
|
ValidateCurrentNode(nsRange* aRange, RangeSubtreeIterator& aIter)
|
|
|
|
{
|
|
|
|
bool before, after;
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> node = aIter.GetCurrentNode();
|
|
|
|
if (!node) {
|
2012-07-09 12:41:47 +04:00
|
|
|
// We don't have to worry that the node was removed if it doesn't exist,
|
|
|
|
// e.g., the iterator is done.
|
|
|
|
return true;
|
|
|
|
}
|
2012-06-28 15:29:56 +04:00
|
|
|
|
|
|
|
nsresult res = nsRange::CompareNodeToRange(node, aRange, &before, &after);
|
2016-06-27 01:16:27 +03:00
|
|
|
NS_ENSURE_SUCCESS(res, false);
|
|
|
|
|
|
|
|
if (before || after) {
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> charData = do_QueryInterface(node);
|
|
|
|
if (charData) {
|
|
|
|
// If we're dealing with the start/end container which is a character
|
|
|
|
// node, pretend that the node is in the range.
|
2017-07-11 14:53:04 +03:00
|
|
|
if (before && node == aRange->GetStartContainer()) {
|
2016-06-27 01:16:27 +03:00
|
|
|
before = false;
|
|
|
|
}
|
2017-07-11 15:11:37 +03:00
|
|
|
if (after && node == aRange->GetEndContainer()) {
|
2016-06-27 01:16:27 +03:00
|
|
|
after = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-06-28 15:29:56 +04:00
|
|
|
|
2016-06-27 01:16:27 +03:00
|
|
|
return !before && !after;
|
2012-06-28 15:29:56 +04:00
|
|
|
}
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
nsresult
|
2013-07-12 07:29:24 +04:00
|
|
|
nsRange::CutContents(DocumentFragment** aFragment)
|
|
|
|
{
|
2008-09-09 13:17:35 +04:00
|
|
|
if (aFragment) {
|
2012-07-30 18:20:58 +04:00
|
|
|
*aFragment = nullptr;
|
2008-09-09 13:17:35 +04:00
|
|
|
}
|
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc = mStart.Container()->OwnerDoc();
|
2008-07-25 14:37:37 +04:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
ErrorResult res;
|
|
|
|
nsCOMPtr<nsINode> commonAncestor = GetCommonAncestorContainer(res);
|
2015-04-27 16:18:52 +03:00
|
|
|
NS_ENSURE_TRUE(!res.Failed(), res.StealNSResult());
|
2008-10-11 23:46:05 +04:00
|
|
|
|
2008-07-25 14:37:37 +04:00
|
|
|
// If aFragment isn't null, create a temporary fragment to hold our return.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DocumentFragment> retval;
|
2008-07-25 14:37:37 +04:00
|
|
|
if (aFragment) {
|
2013-07-12 07:29:24 +04:00
|
|
|
retval = new DocumentFragment(doc->NodeInfoManager());
|
2008-07-25 14:37:37 +04:00
|
|
|
}
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> commonCloneAncestor = retval.get();
|
2008-07-25 14:37:37 +04:00
|
|
|
|
2007-03-24 15:18:02 +03:00
|
|
|
// Batch possible DOMSubtreeModified events.
|
2012-07-30 18:20:58 +04:00
|
|
|
mozAutoSubtreeModified subtree(mRoot ? mRoot->OwnerDoc(): nullptr, nullptr);
|
2007-03-24 15:18:02 +03:00
|
|
|
|
2002-03-18 17:38:11 +03:00
|
|
|
// Save the range end points locally to avoid interference
|
|
|
|
// of Range gravity during our edits!
|
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
nsCOMPtr<nsINode> startContainer = mStart.Container();
|
|
|
|
uint32_t startOffset = mStart.Offset();
|
|
|
|
nsCOMPtr<nsINode> endContainer = mEnd.Container();
|
|
|
|
uint32_t endOffset = mEnd.Offset();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2012-04-01 15:45:35 +04:00
|
|
|
if (retval) {
|
|
|
|
// For extractContents(), abort early if there's a doctype (bug 719533).
|
|
|
|
// This can happen only if the common ancestor is a document, in which case
|
|
|
|
// we just need to find its doctype child and check if that's in the range.
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsIDocument> commonAncestorDocument = do_QueryInterface(commonAncestor);
|
2012-04-01 15:45:35 +04:00
|
|
|
if (commonAncestorDocument) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DocumentType> doctype = commonAncestorDocument->GetDoctype();
|
2012-04-01 15:45:35 +04:00
|
|
|
|
|
|
|
if (doctype &&
|
2017-07-19 16:49:52 +03:00
|
|
|
nsContentUtils::ComparePoints(startContainer,
|
|
|
|
static_cast<int32_t>(startOffset),
|
2013-07-12 07:29:24 +04:00
|
|
|
doctype, 0) < 0 &&
|
|
|
|
nsContentUtils::ComparePoints(doctype, 0,
|
2017-07-19 16:49:52 +03:00
|
|
|
endContainer,
|
|
|
|
static_cast<int32_t>(endOffset)) < 0) {
|
2012-04-01 15:45:35 +04:00
|
|
|
return NS_ERROR_DOM_HIERARCHY_REQUEST_ERR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-18 17:38:11 +03:00
|
|
|
// Create and initialize a subtree iterator that will give
|
|
|
|
// us all the subtrees within the range.
|
|
|
|
|
|
|
|
RangeSubtreeIterator iter;
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
nsresult rv = iter.Init(this);
|
2008-07-25 14:37:37 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
if (iter.IsDone())
|
1999-02-12 08:28:46 +03:00
|
|
|
{
|
2002-03-18 17:38:11 +03:00
|
|
|
// There's nothing for us to delete.
|
2008-09-09 13:17:35 +04:00
|
|
|
rv = CollapseRangeAfterDelete(this);
|
|
|
|
if (NS_SUCCEEDED(rv) && aFragment) {
|
2015-03-31 17:03:49 +03:00
|
|
|
retval.forget(aFragment);
|
2008-09-09 13:17:35 +04:00
|
|
|
}
|
|
|
|
return rv;
|
1999-02-12 08:28:46 +03:00
|
|
|
}
|
|
|
|
|
2017-08-09 18:40:21 +03:00
|
|
|
iter.First();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool handled = false;
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
// With the exception of text nodes that contain one of the range
|
|
|
|
// end points, the subtree iterator should only give us back subtrees
|
|
|
|
// that are completely contained between the range's end points.
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
while (!iter.IsDone())
|
1999-02-12 08:28:46 +03:00
|
|
|
{
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> nodeToResult;
|
|
|
|
nsCOMPtr<nsINode> node = iter.GetCurrentNode();
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2017-08-09 18:40:21 +03:00
|
|
|
// Before we delete anything, advance the iterator to the next node that's
|
|
|
|
// not a descendant of this one. XXX It's a bit silly to iterate through
|
|
|
|
// the descendants only to throw them out, we should use an iterator that
|
|
|
|
// skips the descendants to begin with.
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2017-08-09 18:40:21 +03:00
|
|
|
iter.Next();
|
|
|
|
nsCOMPtr<nsINode> nextNode = iter.GetCurrentNode();
|
|
|
|
while (nextNode && nsContentUtils::ContentIsDescendantOf(nextNode, node)) {
|
|
|
|
iter.Next();
|
|
|
|
nextNode = iter.GetCurrentNode();
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
handled = false;
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
// If it's CharacterData, make sure we might need to delete
|
|
|
|
// part of the data, instead of removing the whole node.
|
|
|
|
//
|
|
|
|
// XXX_kin: We need to also handle ProcessingInstruction
|
|
|
|
// XXX_kin: according to the spec.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> charData(do_QueryInterface(node));
|
|
|
|
|
|
|
|
if (charData)
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t dataLength = 0;
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
if (node == startContainer)
|
|
|
|
{
|
|
|
|
if (node == endContainer)
|
|
|
|
{
|
|
|
|
// This range is completely contained within a single text node.
|
2008-07-25 14:37:37 +04:00
|
|
|
// Delete or extract the data between startOffset and endOffset.
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
if (endOffset > startOffset)
|
|
|
|
{
|
2009-06-28 23:48:05 +04:00
|
|
|
if (retval) {
|
|
|
|
nsAutoString cutValue;
|
|
|
|
rv = charData->SubstringData(startOffset, endOffset - startOffset,
|
|
|
|
cutValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-01-30 07:10:50 +03:00
|
|
|
ErrorResult err;
|
|
|
|
nsCOMPtr<nsINode> clone = node->CloneNode(false, err);
|
|
|
|
if (NS_WARN_IF(err.Failed())) {
|
|
|
|
return err.StealNSResult();
|
|
|
|
}
|
|
|
|
clone->SetNodeValue(cutValue, err);
|
|
|
|
if (NS_WARN_IF(err.Failed())) {
|
|
|
|
return err.StealNSResult();
|
|
|
|
}
|
|
|
|
nodeToResult = clone;
|
2009-06-28 23:48:05 +04:00
|
|
|
}
|
|
|
|
|
2012-06-28 15:29:56 +04:00
|
|
|
nsMutationGuard guard;
|
2009-06-28 23:48:05 +04:00
|
|
|
rv = charData->DeleteData(startOffset, endOffset - startOffset);
|
2008-07-25 14:37:37 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2012-06-28 15:29:56 +04:00
|
|
|
NS_ENSURE_STATE(!guard.Mutated(0) ||
|
|
|
|
ValidateCurrentNode(this, iter));
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
handled = true;
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2008-07-25 14:37:37 +04:00
|
|
|
// Delete or extract everything after startOffset.
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2008-07-25 14:37:37 +04:00
|
|
|
rv = charData->GetLength(&dataLength);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2017-07-19 16:49:52 +03:00
|
|
|
if (dataLength >= startOffset) {
|
2017-08-09 17:39:06 +03:00
|
|
|
if (retval) {
|
|
|
|
nsAutoString cutValue;
|
|
|
|
rv = charData->SubstringData(startOffset, dataLength, cutValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-01-30 07:10:50 +03:00
|
|
|
ErrorResult err;
|
|
|
|
nsCOMPtr<nsINode> clone = node->CloneNode(false, err);
|
|
|
|
if (NS_WARN_IF(err.Failed())) {
|
|
|
|
return err.StealNSResult();
|
|
|
|
}
|
|
|
|
clone->SetNodeValue(cutValue, err);
|
|
|
|
if (NS_WARN_IF(err.Failed())) {
|
|
|
|
return err.StealNSResult();
|
|
|
|
}
|
|
|
|
nodeToResult = clone;
|
2017-08-09 17:39:06 +03:00
|
|
|
}
|
|
|
|
|
2012-06-28 15:29:56 +04:00
|
|
|
nsMutationGuard guard;
|
2017-08-09 17:39:06 +03:00
|
|
|
rv = charData->DeleteData(startOffset, dataLength);
|
2008-07-25 14:37:37 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2017-08-09 17:39:06 +03:00
|
|
|
NS_ENSURE_STATE(!guard.Mutated(0) ||
|
2012-06-28 15:29:56 +04:00
|
|
|
ValidateCurrentNode(this, iter));
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
handled = true;
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else if (node == endContainer)
|
|
|
|
{
|
2008-07-25 14:37:37 +04:00
|
|
|
// Delete or extract everything before endOffset.
|
2017-08-09 17:39:06 +03:00
|
|
|
if (retval) {
|
|
|
|
nsAutoString cutValue;
|
|
|
|
rv = charData->SubstringData(0, endOffset, cutValue);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2018-01-30 07:10:50 +03:00
|
|
|
ErrorResult err;
|
|
|
|
nsCOMPtr<nsINode> clone = node->CloneNode(false, err);
|
|
|
|
if (NS_WARN_IF(err.Failed())) {
|
|
|
|
return err.StealNSResult();
|
|
|
|
}
|
|
|
|
clone->SetNodeValue(cutValue, err);
|
|
|
|
if (NS_WARN_IF(err.Failed())) {
|
|
|
|
return err.StealNSResult();
|
|
|
|
}
|
|
|
|
nodeToResult = clone;
|
2017-08-09 17:39:06 +03:00
|
|
|
}
|
|
|
|
|
2017-07-19 16:49:52 +03:00
|
|
|
nsMutationGuard guard;
|
2017-08-09 17:39:06 +03:00
|
|
|
rv = charData->DeleteData(0, endOffset);
|
2017-07-19 16:49:52 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2017-08-09 17:39:06 +03:00
|
|
|
NS_ENSURE_STATE(!guard.Mutated(0) ||
|
2017-07-19 16:49:52 +03:00
|
|
|
ValidateCurrentNode(this, iter));
|
2011-10-17 18:59:28 +04:00
|
|
|
handled = true;
|
2013-07-12 07:29:24 +04:00
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
|
|
|
|
2008-10-11 23:46:05 +04:00
|
|
|
if (!handled && (node == endContainer || node == startContainer))
|
|
|
|
{
|
2013-07-12 07:29:24 +04:00
|
|
|
if (node && node->IsElement() &&
|
2008-10-11 23:46:05 +04:00
|
|
|
((node == endContainer && endOffset == 0) ||
|
|
|
|
(node == startContainer &&
|
2017-07-19 16:49:52 +03:00
|
|
|
node->AsElement()->GetChildCount() == startOffset))) {
|
2008-10-11 23:46:05 +04:00
|
|
|
if (retval) {
|
2013-07-12 07:29:24 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
nodeToResult = node->CloneNode(false, rv);
|
2015-04-27 16:18:52 +03:00
|
|
|
NS_ENSURE_TRUE(!rv.Failed(), rv.StealNSResult());
|
2008-10-11 23:46:05 +04:00
|
|
|
}
|
2011-10-17 18:59:28 +04:00
|
|
|
handled = true;
|
2008-10-11 23:46:05 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2002-03-18 17:38:11 +03:00
|
|
|
if (!handled)
|
|
|
|
{
|
|
|
|
// node was not handled above, so it must be completely contained
|
|
|
|
// within the range. Just remove it from the tree!
|
2008-10-11 23:46:05 +04:00
|
|
|
nodeToResult = node;
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t parentCount = 0;
|
2008-10-11 23:46:05 +04:00
|
|
|
// Set the result to document fragment if we have 'retval'.
|
|
|
|
if (retval) {
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> oldCommonAncestor = commonAncestor;
|
2008-10-11 23:46:05 +04:00
|
|
|
if (!iter.IsDone()) {
|
|
|
|
// Setup the parameters for the next iteration of the loop.
|
2017-08-09 18:40:21 +03:00
|
|
|
NS_ENSURE_STATE(nextNode);
|
2008-10-11 23:46:05 +04:00
|
|
|
|
2017-08-09 18:40:21 +03:00
|
|
|
// Get node's and nextNode's common parent. Do this before moving
|
2008-10-11 23:46:05 +04:00
|
|
|
// nodes from original DOM to result fragment.
|
2017-08-09 18:40:21 +03:00
|
|
|
commonAncestor = nsContentUtils::GetCommonAncestor(node, nextNode);
|
2008-10-11 23:46:05 +04:00
|
|
|
NS_ENSURE_STATE(commonAncestor);
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> parentCounterNode = node;
|
2008-10-11 23:46:05 +04:00
|
|
|
while (parentCounterNode && parentCounterNode != commonAncestor)
|
|
|
|
{
|
|
|
|
++parentCount;
|
2013-07-12 07:29:24 +04:00
|
|
|
parentCounterNode = parentCounterNode->GetParentNode();
|
2008-10-11 23:46:05 +04:00
|
|
|
NS_ENSURE_STATE(parentCounterNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone the parent hierarchy between commonAncestor and node.
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> closestAncestor, farthestAncestor;
|
2008-10-11 23:46:05 +04:00
|
|
|
rv = CloneParentsBetween(oldCommonAncestor, node,
|
|
|
|
getter_AddRefs(closestAncestor),
|
|
|
|
getter_AddRefs(farthestAncestor));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
2017-08-09 18:40:21 +03:00
|
|
|
ErrorResult res;
|
2008-10-11 23:46:05 +04:00
|
|
|
if (farthestAncestor)
|
|
|
|
{
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> n = do_QueryInterface(commonCloneAncestor);
|
2017-08-09 18:40:21 +03:00
|
|
|
n->AppendChild(*farthestAncestor, res);
|
2017-10-23 23:30:18 +03:00
|
|
|
res.WouldReportJSException();
|
2017-08-09 18:40:21 +03:00
|
|
|
if (NS_WARN_IF(res.Failed())) {
|
|
|
|
return res.StealNSResult();
|
|
|
|
}
|
2005-07-26 19:45:49 +04:00
|
|
|
}
|
2008-10-11 23:46:05 +04:00
|
|
|
|
2012-06-28 15:29:56 +04:00
|
|
|
nsMutationGuard guard;
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> parent = nodeToResult->GetParentNode();
|
2017-08-09 18:40:21 +03:00
|
|
|
if (closestAncestor) {
|
|
|
|
closestAncestor->AppendChild(*nodeToResult, res);
|
|
|
|
} else {
|
|
|
|
commonCloneAncestor->AppendChild(*nodeToResult, res);
|
|
|
|
}
|
2017-10-23 23:30:18 +03:00
|
|
|
res.WouldReportJSException();
|
2017-08-09 18:40:21 +03:00
|
|
|
if (NS_WARN_IF(res.Failed())) {
|
|
|
|
return res.StealNSResult();
|
|
|
|
}
|
2012-06-28 15:29:56 +04:00
|
|
|
NS_ENSURE_STATE(!guard.Mutated(parent ? 2 : 1) ||
|
|
|
|
ValidateCurrentNode(this, iter));
|
2008-10-11 23:46:05 +04:00
|
|
|
} else if (nodeToResult) {
|
2012-06-28 15:29:56 +04:00
|
|
|
nsMutationGuard guard;
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> node = nodeToResult;
|
2016-02-24 03:23:43 +03:00
|
|
|
nsCOMPtr<nsINode> parent = node->GetParentNode();
|
2012-10-09 16:31:24 +04:00
|
|
|
if (parent) {
|
|
|
|
mozilla::ErrorResult error;
|
|
|
|
parent->RemoveChild(*node, error);
|
2015-04-27 16:18:52 +03:00
|
|
|
NS_ENSURE_FALSE(error.Failed(), error.StealNSResult());
|
2012-10-09 16:31:24 +04:00
|
|
|
}
|
2012-06-28 15:29:56 +04:00
|
|
|
NS_ENSURE_STATE(!guard.Mutated(1) ||
|
|
|
|
ValidateCurrentNode(this, iter));
|
2008-10-11 23:46:05 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!iter.IsDone() && retval) {
|
|
|
|
// Find the equivalent of commonAncestor in the cloned tree.
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> newCloneAncestor = nodeToResult;
|
2012-08-22 19:56:38 +04:00
|
|
|
for (uint32_t i = parentCount; i; --i)
|
2008-10-11 23:46:05 +04:00
|
|
|
{
|
2013-07-12 07:29:24 +04:00
|
|
|
newCloneAncestor = newCloneAncestor->GetParentNode();
|
2008-10-11 23:46:05 +04:00
|
|
|
NS_ENSURE_STATE(newCloneAncestor);
|
|
|
|
}
|
|
|
|
commonCloneAncestor = newCloneAncestor;
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
1999-02-12 08:28:46 +03:00
|
|
|
}
|
1999-01-29 21:57:56 +03:00
|
|
|
|
2008-09-09 13:17:35 +04:00
|
|
|
rv = CollapseRangeAfterDelete(this);
|
|
|
|
if (NS_SUCCEEDED(rv) && aFragment) {
|
2015-03-31 17:03:49 +03:00
|
|
|
retval.forget(aFragment);
|
2008-07-25 14:37:37 +04:00
|
|
|
}
|
2008-09-09 13:17:35 +04:00
|
|
|
return rv;
|
1998-12-08 05:26:15 +03:00
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::DeleteContents()
|
2008-07-25 14:37:37 +04:00
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
return CutContents(nullptr);
|
2008-07-25 14:37:37 +04:00
|
|
|
}
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
void
|
|
|
|
nsRange::DeleteContents(ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
aRv = CutContents(nullptr);
|
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::ExtractContents(nsIDOMDocumentFragment** aReturn)
|
2008-07-25 14:37:37 +04:00
|
|
|
{
|
|
|
|
NS_ENSURE_ARG_POINTER(aReturn);
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DocumentFragment> fragment;
|
2013-02-07 16:09:41 +04:00
|
|
|
nsresult rv = CutContents(getter_AddRefs(fragment));
|
|
|
|
fragment.forget(aReturn);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
already_AddRefed<DocumentFragment>
|
2013-02-07 16:09:41 +04:00
|
|
|
nsRange::ExtractContents(ErrorResult& rv)
|
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DocumentFragment> fragment;
|
2013-02-07 16:09:41 +04:00
|
|
|
rv = CutContents(getter_AddRefs(fragment));
|
|
|
|
return fragment.forget();
|
2008-07-25 14:37:37 +04:00
|
|
|
}
|
|
|
|
|
2003-01-15 02:05:52 +03:00
|
|
|
NS_IMETHODIMP
|
2012-08-22 19:56:38 +04:00
|
|
|
nsRange::CompareBoundaryPoints(uint16_t aHow, nsIDOMRange* aOtherRange,
|
|
|
|
int16_t* aCmpRet)
|
1998-12-09 21:44:26 +03:00
|
|
|
{
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange* otherRange = static_cast<nsRange*>(aOtherRange);
|
2006-10-21 05:30:54 +04:00
|
|
|
NS_ENSURE_TRUE(otherRange, NS_ERROR_NULL_POINTER);
|
1998-12-09 21:44:26 +03:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
*aCmpRet = CompareBoundaryPoints(aHow, *otherRange, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int16_t
|
|
|
|
nsRange::CompareBoundaryPoints(uint16_t aHow, nsRange& aOtherRange,
|
|
|
|
ErrorResult& rv)
|
|
|
|
{
|
|
|
|
if (!mIsPositioned || !aOtherRange.IsPositioned()) {
|
|
|
|
rv.Throw(NS_ERROR_NOT_INITIALIZED);
|
|
|
|
return 0;
|
|
|
|
}
|
2000-12-27 03:37:01 +03:00
|
|
|
|
2006-10-21 05:30:54 +04:00
|
|
|
nsINode *ourNode, *otherNode;
|
2017-07-19 16:49:52 +03:00
|
|
|
uint32_t ourOffset, otherOffset;
|
1998-12-09 22:21:49 +03:00
|
|
|
|
2006-10-21 05:30:54 +04:00
|
|
|
switch (aHow) {
|
|
|
|
case nsIDOMRange::START_TO_START:
|
2017-08-18 01:05:40 +03:00
|
|
|
ourNode = mStart.Container();
|
|
|
|
ourOffset = mStart.Offset();
|
2017-07-11 14:53:04 +03:00
|
|
|
otherNode = aOtherRange.GetStartContainer();
|
2013-02-07 16:09:41 +04:00
|
|
|
otherOffset = aOtherRange.StartOffset();
|
2006-10-21 05:30:54 +04:00
|
|
|
break;
|
|
|
|
case nsIDOMRange::START_TO_END:
|
2017-08-18 01:05:40 +03:00
|
|
|
ourNode = mEnd.Container();
|
|
|
|
ourOffset = mEnd.Offset();
|
2017-07-11 14:53:04 +03:00
|
|
|
otherNode = aOtherRange.GetStartContainer();
|
2013-02-07 16:09:41 +04:00
|
|
|
otherOffset = aOtherRange.StartOffset();
|
2006-10-21 05:30:54 +04:00
|
|
|
break;
|
|
|
|
case nsIDOMRange::END_TO_START:
|
2017-08-18 01:05:40 +03:00
|
|
|
ourNode = mStart.Container();
|
|
|
|
ourOffset = mStart.Offset();
|
2017-07-11 15:11:37 +03:00
|
|
|
otherNode = aOtherRange.GetEndContainer();
|
2013-02-07 16:09:41 +04:00
|
|
|
otherOffset = aOtherRange.EndOffset();
|
2006-10-21 05:30:54 +04:00
|
|
|
break;
|
|
|
|
case nsIDOMRange::END_TO_END:
|
2017-08-18 01:05:40 +03:00
|
|
|
ourNode = mEnd.Container();
|
|
|
|
ourOffset = mEnd.Offset();
|
2017-07-11 15:11:37 +03:00
|
|
|
otherNode = aOtherRange.GetEndContainer();
|
2013-02-07 16:09:41 +04:00
|
|
|
otherOffset = aOtherRange.EndOffset();
|
2006-10-21 05:30:54 +04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
// We were passed an illegal value
|
2013-02-07 16:09:41 +04:00
|
|
|
rv.Throw(NS_ERROR_DOM_NOT_SUPPORTED_ERR);
|
|
|
|
return 0;
|
1998-12-09 21:44:26 +03:00
|
|
|
}
|
1998-12-09 22:21:49 +03:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
if (mRoot != aOtherRange.GetRoot()) {
|
|
|
|
rv.Throw(NS_ERROR_DOM_WRONG_DOCUMENT_ERR);
|
|
|
|
return 0;
|
|
|
|
}
|
2000-12-27 03:37:01 +03:00
|
|
|
|
2017-07-19 16:49:52 +03:00
|
|
|
return nsContentUtils::ComparePoints(ourNode,
|
|
|
|
static_cast<int32_t>(ourOffset),
|
|
|
|
otherNode,
|
|
|
|
static_cast<int32_t>(otherOffset));
|
1998-12-09 21:44:26 +03:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
/* static */ nsresult
|
2013-07-12 07:29:24 +04:00
|
|
|
nsRange::CloneParentsBetween(nsINode *aAncestor,
|
|
|
|
nsINode *aNode,
|
|
|
|
nsINode **aClosestAncestor,
|
|
|
|
nsINode **aFarthestAncestor)
|
1998-12-15 21:47:44 +03:00
|
|
|
{
|
2002-03-18 17:38:11 +03:00
|
|
|
NS_ENSURE_ARG_POINTER((aAncestor && aNode && aClosestAncestor && aFarthestAncestor));
|
1998-12-15 21:47:44 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
*aClosestAncestor = nullptr;
|
|
|
|
*aFarthestAncestor = nullptr;
|
1998-12-15 21:47:44 +03:00
|
|
|
|
2002-03-18 17:38:11 +03:00
|
|
|
if (aAncestor == aNode)
|
|
|
|
return NS_OK;
|
1998-12-15 21:47:44 +03:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> firstParent, lastParent;
|
|
|
|
nsCOMPtr<nsINode> parent = aNode->GetParentNode();
|
1998-12-15 21:47:44 +03:00
|
|
|
|
2002-03-18 17:38:11 +03:00
|
|
|
while(parent && parent != aAncestor)
|
1998-12-15 21:47:44 +03:00
|
|
|
{
|
2013-07-12 07:29:24 +04:00
|
|
|
ErrorResult rv;
|
|
|
|
nsCOMPtr<nsINode> clone = parent->CloneNode(false, rv);
|
1998-12-15 21:47:44 +03:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
if (rv.Failed()) {
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-07-12 07:29:24 +04:00
|
|
|
}
|
|
|
|
if (!clone) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
if (! firstParent) {
|
2002-03-18 17:38:11 +03:00
|
|
|
firstParent = lastParent = clone;
|
2013-07-12 07:29:24 +04:00
|
|
|
} else {
|
|
|
|
clone->AppendChild(*lastParent, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
if (rv.Failed()) return rv.StealNSResult();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
lastParent = clone;
|
1998-12-15 21:47:44 +03:00
|
|
|
}
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
parent = parent->GetParentNode();
|
1998-12-15 21:47:44 +03:00
|
|
|
}
|
|
|
|
|
2002-03-18 17:38:11 +03:00
|
|
|
*aClosestAncestor = firstParent;
|
|
|
|
NS_IF_ADDREF(*aClosestAncestor);
|
1998-12-15 21:47:44 +03:00
|
|
|
|
2002-03-18 17:38:11 +03:00
|
|
|
*aFarthestAncestor = lastParent;
|
|
|
|
NS_IF_ADDREF(*aFarthestAncestor);
|
1998-12-15 21:47:44 +03:00
|
|
|
|
2002-03-18 17:38:11 +03:00
|
|
|
return NS_OK;
|
1998-12-15 21:47:44 +03:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::CloneContents(nsIDOMDocumentFragment** aReturn)
|
1998-12-10 21:58:49 +03:00
|
|
|
{
|
2013-02-07 16:09:41 +04:00
|
|
|
ErrorResult rv;
|
2014-03-15 23:00:15 +04:00
|
|
|
*aReturn = CloneContents(rv).take();
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
already_AddRefed<DocumentFragment>
|
2013-02-07 16:09:41 +04:00
|
|
|
nsRange::CloneContents(ErrorResult& aRv)
|
|
|
|
{
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> commonAncestor = GetCommonAncestorContainer(aRv);
|
2013-02-07 16:09:41 +04:00
|
|
|
MOZ_ASSERT(!aRv.Failed(), "GetCommonAncestorContainer() shouldn't fail!");
|
2001-05-19 01:05:51 +04:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc = mStart.Container()->OwnerDoc();
|
2013-07-12 07:29:24 +04:00
|
|
|
NS_ASSERTION(doc, "CloneContents needs a document to continue.");
|
|
|
|
if (!doc) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2005-10-26 21:06:41 +04:00
|
|
|
|
2001-07-21 06:53:43 +04:00
|
|
|
// Create a new document fragment in the context of this document,
|
|
|
|
// which might be null
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2001-07-21 06:53:43 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DocumentFragment> clonedFrag =
|
2013-07-12 07:29:24 +04:00
|
|
|
new DocumentFragment(doc->NodeInfoManager());
|
2001-07-21 06:53:43 +04:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> commonCloneAncestor = clonedFrag.get();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
// Create and initialize a subtree iterator that will give
|
|
|
|
// us all the subtrees within the range.
|
|
|
|
|
|
|
|
RangeSubtreeIterator iter;
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv = iter.Init(this);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
if (iter.IsDone())
|
1998-12-10 21:58:49 +03:00
|
|
|
{
|
2002-03-18 17:38:11 +03:00
|
|
|
// There's nothing to add to the doc frag, we must be done!
|
2013-02-07 16:09:41 +04:00
|
|
|
return clonedFrag.forget();
|
2001-05-19 01:05:51 +04:00
|
|
|
}
|
1998-12-15 21:47:44 +03:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
iter.First();
|
1998-12-15 21:47:44 +03:00
|
|
|
|
2002-03-18 17:38:11 +03:00
|
|
|
// With the exception of text nodes that contain one of the range
|
2008-10-11 23:46:05 +04:00
|
|
|
// end points and elements which don't have any content selected the subtree
|
|
|
|
// iterator should only give us back subtrees that are completely contained
|
|
|
|
// between the range's end points.
|
2002-03-18 17:38:11 +03:00
|
|
|
//
|
|
|
|
// Unfortunately these subtrees don't contain the parent hierarchy/context
|
|
|
|
// that the Range spec requires us to return. This loop clones the
|
|
|
|
// parent hierarchy, adds a cloned version of the subtree, to it, then
|
|
|
|
// correctly places this new subtree into the doc fragment.
|
2001-06-27 00:45:18 +04:00
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
while (!iter.IsDone())
|
2001-05-19 01:05:51 +04:00
|
|
|
{
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> node = iter.GetCurrentNode();
|
|
|
|
bool deepClone = !node->IsElement() ||
|
2017-08-18 01:05:40 +03:00
|
|
|
(!(node == mEnd.Container() && mEnd.Offset() == 0) &&
|
|
|
|
!(node == mStart.Container() &&
|
|
|
|
mStart.Offset() == node->AsElement()->GetChildCount()));
|
2008-10-11 23:46:05 +04:00
|
|
|
|
2002-03-18 17:38:11 +03:00
|
|
|
// Clone the current subtree!
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> clone = node->CloneNode(deepClone, aRv);
|
2013-02-07 16:09:41 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
// If it's CharacterData, make sure we only clone what
|
|
|
|
// is in the range.
|
|
|
|
//
|
|
|
|
// XXX_kin: We need to also handle ProcessingInstruction
|
|
|
|
// XXX_kin: according to the spec.
|
|
|
|
|
|
|
|
nsCOMPtr<nsIDOMCharacterData> charData(do_QueryInterface(clone));
|
|
|
|
|
|
|
|
if (charData)
|
|
|
|
{
|
2017-08-18 01:05:40 +03:00
|
|
|
if (node == mEnd.Container()) {
|
2002-03-18 17:38:11 +03:00
|
|
|
// We only need the data before mEndOffset, so get rid of any
|
|
|
|
// data after it.
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t dataLength = 0;
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv = charData->GetLength(&dataLength);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
if (dataLength > (uint32_t)mEnd.Offset())
|
|
|
|
{
|
|
|
|
aRv = charData->DeleteData(mEnd.Offset(), dataLength - mEnd.Offset());
|
2013-02-07 16:09:41 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
2013-07-12 07:29:24 +04:00
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
if (node == mStart.Container()) {
|
2002-03-18 17:38:11 +03:00
|
|
|
// We don't need any data before mStartOffset, so just
|
|
|
|
// delete it!
|
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
if (mStart.Offset() > 0)
|
2002-03-18 17:38:11 +03:00
|
|
|
{
|
2017-08-18 01:05:40 +03:00
|
|
|
aRv = charData->DeleteData(0, mStart.Offset());
|
2013-02-07 16:09:41 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clone the parent hierarchy between commonAncestor and node.
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> closestAncestor, farthestAncestor;
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv = CloneParentsBetween(commonAncestor, node,
|
2002-03-18 17:38:11 +03:00
|
|
|
getter_AddRefs(closestAncestor),
|
|
|
|
getter_AddRefs(farthestAncestor));
|
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
// Hook the parent hierarchy/context of the subtree into the clone tree.
|
|
|
|
|
|
|
|
if (farthestAncestor)
|
|
|
|
{
|
2013-07-12 07:29:24 +04:00
|
|
|
commonCloneAncestor->AppendChild(*farthestAncestor, aRv);
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Place the cloned subtree into the cloned doc frag tree!
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> cloneNode = do_QueryInterface(clone);
|
2002-03-18 17:38:11 +03:00
|
|
|
if (closestAncestor)
|
|
|
|
{
|
|
|
|
// Append the subtree under closestAncestor since it is the
|
|
|
|
// immediate parent of the subtree.
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
closestAncestor->AppendChild(*cloneNode, aRv);
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-07-12 07:29:24 +04:00
|
|
|
// If we get here, there is no missing parent hierarchy between
|
2002-03-18 17:38:11 +03:00
|
|
|
// commonAncestor and node, so just append clone to commonCloneAncestor.
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
commonCloneAncestor->AppendChild(*cloneNode, aRv);
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get the next subtree to be processed. The idea here is to setup
|
|
|
|
// the parameters for the next iteration of the loop.
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
iter.Next();
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
if (iter.IsDone())
|
|
|
|
break; // We must be done!
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> nextNode = iter.GetCurrentNode();
|
2013-02-07 16:09:41 +04:00
|
|
|
if (!nextNode) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
// Get node and nextNode's common parent.
|
2013-07-12 07:29:24 +04:00
|
|
|
commonAncestor = nsContentUtils::GetCommonAncestor(node, nextNode);
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
if (!commonAncestor) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
|
|
|
// Find the equivalent of commonAncestor in the cloned tree!
|
|
|
|
|
|
|
|
while (node && node != commonAncestor)
|
|
|
|
{
|
2013-07-12 07:29:24 +04:00
|
|
|
node = node->GetParentNode();
|
2013-02-07 16:09:41 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!node) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
cloneNode = cloneNode->GetParentNode();
|
|
|
|
if (!cloneNode) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2002-03-18 17:38:11 +03:00
|
|
|
}
|
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
commonCloneAncestor = cloneNode;
|
2001-05-19 01:05:51 +04:00
|
|
|
}
|
2001-06-27 00:45:18 +04:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
return clonedFrag.forget();
|
1998-12-10 21:58:49 +03:00
|
|
|
}
|
|
|
|
|
2012-06-25 15:44:11 +04:00
|
|
|
already_AddRefed<nsRange>
|
|
|
|
nsRange::CloneRange() const
|
1998-12-10 21:58:49 +03:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRange> range = new nsRange(mOwner);
|
1998-12-10 21:58:49 +03:00
|
|
|
|
2007-06-19 07:01:03 +04:00
|
|
|
range->SetMaySpanAnonymousSubtrees(mMaySpanAnonymousSubtrees);
|
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
range->DoSetRange(mStart.AsRaw(), mEnd.AsRaw(), mRoot);
|
2006-10-21 06:05:10 +04:00
|
|
|
|
2012-06-25 15:44:11 +04:00
|
|
|
return range.forget();
|
1998-12-10 21:58:49 +03:00
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2012-01-10 18:19:54 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::CloneRange(nsIDOMRange** aReturn)
|
2009-04-06 21:17:37 +04:00
|
|
|
{
|
2014-03-15 23:00:15 +04:00
|
|
|
*aReturn = CloneRange().take();
|
2012-06-25 15:44:11 +04:00
|
|
|
return NS_OK;
|
2009-04-06 21:17:37 +04:00
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
2012-04-01 15:50:31 +04:00
|
|
|
nsRange::InsertNode(nsIDOMNode* aNode)
|
2001-01-10 00:44:35 +03:00
|
|
|
{
|
2013-02-07 16:09:41 +04:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNode);
|
|
|
|
if (!node) {
|
|
|
|
return NS_ERROR_DOM_NOT_OBJECT_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
ErrorResult rv;
|
|
|
|
InsertNode(*node, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRange::InsertNode(nsINode& aNode, ErrorResult& aRv)
|
|
|
|
{
|
2014-09-25 13:04:45 +04:00
|
|
|
if (!nsContentUtils::LegacyIsCallerNativeCode() &&
|
|
|
|
!nsContentUtils::CanCallerAccess(&aNode)) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2017-07-19 16:49:52 +03:00
|
|
|
uint32_t tStartOffset = StartOffset();
|
2001-05-19 01:05:51 +04:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> tStartContainer = GetStartContainer(aRv);
|
2013-02-07 16:09:41 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2006-03-03 05:53:44 +03:00
|
|
|
|
2016-08-16 15:52:06 +03:00
|
|
|
if (&aNode == tStartContainer) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-04-01 15:50:31 +04:00
|
|
|
// This is the node we'll be inserting before, and its parent
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> referenceNode;
|
|
|
|
nsCOMPtr<nsINode> referenceParentNode = tStartContainer;
|
2001-05-19 01:05:51 +04:00
|
|
|
|
2012-04-01 15:50:31 +04:00
|
|
|
nsCOMPtr<nsIDOMText> startTextNode(do_QueryInterface(tStartContainer));
|
2018-01-30 07:10:51 +03:00
|
|
|
nsCOMPtr<nsINodeList> tChildList;
|
2012-04-01 15:50:31 +04:00
|
|
|
if (startTextNode) {
|
2013-07-12 07:29:24 +04:00
|
|
|
referenceParentNode = tStartContainer->GetParentNode();
|
2013-02-07 16:09:41 +04:00
|
|
|
if (!referenceParentNode) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_HIERARCHY_REQUEST_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2009-08-19 04:18:24 +04:00
|
|
|
|
2015-10-07 16:07:39 +03:00
|
|
|
referenceParentNode->EnsurePreInsertionValidity(aNode, tStartContainer,
|
|
|
|
aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2003-04-26 05:14:42 +04:00
|
|
|
nsCOMPtr<nsIDOMText> secondPart;
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv = startTextNode->SplitText(tStartOffset, getter_AddRefs(secondPart));
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2009-08-19 04:18:24 +04:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
referenceNode = do_QueryInterface(secondPart);
|
2012-04-01 15:50:31 +04:00
|
|
|
} else {
|
2018-01-30 07:10:51 +03:00
|
|
|
tChildList = tStartContainer->ChildNodes();
|
2001-05-19 01:05:51 +04:00
|
|
|
|
2012-04-01 15:50:31 +04:00
|
|
|
// find the insertion point in the DOM and insert the Node
|
2018-01-30 07:10:51 +03:00
|
|
|
referenceNode = tChildList->Item(tStartOffset);
|
2015-10-07 16:07:39 +03:00
|
|
|
|
|
|
|
tStartContainer->EnsurePreInsertionValidity(aNode, referenceNode, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-04-01 15:50:31 +04:00
|
|
|
}
|
2012-04-01 16:02:21 +04:00
|
|
|
|
2012-06-19 17:17:37 +04:00
|
|
|
// We might need to update the end to include the new node (bug 433662).
|
|
|
|
// Ideally we'd only do this if needed, but it's tricky to know when it's
|
|
|
|
// needed in advance (bug 765799).
|
2017-07-19 16:49:52 +03:00
|
|
|
uint32_t newOffset;
|
2012-04-01 16:02:21 +04:00
|
|
|
|
2012-06-19 17:17:37 +04:00
|
|
|
if (referenceNode) {
|
2017-07-19 16:49:52 +03:00
|
|
|
int32_t indexInParent = IndexOf(referenceNode);
|
|
|
|
if (NS_WARN_IF(indexInParent < 0)) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
newOffset = static_cast<uint32_t>(indexInParent);
|
2012-06-19 17:17:37 +04:00
|
|
|
} else {
|
2018-01-30 07:10:51 +03:00
|
|
|
newOffset = tChildList->Length();
|
2012-06-19 17:17:37 +04:00
|
|
|
}
|
2012-04-01 16:02:21 +04:00
|
|
|
|
2018-01-30 07:10:53 +03:00
|
|
|
if (aNode.NodeType() == nsINode::DOCUMENT_FRAGMENT_NODE) {
|
2013-02-07 16:09:41 +04:00
|
|
|
newOffset += aNode.GetChildCount();
|
2012-06-19 17:17:37 +04:00
|
|
|
} else {
|
|
|
|
newOffset++;
|
2012-04-01 16:02:21 +04:00
|
|
|
}
|
|
|
|
|
2012-06-19 17:17:37 +04:00
|
|
|
// Now actually insert the node
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> tResultNode;
|
|
|
|
tResultNode = referenceParentNode->InsertBefore(aNode, referenceNode, aRv);
|
2013-02-07 16:09:41 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-04-01 16:02:21 +04:00
|
|
|
|
|
|
|
if (Collapsed()) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv = SetEnd(referenceParentNode, newOffset);
|
2012-04-01 16:02:21 +04:00
|
|
|
}
|
2001-01-10 00:44:35 +03:00
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::SurroundContents(nsIDOMNode* aNewParent)
|
2001-01-10 00:44:35 +03:00
|
|
|
{
|
2013-02-07 16:09:41 +04:00
|
|
|
nsCOMPtr<nsINode> node = do_QueryInterface(aNewParent);
|
|
|
|
if (!node) {
|
|
|
|
return NS_ERROR_DOM_NOT_OBJECT_ERR;
|
|
|
|
}
|
|
|
|
ErrorResult rv;
|
|
|
|
SurroundContents(*node, rv);
|
2015-04-27 16:18:51 +03:00
|
|
|
return rv.StealNSResult();
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRange::SurroundContents(nsINode& aNewParent, ErrorResult& aRv)
|
|
|
|
{
|
2014-09-25 13:04:45 +04:00
|
|
|
if (!nsContentUtils::LegacyIsCallerNativeCode() &&
|
|
|
|
!nsContentUtils::CanCallerAccess(&aNewParent)) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_SECURITY_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2008-09-16 12:13:11 +04:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
if (!mRoot) {
|
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2012-03-01 22:35:48 +04:00
|
|
|
// INVALID_STATE_ERROR: Raised if the Range partially selects a non-text
|
2008-09-16 12:13:11 +04:00
|
|
|
// node.
|
2017-08-18 01:05:40 +03:00
|
|
|
if (mStart.Container() != mEnd.Container()) {
|
|
|
|
bool startIsText = mStart.Container()->IsNodeOfType(nsINode::eTEXT);
|
|
|
|
bool endIsText = mEnd.Container()->IsNodeOfType(nsINode::eTEXT);
|
|
|
|
nsINode* startGrandParent = mStart.Container()->GetParentNode();
|
|
|
|
nsINode* endGrandParent = mEnd.Container()->GetParentNode();
|
2013-02-07 16:09:41 +04:00
|
|
|
if (!((startIsText && endIsText &&
|
|
|
|
startGrandParent &&
|
|
|
|
startGrandParent == endGrandParent) ||
|
|
|
|
(startIsText &&
|
|
|
|
startGrandParent &&
|
2017-08-18 01:05:40 +03:00
|
|
|
startGrandParent == mEnd.Container()) ||
|
2013-02-07 16:09:41 +04:00
|
|
|
(endIsText &&
|
|
|
|
endGrandParent &&
|
2017-08-18 01:05:40 +03:00
|
|
|
endGrandParent == mStart.Container()))) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
|
|
|
|
return;
|
|
|
|
}
|
2008-09-16 12:13:11 +04:00
|
|
|
}
|
|
|
|
|
2012-03-21 22:06:50 +04:00
|
|
|
// INVALID_NODE_TYPE_ERROR if aNewParent is something that can't be inserted
|
|
|
|
// (Document, DocumentType, DocumentFragment)
|
2013-02-07 16:09:41 +04:00
|
|
|
uint16_t nodeType = aNewParent.NodeType();
|
2018-01-30 07:10:53 +03:00
|
|
|
if (nodeType == nsINode::DOCUMENT_NODE ||
|
|
|
|
nodeType == nsINode::DOCUMENT_TYPE_NODE ||
|
|
|
|
nodeType == nsINode::DOCUMENT_FRAGMENT_NODE) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INVALID_NODE_TYPE_ERR);
|
|
|
|
return;
|
2012-03-21 22:06:50 +04:00
|
|
|
}
|
|
|
|
|
2004-09-01 20:18:31 +04:00
|
|
|
// Extract the contents within the range.
|
2001-05-19 01:05:51 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DocumentFragment> docFrag = ExtractContents(aRv);
|
2001-05-19 01:05:51 +04:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!docFrag) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2001-05-19 01:05:51 +04:00
|
|
|
|
2004-09-01 20:18:31 +04:00
|
|
|
// Spec says we need to remove all of aNewParent's
|
|
|
|
// children prior to insertion.
|
2001-05-19 01:05:51 +04:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
nsCOMPtr<nsINodeList> children = aNewParent.ChildNodes();
|
|
|
|
if (!children) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-12 07:10:18 +04:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
uint32_t numChildren = children->Length();
|
2001-06-15 22:15:09 +04:00
|
|
|
|
2004-09-01 20:18:31 +04:00
|
|
|
while (numChildren)
|
|
|
|
{
|
2013-02-07 16:09:41 +04:00
|
|
|
nsCOMPtr<nsINode> child = children->Item(--numChildren);
|
|
|
|
if (!child) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return;
|
|
|
|
}
|
2013-03-12 07:10:18 +04:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
aNewParent.RemoveChild(*child, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2004-09-01 20:18:31 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Insert aNewParent at the range's start point.
|
2001-06-15 22:15:09 +04:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
InsertNode(aNewParent, aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2001-06-15 22:15:09 +04:00
|
|
|
|
2004-09-01 20:18:31 +04:00
|
|
|
// Append the content we extracted under aNewParent.
|
2013-07-12 07:29:24 +04:00
|
|
|
aNewParent.AppendChild(*docFrag, aRv);
|
2013-02-07 16:09:41 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2001-05-19 01:05:51 +04:00
|
|
|
|
2004-09-01 20:18:31 +04:00
|
|
|
// Select aNewParent, and its contents.
|
2001-05-19 01:05:51 +04:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
SelectNode(aNewParent, aRv);
|
2001-01-10 00:44:35 +03:00
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::ToString(nsAString& aReturn)
|
2013-07-12 07:29:24 +04:00
|
|
|
{
|
1998-12-15 07:42:29 +03:00
|
|
|
// clear the string
|
|
|
|
aReturn.Truncate();
|
2013-07-12 07:29:24 +04:00
|
|
|
|
1999-05-12 00:22:35 +04:00
|
|
|
// If we're unpositioned, return the empty string
|
2006-11-02 10:41:45 +03:00
|
|
|
if (!mIsPositioned) {
|
1999-05-12 00:22:35 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-08-20 02:20:57 +04:00
|
|
|
|
1999-09-30 01:52:10 +04:00
|
|
|
#ifdef DEBUG_range
|
2000-10-29 02:17:53 +04:00
|
|
|
printf("Range dump: -----------------------\n");
|
1999-08-20 02:20:57 +04:00
|
|
|
#endif /* DEBUG */
|
2013-07-12 07:29:24 +04:00
|
|
|
|
1998-12-15 07:42:29 +03:00
|
|
|
// effeciency hack for simple case
|
2017-08-18 01:05:40 +03:00
|
|
|
if (mStart.Container() == mEnd.Container()) {
|
|
|
|
nsCOMPtr<nsIDOMText> textNode = do_QueryInterface(mStart.Container());
|
2013-07-12 07:29:24 +04:00
|
|
|
|
1999-03-09 22:21:04 +03:00
|
|
|
if (textNode)
|
1998-12-15 07:42:29 +03:00
|
|
|
{
|
1999-09-30 01:52:10 +04:00
|
|
|
#ifdef DEBUG_range
|
1999-08-20 02:20:57 +04:00
|
|
|
// If debug, dump it:
|
2017-08-18 01:05:40 +03:00
|
|
|
nsCOMPtr<nsIContent> cN = do_QueryInterface(mStart.Container());
|
1999-08-20 02:20:57 +04:00
|
|
|
if (cN) cN->List(stdout);
|
2000-10-29 02:17:53 +04:00
|
|
|
printf("End Range dump: -----------------------\n");
|
1999-08-20 02:20:57 +04:00
|
|
|
#endif /* DEBUG */
|
|
|
|
|
1999-03-09 22:21:04 +03:00
|
|
|
// grab the text
|
2017-08-18 01:05:40 +03:00
|
|
|
if (NS_FAILED(textNode->SubstringData(mStart.Offset(),mEnd.Offset()-mStart.Offset(),aReturn)))
|
1999-03-09 22:21:04 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
return NS_OK;
|
1998-12-15 07:42:29 +03:00
|
|
|
}
|
2013-07-12 07:29:24 +04:00
|
|
|
}
|
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
/* complex case: mStart.Container() != mEnd.Container(), or mStartParent not a text
|
2017-07-11 16:46:11 +03:00
|
|
|
node revisit - there are potential optimizations here and also tradeoffs.
|
1998-12-15 07:42:29 +03:00
|
|
|
*/
|
1999-02-12 08:28:46 +03:00
|
|
|
|
2012-06-11 03:44:50 +04:00
|
|
|
nsCOMPtr<nsIContentIterator> iter = NS_NewContentIterator();
|
|
|
|
nsresult rv = iter->Init(this);
|
2006-11-02 10:41:45 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
2013-07-12 07:29:24 +04:00
|
|
|
|
1998-12-15 07:42:29 +03:00
|
|
|
nsString tempString;
|
2013-07-12 07:29:24 +04:00
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
// loop through the content iterator, which returns nodes in the range in
|
1998-12-15 07:42:29 +03:00
|
|
|
// close tag order, and grab the text from any text node
|
2004-01-24 03:46:17 +03:00
|
|
|
while (!iter->IsDone())
|
1998-12-15 07:42:29 +03:00
|
|
|
{
|
2008-10-15 13:40:28 +04:00
|
|
|
nsINode *n = iter->GetCurrentNode();
|
2004-01-24 03:46:17 +03:00
|
|
|
|
1999-09-30 01:52:10 +04:00
|
|
|
#ifdef DEBUG_range
|
1999-08-20 02:20:57 +04:00
|
|
|
// If debug, dump it:
|
2008-10-15 13:40:28 +04:00
|
|
|
n->List(stdout);
|
1999-08-20 02:20:57 +04:00
|
|
|
#endif /* DEBUG */
|
2008-10-15 13:40:28 +04:00
|
|
|
nsCOMPtr<nsIDOMText> textNode(do_QueryInterface(n));
|
1999-03-09 22:21:04 +03:00
|
|
|
if (textNode) // if it's a text node, get the text
|
1998-12-15 07:42:29 +03:00
|
|
|
{
|
2017-08-18 01:05:40 +03:00
|
|
|
if (n == mStart.Container()) { // only include text past start offset
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t strLength;
|
1998-12-15 07:42:29 +03:00
|
|
|
textNode->GetLength(&strLength);
|
2017-08-18 01:05:40 +03:00
|
|
|
textNode->SubstringData(mStart.Offset(),strLength-mStart.Offset(),tempString);
|
1998-12-15 07:42:29 +03:00
|
|
|
aReturn += tempString;
|
2017-08-18 01:05:40 +03:00
|
|
|
} else if (n == mEnd.Container()) { // only include text before end offset
|
|
|
|
textNode->SubstringData(0,mEnd.Offset(),tempString);
|
1998-12-15 07:42:29 +03:00
|
|
|
aReturn += tempString;
|
2017-07-11 16:57:55 +03:00
|
|
|
} else { // grab the whole kit-n-kaboodle
|
1998-12-15 07:42:29 +03:00
|
|
|
textNode->GetData(tempString);
|
|
|
|
aReturn += tempString;
|
|
|
|
}
|
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
|
|
|
iter->Next();
|
1998-12-15 07:42:29 +03:00
|
|
|
}
|
1999-08-20 02:20:57 +04:00
|
|
|
|
1999-09-30 01:52:10 +04:00
|
|
|
#ifdef DEBUG_range
|
2000-10-29 02:17:53 +04:00
|
|
|
printf("End Range dump: -----------------------\n");
|
1999-08-20 02:20:57 +04:00
|
|
|
#endif /* DEBUG */
|
1998-12-15 07:42:29 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1998-11-25 00:20:11 +03:00
|
|
|
|
1998-12-18 12:28:55 +03:00
|
|
|
|
2000-08-24 07:54:30 +04:00
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
NS_IMETHODIMP
|
2000-08-24 07:54:30 +04:00
|
|
|
nsRange::Detach()
|
|
|
|
{
|
1999-01-03 17:29:54 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-04-16 00:19:26 +04:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
NS_IMETHODIMP
|
2006-06-06 00:32:48 +04:00
|
|
|
nsRange::CreateContextualFragment(const nsAString& aFragment,
|
1999-06-04 02:38:38 +04:00
|
|
|
nsIDOMDocumentFragment** aReturn)
|
1999-04-28 02:16:04 +04:00
|
|
|
{
|
2008-03-06 23:14:33 +03:00
|
|
|
if (mIsPositioned) {
|
2017-08-18 01:05:40 +03:00
|
|
|
return nsContentUtils::CreateContextualFragment(mStart.Container(), aFragment,
|
2011-10-17 18:59:28 +04:00
|
|
|
false, aReturn);
|
2008-03-06 23:14:33 +03:00
|
|
|
}
|
|
|
|
return NS_ERROR_FAILURE;
|
1999-04-28 02:16:04 +04:00
|
|
|
}
|
2009-09-25 01:01:48 +04:00
|
|
|
|
2013-07-12 07:29:24 +04:00
|
|
|
already_AddRefed<DocumentFragment>
|
2013-02-07 16:09:41 +04:00
|
|
|
nsRange::CreateContextualFragment(const nsAString& aFragment, ErrorResult& aRv)
|
|
|
|
{
|
|
|
|
if (!mIsPositioned) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
return nsContentUtils::CreateContextualFragment(mStart.Container(), aFragment,
|
2013-02-07 16:09:41 +04:00
|
|
|
false, aRv);
|
|
|
|
}
|
|
|
|
|
2009-09-25 01:01:48 +04:00
|
|
|
static void ExtractRectFromOffset(nsIFrame* aFrame,
|
2014-06-28 00:58:57 +04:00
|
|
|
const int32_t aOffset, nsRect* aR, bool aKeepLeft,
|
|
|
|
bool aClampToEdge)
|
2009-09-25 01:01:48 +04:00
|
|
|
{
|
|
|
|
nsPoint point;
|
|
|
|
aFrame->GetPointFromOffset(aOffset, &point);
|
|
|
|
|
2014-06-28 00:58:57 +04:00
|
|
|
if (!aClampToEdge && !aR->Contains(point)) {
|
2018-02-19 20:56:10 +03:00
|
|
|
aR->SetRectX(point.x, 0);
|
2014-06-28 00:58:57 +04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aClampToEdge) {
|
|
|
|
point = aR->ClampPoint(point);
|
|
|
|
}
|
2009-09-25 01:01:48 +04:00
|
|
|
|
|
|
|
if (aKeepLeft) {
|
2018-02-19 20:56:10 +03:00
|
|
|
aR->SetRightEdge(point.x);
|
2009-09-25 01:01:48 +04:00
|
|
|
} else {
|
2018-02-19 20:56:10 +03:00
|
|
|
aR->SetLeftEdge(point.x);
|
2009-09-25 01:01:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-19 08:23:48 +04:00
|
|
|
static nsTextFrame*
|
2014-07-24 01:06:00 +04:00
|
|
|
GetTextFrameForContent(nsIContent* aContent, bool aFlushLayout)
|
2013-09-19 08:23:48 +04:00
|
|
|
{
|
2017-11-01 15:00:04 +03:00
|
|
|
nsIDocument* doc = aContent->OwnerDoc();
|
|
|
|
nsIPresShell* presShell = doc->GetShell();
|
|
|
|
if (!presShell) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-07-24 01:06:00 +04:00
|
|
|
|
2017-11-01 15:00:04 +03:00
|
|
|
const bool frameWillBeUnsuppressed =
|
|
|
|
presShell->FrameConstructor()->EnsureFrameForTextNodeIsCreatedAfterFlush(
|
|
|
|
static_cast<nsGenericDOMDataNode*>(aContent));
|
|
|
|
if (aFlushLayout) {
|
|
|
|
doc->FlushPendingNotifications(FlushType::Layout);
|
|
|
|
} else if (frameWillBeUnsuppressed) {
|
|
|
|
doc->FlushPendingNotifications(FlushType::Frames);
|
|
|
|
}
|
2014-07-24 01:06:00 +04:00
|
|
|
|
2017-11-01 15:00:04 +03:00
|
|
|
nsIFrame* frame = aContent->GetPrimaryFrame();
|
|
|
|
if (!frame || !frame->IsTextFrame()) {
|
|
|
|
return nullptr;
|
2013-09-19 08:23:48 +04:00
|
|
|
}
|
2017-11-01 15:00:04 +03:00
|
|
|
return static_cast<nsTextFrame*>(frame);
|
2013-09-19 08:23:48 +04:00
|
|
|
}
|
|
|
|
|
2009-12-25 00:20:05 +03:00
|
|
|
static nsresult GetPartialTextRect(nsLayoutUtils::RectCallback* aCallback,
|
2017-03-03 00:13:12 +03:00
|
|
|
Sequence<nsString>* aTextList,
|
2014-06-28 00:58:57 +04:00
|
|
|
nsIContent* aContent, int32_t aStartOffset,
|
2014-07-24 01:06:00 +04:00
|
|
|
int32_t aEndOffset, bool aClampToEdge,
|
|
|
|
bool aFlushLayout)
|
2009-09-25 01:01:48 +04:00
|
|
|
{
|
2014-07-24 01:06:00 +04:00
|
|
|
nsTextFrame* textFrame = GetTextFrameForContent(aContent, aFlushLayout);
|
2013-09-19 08:23:48 +04:00
|
|
|
if (textFrame) {
|
2009-09-25 01:01:48 +04:00
|
|
|
nsIFrame* relativeTo = nsLayoutUtils::GetContainingBlockForClientRect(textFrame);
|
|
|
|
for (nsTextFrame* f = textFrame; f; f = static_cast<nsTextFrame*>(f->GetNextContinuation())) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t fstart = f->GetContentOffset(), fend = f->GetContentEnd();
|
2009-09-25 01:01:48 +04:00
|
|
|
if (fend <= aStartOffset || fstart >= aEndOffset)
|
|
|
|
continue;
|
|
|
|
|
2016-11-03 02:39:43 +03:00
|
|
|
// Calculate the text content offsets we'll need if text is requested.
|
|
|
|
int32_t textContentStart = fstart;
|
|
|
|
int32_t textContentEnd = fend;
|
|
|
|
|
2010-04-28 21:47:23 +04:00
|
|
|
// overlapping with the offset we want
|
2011-11-24 06:48:23 +04:00
|
|
|
f->EnsureTextRun(nsTextFrame::eInflated);
|
|
|
|
NS_ENSURE_TRUE(f->GetTextRun(nsTextFrame::eInflated), NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
bool rtl = f->GetTextRun(nsTextFrame::eInflated)->IsRightToLeft();
|
2014-07-21 17:58:14 +04:00
|
|
|
nsRect r = f->GetRectRelativeToSelf();
|
2009-09-25 01:01:48 +04:00
|
|
|
if (fstart < aStartOffset) {
|
2010-04-28 21:47:23 +04:00
|
|
|
// aStartOffset is within this frame
|
2014-07-21 17:58:14 +04:00
|
|
|
ExtractRectFromOffset(f, aStartOffset, &r, rtl, aClampToEdge);
|
2016-11-03 02:39:43 +03:00
|
|
|
textContentStart = aStartOffset;
|
2009-09-25 01:01:48 +04:00
|
|
|
}
|
|
|
|
if (fend > aEndOffset) {
|
2010-04-28 21:47:23 +04:00
|
|
|
// aEndOffset is in the middle of this frame
|
2014-07-21 17:58:14 +04:00
|
|
|
ExtractRectFromOffset(f, aEndOffset, &r, !rtl, aClampToEdge);
|
2016-11-03 02:39:43 +03:00
|
|
|
textContentEnd = aEndOffset;
|
2009-09-25 01:01:48 +04:00
|
|
|
}
|
2014-07-21 17:58:14 +04:00
|
|
|
r = nsLayoutUtils::TransformFrameRectToAncestor(f, r, relativeTo);
|
2009-09-25 01:01:48 +04:00
|
|
|
aCallback->AddRect(r);
|
2016-11-03 02:39:43 +03:00
|
|
|
|
|
|
|
// Finally capture the text, if requested.
|
|
|
|
if (aTextList) {
|
2017-03-02 03:15:22 +03:00
|
|
|
nsIFrame::RenderedText renderedText = f->GetRenderedText(
|
|
|
|
textContentStart,
|
|
|
|
textContentEnd,
|
|
|
|
nsIFrame::TextOffsetType::OFFSETS_IN_CONTENT_TEXT,
|
|
|
|
nsIFrame::TrailingWhitespace::DONT_TRIM_TRAILING_WHITESPACE);
|
|
|
|
|
|
|
|
aTextList->AppendElement(renderedText.mString, fallible);
|
2016-11-03 02:39:43 +03:00
|
|
|
}
|
2009-09-25 01:01:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-06-05 00:54:00 +04:00
|
|
|
/* static */ void
|
2016-11-02 23:49:43 +03:00
|
|
|
nsRange::CollectClientRectsAndText(nsLayoutUtils::RectCallback* aCollector,
|
2017-03-03 00:13:12 +03:00
|
|
|
Sequence<nsString>* aTextList,
|
2016-11-02 23:49:43 +03:00
|
|
|
nsRange* aRange,
|
2017-07-11 17:10:42 +03:00
|
|
|
nsINode* aStartContainer,
|
2017-07-19 16:49:52 +03:00
|
|
|
uint32_t aStartOffset,
|
2017-07-11 18:09:37 +03:00
|
|
|
nsINode* aEndContainer,
|
2017-07-19 16:49:52 +03:00
|
|
|
uint32_t aEndOffset,
|
2016-11-02 23:49:43 +03:00
|
|
|
bool aClampToEdge, bool aFlushLayout)
|
2009-09-25 01:01:48 +04:00
|
|
|
{
|
2017-07-19 16:49:52 +03:00
|
|
|
// Currently, this method is called with start of end offset of nsRange.
|
|
|
|
// So, they must be between 0 - INT32_MAX.
|
|
|
|
MOZ_ASSERT(IsValidOffset(aStartOffset));
|
|
|
|
MOZ_ASSERT(IsValidOffset(aEndOffset));
|
|
|
|
|
2009-12-25 00:20:05 +03:00
|
|
|
// Hold strong pointers across the flush
|
2017-07-11 17:10:42 +03:00
|
|
|
nsCOMPtr<nsINode> startContainer = aStartContainer;
|
2017-07-11 18:09:37 +03:00
|
|
|
nsCOMPtr<nsINode> endContainer = aEndContainer;
|
2009-09-25 01:01:48 +04:00
|
|
|
|
2009-12-25 00:20:05 +03:00
|
|
|
// Flush out layout so our frames are up to date.
|
2017-07-11 17:10:42 +03:00
|
|
|
if (!aStartContainer->IsInUncomposedDoc()) {
|
2009-12-25 00:20:05 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-24 01:06:00 +04:00
|
|
|
if (aFlushLayout) {
|
2017-07-11 17:10:42 +03:00
|
|
|
aStartContainer->OwnerDoc()->FlushPendingNotifications(FlushType::Layout);
|
2014-07-24 01:06:00 +04:00
|
|
|
// Recheck whether we're still in the document
|
2017-07-11 17:10:42 +03:00
|
|
|
if (!aStartContainer->IsInUncomposedDoc()) {
|
2014-07-24 01:06:00 +04:00
|
|
|
return;
|
|
|
|
}
|
2009-09-25 01:01:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
RangeSubtreeIterator iter;
|
|
|
|
|
|
|
|
nsresult rv = iter.Init(aRange);
|
|
|
|
if (NS_FAILED(rv)) return;
|
|
|
|
|
|
|
|
if (iter.IsDone()) {
|
|
|
|
// the range is collapsed, only continue if the cursor is in a text node
|
2017-07-11 17:10:42 +03:00
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(aStartContainer);
|
2010-01-11 17:08:19 +03:00
|
|
|
if (content && content->IsNodeOfType(nsINode::eTEXT)) {
|
2014-07-24 01:06:00 +04:00
|
|
|
nsTextFrame* textFrame = GetTextFrameForContent(content, aFlushLayout);
|
2013-09-19 08:23:48 +04:00
|
|
|
if (textFrame) {
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t outOffset;
|
2009-09-25 01:01:48 +04:00
|
|
|
nsIFrame* outFrame;
|
2017-07-19 16:49:52 +03:00
|
|
|
textFrame->GetChildFrameContainingOffset(
|
|
|
|
static_cast<int32_t>(aStartOffset), false,
|
|
|
|
&outOffset, &outFrame);
|
2009-09-25 01:01:48 +04:00
|
|
|
if (outFrame) {
|
2013-07-12 07:29:24 +04:00
|
|
|
nsIFrame* relativeTo =
|
2009-09-25 01:01:48 +04:00
|
|
|
nsLayoutUtils::GetContainingBlockForClientRect(outFrame);
|
2014-07-21 17:58:14 +04:00
|
|
|
nsRect r = outFrame->GetRectRelativeToSelf();
|
2017-07-19 16:49:52 +03:00
|
|
|
ExtractRectFromOffset(outFrame, static_cast<int32_t>(aStartOffset),
|
|
|
|
&r, false, aClampToEdge);
|
2017-08-14 15:30:10 +03:00
|
|
|
r.SetWidth(0);
|
2014-07-21 17:58:14 +04:00
|
|
|
r = nsLayoutUtils::TransformFrameRectToAncestor(outFrame, r, relativeTo);
|
2009-09-25 01:01:48 +04:00
|
|
|
aCollector->AddRect(r);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> node = iter.GetCurrentNode();
|
2009-09-25 01:01:48 +04:00
|
|
|
iter.Next();
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(node);
|
2010-01-11 17:08:19 +03:00
|
|
|
if (!content)
|
|
|
|
continue;
|
2009-09-25 01:01:48 +04:00
|
|
|
if (content->IsNodeOfType(nsINode::eTEXT)) {
|
|
|
|
if (node == startContainer) {
|
2013-07-12 07:29:24 +04:00
|
|
|
int32_t offset = startContainer == endContainer ?
|
2017-07-19 16:49:52 +03:00
|
|
|
static_cast<int32_t>(aEndOffset) : content->GetText()->GetLength();
|
|
|
|
GetPartialTextRect(aCollector, aTextList, content,
|
|
|
|
static_cast<int32_t>(aStartOffset), offset,
|
2014-07-24 01:06:00 +04:00
|
|
|
aClampToEdge, aFlushLayout);
|
2009-09-25 01:01:48 +04:00
|
|
|
continue;
|
|
|
|
} else if (node == endContainer) {
|
2017-07-19 16:49:52 +03:00
|
|
|
GetPartialTextRect(aCollector, aTextList, content,
|
|
|
|
0, static_cast<int32_t>(aEndOffset),
|
2014-07-24 01:06:00 +04:00
|
|
|
aClampToEdge, aFlushLayout);
|
2010-01-11 17:08:19 +03:00
|
|
|
continue;
|
2009-09-25 01:01:48 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-12-25 00:20:05 +03:00
|
|
|
nsIFrame* frame = content->GetPrimaryFrame();
|
2009-09-25 01:01:48 +04:00
|
|
|
if (frame) {
|
2016-11-03 02:39:43 +03:00
|
|
|
nsLayoutUtils::GetAllInFlowRectsAndTexts(frame,
|
2014-07-21 17:58:14 +04:00
|
|
|
nsLayoutUtils::GetContainingBlockForClientRect(frame), aCollector,
|
2016-11-03 02:39:43 +03:00
|
|
|
aTextList,
|
2014-07-21 17:58:14 +04:00
|
|
|
nsLayoutUtils::RECTS_ACCOUNT_FOR_TRANSFORMS);
|
2009-09-25 01:01:48 +04:00
|
|
|
}
|
|
|
|
} while (!iter.IsDone());
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::GetBoundingClientRect(nsIDOMClientRect** aResult)
|
|
|
|
{
|
2014-06-28 00:58:57 +04:00
|
|
|
*aResult = GetBoundingClientRect(true).take();
|
2013-02-07 16:09:41 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2009-09-25 01:01:48 +04:00
|
|
|
|
2013-09-20 14:21:03 +04:00
|
|
|
already_AddRefed<DOMRect>
|
2014-07-24 01:06:00 +04:00
|
|
|
nsRange::GetBoundingClientRect(bool aClampToEdge, bool aFlushLayout)
|
2013-02-07 16:09:41 +04:00
|
|
|
{
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMRect> rect = new DOMRect(ToSupports(this));
|
2017-08-18 01:05:40 +03:00
|
|
|
if (!mStart.Container()) {
|
2013-02-07 16:09:41 +04:00
|
|
|
return rect.forget();
|
2013-03-17 11:55:17 +04:00
|
|
|
}
|
2010-01-11 17:08:19 +03:00
|
|
|
|
2009-09-25 01:01:48 +04:00
|
|
|
nsLayoutUtils::RectAccumulator accumulator;
|
2017-08-18 01:05:40 +03:00
|
|
|
CollectClientRectsAndText(&accumulator, nullptr, this, mStart.Container(),
|
|
|
|
mStart.Offset(), mEnd.Container(), mEnd.Offset(), aClampToEdge, aFlushLayout);
|
2009-09-25 01:01:48 +04:00
|
|
|
|
2017-07-06 15:00:35 +03:00
|
|
|
nsRect r = accumulator.mResultRect.IsEmpty() ? accumulator.mFirstRect :
|
2009-09-25 01:01:48 +04:00
|
|
|
accumulator.mResultRect;
|
|
|
|
rect->SetLayoutRect(r);
|
2013-02-07 16:09:41 +04:00
|
|
|
return rect.forget();
|
2009-09-25 01:01:48 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::GetClientRects(nsIDOMClientRectList** aResult)
|
|
|
|
{
|
2014-06-28 00:58:57 +04:00
|
|
|
*aResult = GetClientRects(true).take();
|
2013-03-17 11:55:16 +04:00
|
|
|
return NS_OK;
|
2013-02-07 16:09:41 +04:00
|
|
|
}
|
2009-09-25 01:01:48 +04:00
|
|
|
|
2013-09-20 14:21:03 +04:00
|
|
|
already_AddRefed<DOMRectList>
|
2014-07-24 01:06:00 +04:00
|
|
|
nsRange::GetClientRects(bool aClampToEdge, bool aFlushLayout)
|
2013-02-07 16:09:41 +04:00
|
|
|
{
|
2017-08-18 01:05:40 +03:00
|
|
|
if (!mStart.Container()) {
|
2013-02-07 16:09:41 +04:00
|
|
|
return nullptr;
|
2013-03-17 11:55:16 +04:00
|
|
|
}
|
2010-01-11 17:08:19 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<DOMRectList> rectList =
|
2013-09-20 14:21:03 +04:00
|
|
|
new DOMRectList(static_cast<nsIDOMRange*>(this));
|
2009-09-25 01:01:48 +04:00
|
|
|
|
|
|
|
nsLayoutUtils::RectListBuilder builder(rectList);
|
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
CollectClientRectsAndText(&builder, nullptr, this, mStart.Container(),
|
|
|
|
mStart.Offset(), mEnd.Container(), mEnd.Offset(), aClampToEdge, aFlushLayout);
|
2013-02-07 16:09:41 +04:00
|
|
|
return rectList.forget();
|
2009-09-25 01:01:48 +04:00
|
|
|
}
|
|
|
|
|
2016-11-02 23:31:06 +03:00
|
|
|
void
|
|
|
|
nsRange::GetClientRectsAndTexts(
|
|
|
|
mozilla::dom::ClientRectsAndTexts& aResult,
|
|
|
|
ErrorResult& aErr)
|
|
|
|
{
|
2017-08-18 01:05:40 +03:00
|
|
|
if (!mStart.Container()) {
|
2016-11-02 23:31:06 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aResult.mRectList = new DOMRectList(static_cast<nsIDOMRange*>(this));
|
|
|
|
|
|
|
|
nsLayoutUtils::RectListBuilder builder(aResult.mRectList);
|
|
|
|
|
2017-03-03 00:13:12 +03:00
|
|
|
CollectClientRectsAndText(&builder, &aResult.mTextList, this,
|
2017-08-18 01:05:40 +03:00
|
|
|
mStart.Container(), mStart.Offset(), mEnd.Container(), mEnd.Offset(), true, true);
|
2016-11-02 23:31:06 +03:00
|
|
|
}
|
|
|
|
|
2018-01-11 07:38:01 +03:00
|
|
|
nsresult
|
2018-02-15 21:48:22 +03:00
|
|
|
nsRange::GetUsedFontFaces(nsTArray<nsAutoPtr<InspectorFontFace>>& aResult,
|
|
|
|
uint32_t aMaxRanges)
|
2011-06-16 10:31:36 +04:00
|
|
|
{
|
2017-08-18 01:05:40 +03:00
|
|
|
NS_ENSURE_TRUE(mStart.Container(), NS_ERROR_UNEXPECTED);
|
2011-06-16 10:31:36 +04:00
|
|
|
|
2017-08-18 01:05:40 +03:00
|
|
|
nsCOMPtr<nsINode> startContainer = do_QueryInterface(mStart.Container());
|
|
|
|
nsCOMPtr<nsINode> endContainer = do_QueryInterface(mEnd.Container());
|
2011-06-16 10:31:36 +04:00
|
|
|
|
|
|
|
// Flush out layout so our frames are up to date.
|
2017-08-18 01:05:40 +03:00
|
|
|
nsIDocument* doc = mStart.Container()->OwnerDoc();
|
2011-06-16 10:31:36 +04:00
|
|
|
NS_ENSURE_TRUE(doc, NS_ERROR_UNEXPECTED);
|
2017-01-05 10:31:56 +03:00
|
|
|
doc->FlushPendingNotifications(FlushType::Frames);
|
2011-06-16 10:31:36 +04:00
|
|
|
|
|
|
|
// Recheck whether we're still in the document
|
2017-08-18 01:05:40 +03:00
|
|
|
NS_ENSURE_TRUE(mStart.Container()->IsInUncomposedDoc(), NS_ERROR_UNEXPECTED);
|
2011-06-16 10:31:36 +04:00
|
|
|
|
2018-01-11 07:38:01 +03:00
|
|
|
// A table to map gfxFontEntry objects to InspectorFontFace objects.
|
|
|
|
// (We hold on to the InspectorFontFaces strongly due to the nsAutoPtrs
|
|
|
|
// in the nsClassHashtable, until we move them out into aResult at the end
|
|
|
|
// of the function.)
|
|
|
|
nsLayoutUtils::UsedFontFaceTable fontFaces;
|
2011-06-16 10:31:36 +04:00
|
|
|
|
|
|
|
RangeSubtreeIterator iter;
|
|
|
|
nsresult rv = iter.Init(this);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
while (!iter.IsDone()) {
|
|
|
|
// only collect anything if the range is not collapsed
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> node = iter.GetCurrentNode();
|
2011-06-16 10:31:36 +04:00
|
|
|
iter.Next();
|
|
|
|
|
|
|
|
nsCOMPtr<nsIContent> content = do_QueryInterface(node);
|
|
|
|
if (!content) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
nsIFrame* frame = content->GetPrimaryFrame();
|
|
|
|
if (!frame) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (content->IsNodeOfType(nsINode::eTEXT)) {
|
|
|
|
if (node == startContainer) {
|
2013-07-12 07:29:24 +04:00
|
|
|
int32_t offset = startContainer == endContainer ?
|
2017-08-18 01:05:40 +03:00
|
|
|
mEnd.Offset() : content->GetText()->GetLength();
|
|
|
|
nsLayoutUtils::GetFontFacesForText(frame, mStart.Offset(), offset,
|
2018-02-15 21:48:22 +03:00
|
|
|
true, fontFaces, aMaxRanges);
|
2011-06-16 10:31:36 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (node == endContainer) {
|
2017-08-18 01:05:40 +03:00
|
|
|
nsLayoutUtils::GetFontFacesForText(frame, 0, mEnd.Offset(),
|
2018-02-15 21:48:22 +03:00
|
|
|
true, fontFaces, aMaxRanges);
|
2011-06-16 10:31:36 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-15 21:48:22 +03:00
|
|
|
nsLayoutUtils::GetFontFacesForFrames(frame, fontFaces, aMaxRanges);
|
2018-01-11 07:38:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Take ownership of the InspectorFontFaces in the table and move them into
|
|
|
|
// the aResult outparam.
|
|
|
|
for (auto iter = fontFaces.Iter(); !iter.Done(); iter.Next()) {
|
|
|
|
aResult.AppendElement(Move(iter.Data()));
|
2011-06-16 10:31:36 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-12-24 17:26:03 +04:00
|
|
|
|
|
|
|
nsINode*
|
|
|
|
nsRange::GetRegisteredCommonAncestor()
|
|
|
|
{
|
2017-09-01 18:13:25 +03:00
|
|
|
MOZ_ASSERT(IsInSelection(),
|
|
|
|
"GetRegisteredCommonAncestor only valid for range in selection");
|
|
|
|
MOZ_ASSERT(mRegisteredCommonAncestor);
|
|
|
|
return mRegisteredCommonAncestor;
|
2011-12-24 17:26:03 +04:00
|
|
|
}
|
|
|
|
|
2017-09-01 18:13:25 +03:00
|
|
|
/* static */ bool nsRange::AutoInvalidateSelection::sIsNested;
|
2011-12-24 17:26:03 +04:00
|
|
|
|
|
|
|
nsRange::AutoInvalidateSelection::~AutoInvalidateSelection()
|
|
|
|
{
|
|
|
|
if (!mCommonAncestor) {
|
|
|
|
return;
|
|
|
|
}
|
2017-09-01 18:13:25 +03:00
|
|
|
sIsNested = false;
|
2011-12-24 17:26:03 +04:00
|
|
|
::InvalidateAllFrames(mCommonAncestor);
|
2017-09-01 18:13:25 +03:00
|
|
|
|
|
|
|
// Our range might not be in a selection anymore, because one of our selection
|
|
|
|
// listeners might have gone ahead and run script of various sorts that messed
|
|
|
|
// with selections, ranges, etc. But if it still is, we should check whether
|
|
|
|
// we have a different common ancestor now, and if so invalidate its subtree
|
|
|
|
// so it paints the selection it's in now.
|
|
|
|
if (mRange->IsInSelection()) {
|
|
|
|
nsINode* commonAncestor = mRange->GetRegisteredCommonAncestor();
|
|
|
|
// XXXbz can commonAncestor really be null here? I wouldn't think so! If
|
|
|
|
// it _were_, then in a debug build GetRegisteredCommonAncestor() would have
|
|
|
|
// fatally asserted.
|
|
|
|
if (commonAncestor && commonAncestor != mCommonAncestor) {
|
|
|
|
::InvalidateAllFrames(commonAncestor);
|
|
|
|
}
|
2011-12-24 17:26:03 +04:00
|
|
|
}
|
|
|
|
}
|
2013-05-21 22:33:28 +04:00
|
|
|
|
|
|
|
/* static */ already_AddRefed<nsRange>
|
2013-08-23 09:17:08 +04:00
|
|
|
nsRange::Constructor(const GlobalObject& aGlobal,
|
|
|
|
ErrorResult& aRv)
|
2013-05-21 22:33:28 +04:00
|
|
|
{
|
2016-01-30 20:05:36 +03:00
|
|
|
nsCOMPtr<nsPIDOMWindowInner> window = do_QueryInterface(aGlobal.GetAsSupports());
|
2013-05-21 22:33:28 +04:00
|
|
|
if (!window || !window->GetDoc()) {
|
|
|
|
aRv.Throw(NS_ERROR_FAILURE);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return window->GetDoc()->CreateRange(aRv);
|
|
|
|
}
|
2014-09-10 21:07:36 +04:00
|
|
|
|
2016-02-13 20:40:23 +03:00
|
|
|
static bool ExcludeIfNextToNonSelectable(nsIContent* aContent)
|
|
|
|
{
|
|
|
|
return aContent->IsNodeOfType(nsINode::eTEXT) &&
|
|
|
|
aContent->HasFlag(NS_CREATE_FRAME_IF_NON_WHITESPACE);
|
|
|
|
}
|
|
|
|
|
2014-09-10 21:07:36 +04:00
|
|
|
void
|
2015-10-18 08:24:48 +03:00
|
|
|
nsRange::ExcludeNonSelectableNodes(nsTArray<RefPtr<nsRange>>* aOutRanges)
|
2014-09-10 21:07:36 +04:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(mIsPositioned);
|
2017-08-18 01:05:40 +03:00
|
|
|
MOZ_ASSERT(mEnd.Container());
|
|
|
|
MOZ_ASSERT(mStart.Container());
|
2014-09-10 21:07:36 +04:00
|
|
|
|
|
|
|
nsRange* range = this;
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsRange> newRange;
|
2014-09-10 21:07:36 +04:00
|
|
|
while (range) {
|
|
|
|
nsCOMPtr<nsIContentIterator> iter = NS_NewPreContentIterator();
|
|
|
|
nsresult rv = iter->Init(range);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool added = false;
|
|
|
|
bool seenSelectable = false;
|
2016-02-13 20:40:23 +03:00
|
|
|
// |firstNonSelectableContent| is the first node in a consecutive sequence
|
|
|
|
// of non-IsSelectable nodes. When we find a selectable node after such
|
|
|
|
// a sequence we'll end the last nsRange, create a new one and restart
|
|
|
|
// the outer loop.
|
2014-09-10 21:07:36 +04:00
|
|
|
nsIContent* firstNonSelectableContent = nullptr;
|
|
|
|
while (true) {
|
|
|
|
ErrorResult err;
|
|
|
|
nsINode* node = iter->GetCurrentNode();
|
|
|
|
iter->Next();
|
|
|
|
bool selectable = true;
|
|
|
|
nsIContent* content =
|
|
|
|
node && node->IsContent() ? node->AsContent() : nullptr;
|
|
|
|
if (content) {
|
2016-02-13 20:40:23 +03:00
|
|
|
if (firstNonSelectableContent && ExcludeIfNextToNonSelectable(content)) {
|
|
|
|
// Ignorable whitespace next to a sequence of non-selectable nodes
|
|
|
|
// counts as non-selectable (bug 1216001).
|
|
|
|
selectable = false;
|
2014-09-10 21:07:36 +04:00
|
|
|
}
|
2016-02-13 20:40:23 +03:00
|
|
|
if (selectable) {
|
|
|
|
nsIFrame* frame = content->GetPrimaryFrame();
|
|
|
|
for (nsIContent* p = content; !frame && (p = p->GetParent()); ) {
|
|
|
|
frame = p->GetPrimaryFrame();
|
|
|
|
}
|
|
|
|
if (frame) {
|
2016-11-29 02:31:29 +03:00
|
|
|
selectable = frame->IsSelectable(nullptr);
|
2016-02-13 20:40:23 +03:00
|
|
|
}
|
2014-09-10 21:07:36 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!selectable) {
|
|
|
|
if (!firstNonSelectableContent) {
|
|
|
|
firstNonSelectableContent = content;
|
|
|
|
}
|
|
|
|
if (iter->IsDone() && seenSelectable) {
|
|
|
|
// The tail end of the initial range is non-selectable - truncate the
|
|
|
|
// current range before the first non-selectable node.
|
|
|
|
range->SetEndBefore(*firstNonSelectableContent, err);
|
|
|
|
}
|
|
|
|
} else if (firstNonSelectableContent) {
|
|
|
|
if (range == this && !seenSelectable) {
|
|
|
|
// This is the initial range and all its nodes until now are
|
|
|
|
// non-selectable so just trim them from the start.
|
|
|
|
range->SetStartBefore(*node, err);
|
|
|
|
if (err.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
break; // restart the same range with a new iterator
|
|
|
|
} else {
|
|
|
|
// Save the end point before truncating the range.
|
2017-08-18 01:05:40 +03:00
|
|
|
nsINode* endContainer = range->mEnd.Container();
|
|
|
|
int32_t endOffset = range->mEnd.Offset();
|
2014-09-10 21:07:36 +04:00
|
|
|
|
|
|
|
// Truncate the current range before the first non-selectable node.
|
|
|
|
range->SetEndBefore(*firstNonSelectableContent, err);
|
|
|
|
|
|
|
|
// Store it in the result (strong ref) - do this before creating
|
|
|
|
// a new range in |newRange| below so we don't drop the last ref
|
|
|
|
// to the range created in the previous iteration.
|
|
|
|
if (!added && !err.Failed()) {
|
|
|
|
aOutRanges->AppendElement(range);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a new range for the remainder.
|
2017-07-11 18:02:14 +03:00
|
|
|
nsINode* startContainer = node;
|
2017-01-08 23:27:00 +03:00
|
|
|
int32_t startOffset = 0;
|
|
|
|
// Don't start *inside* a node with independent selection though
|
|
|
|
// (e.g. <input>).
|
|
|
|
if (content && content->HasIndependentSelection()) {
|
2017-07-11 18:02:14 +03:00
|
|
|
nsINode* parent = startContainer->GetParent();
|
2017-01-08 23:27:00 +03:00
|
|
|
if (parent) {
|
2018-01-23 16:30:18 +03:00
|
|
|
startOffset = parent->ComputeIndexOf(startContainer);
|
2017-07-11 18:02:14 +03:00
|
|
|
startContainer = parent;
|
2017-01-08 23:27:00 +03:00
|
|
|
}
|
|
|
|
}
|
2017-07-11 18:08:37 +03:00
|
|
|
rv = CreateRange(startContainer, startOffset, endContainer, endOffset,
|
2014-09-10 21:07:36 +04:00
|
|
|
getter_AddRefs(newRange));
|
|
|
|
if (NS_FAILED(rv) || newRange->Collapsed()) {
|
|
|
|
newRange = nullptr;
|
|
|
|
}
|
|
|
|
range = newRange;
|
|
|
|
break; // create a new iterator for the new range, if any
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
seenSelectable = true;
|
|
|
|
if (!added) {
|
|
|
|
added = true;
|
|
|
|
aOutRanges->AppendElement(range);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (iter->IsDone()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-10-30 08:37:03 +03:00
|
|
|
|
|
|
|
struct InnerTextAccumulator
|
|
|
|
{
|
|
|
|
explicit InnerTextAccumulator(mozilla::dom::DOMString& aValue)
|
|
|
|
: mString(aValue.AsAString()), mRequiredLineBreakCount(0) {}
|
|
|
|
void FlushLineBreaks()
|
|
|
|
{
|
|
|
|
while (mRequiredLineBreakCount > 0) {
|
|
|
|
// Required line breaks at the start of the text are suppressed.
|
|
|
|
if (!mString.IsEmpty()) {
|
|
|
|
mString.Append('\n');
|
|
|
|
}
|
|
|
|
--mRequiredLineBreakCount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
void Append(char aCh)
|
|
|
|
{
|
|
|
|
Append(nsAutoString(aCh));
|
|
|
|
}
|
|
|
|
void Append(const nsAString& aString)
|
|
|
|
{
|
|
|
|
if (aString.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
FlushLineBreaks();
|
|
|
|
mString.Append(aString);
|
|
|
|
}
|
|
|
|
void AddRequiredLineBreakCount(int8_t aCount)
|
|
|
|
{
|
|
|
|
mRequiredLineBreakCount = std::max(mRequiredLineBreakCount, aCount);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAString& mString;
|
|
|
|
int8_t mRequiredLineBreakCount;
|
|
|
|
};
|
|
|
|
|
|
|
|
static bool
|
|
|
|
IsVisibleAndNotInReplacedElement(nsIFrame* aFrame)
|
|
|
|
{
|
|
|
|
if (!aFrame || !aFrame->StyleVisibility()->IsVisible()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
for (nsIFrame* f = aFrame->GetParent(); f; f = f->GetParent()) {
|
|
|
|
if (f->IsFrameOfType(nsIFrame::eReplaced) &&
|
2016-08-11 14:18:02 +03:00
|
|
|
!f->GetContent()->IsHTMLElement(nsGkAtoms::button) &&
|
|
|
|
!f->GetContent()->IsHTMLElement(nsGkAtoms::select)) {
|
2015-10-30 08:37:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
2017-03-18 20:44:27 +03:00
|
|
|
ElementIsVisibleNoFlush(Element* aElement)
|
2015-10-30 08:37:03 +03:00
|
|
|
{
|
|
|
|
if (!aElement) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-03-18 20:44:27 +03:00
|
|
|
RefPtr<nsStyleContext> sc =
|
2017-04-05 08:39:23 +03:00
|
|
|
nsComputedDOMStyle::GetStyleContextNoFlush(aElement, nullptr, nullptr);
|
2015-10-30 08:37:03 +03:00
|
|
|
return sc && sc->StyleVisibility()->IsVisible();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2018-01-12 17:33:13 +03:00
|
|
|
AppendTransformedText(InnerTextAccumulator& aResult, nsIContent* aContainer)
|
2015-10-30 08:37:03 +03:00
|
|
|
{
|
2018-01-12 17:33:13 +03:00
|
|
|
auto textNode = static_cast<nsGenericDOMDataNode*>(aContainer);
|
|
|
|
|
|
|
|
nsIFrame* frame = textNode->GetPrimaryFrame();
|
2015-10-30 08:37:03 +03:00
|
|
|
if (!IsVisibleAndNotInReplacedElement(frame)) {
|
|
|
|
return;
|
|
|
|
}
|
2018-01-12 17:33:13 +03:00
|
|
|
|
|
|
|
nsIFrame::RenderedText text =
|
|
|
|
frame->GetRenderedText(0, aContainer->GetChildCount());
|
2015-10-30 08:37:03 +03:00
|
|
|
aResult.Append(text.mString);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* States for tree traversal. AT_NODE means that we are about to enter
|
|
|
|
* the current DOM node. AFTER_NODE means that we have just finished traversing
|
|
|
|
* the children of the current DOM node and are about to apply any
|
|
|
|
* "after processing the node's children" steps before we finish visiting
|
|
|
|
* the node.
|
|
|
|
*/
|
|
|
|
enum TreeTraversalState {
|
|
|
|
AT_NODE,
|
|
|
|
AFTER_NODE
|
|
|
|
};
|
|
|
|
|
|
|
|
static int8_t
|
|
|
|
GetRequiredInnerTextLineBreakCount(nsIFrame* aFrame)
|
|
|
|
{
|
|
|
|
if (aFrame->GetContent()->IsHTMLElement(nsGkAtoms::p)) {
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
const nsStyleDisplay* styleDisplay = aFrame->StyleDisplay();
|
|
|
|
if (styleDisplay->IsBlockOutside(aFrame) ||
|
2016-08-28 05:31:50 +03:00
|
|
|
styleDisplay->mDisplay == StyleDisplay::TableCaption) {
|
2015-10-30 08:37:03 +03:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
IsLastCellOfRow(nsIFrame* aFrame)
|
|
|
|
{
|
2017-05-01 20:32:52 +03:00
|
|
|
LayoutFrameType type = aFrame->Type();
|
|
|
|
if (type != LayoutFrameType::TableCell &&
|
|
|
|
type != LayoutFrameType::BCTableCell) {
|
2015-10-30 08:37:03 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (nsIFrame* c = aFrame; c; c = c->GetNextContinuation()) {
|
|
|
|
if (c->GetNextSibling()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
IsLastRowOfRowGroup(nsIFrame* aFrame)
|
|
|
|
{
|
2017-04-30 18:30:08 +03:00
|
|
|
if (!aFrame->IsTableRowFrame()) {
|
2015-10-30 08:37:03 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (nsIFrame* c = aFrame; c; c = c->GetNextContinuation()) {
|
|
|
|
if (c->GetNextSibling()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool
|
|
|
|
IsLastNonemptyRowGroupOfTable(nsIFrame* aFrame)
|
|
|
|
{
|
2017-04-30 18:30:08 +03:00
|
|
|
if (!aFrame->IsTableRowGroupFrame()) {
|
2015-10-30 08:37:03 +03:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
for (nsIFrame* c = aFrame; c; c = c->GetNextContinuation()) {
|
|
|
|
for (nsIFrame* next = c->GetNextSibling(); next; next = next->GetNextSibling()) {
|
2016-01-29 17:42:14 +03:00
|
|
|
if (next->PrincipalChildList().FirstChild()) {
|
2015-10-30 08:37:03 +03:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRange::GetInnerTextNoFlush(DOMString& aValue, ErrorResult& aError,
|
2018-01-12 17:33:13 +03:00
|
|
|
nsIContent* aContainer)
|
2015-10-30 08:37:03 +03:00
|
|
|
{
|
|
|
|
InnerTextAccumulator result(aValue);
|
2018-01-12 17:33:13 +03:00
|
|
|
|
|
|
|
if (aContainer->IsNodeOfType(nsINode::eTEXT)) {
|
|
|
|
AppendTransformedText(result, aContainer);
|
|
|
|
return;
|
2015-10-30 08:37:03 +03:00
|
|
|
}
|
|
|
|
|
2018-01-12 17:33:13 +03:00
|
|
|
nsIContent* currentNode = aContainer;
|
|
|
|
TreeTraversalState currentState = AFTER_NODE;
|
|
|
|
|
|
|
|
nsIContent* endNode = aContainer;
|
2015-10-30 08:37:03 +03:00
|
|
|
TreeTraversalState endState = AFTER_NODE;
|
2018-01-12 17:33:13 +03:00
|
|
|
|
|
|
|
nsIContent* firstChild = aContainer->GetFirstChild();
|
|
|
|
if (firstChild) {
|
|
|
|
currentNode = firstChild;
|
|
|
|
currentState = AT_NODE;
|
2015-10-30 08:37:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
while (currentNode != endNode || currentState != endState) {
|
|
|
|
nsIFrame* f = currentNode->GetPrimaryFrame();
|
|
|
|
bool isVisibleAndNotReplaced = IsVisibleAndNotInReplacedElement(f);
|
|
|
|
if (currentState == AT_NODE) {
|
|
|
|
bool isText = currentNode->IsNodeOfType(nsINode::eTEXT);
|
|
|
|
if (isText && currentNode->GetParent()->IsHTMLElement(nsGkAtoms::rp) &&
|
2017-03-18 20:44:27 +03:00
|
|
|
ElementIsVisibleNoFlush(currentNode->GetParent()->AsElement())) {
|
2015-10-30 08:37:03 +03:00
|
|
|
nsAutoString str;
|
|
|
|
currentNode->GetTextContent(str, aError);
|
|
|
|
result.Append(str);
|
|
|
|
} else if (isVisibleAndNotReplaced) {
|
|
|
|
result.AddRequiredLineBreakCount(GetRequiredInnerTextLineBreakCount(f));
|
|
|
|
if (isText) {
|
|
|
|
nsIFrame::RenderedText text = f->GetRenderedText();
|
|
|
|
result.Append(text.mString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
nsIContent* child = currentNode->GetFirstChild();
|
|
|
|
if (child) {
|
|
|
|
currentNode = child;
|
2015-10-30 09:22:33 +03:00
|
|
|
continue;
|
2015-10-30 08:37:03 +03:00
|
|
|
}
|
2015-10-30 09:22:33 +03:00
|
|
|
currentState = AFTER_NODE;
|
|
|
|
}
|
|
|
|
if (currentNode == endNode && currentState == endState) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (isVisibleAndNotReplaced) {
|
|
|
|
if (currentNode->IsHTMLElement(nsGkAtoms::br)) {
|
|
|
|
result.Append('\n');
|
|
|
|
}
|
|
|
|
switch (f->StyleDisplay()->mDisplay) {
|
2016-08-28 05:31:50 +03:00
|
|
|
case StyleDisplay::TableCell:
|
2015-10-30 09:22:33 +03:00
|
|
|
if (!IsLastCellOfRow(f)) {
|
|
|
|
result.Append('\t');
|
2015-10-30 08:37:03 +03:00
|
|
|
}
|
2015-10-30 09:22:33 +03:00
|
|
|
break;
|
2016-08-28 05:31:50 +03:00
|
|
|
case StyleDisplay::TableRow:
|
2015-10-30 09:22:33 +03:00
|
|
|
if (!IsLastRowOfRowGroup(f) ||
|
|
|
|
!IsLastNonemptyRowGroupOfTable(f->GetParent())) {
|
|
|
|
result.Append('\n');
|
2015-10-30 08:37:03 +03:00
|
|
|
}
|
2015-10-30 09:22:33 +03:00
|
|
|
break;
|
2016-08-28 05:31:50 +03:00
|
|
|
default:
|
|
|
|
break; // Do nothing
|
2015-10-30 08:37:03 +03:00
|
|
|
}
|
2015-10-30 09:22:33 +03:00
|
|
|
result.AddRequiredLineBreakCount(GetRequiredInnerTextLineBreakCount(f));
|
|
|
|
}
|
|
|
|
nsIContent* next = currentNode->GetNextSibling();
|
|
|
|
if (next) {
|
|
|
|
currentNode = next;
|
|
|
|
currentState = AT_NODE;
|
|
|
|
} else {
|
|
|
|
currentNode = currentNode->GetParent();
|
2015-10-30 08:37:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Do not flush trailing line breaks! Required breaks at the end of the text
|
|
|
|
// are suppressed.
|
|
|
|
}
|