| 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 "core/dom/Element.h" | 34 #include "core/dom/Element.h" |
| 35 #include "core/dom/ElementTraversal.h" | 35 #include "core/dom/ElementTraversal.h" |
| 36 #include "core/dom/TreeScope.h" | 36 #include "core/dom/TreeScope.h" |
| 37 | 37 |
| 38 namespace blink { | 38 namespace blink { |
| 39 | 39 |
| 40 inline bool keyMatchesId(const AtomicString& key, const Element& element) | |
| 41 { | |
| 42 return element.getIdAttribute() == key; | |
| 43 } | |
| 44 | |
| 45 inline bool keyMatchesMapName(const AtomicString& key, const Element& element) | |
| 46 { | |
| 47 return false; | |
| 48 } | |
| 49 | |
| 50 inline bool keyMatchesLowercasedMapName(const AtomicString& key, const Element&
element) | |
| 51 { | |
| 52 return false; | |
| 53 } | |
| 54 | |
| 55 inline bool keyMatchesLabelForAttribute(const AtomicString& key, const Element&
element) | |
| 56 { | |
| 57 return false; | |
| 58 } | |
| 59 | |
| 60 PassOwnPtr<DocumentOrderedMap> DocumentOrderedMap::create() | 40 PassOwnPtr<DocumentOrderedMap> DocumentOrderedMap::create() |
| 61 { | 41 { |
| 62 return adoptPtr(new DocumentOrderedMap()); | 42 return adoptPtr(new DocumentOrderedMap()); |
| 63 } | 43 } |
| 64 | 44 |
| 65 void DocumentOrderedMap::add(const AtomicString& key, Element* element) | 45 void DocumentOrderedMap::add(const AtomicString& key, Element* element) |
| 66 { | 46 { |
| 67 ASSERT(key); | 47 ASSERT(key); |
| 68 ASSERT(element); | 48 ASSERT(element); |
| 69 | 49 |
| 70 Map::AddResult addResult = m_map.add(key, adoptPtr(new MapEntry(element))); | 50 Map::AddResult addResult = m_map.add(key, adoptPtr(new MapEntry(element))); |
| 71 if (addResult.isNewEntry) | 51 if (addResult.isNewEntry) |
| 72 return; | 52 return; |
| 73 | 53 |
| 74 OwnPtr<MapEntry>& entry = addResult.storedValue->value; | 54 OwnPtr<MapEntry>& entry = addResult.storedValue->value; |
| 75 ASSERT(entry->count); | 55 ASSERT(entry->count); |
| 76 entry->element = nullptr; | 56 entry->element = nullptr; |
| 77 entry->count++; | 57 entry->count++; |
| 78 entry->orderedList.clear(); | |
| 79 } | 58 } |
| 80 | 59 |
| 81 void DocumentOrderedMap::remove(const AtomicString& key, Element* element) | 60 void DocumentOrderedMap::remove(const AtomicString& key, Element* element) |
| 82 { | 61 { |
| 83 ASSERT(key); | 62 ASSERT(key); |
| 84 ASSERT(element); | 63 ASSERT(element); |
| 85 | 64 |
| 86 Map::iterator it = m_map.find(key); | 65 Map::iterator it = m_map.find(key); |
| 87 if (it == m_map.end()) | 66 if (it == m_map.end()) |
| 88 return; | 67 return; |
| 89 | 68 |
| 90 OwnPtr<MapEntry>& entry = it->value; | 69 OwnPtr<MapEntry>& entry = it->value; |
| 91 ASSERT(entry->count); | 70 ASSERT(entry->count); |
| 92 if (entry->count == 1) { | 71 |
| 93 ASSERT(!entry->element || entry->element == element); | 72 entry->count--; |
| 73 if (!entry->count) |
| 94 m_map.remove(it); | 74 m_map.remove(it); |
| 95 } else { | 75 else if (entry->element == element) |
| 96 if (entry->element == element) { | 76 entry->element = nullptr; |
| 97 ASSERT(entry->orderedList.isEmpty() || entry->orderedList.first() ==
element); | |
| 98 entry->element = entry->orderedList.size() > 1 ? entry->orderedList[
1] : nullptr; | |
| 99 } | |
| 100 entry->count--; | |
| 101 entry->orderedList.clear(); | |
| 102 } | |
| 103 } | 77 } |
| 104 | 78 |
| 105 template<bool keyMatches(const AtomicString&, const Element&)> | 79 Element* DocumentOrderedMap::getElementById(const AtomicString& key, const TreeS
cope* scope) const |
| 106 inline Element* DocumentOrderedMap::get(const AtomicString& key, const TreeScope
* scope) const | |
| 107 { | 80 { |
| 108 ASSERT(key); | 81 ASSERT(key); |
| 109 ASSERT(scope); | 82 ASSERT(scope); |
| 110 | 83 |
| 111 MapEntry* entry = m_map.get(key); | 84 MapEntry* entry = m_map.get(key); |
| 112 if (!entry) | 85 if (!entry) |
| 113 return 0; | 86 return 0; |
| 114 | 87 |
| 115 ASSERT(entry->count); | 88 ASSERT(entry->count); |
| 116 if (entry->element) | 89 if (entry->element) |
| 117 return entry->element; | 90 return entry->element; |
| 118 | 91 |
| 119 // We know there's at least one node that matches; iterate to find the first
one. | 92 // We know there's at least one node that matches; iterate to find the first
one. |
| 120 for (Element* element = ElementTraversal::firstWithin(scope->rootNode()); el
ement; element = ElementTraversal::next(*element)) { | 93 for (Element* element = ElementTraversal::firstWithin(scope->rootNode()); el
ement; element = ElementTraversal::next(*element)) { |
| 121 if (!keyMatches(key, *element)) | 94 if (element->getIdAttribute() != key) |
| 122 continue; | 95 continue; |
| 123 entry->element = element; | 96 entry->element = element; |
| 124 return element; | 97 return element; |
| 125 } | 98 } |
| 126 ASSERT_NOT_REACHED(); | 99 ASSERT_NOT_REACHED(); |
| 127 return 0; | 100 return 0; |
| 128 } | 101 } |
| 129 | 102 |
| 130 Element* DocumentOrderedMap::getElementById(const AtomicString& key, const TreeS
cope* scope) const | |
| 131 { | |
| 132 return get<keyMatchesId>(key, scope); | |
| 133 } | |
| 134 | |
| 135 const Vector<RawPtr<Element> >& DocumentOrderedMap::getAllElementsById(const Ato
micString& key, const TreeScope* scope) const | |
| 136 { | |
| 137 ASSERT(key); | |
| 138 ASSERT(scope); | |
| 139 DEFINE_STATIC_LOCAL(OwnPtr<Vector<RawPtr<Element> > >, emptyVector, (adoptPt
r(new Vector<RawPtr<Element> >()))); | |
| 140 | |
| 141 Map::iterator it = m_map.find(key); | |
| 142 if (it == m_map.end()) | |
| 143 return *emptyVector; | |
| 144 | |
| 145 OwnPtr<MapEntry>& entry = it->value; | |
| 146 ASSERT(entry->count); | |
| 147 | |
| 148 if (entry->orderedList.isEmpty()) { | |
| 149 entry->orderedList.reserveCapacity(entry->count); | |
| 150 for (Element* element = entry->element ? entry->element.get() : ElementT
raversal::firstWithin(scope->rootNode()); entry->orderedList.size() < entry->cou
nt; element = ElementTraversal::next(*element)) { | |
| 151 ASSERT(element); | |
| 152 if (!keyMatchesId(key, *element)) | |
| 153 continue; | |
| 154 entry->orderedList.uncheckedAppend(element); | |
| 155 } | |
| 156 if (!entry->element) | |
| 157 entry->element = entry->orderedList.first(); | |
| 158 } | |
| 159 | |
| 160 return entry->orderedList; | |
| 161 } | |
| 162 | |
| 163 Element* DocumentOrderedMap::getElementByMapName(const AtomicString& key, const
TreeScope* scope) const | |
| 164 { | |
| 165 return get<keyMatchesMapName>(key, scope); | |
| 166 } | |
| 167 | |
| 168 Element* DocumentOrderedMap::getElementByLowercasedMapName(const AtomicString& k
ey, const TreeScope* scope) const | |
| 169 { | |
| 170 return get<keyMatchesLowercasedMapName>(key, scope); | |
| 171 } | |
| 172 | |
| 173 Element* DocumentOrderedMap::getElementByLabelForAttribute(const AtomicString& k
ey, const TreeScope* scope) const | |
| 174 { | |
| 175 return get<keyMatchesLabelForAttribute>(key, scope); | |
| 176 } | |
| 177 | |
| 178 void DocumentOrderedMap::trace(Visitor* visitor) | |
| 179 { | |
| 180 #if ENABLE(OILPAN) | |
| 181 visitor->trace(m_map); | |
| 182 #endif | |
| 183 } | |
| 184 | |
| 185 void DocumentOrderedMap::MapEntry::trace(Visitor* visitor) | |
| 186 { | |
| 187 visitor->trace(element); | |
| 188 #if ENABLE(OILPAN) | |
| 189 visitor->trace(orderedList); | |
| 190 #endif | |
| 191 } | |
| 192 | |
| 193 } // namespace blink | 103 } // namespace blink |
| OLD | NEW |