2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "core/dom/DocumentOrderedMap.h"
34 #include "HTMLNames.h"
35 #include "core/dom/Element.h"
36 #include "core/dom/ElementTraversal.h"
37 #include "core/dom/TreeScope.h"
38 #include "core/html/HTMLLabelElement.h"
39 #include "core/html/HTMLMapElement.h"
43 using namespace HTMLNames;
45 inline bool keyMatchesId(StringImpl* key, Element* element)
47 return element->getIdAttribute().impl() == key;
50 inline bool keyMatchesMapName(StringImpl* key, Element* element)
52 return element->hasTagName(mapTag) && toHTMLMapElement(element)->getName().impl() == key;
55 inline bool keyMatchesLowercasedMapName(StringImpl* key, Element* element)
57 return element->hasTagName(mapTag) && toHTMLMapElement(element)->getName().lower().impl() == key;
60 inline bool keyMatchesLabelForAttribute(StringImpl* key, Element* element)
62 return isHTMLLabelElement(element) && element->getAttribute(forAttr).impl() == key;
65 void DocumentOrderedMap::clear()
68 m_duplicateCounts.clear();
71 void DocumentOrderedMap::add(StringImpl* key, Element* element)
76 if (!m_duplicateCounts.contains(key)) {
77 // Fast path. The key is not already in m_duplicateCounts, so we assume that it's
78 // also not already in m_map and try to add it. If that add succeeds, we're done.
79 Map::AddResult addResult = m_map.add(key, element);
80 if (addResult.isNewEntry)
83 // The add failed, so this key was already cached in m_map.
84 // There are multiple elements with this key. Remove the m_map
85 // cache for this key so get searches for it next time it is called.
86 m_map.remove(addResult.iterator);
87 m_duplicateCounts.add(key);
89 // There are multiple elements with this key. Remove the m_map
90 // cache for this key so get will search for it next time it is called.
91 Map::iterator cachedItem = m_map.find(key);
92 if (cachedItem != m_map.end()) {
93 m_map.remove(cachedItem);
94 m_duplicateCounts.add(key);
98 m_duplicateCounts.add(key);
101 void DocumentOrderedMap::remove(StringImpl* key, Element* element)
106 Map::iterator cachedItem = m_map.find(key);
107 if (cachedItem != m_map.end() && cachedItem->value == element)
108 m_map.remove(cachedItem);
110 m_duplicateCounts.remove(key);
113 template<bool keyMatches(StringImpl*, Element*)>
114 inline Element* DocumentOrderedMap::get(StringImpl* key, const TreeScope* scope) const
119 Element* element = m_map.get(key);
123 if (m_duplicateCounts.contains(key)) {
124 // We know there's at least one node that matches; iterate to find the first one.
125 for (element = ElementTraversal::firstWithin(scope->rootNode()); element; element = ElementTraversal::next(element)) {
126 if (!keyMatches(key, element))
128 m_duplicateCounts.remove(key);
129 m_map.set(key, element);
132 ASSERT_NOT_REACHED();
138 Element* DocumentOrderedMap::getElementById(StringImpl* key, const TreeScope* scope) const
140 return get<keyMatchesId>(key, scope);
143 Element* DocumentOrderedMap::getElementByMapName(StringImpl* key, const TreeScope* scope) const
145 return get<keyMatchesMapName>(key, scope);
148 Element* DocumentOrderedMap::getElementByLowercasedMapName(StringImpl* key, const TreeScope* scope) const
150 return get<keyMatchesLowercasedMapName>(key, scope);
153 Element* DocumentOrderedMap::getElementByLabelForAttribute(StringImpl* key, const TreeScope* scope) const
155 return get<keyMatchesLabelForAttribute>(key, scope);
158 } // namespace WebCore