Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(916)

Side by Side Diff: Source/core/dom/DocumentOrderedMap.cpp

Issue 192293002: Use new is*Element() helper functions in DOM code (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Add is*Element(PassRefPtr) helper Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 23 matching lines...) Expand all
34 #include "HTMLNames.h" 34 #include "HTMLNames.h"
35 #include "core/dom/Element.h" 35 #include "core/dom/Element.h"
36 #include "core/dom/ElementTraversal.h" 36 #include "core/dom/ElementTraversal.h"
37 #include "core/dom/TreeScope.h" 37 #include "core/dom/TreeScope.h"
38 #include "core/html/HTMLMapElement.h" 38 #include "core/html/HTMLMapElement.h"
39 39
40 namespace WebCore { 40 namespace WebCore {
41 41
42 using namespace HTMLNames; 42 using namespace HTMLNames;
43 43
44 inline bool keyMatchesId(StringImpl* key, Element* element) 44 inline bool keyMatchesId(StringImpl* key, Element& element)
45 { 45 {
46 return element->getIdAttribute().impl() == key; 46 return element.getIdAttribute().impl() == key;
47 } 47 }
48 48
49 inline bool keyMatchesMapName(StringImpl* key, Element* element) 49 inline bool keyMatchesMapName(StringImpl* key, Element& element)
50 { 50 {
51 return element->hasTagName(mapTag) && toHTMLMapElement(element)->getName().i mpl() == key; 51 return isHTMLMapElement(element) && toHTMLMapElement(element).getName().impl () == key;
52 } 52 }
53 53
54 inline bool keyMatchesLowercasedMapName(StringImpl* key, Element* element) 54 inline bool keyMatchesLowercasedMapName(StringImpl* key, Element& element)
55 { 55 {
56 return element->hasTagName(mapTag) && toHTMLMapElement(element)->getName().l ower().impl() == key; 56 return isHTMLMapElement(element) && toHTMLMapElement(element).getName().lowe r().impl() == key;
57 } 57 }
58 58
59 inline bool keyMatchesLabelForAttribute(StringImpl* key, Element* element) 59 inline bool keyMatchesLabelForAttribute(StringImpl* key, Element& element)
60 { 60 {
61 return element->hasTagName(labelTag) && element->getAttribute(forAttr).impl( ) == key; 61 return isHTMLLabelElement(element) && element.getAttribute(forAttr).impl() = = key;
62 } 62 }
63 63
64 void DocumentOrderedMap::add(StringImpl* key, Element* element) 64 void DocumentOrderedMap::add(StringImpl* key, Element* element)
65 { 65 {
66 ASSERT(key); 66 ASSERT(key);
67 ASSERT(element); 67 ASSERT(element);
68 68
69 Map::AddResult addResult = m_map.add(key, adoptPtr(new MapEntry(element))); 69 Map::AddResult addResult = m_map.add(key, adoptPtr(new MapEntry(element)));
70 if (addResult.isNewEntry) 70 if (addResult.isNewEntry)
71 return; 71 return;
(...skipping 22 matching lines...) Expand all
94 } else { 94 } else {
95 if (entry->element == element) { 95 if (entry->element == element) {
96 ASSERT(entry->orderedList.isEmpty() || entry->orderedList.first() == element); 96 ASSERT(entry->orderedList.isEmpty() || entry->orderedList.first() == element);
97 entry->element = entry->orderedList.size() > 1 ? entry->orderedList[ 1] : 0; 97 entry->element = entry->orderedList.size() > 1 ? entry->orderedList[ 1] : 0;
98 } 98 }
99 entry->count--; 99 entry->count--;
100 entry->orderedList.clear(); 100 entry->orderedList.clear();
101 } 101 }
102 } 102 }
103 103
104 template<bool keyMatches(StringImpl*, Element*)> 104 template<bool keyMatches(StringImpl*, Element&)>
105 inline Element* DocumentOrderedMap::get(StringImpl* key, const TreeScope* scope) const 105 inline Element* DocumentOrderedMap::get(StringImpl* key, const TreeScope* scope) const
106 { 106 {
107 ASSERT(key); 107 ASSERT(key);
108 ASSERT(scope); 108 ASSERT(scope);
109 109
110 MapEntry* entry = m_map.get(key); 110 MapEntry* entry = m_map.get(key);
111 if (!entry) 111 if (!entry)
112 return 0; 112 return 0;
113 113
114 ASSERT(entry->count); 114 ASSERT(entry->count);
115 if (entry->element) 115 if (entry->element)
116 return entry->element; 116 return entry->element;
117 117
118 // We know there's at least one node that matches; iterate to find the first one. 118 // We know there's at least one node that matches; iterate to find the first one.
119 for (Element* element = ElementTraversal::firstWithin(scope->rootNode()); el ement; element = ElementTraversal::next(*element)) { 119 for (Element* element = ElementTraversal::firstWithin(scope->rootNode()); el ement; element = ElementTraversal::next(*element)) {
120 if (!keyMatches(key, element)) 120 if (!keyMatches(key, *element))
121 continue; 121 continue;
122 entry->element = element; 122 entry->element = element;
123 return element; 123 return element;
124 } 124 }
125 ASSERT_NOT_REACHED(); 125 ASSERT_NOT_REACHED();
126 return 0; 126 return 0;
127 } 127 }
128 128
129 Element* DocumentOrderedMap::getElementById(StringImpl* key, const TreeScope* sc ope) const 129 Element* DocumentOrderedMap::getElementById(StringImpl* key, const TreeScope* sc ope) const
130 { 130 {
(...skipping 10 matching lines...) Expand all
141 if (it == m_map.end()) 141 if (it == m_map.end())
142 return emptyVector; 142 return emptyVector;
143 143
144 OwnPtr<MapEntry>& entry = it->value; 144 OwnPtr<MapEntry>& entry = it->value;
145 ASSERT(entry->count); 145 ASSERT(entry->count);
146 146
147 if (entry->orderedList.isEmpty()) { 147 if (entry->orderedList.isEmpty()) {
148 entry->orderedList.reserveCapacity(entry->count); 148 entry->orderedList.reserveCapacity(entry->count);
149 for (Element* element = entry->element ? entry->element : ElementTravers al::firstWithin(scope->rootNode()); entry->orderedList.size() < entry->count; el ement = ElementTraversal::next(*element)) { 149 for (Element* element = entry->element ? entry->element : ElementTravers al::firstWithin(scope->rootNode()); entry->orderedList.size() < entry->count; el ement = ElementTraversal::next(*element)) {
150 ASSERT(element); 150 ASSERT(element);
151 if (!keyMatchesId(key, element)) 151 if (!keyMatchesId(key, *element))
152 continue; 152 continue;
153 entry->orderedList.uncheckedAppend(element); 153 entry->orderedList.uncheckedAppend(element);
154 } 154 }
155 if (!entry->element) 155 if (!entry->element)
156 entry->element = entry->orderedList.first(); 156 entry->element = entry->orderedList.first();
157 } 157 }
158 158
159 return entry->orderedList; 159 return entry->orderedList;
160 } 160 }
161 161
162 Element* DocumentOrderedMap::getElementByMapName(StringImpl* key, const TreeScop e* scope) const 162 Element* DocumentOrderedMap::getElementByMapName(StringImpl* key, const TreeScop e* scope) const
163 { 163 {
164 return get<keyMatchesMapName>(key, scope); 164 return get<keyMatchesMapName>(key, scope);
165 } 165 }
166 166
167 Element* DocumentOrderedMap::getElementByLowercasedMapName(StringImpl* key, cons t TreeScope* scope) const 167 Element* DocumentOrderedMap::getElementByLowercasedMapName(StringImpl* key, cons t TreeScope* scope) const
168 { 168 {
169 return get<keyMatchesLowercasedMapName>(key, scope); 169 return get<keyMatchesLowercasedMapName>(key, scope);
170 } 170 }
171 171
172 Element* DocumentOrderedMap::getElementByLabelForAttribute(StringImpl* key, cons t TreeScope* scope) const 172 Element* DocumentOrderedMap::getElementByLabelForAttribute(StringImpl* key, cons t TreeScope* scope) const
173 { 173 {
174 return get<keyMatchesLabelForAttribute>(key, scope); 174 return get<keyMatchesLabelForAttribute>(key, scope);
175 } 175 }
176 176
177 } // namespace WebCore 177 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698