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-12-15 02:16:31 +03:00
|
|
|
|
2019-01-11 04:47:15 +03:00
|
|
|
#include "ContentIterator.h"
|
|
|
|
|
2016-06-23 12:58:22 +03:00
|
|
|
#include "mozilla/DebugOnly.h"
|
2019-01-11 04:47:15 +03:00
|
|
|
#include "mozilla/RangeBoundary.h"
|
2019-06-28 10:48:07 +03:00
|
|
|
#include "mozilla/RangeUtils.h"
|
2019-01-11 04:47:15 +03:00
|
|
|
|
2002-06-26 00:03:06 +04:00
|
|
|
#include "nsContentUtils.h"
|
2017-09-05 13:19:06 +03:00
|
|
|
#include "nsElementTable.h"
|
2019-01-11 04:47:15 +03:00
|
|
|
#include "nsIContent.h"
|
|
|
|
#include "nsRange.h"
|
|
|
|
|
|
|
|
namespace mozilla {
|
1999-07-03 10:19:11 +04:00
|
|
|
|
2002-11-21 18:07:49 +03:00
|
|
|
///////////////////////////////////////////////////////////////////////////
|
2008-10-15 13:40:28 +04:00
|
|
|
// NodeIsInTraversalRange: returns true if content is visited during
|
2002-11-21 18:07:49 +03:00
|
|
|
// the traversal of the range in the specified mode.
|
|
|
|
//
|
2012-06-14 10:47:03 +04:00
|
|
|
static bool NodeIsInTraversalRange(nsINode* aNode, bool aIsPreMode,
|
2017-10-02 17:58:31 +03:00
|
|
|
const RawRangeBoundary& aStart,
|
|
|
|
const RawRangeBoundary& aEnd) {
|
|
|
|
if (NS_WARN_IF(!aStart.IsSet()) || NS_WARN_IF(!aEnd.IsSet()) ||
|
2017-07-11 17:10:42 +03:00
|
|
|
NS_WARN_IF(!aNode)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2015-10-20 19:45:03 +03:00
|
|
|
// If a leaf node contains an end point of the traversal range, it is
|
2012-06-14 10:47:03 +04:00
|
|
|
// always in the traversal range.
|
2017-10-02 17:58:31 +03:00
|
|
|
if (aNode == aStart.Container() || aNode == aEnd.Container()) {
|
2018-04-15 13:46:24 +03:00
|
|
|
if (aNode->IsCharacterData()) {
|
2015-10-20 19:45:03 +03:00
|
|
|
return true; // text node or something
|
|
|
|
}
|
|
|
|
if (!aNode->HasChildren()) {
|
2017-10-02 17:58:31 +03:00
|
|
|
MOZ_ASSERT(
|
|
|
|
aNode != aStart.Container() || aStart.IsStartOfContainer(),
|
|
|
|
"aStart.Container() doesn't have children and not a data node, "
|
|
|
|
"aStart should be at the beginning of its container");
|
|
|
|
MOZ_ASSERT(aNode != aEnd.Container() || aEnd.IsStartOfContainer(),
|
|
|
|
"aEnd.Container() doesn't have children and not a data node, "
|
|
|
|
"aEnd should be at the beginning of its container");
|
2015-10-20 19:45:03 +03:00
|
|
|
return true;
|
|
|
|
}
|
2002-12-19 22:36:18 +03:00
|
|
|
}
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* parent = aNode->GetParentNode();
|
2012-06-14 10:47:03 +04:00
|
|
|
if (!parent) {
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (!aIsPreMode) {
|
2017-10-02 17:58:31 +03:00
|
|
|
// aNode should always be content, as we have a parent, but let's just be
|
|
|
|
// extra careful and check.
|
|
|
|
nsIContent* content =
|
|
|
|
NS_WARN_IF(!aNode->IsContent()) ? nullptr : aNode->AsContent();
|
Bug 1383242 - Properly compare node to traversal range under different modes; r=smaug
When the node borders one of the range bounds, `NodeIsInTraversalRange`
should return different results depending on whether it's in pre mode or
not.
> <div><br></div>
> \__/
In this pre mode example, the node <br> is within the range, and the
node position (which is at the start of the node in pre mode) and the
start bound are both (<div>, 0). Therefore, it shows the start bound
should be inclusive in pre mode.
> <div><br></div>
> \___/
In this pre mode example, the node <br> is outside of the range, yet the
node position and the end bound are both (<div>, 0). Therefore, it shows
the end bound should be exclusive in pre mode.
> <div><br></div>
> \____/
in this post mode example, the node <br> is outside of the range, yet
the node position (which is at the end of the node in post mode) and the
start bound are both (<div>, 1). Therefore, it shows the start bound
should be exclusive in post mode.
> <div><br></div>
> \__/
In this post mode example, the node <br> is within the range, and the
node position and the end bound are both (<div>, 1). Therefore, it shows
the end bound should be inclusive in post mode.
In summary, the correct pre mode bound check is `start <= node < end`,
and the correct post mode bound check is `start < node <= end`. This
patch fixes `NodeIsInTraversalRange` to have the correct bounds check.
MozReview-Commit-ID: IjJN1ua6jQ9
--HG--
extra : rebase_source : 5d976071820dec15492470713258339ed36493da
2017-08-02 20:48:09 +03:00
|
|
|
// Post mode: start < node <= end.
|
2017-10-02 17:58:31 +03:00
|
|
|
RawRangeBoundary afterNode(parent, content);
|
|
|
|
return nsContentUtils::ComparePoints(aStart, afterNode) < 0 &&
|
|
|
|
nsContentUtils::ComparePoints(aEnd, afterNode) >= 0;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
|
Bug 1383242 - Properly compare node to traversal range under different modes; r=smaug
When the node borders one of the range bounds, `NodeIsInTraversalRange`
should return different results depending on whether it's in pre mode or
not.
> <div><br></div>
> \__/
In this pre mode example, the node <br> is within the range, and the
node position (which is at the start of the node in pre mode) and the
start bound are both (<div>, 0). Therefore, it shows the start bound
should be inclusive in pre mode.
> <div><br></div>
> \___/
In this pre mode example, the node <br> is outside of the range, yet the
node position and the end bound are both (<div>, 0). Therefore, it shows
the end bound should be exclusive in pre mode.
> <div><br></div>
> \____/
in this post mode example, the node <br> is outside of the range, yet
the node position (which is at the end of the node in post mode) and the
start bound are both (<div>, 1). Therefore, it shows the start bound
should be exclusive in post mode.
> <div><br></div>
> \__/
In this post mode example, the node <br> is within the range, and the
node position and the end bound are both (<div>, 1). Therefore, it shows
the end bound should be inclusive in post mode.
In summary, the correct pre mode bound check is `start <= node < end`,
and the correct post mode bound check is `start < node <= end`. This
patch fixes `NodeIsInTraversalRange` to have the correct bounds check.
MozReview-Commit-ID: IjJN1ua6jQ9
--HG--
extra : rebase_source : 5d976071820dec15492470713258339ed36493da
2017-08-02 20:48:09 +03:00
|
|
|
// Pre mode: start <= node < end.
|
2017-10-02 17:58:31 +03:00
|
|
|
RawRangeBoundary beforeNode(parent, aNode->GetPreviousSibling());
|
|
|
|
return nsContentUtils::ComparePoints(aStart, beforeNode) <= 0 &&
|
|
|
|
nsContentUtils::ComparePoints(aEnd, beforeNode) > 0;
|
2002-11-21 18:07:49 +03:00
|
|
|
}
|
1999-07-03 10:19:11 +04:00
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
ContentIteratorBase::ContentIteratorBase(bool aPre)
|
|
|
|
: mIsDone(false), mPre(aPre) {}
|
1999-02-12 08:28:12 +03:00
|
|
|
|
|
|
|
/******************************************************
|
|
|
|
* Init routines
|
|
|
|
******************************************************/
|
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
nsresult ContentIteratorBase::Init(nsINode* aRoot) {
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aRoot)) {
|
2012-06-14 10:47:03 +04:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
}
|
2008-10-15 13:40:28 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = false;
|
2012-06-14 10:47:03 +04:00
|
|
|
|
|
|
|
if (mPre) {
|
2004-01-24 03:46:17 +03:00
|
|
|
mFirst = aRoot;
|
2012-06-14 10:47:03 +04:00
|
|
|
mLast = GetDeepLastChild(aRoot);
|
2016-09-02 10:12:24 +03:00
|
|
|
NS_WARNING_ASSERTION(mLast, "GetDeepLastChild returned null");
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2012-06-14 10:47:03 +04:00
|
|
|
mFirst = GetDeepFirstChild(aRoot);
|
2016-09-02 10:12:24 +03:00
|
|
|
NS_WARNING_ASSERTION(mFirst, "GetDeepFirstChild returned null");
|
2004-01-24 03:46:17 +03:00
|
|
|
mLast = aRoot;
|
2002-11-21 18:07:49 +03:00
|
|
|
}
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
mCommonParent = aRoot;
|
1998-12-15 02:16:31 +03:00
|
|
|
mCurNode = mFirst;
|
1999-02-12 08:28:12 +03:00
|
|
|
return NS_OK;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
nsresult ContentIteratorBase::Init(nsRange* aRange) {
|
2017-06-26 11:26:27 +03:00
|
|
|
mIsDone = false;
|
|
|
|
|
2018-05-17 19:01:38 +03:00
|
|
|
if (NS_WARN_IF(!aRange)) {
|
2015-11-10 05:49:04 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
2017-06-26 11:26:27 +03:00
|
|
|
|
2018-05-17 19:01:38 +03:00
|
|
|
if (NS_WARN_IF(!aRange->IsPositioned())) {
|
2017-06-26 11:26:27 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2018-05-17 19:01:38 +03:00
|
|
|
return InitInternal(aRange->StartRef().AsRaw(), aRange->EndRef().AsRaw());
|
2017-06-26 11:26:27 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
nsresult ContentIteratorBase::Init(nsINode* aStartContainer,
|
|
|
|
uint32_t aStartOffset,
|
|
|
|
nsINode* aEndContainer,
|
|
|
|
uint32_t aEndOffset) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = false;
|
1999-10-09 03:34:07 +04:00
|
|
|
|
2019-06-28 10:48:07 +03:00
|
|
|
if (NS_WARN_IF(!RangeUtils::IsValidPoints(aStartContainer, aStartOffset,
|
|
|
|
aEndContainer, aEndOffset))) {
|
2017-06-26 11:26:27 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
2015-11-10 05:49:04 +03:00
|
|
|
}
|
1999-03-01 11:17:18 +03:00
|
|
|
|
2017-10-02 17:58:31 +03:00
|
|
|
return InitInternal(RawRangeBoundary(aStartContainer, aStartOffset),
|
|
|
|
RawRangeBoundary(aEndContainer, aEndOffset));
|
|
|
|
}
|
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
nsresult ContentIteratorBase::Init(const RawRangeBoundary& aStart,
|
|
|
|
const RawRangeBoundary& aEnd) {
|
2017-10-02 17:58:31 +03:00
|
|
|
mIsDone = false;
|
|
|
|
|
2019-06-28 10:48:07 +03:00
|
|
|
if (NS_WARN_IF(!RangeUtils::IsValidPoints(aStart, aEnd))) {
|
2017-10-02 17:58:31 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return InitInternal(aStart, aEnd);
|
2017-06-26 11:26:27 +03:00
|
|
|
}
|
2006-10-21 05:30:54 +04:00
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
nsresult ContentIteratorBase::InitInternal(const RawRangeBoundary& aStart,
|
|
|
|
const RawRangeBoundary& aEnd) {
|
2017-06-26 11:26:27 +03:00
|
|
|
// get common content parent
|
|
|
|
mCommonParent =
|
2017-10-02 17:58:31 +03:00
|
|
|
nsContentUtils::GetCommonAncestor(aStart.Container(), aEnd.Container());
|
2017-06-26 11:26:27 +03:00
|
|
|
if (NS_WARN_IF(!mCommonParent)) {
|
2015-11-10 05:49:04 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
1999-05-18 15:11:14 +04:00
|
|
|
|
2018-04-15 13:46:24 +03:00
|
|
|
bool startIsData = aStart.Container()->IsCharacterData();
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2017-10-02 17:58:31 +03:00
|
|
|
// Check to see if we have a collapsed range, if so, there is nothing to
|
|
|
|
// iterate over.
|
|
|
|
//
|
|
|
|
// XXX: CharacterDataNodes (text nodes) are currently an exception, since
|
|
|
|
// we always want to be able to iterate text nodes at the end points
|
|
|
|
// of a range.
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2017-10-02 17:58:31 +03:00
|
|
|
if (!startIsData && aStart == aEnd) {
|
|
|
|
MakeEmpty();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2017-10-02 17:58:31 +03:00
|
|
|
// Handle ranges within a single character data node.
|
|
|
|
if (startIsData && aStart.Container() == aEnd.Container()) {
|
|
|
|
mFirst = aStart.Container()->AsContent();
|
|
|
|
mLast = mFirst;
|
|
|
|
mCurNode = mFirst;
|
|
|
|
|
|
|
|
return NS_OK;
|
1999-05-18 15:11:14 +04:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
|
2002-11-21 18:07:49 +03:00
|
|
|
// Find first node in range.
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIContent* cChild = nullptr;
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2017-10-02 17:58:31 +03:00
|
|
|
// Try to get the child at our starting point. This might return null if
|
|
|
|
// aStart is immediately after the last node in aStart.Container().
|
|
|
|
if (!startIsData) {
|
|
|
|
cChild = aStart.GetChildAtOffset();
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (!cChild) {
|
Bug 1319660 - Fix possible crash when editing contentEditable; r=esawin r=masayuki r=smaug
Bug 1319660 - 1. Don't take shortcut if old replacement ranges don't match; r=esawin
The block at [1] is a shortcut we take when we reconcile Java text
changes with Gecko text changes. However, we only checked that the new
ranges are the same, i.e. that the new Gecko text is the same as the new
Java text. We should also be checking that the old ranges are the same,
i.e. that the replaced Gecko text is the same as the replaced Java text.
[1] https://dxr.mozilla.org/mozilla-central/rev/bbbd2f7539f224a482cc6d2dd10e6a5f31c8baf3/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoEditable.java#1233
Bug 1319660 - 2. Use previous node instead of sibling when adjusting last node; r=masayuki r=smaug
nsContentIterator in pre mode adjusts its last node if the node is a
childless node like <br>. However, right now it's using GetPrevSibling,
which can lead to error in some edge cases such as:
<p></p><div><br></div>
In this case, if the last node is <br> with offset 0, GetPrevSibling
will return <p> because <p> is <br>'s parent's previous sibling, and the
last node will be set to <p>. However, the correct last node in this
case is <div>, because <br> with offset 0 refers to the position to the
left of <br>, which is <div> with offset 0. In this case, PrevNode
returns the correct <div> value, so we should set the last node to the
result of PrevNode.
For the first node, for a childless node in pre mode, GetNextSibling and
NextNode are the same, so there is no bug in this case. Nevertheless,
this patch changes the call to NextNode to be consistent with calling
PrevNode for the last node.
Bug 1319660 - 3. Add test for correctly adjusting last node in content iterator; r=masayuki
Add a test for the previous patch that makes sure querying selected text
in an edge case works correctly.
Bug 1319660 - 4. Add test for start node regression; r=me
Add a new test case for the NextNode() regression. r=me for trivial
test-only patch.
Bug 1319660 - 5. Restore GetNextSibling call for first node of pre-content-iterator; r=smaug
The last patch changed the `GetNextSibling()` call to `NextNode()`
because I assumed they're equivalent in this case. That turned out to
not be the case because we can reach this line even if the node has
children -- the index just has to be after the last child. So this patch
restores the `GetNextSibling` call to restore the correct behavior.
I also added some comment to clarify that we can reach this line due to
one of two conditions: 1) the node has no children; 2) the node has
children but the index is after the last child.
This patch also replaces the `HasChildren()` check when setting
`cChild`. If the index is after the last child (i.e. index ==
childCount), `GetChildAt()` fails and we erroneously log an assertion
warning, even though the input was valid. The new check handles all
cases whether start node has children or not.
2017-01-23 22:35:04 +03:00
|
|
|
// No children (possibly a <br> or text node), or index is after last child.
|
2012-06-14 10:47:03 +04:00
|
|
|
|
|
|
|
if (mPre) {
|
2002-11-21 18:07:49 +03:00
|
|
|
// XXX: In the future, if start offset is after the last
|
|
|
|
// character in the cdata node, should we set mFirst to
|
|
|
|
// the next sibling?
|
|
|
|
|
Bug 1319660 - Fix possible crash when editing contentEditable; r=esawin r=masayuki r=smaug
Bug 1319660 - 1. Don't take shortcut if old replacement ranges don't match; r=esawin
The block at [1] is a shortcut we take when we reconcile Java text
changes with Gecko text changes. However, we only checked that the new
ranges are the same, i.e. that the new Gecko text is the same as the new
Java text. We should also be checking that the old ranges are the same,
i.e. that the replaced Gecko text is the same as the replaced Java text.
[1] https://dxr.mozilla.org/mozilla-central/rev/bbbd2f7539f224a482cc6d2dd10e6a5f31c8baf3/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoEditable.java#1233
Bug 1319660 - 2. Use previous node instead of sibling when adjusting last node; r=masayuki r=smaug
nsContentIterator in pre mode adjusts its last node if the node is a
childless node like <br>. However, right now it's using GetPrevSibling,
which can lead to error in some edge cases such as:
<p></p><div><br></div>
In this case, if the last node is <br> with offset 0, GetPrevSibling
will return <p> because <p> is <br>'s parent's previous sibling, and the
last node will be set to <p>. However, the correct last node in this
case is <div>, because <br> with offset 0 refers to the position to the
left of <br>, which is <div> with offset 0. In this case, PrevNode
returns the correct <div> value, so we should set the last node to the
result of PrevNode.
For the first node, for a childless node in pre mode, GetNextSibling and
NextNode are the same, so there is no bug in this case. Nevertheless,
this patch changes the call to NextNode to be consistent with calling
PrevNode for the last node.
Bug 1319660 - 3. Add test for correctly adjusting last node in content iterator; r=masayuki
Add a test for the previous patch that makes sure querying selected text
in an edge case works correctly.
Bug 1319660 - 4. Add test for start node regression; r=me
Add a new test case for the NextNode() regression. r=me for trivial
test-only patch.
Bug 1319660 - 5. Restore GetNextSibling call for first node of pre-content-iterator; r=smaug
The last patch changed the `GetNextSibling()` call to `NextNode()`
because I assumed they're equivalent in this case. That turned out to
not be the case because we can reach this line even if the node has
children -- the index just has to be after the last child. So this patch
restores the `GetNextSibling` call to restore the correct behavior.
I also added some comment to clarify that we can reach this line due to
one of two conditions: 1) the node has no children; 2) the node has
children but the index is after the last child.
This patch also replaces the `HasChildren()` check when setting
`cChild`. If the index is after the last child (i.e. index ==
childCount), `GetChildAt()` fails and we erroneously log an assertion
warning, even though the input was valid. The new check handles all
cases whether start node has children or not.
2017-01-23 22:35:04 +03:00
|
|
|
// Normally we would skip the start node because the start node is outside
|
2017-08-16 16:13:47 +03:00
|
|
|
// of the range in pre mode. However, if aStartOffset == 0, and the node
|
|
|
|
// is a non-container node (e.g. <br>), we don't skip the node in this
|
|
|
|
// case in order to address bug 1215798.
|
Bug 1351170 - 1. Correctly calculate start offset for non-text nodes; r=masayuki
When the start node is a non-container node (i.e. <br>), and the start
offset is 0, we should not include a newline character for the node. For
example, for this range,
> <br/>hello
> \___/
the start node/offset is (<br/>, 0) and end node/offset is ("hello", 1).
The calculated range offset should be 0, and the range length should be
2: 1 for the <br/> newline character plus 1 for "h".
The patch also ensures this behavior for pre-mode nsContentIterator, for
both start and end node adjustments. For start nodes, we include any
non-container nodes with offset 0 in the range. For end node, we exclude
any non-container nodes with offset 0 from the range.
MozReview-Commit-ID: Lt2tCLbapq7
--HG--
extra : rebase_source : 7d86b6cf04581f1cd71fa85f8c8586541b3a84e9
2017-07-19 21:29:59 +03:00
|
|
|
bool startIsContainer = true;
|
2017-10-02 17:58:31 +03:00
|
|
|
if (aStart.Container()->IsHTMLElement()) {
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* name = aStart.Container()->NodeInfo()->NameAtom();
|
2017-09-05 13:19:06 +03:00
|
|
|
startIsContainer =
|
|
|
|
nsHTMLElement::IsContainer(nsHTMLTags::AtomTagToId(name));
|
Bug 1351170 - 1. Correctly calculate start offset for non-text nodes; r=masayuki
When the start node is a non-container node (i.e. <br>), and the start
offset is 0, we should not include a newline character for the node. For
example, for this range,
> <br/>hello
> \___/
the start node/offset is (<br/>, 0) and end node/offset is ("hello", 1).
The calculated range offset should be 0, and the range length should be
2: 1 for the <br/> newline character plus 1 for "h".
The patch also ensures this behavior for pre-mode nsContentIterator, for
both start and end node adjustments. For start nodes, we include any
non-container nodes with offset 0 in the range. For end node, we exclude
any non-container nodes with offset 0 from the range.
MozReview-Commit-ID: Lt2tCLbapq7
--HG--
extra : rebase_source : 7d86b6cf04581f1cd71fa85f8c8586541b3a84e9
2017-07-19 21:29:59 +03:00
|
|
|
}
|
2017-10-02 17:58:31 +03:00
|
|
|
if (!startIsData && (startIsContainer || !aStart.IsStartOfContainer())) {
|
|
|
|
mFirst = GetNextSibling(aStart.Container());
|
2017-01-18 18:55:53 +03:00
|
|
|
NS_WARNING_ASSERTION(mFirst, "GetNextSibling returned null");
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// Does mFirst node really intersect the range? The range could be
|
|
|
|
// 'degenerate', i.e., not collapsed but still contain no content.
|
2015-11-10 05:49:04 +03:00
|
|
|
if (mFirst &&
|
2017-10-02 17:58:31 +03:00
|
|
|
NS_WARN_IF(!NodeIsInTraversalRange(mFirst, mPre, aStart, aEnd))) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mFirst = nullptr;
|
2006-11-02 10:41:45 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2017-10-02 17:58:31 +03:00
|
|
|
mFirst = aStart.Container()->AsContent();
|
2006-11-02 10:41:45 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2006-11-02 10:41:45 +03:00
|
|
|
// post-order
|
2017-10-02 17:58:31 +03:00
|
|
|
if (NS_WARN_IF(!aStart.Container()->IsContent())) {
|
2006-11-02 10:41:45 +03:00
|
|
|
// What else can we do?
|
2012-07-30 18:20:58 +04:00
|
|
|
mFirst = nullptr;
|
2015-11-10 05:49:04 +03:00
|
|
|
} else {
|
2017-10-02 17:58:31 +03:00
|
|
|
mFirst = aStart.Container()->AsContent();
|
2002-11-21 18:07:49 +03:00
|
|
|
}
|
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
if (mPre) {
|
2002-11-21 18:07:49 +03:00
|
|
|
mFirst = cChild;
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
// post-order
|
2012-06-14 10:47:03 +04:00
|
|
|
mFirst = GetDeepFirstChild(cChild);
|
2016-09-02 10:12:24 +03:00
|
|
|
NS_WARNING_ASSERTION(mFirst, "GetDeepFirstChild returned null");
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// Does mFirst node really intersect the range? The range could be
|
|
|
|
// 'degenerate', i.e., not collapsed but still contain no content.
|
|
|
|
|
2017-10-02 17:58:31 +03:00
|
|
|
if (mFirst && !NodeIsInTraversalRange(mFirst, mPre, aStart, aEnd)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mFirst = nullptr;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
|
|
|
}
|
1999-02-14 12:15:13 +03:00
|
|
|
|
2002-11-21 18:07:49 +03:00
|
|
|
// Find last node in range.
|
|
|
|
|
2018-04-15 13:46:24 +03:00
|
|
|
bool endIsData = aEnd.Container()->IsCharacterData();
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2017-10-02 17:58:31 +03:00
|
|
|
if (endIsData || !aEnd.Container()->HasChildren() ||
|
|
|
|
aEnd.IsStartOfContainer()) {
|
2006-11-02 10:41:45 +03:00
|
|
|
if (mPre) {
|
2017-10-02 17:58:31 +03:00
|
|
|
if (NS_WARN_IF(!aEnd.Container()->IsContent())) {
|
2015-11-10 05:49:04 +03:00
|
|
|
// Not much else to do here...
|
|
|
|
mLast = nullptr;
|
|
|
|
} else {
|
Bug 1351170 - 1. Correctly calculate start offset for non-text nodes; r=masayuki
When the start node is a non-container node (i.e. <br>), and the start
offset is 0, we should not include a newline character for the node. For
example, for this range,
> <br/>hello
> \___/
the start node/offset is (<br/>, 0) and end node/offset is ("hello", 1).
The calculated range offset should be 0, and the range length should be
2: 1 for the <br/> newline character plus 1 for "h".
The patch also ensures this behavior for pre-mode nsContentIterator, for
both start and end node adjustments. For start nodes, we include any
non-container nodes with offset 0 in the range. For end node, we exclude
any non-container nodes with offset 0 from the range.
MozReview-Commit-ID: Lt2tCLbapq7
--HG--
extra : rebase_source : 7d86b6cf04581f1cd71fa85f8c8586541b3a84e9
2017-07-19 21:29:59 +03:00
|
|
|
// If the end node is a non-container element and the end offset is 0,
|
2015-10-21 17:26:20 +03:00
|
|
|
// the last element should be the previous node (i.e., shouldn't
|
|
|
|
// include the end node in the range).
|
Bug 1351170 - 1. Correctly calculate start offset for non-text nodes; r=masayuki
When the start node is a non-container node (i.e. <br>), and the start
offset is 0, we should not include a newline character for the node. For
example, for this range,
> <br/>hello
> \___/
the start node/offset is (<br/>, 0) and end node/offset is ("hello", 1).
The calculated range offset should be 0, and the range length should be
2: 1 for the <br/> newline character plus 1 for "h".
The patch also ensures this behavior for pre-mode nsContentIterator, for
both start and end node adjustments. For start nodes, we include any
non-container nodes with offset 0 in the range. For end node, we exclude
any non-container nodes with offset 0 from the range.
MozReview-Commit-ID: Lt2tCLbapq7
--HG--
extra : rebase_source : 7d86b6cf04581f1cd71fa85f8c8586541b3a84e9
2017-07-19 21:29:59 +03:00
|
|
|
bool endIsContainer = true;
|
2017-10-02 17:58:31 +03:00
|
|
|
if (aEnd.Container()->IsHTMLElement()) {
|
2017-10-03 01:05:19 +03:00
|
|
|
nsAtom* name = aEnd.Container()->NodeInfo()->NameAtom();
|
2017-09-05 13:19:06 +03:00
|
|
|
endIsContainer =
|
|
|
|
nsHTMLElement::IsContainer(nsHTMLTags::AtomTagToId(name));
|
Bug 1351170 - 1. Correctly calculate start offset for non-text nodes; r=masayuki
When the start node is a non-container node (i.e. <br>), and the start
offset is 0, we should not include a newline character for the node. For
example, for this range,
> <br/>hello
> \___/
the start node/offset is (<br/>, 0) and end node/offset is ("hello", 1).
The calculated range offset should be 0, and the range length should be
2: 1 for the <br/> newline character plus 1 for "h".
The patch also ensures this behavior for pre-mode nsContentIterator, for
both start and end node adjustments. For start nodes, we include any
non-container nodes with offset 0 in the range. For end node, we exclude
any non-container nodes with offset 0 from the range.
MozReview-Commit-ID: Lt2tCLbapq7
--HG--
extra : rebase_source : 7d86b6cf04581f1cd71fa85f8c8586541b3a84e9
2017-07-19 21:29:59 +03:00
|
|
|
}
|
2017-10-02 17:58:31 +03:00
|
|
|
if (!endIsData && !endIsContainer && aEnd.IsStartOfContainer()) {
|
|
|
|
mLast = PrevNode(aEnd.Container());
|
Bug 1319660 - Fix possible crash when editing contentEditable; r=esawin r=masayuki r=smaug
Bug 1319660 - 1. Don't take shortcut if old replacement ranges don't match; r=esawin
The block at [1] is a shortcut we take when we reconcile Java text
changes with Gecko text changes. However, we only checked that the new
ranges are the same, i.e. that the new Gecko text is the same as the new
Java text. We should also be checking that the old ranges are the same,
i.e. that the replaced Gecko text is the same as the replaced Java text.
[1] https://dxr.mozilla.org/mozilla-central/rev/bbbd2f7539f224a482cc6d2dd10e6a5f31c8baf3/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoEditable.java#1233
Bug 1319660 - 2. Use previous node instead of sibling when adjusting last node; r=masayuki r=smaug
nsContentIterator in pre mode adjusts its last node if the node is a
childless node like <br>. However, right now it's using GetPrevSibling,
which can lead to error in some edge cases such as:
<p></p><div><br></div>
In this case, if the last node is <br> with offset 0, GetPrevSibling
will return <p> because <p> is <br>'s parent's previous sibling, and the
last node will be set to <p>. However, the correct last node in this
case is <div>, because <br> with offset 0 refers to the position to the
left of <br>, which is <div> with offset 0. In this case, PrevNode
returns the correct <div> value, so we should set the last node to the
result of PrevNode.
For the first node, for a childless node in pre mode, GetNextSibling and
NextNode are the same, so there is no bug in this case. Nevertheless,
this patch changes the call to NextNode to be consistent with calling
PrevNode for the last node.
Bug 1319660 - 3. Add test for correctly adjusting last node in content iterator; r=masayuki
Add a test for the previous patch that makes sure querying selected text
in an edge case works correctly.
Bug 1319660 - 4. Add test for start node regression; r=me
Add a new test case for the NextNode() regression. r=me for trivial
test-only patch.
Bug 1319660 - 5. Restore GetNextSibling call for first node of pre-content-iterator; r=smaug
The last patch changed the `GetNextSibling()` call to `NextNode()`
because I assumed they're equivalent in this case. That turned out to
not be the case because we can reach this line even if the node has
children -- the index just has to be after the last child. So this patch
restores the `GetNextSibling` call to restore the correct behavior.
I also added some comment to clarify that we can reach this line due to
one of two conditions: 1) the node has no children; 2) the node has
children but the index is after the last child.
This patch also replaces the `HasChildren()` check when setting
`cChild`. If the index is after the last child (i.e. index ==
childCount), `GetChildAt()` fails and we erroneously log an assertion
warning, even though the input was valid. The new check handles all
cases whether start node has children or not.
2017-01-23 22:35:04 +03:00
|
|
|
NS_WARNING_ASSERTION(mLast, "PrevNode returned null");
|
Bug 1351170 - 1. Correctly calculate start offset for non-text nodes; r=masayuki
When the start node is a non-container node (i.e. <br>), and the start
offset is 0, we should not include a newline character for the node. For
example, for this range,
> <br/>hello
> \___/
the start node/offset is (<br/>, 0) and end node/offset is ("hello", 1).
The calculated range offset should be 0, and the range length should be
2: 1 for the <br/> newline character plus 1 for "h".
The patch also ensures this behavior for pre-mode nsContentIterator, for
both start and end node adjustments. For start nodes, we include any
non-container nodes with offset 0 in the range. For end node, we exclude
any non-container nodes with offset 0 from the range.
MozReview-Commit-ID: Lt2tCLbapq7
--HG--
extra : rebase_source : 7d86b6cf04581f1cd71fa85f8c8586541b3a84e9
2017-07-19 21:29:59 +03:00
|
|
|
if (mLast && mLast != mFirst &&
|
|
|
|
NS_WARN_IF(!NodeIsInTraversalRange(
|
2017-10-02 17:58:31 +03:00
|
|
|
mLast, mPre, RawRangeBoundary(mFirst, 0), aEnd))) {
|
2015-10-21 17:26:20 +03:00
|
|
|
mLast = nullptr;
|
|
|
|
}
|
|
|
|
} else {
|
2017-10-02 17:58:31 +03:00
|
|
|
mLast = aEnd.Container()->AsContent();
|
2015-10-21 17:26:20 +03:00
|
|
|
}
|
2006-11-02 10:41:45 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
// post-order
|
|
|
|
//
|
|
|
|
// XXX: In the future, if end offset is before the first character in the
|
|
|
|
// cdata node, should we set mLast to the prev sibling?
|
|
|
|
|
|
|
|
if (!endIsData) {
|
2017-10-02 17:58:31 +03:00
|
|
|
mLast = GetPrevSibling(aEnd.Container());
|
2016-09-02 10:12:24 +03:00
|
|
|
NS_WARNING_ASSERTION(mLast, "GetPrevSibling returned null");
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2017-10-02 17:58:31 +03:00
|
|
|
if (!NodeIsInTraversalRange(mLast, mPre, aStart, aEnd)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mLast = nullptr;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
|
|
|
} else {
|
2017-10-02 17:58:31 +03:00
|
|
|
mLast = aEnd.Container()->AsContent();
|
2006-11-02 10:41:45 +03:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2017-10-02 17:58:31 +03:00
|
|
|
cChild = aEnd.Ref();
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!cChild)) {
|
2012-06-14 10:47:03 +04:00
|
|
|
// No child at offset!
|
2019-01-11 04:47:15 +03:00
|
|
|
MOZ_ASSERT_UNREACHABLE("ContentIterator::ContentIterator");
|
2012-06-14 10:47:03 +04:00
|
|
|
return NS_ERROR_FAILURE;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (mPre) {
|
2012-06-14 10:47:03 +04:00
|
|
|
mLast = GetDeepLastChild(cChild);
|
2016-09-02 10:12:24 +03:00
|
|
|
NS_WARNING_ASSERTION(mLast, "GetDeepLastChild returned null");
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2017-10-02 17:58:31 +03:00
|
|
|
if (NS_WARN_IF(!NodeIsInTraversalRange(mLast, mPre, aStart, aEnd))) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mLast = nullptr;
|
2006-11-02 10:41:45 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
|
|
|
// post-order
|
2002-11-21 18:07:49 +03:00
|
|
|
mLast = cChild;
|
2006-11-02 10:41:45 +03:00
|
|
|
}
|
2002-11-21 18:07:49 +03:00
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// If either first or last is null, they both have to be null!
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2015-12-17 02:58:25 +03:00
|
|
|
if (!mFirst || !mLast) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mFirst = nullptr;
|
|
|
|
mLast = nullptr;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
|
1998-12-15 02:16:31 +03:00
|
|
|
mCurNode = mFirst;
|
2002-11-21 18:07:49 +03:00
|
|
|
mIsDone = !mCurNode;
|
|
|
|
|
2017-10-02 19:09:42 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-02-12 08:28:12 +03:00
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
void ContentIteratorBase::MakeEmpty() {
|
2012-07-30 18:20:58 +04:00
|
|
|
mCurNode = nullptr;
|
|
|
|
mFirst = nullptr;
|
|
|
|
mLast = nullptr;
|
|
|
|
mCommonParent = nullptr;
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = true;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
nsINode* ContentIteratorBase::GetDeepFirstChild(nsINode* aRoot) {
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aRoot) || !aRoot->HasChildren()) {
|
2012-06-14 10:47:03 +04:00
|
|
|
return aRoot;
|
|
|
|
}
|
2017-12-04 19:23:48 +03:00
|
|
|
|
|
|
|
return GetDeepFirstChild(aRoot->GetFirstChild());
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
nsIContent* ContentIteratorBase::GetDeepFirstChild(nsIContent* aRoot) {
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aRoot)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2003-09-27 08:18:26 +04:00
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent* node = aRoot;
|
|
|
|
nsIContent* child = node->GetFirstChild();
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
while (child) {
|
|
|
|
node = child;
|
|
|
|
child = node->GetFirstChild();
|
1999-02-12 08:28:12 +03:00
|
|
|
}
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
return node;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
nsINode* ContentIteratorBase::GetDeepLastChild(nsINode* aRoot) {
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aRoot) || !aRoot->HasChildren()) {
|
2012-06-14 10:47:03 +04:00
|
|
|
return aRoot;
|
|
|
|
}
|
2017-12-04 19:23:48 +03:00
|
|
|
|
|
|
|
return GetDeepLastChild(aRoot->GetLastChild());
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
nsIContent* ContentIteratorBase::GetDeepLastChild(nsIContent* aRoot) {
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aRoot)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2003-09-27 08:18:26 +04:00
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent* node = aRoot;
|
2017-12-04 19:23:48 +03:00
|
|
|
while (node->HasChildren()) {
|
|
|
|
nsIContent* child = node->GetLastChild();
|
2012-06-14 10:47:03 +04:00
|
|
|
node = child;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
return node;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// Get the next sibling, or parent's next sibling, or grandpa's next sibling...
|
2019-01-11 04:48:11 +03:00
|
|
|
nsIContent* ContentIteratorBase::GetNextSibling(nsINode* aNode) {
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aNode)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2003-07-29 01:09:56 +04:00
|
|
|
|
2017-12-04 19:23:48 +03:00
|
|
|
if (aNode->GetNextSibling()) {
|
|
|
|
return aNode->GetNextSibling();
|
|
|
|
}
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* parent = aNode->GetParentNode();
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!parent)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2017-12-04 19:23:48 +03:00
|
|
|
// XXX This is a hack to preserve previous behaviour: This should be fixed
|
|
|
|
// in bug 1404916. If we were positioned on anonymous content, move to
|
|
|
|
// the first child of our parent.
|
|
|
|
if (parent->GetLastChild() && parent->GetLastChild() != aNode) {
|
|
|
|
return parent->GetFirstChild();
|
2017-10-02 19:09:42 +03:00
|
|
|
}
|
|
|
|
|
2017-12-04 19:23:48 +03:00
|
|
|
return GetNextSibling(parent);
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// Get the prev sibling, or parent's prev sibling, or grandpa's prev sibling...
|
2019-01-11 04:48:11 +03:00
|
|
|
nsIContent* ContentIteratorBase::GetPrevSibling(nsINode* aNode) {
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aNode)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2003-07-29 01:09:56 +04:00
|
|
|
|
2017-12-04 19:23:48 +03:00
|
|
|
if (aNode->GetPreviousSibling()) {
|
|
|
|
return aNode->GetPreviousSibling();
|
|
|
|
}
|
|
|
|
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* parent = aNode->GetParentNode();
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!parent)) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2017-12-04 19:23:48 +03:00
|
|
|
// XXX This is a hack to preserve previous behaviour: This should be fixed
|
|
|
|
// in bug 1404916. If we were positioned on anonymous content, move to
|
|
|
|
// the last child of our parent.
|
|
|
|
if (parent->GetFirstChild() && parent->GetFirstChild() != aNode) {
|
|
|
|
return parent->GetLastChild();
|
2017-10-02 19:09:42 +03:00
|
|
|
}
|
|
|
|
|
2017-12-04 19:23:48 +03:00
|
|
|
return GetPrevSibling(parent);
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
nsINode* ContentIteratorBase::NextNode(nsINode* aNode) {
|
2012-06-14 10:47:03 +04:00
|
|
|
nsINode* node = aNode;
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// if we are a Pre-order iterator, use pre-order
|
|
|
|
if (mPre) {
|
1999-03-23 13:30:24 +03:00
|
|
|
// if it has children then next node is first child
|
2012-06-14 10:47:03 +04:00
|
|
|
if (node->HasChildren()) {
|
2012-06-14 10:47:03 +04:00
|
|
|
nsIContent* firstChild = node->GetFirstChild();
|
2015-11-10 05:49:04 +03:00
|
|
|
MOZ_ASSERT(firstChild);
|
2003-09-27 08:18:26 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
return firstChild;
|
1999-03-23 13:30:24 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
1999-03-23 13:30:24 +03:00
|
|
|
// else next sibling is next
|
2017-12-04 19:23:48 +03:00
|
|
|
return GetNextSibling(node);
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
2003-07-29 01:09:56 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// post-order
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* parent = node->GetParentNode();
|
2016-03-31 09:00:50 +03:00
|
|
|
if (NS_WARN_IF(!parent)) {
|
|
|
|
MOZ_ASSERT(parent, "The node is the root node but not the last node");
|
|
|
|
mIsDone = true;
|
|
|
|
return node;
|
|
|
|
}
|
2017-10-02 19:09:42 +03:00
|
|
|
|
2017-12-04 19:23:48 +03:00
|
|
|
nsIContent* sibling = node->GetNextSibling();
|
2012-06-14 10:47:03 +04:00
|
|
|
if (sibling) {
|
|
|
|
// next node is sibling's "deep left" child
|
2017-12-04 19:23:48 +03:00
|
|
|
return GetDeepFirstChild(sibling);
|
1999-03-23 13:30:24 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
return parent;
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
nsINode* ContentIteratorBase::PrevNode(nsINode* aNode) {
|
2012-06-14 10:47:03 +04:00
|
|
|
nsINode* node = aNode;
|
|
|
|
|
|
|
|
// if we are a Pre-order iterator, use pre-order
|
|
|
|
if (mPre) {
|
2012-10-09 16:31:24 +04:00
|
|
|
nsINode* parent = node->GetParentNode();
|
2016-03-31 09:00:50 +03:00
|
|
|
if (NS_WARN_IF(!parent)) {
|
|
|
|
MOZ_ASSERT(parent, "The node is the root node but not the first node");
|
|
|
|
mIsDone = true;
|
|
|
|
return aNode;
|
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
|
2017-12-04 19:23:48 +03:00
|
|
|
nsIContent* sibling = node->GetPreviousSibling();
|
|
|
|
if (sibling) {
|
|
|
|
return GetDeepLastChild(sibling);
|
2017-10-02 19:09:42 +03:00
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
return parent;
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// post-order
|
2017-12-04 19:23:48 +03:00
|
|
|
if (node->HasChildren()) {
|
|
|
|
return node->GetLastChild();
|
1999-03-23 13:30:24 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// else prev sibling is previous
|
2017-12-04 19:23:48 +03:00
|
|
|
return GetPrevSibling(node);
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
|
|
|
|
1999-02-12 08:28:12 +03:00
|
|
|
/******************************************************
|
2019-01-11 04:48:11 +03:00
|
|
|
* ContentIteratorBase routines
|
1999-02-12 08:28:12 +03:00
|
|
|
******************************************************/
|
1998-12-15 02:16:31 +03:00
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
void ContentIteratorBase::First() {
|
2004-01-24 03:46:17 +03:00
|
|
|
if (mFirst) {
|
2016-06-23 12:58:22 +03:00
|
|
|
mozilla::DebugOnly<nsresult> rv = PositionAt(mFirst);
|
2004-01-24 03:46:17 +03:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to position iterator!");
|
|
|
|
}
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mIsDone = mFirst == nullptr;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
void ContentIteratorBase::Last() {
|
2016-08-12 08:11:33 +03:00
|
|
|
// Note that mLast can be nullptr if MakeEmpty() is called in Init() since
|
|
|
|
// at that time, Init() returns NS_OK.
|
|
|
|
if (!mLast) {
|
|
|
|
MOZ_ASSERT(mIsDone);
|
|
|
|
return;
|
2004-01-24 03:46:17 +03:00
|
|
|
}
|
|
|
|
|
2016-08-12 08:11:33 +03:00
|
|
|
mozilla::DebugOnly<nsresult> rv = PositionAt(mLast);
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to position iterator!");
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mIsDone = mLast == nullptr;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
void ContentIteratorBase::Next() {
|
2015-11-10 05:49:04 +03:00
|
|
|
if (mIsDone || NS_WARN_IF(!mCurNode)) {
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (mCurNode == mLast) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = true;
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
1999-01-29 02:55:53 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2017-12-04 19:23:48 +03:00
|
|
|
mCurNode = NextNode(mCurNode);
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
void ContentIteratorBase::Prev() {
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(mIsDone) || NS_WARN_IF(!mCurNode)) {
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
if (mCurNode == mFirst) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = true;
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2017-12-04 19:23:48 +03:00
|
|
|
mCurNode = PrevNode(mCurNode);
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
bool ContentIteratorBase::IsDone() { return mIsDone; }
|
1999-02-12 08:28:12 +03:00
|
|
|
|
2001-09-24 10:13:03 +04:00
|
|
|
// Keeping arrays of indexes for the stack of nodes makes PositionAt
|
|
|
|
// interesting...
|
2019-01-11 04:48:11 +03:00
|
|
|
nsresult ContentIteratorBase::PositionAt(nsINode* aCurNode) {
|
2015-11-10 05:49:04 +03:00
|
|
|
if (NS_WARN_IF(!aCurNode)) {
|
1999-03-23 13:30:24 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2001-09-24 10:13:03 +04:00
|
|
|
// take an early out if this doesn't actually change the position
|
2017-12-04 19:23:48 +03:00
|
|
|
if (mCurNode == aCurNode) {
|
|
|
|
mIsDone = false;
|
2001-09-24 10:13:03 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2017-12-04 19:23:48 +03:00
|
|
|
mCurNode = aCurNode;
|
2001-09-24 10:13:03 +04:00
|
|
|
|
2002-11-21 18:07:49 +03:00
|
|
|
// Check to see if the node falls within the traversal range.
|
|
|
|
|
2017-10-02 17:58:31 +03:00
|
|
|
RawRangeBoundary first(mFirst, 0);
|
|
|
|
RawRangeBoundary last(mLast, 0);
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2017-10-02 17:58:31 +03:00
|
|
|
if (mFirst && mLast) {
|
2012-06-14 10:47:03 +04:00
|
|
|
if (mPre) {
|
2017-10-02 17:58:31 +03:00
|
|
|
// In pre we want to record the point immediately before mFirst, which is
|
|
|
|
// the point immediately after mFirst's previous sibling.
|
|
|
|
first.SetAfterRef(mFirst->GetParentNode(), mFirst->GetPreviousSibling());
|
2002-11-21 18:07:49 +03:00
|
|
|
|
2017-10-02 17:58:31 +03:00
|
|
|
// If mLast has no children, then we want to make sure to include it.
|
|
|
|
if (!mLast->HasChildren()) {
|
|
|
|
last.SetAfterRef(mLast->GetParentNode(), mLast->AsContent());
|
2002-11-21 18:07:49 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2017-10-02 17:58:31 +03:00
|
|
|
// If the first node has any children, we want to be immediately after the
|
|
|
|
// last. Otherwise we want to be immediately before mFirst.
|
|
|
|
if (mFirst->HasChildren()) {
|
|
|
|
first.SetAfterRef(mFirst, mFirst->GetLastChild());
|
2012-06-14 10:47:03 +04:00
|
|
|
} else {
|
2017-10-02 17:58:31 +03:00
|
|
|
first.SetAfterRef(mFirst->GetParentNode(),
|
|
|
|
mFirst->GetPreviousSibling());
|
2012-06-14 10:47:03 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2017-10-02 17:58:31 +03:00
|
|
|
// Set the last point immediately after the final node.
|
|
|
|
last.SetAfterRef(mLast->GetParentNode(), mLast->AsContent());
|
2002-11-21 18:07:49 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-02 17:58:31 +03:00
|
|
|
NS_WARNING_ASSERTION(first.IsSetAndValid(), "first is not valid");
|
|
|
|
NS_WARNING_ASSERTION(last.IsSetAndValid(), "last is not valid");
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// The end positions are always in the range even if it has no parent. We
|
|
|
|
// need to allow that or 'iter->Init(root)' would assert in Last() or First()
|
|
|
|
// for example, bug 327694.
|
2009-08-27 22:05:23 +04:00
|
|
|
if (mFirst != mCurNode && mLast != mCurNode &&
|
2017-10-02 17:58:31 +03:00
|
|
|
(NS_WARN_IF(!first.IsSet()) || NS_WARN_IF(!last.IsSet()) ||
|
|
|
|
NS_WARN_IF(!NodeIsInTraversalRange(mCurNode, mPre, first, last)))) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = true;
|
2002-11-21 18:07:49 +03:00
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = false;
|
1999-03-23 13:30:24 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-01-11 04:48:11 +03:00
|
|
|
nsINode* ContentIteratorBase::GetCurrentNode() {
|
2004-01-24 03:46:17 +03:00
|
|
|
if (mIsDone) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2002-12-11 17:24:49 +03:00
|
|
|
}
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
NS_ASSERTION(mCurNode, "Null current node in an iterator that's not done!");
|
|
|
|
|
|
|
|
return mCurNode;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
|
|
|
|
1999-02-14 12:15:13 +03:00
|
|
|
/******************************************************
|
2019-01-11 04:52:26 +03:00
|
|
|
* ContentSubtreeIterator init routines
|
2019-01-10 12:42:27 +03:00
|
|
|
******************************************************/
|
|
|
|
|
2019-01-11 04:47:15 +03:00
|
|
|
nsresult ContentSubtreeIterator::Init(nsINode* aRoot) {
|
1999-02-14 12:15:13 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
2019-01-11 04:47:15 +03:00
|
|
|
nsresult ContentSubtreeIterator::Init(nsRange* aRange) {
|
2012-06-12 12:45:09 +04:00
|
|
|
MOZ_ASSERT(aRange);
|
1999-02-16 18:48:13 +03:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = false;
|
1999-10-09 03:34:07 +04:00
|
|
|
|
2018-05-17 19:01:38 +03:00
|
|
|
if (NS_WARN_IF(!aRange->IsPositioned())) {
|
2017-06-26 11:26:27 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2018-05-17 19:01:38 +03:00
|
|
|
mRange = aRange;
|
2017-06-26 11:26:27 +03:00
|
|
|
|
|
|
|
return InitWithRange();
|
|
|
|
}
|
|
|
|
|
2019-01-11 04:47:15 +03:00
|
|
|
nsresult ContentSubtreeIterator::Init(nsINode* aStartContainer,
|
|
|
|
uint32_t aStartOffset,
|
|
|
|
nsINode* aEndContainer,
|
|
|
|
uint32_t aEndOffset) {
|
2017-10-02 17:58:31 +03:00
|
|
|
return Init(RawRangeBoundary(aStartContainer, aStartOffset),
|
|
|
|
RawRangeBoundary(aEndContainer, aEndOffset));
|
|
|
|
}
|
|
|
|
|
2019-06-28 10:47:29 +03:00
|
|
|
nsresult ContentSubtreeIterator::Init(const RawRangeBoundary& aStartBoundary,
|
|
|
|
const RawRangeBoundary& aEndBoundary) {
|
2017-06-26 11:26:27 +03:00
|
|
|
mIsDone = false;
|
|
|
|
|
2019-06-28 10:47:29 +03:00
|
|
|
RefPtr<nsRange> range =
|
|
|
|
nsRange::Create(aStartBoundary, aEndBoundary, IgnoreErrors());
|
|
|
|
if (NS_WARN_IF(!range) || NS_WARN_IF(!range->IsPositioned())) {
|
2017-06-26 11:26:27 +03:00
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2019-06-28 10:47:29 +03:00
|
|
|
if (NS_WARN_IF(range->StartRef() != aStartBoundary) ||
|
|
|
|
NS_WARN_IF(range->EndRef() != aEndBoundary)) {
|
2017-06-26 11:26:27 +03:00
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
}
|
|
|
|
|
2018-05-30 22:15:35 +03:00
|
|
|
mRange = std::move(range);
|
2017-06-26 11:26:27 +03:00
|
|
|
|
|
|
|
return InitWithRange();
|
|
|
|
}
|
|
|
|
|
2019-01-11 04:47:15 +03:00
|
|
|
nsresult ContentSubtreeIterator::InitWithRange() {
|
2017-06-26 11:26:27 +03:00
|
|
|
MOZ_ASSERT(mRange);
|
|
|
|
MOZ_ASSERT(mRange->IsPositioned());
|
1999-03-01 11:17:18 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
// get the start node and offset, convert to nsINode
|
|
|
|
mCommonParent = mRange->GetCommonAncestor();
|
2017-07-11 18:02:14 +03:00
|
|
|
nsINode* startContainer = mRange->GetStartContainer();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t startOffset = mRange->StartOffset();
|
2017-07-11 18:08:37 +03:00
|
|
|
nsINode* endContainer = mRange->GetEndContainer();
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t endOffset = mRange->EndOffset();
|
2017-07-11 18:08:37 +03:00
|
|
|
MOZ_ASSERT(mCommonParent && startContainer && endContainer);
|
2012-07-05 11:45:08 +04:00
|
|
|
// Bug 767169
|
2017-07-11 18:02:14 +03:00
|
|
|
MOZ_ASSERT(uint32_t(startOffset) <= startContainer->Length() &&
|
2017-07-11 18:08:37 +03:00
|
|
|
uint32_t(endOffset) <= endContainer->Length());
|
2005-08-24 00:59:54 +04:00
|
|
|
|
1999-06-02 04:41:45 +04:00
|
|
|
// short circuit when start node == end node
|
2017-07-11 18:08:37 +03:00
|
|
|
if (startContainer == endContainer) {
|
2017-07-11 18:02:14 +03:00
|
|
|
nsINode* child = startContainer->GetFirstChild();
|
2012-06-12 12:45:09 +04:00
|
|
|
|
|
|
|
if (!child || startOffset == endOffset) {
|
|
|
|
// Text node, empty container, or collapsed
|
1999-06-02 04:41:45 +04:00
|
|
|
MakeEmpty();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
2012-06-12 12:45:09 +04:00
|
|
|
|
2001-02-15 16:19:14 +03:00
|
|
|
// cache ancestors
|
2018-06-19 21:46:43 +03:00
|
|
|
mEndNodes.Clear();
|
|
|
|
nsIContent* endNode =
|
|
|
|
endContainer->IsContent() ? endContainer->AsContent() : nullptr;
|
|
|
|
while (endNode) {
|
|
|
|
mEndNodes.AppendElement(endNode);
|
|
|
|
endNode = endNode->GetParent();
|
|
|
|
}
|
2001-02-15 16:19:14 +03:00
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
nsIContent* firstCandidate = nullptr;
|
|
|
|
nsIContent* lastCandidate = nullptr;
|
2008-03-17 07:56:22 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
// find first node in range
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t offset = mRange->StartOffset();
|
2012-06-12 12:45:09 +04:00
|
|
|
|
2016-10-13 15:33:07 +03:00
|
|
|
nsINode* node = nullptr;
|
2017-07-11 18:02:14 +03:00
|
|
|
if (!startContainer->GetChildCount()) {
|
2012-06-12 12:45:09 +04:00
|
|
|
// no children, start at the node itself
|
2017-07-11 18:02:14 +03:00
|
|
|
node = startContainer;
|
2012-06-12 12:45:09 +04:00
|
|
|
} else {
|
2018-06-19 21:46:43 +03:00
|
|
|
nsIContent* child = mRange->GetChildAtStartOffset();
|
|
|
|
MOZ_ASSERT(child == startContainer->GetChildAt_Deprecated(offset));
|
2012-06-12 12:45:09 +04:00
|
|
|
if (!child) {
|
|
|
|
// offset after last child
|
2017-07-11 18:02:14 +03:00
|
|
|
node = startContainer;
|
2012-06-12 12:45:09 +04:00
|
|
|
} else {
|
|
|
|
firstCandidate = child;
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
if (!firstCandidate) {
|
|
|
|
// then firstCandidate is next node after node
|
2012-06-14 10:47:03 +04:00
|
|
|
firstCandidate = GetNextSibling(node);
|
2012-06-12 12:45:09 +04:00
|
|
|
|
|
|
|
if (!firstCandidate) {
|
1999-03-01 11:17:18 +03:00
|
|
|
MakeEmpty();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
2012-06-12 12:45:09 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
firstCandidate = GetDeepFirstChild(firstCandidate);
|
2012-06-12 12:45:09 +04:00
|
|
|
|
|
|
|
// confirm that this first possible contained node is indeed contained. Else
|
|
|
|
// we have a range that does not fully contain any node.
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nodeBefore, nodeAfter;
|
2019-06-28 10:48:07 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(RangeUtils::CompareNodeToRange(firstCandidate, mRange,
|
|
|
|
&nodeBefore, &nodeAfter));
|
2003-01-15 02:05:52 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
if (nodeBefore || nodeAfter) {
|
1999-03-01 11:17:18 +03:00
|
|
|
MakeEmpty();
|
1999-02-16 18:48:13 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
// cool, we have the first node in the range. Now we walk up its ancestors
|
|
|
|
// to find the most senior that is still in the range. That's the real first
|
|
|
|
// node.
|
|
|
|
mFirst = GetTopAncestorInRange(firstCandidate);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
1999-03-01 11:17:18 +03:00
|
|
|
// now to find the last node
|
2012-06-12 12:45:09 +04:00
|
|
|
offset = mRange->EndOffset();
|
2017-07-11 18:08:37 +03:00
|
|
|
int32_t numChildren = endContainer->GetChildCount();
|
1999-07-25 09:30:15 +04:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
if (offset > numChildren) {
|
2012-07-05 11:45:08 +04:00
|
|
|
// Can happen for text nodes
|
2012-06-12 12:45:09 +04:00
|
|
|
offset = numChildren;
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
2012-06-28 15:29:56 +04:00
|
|
|
if (!offset || !numChildren) {
|
2017-07-11 18:08:37 +03:00
|
|
|
node = endContainer;
|
2012-06-12 12:45:09 +04:00
|
|
|
} else {
|
2018-06-19 21:46:43 +03:00
|
|
|
lastCandidate = mRange->EndRef().Ref();
|
|
|
|
MOZ_ASSERT(lastCandidate == endContainer->GetChildAt_Deprecated(--offset));
|
2012-06-12 12:45:09 +04:00
|
|
|
NS_ASSERTION(lastCandidate,
|
2019-01-11 04:47:15 +03:00
|
|
|
"tree traversal trouble in ContentSubtreeIterator::Init");
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
2012-06-12 12:45:09 +04:00
|
|
|
|
|
|
|
if (!lastCandidate) {
|
|
|
|
// then lastCandidate is prev node before node
|
2012-06-14 10:47:03 +04:00
|
|
|
lastCandidate = GetPrevSibling(node);
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2012-06-12 12:45:09 +04:00
|
|
|
|
2012-06-28 15:29:56 +04:00
|
|
|
if (!lastCandidate) {
|
|
|
|
MakeEmpty();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
lastCandidate = GetDeepLastChild(lastCandidate);
|
2003-01-15 02:05:52 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
// confirm that this last possible contained node is indeed contained. Else
|
|
|
|
// we have a range that does not fully contain any node.
|
|
|
|
|
2019-06-28 10:48:07 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(RangeUtils::CompareNodeToRange(lastCandidate, mRange,
|
|
|
|
&nodeBefore, &nodeAfter));
|
2012-06-12 12:45:09 +04:00
|
|
|
|
|
|
|
if (nodeBefore || nodeAfter) {
|
1999-03-01 11:17:18 +03:00
|
|
|
MakeEmpty();
|
|
|
|
return NS_OK;
|
1999-02-16 18:48:13 +03:00
|
|
|
}
|
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
// cool, we have the last node in the range. Now we walk up its ancestors to
|
|
|
|
// find the most senior that is still in the range. That's the real first
|
|
|
|
// node.
|
|
|
|
mLast = GetTopAncestorInRange(lastCandidate);
|
|
|
|
|
1999-02-16 18:48:13 +03:00
|
|
|
mCurNode = mFirst;
|
1999-03-01 11:17:18 +03:00
|
|
|
|
1999-02-16 18:48:13 +03:00
|
|
|
return NS_OK;
|
1998-12-15 02:16:31 +03:00
|
|
|
}
|
1999-02-14 12:15:13 +03:00
|
|
|
|
|
|
|
/****************************************************************
|
2019-01-11 04:47:15 +03:00
|
|
|
* ContentSubtreeIterator overrides of ContentIterator routines
|
1999-02-14 12:15:13 +03:00
|
|
|
****************************************************************/
|
|
|
|
|
2001-09-28 01:49:40 +04:00
|
|
|
// we can't call PositionAt in a subtree iterator...
|
2019-01-11 04:47:15 +03:00
|
|
|
void ContentSubtreeIterator::First() {
|
2012-07-30 18:20:58 +04:00
|
|
|
mIsDone = mFirst == nullptr;
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2001-09-28 01:49:40 +04:00
|
|
|
mCurNode = mFirst;
|
|
|
|
}
|
|
|
|
|
|
|
|
// we can't call PositionAt in a subtree iterator...
|
2019-01-11 04:47:15 +03:00
|
|
|
void ContentSubtreeIterator::Last() {
|
2012-07-30 18:20:58 +04:00
|
|
|
mIsDone = mLast == nullptr;
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2001-09-28 01:49:40 +04:00
|
|
|
mCurNode = mLast;
|
|
|
|
}
|
|
|
|
|
2019-01-11 04:47:15 +03:00
|
|
|
void ContentSubtreeIterator::Next() {
|
2012-06-12 12:45:09 +04:00
|
|
|
if (mIsDone || !mCurNode) {
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
2012-06-12 12:45:09 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
if (mCurNode == mLast) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = true;
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2004-03-10 02:57:56 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
nsINode* nextNode = GetNextSibling(mCurNode);
|
2004-03-10 02:57:56 +03:00
|
|
|
NS_ASSERTION(nextNode, "No next sibling!?! This could mean deadlock!");
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t i = mEndNodes.IndexOf(nextNode);
|
2012-06-12 12:45:09 +04:00
|
|
|
while (i != -1) {
|
2001-02-15 16:19:14 +03:00
|
|
|
// as long as we are finding ancestors of the endpoint of the range,
|
|
|
|
// dive down into their children
|
2011-09-27 11:54:58 +04:00
|
|
|
nextNode = nextNode->GetFirstChild();
|
2004-03-11 02:32:05 +03:00
|
|
|
NS_ASSERTION(nextNode, "Iterator error, expected a child node!");
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2004-03-11 02:32:05 +03:00
|
|
|
// should be impossible to get a null pointer. If we went all the way
|
2012-06-12 12:45:09 +04:00
|
|
|
// down the child chain to the bottom without finding an interior node,
|
2001-02-15 16:19:14 +03:00
|
|
|
// then the previous node should have been the last, which was
|
|
|
|
// was tested at top of routine.
|
2004-03-10 02:57:56 +03:00
|
|
|
i = mEndNodes.IndexOf(nextNode);
|
2001-02-15 16:19:14 +03:00
|
|
|
}
|
|
|
|
|
2004-01-24 03:46:17 +03:00
|
|
|
mCurNode = nextNode;
|
|
|
|
|
2004-03-10 02:57:56 +03:00
|
|
|
// This shouldn't be needed, but since our selection code can put us
|
|
|
|
// in a situation where mLast is in generated content, we need this
|
|
|
|
// to stop the iterator when we've walked past past the last node!
|
2012-07-30 18:20:58 +04:00
|
|
|
mIsDone = mCurNode == nullptr;
|
1999-02-14 12:15:13 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 04:47:15 +03:00
|
|
|
void ContentSubtreeIterator::Prev() {
|
2004-01-24 03:46:17 +03:00
|
|
|
// Prev should be optimized to use the mStartNodes, just as Next
|
|
|
|
// uses mEndNodes.
|
2012-06-12 12:45:09 +04:00
|
|
|
if (mIsDone || !mCurNode) {
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
2012-06-12 12:45:09 +04:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
if (mCurNode == mFirst) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDone = true;
|
2004-01-24 03:46:17 +03:00
|
|
|
return;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// If any of these function calls return null, so will all succeeding ones,
|
|
|
|
// so mCurNode will wind up set to null.
|
2012-06-14 10:47:03 +04:00
|
|
|
nsINode* prevNode = GetDeepFirstChild(mCurNode);
|
2012-06-12 12:45:09 +04:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
prevNode = PrevNode(prevNode);
|
2004-01-24 03:46:17 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
prevNode = GetDeepLastChild(prevNode);
|
2012-06-12 12:45:09 +04:00
|
|
|
|
|
|
|
mCurNode = GetTopAncestorInRange(prevNode);
|
2004-03-10 02:57:56 +03:00
|
|
|
|
|
|
|
// This shouldn't be needed, but since our selection code can put us
|
|
|
|
// in a situation where mFirst is in generated content, we need this
|
|
|
|
// to stop the iterator when we've walked past past the first node!
|
2012-07-30 18:20:58 +04:00
|
|
|
mIsDone = mCurNode == nullptr;
|
1999-02-14 12:15:13 +03:00
|
|
|
}
|
|
|
|
|
2019-01-11 04:47:15 +03:00
|
|
|
nsresult ContentSubtreeIterator::PositionAt(nsINode* aCurNode) {
|
2004-01-24 03:46:17 +03:00
|
|
|
NS_ERROR("Not implemented!");
|
|
|
|
|
1999-03-23 13:30:24 +03:00
|
|
|
return NS_ERROR_NOT_IMPLEMENTED;
|
|
|
|
}
|
|
|
|
|
1999-03-01 11:17:18 +03:00
|
|
|
/****************************************************************
|
2019-01-11 04:47:15 +03:00
|
|
|
* ContentSubtreeIterator helper routines
|
1999-03-01 11:17:18 +03:00
|
|
|
****************************************************************/
|
|
|
|
|
2019-01-11 04:47:15 +03:00
|
|
|
nsIContent* ContentSubtreeIterator::GetTopAncestorInRange(nsINode* aNode) {
|
2012-10-09 16:31:24 +04:00
|
|
|
if (!aNode || !aNode->GetParentNode()) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-12 12:45:09 +04:00
|
|
|
}
|
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
// aNode has a parent, so it must be content.
|
|
|
|
nsIContent* content = aNode->AsContent();
|
|
|
|
|
1999-03-01 11:17:18 +03:00
|
|
|
// sanity check: aNode is itself in the range
|
2011-09-29 10:19:26 +04:00
|
|
|
bool nodeBefore, nodeAfter;
|
2012-06-12 12:45:09 +04:00
|
|
|
nsresult res =
|
2019-06-28 10:48:07 +03:00
|
|
|
RangeUtils::CompareNodeToRange(aNode, mRange, &nodeBefore, &nodeAfter);
|
2012-06-12 12:45:09 +04:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(res) && !nodeBefore && !nodeAfter,
|
|
|
|
"aNode isn't in mRange, or something else weird happened");
|
|
|
|
if (NS_FAILED(res) || nodeBefore || nodeAfter) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2012-06-12 12:45:09 +04:00
|
|
|
}
|
2003-01-15 02:05:52 +03:00
|
|
|
|
2012-06-14 10:47:03 +04:00
|
|
|
while (content) {
|
|
|
|
nsIContent* parent = content->GetParent();
|
|
|
|
// content always has a parent. If its parent is the root, however --
|
|
|
|
// i.e., either it's not content, or it is content but its own parent is
|
|
|
|
// null -- then we're finished, since we don't go up to the root.
|
|
|
|
//
|
|
|
|
// We have to special-case this because CompareNodeToRange treats the root
|
|
|
|
// node differently -- see bug 765205.
|
2012-10-09 16:31:24 +04:00
|
|
|
if (!parent || !parent->GetParentNode()) {
|
2012-06-14 10:47:03 +04:00
|
|
|
return content;
|
Checking in for bug 50742, this change removes the use of XIF in mozilla and replaces the XIF converter with a HTML (and XML) serializer.
Contextual information added to HTML copy and intelligence added to HTML paste in the editor (fixes bugs 47014, 50568 and 46554, and partly (at least) fixes bug 53188).
Code written by vidur, jfrancis, jst, akkana. Tested by jfrancis, akkana, vidur, jst, kin. Reviwed (and super reviewed) by waterson, vidur, kin, jfrancis, jst
2000-10-07 14:57:30 +04:00
|
|
|
}
|
2019-06-28 10:48:07 +03:00
|
|
|
MOZ_ALWAYS_SUCCEEDS(RangeUtils::CompareNodeToRange(
|
|
|
|
parent, mRange, &nodeBefore, &nodeAfter));
|
2003-01-15 02:05:52 +03:00
|
|
|
|
2012-06-12 12:45:09 +04:00
|
|
|
if (nodeBefore || nodeAfter) {
|
2012-06-14 10:47:03 +04:00
|
|
|
return content;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
2012-06-14 10:47:03 +04:00
|
|
|
content = parent;
|
1999-03-01 11:17:18 +03:00
|
|
|
}
|
|
|
|
|
2013-06-29 05:38:30 +04:00
|
|
|
MOZ_CRASH("This should only be possible if aNode was null");
|
2012-06-12 12:45:09 +04:00
|
|
|
}
|
2019-01-11 04:47:15 +03:00
|
|
|
|
|
|
|
} // namespace mozilla
|