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

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

Issue 1532103002: Better handling of DocumentOrderedMap same-ID lookups during tree removals (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: review updates, part2 Created 5 years 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
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 "core/HTMLNames.h" 34 #include "core/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 blink { 40 namespace blink {
41 41
42 using namespace HTMLNames; 42 using namespace HTMLNames;
43 43
44
45 PassOwnPtrWillBeRawPtr<DocumentOrderedMap> DocumentOrderedMap::create()
46 {
47 return adoptPtrWillBeNoop(new DocumentOrderedMap);
48 }
49
50 DocumentOrderedMap::DocumentOrderedMap()
51 {
52 }
53
54 DocumentOrderedMap::~DocumentOrderedMap()
55 {
56 }
57
44 inline bool keyMatchesId(const AtomicString& key, const Element& element) 58 inline bool keyMatchesId(const AtomicString& key, const Element& element)
45 { 59 {
46 return element.getIdAttribute() == key; 60 return element.getIdAttribute() == key;
47 } 61 }
48 62
49 inline bool keyMatchesMapName(const AtomicString& key, const Element& element) 63 inline bool keyMatchesMapName(const AtomicString& key, const Element& element)
50 { 64 {
51 return isHTMLMapElement(element) && toHTMLMapElement(element).getName() == k ey; 65 return isHTMLMapElement(element) && toHTMLMapElement(element).getName() == k ey;
52 } 66 }
53 67
54 inline bool keyMatchesLowercasedMapName(const AtomicString& key, const Element& element) 68 inline bool keyMatchesLowercasedMapName(const AtomicString& key, const Element& element)
55 { 69 {
56 return isHTMLMapElement(element) && toHTMLMapElement(element).getName().lowe r() == key; 70 return isHTMLMapElement(element) && toHTMLMapElement(element).getName().lowe r() == key;
57 } 71 }
58 72
59 inline bool keyMatchesLabelForAttribute(const AtomicString& key, const Element& element) 73 inline bool keyMatchesLabelForAttribute(const AtomicString& key, const Element& element)
60 { 74 {
61 return isHTMLLabelElement(element) && element.getAttribute(forAttr) == key; 75 return isHTMLLabelElement(element) && element.getAttribute(forAttr) == key;
62 } 76 }
63 77
64 PassOwnPtrWillBeRawPtr<DocumentOrderedMap> DocumentOrderedMap::create()
65 {
66 return adoptPtrWillBeNoop(new DocumentOrderedMap());
67 }
68
69 void DocumentOrderedMap::add(const AtomicString& key, Element* element) 78 void DocumentOrderedMap::add(const AtomicString& key, Element* element)
70 { 79 {
71 ASSERT(key); 80 ASSERT(key);
72 ASSERT(element); 81 ASSERT(element);
73 82
74 Map::AddResult addResult = m_map.add(key, adoptPtrWillBeNoop(new MapEntry(el ement))); 83 Map::AddResult addResult = m_map.add(key, adoptPtrWillBeNoop(new MapEntry(el ement)));
75 if (addResult.isNewEntry) 84 if (addResult.isNewEntry)
76 return; 85 return;
77 86
78 OwnPtrWillBeMember<MapEntry>& entry = addResult.storedValue->value; 87 OwnPtrWillBeMember<MapEntry>& entry = addResult.storedValue->value;
(...skipping 20 matching lines...) Expand all
99 } else { 108 } else {
100 if (entry->element == element) { 109 if (entry->element == element) {
101 ASSERT(entry->orderedList.isEmpty() || entry->orderedList.first() == element); 110 ASSERT(entry->orderedList.isEmpty() || entry->orderedList.first() == element);
102 entry->element = entry->orderedList.size() > 1 ? entry->orderedList[ 1] : nullptr; 111 entry->element = entry->orderedList.size() > 1 ? entry->orderedList[ 1] : nullptr;
103 } 112 }
104 entry->count--; 113 entry->count--;
105 entry->orderedList.clear(); 114 entry->orderedList.clear();
106 } 115 }
107 } 116 }
108 117
118 #if ENABLE(ASSERT)
119 void DocumentOrderedMap::willRemoveId(const AtomicString& key)
120 {
121 ASSERT(m_removingId.isNull() || key.isNull());
122 m_removingId = key;
123 }
124 #endif
125
109 template<bool keyMatches(const AtomicString&, const Element&)> 126 template<bool keyMatches(const AtomicString&, const Element&)>
110 inline Element* DocumentOrderedMap::get(const AtomicString& key, const TreeScope * scope) const 127 inline Element* DocumentOrderedMap::get(const AtomicString& key, const TreeScope * scope) const
111 { 128 {
112 ASSERT(key); 129 ASSERT(key);
113 ASSERT(scope); 130 ASSERT(scope);
114 131
115 MapEntry* entry = m_map.get(key); 132 MapEntry* entry = m_map.get(key);
116 if (!entry) 133 if (!entry)
117 return 0; 134 return 0;
118 135
119 ASSERT(entry->count); 136 ASSERT(entry->count);
120 if (entry->element) 137 if (entry->element)
121 return entry->element; 138 return entry->element;
122 139
123 // We know there's at least one node that matches; iterate to find the first one. 140 // Iterate to find the node that matches. Nothing will match iff an element
141 // with children having duplicate IDs is being removed -- the tree traversal
142 // will be over an updated tree not having that element. In all other cases,
143 // a match is expected.
144 //
145 // Such calls to get()/getElementById() while handling element removals will
146 // legitimately happen when e.g., adjusting form ID associations. Quietly
147 // allow those lookups to (expectedly) fail by having the tree scope removal
148 // register the element ID it is in the process of removing.
124 for (Element& element : ElementTraversal::startsAfter(scope->rootNode())) { 149 for (Element& element : ElementTraversal::startsAfter(scope->rootNode())) {
125 if (!keyMatches(key, element)) 150 if (!keyMatches(key, element))
126 continue; 151 continue;
127 entry->element = &element; 152 entry->element = &element;
128 return &element; 153 return &element;
129 } 154 }
130 ASSERT_NOT_REACHED(); 155 ASSERT(key == m_removingId);
131 return 0; 156 return 0;
132 } 157 }
133 158
134 Element* DocumentOrderedMap::getElementById(const AtomicString& key, const TreeS cope* scope) const 159 Element* DocumentOrderedMap::getElementById(const AtomicString& key, const TreeS cope* scope) const
135 { 160 {
136 return get<keyMatchesId>(key, scope); 161 return get<keyMatchesId>(key, scope);
137 } 162 }
138 163
139 const WillBeHeapVector<RawPtrWillBeMember<Element>>& DocumentOrderedMap::getAllE lementsById(const AtomicString& key, const TreeScope* scope) const 164 const WillBeHeapVector<RawPtrWillBeMember<Element>>& DocumentOrderedMap::getAllE lementsById(const AtomicString& key, const TreeScope* scope) const
140 { 165 {
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 213
189 DEFINE_TRACE(DocumentOrderedMap::MapEntry) 214 DEFINE_TRACE(DocumentOrderedMap::MapEntry)
190 { 215 {
191 visitor->trace(element); 216 visitor->trace(element);
192 #if ENABLE(OILPAN) 217 #if ENABLE(OILPAN)
193 visitor->trace(orderedList); 218 visitor->trace(orderedList);
194 #endif 219 #endif
195 } 220 }
196 221
197 } // namespace blink 222 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/dom/DocumentOrderedMap.h ('k') | third_party/WebKit/Source/core/dom/TreeScope.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698