summaryrefslogtreecommitdiffstats
path: root/dom/base/HighlightRegistry.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'dom/base/HighlightRegistry.cpp')
-rw-r--r--dom/base/HighlightRegistry.cpp22
1 files changed, 16 insertions, 6 deletions
diff --git a/dom/base/HighlightRegistry.cpp b/dom/base/HighlightRegistry.cpp
index 035aadb78a..755977286f 100644
--- a/dom/base/HighlightRegistry.cpp
+++ b/dom/base/HighlightRegistry.cpp
@@ -133,18 +133,28 @@ void HighlightRegistry::AddHighlightSelectionsToFrameSelection() {
void HighlightRegistry::Set(const nsAString& aKey, Highlight& aValue,
ErrorResult& aRv) {
+ // manually check if the highlight `aKey` is already registered to be able to
+ // provide a fast path later that avoids calling `std::find_if()`.
+ const bool highlightAlreadyPresent =
+ HighlightRegistry_Binding::MaplikeHelpers::Has(this, aKey, aRv);
+ if (aRv.Failed()) {
+ return;
+ }
HighlightRegistry_Binding::MaplikeHelpers::Set(this, aKey, aValue, aRv);
if (aRv.Failed()) {
return;
}
RefPtr<nsFrameSelection> frameSelection = GetFrameSelection();
RefPtr<nsAtom> highlightNameAtom = NS_AtomizeMainThread(aKey);
- auto foundIter =
- std::find_if(mHighlightsOrdered.begin(), mHighlightsOrdered.end(),
- [&highlightNameAtom](auto const& aElm) {
- return aElm.first() == highlightNameAtom;
- });
- if (foundIter != mHighlightsOrdered.end()) {
+ if (highlightAlreadyPresent) {
+ // If the highlight named `aKey` was present before, replace its value.
+ auto foundIter =
+ std::find_if(mHighlightsOrdered.begin(), mHighlightsOrdered.end(),
+ [&highlightNameAtom](auto const& aElm) {
+ return aElm.first() == highlightNameAtom;
+ });
+ MOZ_ASSERT(foundIter != mHighlightsOrdered.end(),
+ "webIDL maplike and DOM mirror are not in sync");
foundIter->second()->RemoveFromHighlightRegistry(*this, *highlightNameAtom);
if (frameSelection) {
frameSelection->RemoveHighlightSelection(highlightNameAtom);