Bug 1943179 - Add [[nodiscard]] to ContentIterator methods that return nsresult. r=masayuki

Differential Revision: https://phabricator.services.mozilla.com/D235361
This commit is contained in:
Jan-Niklas Jaeschke
2025-01-28 15:54:36 +00:00
parent 4e47d7a16d
commit d85b643e72
3 changed files with 41 additions and 25 deletions

View File

@@ -39,13 +39,15 @@ class ContentIteratorBase {
* (https://dom.spec.whatwg.org/#concept-tree-inclusive-descendant) of * (https://dom.spec.whatwg.org/#concept-tree-inclusive-descendant) of
* aRoot. * aRoot.
*/ */
virtual nsresult Init(nsINode* aRoot); [[nodiscard]] virtual nsresult Init(nsINode* aRoot);
virtual nsresult Init(dom::AbstractRange* aRange); [[nodiscard]] virtual nsresult Init(dom::AbstractRange* aRange);
virtual nsresult Init(nsINode* aStartContainer, uint32_t aStartOffset, [[nodiscard]] virtual nsresult Init(nsINode* aStartContainer,
nsINode* aEndContainer, uint32_t aEndOffset); uint32_t aStartOffset,
virtual nsresult Init(const RawRangeBoundary& aStart, nsINode* aEndContainer,
const RawRangeBoundary& aEnd); uint32_t aEndOffset);
[[nodiscard]] virtual nsresult Init(const RawRangeBoundary& aStart,
const RawRangeBoundary& aEnd);
virtual void First(); virtual void First();
virtual void Last(); virtual void Last();
@@ -56,7 +58,7 @@ class ContentIteratorBase {
bool IsDone() const { return !mCurNode; } bool IsDone() const { return !mCurNode; }
virtual nsresult PositionAt(nsINode* aCurNode); [[nodiscard]] virtual nsresult PositionAt(nsINode* aCurNode);
protected: protected:
enum class Order { enum class Order {
@@ -76,8 +78,8 @@ class ContentIteratorBase {
* - aStartOffset and aEndOffset are valid for its container. * - aStartOffset and aEndOffset are valid for its container.
* - The start point and the end point are in document order. * - The start point and the end point are in document order.
*/ */
nsresult InitInternal(const RawRangeBoundary& aStart, [[nodiscard]] nsresult InitInternal(const RawRangeBoundary& aStart,
const RawRangeBoundary& aEnd); const RawRangeBoundary& aEnd);
// Recursively get the deepest first/last child of aRoot. This will return // Recursively get the deepest first/last child of aRoot. This will return
// aRoot itself if it has no children. // aRoot itself if it has no children.
@@ -108,11 +110,11 @@ class ContentIteratorBase {
void SetEmpty(); void SetEmpty();
NodeType mCurNode; NodeType mCurNode = nullptr;
NodeType mFirst; NodeType mFirst = nullptr;
NodeType mLast; NodeType mLast = nullptr;
// See <https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor>. // See <https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor>.
NodeType mClosestCommonInclusiveAncestor; NodeType mClosestCommonInclusiveAncestor = nullptr;
Maybe<nsMutationGuard> mMutationGuard; Maybe<nsMutationGuard> mMutationGuard;
Maybe<JS::AutoAssertNoGC> mAssertNoGC; Maybe<JS::AutoAssertNoGC> mAssertNoGC;
@@ -227,9 +229,9 @@ class ContentSubtreeIterator final : public SafeContentIteratorBase {
/** /**
* Not supported. * Not supported.
*/ */
virtual nsresult Init(nsINode* aRoot) override; [[nodiscard]] virtual nsresult Init(nsINode* aRoot) override;
virtual nsresult Init(dom::AbstractRange* aRange) override; [[nodiscard]] virtual nsresult Init(dom::AbstractRange* aRange) override;
/** /**
* Initialize the iterator with aRange that does correct things * Initialize the iterator with aRange that does correct things
@@ -252,11 +254,15 @@ class ContentSubtreeIterator final : public SafeContentIteratorBase {
* Examples of what nodes will be returned can be found * Examples of what nodes will be returned can be found
* at test_content_iterator_subtree_shadow_tree.html. * at test_content_iterator_subtree_shadow_tree.html.
*/ */
nsresult InitWithAllowCrossShadowBoundary(dom::AbstractRange* aRange); [[nodiscard]] nsresult InitWithAllowCrossShadowBoundary(
virtual nsresult Init(nsINode* aStartContainer, uint32_t aStartOffset, dom::AbstractRange* aRange);
nsINode* aEndContainer, uint32_t aEndOffset) override; [[nodiscard]] virtual nsresult Init(nsINode* aStartContainer,
virtual nsresult Init(const RawRangeBoundary& aStartBoundary, uint32_t aStartOffset,
const RawRangeBoundary& aEndBoundary) override; nsINode* aEndContainer,
uint32_t aEndOffset) override;
[[nodiscard]] virtual nsresult Init(
const RawRangeBoundary& aStartBoundary,
const RawRangeBoundary& aEndBoundary) override;
void Next() override; void Next() override;
void Prev() override; void Prev() override;
@@ -265,7 +271,7 @@ class ContentSubtreeIterator final : public SafeContentIteratorBase {
// Must override these because we don't do PositionAt // Must override these because we don't do PositionAt
void Last() override; void Last() override;
nsresult PositionAt(nsINode* aCurNode) override; [[nodiscard]] nsresult PositionAt(nsINode* aCurNode) override;
friend void ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback&, friend void ImplCycleCollectionTraverse(nsCycleCollectionTraversalCallback&,
ContentSubtreeIterator&, const char*, ContentSubtreeIterator&, const char*,
@@ -301,7 +307,7 @@ class ContentSubtreeIterator final : public SafeContentIteratorBase {
/** /**
* Callers must guarantee that mRange isn't nullptr and is positioned. * Callers must guarantee that mRange isn't nullptr and is positioned.
*/ */
nsresult InitWithRange(); [[nodiscard]] nsresult InitWithRange();
// Returns the highest inclusive ancestor of aNode that's in the range // Returns the highest inclusive ancestor of aNode that's in the range
// (possibly aNode itself). Returns null if aNode is null, or is not itself // (possibly aNode itself). Returns null if aNode is null, or is not itself

View File

@@ -346,7 +346,10 @@ const nsTHashSet<const nsINode*>& SelectionNodeCache::MaybeCollect(
const nsINode* endContainer = const nsINode* endContainer =
endRef.IsEndOfContainer() ? nullptr : endRef.Container(); endRef.IsEndOfContainer() ? nullptr : endRef.Container();
UnsafePreContentIterator iter; UnsafePreContentIterator iter;
iter.Init(range); nsresult rv = iter.Init(range);
if (NS_FAILED(rv)) {
continue;
}
for (; !iter.IsDone(); iter.Next()) { for (; !iter.IsDone(); iter.Next()) {
if (const nsINode* node = iter.GetCurrentNode()) { if (const nsINode* node = iter.GetCurrentNode()) {
// Only collect start and end container if they are fully // Only collect start and end container if they are fully
@@ -1944,7 +1947,10 @@ nsresult Selection::SelectFrames(nsPresContext* aPresContext,
} }
ContentSubtreeIterator subtreeIter; ContentSubtreeIterator subtreeIter;
subtreeIter.InitWithAllowCrossShadowBoundary(&aRange); nsresult rv = subtreeIter.InitWithAllowCrossShadowBoundary(&aRange);
if (NS_FAILED(rv)) {
return rv;
}
if (isFirstContentTextNode && !subtreeIter.IsDone() && if (isFirstContentTextNode && !subtreeIter.IsDone() &&
subtreeIter.GetCurrentNode() == startContent) { subtreeIter.GetCurrentNode() == startContent) {
subtreeIter.Next(); // first content has already been handled. subtreeIter.Next(); // first content has already been handled.

View File

@@ -3539,7 +3539,11 @@ already_AddRefed<Element> HTMLEditor::GetSelectedElement(const nsAtom* aTagName,
} }
PostContentIterator postOrderIter; PostContentIterator postOrderIter;
postOrderIter.Init(firstRange); nsresult rv = postOrderIter.Init(firstRange);
if (NS_FAILED(rv)) {
aRv.Throw(rv);
return nullptr;
}
RefPtr<Element> lastElementInRange; RefPtr<Element> lastElementInRange;
for (nsINode* lastNodeInRange = nullptr; !postOrderIter.IsDone(); for (nsINode* lastNodeInRange = nullptr; !postOrderIter.IsDone();