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 "core/HTMLNames.h"
35 #include "core/dom/Element.h"
36 #include "core/dom/ElementTraversal.h"
37 #include "core/dom/TreeScope.h"
38 #include "core/html/HTMLMapElement.h"
42 using namespace HTMLNames
;
44 inline bool keyMatchesId(const AtomicString
& key
, const Element
& element
)
46 return element
.getIdAttribute() == key
;
49 inline bool keyMatchesMapName(const AtomicString
& key
, const Element
& element
)
51 return isHTMLMapElement(element
) && toHTMLMapElement(element
).getName() == key
;
54 inline bool keyMatchesLowercasedMapName(const AtomicString
& key
, const Element
& element
)
56 return isHTMLMapElement(element
) && toHTMLMapElement(element
).getName().lower() == key
;
59 inline bool keyMatchesLabelForAttribute(const AtomicString
& key
, const Element
& element
)
61 return isHTMLLabelElement(element
) && element
.getAttribute(forAttr
) == key
;
64 PassOwnPtrWillBeRawPtr
<DocumentOrderedMap
> DocumentOrderedMap::create()
66 return adoptPtrWillBeNoop(new DocumentOrderedMap());
69 void DocumentOrderedMap::add(const AtomicString
& key
, Element
* element
)
74 Map::AddResult addResult
= m_map
.add(key
, adoptPtrWillBeNoop(new MapEntry(element
)));
75 if (addResult
.isNewEntry
)
78 OwnPtrWillBeMember
<MapEntry
>& entry
= addResult
.storedValue
->value
;
80 entry
->element
= nullptr;
82 entry
->orderedList
.clear();
85 void DocumentOrderedMap::remove(const AtomicString
& key
, Element
* element
)
90 Map::iterator it
= m_map
.find(key
);
91 if (it
== m_map
.end())
94 OwnPtrWillBeMember
<MapEntry
>& entry
= it
->value
;
96 if (entry
->count
== 1) {
97 ASSERT(!entry
->element
|| entry
->element
== element
);
100 if (entry
->element
== element
) {
101 ASSERT(entry
->orderedList
.isEmpty() || entry
->orderedList
.first() == element
);
102 entry
->element
= entry
->orderedList
.size() > 1 ? entry
->orderedList
[1] : nullptr;
105 entry
->orderedList
.clear();
109 template<bool keyMatches(const AtomicString
&, const Element
&)>
110 inline Element
* DocumentOrderedMap::get(const AtomicString
& key
, const TreeScope
* scope
) const
115 MapEntry
* entry
= m_map
.get(key
);
119 ASSERT(entry
->count
);
121 return entry
->element
;
123 // We know there's at least one node that matches; iterate to find the first one.
124 for (Element
& element
: ElementTraversal::startsAfter(scope
->rootNode())) {
125 if (!keyMatches(key
, element
))
127 entry
->element
= &element
;
130 ASSERT_NOT_REACHED();
134 Element
* DocumentOrderedMap::getElementById(const AtomicString
& key
, const TreeScope
* scope
) const
136 return get
<keyMatchesId
>(key
, scope
);
139 const WillBeHeapVector
<RawPtrWillBeMember
<Element
>>& DocumentOrderedMap::getAllElementsById(const AtomicString
& key
, const TreeScope
* scope
) const
143 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent
<WillBeHeapVector
<RawPtrWillBeMember
<Element
>>>, emptyVector
, (adoptPtrWillBeNoop(new WillBeHeapVector
<RawPtrWillBeMember
<Element
>>())));
145 Map::iterator it
= m_map
.find(key
);
146 if (it
== m_map
.end())
149 OwnPtrWillBeMember
<MapEntry
>& entry
= it
->value
;
150 ASSERT(entry
->count
);
152 if (entry
->orderedList
.isEmpty()) {
153 entry
->orderedList
.reserveCapacity(entry
->count
);
154 for (Element
* element
= entry
->element
? entry
->element
.get() : ElementTraversal::firstWithin(scope
->rootNode()); entry
->orderedList
.size() < entry
->count
; element
= ElementTraversal::next(*element
)) {
156 if (!keyMatchesId(key
, *element
))
158 entry
->orderedList
.uncheckedAppend(element
);
161 entry
->element
= entry
->orderedList
.first();
164 return entry
->orderedList
;
167 Element
* DocumentOrderedMap::getElementByMapName(const AtomicString
& key
, const TreeScope
* scope
) const
169 return get
<keyMatchesMapName
>(key
, scope
);
172 Element
* DocumentOrderedMap::getElementByLowercasedMapName(const AtomicString
& key
, const TreeScope
* scope
) const
174 return get
<keyMatchesLowercasedMapName
>(key
, scope
);
177 Element
* DocumentOrderedMap::getElementByLabelForAttribute(const AtomicString
& key
, const TreeScope
* scope
) const
179 return get
<keyMatchesLabelForAttribute
>(key
, scope
);
182 DEFINE_TRACE(DocumentOrderedMap
)
185 visitor
->trace(m_map
);
189 DEFINE_TRACE(DocumentOrderedMap::MapEntry
)
191 visitor
->trace(element
);
193 visitor
->trace(orderedList
);