Bug 1444847 - part 1: Create `mozilla::dom::AbstractRange` r=smaug
This patch is based on the patch created by Makoto Kato-san.
`Range` and `StaticRange` have common base interface, `AbstractRange`.
https://dom.spec.whatwg.org/#abstractrange
This interface has simply returns `startContainer`, `endContainer`,
`startOffset`, `endOffset` and `collapsed`.
Different from the original patch's approach, this patch moves related
members in `nsRange` to `AbstractRange` since this approach avoids
virtual call cost. Additionally, this patch makes them not throw as
declared by the spec. As far as I know, the destruction cost of
`ErrorResult` may appear in profile so that we should avoid creating
the instance if we can avoid it.
Unfortunately, the instance size of `nsRange` becomes larger with this
patch. The size is changed from 176 to 184. I.e., now, `nsRange`
requires bigger chunk.
Differential Revision: https://phabricator.services.mozilla.com/D35140
--HG--
extra : moz-landing-system : lando
2019-06-28 07:46:35 +00: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: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
#include "mozilla/dom/AbstractRange.h"
|
|
|
|
#include "mozilla/dom/AbstractRangeBinding.h"
|
2019-06-28 07:48:52 +00:00
|
|
|
|
2019-12-19 13:27:28 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
|
|
|
#include "mozilla/Attributes.h"
|
2019-06-28 07:48:52 +00:00
|
|
|
#include "mozilla/RangeUtils.h"
|
|
|
|
#include "mozilla/dom/StaticRange.h"
|
Bug 1444847 - part 1: Create `mozilla::dom::AbstractRange` r=smaug
This patch is based on the patch created by Makoto Kato-san.
`Range` and `StaticRange` have common base interface, `AbstractRange`.
https://dom.spec.whatwg.org/#abstractrange
This interface has simply returns `startContainer`, `endContainer`,
`startOffset`, `endOffset` and `collapsed`.
Different from the original patch's approach, this patch moves related
members in `nsRange` to `AbstractRange` since this approach avoids
virtual call cost. Additionally, this patch makes them not throw as
declared by the spec. As far as I know, the destruction cost of
`ErrorResult` may appear in profile so that we should avoid creating
the instance if we can avoid it.
Unfortunately, the instance size of `nsRange` becomes larger with this
patch. The size is changed from 176 to 184. I.e., now, `nsRange`
requires bigger chunk.
Differential Revision: https://phabricator.services.mozilla.com/D35140
--HG--
extra : moz-landing-system : lando
2019-06-28 07:46:35 +00:00
|
|
|
#include "nsContentUtils.h"
|
|
|
|
#include "nsCycleCollectionParticipant.h"
|
2019-06-28 07:48:52 +00:00
|
|
|
#include "nsGkAtoms.h"
|
Bug 1444847 - part 1: Create `mozilla::dom::AbstractRange` r=smaug
This patch is based on the patch created by Makoto Kato-san.
`Range` and `StaticRange` have common base interface, `AbstractRange`.
https://dom.spec.whatwg.org/#abstractrange
This interface has simply returns `startContainer`, `endContainer`,
`startOffset`, `endOffset` and `collapsed`.
Different from the original patch's approach, this patch moves related
members in `nsRange` to `AbstractRange` since this approach avoids
virtual call cost. Additionally, this patch makes them not throw as
declared by the spec. As far as I know, the destruction cost of
`ErrorResult` may appear in profile so that we should avoid creating
the instance if we can avoid it.
Unfortunately, the instance size of `nsRange` becomes larger with this
patch. The size is changed from 176 to 184. I.e., now, `nsRange`
requires bigger chunk.
Differential Revision: https://phabricator.services.mozilla.com/D35140
--HG--
extra : moz-landing-system : lando
2019-06-28 07:46:35 +00:00
|
|
|
#include "nsINode.h"
|
2019-06-28 07:48:52 +00:00
|
|
|
#include "nsRange.h"
|
2020-02-14 15:02:43 +00:00
|
|
|
#include "nsTArray.h"
|
Bug 1444847 - part 1: Create `mozilla::dom::AbstractRange` r=smaug
This patch is based on the patch created by Makoto Kato-san.
`Range` and `StaticRange` have common base interface, `AbstractRange`.
https://dom.spec.whatwg.org/#abstractrange
This interface has simply returns `startContainer`, `endContainer`,
`startOffset`, `endOffset` and `collapsed`.
Different from the original patch's approach, this patch moves related
members in `nsRange` to `AbstractRange` since this approach avoids
virtual call cost. Additionally, this patch makes them not throw as
declared by the spec. As far as I know, the destruction cost of
`ErrorResult` may appear in profile so that we should avoid creating
the instance if we can avoid it.
Unfortunately, the instance size of `nsRange` becomes larger with this
patch. The size is changed from 176 to 184. I.e., now, `nsRange`
requires bigger chunk.
Differential Revision: https://phabricator.services.mozilla.com/D35140
--HG--
extra : moz-landing-system : lando
2019-06-28 07:46:35 +00:00
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2019-06-28 07:48:52 +00:00
|
|
|
template nsresult AbstractRange::SetStartAndEndInternal(
|
|
|
|
const RangeBoundary& aStartBoundary, const RangeBoundary& aEndBoundary,
|
|
|
|
nsRange* aRange);
|
|
|
|
template nsresult AbstractRange::SetStartAndEndInternal(
|
|
|
|
const RangeBoundary& aStartBoundary, const RawRangeBoundary& aEndBoundary,
|
|
|
|
nsRange* aRange);
|
|
|
|
template nsresult AbstractRange::SetStartAndEndInternal(
|
|
|
|
const RawRangeBoundary& aStartBoundary, const RangeBoundary& aEndBoundary,
|
|
|
|
nsRange* aRange);
|
|
|
|
template nsresult AbstractRange::SetStartAndEndInternal(
|
|
|
|
const RawRangeBoundary& aStartBoundary,
|
|
|
|
const RawRangeBoundary& aEndBoundary, nsRange* aRange);
|
|
|
|
template nsresult AbstractRange::SetStartAndEndInternal(
|
|
|
|
const RangeBoundary& aStartBoundary, const RangeBoundary& aEndBoundary,
|
|
|
|
StaticRange* aRange);
|
|
|
|
template nsresult AbstractRange::SetStartAndEndInternal(
|
|
|
|
const RangeBoundary& aStartBoundary, const RawRangeBoundary& aEndBoundary,
|
|
|
|
StaticRange* aRange);
|
|
|
|
template nsresult AbstractRange::SetStartAndEndInternal(
|
|
|
|
const RawRangeBoundary& aStartBoundary, const RangeBoundary& aEndBoundary,
|
|
|
|
StaticRange* aRange);
|
|
|
|
template nsresult AbstractRange::SetStartAndEndInternal(
|
|
|
|
const RawRangeBoundary& aStartBoundary,
|
|
|
|
const RawRangeBoundary& aEndBoundary, StaticRange* aRange);
|
2020-02-14 15:02:43 +00:00
|
|
|
template bool AbstractRange::MaybeCacheToReuse(nsRange& aInstance);
|
|
|
|
template bool AbstractRange::MaybeCacheToReuse(StaticRange& aInstance);
|
|
|
|
|
|
|
|
bool AbstractRange::sHasShutDown = false;
|
2019-06-28 07:48:52 +00:00
|
|
|
|
Bug 1444847 - part 1: Create `mozilla::dom::AbstractRange` r=smaug
This patch is based on the patch created by Makoto Kato-san.
`Range` and `StaticRange` have common base interface, `AbstractRange`.
https://dom.spec.whatwg.org/#abstractrange
This interface has simply returns `startContainer`, `endContainer`,
`startOffset`, `endOffset` and `collapsed`.
Different from the original patch's approach, this patch moves related
members in `nsRange` to `AbstractRange` since this approach avoids
virtual call cost. Additionally, this patch makes them not throw as
declared by the spec. As far as I know, the destruction cost of
`ErrorResult` may appear in profile so that we should avoid creating
the instance if we can avoid it.
Unfortunately, the instance size of `nsRange` becomes larger with this
patch. The size is changed from 176 to 184. I.e., now, `nsRange`
requires bigger chunk.
Differential Revision: https://phabricator.services.mozilla.com/D35140
--HG--
extra : moz-landing-system : lando
2019-06-28 07:46:35 +00:00
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(AbstractRange)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(AbstractRange)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(AbstractRange)
|
|
|
|
NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsISupports)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_CLASS(AbstractRange)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(AbstractRange)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner);
|
|
|
|
// mStart and mEnd may depend on or be depended on some other members in
|
|
|
|
// concrete classes so that they should be unlinked in sub classes.
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(AbstractRange)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mStart)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mEnd)
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION_TRACE_WRAPPERCACHE(AbstractRange)
|
|
|
|
|
2020-01-30 17:10:59 +00:00
|
|
|
// NOTE: If you need to change default value of members of AbstractRange,
|
2020-02-14 15:02:43 +00:00
|
|
|
// update nsRange::Create(nsINode* aNode) and ClearForReuse() too.
|
Bug 1444847 - part 1: Create `mozilla::dom::AbstractRange` r=smaug
This patch is based on the patch created by Makoto Kato-san.
`Range` and `StaticRange` have common base interface, `AbstractRange`.
https://dom.spec.whatwg.org/#abstractrange
This interface has simply returns `startContainer`, `endContainer`,
`startOffset`, `endOffset` and `collapsed`.
Different from the original patch's approach, this patch moves related
members in `nsRange` to `AbstractRange` since this approach avoids
virtual call cost. Additionally, this patch makes them not throw as
declared by the spec. As far as I know, the destruction cost of
`ErrorResult` may appear in profile so that we should avoid creating
the instance if we can avoid it.
Unfortunately, the instance size of `nsRange` becomes larger with this
patch. The size is changed from 176 to 184. I.e., now, `nsRange`
requires bigger chunk.
Differential Revision: https://phabricator.services.mozilla.com/D35140
--HG--
extra : moz-landing-system : lando
2019-06-28 07:46:35 +00:00
|
|
|
AbstractRange::AbstractRange(nsINode* aNode)
|
|
|
|
: mIsPositioned(false), mIsGenerated(false), mCalledByJS(false) {
|
2020-02-14 15:02:43 +00:00
|
|
|
Init(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AbstractRange::Init(nsINode* aNode) {
|
Bug 1444847 - part 1: Create `mozilla::dom::AbstractRange` r=smaug
This patch is based on the patch created by Makoto Kato-san.
`Range` and `StaticRange` have common base interface, `AbstractRange`.
https://dom.spec.whatwg.org/#abstractrange
This interface has simply returns `startContainer`, `endContainer`,
`startOffset`, `endOffset` and `collapsed`.
Different from the original patch's approach, this patch moves related
members in `nsRange` to `AbstractRange` since this approach avoids
virtual call cost. Additionally, this patch makes them not throw as
declared by the spec. As far as I know, the destruction cost of
`ErrorResult` may appear in profile so that we should avoid creating
the instance if we can avoid it.
Unfortunately, the instance size of `nsRange` becomes larger with this
patch. The size is changed from 176 to 184. I.e., now, `nsRange`
requires bigger chunk.
Differential Revision: https://phabricator.services.mozilla.com/D35140
--HG--
extra : moz-landing-system : lando
2019-06-28 07:46:35 +00:00
|
|
|
MOZ_ASSERT(aNode, "range isn't in a document!");
|
|
|
|
mOwner = aNode->OwnerDoc();
|
|
|
|
}
|
|
|
|
|
2020-02-14 15:02:43 +00:00
|
|
|
// static
|
|
|
|
void AbstractRange::Shutdown() {
|
|
|
|
sHasShutDown = true;
|
|
|
|
if (nsTArray<RefPtr<nsRange>>* cachedRanges = nsRange::sCachedRanges) {
|
|
|
|
nsRange::sCachedRanges = nullptr;
|
|
|
|
cachedRanges->Clear();
|
|
|
|
delete cachedRanges;
|
|
|
|
}
|
|
|
|
if (nsTArray<RefPtr<StaticRange>>* cachedRanges =
|
|
|
|
StaticRange::sCachedRanges) {
|
|
|
|
StaticRange::sCachedRanges = nullptr;
|
|
|
|
cachedRanges->Clear();
|
|
|
|
delete cachedRanges;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// static
|
|
|
|
template <class RangeType>
|
|
|
|
bool AbstractRange::MaybeCacheToReuse(RangeType& aInstance) {
|
|
|
|
static const size_t kMaxRangeCache = 64;
|
|
|
|
|
|
|
|
// If the instance is not used by JS and the cache is not yet full, we
|
|
|
|
// should reuse it. Otherwise, delete it.
|
|
|
|
if (sHasShutDown || aInstance.GetWrapperMaybeDead() || aInstance.GetFlags() ||
|
|
|
|
(RangeType::sCachedRanges &&
|
|
|
|
RangeType::sCachedRanges->Length() == kMaxRangeCache)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
aInstance.ClearForReuse();
|
|
|
|
|
|
|
|
if (!RangeType::sCachedRanges) {
|
|
|
|
RangeType::sCachedRanges = new nsTArray<RefPtr<RangeType>>(16);
|
|
|
|
}
|
|
|
|
RangeType::sCachedRanges->AppendElement(&aInstance);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2020-01-13 10:29:44 +00:00
|
|
|
nsINode* AbstractRange::GetClosestCommonInclusiveAncestor() const {
|
|
|
|
return mIsPositioned ? nsContentUtils::GetClosestCommonInclusiveAncestor(
|
|
|
|
mStart.Container(), mEnd.Container())
|
Bug 1444847 - part 1: Create `mozilla::dom::AbstractRange` r=smaug
This patch is based on the patch created by Makoto Kato-san.
`Range` and `StaticRange` have common base interface, `AbstractRange`.
https://dom.spec.whatwg.org/#abstractrange
This interface has simply returns `startContainer`, `endContainer`,
`startOffset`, `endOffset` and `collapsed`.
Different from the original patch's approach, this patch moves related
members in `nsRange` to `AbstractRange` since this approach avoids
virtual call cost. Additionally, this patch makes them not throw as
declared by the spec. As far as I know, the destruction cost of
`ErrorResult` may appear in profile so that we should avoid creating
the instance if we can avoid it.
Unfortunately, the instance size of `nsRange` becomes larger with this
patch. The size is changed from 176 to 184. I.e., now, `nsRange`
requires bigger chunk.
Differential Revision: https://phabricator.services.mozilla.com/D35140
--HG--
extra : moz-landing-system : lando
2019-06-28 07:46:35 +00:00
|
|
|
: nullptr;
|
|
|
|
}
|
|
|
|
|
2019-06-28 07:48:52 +00:00
|
|
|
// static
|
|
|
|
template <typename SPT, typename SRT, typename EPT, typename ERT,
|
|
|
|
typename RangeType>
|
|
|
|
nsresult AbstractRange::SetStartAndEndInternal(
|
|
|
|
const RangeBoundaryBase<SPT, SRT>& aStartBoundary,
|
|
|
|
const RangeBoundaryBase<EPT, ERT>& aEndBoundary, RangeType* aRange) {
|
|
|
|
if (NS_WARN_IF(!aStartBoundary.IsSet()) ||
|
|
|
|
NS_WARN_IF(!aEndBoundary.IsSet())) {
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsINode* newStartRoot =
|
|
|
|
RangeUtils::ComputeRootNode(aStartBoundary.Container());
|
|
|
|
if (!newStartRoot) {
|
|
|
|
return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
|
|
|
|
}
|
|
|
|
if (!aStartBoundary.IsSetAndValid()) {
|
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aStartBoundary.Container() == aEndBoundary.Container()) {
|
|
|
|
if (!aEndBoundary.IsSetAndValid()) {
|
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
}
|
|
|
|
// XXX: Offsets - handle this more efficiently.
|
|
|
|
// If the end offset is less than the start offset, this should be
|
|
|
|
// collapsed at the end offset.
|
2019-12-16 11:31:30 +00:00
|
|
|
if (*aStartBoundary.Offset(
|
|
|
|
RangeBoundaryBase<SPT, SRT>::OffsetFilter::kValidOffsets) >
|
|
|
|
*aEndBoundary.Offset(
|
|
|
|
RangeBoundaryBase<EPT, ERT>::OffsetFilter::kValidOffsets)) {
|
2019-06-28 07:48:52 +00:00
|
|
|
aRange->DoSetRange(aEndBoundary, aEndBoundary, newStartRoot);
|
|
|
|
} else {
|
|
|
|
aRange->DoSetRange(aStartBoundary, aEndBoundary, newStartRoot);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsINode* newEndRoot = RangeUtils::ComputeRootNode(aEndBoundary.Container());
|
|
|
|
if (!newEndRoot) {
|
|
|
|
return NS_ERROR_DOM_INVALID_NODE_TYPE_ERR;
|
|
|
|
}
|
|
|
|
if (!aEndBoundary.IsSetAndValid()) {
|
|
|
|
return NS_ERROR_DOM_INDEX_SIZE_ERR;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If they have different root, this should be collapsed at the end point.
|
|
|
|
if (newStartRoot != newEndRoot) {
|
|
|
|
aRange->DoSetRange(aEndBoundary, aEndBoundary, newEndRoot);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2019-12-19 13:27:28 +00:00
|
|
|
const Maybe<int32_t> pointOrder =
|
|
|
|
nsContentUtils::ComparePoints(aStartBoundary, aEndBoundary);
|
|
|
|
if (!pointOrder) {
|
|
|
|
// Safely return a value but also detected this in debug builds.
|
|
|
|
MOZ_ASSERT_UNREACHABLE();
|
|
|
|
return NS_ERROR_INVALID_ARG;
|
|
|
|
}
|
|
|
|
|
2019-06-28 07:48:52 +00:00
|
|
|
// If the end point is before the start point, this should be collapsed at
|
|
|
|
// the end point.
|
2019-12-19 13:27:28 +00:00
|
|
|
if (*pointOrder == 1) {
|
2019-06-28 07:48:52 +00:00
|
|
|
aRange->DoSetRange(aEndBoundary, aEndBoundary, newEndRoot);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, set the range as specified.
|
|
|
|
aRange->DoSetRange(aStartBoundary, aEndBoundary, newStartRoot);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
Bug 1444847 - part 1: Create `mozilla::dom::AbstractRange` r=smaug
This patch is based on the patch created by Makoto Kato-san.
`Range` and `StaticRange` have common base interface, `AbstractRange`.
https://dom.spec.whatwg.org/#abstractrange
This interface has simply returns `startContainer`, `endContainer`,
`startOffset`, `endOffset` and `collapsed`.
Different from the original patch's approach, this patch moves related
members in `nsRange` to `AbstractRange` since this approach avoids
virtual call cost. Additionally, this patch makes them not throw as
declared by the spec. As far as I know, the destruction cost of
`ErrorResult` may appear in profile so that we should avoid creating
the instance if we can avoid it.
Unfortunately, the instance size of `nsRange` becomes larger with this
patch. The size is changed from 176 to 184. I.e., now, `nsRange`
requires bigger chunk.
Differential Revision: https://phabricator.services.mozilla.com/D35140
--HG--
extra : moz-landing-system : lando
2019-06-28 07:46:35 +00:00
|
|
|
JSObject* AbstractRange::WrapObject(JSContext* aCx,
|
|
|
|
JS::Handle<JSObject*> aGivenProto) {
|
|
|
|
MOZ_CRASH("Must be overridden");
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|