DocumentOrderedMap.h [plain text]
#pragma once
#include <wtf/HashMap.h>
#include <wtf/HashSet.h>
#include <wtf/Vector.h>
#include <wtf/text/AtomicStringImpl.h>
namespace WebCore {
class Element;
class HTMLImageElement;
class HTMLLabelElement;
class HTMLMapElement;
class TreeScope;
class DocumentOrderedMap {
WTF_MAKE_FAST_ALLOCATED;
public:
void add(const AtomicStringImpl&, Element&, const TreeScope&);
void remove(const AtomicStringImpl&, Element&);
void clear();
bool contains(const AtomicStringImpl&) const;
bool containsSingle(const AtomicStringImpl&) const;
bool containsMultiple(const AtomicStringImpl&) const;
Element* getElementById(const AtomicStringImpl&, const TreeScope&) const;
Element* getElementByName(const AtomicStringImpl&, const TreeScope&) const;
HTMLMapElement* getElementByMapName(const AtomicStringImpl&, const TreeScope&) const;
HTMLImageElement* getElementByUsemap(const AtomicStringImpl&, const TreeScope&) const;
HTMLLabelElement* getElementByLabelForAttribute(const AtomicStringImpl&, const TreeScope&) const;
Element* getElementByWindowNamedItem(const AtomicStringImpl&, const TreeScope&) const;
Element* getElementByDocumentNamedItem(const AtomicStringImpl&, const TreeScope&) const;
const Vector<Element*>* getAllElementsById(const AtomicStringImpl&, const TreeScope&) const;
private:
template <typename KeyMatchingFunction>
Element* get(const AtomicStringImpl&, const TreeScope&, const KeyMatchingFunction&) const;
struct MapEntry {
MapEntry() { }
explicit MapEntry(Element* firstElement)
: element(firstElement)
, count(1)
{ }
Element* element { nullptr };
unsigned count { 0 };
Vector<Element*> orderedList;
#if !ASSERT_DISABLED || ENABLE(SECURITY_ASSERTIONS)
HashSet<Element*> registeredElements;
#endif
};
typedef HashMap<const AtomicStringImpl*, MapEntry> Map;
mutable Map m_map;
};
inline bool DocumentOrderedMap::containsSingle(const AtomicStringImpl& id) const
{
auto it = m_map.find(&id);
return it != m_map.end() && it->value.count == 1;
}
inline bool DocumentOrderedMap::contains(const AtomicStringImpl& id) const
{
return m_map.contains(&id);
}
inline bool DocumentOrderedMap::containsMultiple(const AtomicStringImpl& id) const
{
auto it = m_map.find(&id);
return it != m_map.end() && it->value.count > 1;
}
}