/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /* vim:set ts=2 sw=2 sts=2 et cindent: */ /* 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(HighlightRegistry)
NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument) for (autoconst& iter : tmp->mHighlightsOrdered) {
iter.second()->RemoveFromHighlightRegistry(*tmp, *iter.first());
}
NS_IMPL_CYCLE_COLLECTION_UNLINK(mHighlightsOrdered)
NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_UNLINK_END
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(HighlightRegistry)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument) for (size_t i = 0; i < tmp->mHighlightsOrdered.Length(); ++i) {
NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mHighlightsOrdered[i].second())
}
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
void HighlightRegistry::MaybeAddRangeToHighlightSelection(
AbstractRange& aRange, Highlight& aHighlight) {
RefPtr<nsFrameSelection> frameSelection = GetFrameSelection(); if (!frameSelection) { return;
}
MOZ_ASSERT(frameSelection->GetPresShell()); if (!frameSelection->GetPresShell()->GetDocument() ||
frameSelection->GetPresShell()->GetDocument() !=
aRange.GetComposedDocOfContainers()) { // ranges that belong to a different document must not be added. return;
} for (autoconst& iter : mHighlightsOrdered) { if (iter.second() != &aHighlight) { continue;
}
void HighlightRegistry::AddHighlightSelectionsToFrameSelection() { if (mHighlightsOrdered.IsEmpty()) { return;
}
RefPtr<nsFrameSelection> frameSelection = GetFrameSelection(); if (!frameSelection) { return;
} for (autoconst& iter : mHighlightsOrdered) {
RefPtr<nsAtom> highlightName = iter.first();
RefPtr<Highlight> highlight = iter.second();
frameSelection->AddHighlightSelection(highlightName, *highlight);
}
}
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()`. constbool 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); if (highlightAlreadyPresent) { // If the highlight named `aKey` was present before, replace its value. auto foundIter =
std::find_if(mHighlightsOrdered.begin(), mHighlightsOrdered.end(),
[&highlightNameAtom](autoconst& 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);
}
foundIter->second() = &aValue;
} else {
mHighlightsOrdered.AppendElement(
CompactPair<RefPtr<nsAtom>, RefPtr<Highlight>>(highlightNameAtom,
&aValue));
}
aValue.AddToHighlightRegistry(*this, *highlightNameAtom); if (frameSelection) {
frameSelection->AddHighlightSelection(highlightNameAtom, aValue);
}
}
void HighlightRegistry::Clear(ErrorResult& aRv) {
HighlightRegistry_Binding::MaplikeHelpers::Clear(this, aRv); if (aRv.Failed()) { return;
} auto frameSelection = GetFrameSelection();
AutoFrameSelectionBatcher batcher(__FUNCTION__);
batcher.AddFrameSelection(frameSelection); for (autoconst& iter : mHighlightsOrdered) { const RefPtr<nsAtom>& highlightName = iter.first(); const RefPtr<Highlight>& highlight = iter.second();
highlight->RemoveFromHighlightRegistry(*this, *highlightName); if (frameSelection) { // The selection batcher makes sure that no script is run in this call. // However, `nsFrameSelection::RemoveHighlightSelection` is marked // `MOZ_CAN_RUN_SCRIPT`, therefore `MOZ_KnownLive` is needed regardless.
frameSelection->RemoveHighlightSelection(MOZ_KnownLive(highlightName));
}
}
mHighlightsOrdered.Clear();
}
bool HighlightRegistry::Delete(const nsAString& aKey, ErrorResult& aRv) { if (!HighlightRegistry_Binding::MaplikeHelpers::Delete(this, aKey, aRv)) { returnfalse;
}
RefPtr<nsAtom> highlightNameAtom = NS_AtomizeMainThread(aKey); auto foundIter =
std::find_if(mHighlightsOrdered.cbegin(), mHighlightsOrdered.cend(),
[&highlightNameAtom](autoconst& aElm) { return aElm.first() == highlightNameAtom;
});
MOZ_ASSERT(foundIter != mHighlightsOrdered.cend(), "HighlightRegistry: maplike and internal data are out of sync!");
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.