Bug 1460435 - Move ID observing from Document to DocumentOrShadowRoot, r=peterv

This commit is contained in:
Olli Pettay 2018-05-14 21:39:53 +03:00
parent 5b0c2d18cb
commit 6845fe60f6
4 changed files with 91 additions and 86 deletions

View File

@ -283,5 +283,48 @@ DocumentOrShadowRoot::ElementsFromPointHelper(float aX, float aY,
}
}
Element*
DocumentOrShadowRoot::AddIDTargetObserver(nsAtom* aID,
IDTargetObserver aObserver,
void* aData, bool aForImage)
{
nsDependentAtomString id(aID);
if (!CheckGetElementByIdArg(id)) {
return nullptr;
}
nsIdentifierMapEntry* entry = mIdentifierMap.PutEntry(aID);
NS_ENSURE_TRUE(entry, nullptr);
entry->AddContentChangeCallback(aObserver, aData, aForImage);
return aForImage ? entry->GetImageIdElement() : entry->GetIdElement();
}
void
DocumentOrShadowRoot::RemoveIDTargetObserver(nsAtom* aID,
IDTargetObserver aObserver,
void* aData, bool aForImage)
{
nsDependentAtomString id(aID);
if (!CheckGetElementByIdArg(id)) {
return;
}
nsIdentifierMapEntry* entry = mIdentifierMap.GetEntry(aID);
if (!entry) {
return;
}
entry->RemoveContentChangeCallback(aObserver, aData, aForImage);
}
void
DocumentOrShadowRoot::ReportEmptyGetElementByIdArg()
{
nsContentUtils::ReportEmptyGetElementByIdArg(AsNode().OwnerDoc());
}
}
}

View File

@ -139,6 +139,54 @@ public:
void ElementsFromPointHelper(float aX, float aY, uint32_t aFlags,
nsTArray<RefPtr<mozilla::dom::Element>>& aElements);
/**
* This gets fired when the element that an id refers to changes.
* This fires at difficult times. It is generally not safe to do anything
* which could modify the DOM in any way. Use
* nsContentUtils::AddScriptRunner.
* @return true to keep the callback in the callback set, false
* to remove it.
*/
typedef bool (* IDTargetObserver)(Element* aOldElement,
Element* aNewelement, void* aData);
/**
* Add an IDTargetObserver for a specific ID. The IDTargetObserver
* will be fired whenever the content associated with the ID changes
* in the future. If aForImage is true, mozSetImageElement can override
* what content is associated with the ID. In that case the IDTargetObserver
* will be notified at those times when the result of LookupImageElement
* changes.
* At most one (aObserver, aData, aForImage) triple can be
* registered for each ID.
* @return the content currently associated with the ID.
*/
Element* AddIDTargetObserver(nsAtom* aID, IDTargetObserver aObserver,
void* aData, bool aForImage);
/**
* Remove the (aObserver, aData, aForImage) triple for a specific ID, if
* registered.
*/
void RemoveIDTargetObserver(nsAtom* aID, IDTargetObserver aObserver,
void* aData, bool aForImage);
/**
* Check that aId is not empty and log a message to the console
* service if it is.
* @returns true if aId looks correct, false otherwise.
*/
inline bool CheckGetElementByIdArg(const nsAString& aId)
{
if (aId.IsEmpty()) {
ReportEmptyGetElementByIdArg();
return false;
}
return true;
}
void ReportEmptyGetElementByIdArg();
protected:
nsIContent* Retarget(nsIContent* aContent) const;

View File

@ -5101,45 +5101,6 @@ nsDocument::BeginLoad()
NS_DOCUMENT_NOTIFY_OBSERVERS(BeginLoad, (this));
}
void
nsIDocument::ReportEmptyGetElementByIdArg()
{
nsContentUtils::ReportEmptyGetElementByIdArg(this);
}
Element*
nsIDocument::AddIDTargetObserver(nsAtom* aID, IDTargetObserver aObserver,
void* aData, bool aForImage)
{
nsDependentAtomString id(aID);
if (!CheckGetElementByIdArg(id))
return nullptr;
nsIdentifierMapEntry* entry = mIdentifierMap.PutEntry(aID);
NS_ENSURE_TRUE(entry, nullptr);
entry->AddContentChangeCallback(aObserver, aData, aForImage);
return aForImage ? entry->GetImageIdElement() : entry->GetIdElement();
}
void
nsIDocument::RemoveIDTargetObserver(nsAtom* aID, IDTargetObserver aObserver,
void* aData, bool aForImage)
{
nsDependentAtomString id(aID);
if (!CheckGetElementByIdArg(id))
return;
nsIdentifierMapEntry* entry = mIdentifierMap.GetEntry(aID);
if (!entry) {
return;
}
entry->RemoveContentChangeCallback(aObserver, aData, aForImage);
}
void
nsIDocument::MozSetImageElement(const nsAString& aImageElementId,
Element* aElement)

View File

@ -800,53 +800,6 @@ public:
mCharacterSetSource = aCharsetSource;
}
/**
* This gets fired when the element that an id refers to changes.
* This fires at difficult times. It is generally not safe to do anything
* which could modify the DOM in any way. Use
* nsContentUtils::AddScriptRunner.
* @return true to keep the callback in the callback set, false
* to remove it.
*/
typedef bool (* IDTargetObserver)(Element* aOldElement,
Element* aNewelement, void* aData);
/**
* Add an IDTargetObserver for a specific ID. The IDTargetObserver
* will be fired whenever the content associated with the ID changes
* in the future. If aForImage is true, mozSetImageElement can override
* what content is associated with the ID. In that case the IDTargetObserver
* will be notified at those times when the result of LookupImageElement
* changes.
* At most one (aObserver, aData, aForImage) triple can be
* registered for each ID.
* @return the content currently associated with the ID.
*/
Element* AddIDTargetObserver(nsAtom* aID, IDTargetObserver aObserver,
void* aData, bool aForImage);
/**
* Remove the (aObserver, aData, aForImage) triple for a specific ID, if
* registered.
*/
void RemoveIDTargetObserver(nsAtom* aID, IDTargetObserver aObserver,
void* aData, bool aForImage);
/**
* Check that aId is not empty and log a message to the console
* service if it is.
* @returns true if aId looks correct, false otherwise.
*/
inline bool CheckGetElementByIdArg(const nsAString& aId)
{
if (aId.IsEmpty()) {
ReportEmptyGetElementByIdArg();
return false;
}
return true;
}
void ReportEmptyGetElementByIdArg();
/**
* Get the Content-Type of this document.
*/