/* -*- 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/. */
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
tmp->mSelections.Clear(); // Unregistering of the common inclusive ancestors would by design // also happen when the actual implementations unlink `mStart`/`mEnd`. // This may introduce additional overhead which is not needed when unlinking, // therefore this is done here beforehand. if (tmp->mRegisteredClosestCommonInclusiveAncestor) {
tmp->UnregisterClosestCommonInclusiveAncestor(true);
}
MOZ_DIAGNOSTIC_ASSERT(!tmp->isInList(), "Shouldn't be registered now that we're unlinking");
// When aMarkDesendants is true, Set // DescendantOfClosestCommonInclusiveAncestorForRangeInSelection flag for the // shadow including children of aNode. When aMarkDesendants is false, unset that // flag for the shadow including children of aNode. void UpdateDescendantsByShadowIncludingOrder(const nsIContent& aNode, bool aMarkDesendants) {
ShadowIncludingTreeIterator iter(*const_cast<nsIContent*>(&aNode));
++iter; // We don't want to mark the root node
while (iter) {
nsINode* node = *iter; if (aMarkDesendants) {
node->SetDescendantOfClosestCommonInclusiveAncestorForRangeInSelection();
} else {
node->ClearDescendantOfClosestCommonInclusiveAncestorForRangeInSelection();
}
if (node->IsClosestCommonInclusiveAncestorForRangeInSelection()) {
iter.SkipChildren(); continue;
}
++iter;
}
}
void AbstractRange::MarkDescendants(const nsINode& aNode) { // Set NodeIsDescendantOfClosestCommonInclusiveAncestorForRangeInSelection 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.IsMaybeSelected()) { // If aNode has a web-exposed shadow root, use this shadow tree and ignore // the children of aNode. if (aNode.GetShadowRootForSelection()) {
UpdateDescendantsByShadowIncludingOrder(*aNode.AsContent(), true); return;
} // don't set the Descendant bit on |aNode| itself
nsINode* node = aNode.GetNextNode(&aNode); while (node) {
node->SetDescendantOfClosestCommonInclusiveAncestorForRangeInSelection(); if (!node->IsClosestCommonInclusiveAncestorForRangeInSelection()) { if (StaticPrefs::dom_shadowdom_selection_across_boundary_enabled()) {
UpdateDescendantsByShadowIncludingOrder(*node->AsContent(), true); // sub-tree of node has been marked already
node = node->GetNextNonChildNode(&aNode);
} else {
node = node->GetNextNode(&aNode);
}
} else { // optimize: skip this sub-tree since it's marked already.
node = node->GetNextNonChildNode(&aNode);
}
}
}
}
void AbstractRange::UnmarkDescendants(const nsINode& aNode) { // Unset NodeIsDescendantOfClosestCommonInclusiveAncestorForRangeInSelection // 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
.IsDescendantOfClosestCommonInclusiveAncestorForRangeInSelection()) { // If aNode has a web-exposed shadow root, use this shadow tree and ignore // the children of aNode. if (aNode.GetShadowRootForSelection()) {
UpdateDescendantsByShadowIncludingOrder(*aNode.AsContent(), false); return;
} // we know |aNode| doesn't have any bit set
nsINode* node = aNode.GetNextNode(&aNode); while (node) {
node->ClearDescendantOfClosestCommonInclusiveAncestorForRangeInSelection(); if (!node->IsClosestCommonInclusiveAncestorForRangeInSelection()) { if (StaticPrefs::dom_shadowdom_selection_across_boundary_enabled()) {
UpdateDescendantsByShadowIncludingOrder(*node->AsContent(), false); // sub-tree has been marked already
node = node->GetNextNonChildNode(&aNode);
} else {
node = node->GetNextNode(&aNode);
}
} else { // We found an ancestor of an overlapping range, skip its descendants.
node = node->GetNextNonChildNode(&aNode);
}
}
}
}
// NOTE: If you need to change default value of members of AbstractRange, // update nsRange::Create(nsINode* aNode) and ClearForReuse() too.
AbstractRange::AbstractRange(nsINode* aNode, bool aIsDynamicRange)
: mRegisteredClosestCommonInclusiveAncestor(nullptr),
mIsPositioned(false),
mIsGenerated(false),
mCalledByJS(false),
mIsDynamicRange(aIsDynamicRange) {
mRefCnt.SetIsOnMainThread();
Init(aNode);
}
AbstractRange::~AbstractRange() = default;
void AbstractRange::Init(nsINode* aNode) {
MOZ_ASSERT(aNode, "range isn't in a document!");
mOwner = aNode->OwnerDoc();
}
// 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)) { returnfalse;
}
aInstance.ClearForReuse();
if (!RangeType::sCachedRanges) {
RangeType::sCachedRanges = new nsTArray<RefPtr<RangeType>>(16);
}
RangeType::sCachedRanges->AppendElement(&aInstance); returntrue;
}
if (MayCrossShadowBoundary() &&
aAllowCrossShadowBoundary == AllowRangeCrossShadowBoundary::Yes) { // Since both the start container and the end container are // guaranteed to be in the same composed document. // If one of the boundary is a document, use that document // as the common ancestor since both nodes. constbool oneBoundaryIsDocument =
(startContainer && startContainer->IsDocument()) ||
(endContainer && endContainer->IsDocument()); if (oneBoundaryIsDocument) {
MOZ_ASSERT_IF(
startContainer && startContainer->IsDocument(),
!endContainer || endContainer->GetComposedDoc() == startContainer);
MOZ_ASSERT_IF(
endContainer && endContainer->IsDocument(),
!startContainer || startContainer->GetComposedDoc() == endContainer);
constauto rescope = [](nsINode*& aContainer) { if (!aContainer) { return;
} // RangeBoundary allows the container to be shadow roots; When // this happens, we should use the shadow host here. if (auto* shadowRoot = ShadowRoot::FromNode(aContainer)) {
aContainer = shadowRoot->GetHost(); return;
}
};
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. if (*aStartBoundary.Offset(
RangeBoundaryBase<SPT, SRT>::OffsetFilter::kValidOffsets) >
*aEndBoundary.Offset(
RangeBoundaryBase<EPT, ERT>::OffsetFilter::kValidOffsets)) {
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;
}
// Different root if (newStartRoot != newEndRoot) { if (aRange->IsStaticRange()) { // StaticRange allows nodes in different trees, so set start and end // accordingly
aRange->DoSetRange(aStartBoundary, aEndBoundary, newEndRoot);
} else {
MOZ_ASSERT(aRange->IsDynamicRange()); // In contrast, nsRange keeps both. It has a pair of start and end // which they have been collapsed to one end, and it also may have a pair // of start and end which are the original value.
aRange->DoSetRange(aEndBoundary, aEndBoundary, newEndRoot);
// Don't create the cross shadow bounday range if the one of the roots is // an UA widget regardless whether the boundaries are allowed to cross // shadow boundary or not. if (!IsRootUAWidget(newStartRoot) && !IsRootUAWidget(newEndRoot)) {
aRange->AsDynamicRange()
->CreateOrUpdateCrossShadowBoundaryRangeIfNeeded(aStartBoundary,
aEndBoundary);
}
} return NS_OK;
}
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;
}
// If the end point is before the start point, this should be collapsed at // the end point. if (*pointOrder == 1) {
aRange->DoSetRange(aEndBoundary, aEndBoundary, newEndRoot); return NS_OK;
}
// Otherwise, set the range as specified.
aRange->DoSetRange(aStartBoundary, aEndBoundary, newStartRoot); return NS_OK;
}
void AbstractRange::UnregisterSelection(const Selection& aSelection) {
mSelections.RemoveElement(&aSelection); if (mSelections.IsEmpty() && mRegisteredClosestCommonInclusiveAncestor) {
UnregisterClosestCommonInclusiveAncestor();
MOZ_DIAGNOSTIC_ASSERT(
!mRegisteredClosestCommonInclusiveAncestor, "How can we have a registered common ancestor when we " "just unregistered?");
MOZ_DIAGNOSTIC_ASSERT(
!isInList(), "Shouldn't be registered if we have no " "mRegisteredClosestCommonInclusiveAncestor after unregistering");
}
}
#ifdef DEBUG bool found = false; for (AbstractRange* range : *ranges) { if (range == this) {
found = true; break;
}
}
MOZ_ASSERT(found, "We should be in the list on our registered common ancestor"); #endif// DEBUG
remove();
// We don't want to waste time unmarking flags on nodes that are // being unlinked anyway. if (!aIsUnlinking && ranges->isEmpty()) {
oldClosestCommonInclusiveAncestor
->ClearClosestCommonInclusiveAncestorForRangeInSelection();
UnmarkDescendants(*oldClosestCommonInclusiveAncestor);
}
oldClosestCommonInclusiveAncestor = nullptr;
}
if (newCommonAncestor) {
RegisterClosestCommonInclusiveAncestor(newCommonAncestor);
} else {
MOZ_DIAGNOSTIC_ASSERT(!mIsPositioned, "unexpected disconnected nodes");
mSelections.Clear();
MOZ_DIAGNOSTIC_ASSERT(
!mRegisteredClosestCommonInclusiveAncestor, "How can we have a registered common ancestor when we " "didn't register ourselves?");
MOZ_DIAGNOSTIC_ASSERT(!isInList(), "Shouldn't be registered if we have no " "mRegisteredClosestCommonInclusiveAncestor");
}
}
}
¤ Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.0.18Bemerkung:
(vorverarbeitet)
¤
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung ist noch experimentell.