DocumentOrderedMap.cpp [plain text]
#include "config.h"
#include "DocumentOrderedMap.h"
#include "ElementIterator.h"
#include "HTMLImageElement.h"
#include "HTMLLabelElement.h"
#include "HTMLMapElement.h"
#include "HTMLNameCollection.h"
namespace WebCore {
using namespace HTMLNames;
void DocumentOrderedMap::clear()
{
m_map.clear();
}
void DocumentOrderedMap::add(const AtomicStringImpl& key, Element& element, const TreeScope& treeScope)
{
UNUSED_PARAM(treeScope);
ASSERT_WITH_SECURITY_IMPLICATION(element.isInTreeScope());
ASSERT_WITH_SECURITY_IMPLICATION(treeScope.rootNode().containsIncludingShadowDOM(&element));
if (!element.isInTreeScope())
return;
Map::AddResult addResult = m_map.ensure(&key, [&element] {
return MapEntry(&element);
});
MapEntry& entry = addResult.iterator->value;
#if !ASSERT_DISABLED || ENABLE(SECURITY_ASSERTIONS)
ASSERT_WITH_SECURITY_IMPLICATION(!entry.registeredElements.contains(&element));
entry.registeredElements.add(&element);
#endif
if (addResult.isNewEntry)
return;
ASSERT_WITH_SECURITY_IMPLICATION(entry.count);
entry.element = nullptr;
entry.count++;
entry.orderedList.clear();
}
void DocumentOrderedMap::remove(const AtomicStringImpl& key, Element& element)
{
m_map.checkConsistency();
auto it = m_map.find(&key);
ASSERT_WITH_SECURITY_IMPLICATION(it != m_map.end());
if (it == m_map.end())
return;
MapEntry& entry = it->value;
ASSERT_WITH_SECURITY_IMPLICATION(entry.registeredElements.remove(&element));
ASSERT_WITH_SECURITY_IMPLICATION(entry.count);
if (entry.count == 1) {
ASSERT_WITH_SECURITY_IMPLICATION(!entry.element || entry.element == &element);
m_map.remove(it);
} else {
if (entry.element == &element)
entry.element = nullptr;
entry.count--;
entry.orderedList.clear(); }
}
template <typename KeyMatchingFunction>
inline Element* DocumentOrderedMap::get(const AtomicStringImpl& key, const TreeScope& scope, const KeyMatchingFunction& keyMatches) const
{
m_map.checkConsistency();
auto it = m_map.find(&key);
if (it == m_map.end())
return nullptr;
MapEntry& entry = it->value;
ASSERT(entry.count);
if (entry.element) {
ASSERT_WITH_SECURITY_IMPLICATION(entry.element->isInTreeScope());
ASSERT_WITH_SECURITY_IMPLICATION(&entry.element->treeScope() == &scope);
ASSERT_WITH_SECURITY_IMPLICATION(entry.registeredElements.contains(entry.element));
return entry.element;
}
for (auto& element : descendantsOfType<Element>(scope.rootNode())) {
if (!keyMatches(key, element))
continue;
entry.element = &element;
ASSERT_WITH_SECURITY_IMPLICATION(element.isInTreeScope());
ASSERT_WITH_SECURITY_IMPLICATION(&element.treeScope() == &scope);
ASSERT_WITH_SECURITY_IMPLICATION(entry.registeredElements.contains(entry.element));
return &element;
}
ASSERT_NOT_REACHED();
return nullptr;
}
Element* DocumentOrderedMap::getElementById(const AtomicStringImpl& key, const TreeScope& scope) const
{
return get(key, scope, [] (const AtomicStringImpl& key, const Element& element) {
return element.getIdAttribute().impl() == &key;
});
}
Element* DocumentOrderedMap::getElementByName(const AtomicStringImpl& key, const TreeScope& scope) const
{
return get(key, scope, [] (const AtomicStringImpl& key, const Element& element) {
return element.getNameAttribute().impl() == &key;
});
}
HTMLMapElement* DocumentOrderedMap::getElementByMapName(const AtomicStringImpl& key, const TreeScope& scope) const
{
return downcast<HTMLMapElement>(get(key, scope, [] (const AtomicStringImpl& key, const Element& element) {
return is<HTMLMapElement>(element) && downcast<HTMLMapElement>(element).getName().impl() == &key;
}));
}
HTMLMapElement* DocumentOrderedMap::getElementByCaseFoldedMapName(const AtomicStringImpl& key, const TreeScope& scope) const
{
return downcast<HTMLMapElement>(get(key, scope, [] (const AtomicStringImpl& key, const Element& element) {
return is<HTMLMapElement>(element) && equal(downcast<HTMLMapElement>(element).getName().string().foldCase().impl(), &key);
}));
}
HTMLImageElement* DocumentOrderedMap::getElementByCaseFoldedUsemap(const AtomicStringImpl& key, const TreeScope& scope) const
{
return downcast<HTMLImageElement>(get(key, scope, [] (const AtomicStringImpl& key, const Element& element) {
return is<HTMLImageElement>(element) && downcast<HTMLImageElement>(element).matchesCaseFoldedUsemap(key);
}));
}
HTMLLabelElement* DocumentOrderedMap::getElementByLabelForAttribute(const AtomicStringImpl& key, const TreeScope& scope) const
{
return downcast<HTMLLabelElement>(get(key, scope, [] (const AtomicStringImpl& key, const Element& element) {
return is<HTMLLabelElement>(element) && element.attributeWithoutSynchronization(forAttr).impl() == &key;
}));
}
Element* DocumentOrderedMap::getElementByWindowNamedItem(const AtomicStringImpl& key, const TreeScope& scope) const
{
return get(key, scope, [] (const AtomicStringImpl& key, const Element& element) {
return WindowNameCollection::elementMatches(element, &key);
});
}
Element* DocumentOrderedMap::getElementByDocumentNamedItem(const AtomicStringImpl& key, const TreeScope& scope) const
{
return get(key, scope, [] (const AtomicStringImpl& key, const Element& element) {
return DocumentNameCollection::elementMatches(element, &key);
});
}
const Vector<Element*>* DocumentOrderedMap::getAllElementsById(const AtomicStringImpl& key, const TreeScope& scope) const
{
m_map.checkConsistency();
auto it = m_map.find(&key);
if (it == m_map.end())
return nullptr;
MapEntry& entry = it->value;
ASSERT_WITH_SECURITY_IMPLICATION(entry.count);
if (!entry.count)
return nullptr;
if (entry.orderedList.isEmpty()) {
entry.orderedList.reserveCapacity(entry.count);
auto elementDescandents = descendantsOfType<Element>(scope.rootNode());
auto it = entry.element ? elementDescandents.beginAt(*entry.element) : elementDescandents.begin();
auto end = elementDescandents.end();
for (; it != end; ++it) {
auto& element = *it;
if (element.getIdAttribute().impl() != &key)
continue;
entry.orderedList.append(&element);
}
ASSERT(entry.orderedList.size() == entry.count);
}
return &entry.orderedList;
}
}