diff options
author | Matt A. Tobin <email@mattatobin.com> | 2020-04-17 05:28:43 -0400 |
---|---|---|
committer | Matt A. Tobin <email@mattatobin.com> | 2020-04-17 05:28:43 -0400 |
commit | 940d191ef8b61309f4ea83d0fea77828f361251b (patch) | |
tree | 328a57b6d2e2b018343e2c5d20e0602d613f0e19 /dom/base/nsRange.h | |
parent | ef689a705ffdd79cdeeca8e68438b4ad6597f38d (diff) | |
download | UXP-940d191ef8b61309f4ea83d0fea77828f361251b.tar UXP-940d191ef8b61309f4ea83d0fea77828f361251b.tar.gz UXP-940d191ef8b61309f4ea83d0fea77828f361251b.tar.lz UXP-940d191ef8b61309f4ea83d0fea77828f361251b.tar.xz UXP-940d191ef8b61309f4ea83d0fea77828f361251b.zip |
Bug 1367683 - Optimize initializing nsRange
Tag #1375
Diffstat (limited to 'dom/base/nsRange.h')
-rw-r--r-- | dom/base/nsRange.h | 57 |
1 files changed, 50 insertions, 7 deletions
diff --git a/dom/base/nsRange.h b/dom/base/nsRange.h index 4b35c749a..70911338d 100644 --- a/dom/base/nsRange.h +++ b/dom/base/nsRange.h @@ -148,19 +148,61 @@ public: nsINode* GetCommonAncestor() const; void Reset(); + + /** + * SetStart() and SetEnd() sets start point or end point separately. + * However, this is expensive especially when it's a range of Selection. + * When you set both start and end of a range, you should use + * SetStartAndEnd() instead. + */ nsresult SetStart(nsINode* aParent, int32_t aOffset); nsresult SetEnd(nsINode* aParent, int32_t aOffset); + already_AddRefed<nsRange> CloneRange() const; - nsresult Set(nsINode* aStartParent, int32_t aStartOffset, - nsINode* aEndParent, int32_t aEndOffset) + /** + * SetStartAndEnd() works similar to call both SetStart() and SetEnd(). + * Different from calls them separately, this does nothing if either + * the start point or the end point is invalid point. + * If the specified start point is after the end point, the range will be + * collapsed at the end point. Similarly, if they are in different root, + * the range will be collapsed at the end point. + */ + nsresult SetStartAndEnd(nsINode* aStartParent, int32_t aStartOffset, + nsINode* aEndParent, int32_t aEndOffset); + + /** + * CollapseTo() works similar to call both SetStart() and SetEnd() with + * same node and offset. This just calls SetStartAndParent() to set + * collapsed range at aParent and aOffset. + */ + nsresult CollapseTo(nsINode* aParent, int32_t aOffset) { - // If this starts being hot, we may be able to optimize this a bit, - // but for now just set start and end separately. - nsresult rv = SetStart(aStartParent, aStartOffset); - NS_ENSURE_SUCCESS(rv, rv); + return SetStartAndEnd(aParent, aOffset, aParent, aOffset); + } - return SetEnd(aEndParent, aEndOffset); + /** + * Retrieves node and offset for setting start or end of a range to + * before or after aNode. + */ + static nsINode* GetParentAndOffsetAfter(nsINode* aNode, int32_t* aOffset) + { + MOZ_ASSERT(aNode); + MOZ_ASSERT(aOffset); + nsINode* parentNode = aNode->GetParentNode(); + *aOffset = parentNode ? parentNode->IndexOf(aNode) : -1; + if (*aOffset >= 0) { + (*aOffset)++; + } + return parentNode; + } + static nsINode* GetParentAndOffsetBefore(nsINode* aNode, int32_t* aOffset) + { + MOZ_ASSERT(aNode); + MOZ_ASSERT(aOffset); + nsINode* parentNode = aNode->GetParentNode(); + *aOffset = parentNode ? parentNode->IndexOf(aNode) : -1; + return parentNode; } NS_IMETHOD GetUsedFontFaces(nsIDOMFontFaceList** aResult); @@ -296,6 +338,7 @@ protected: void RegisterCommonAncestor(nsINode* aNode); void UnregisterCommonAncestor(nsINode* aNode); nsINode* IsValidBoundary(nsINode* aNode); + static bool IsValidOffset(nsINode* aNode, int32_t aOffset); // CharacterDataChanged set aNotInsertedYet to true to disable an assertion // and suppress re-registering a range common ancestor node since |