CrossShadowBoundaryRange.h (3942B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_CrossShadowBoundaryRange_h 8 #define mozilla_dom_CrossShadowBoundaryRange_h 9 10 #include "mozilla/RangeBoundary.h" 11 #include "mozilla/RangeUtils.h" 12 #include "mozilla/dom/AbstractRange.h" 13 #include "mozilla/dom/StaticRange.h" 14 #include "nsTArray.h" 15 16 namespace mozilla { 17 class ErrorResult; 18 19 namespace dom { 20 21 class CrossShadowBoundaryRange final : public StaticRange, 22 public nsStubMutationObserver { 23 public: 24 NS_DECL_ISUPPORTS_INHERITED 25 NS_IMETHODIMP_(void) DeleteCycleCollectable(void) override; 26 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_INHERITED( 27 CrossShadowBoundaryRange, StaticRange) 28 29 CrossShadowBoundaryRange() = delete; 30 explicit CrossShadowBoundaryRange(const StaticRange& aOther) = delete; 31 32 template <typename SPT, typename SRT, typename EPT, typename ERT> 33 static already_AddRefed<CrossShadowBoundaryRange> Create( 34 const RangeBoundaryBase<SPT, SRT>& aStartBoundary, 35 const RangeBoundaryBase<EPT, ERT>& aEndBoundary, nsRange* aOwner); 36 37 void NotifyNodeBecomesShadowHost(nsINode* aNode) { 38 if (aNode == mStart.GetContainer()) { 39 mStart.NotifyParentBecomesShadowHost(); 40 } 41 42 if (aNode == mEnd.GetContainer()) { 43 mEnd.NotifyParentBecomesShadowHost(); 44 } 45 } 46 47 nsINode* GetCommonAncestor() const { return mCommonAncestor; } 48 49 // CrossShadowBoundaryRange should have a very limited usage. 50 nsresult SetStartAndEnd(nsINode* aStartContainer, uint32_t aStartOffset, 51 nsINode* aEndContainer, uint32_t aEndOffset) = delete; 52 53 template <typename SPT, typename SRT, typename EPT, typename ERT> 54 nsresult SetStartAndEnd(const RangeBoundaryBase<SPT, SRT>& aStartBoundary, 55 const RangeBoundaryBase<EPT, ERT>& aEndBoundary) { 56 return StaticRange::SetStartAndEnd(aStartBoundary, aEndBoundary); 57 } 58 59 NS_DECL_NSIMUTATIONOBSERVER_CHARACTERDATACHANGED 60 NS_DECL_NSIMUTATIONOBSERVER_CONTENTREMOVED 61 NS_DECL_NSIMUTATIONOBSERVER_PARENTCHAINCHANGED 62 63 private: 64 explicit CrossShadowBoundaryRange(nsINode* aNode, nsRange* aOwner) 65 : StaticRange(aNode, RangeBoundaryIsMutationObserved::Yes, 66 TreeKind::Flat), 67 mOwner(aOwner) {} 68 virtual ~CrossShadowBoundaryRange() = default; 69 70 /** 71 * DoSetRange() is called when `AbstractRange::SetStartAndEndInternal()` sets 72 * mStart and mEnd. 73 * 74 * @param aStartBoundary Computed start point. This must equals or be before 75 * aEndBoundary in the DOM tree order. 76 * @param aEndBoundary Computed end point. 77 * @param aRootNode The root node of aStartBoundary or aEndBoundary. 78 * It's useless to CrossShadowBoundaryRange. 79 * @param aOwner The nsRange that owns this CrossShadowBoundaryRange. 80 */ 81 template <typename SPT, typename SRT, typename EPT, typename ERT> 82 void DoSetRange(const RangeBoundaryBase<SPT, SRT>& aStartBoundary, 83 const RangeBoundaryBase<EPT, ERT>& aEndBoundary, 84 nsINode* aRootNode, nsRange* aOwner); 85 86 // This is either NULL if this CrossShadowBoundaryRange has been 87 // reset by Release() or the closest common shadow-including ancestor 88 // of mStart and mEnd. 89 nsCOMPtr<nsINode> mCommonAncestor; 90 91 static nsTArray<RefPtr<CrossShadowBoundaryRange>>* sCachedRanges; 92 93 friend class AbstractRange; 94 95 // nsRange owns CrossShadowBoundaryRange; it always outlives 96 // CrossShadowBoundaryRange, so it's safe to use raw pointer here. 97 nsRange* mOwner; 98 }; 99 } // namespace dom 100 } // namespace mozilla 101 102 #endif // #ifndef mozilla_dom_CrossShadowBoundaryRange_h