| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv
ed. | 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009 Apple Inc. All rights reserv
ed. |
| 3 * | 3 * |
| 4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
| 6 * met: | 6 * met: |
| 7 * | 7 * |
| 8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
| (...skipping 19 matching lines...) Expand all Loading... |
| 30 | 30 |
| 31 #include "config.h" | 31 #include "config.h" |
| 32 #include "core/dom/DocumentOrderedMap.h" | 32 #include "core/dom/DocumentOrderedMap.h" |
| 33 | 33 |
| 34 #include "HTMLNames.h" | 34 #include "HTMLNames.h" |
| 35 #include "core/dom/Element.h" | 35 #include "core/dom/Element.h" |
| 36 #include "core/dom/NodeTraversal.h" | 36 #include "core/dom/NodeTraversal.h" |
| 37 #include "core/dom/TreeScope.h" | 37 #include "core/dom/TreeScope.h" |
| 38 #include "core/html/HTMLLabelElement.h" | 38 #include "core/html/HTMLLabelElement.h" |
| 39 #include "core/html/HTMLMapElement.h" | 39 #include "core/html/HTMLMapElement.h" |
| 40 #include "core/html/HTMLNameCollection.h" | |
| 41 | 40 |
| 42 namespace WebCore { | 41 namespace WebCore { |
| 43 | 42 |
| 44 using namespace HTMLNames; | 43 using namespace HTMLNames; |
| 45 | 44 |
| 46 inline bool keyMatchesId(StringImpl* key, Element* element) | 45 inline bool keyMatchesId(StringImpl* key, Element* element) |
| 47 { | 46 { |
| 48 return element->getIdAttribute().impl() == key; | 47 return element->getIdAttribute().impl() == key; |
| 49 } | 48 } |
| 50 | 49 |
| 51 inline bool keyMatchesName(StringImpl* key, Element* element) | |
| 52 { | |
| 53 return element->getNameAttribute().impl() == key; | |
| 54 } | |
| 55 | |
| 56 inline bool keyMatchesMapName(StringImpl* key, Element* element) | 50 inline bool keyMatchesMapName(StringImpl* key, Element* element) |
| 57 { | 51 { |
| 58 return element->hasTagName(mapTag) && toHTMLMapElement(element)->getName().i
mpl() == key; | 52 return element->hasTagName(mapTag) && toHTMLMapElement(element)->getName().i
mpl() == key; |
| 59 } | 53 } |
| 60 | 54 |
| 61 inline bool keyMatchesLowercasedMapName(StringImpl* key, Element* element) | 55 inline bool keyMatchesLowercasedMapName(StringImpl* key, Element* element) |
| 62 { | 56 { |
| 63 return element->hasTagName(mapTag) && toHTMLMapElement(element)->getName().l
ower().impl() == key; | 57 return element->hasTagName(mapTag) && toHTMLMapElement(element)->getName().l
ower().impl() == key; |
| 64 } | 58 } |
| 65 | 59 |
| 66 inline bool keyMatchesLabelForAttribute(StringImpl* key, Element* element) | 60 inline bool keyMatchesLabelForAttribute(StringImpl* key, Element* element) |
| 67 { | 61 { |
| 68 return isHTMLLabelElement(element) && element->getAttribute(forAttr).impl()
== key; | 62 return isHTMLLabelElement(element) && element->getAttribute(forAttr).impl()
== key; |
| 69 } | 63 } |
| 70 | 64 |
| 71 void DocumentOrderedMap::clear() | 65 void DocumentOrderedMap::clear() |
| 72 { | 66 { |
| 73 m_map.clear(); | 67 m_map.clear(); |
| 68 m_duplicateCounts.clear(); |
| 74 } | 69 } |
| 75 | 70 |
| 76 void DocumentOrderedMap::add(StringImpl* key, Element* element) | 71 void DocumentOrderedMap::add(StringImpl* key, Element* element) |
| 77 { | 72 { |
| 78 ASSERT(key); | 73 ASSERT(key); |
| 79 ASSERT(element); | 74 ASSERT(element); |
| 80 | 75 |
| 81 Map::AddResult addResult = m_map.add(key, MapEntry(element)); | 76 if (!m_duplicateCounts.contains(key)) { |
| 82 if (addResult.isNewEntry) | 77 // Fast path. The key is not already in m_duplicateCounts, so we assume
that it's |
| 83 return; | 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) |
| 81 return; |
| 84 | 82 |
| 85 MapEntry& entry = addResult.iterator->value; | 83 // The add failed, so this key was already cached in m_map. |
| 86 ASSERT(entry.count); | 84 // There are multiple elements with this key. Remove the m_map |
| 87 entry.element = 0; | 85 // cache for this key so get searches for it next time it is called. |
| 88 entry.count++; | 86 m_map.remove(addResult.iterator); |
| 89 entry.orderedList.clear(); | 87 m_duplicateCounts.add(key); |
| 88 } else { |
| 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); |
| 95 } |
| 96 } |
| 97 |
| 98 m_duplicateCounts.add(key); |
| 90 } | 99 } |
| 91 | 100 |
| 92 void DocumentOrderedMap::remove(StringImpl* key, Element* element) | 101 void DocumentOrderedMap::remove(StringImpl* key, Element* element) |
| 93 { | 102 { |
| 94 ASSERT(key); | 103 ASSERT(key); |
| 95 ASSERT(element); | 104 ASSERT(element); |
| 96 | 105 |
| 97 Map::iterator it = m_map.find(key); | 106 Map::iterator cachedItem = m_map.find(key); |
| 98 ASSERT(it != m_map.end()); | 107 if (cachedItem != m_map.end() && cachedItem->value == element) |
| 99 MapEntry& entry = it->value; | 108 m_map.remove(cachedItem); |
| 100 | 109 else |
| 101 ASSERT(entry.count); | 110 m_duplicateCounts.remove(key); |
| 102 if (entry.count == 1) { | |
| 103 ASSERT(!entry.element || entry.element == element); | |
| 104 m_map.remove(it); | |
| 105 } else { | |
| 106 if (entry.element == element) | |
| 107 entry.element = 0; | |
| 108 entry.count--; | |
| 109 entry.orderedList.clear(); // FIXME: Remove the element instead if there
are only few items left. | |
| 110 } | |
| 111 } | 111 } |
| 112 | 112 |
| 113 template<bool keyMatches(StringImpl*, Element*)> | 113 template<bool keyMatches(StringImpl*, Element*)> |
| 114 inline Element* DocumentOrderedMap::get(StringImpl* key, const TreeScope* scope)
const | 114 inline Element* DocumentOrderedMap::get(StringImpl* key, const TreeScope* scope)
const |
| 115 { | 115 { |
| 116 ASSERT(key); | 116 ASSERT(key); |
| 117 ASSERT(scope); | 117 ASSERT(scope); |
| 118 | 118 |
| 119 Map::iterator it = m_map.find(key); | 119 Element* element = m_map.get(key); |
| 120 if (it == m_map.end()) | 120 if (element) |
| 121 return 0; | 121 return element; |
| 122 | 122 |
| 123 MapEntry& entry = it->value; | 123 if (m_duplicateCounts.contains(key)) { |
| 124 ASSERT(entry.count); | 124 // We know there's at least one node that matches; iterate to find the f
irst one. |
| 125 if (entry.element) | 125 for (element = ElementTraversal::firstWithin(scope->rootNode()); element
; element = ElementTraversal::next(element)) { |
| 126 return entry.element; | 126 if (!keyMatches(key, element)) |
| 127 continue; |
| 128 m_duplicateCounts.remove(key); |
| 129 m_map.set(key, element); |
| 130 return element; |
| 131 } |
| 132 ASSERT_NOT_REACHED(); |
| 133 } |
| 127 | 134 |
| 128 // We know there's at least one node that matches; iterate to find the first
one. | |
| 129 for (Element* element = ElementTraversal::firstWithin(scope->rootNode()); el
ement; element = ElementTraversal::next(element)) { | |
| 130 if (!keyMatches(key, element)) | |
| 131 continue; | |
| 132 entry.element = element; | |
| 133 return element; | |
| 134 } | |
| 135 ASSERT_NOT_REACHED(); | |
| 136 return 0; | 135 return 0; |
| 137 } | 136 } |
| 138 | 137 |
| 139 Element* DocumentOrderedMap::getElementById(StringImpl* key, const TreeScope* sc
ope) const | 138 Element* DocumentOrderedMap::getElementById(StringImpl* key, const TreeScope* sc
ope) const |
| 140 { | 139 { |
| 141 return get<keyMatchesId>(key, scope); | 140 return get<keyMatchesId>(key, scope); |
| 142 } | 141 } |
| 143 | 142 |
| 144 Element* DocumentOrderedMap::getElementByName(StringImpl* key, const TreeScope*
scope) const | |
| 145 { | |
| 146 return get<keyMatchesName>(key, scope); | |
| 147 } | |
| 148 | |
| 149 Element* DocumentOrderedMap::getElementByMapName(StringImpl* key, const TreeScop
e* scope) const | 143 Element* DocumentOrderedMap::getElementByMapName(StringImpl* key, const TreeScop
e* scope) const |
| 150 { | 144 { |
| 151 return get<keyMatchesMapName>(key, scope); | 145 return get<keyMatchesMapName>(key, scope); |
| 152 } | 146 } |
| 153 | 147 |
| 154 Element* DocumentOrderedMap::getElementByLowercasedMapName(StringImpl* key, cons
t TreeScope* scope) const | 148 Element* DocumentOrderedMap::getElementByLowercasedMapName(StringImpl* key, cons
t TreeScope* scope) const |
| 155 { | 149 { |
| 156 return get<keyMatchesLowercasedMapName>(key, scope); | 150 return get<keyMatchesLowercasedMapName>(key, scope); |
| 157 } | 151 } |
| 158 | 152 |
| 159 Element* DocumentOrderedMap::getElementByLabelForAttribute(StringImpl* key, cons
t TreeScope* scope) const | 153 Element* DocumentOrderedMap::getElementByLabelForAttribute(StringImpl* key, cons
t TreeScope* scope) const |
| 160 { | 154 { |
| 161 return get<keyMatchesLabelForAttribute>(key, scope); | 155 return get<keyMatchesLabelForAttribute>(key, scope); |
| 162 } | 156 } |
| 163 | 157 |
| 164 const Vector<Element*>* DocumentOrderedMap::getAllElementsById(StringImpl* key,
const TreeScope* scope) const | |
| 165 { | |
| 166 ASSERT(key); | |
| 167 ASSERT(scope); | |
| 168 | |
| 169 Map::iterator it = m_map.find(key); | |
| 170 if (it == m_map.end()) | |
| 171 return 0; | |
| 172 | |
| 173 MapEntry& entry = it->value; | |
| 174 ASSERT(entry.count); | |
| 175 if (!entry.count) | |
| 176 return 0; | |
| 177 | |
| 178 if (entry.orderedList.isEmpty()) { | |
| 179 entry.orderedList.reserveCapacity(entry.count); | |
| 180 for (Element* element = entry.element ? entry.element : ElementTraversal
::firstWithin(scope->rootNode()); element; element = ElementTraversal::next(elem
ent)) { | |
| 181 if (!keyMatchesId(key, element)) | |
| 182 continue; | |
| 183 entry.orderedList.append(element); | |
| 184 } | |
| 185 ASSERT(entry.orderedList.size() == entry.count); | |
| 186 } | |
| 187 | |
| 188 return &entry.orderedList; | |
| 189 } | |
| 190 | |
| 191 } // namespace WebCore | 158 } // namespace WebCore |
| OLD | NEW |