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"
|
1998-12-18 05:51:34 +03:00
|
|
|
#include "nsIDOMNodeList.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"
|
2011-06-16 10:31:36 +04:00
|
|
|
#include "nsFontFaceList.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"
|
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
|
|
|
}
|
|
|
|
|
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()) {
|
|
|
|
case nsIDOMNode::TEXT_NODE:
|
|
|
|
case nsIDOMNode::ELEMENT_NODE:
|
|
|
|
{
|
|
|
|
nsIContent* content = static_cast<nsIContent*>(aNode);
|
|
|
|
frame = content->GetPrimaryFrame();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case nsIDOMNode::DOCUMENT_NODE:
|
|
|
|
{
|
|
|
|
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 {
|
|
|
|
nodeStart = parent->IndexOf(aNode);
|
|
|
|
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
|
|
|
|
|
|
|
// Collect the potential ranges and their selection objects.
|
|
|
|
RangeHashTable ancestorSelectionRanges;
|
|
|
|
nsTHashtable<nsPtrHashKey<Selection>> ancestorSelections;
|
|
|
|
uint32_t maxRangeCount = 0;
|
2012-10-09 16:31:24 +04:00
|
|
|
for (; n; n = GetNextRangeCommonAncestor(n->GetParentNode())) {
|
2017-07-22 08:29:07 +03:00
|
|
|
nsTHashtable<nsPtrHashKey<nsRange>>* ranges =
|
|
|
|
n->GetExistingCommonAncestorRanges();
|
|
|
|
if (!ranges) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-07-22 19:42:01 +03:00
|
|
|
for (auto iter = ranges->ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
nsRange* range = iter.Get()->GetKey();
|
|
|
|
if (range->IsInSelection() && !range->Collapsed()) {
|
2016-02-13 20:40:23 +03:00
|
|
|
ancestorSelectionRanges.PutEntry(range);
|
|
|
|
Selection* selection = range->mSelection;
|
|
|
|
ancestorSelections.PutEntry(selection);
|
|
|
|
maxRangeCount = std::max(maxRangeCount, selection->RangeCount());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ancestorSelectionRanges.IsEmpty()) {
|
|
|
|
nsTArray<const nsRange*> sortedRanges(maxRangeCount);
|
|
|
|
for (auto iter = ancestorSelections.ConstIter(); !iter.Done(); iter.Next()) {
|
|
|
|
Selection* selection = iter.Get()->GetKey();
|
|
|
|
// Sort the found ranges for |selection| in document order
|
|
|
|
// (Selection::GetRangeAt returns its ranges ordered).
|
|
|
|
for (uint32_t i = 0, len = selection->RangeCount(); i < len; ++i) {
|
|
|
|
nsRange* range = selection->GetRangeAt(i);
|
|
|
|
if (ancestorSelectionRanges.Contains(range)) {
|
|
|
|
sortedRanges.AppendElement(range);
|
2015-07-22 19:42:01 +03:00
|
|
|
}
|
|
|
|
}
|
2016-02-13 20:40:23 +03:00
|
|
|
MOZ_ASSERT(!sortedRanges.IsEmpty());
|
|
|
|
// Binary search the now sorted ranges.
|
|
|
|
IsItemInRangeComparator comparator = { aNode, aStartOffset, aEndOffset };
|
|
|
|
size_t unused;
|
|
|
|
if (mozilla::BinarySearchIf(sortedRanges, 0, sortedRanges.Length(), comparator, &unused)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
sortedRanges.ClearAndRetainStorage();
|
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)
|
2012-07-30 18:20:58 +04:00
|
|
|
DoSetRange(nullptr, 0, nullptr, 0, 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)
|
|
|
|
, mStartOffset(0)
|
|
|
|
, mEndOffset(0)
|
|
|
|
, mIsPositioned(false)
|
|
|
|
, mMaySpanAnonymousSubtrees(false)
|
|
|
|
, mIsGenerated(false)
|
|
|
|
, mStartOffsetWasIncremented(false)
|
|
|
|
, mEndOffsetWasIncremented(false)
|
2017-03-10 10:55:12 +03:00
|
|
|
, mCalledByJS(false)
|
2015-08-12 20:26:01 +03:00
|
|
|
#ifdef DEBUG
|
|
|
|
, mAssertNextInsertOrAppendIndex(-1)
|
|
|
|
, mAssertNextInsertOrAppendNode(nullptr)
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2008-12-03 13:03:10 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(nsRange)
|
2013-07-09 21:30:58 +04:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE_WITH_LAST_RELEASE(nsRange,
|
|
|
|
DoSetRange(nullptr, 0, nullptr, 0, 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);
|
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().
|
|
|
|
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-07-11 16:46:11 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStartContainer)
|
2017-07-11 16:57:55 +03:00
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEndContainer)
|
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");
|
|
|
|
NS_ASSERTION(IsInSelection(), "registering range not in selection");
|
|
|
|
|
|
|
|
MarkDescendants(aNode);
|
|
|
|
|
2017-07-22 08:29:07 +03:00
|
|
|
UniquePtr<nsTHashtable<nsPtrHashKey<nsRange>>>& ranges =
|
|
|
|
aNode->GetCommonAncestorRangesPtr();
|
2011-12-20 13:15:41 +04:00
|
|
|
if (!ranges) {
|
2017-07-22 08:29:07 +03:00
|
|
|
ranges = MakeUnique<nsRange::RangeHashTable>();
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
|
|
|
ranges->PutEntry(this);
|
|
|
|
aNode->SetCommonAncestorForRangeInSelection();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2012-01-10 18:19:54 +04:00
|
|
|
nsRange::UnregisterCommonAncestor(nsINode* aNode)
|
2011-12-20 13:15:41 +04:00
|
|
|
{
|
|
|
|
NS_PRECONDITION(aNode, "bad arg");
|
|
|
|
NS_ASSERTION(aNode->IsCommonAncestorForRangeInSelection(), "wrong node");
|
2017-07-22 08:29:07 +03:00
|
|
|
nsTHashtable<nsPtrHashKey<nsRange>>* ranges =
|
|
|
|
aNode->GetExistingCommonAncestorRanges();
|
|
|
|
MOZ_ASSERT(ranges);
|
2011-12-20 13:15:41 +04:00
|
|
|
NS_ASSERTION(ranges->GetEntry(this), "unknown range");
|
|
|
|
|
|
|
|
if (ranges->Count() == 1) {
|
|
|
|
aNode->ClearCommonAncestorForRangeInSelection();
|
2017-07-22 08:29:07 +03:00
|
|
|
aNode->GetCommonAncestorRangesPtr().reset();
|
2011-12-20 13:15:41 +04:00
|
|
|
UnmarkDescendants(aNode);
|
|
|
|
} else {
|
|
|
|
ranges->RemoveEntry(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-11-04 00:51:01 +03:00
|
|
|
/******************************************************
|
|
|
|
* nsIMutationObserver implementation
|
|
|
|
******************************************************/
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRange::CharacterDataChanged(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContent,
|
|
|
|
CharacterDataChangeInfo* aInfo)
|
|
|
|
{
|
2012-11-21 00:14:15 +04:00
|
|
|
MOZ_ASSERT(mAssertNextInsertOrAppendIndex == -1,
|
|
|
|
"splitText failed to notify insert/append?");
|
2006-11-04 00:51:01 +03:00
|
|
|
NS_ASSERTION(mIsPositioned, "shouldn't be notified if not positioned");
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsINode* newRoot = nullptr;
|
|
|
|
nsINode* newStartNode = nullptr;
|
|
|
|
nsINode* newEndNode = nullptr;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t newStartOffset = 0;
|
|
|
|
uint32_t newEndOffset = 0;
|
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)
|
|
|
|
// then we need to increment the corresponding offset to account for the new
|
|
|
|
// text node that will be inserted. If so, we need to prevent the next
|
|
|
|
// ContentInserted or ContentAppended for this range from incrementing it
|
|
|
|
// again (when the new text node is notified).
|
|
|
|
nsINode* parentNode = aContent->GetParentNode();
|
|
|
|
int32_t index = -1;
|
2017-07-19 16:49:52 +03:00
|
|
|
if (parentNode == mEndContainer && mEndOffset > 0) {
|
|
|
|
index = parentNode->IndexOf(aContent);
|
|
|
|
NS_WARNING_ASSERTION(index >= 0,
|
|
|
|
"Shouldn't be called during removing the node or something");
|
|
|
|
if (static_cast<uint32_t>(index + 1) == mEndOffset) {
|
|
|
|
newEndNode = mEndContainer;
|
|
|
|
newEndOffset = mEndOffset + 1;
|
|
|
|
MOZ_ASSERT(IsValidOffset(newEndOffset));
|
|
|
|
mEndOffsetWasIncremented = true;
|
|
|
|
}
|
2012-11-21 00:14:15 +04:00
|
|
|
}
|
2017-07-19 16:49:52 +03:00
|
|
|
if (parentNode == mStartContainer && mStartOffset > 0) {
|
|
|
|
if (index <= 0) {
|
|
|
|
index = parentNode->IndexOf(aContent);
|
|
|
|
}
|
|
|
|
if (static_cast<uint32_t>(index + 1) == mStartOffset) {
|
|
|
|
newStartNode = mStartContainer;
|
|
|
|
newStartOffset = mStartOffset + 1;
|
|
|
|
MOZ_ASSERT(IsValidOffset(newStartOffset));
|
|
|
|
mStartOffsetWasIncremented = true;
|
|
|
|
}
|
2012-11-21 00:14:15 +04:00
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
if (mStartOffsetWasIncremented || mEndOffsetWasIncremented) {
|
|
|
|
mAssertNextInsertOrAppendIndex =
|
2017-07-19 21:29:59 +03:00
|
|
|
(mStartOffsetWasIncremented ? newStartOffset : newEndOffset) - 1;
|
2012-11-21 00:14:15 +04:00
|
|
|
mAssertNextInsertOrAppendNode = aInfo->mDetails->mNextSibling;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
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-07-19 16:49:52 +03:00
|
|
|
if (aContent == mStartContainer && aInfo->mChangeStart < mStartOffset) {
|
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-07-19 16:49:52 +03:00
|
|
|
NS_ASSERTION(mStartOffset <= aInfo->mChangeEnd + 1,
|
2011-08-16 04:55:20 +04:00
|
|
|
"mStartOffset is beyond the end of this node");
|
2017-07-19 16:49:52 +03:00
|
|
|
newStartOffset = mStartOffset - aInfo->mChangeStart;
|
2011-09-24 04:56:38 +04:00
|
|
|
newStartNode = aInfo->mDetails->mNextSibling;
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(aContent == mRoot)) {
|
2011-09-24 04:56:38 +04:00
|
|
|
newRoot = IsValidBoundary(newStartNode);
|
|
|
|
}
|
2011-12-20 13:15:41 +04:00
|
|
|
|
2017-07-11 16:57:55 +03:00
|
|
|
bool isCommonAncestor =
|
|
|
|
IsInSelection() && mStartContainer == mEndContainer;
|
2011-12-20 13:15:41 +04:00
|
|
|
if (isCommonAncestor) {
|
2017-07-11 16:46:11 +03:00
|
|
|
UnregisterCommonAncestor(mStartContainer);
|
2011-12-20 13:15:41 +04:00
|
|
|
RegisterCommonAncestor(newStartNode);
|
|
|
|
}
|
2017-07-11 16:46:11 +03:00
|
|
|
if (mStartContainer->IsDescendantOfCommonAncestorForRangeInSelection()) {
|
2011-12-20 13:15:41 +04:00
|
|
|
newStartNode->SetDescendantOfCommonAncestorForRangeInSelection();
|
|
|
|
}
|
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-07-19 21:29:59 +03:00
|
|
|
newStartNode = mStartContainer;
|
2017-07-19 16:49:52 +03:00
|
|
|
newStartOffset = mStartOffset <= aInfo->mChangeEnd ?
|
2011-08-16 04:55:20 +04:00
|
|
|
aInfo->mChangeStart :
|
|
|
|
mStartOffset + aInfo->mChangeStart - aInfo->mChangeEnd +
|
|
|
|
aInfo->mReplaceLength;
|
|
|
|
}
|
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-07-19 16:49:52 +03:00
|
|
|
if (aContent == mEndContainer && aInfo->mChangeStart < mEndOffset) {
|
2012-10-09 16:31:24 +04:00
|
|
|
if (aInfo->mDetails && (aContent->GetParentNode() || newStartNode)) {
|
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-07-19 16:49:52 +03:00
|
|
|
NS_ASSERTION(mEndOffset <= aInfo->mChangeEnd + 1,
|
2011-08-16 04:55:20 +04:00
|
|
|
"mEndOffset is beyond the end of this node");
|
2017-07-19 16:49:52 +03:00
|
|
|
newEndOffset = mEndOffset - aInfo->mChangeStart;
|
2011-09-24 04:56:38 +04:00
|
|
|
newEndNode = aInfo->mDetails->mNextSibling;
|
2011-12-20 13:15:41 +04:00
|
|
|
|
2017-07-11 16:57:55 +03:00
|
|
|
bool isCommonAncestor =
|
|
|
|
IsInSelection() && mStartContainer == mEndContainer;
|
2011-12-20 13:15:41 +04:00
|
|
|
if (isCommonAncestor && !newStartNode) {
|
|
|
|
// The split occurs inside the range.
|
2017-07-11 16:46:11 +03:00
|
|
|
UnregisterCommonAncestor(mStartContainer);
|
|
|
|
RegisterCommonAncestor(mStartContainer->GetParentNode());
|
2011-12-20 13:15:41 +04:00
|
|
|
newEndNode->SetDescendantOfCommonAncestorForRangeInSelection();
|
2017-07-11 16:57:55 +03:00
|
|
|
} else if (mEndContainer->
|
|
|
|
IsDescendantOfCommonAncestorForRangeInSelection()) {
|
2011-12-20 13:15:41 +04:00
|
|
|
newEndNode->SetDescendantOfCommonAncestorForRangeInSelection();
|
|
|
|
}
|
2011-08-16 04:55:20 +04:00
|
|
|
} else {
|
2017-07-19 21:29:59 +03:00
|
|
|
newEndNode = mEndContainer;
|
2017-07-19 16:49:52 +03:00
|
|
|
newEndOffset = mEndOffset <= aInfo->mChangeEnd ?
|
2011-08-16 04:55:20 +04:00
|
|
|
aInfo->mChangeStart :
|
|
|
|
mEndOffset + aInfo->mChangeStart - aInfo->mChangeEnd +
|
|
|
|
aInfo->mReplaceLength;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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-07-11 16:46:11 +03:00
|
|
|
if (removed == mStartContainer) {
|
2017-07-19 16:49:52 +03:00
|
|
|
newStartOffset = mStartOffset + aInfo->mChangeStart;
|
2011-09-24 04:56:38 +04:00
|
|
|
newStartNode = aContent;
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(removed == mRoot)) {
|
2011-09-24 04:56:38 +04:00
|
|
|
newRoot = IsValidBoundary(newStartNode);
|
|
|
|
}
|
2011-08-16 04:55:20 +04:00
|
|
|
}
|
2017-07-11 16:57:55 +03:00
|
|
|
if (removed == mEndContainer) {
|
2017-07-19 16:49:52 +03:00
|
|
|
newEndOffset = mEndOffset + aInfo->mChangeStart;
|
2011-09-24 04:56:38 +04:00
|
|
|
newEndNode = aContent;
|
2012-10-26 17:32:10 +04:00
|
|
|
if (MOZ_UNLIKELY(removed == mRoot)) {
|
2011-09-24 04:56:38 +04:00
|
|
|
newRoot = IsValidBoundary(newEndNode);
|
|
|
|
}
|
|
|
|
}
|
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-07-11 16:46:11 +03:00
|
|
|
if (parentNode == mStartContainer && mStartOffset > 0 &&
|
2017-07-19 16:49:52 +03:00
|
|
|
mStartOffset < parentNode->GetChildCount() &&
|
2012-11-21 00:14:15 +04:00
|
|
|
removed == parentNode->GetChildAt(mStartOffset)) {
|
|
|
|
newStartNode = aContent;
|
|
|
|
newStartOffset = aInfo->mChangeStart;
|
|
|
|
}
|
2017-07-11 16:57:55 +03:00
|
|
|
if (parentNode == mEndContainer && mEndOffset > 0 &&
|
2017-07-19 16:49:52 +03:00
|
|
|
mEndOffset < parentNode->GetChildCount() &&
|
2012-11-21 00:14:15 +04:00
|
|
|
removed == parentNode->GetChildAt(mEndOffset)) {
|
|
|
|
newEndNode = aContent;
|
|
|
|
newEndOffset = aInfo->mChangeEnd;
|
|
|
|
}
|
2011-09-24 04:56:38 +04:00
|
|
|
}
|
2012-11-21 00:14:15 +04:00
|
|
|
|
2011-09-24 04:56:38 +04:00
|
|
|
if (newStartNode || newEndNode) {
|
|
|
|
if (!newStartNode) {
|
2017-07-11 16:46:11 +03:00
|
|
|
newStartNode = mStartContainer;
|
2011-09-24 04:56:38 +04:00
|
|
|
newStartOffset = mStartOffset;
|
|
|
|
}
|
|
|
|
if (!newEndNode) {
|
2017-07-11 16:57:55 +03:00
|
|
|
newEndNode = mEndContainer;
|
2011-09-24 04:56:38 +04:00
|
|
|
newEndOffset = mEndOffset;
|
2011-08-16 04:55:20 +04:00
|
|
|
}
|
2011-09-24 04:56:38 +04:00
|
|
|
DoSetRange(newStartNode, newStartOffset, newEndNode, newEndOffset,
|
2011-12-20 13:15:41 +04:00
|
|
|
newRoot ? newRoot : mRoot.get(),
|
2012-10-09 16:31:24 +04:00
|
|
|
!newEndNode->GetParentNode() || !newStartNode->GetParentNode());
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRange::ContentAppended(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aFirstNewContent,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aNewIndexInContainer)
|
2011-12-20 13:15:41 +04:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mIsPositioned, "shouldn't be notified if not positioned");
|
|
|
|
|
|
|
|
nsINode* container = NODE_FROM(aContainer, aDocument);
|
|
|
|
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
|
|
|
|
|
|
|
if (mStartOffsetWasIncremented || mEndOffsetWasIncremented) {
|
|
|
|
MOZ_ASSERT(mAssertNextInsertOrAppendIndex == aNewIndexInContainer);
|
|
|
|
MOZ_ASSERT(mAssertNextInsertOrAppendNode == aFirstNewContent);
|
2012-11-23 19:37:31 +04:00
|
|
|
MOZ_ASSERT(aFirstNewContent->IsNodeOfType(nsINode::eDATA_NODE));
|
2012-11-21 00:14:15 +04:00
|
|
|
mStartOffsetWasIncremented = mEndOffsetWasIncremented = false;
|
|
|
|
#ifdef DEBUG
|
|
|
|
mAssertNextInsertOrAppendIndex = -1;
|
|
|
|
mAssertNextInsertOrAppendNode = nullptr;
|
|
|
|
#endif
|
|
|
|
}
|
2006-11-04 00:51:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRange::ContentInserted(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aIndexInContainer)
|
2006-11-04 00:51:01 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mIsPositioned, "shouldn't be notified if not positioned");
|
|
|
|
|
2017-07-19 21:29:59 +03:00
|
|
|
bool rangeChanged = false;
|
|
|
|
uint32_t newStartOffset = mStartOffset;
|
|
|
|
uint32_t newEndOffset = mEndOffset;
|
2006-11-04 00:51:01 +03:00
|
|
|
nsINode* container = NODE_FROM(aContainer, aDocument);
|
|
|
|
|
|
|
|
// Adjust position if a sibling was inserted.
|
2017-07-19 16:49:52 +03:00
|
|
|
if (container == mStartContainer &&
|
|
|
|
(NS_WARN_IF(aIndexInContainer < 0) ||
|
|
|
|
static_cast<uint32_t>(aIndexInContainer) < mStartOffset) &&
|
2012-11-21 00:14:15 +04:00
|
|
|
!mStartOffsetWasIncremented) {
|
2017-07-19 21:29:59 +03:00
|
|
|
++newStartOffset;
|
2017-07-19 16:49:52 +03:00
|
|
|
MOZ_ASSERT(IsValidOffset(newStartOffset));
|
2017-07-19 21:29:59 +03:00
|
|
|
rangeChanged = true;
|
2006-11-04 00:51:01 +03:00
|
|
|
}
|
2017-07-19 16:49:52 +03:00
|
|
|
if (container == mEndContainer &&
|
|
|
|
(NS_WARN_IF(aIndexInContainer < 0) ||
|
|
|
|
static_cast<uint32_t>(aIndexInContainer) < mEndOffset) &&
|
2012-11-21 00:14:15 +04:00
|
|
|
!mEndOffsetWasIncremented) {
|
2017-07-19 21:29:59 +03:00
|
|
|
++newEndOffset;
|
2017-07-19 16:49:52 +03:00
|
|
|
MOZ_ASSERT(IsValidOffset(newEndOffset));
|
2017-07-19 21:29:59 +03:00
|
|
|
rangeChanged = true;
|
2006-11-04 00:51:01 +03:00
|
|
|
}
|
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
|
|
|
|
|
|
|
if (mStartOffsetWasIncremented || mEndOffsetWasIncremented) {
|
|
|
|
MOZ_ASSERT(mAssertNextInsertOrAppendIndex == aIndexInContainer);
|
|
|
|
MOZ_ASSERT(mAssertNextInsertOrAppendNode == aChild);
|
2012-11-23 19:37:31 +04:00
|
|
|
MOZ_ASSERT(aChild->IsNodeOfType(nsINode::eDATA_NODE));
|
2012-11-21 00:14:15 +04:00
|
|
|
mStartOffsetWasIncremented = mEndOffsetWasIncremented = false;
|
|
|
|
#ifdef DEBUG
|
|
|
|
mAssertNextInsertOrAppendIndex = -1;
|
|
|
|
mAssertNextInsertOrAppendNode = nullptr;
|
|
|
|
#endif
|
|
|
|
}
|
2017-07-19 21:29:59 +03:00
|
|
|
|
|
|
|
if (rangeChanged) {
|
|
|
|
DoSetRange(mStartContainer, newStartOffset,
|
|
|
|
mEndContainer, newEndOffset, mRoot);
|
|
|
|
}
|
2006-11-04 00:51:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsRange::ContentRemoved(nsIDocument* aDocument,
|
|
|
|
nsIContent* aContainer,
|
|
|
|
nsIContent* aChild,
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t aIndexInContainer,
|
2010-07-22 02:05:17 +04:00
|
|
|
nsIContent* aPreviousSibling)
|
2006-11-04 00:51:01 +03:00
|
|
|
{
|
|
|
|
NS_ASSERTION(mIsPositioned, "shouldn't be notified if not positioned");
|
2012-11-21 00:14:15 +04:00
|
|
|
MOZ_ASSERT(!mStartOffsetWasIncremented && !mEndOffsetWasIncremented &&
|
|
|
|
mAssertNextInsertOrAppendIndex == -1,
|
|
|
|
"splitText failed to notify insert/append?");
|
2006-11-04 00:51:01 +03:00
|
|
|
|
|
|
|
nsINode* container = NODE_FROM(aContainer, aDocument);
|
2011-12-20 13:15:41 +04:00
|
|
|
bool gravitateStart = false;
|
|
|
|
bool gravitateEnd = false;
|
2013-08-13 19:40:42 +04:00
|
|
|
bool didCheckStartParentDescendant = false;
|
2017-07-19 21:29:59 +03:00
|
|
|
bool rangeChanged = false;
|
|
|
|
uint32_t newStartOffset = mStartOffset;
|
|
|
|
uint32_t newEndOffset = mEndOffset;
|
2006-11-04 00:51:01 +03:00
|
|
|
|
|
|
|
// Adjust position if a sibling was removed...
|
2017-07-11 16:46:11 +03:00
|
|
|
if (container == mStartContainer) {
|
2017-07-19 16:49:52 +03:00
|
|
|
if (aIndexInContainer < static_cast<int32_t>(mStartOffset)) {
|
2017-07-19 21:29:59 +03:00
|
|
|
--newStartOffset;
|
|
|
|
rangeChanged = true;
|
2009-07-27 16:58:23 +04:00
|
|
|
}
|
2013-08-13 19:40:42 +04:00
|
|
|
} else { // ...or gravitate if an ancestor was removed.
|
|
|
|
didCheckStartParentDescendant = true;
|
2017-07-11 16:46:11 +03:00
|
|
|
gravitateStart =
|
|
|
|
nsContentUtils::ContentIsDescendantOf(mStartContainer, aChild);
|
2006-11-04 00:51:01 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do same thing for end boundry.
|
2017-07-11 16:57:55 +03:00
|
|
|
if (container == mEndContainer) {
|
2017-07-19 16:49:52 +03:00
|
|
|
if (aIndexInContainer < static_cast<int32_t>(mEndOffset)) {
|
2017-07-19 21:29:59 +03:00
|
|
|
--newEndOffset;
|
|
|
|
rangeChanged = true;
|
2009-07-27 16:58:23 +04:00
|
|
|
}
|
2017-07-11 16:57:55 +03:00
|
|
|
} else if (didCheckStartParentDescendant &&
|
|
|
|
mStartContainer == mEndContainer) {
|
2013-08-13 19:40:42 +04:00
|
|
|
gravitateEnd = gravitateStart;
|
|
|
|
} else {
|
2017-07-11 16:57:55 +03:00
|
|
|
gravitateEnd = nsContentUtils::ContentIsDescendantOf(mEndContainer, aChild);
|
2011-12-20 13:15:41 +04:00
|
|
|
}
|
|
|
|
|
2017-07-19 21:29:59 +03:00
|
|
|
if (gravitateStart || gravitateEnd || rangeChanged) {
|
2017-07-11 16:46:11 +03:00
|
|
|
DoSetRange(gravitateStart ? container : mStartContainer.get(),
|
2017-07-19 21:29:59 +03:00
|
|
|
gravitateStart ? aIndexInContainer : newStartOffset,
|
2017-07-11 16:57:55 +03:00
|
|
|
gravitateEnd ? container : mEndContainer.get(),
|
2017-07-19 21:29:59 +03:00
|
|
|
gravitateEnd ? aIndexInContainer : newEndOffset,
|
2011-12-20 13:15:41 +04:00
|
|
|
mRoot);
|
|
|
|
}
|
|
|
|
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)
|
|
|
|
{
|
2012-11-21 00:14:15 +04:00
|
|
|
MOZ_ASSERT(!mStartOffsetWasIncremented && !mEndOffsetWasIncremented &&
|
|
|
|
mAssertNextInsertOrAppendIndex == -1,
|
|
|
|
"splitText failed to notify insert/append?");
|
2008-02-24 15:46:09 +03:00
|
|
|
NS_ASSERTION(mRoot == aContent, "Wrong ParentChainChanged notification?");
|
2017-07-11 16:46:11 +03:00
|
|
|
nsINode* newRoot = IsValidBoundary(mStartContainer);
|
2008-03-13 13:12:51 +03:00
|
|
|
NS_ASSERTION(newRoot, "No valid boundary or root found!");
|
2017-07-11 16:57:55 +03:00
|
|
|
if (newRoot != IsValidBoundary(mEndContainer)) {
|
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-07-11 16:57:55 +03:00
|
|
|
NS_ASSERTION(mEndContainer->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-07-11 16:57:55 +03:00
|
|
|
DoSetRange(mStartContainer, mStartOffset, mEndContainer, mEndOffset, 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-07-11 17:52:39 +03:00
|
|
|
*aResult = IsPointInRange(*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-07-11 17:33:04 +03:00
|
|
|
nsRange::IsPointInRange(nsINode& aContainer, uint32_t aOffset, ErrorResult& aRv)
|
2013-02-07 16:09:41 +04:00
|
|
|
{
|
2017-07-11 17:33:04 +03:00
|
|
|
uint16_t compareResult = ComparePoint(aContainer, aOffset, 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-07-11 17:52:39 +03:00
|
|
|
*aResult = ComparePoint(*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-07-11 17:33:04 +03:00
|
|
|
nsRange::ComparePoint(nsINode& aContainer, uint32_t aOffset, ErrorResult& aRv)
|
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-07-11 17:33:04 +03:00
|
|
|
if (!nsContentUtils::ContentIsDescendantOf(&aContainer, 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
|
|
|
|
2017-07-11 17:33:04 +03:00
|
|
|
if (aContainer.NodeType() == nsIDOMNode::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-07-11 17:33:04 +03:00
|
|
|
if (aOffset > aContainer.Length()) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-07-19 16:49:52 +03:00
|
|
|
int32_t cmp =
|
|
|
|
nsContentUtils::ComparePoints(&aContainer,
|
|
|
|
static_cast<int32_t>(aOffset),
|
|
|
|
mStartContainer,
|
|
|
|
static_cast<int32_t>(mStartOffset));
|
|
|
|
if (cmp <= 0) {
|
2013-02-07 16:09:41 +04:00
|
|
|
return cmp;
|
2000-06-08 02:57:36 +04:00
|
|
|
}
|
2017-07-19 16:49:52 +03:00
|
|
|
if (nsContentUtils::ComparePoints(mEndContainer,
|
|
|
|
static_cast<int32_t>(mEndOffset),
|
|
|
|
&aContainer,
|
|
|
|
static_cast<int32_t>(aOffset)) == -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.
|
2013-02-07 16:09:41 +04:00
|
|
|
int32_t nodeIndex = parent->IndexOf(&aNode);
|
2012-07-18 14:36:08 +04:00
|
|
|
|
|
|
|
// Steps 6-7.
|
|
|
|
// Note: if disconnected is true, ComparePoints returns 1.
|
|
|
|
bool disconnected = false;
|
2017-07-19 16:49:52 +03:00
|
|
|
bool result =
|
|
|
|
nsContentUtils::ComparePoints(mStartContainer,
|
|
|
|
static_cast<int32_t>(mStartOffset),
|
|
|
|
parent, nodeIndex + 1,
|
|
|
|
&disconnected) < 0 &&
|
|
|
|
nsContentUtils::ComparePoints(parent, nodeIndex,
|
|
|
|
mEndContainer,
|
|
|
|
static_cast<int32_t>(mEndOffset),
|
|
|
|
&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
|
|
|
}
|
|
|
|
|
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-07-19 16:49:52 +03:00
|
|
|
nsRange::DoSetRange(nsINode* aStartN, uint32_t aStartOffset,
|
|
|
|
nsINode* aEndN, uint32_t aEndOffset,
|
2011-12-20 13:15:41 +04:00
|
|
|
nsINode* aRoot, bool aNotInsertedYet)
|
1998-12-04 21:21:52 +03:00
|
|
|
{
|
2006-11-04 00:51:01 +03:00
|
|
|
NS_PRECONDITION((aStartN && aEndN && aRoot) ||
|
|
|
|
(!aStartN && !aEndN && !aRoot),
|
|
|
|
"Set all or none");
|
2011-09-24 04:56:38 +04:00
|
|
|
NS_PRECONDITION(!aRoot || aNotInsertedYet ||
|
2006-11-04 00:51:01 +03:00
|
|
|
(nsContentUtils::ContentIsDescendantOf(aStartN, aRoot) &&
|
2006-11-07 02:52:30 +03:00
|
|
|
nsContentUtils::ContentIsDescendantOf(aEndN, aRoot) &&
|
|
|
|
aRoot == IsValidBoundary(aStartN) &&
|
|
|
|
aRoot == IsValidBoundary(aEndN)),
|
2006-11-04 00:51:01 +03:00
|
|
|
"Wrong root");
|
|
|
|
NS_PRECONDITION(!aRoot ||
|
2007-03-01 23:48:26 +03:00
|
|
|
(aStartN->IsNodeOfType(nsINode::eCONTENT) &&
|
|
|
|
aEndN->IsNodeOfType(nsINode::eCONTENT) &&
|
|
|
|
aRoot ==
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<nsIContent*>(aStartN)->GetBindingParent() &&
|
2007-03-01 23:48:26 +03:00
|
|
|
aRoot ==
|
2007-07-08 11:08:04 +04:00
|
|
|
static_cast<nsIContent*>(aEndN)->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*/
|
|
|
|
aRoot->IsNodeOfType(nsINode::eCONTENT))),
|
2006-11-04 00:51:01 +03:00
|
|
|
"Bad root");
|
2017-07-19 16:49:52 +03:00
|
|
|
MOZ_ASSERT(IsValidOffset(aStartOffset));
|
|
|
|
MOZ_ASSERT(IsValidOffset(aEndOffset));
|
2006-11-04 00:51:01 +03:00
|
|
|
|
|
|
|
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-07-11 16:57:55 +03:00
|
|
|
(mStartContainer != aStartN || mEndContainer != aEndN) &&
|
2017-07-11 16:46:11 +03:00
|
|
|
IsInSelection() && !aNotInsertedYet;
|
2012-07-30 18:20:58 +04:00
|
|
|
nsINode* oldCommonAncestor = checkCommonAncestor ? GetCommonAncestor() : nullptr;
|
2017-07-11 16:46:11 +03:00
|
|
|
mStartContainer = aStartN;
|
1998-12-04 21:21:52 +03:00
|
|
|
mStartOffset = aStartOffset;
|
2017-07-11 16:57:55 +03:00
|
|
|
mEndContainer = aEndN;
|
1998-12-04 21:21:52 +03:00
|
|
|
mEndOffset = aEndOffset;
|
2017-07-11 16:46:11 +03:00
|
|
|
mIsPositioned = !!mStartContainer;
|
2011-12-20 13:15:41 +04:00
|
|
|
if (checkCommonAncestor) {
|
|
|
|
nsINode* newCommonAncestor = GetCommonAncestor();
|
|
|
|
if (newCommonAncestor != oldCommonAncestor) {
|
|
|
|
if (oldCommonAncestor) {
|
|
|
|
UnregisterCommonAncestor(oldCommonAncestor);
|
|
|
|
}
|
|
|
|
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;
|
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.
|
|
|
|
if (mSelection) {
|
2017-03-17 07:32:51 +03:00
|
|
|
// 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;
|
2017-03-14 04:36:21 +03:00
|
|
|
// 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;
|
2017-03-17 07:32:51 +03:00
|
|
|
selection->NotifySelectionListeners(calledByJSRestorer.SavedValue());
|
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();
|
|
|
|
|
|
|
|
return parent ? parent->IndexOf(aChild) : -1;
|
|
|
|
}
|
|
|
|
|
2015-08-12 20:26:01 +03:00
|
|
|
void
|
|
|
|
nsRange::SetSelection(mozilla::dom::Selection* aSelection)
|
|
|
|
{
|
|
|
|
if (mSelection == aSelection) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// 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);
|
|
|
|
|
|
|
|
mSelection = aSelection;
|
|
|
|
nsINode* commonAncestor = GetCommonAncestor();
|
|
|
|
NS_ASSERTION(commonAncestor, "unexpected disconnected nodes");
|
|
|
|
if (mSelection) {
|
|
|
|
RegisterCommonAncestor(commonAncestor);
|
|
|
|
} else {
|
|
|
|
UnregisterCommonAncestor(commonAncestor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-07-11 16:57:55 +03:00
|
|
|
nsContentUtils::GetCommonAncestor(mStartContainer, mEndContainer) :
|
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()
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
DoSetRange(nullptr, 0, nullptr, 0, 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-07-11 17:10:42 +03:00
|
|
|
return CallQueryInterface(mStartContainer, 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-07-11 16:46:11 +03:00
|
|
|
return mStartContainer;
|
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
|
|
|
|
1998-12-03 12:51:05 +03:00
|
|
|
*aStartOffset = mStartOffset;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mStartOffset;
|
|
|
|
}
|
|
|
|
|
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-07-11 18:09:37 +03:00
|
|
|
return CallQueryInterface(mEndContainer, 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-07-11 16:57:55 +03:00
|
|
|
return mEndContainer;
|
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
|
|
|
|
1998-12-03 12:51:05 +03:00
|
|
|
*aEndOffset = mEndOffset;
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mEndOffset;
|
|
|
|
}
|
|
|
|
|
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-07-11 16:57:55 +03:00
|
|
|
return nsContentUtils::GetCommonAncestor(mStartContainer, mEndContainer);
|
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();
|
|
|
|
}
|
|
|
|
|
2012-01-10 18:19:54 +04:00
|
|
|
nsINode*
|
|
|
|
nsRange::IsValidBoundary(nsINode* aNode)
|
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
|
|
|
}
|
|
|
|
|
2007-03-01 01:19:20 +03:00
|
|
|
if (aNode->IsNodeOfType(nsINode::eCONTENT)) {
|
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
|
|
|
}
|
|
|
|
|
2015-03-03 14:09:00 +03:00
|
|
|
nsIContent* content = static_cast<nsIContent*>(aNode);
|
|
|
|
|
2007-06-19 07:01:03 +04:00
|
|
|
if (!mMaySpanAnonymousSubtrees) {
|
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-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);
|
|
|
|
aRv = SetStart(&aNode, aOffset);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
/* virtual */ nsresult
|
2017-07-19 16:49:52 +03:00
|
|
|
nsRange::SetStart(nsINode* aContainer, uint32_t aOffset)
|
2009-04-06 21:17:37 +04:00
|
|
|
{
|
2017-07-11 17:33:04 +03:00
|
|
|
nsINode* newRoot = IsValidBoundary(aContainer);
|
2015-07-15 03:52:25 +03:00
|
|
|
if (!newRoot) {
|
|
|
|
return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
|
|
|
|
}
|
2006-10-21 05:30:54 +04:00
|
|
|
|
2017-07-11 17:33:04 +03:00
|
|
|
if (!IsValidOffset(aContainer, aOffset)) {
|
2002-08-23 22:02:45 +04:00
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
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-07-19 16:49:52 +03:00
|
|
|
nsContentUtils::ComparePoints(aContainer,
|
|
|
|
static_cast<int32_t>(aOffset),
|
|
|
|
mEndContainer,
|
|
|
|
static_cast<int32_t>(mEndOffset)) == 1) {
|
2017-07-11 17:33:04 +03:00
|
|
|
DoSetRange(aContainer, aOffset, aContainer, aOffset, newRoot);
|
2006-10-21 05:30:54 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
1998-12-03 12:51:05 +03:00
|
|
|
}
|
2002-06-26 00:03:06 +04:00
|
|
|
|
2017-07-11 17:33:04 +03:00
|
|
|
DoSetRange(aContainer, aOffset, mEndContainer, mEndOffset, mRoot);
|
2013-07-12 07:29:24 +04: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::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);
|
|
|
|
aRv = SetEnd(&aNode, aOffset);
|
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
|
|
|
|
2010-07-17 20:24:50 +04:00
|
|
|
/* virtual */ nsresult
|
2017-07-19 16:49:52 +03:00
|
|
|
nsRange::SetEnd(nsINode* aContainer, uint32_t aOffset)
|
2009-04-06 21:17:37 +04:00
|
|
|
{
|
2017-07-11 17:33:04 +03:00
|
|
|
nsINode* newRoot = IsValidBoundary(aContainer);
|
2015-07-15 03:54:58 +03:00
|
|
|
if (!newRoot) {
|
|
|
|
return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
|
|
|
|
}
|
2006-10-21 05:30:54 +04:00
|
|
|
|
2017-07-11 17:33:04 +03:00
|
|
|
if (!IsValidOffset(aContainer, aOffset)) {
|
2002-08-23 22:02:45 +04:00
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
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-07-19 16:49:52 +03:00
|
|
|
nsContentUtils::ComparePoints(mStartContainer,
|
|
|
|
static_cast<int32_t>(mStartOffset),
|
|
|
|
aContainer,
|
|
|
|
static_cast<int32_t>(aOffset)) == 1) {
|
2017-07-11 17:33:04 +03:00
|
|
|
DoSetRange(aContainer, aOffset, aContainer, aOffset, newRoot);
|
2006-10-21 05:30:54 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
1998-12-04 21:21:52 +03:00
|
|
|
}
|
2006-10-21 05:30:54 +04:00
|
|
|
|
2017-07-11 17:33:04 +03:00
|
|
|
DoSetRange(mStartContainer, mStartOffset, aContainer, aOffset, mRoot);
|
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-05-30 07:18:25 +03:00
|
|
|
nsresult
|
2017-07-19 16:49:52 +03:00
|
|
|
nsRange::SetStartAndEnd(nsINode* aStartContainer, uint32_t aStartOffset,
|
|
|
|
nsINode* aEndContainer, uint32_t aEndOffset)
|
2017-05-30 07:18:25 +03:00
|
|
|
{
|
2017-07-11 18:09:37 +03:00
|
|
|
if (NS_WARN_IF(!aStartContainer) || NS_WARN_IF(!aEndContainer)) {
|
2017-05-30 07:18:25 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2017-07-11 17:10:42 +03:00
|
|
|
nsINode* newStartRoot = IsValidBoundary(aStartContainer);
|
2017-05-30 07:18:25 +03:00
|
|
|
if (!newStartRoot) {
|
|
|
|
return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
|
|
|
|
}
|
2017-07-11 17:10:42 +03:00
|
|
|
if (!IsValidOffset(aStartContainer, aStartOffset)) {
|
2017-05-30 07:18:25 +03:00
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
}
|
|
|
|
|
2017-07-11 18:09:37 +03:00
|
|
|
if (aStartContainer == aEndContainer) {
|
|
|
|
if (!IsValidOffset(aEndContainer, aEndOffset)) {
|
2017-05-30 07:18:25 +03:00
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
}
|
|
|
|
// If the end offset is less than the start offset, this should be
|
|
|
|
// collapsed at the end offset.
|
|
|
|
if (aStartOffset > aEndOffset) {
|
2017-07-11 18:09:37 +03:00
|
|
|
DoSetRange(aEndContainer, aEndOffset,
|
|
|
|
aEndContainer, aEndOffset, newStartRoot);
|
2017-05-30 07:18:25 +03:00
|
|
|
} else {
|
2017-07-11 17:10:42 +03:00
|
|
|
DoSetRange(aStartContainer, aStartOffset,
|
2017-07-11 18:09:37 +03:00
|
|
|
aEndContainer, aEndOffset, newStartRoot);
|
2017-05-30 07:18:25 +03:00
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2017-07-11 18:09:37 +03:00
|
|
|
nsINode* newEndRoot = IsValidBoundary(aEndContainer);
|
2017-05-30 07:18:25 +03:00
|
|
|
if (!newEndRoot) {
|
|
|
|
return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
|
|
|
|
}
|
2017-07-11 18:09:37 +03:00
|
|
|
if (!IsValidOffset(aEndContainer, aEndOffset)) {
|
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-07-11 18:09:37 +03:00
|
|
|
DoSetRange(aEndContainer, aEndOffset,
|
|
|
|
aEndContainer, aEndOffset, 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-07-19 16:49:52 +03:00
|
|
|
if (nsContentUtils::ComparePoints(aStartContainer,
|
|
|
|
static_cast<int32_t>(aStartOffset),
|
|
|
|
aEndContainer,
|
|
|
|
static_cast<int32_t>(aEndOffset)) == 1) {
|
2017-07-11 18:09:37 +03:00
|
|
|
DoSetRange(aEndContainer, aEndOffset,
|
|
|
|
aEndContainer, aEndOffset, newEndRoot);
|
2017-05-30 07:18:25 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, set the range as specified.
|
2017-07-11 17:10:42 +03:00
|
|
|
DoSetRange(aStartContainer, aStartOffset,
|
2017-07-11 18:09:37 +03:00
|
|
|
aEndContainer, aEndOffset, 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) {
|
|
|
|
DoSetRange(mStartContainer, mStartOffset,
|
|
|
|
mStartContainer, mStartOffset, mRoot);
|
|
|
|
} else {
|
2017-07-11 16:57:55 +03:00
|
|
|
DoSetRange(mEndContainer, mEndOffset,
|
|
|
|
mEndContainer, mEndOffset, 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
|
|
|
|
2017-07-11 17:52:39 +03:00
|
|
|
int32_t index = container->IndexOf(&aNode);
|
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-07-11 17:52:39 +03:00
|
|
|
DoSetRange(container, index, 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);
|
2013-02-07 16:09:41 +04:00
|
|
|
DoSetRange(&aNode, 0, &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-07-11 16:46:11 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc = mStartContainer->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-07-11 16:46:11 +03:00
|
|
|
nsCOMPtr<nsINode> startContainer = mStartContainer;
|
2017-07-19 16:49:52 +03:00
|
|
|
uint32_t startOffset = mStartOffset;
|
2017-07-11 16:57:55 +03:00
|
|
|
nsCOMPtr<nsINode> endContainer = mEndContainer;
|
2017-07-19 16:49:52 +03:00
|
|
|
uint32_t endOffset = mEndOffset;
|
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
|
|
|
}
|
|
|
|
|
2002-03-18 17:38:11 +03:00
|
|
|
// We delete backwards to avoid iterator problems!
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
iter.Last();
|
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
|
|
|
|
2002-03-18 17:38:11 +03:00
|
|
|
// Before we delete anything, advance the iterator to the
|
|
|
|
// next subtree.
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
iter.Prev();
|
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);
|
|
|
|
nsCOMPtr<nsIDOMNode> clone;
|
2012-02-23 17:23:30 +04:00
|
|
|
rv = charData->CloneNode(false, 1, getter_AddRefs(clone));
|
2009-06-28 23:48:05 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
clone->SetNodeValue(cutValue);
|
2013-07-12 07:29:24 +04:00
|
|
|
nodeToResult = do_QueryInterface(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);
|
|
|
|
nsCOMPtr<nsIDOMNode> clone;
|
|
|
|
rv = charData->CloneNode(false, 1, getter_AddRefs(clone));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
clone->SetNodeValue(cutValue);
|
|
|
|
nodeToResult = do_QueryInterface(clone);
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
nsCOMPtr<nsIDOMNode> clone;
|
|
|
|
rv = charData->CloneNode(false, 1, getter_AddRefs(clone));
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
clone->SetNodeValue(cutValue);
|
|
|
|
nodeToResult = do_QueryInterface(clone);
|
|
|
|
}
|
|
|
|
|
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.
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> prevNode = iter.GetCurrentNode();
|
2008-10-11 23:46:05 +04:00
|
|
|
NS_ENSURE_STATE(prevNode);
|
|
|
|
|
|
|
|
// Get node's and prevNode's common parent. Do this before moving
|
|
|
|
// nodes from original DOM to result fragment.
|
2013-07-12 07:29:24 +04:00
|
|
|
commonAncestor = nsContentUtils::GetCommonAncestor(node, prevNode);
|
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);
|
|
|
|
|
|
|
|
if (farthestAncestor)
|
|
|
|
{
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsINode> n = do_QueryInterface(commonCloneAncestor);
|
|
|
|
rv = PrependChild(n, farthestAncestor);
|
2008-10-11 23:46:05 +04:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
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();
|
2008-10-11 23:46:05 +04:00
|
|
|
rv = closestAncestor ? PrependChild(closestAncestor, nodeToResult)
|
|
|
|
: PrependChild(commonCloneAncestor, nodeToResult);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
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-07-11 16:46:11 +03:00
|
|
|
ourNode = mStartContainer;
|
2006-10-21 05:30:54 +04:00
|
|
|
ourOffset = mStartOffset;
|
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-07-11 16:57:55 +03:00
|
|
|
ourNode = mEndContainer;
|
2006-10-21 05:30:54 +04:00
|
|
|
ourOffset = mEndOffset;
|
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-07-11 16:46:11 +03:00
|
|
|
ourNode = mStartContainer;
|
2006-10-21 05:30:54 +04:00
|
|
|
ourOffset = mStartOffset;
|
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-07-11 16:57:55 +03:00
|
|
|
ourNode = mEndContainer;
|
2006-10-21 05:30:54 +04:00
|
|
|
ourOffset = mEndOffset;
|
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-07-11 16:46:11 +03:00
|
|
|
nsCOMPtr<nsIDocument> doc = mStartContainer->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-07-11 16:57:55 +03:00
|
|
|
(!(node == mEndContainer && mEndOffset == 0) &&
|
2017-07-11 16:46:11 +03:00
|
|
|
!(node == mStartContainer &&
|
2017-07-19 16:49:52 +03:00
|
|
|
mStartOffset == 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-07-11 16:57:55 +03:00
|
|
|
if (node == mEndContainer) {
|
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-07-19 16:49:52 +03:00
|
|
|
if (dataLength > mEndOffset) {
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv = charData->DeleteData(mEndOffset, dataLength - mEndOffset);
|
|
|
|
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-07-11 16:46:11 +03:00
|
|
|
if (node == mStartContainer) {
|
2002-03-18 17:38:11 +03:00
|
|
|
// We don't need any data before mStartOffset, so just
|
|
|
|
// delete it!
|
|
|
|
|
|
|
|
if (mStartOffset > 0)
|
|
|
|
{
|
2013-02-07 16:09:41 +04:00
|
|
|
aRv = charData->DeleteData(0, mStartOffset);
|
|
|
|
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-07-11 16:46:11 +03:00
|
|
|
range->DoSetRange(mStartContainer, mStartOffset,
|
2017-07-11 16:57:55 +03:00
|
|
|
mEndContainer, mEndOffset, 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));
|
2012-04-01 16:02:21 +04:00
|
|
|
nsCOMPtr<nsIDOMNodeList> 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 {
|
2013-07-12 07:29:24 +04:00
|
|
|
aRv = tStartContainer->AsDOMNode()->GetChildNodes(getter_AddRefs(tChildList));
|
2013-02-07 16:09:41 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
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
|
2013-07-12 07:29:24 +04:00
|
|
|
nsCOMPtr<nsIDOMNode> q;
|
|
|
|
aRv = tChildList->Item(tStartOffset, getter_AddRefs(q));
|
|
|
|
referenceNode = do_QueryInterface(q);
|
2013-02-07 16:09:41 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
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 {
|
2017-07-19 16:49:52 +03:00
|
|
|
aRv = tChildList->GetLength(&newOffset);
|
2013-02-07 16:09:41 +04:00
|
|
|
if (aRv.Failed()) {
|
|
|
|
return;
|
|
|
|
}
|
2012-06-19 17:17:37 +04:00
|
|
|
}
|
2012-04-01 16:02:21 +04:00
|
|
|
|
2013-02-07 16:09:41 +04:00
|
|
|
if (aNode.NodeType() == nsIDOMNode::DOCUMENT_FRAGMENT_NODE) {
|
|
|
|
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-07-11 16:57:55 +03:00
|
|
|
if (mStartContainer != mEndContainer) {
|
2017-07-11 16:46:11 +03:00
|
|
|
bool startIsText = mStartContainer->IsNodeOfType(nsINode::eTEXT);
|
2017-07-11 16:57:55 +03:00
|
|
|
bool endIsText = mEndContainer->IsNodeOfType(nsINode::eTEXT);
|
2017-07-11 16:46:11 +03:00
|
|
|
nsINode* startGrandParent = mStartContainer->GetParentNode();
|
2017-07-11 16:57:55 +03:00
|
|
|
nsINode* endGrandParent = mEndContainer->GetParentNode();
|
2013-02-07 16:09:41 +04:00
|
|
|
if (!((startIsText && endIsText &&
|
|
|
|
startGrandParent &&
|
|
|
|
startGrandParent == endGrandParent) ||
|
|
|
|
(startIsText &&
|
|
|
|
startGrandParent &&
|
2017-07-11 16:57:55 +03:00
|
|
|
startGrandParent == mEndContainer) ||
|
2013-02-07 16:09:41 +04:00
|
|
|
(endIsText &&
|
|
|
|
endGrandParent &&
|
2017-07-11 16:46:11 +03:00
|
|
|
endGrandParent == mStartContainer))) {
|
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();
|
2012-03-21 22:06:50 +04:00
|
|
|
if (nodeType == nsIDOMNode::DOCUMENT_NODE ||
|
|
|
|
nodeType == nsIDOMNode::DOCUMENT_TYPE_NODE ||
|
|
|
|
nodeType == nsIDOMNode::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-07-11 16:57:55 +03:00
|
|
|
if (mStartContainer == mEndContainer) {
|
2017-07-11 16:46:11 +03:00
|
|
|
nsCOMPtr<nsIDOMText> textNode = do_QueryInterface(mStartContainer);
|
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-07-11 16:46:11 +03:00
|
|
|
nsCOMPtr<nsIContent> cN = do_QueryInterface(mStartContainer);
|
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
|
1999-05-11 03:29:01 +04:00
|
|
|
if (NS_FAILED(textNode->SubstringData(mStartOffset,mEndOffset-mStartOffset,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-07-11 16:57:55 +03:00
|
|
|
/* complex case: mStartContainer != mEndContainer, 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-07-11 16:46:11 +03:00
|
|
|
if (n == mStartContainer) { // 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);
|
|
|
|
textNode->SubstringData(mStartOffset,strLength-mStartOffset,tempString);
|
|
|
|
aReturn += tempString;
|
2017-07-11 16:57:55 +03:00
|
|
|
} else if (n == mEndContainer) { // only include text before end offset
|
1998-12-15 07:42:29 +03:00
|
|
|
textNode->SubstringData(0,mEndOffset,tempString);
|
|
|
|
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-07-11 16:46:11 +03:00
|
|
|
return nsContentUtils::CreateContextualFragment(mStartContainer, 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-07-11 16:46:11 +03:00
|
|
|
return nsContentUtils::CreateContextualFragment(mStartContainer, 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)) {
|
|
|
|
aR->width = 0;
|
|
|
|
aR->x = point.x;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aClampToEdge) {
|
|
|
|
point = aR->ClampPoint(point);
|
|
|
|
}
|
2009-09-25 01:01:48 +04:00
|
|
|
|
|
|
|
if (aKeepLeft) {
|
|
|
|
aR->width = point.x - aR->x;
|
|
|
|
} else {
|
|
|
|
aR->width = aR->XMost() - point.x;
|
|
|
|
aR->x = point.x;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
|
|
|
nsIPresShell* presShell = aContent->OwnerDoc()->GetShell();
|
|
|
|
if (presShell) {
|
2014-04-03 11:48:51 +04:00
|
|
|
presShell->FrameConstructor()->EnsureFrameForTextNode(
|
2013-09-19 08:23:48 +04:00
|
|
|
static_cast<nsGenericDOMDataNode*>(aContent));
|
2014-07-24 01:06:00 +04:00
|
|
|
|
|
|
|
if (aFlushLayout) {
|
2017-01-05 10:31:56 +03:00
|
|
|
aContent->OwnerDoc()->FlushPendingNotifications(FlushType::Layout);
|
2014-07-24 01:06:00 +04:00
|
|
|
}
|
|
|
|
|
2014-04-03 11:48:51 +04:00
|
|
|
nsIFrame* frame = aContent->GetPrimaryFrame();
|
2017-04-30 18:30:08 +03:00
|
|
|
if (frame && frame->IsTextFrame()) {
|
2013-09-19 08:23:48 +04:00
|
|
|
return static_cast<nsTextFrame*>(frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
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);
|
2009-09-25 01:01:48 +04:00
|
|
|
r.width = 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-07-11 16:46:11 +03:00
|
|
|
if (!mStartContainer) {
|
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-07-11 16:46:11 +03:00
|
|
|
CollectClientRectsAndText(&accumulator, nullptr, this, mStartContainer,
|
2017-07-11 16:57:55 +03:00
|
|
|
mStartOffset, mEndContainer, mEndOffset, 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-07-11 16:46:11 +03:00
|
|
|
if (!mStartContainer) {
|
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-07-11 16:46:11 +03:00
|
|
|
CollectClientRectsAndText(&builder, nullptr, this, mStartContainer,
|
2017-07-11 16:57:55 +03:00
|
|
|
mStartOffset, mEndContainer, mEndOffset, 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-07-11 16:46:11 +03:00
|
|
|
if (!mStartContainer) {
|
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-07-11 16:57:55 +03:00
|
|
|
mStartContainer, mStartOffset, mEndContainer, mEndOffset, true, true);
|
2016-11-02 23:31:06 +03:00
|
|
|
}
|
|
|
|
|
2011-06-16 10:31:36 +04:00
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRange::GetUsedFontFaces(nsIDOMFontFaceList** aResult)
|
|
|
|
{
|
2012-07-30 18:20:58 +04:00
|
|
|
*aResult = nullptr;
|
2011-06-16 10:31:36 +04:00
|
|
|
|
2017-07-11 16:46:11 +03:00
|
|
|
NS_ENSURE_TRUE(mStartContainer, NS_ERROR_UNEXPECTED);
|
2011-06-16 10:31:36 +04:00
|
|
|
|
2017-07-11 16:46:11 +03:00
|
|
|
nsCOMPtr<nsINode> startContainer = do_QueryInterface(mStartContainer);
|
2017-07-11 16:57:55 +03:00
|
|
|
nsCOMPtr<nsINode> endContainer = do_QueryInterface(mEndContainer);
|
2011-06-16 10:31:36 +04:00
|
|
|
|
|
|
|
// Flush out layout so our frames are up to date.
|
2017-07-11 16:46:11 +03:00
|
|
|
nsIDocument* doc = mStartContainer->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-07-11 16:46:11 +03:00
|
|
|
NS_ENSURE_TRUE(mStartContainer->IsInUncomposedDoc(), NS_ERROR_UNEXPECTED);
|
2011-06-16 10:31:36 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<nsFontFaceList> fontFaceList = new nsFontFaceList();
|
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 ?
|
2011-06-16 10:31:36 +04:00
|
|
|
mEndOffset : content->GetText()->GetLength();
|
|
|
|
nsLayoutUtils::GetFontFacesForText(frame, mStartOffset, offset,
|
2011-10-17 18:59:28 +04:00
|
|
|
true, fontFaceList);
|
2011-06-16 10:31:36 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (node == endContainer) {
|
|
|
|
nsLayoutUtils::GetFontFacesForText(frame, 0, mEndOffset,
|
2011-10-17 18:59:28 +04:00
|
|
|
true, fontFaceList);
|
2011-06-16 10:31:36 +04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsLayoutUtils::GetFontFacesForFrames(frame, fontFaceList);
|
|
|
|
}
|
|
|
|
|
|
|
|
fontFaceList.forget(aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2011-12-24 17:26:03 +04:00
|
|
|
|
|
|
|
nsINode*
|
|
|
|
nsRange::GetRegisteredCommonAncestor()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(IsInSelection(),
|
|
|
|
"GetRegisteredCommonAncestor only valid for range in selection");
|
2017-07-11 16:46:11 +03:00
|
|
|
nsINode* ancestor = GetNextRangeCommonAncestor(mStartContainer);
|
2011-12-24 17:26:03 +04:00
|
|
|
while (ancestor) {
|
2017-07-22 08:29:07 +03:00
|
|
|
nsTHashtable<nsPtrHashKey<nsRange>>* ranges =
|
|
|
|
ancestor->GetExistingCommonAncestorRanges();
|
2011-12-24 17:26:03 +04:00
|
|
|
if (ranges->GetEntry(this)) {
|
|
|
|
break;
|
|
|
|
}
|
2012-10-09 16:31:24 +04:00
|
|
|
ancestor = GetNextRangeCommonAncestor(ancestor->GetParentNode());
|
2011-12-24 17:26:03 +04:00
|
|
|
}
|
|
|
|
NS_ASSERTION(ancestor, "can't find common ancestor for selected range");
|
|
|
|
return ancestor;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */ bool nsRange::AutoInvalidateSelection::mIsNested;
|
|
|
|
|
|
|
|
nsRange::AutoInvalidateSelection::~AutoInvalidateSelection()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mWasInSelection == mRange->IsInSelection(),
|
|
|
|
"Range got unselected in AutoInvalidateSelection block");
|
|
|
|
if (!mCommonAncestor) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
mIsNested = false;
|
|
|
|
::InvalidateAllFrames(mCommonAncestor);
|
|
|
|
nsINode* commonAncestor = mRange->GetRegisteredCommonAncestor();
|
2017-04-11 06:52:00 +03:00
|
|
|
if (commonAncestor && commonAncestor != mCommonAncestor) {
|
2011-12-24 17:26:03 +04:00
|
|
|
::InvalidateAllFrames(commonAncestor);
|
|
|
|
}
|
|
|
|
}
|
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-07-11 16:57:55 +03:00
|
|
|
MOZ_ASSERT(mEndContainer);
|
2017-07-11 16:46:11 +03:00
|
|
|
MOZ_ASSERT(mStartContainer);
|
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-07-11 18:08:37 +03:00
|
|
|
nsINode* endContainer = range->mEndContainer;
|
2014-09-10 21:07:36 +04:00
|
|
|
int32_t endOffset = range->mEndOffset;
|
|
|
|
|
|
|
|
// 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) {
|
2017-07-11 18:02:14 +03:00
|
|
|
startOffset = parent->IndexOf(startContainer);
|
|
|
|
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
|
|
|
|
AppendTransformedText(InnerTextAccumulator& aResult,
|
|
|
|
nsGenericDOMDataNode* aTextNode,
|
2017-07-19 16:49:52 +03:00
|
|
|
uint32_t aStart, uint32_t aEnd)
|
2015-10-30 08:37:03 +03:00
|
|
|
{
|
|
|
|
nsIFrame* frame = aTextNode->GetPrimaryFrame();
|
|
|
|
if (!IsVisibleAndNotInReplacedElement(frame)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
nsIFrame::RenderedText text = frame->GetRenderedText(aStart, aEnd);
|
|
|
|
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,
|
2017-07-11 17:10:42 +03:00
|
|
|
nsIContent* aStartContainer, uint32_t aStartOffset,
|
2017-07-11 18:09:37 +03:00
|
|
|
nsIContent* aEndContainer, uint32_t aEndOffset)
|
2015-10-30 08:37:03 +03:00
|
|
|
{
|
|
|
|
InnerTextAccumulator result(aValue);
|
2017-07-11 17:10:42 +03:00
|
|
|
nsIContent* currentNode = aStartContainer;
|
2015-10-30 08:37:03 +03:00
|
|
|
TreeTraversalState currentState = AFTER_NODE;
|
2017-07-11 17:10:42 +03:00
|
|
|
if (aStartContainer->IsNodeOfType(nsINode::eTEXT)) {
|
|
|
|
auto t = static_cast<nsGenericDOMDataNode*>(aStartContainer);
|
2017-07-11 18:09:37 +03:00
|
|
|
if (aStartContainer == aEndContainer) {
|
2015-10-30 08:37:03 +03:00
|
|
|
AppendTransformedText(result, t, aStartOffset, aEndOffset);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
AppendTransformedText(result, t, aStartOffset, t->TextLength());
|
|
|
|
} else {
|
2017-07-11 17:10:42 +03:00
|
|
|
if (uint32_t(aStartOffset) < aStartContainer->GetChildCount()) {
|
|
|
|
currentNode = aStartContainer->GetChildAt(aStartOffset);
|
2015-10-30 08:37:03 +03:00
|
|
|
currentState = AT_NODE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-11 18:09:37 +03:00
|
|
|
nsIContent* endNode = aEndContainer;
|
2015-10-30 08:37:03 +03:00
|
|
|
TreeTraversalState endState = AFTER_NODE;
|
2017-07-11 18:09:37 +03:00
|
|
|
if (aEndContainer->IsNodeOfType(nsINode::eTEXT)) {
|
2015-10-30 08:37:03 +03:00
|
|
|
endState = AT_NODE;
|
|
|
|
} else {
|
2017-07-19 16:49:52 +03:00
|
|
|
if (aEndOffset < aEndContainer->GetChildCount()) {
|
2017-07-11 18:09:37 +03:00
|
|
|
endNode = aEndContainer->GetChildAt(aEndOffset);
|
2015-10-30 08:37:03 +03:00
|
|
|
endState = AT_NODE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-11 18:09:37 +03:00
|
|
|
if (aEndContainer->IsNodeOfType(nsINode::eTEXT)) {
|
|
|
|
nsGenericDOMDataNode* t = static_cast<nsGenericDOMDataNode*>(aEndContainer);
|
2015-10-30 08:37:03 +03:00
|
|
|
AppendTransformedText(result, t, 0, aEndOffset);
|
|
|
|
}
|
|
|
|
// Do not flush trailing line breaks! Required breaks at the end of the text
|
|
|
|
// are suppressed.
|
|
|
|
}
|