2017-06-20 16:57:08 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* 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/. */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Base class for all our document implementations.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef nsIdentifierMapEntry_h
|
|
|
|
#define nsIdentifierMapEntry_h
|
|
|
|
|
|
|
|
#include "PLDHashTable.h"
|
|
|
|
|
|
|
|
#include "mozilla/MemoryReporting.h"
|
|
|
|
#include "mozilla/Move.h"
|
|
|
|
#include "mozilla/net/ReferrerPolicy.h"
|
|
|
|
|
|
|
|
#include "nsCOMPtr.h"
|
2017-10-03 01:05:19 +03:00
|
|
|
#include "nsAtom.h"
|
2017-06-20 16:57:08 +03:00
|
|
|
#include "nsTArray.h"
|
|
|
|
#include "nsTHashtable.h"
|
|
|
|
|
|
|
|
class nsIContent;
|
2017-12-20 08:46:01 +03:00
|
|
|
class nsContentList;
|
|
|
|
class nsBaseContentList;
|
2017-06-20 16:57:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Right now our identifier map entries contain information for 'name'
|
|
|
|
* and 'id' mappings of a given string. This is so that
|
|
|
|
* nsHTMLDocument::ResolveName only has to do one hash lookup instead
|
|
|
|
* of two. It's not clear whether this still matters for performance.
|
|
|
|
*
|
|
|
|
* We also store the document.all result list here. This is mainly so that
|
|
|
|
* when all elements with the given ID are removed and we remove
|
|
|
|
* the ID's nsIdentifierMapEntry, the document.all result is released too.
|
|
|
|
* Perhaps the document.all results should have their own hashtable
|
|
|
|
* in nsHTMLDocument.
|
|
|
|
*/
|
|
|
|
class nsIdentifierMapEntry : public PLDHashEntryHdr
|
|
|
|
{
|
2017-12-20 08:46:01 +03:00
|
|
|
typedef mozilla::dom::Element Element;
|
|
|
|
typedef mozilla::net::ReferrerPolicy ReferrerPolicy;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @see nsIDocument::IDTargetObserver, this is just here to avoid include
|
|
|
|
* hell.
|
|
|
|
*/
|
|
|
|
typedef bool (* IDTargetObserver)(Element* aOldElement,
|
|
|
|
Element* aNewelement, void* aData);
|
2017-06-20 16:57:08 +03:00
|
|
|
public:
|
|
|
|
struct AtomOrString
|
|
|
|
{
|
2017-10-03 01:05:19 +03:00
|
|
|
MOZ_IMPLICIT AtomOrString(nsAtom* aAtom) : mAtom(aAtom) {}
|
2017-06-20 16:57:08 +03:00
|
|
|
MOZ_IMPLICIT AtomOrString(const nsAString& aString) : mString(aString) {}
|
|
|
|
AtomOrString(const AtomOrString& aOther)
|
|
|
|
: mAtom(aOther.mAtom)
|
|
|
|
, mString(aOther.mString)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
AtomOrString(AtomOrString&& aOther)
|
|
|
|
: mAtom(aOther.mAtom.forget())
|
|
|
|
, mString(aOther.mString)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-10-03 01:05:19 +03:00
|
|
|
RefPtr<nsAtom> mAtom;
|
2017-06-20 16:57:08 +03:00
|
|
|
const nsString mString;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef const AtomOrString& KeyType;
|
|
|
|
typedef const AtomOrString* KeyTypePointer;
|
|
|
|
|
2017-12-20 08:46:01 +03:00
|
|
|
explicit nsIdentifierMapEntry(const AtomOrString& aKey);
|
|
|
|
explicit nsIdentifierMapEntry(const AtomOrString* aKey);
|
|
|
|
nsIdentifierMapEntry(nsIdentifierMapEntry&& aOther);
|
2017-06-20 16:57:08 +03:00
|
|
|
~nsIdentifierMapEntry();
|
|
|
|
|
|
|
|
nsString GetKeyAsString() const
|
|
|
|
{
|
|
|
|
if (mKey.mAtom) {
|
|
|
|
return nsAtomString(mKey.mAtom);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mKey.mString;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool KeyEquals(const KeyTypePointer aOtherKey) const
|
|
|
|
{
|
|
|
|
if (mKey.mAtom) {
|
|
|
|
if (aOtherKey->mAtom) {
|
|
|
|
return mKey.mAtom == aOtherKey->mAtom;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mKey.mAtom->Equals(aOtherKey->mString);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aOtherKey->mAtom) {
|
|
|
|
return aOtherKey->mAtom->Equals(mKey.mString);
|
|
|
|
}
|
|
|
|
|
|
|
|
return mKey.mString.Equals(aOtherKey->mString);
|
|
|
|
}
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType aKey) { return &aKey; }
|
|
|
|
|
|
|
|
static PLDHashNumber HashKey(const KeyTypePointer aKey)
|
|
|
|
{
|
|
|
|
return aKey->mAtom ?
|
|
|
|
aKey->mAtom->hash() : mozilla::HashString(aKey->mString);
|
|
|
|
}
|
|
|
|
|
|
|
|
enum { ALLOW_MEMMOVE = false };
|
|
|
|
|
|
|
|
void AddNameElement(nsINode* aDocument, Element* aElement);
|
|
|
|
void RemoveNameElement(Element* aElement);
|
|
|
|
bool IsEmpty();
|
|
|
|
nsBaseContentList* GetNameContentList() {
|
|
|
|
return mNameContentList;
|
|
|
|
}
|
2017-12-20 08:46:01 +03:00
|
|
|
bool HasNameElement() const;
|
2017-06-20 16:57:08 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the element if we know the element associated with this
|
|
|
|
* id. Otherwise returns null.
|
|
|
|
*/
|
|
|
|
Element* GetIdElement();
|
|
|
|
/**
|
|
|
|
* Returns the list of all elements associated with this id.
|
|
|
|
*/
|
|
|
|
const nsTArray<Element*>& GetIdElements() const {
|
|
|
|
return mIdContentList;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* If this entry has a non-null image element set (using SetImageElement),
|
|
|
|
* the image element will be returned, otherwise the same as GetIdElement().
|
|
|
|
*/
|
|
|
|
Element* GetImageIdElement();
|
|
|
|
/**
|
|
|
|
* This can fire ID change callbacks.
|
|
|
|
* @return true if the content could be added, false if we failed due
|
|
|
|
* to OOM.
|
|
|
|
*/
|
|
|
|
bool AddIdElement(Element* aElement);
|
|
|
|
/**
|
|
|
|
* This can fire ID change callbacks.
|
|
|
|
*/
|
|
|
|
void RemoveIdElement(Element* aElement);
|
|
|
|
/**
|
|
|
|
* Set the image element override for this ID. This will be returned by
|
|
|
|
* GetIdElement(true) if non-null.
|
|
|
|
*/
|
|
|
|
void SetImageElement(Element* aElement);
|
|
|
|
bool HasIdElementExposedAsHTMLDocumentProperty();
|
|
|
|
|
|
|
|
bool HasContentChangeCallback() { return mChangeCallbacks != nullptr; }
|
2017-12-20 08:46:01 +03:00
|
|
|
void AddContentChangeCallback(IDTargetObserver aCallback,
|
2017-06-20 16:57:08 +03:00
|
|
|
void* aData, bool aForImage);
|
2017-12-20 08:46:01 +03:00
|
|
|
void RemoveContentChangeCallback(IDTargetObserver aCallback,
|
2017-06-20 16:57:08 +03:00
|
|
|
void* aData, bool aForImage);
|
|
|
|
|
|
|
|
void Traverse(nsCycleCollectionTraversalCallback* aCallback);
|
|
|
|
|
|
|
|
struct ChangeCallback {
|
2017-12-20 08:46:01 +03:00
|
|
|
IDTargetObserver mCallback;
|
2017-06-20 16:57:08 +03:00
|
|
|
void* mData;
|
|
|
|
bool mForImage;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ChangeCallbackEntry : public PLDHashEntryHdr {
|
|
|
|
typedef const ChangeCallback KeyType;
|
|
|
|
typedef const ChangeCallback* KeyTypePointer;
|
|
|
|
|
|
|
|
explicit ChangeCallbackEntry(const ChangeCallback* aKey) :
|
|
|
|
mKey(*aKey) { }
|
|
|
|
ChangeCallbackEntry(const ChangeCallbackEntry& toCopy) :
|
|
|
|
mKey(toCopy.mKey) { }
|
|
|
|
|
|
|
|
KeyType GetKey() const { return mKey; }
|
|
|
|
bool KeyEquals(KeyTypePointer aKey) const {
|
|
|
|
return aKey->mCallback == mKey.mCallback &&
|
|
|
|
aKey->mData == mKey.mData &&
|
|
|
|
aKey->mForImage == mKey.mForImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
static KeyTypePointer KeyToPointer(KeyType& aKey) { return &aKey; }
|
|
|
|
static PLDHashNumber HashKey(KeyTypePointer aKey)
|
|
|
|
{
|
|
|
|
return mozilla::HashGeneric(aKey->mCallback, aKey->mData);
|
|
|
|
}
|
|
|
|
enum { ALLOW_MEMMOVE = true };
|
|
|
|
|
|
|
|
ChangeCallback mKey;
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t SizeOfExcludingThis(mozilla::MallocSizeOf aMallocSizeOf) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsIdentifierMapEntry(const nsIdentifierMapEntry& aOther) = delete;
|
|
|
|
nsIdentifierMapEntry& operator=(const nsIdentifierMapEntry& aOther) = delete;
|
|
|
|
|
|
|
|
void FireChangeCallbacks(Element* aOldElement, Element* aNewElement,
|
|
|
|
bool aImageOnly = false);
|
|
|
|
|
|
|
|
AtomOrString mKey;
|
|
|
|
// empty if there are no elements with this ID.
|
|
|
|
// The elements are stored as weak pointers.
|
|
|
|
AutoTArray<Element*, 1> mIdContentList;
|
|
|
|
RefPtr<nsBaseContentList> mNameContentList;
|
|
|
|
nsAutoPtr<nsTHashtable<ChangeCallbackEntry> > mChangeCallbacks;
|
|
|
|
RefPtr<Element> mImageElement;
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // #ifndef nsIdentifierMapEntry_h
|