tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / html / HTMLFormCollection.cpp
1 /*
2  * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3  *           (C) 1999 Antti Koivisto (koivisto@kde.org)
4  * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2010 Apple Inc. All rights reserved.
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Library General Public License for more details.
15  *
16  * You should have received a copy of the GNU Library General Public License
17  * along with this library; see the file COPYING.LIB.  If not, write to
18  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19  * Boston, MA 02110-1301, USA.
20  *
21  */
22
23 #include "config.h"
24 #include "HTMLFormCollection.h"
25
26 #include "CollectionCache.h"
27 #include "HTMLFormControlElement.h"
28 #include "HTMLFormElement.h"
29 #include "HTMLImageElement.h"
30 #include "HTMLNames.h"
31
32 namespace WebCore {
33
34 using namespace HTMLNames;
35
36 // Since the collections are to be "live", we have to do the
37 // calculation every time if anything has changed.
38
39 inline CollectionCache* HTMLFormCollection::formCollectionInfo(HTMLFormElement* form)
40 {
41     if (!form->m_collectionCache)
42         form->m_collectionCache = adoptPtr(new CollectionCache);
43     return form->m_collectionCache.get();
44 }
45
46 HTMLFormCollection::HTMLFormCollection(PassRefPtr<HTMLFormElement> form)
47     : HTMLCollection(form.get(), OtherCollection, formCollectionInfo(form.get()))
48 {
49 }
50
51 PassRefPtr<HTMLFormCollection> HTMLFormCollection::create(PassRefPtr<HTMLFormElement> form)
52 {
53     return adoptRef(new HTMLFormCollection(form));
54 }
55
56 HTMLFormCollection::~HTMLFormCollection()
57 {
58 }
59
60 unsigned HTMLFormCollection::calcLength() const
61 {
62     return static_cast<HTMLFormElement*>(base())->length();
63 }
64
65 Node* HTMLFormCollection::item(unsigned index) const
66 {
67     resetCollectionInfo();
68
69     if (info()->current && info()->position == index)
70         return info()->current;
71
72     if (info()->hasLength && info()->length <= index)
73         return 0;
74
75     if (!info()->current || info()->position > index) {
76         info()->current = 0;
77         info()->position = 0;
78         info()->elementsArrayPosition = 0;
79     }
80
81     Vector<FormAssociatedElement*>& elementsArray = static_cast<HTMLFormElement*>(base())->m_associatedElements;
82     unsigned currentIndex = info()->position;
83     
84     for (unsigned i = info()->elementsArrayPosition; i < elementsArray.size(); i++) {
85         if (elementsArray[i]->isEnumeratable()) {
86             HTMLElement* element = toHTMLElement(elementsArray[i]);
87             if (index == currentIndex) {
88                 info()->position = index;
89                 info()->current = element;
90                 info()->elementsArrayPosition = i;
91                 return element;
92             }
93
94             currentIndex++;
95         }
96     }
97
98     return 0;
99 }
100
101 Element* HTMLFormCollection::getNamedItem(const QualifiedName& attrName, const AtomicString& name) const
102 {
103     info()->position = 0;
104     return getNamedFormItem(attrName, name, 0);
105 }
106
107 Element* HTMLFormCollection::getNamedFormItem(const QualifiedName& attrName, const String& name, int duplicateNumber) const
108 {
109     HTMLFormElement* form = static_cast<HTMLFormElement*>(base());
110
111     bool foundInputElements = false;
112     for (unsigned i = 0; i < form->m_associatedElements.size(); ++i) {
113         FormAssociatedElement* associatedElement = form->m_associatedElements[i];
114         HTMLElement* element = toHTMLElement(associatedElement);
115         if (associatedElement->isEnumeratable() && element->getAttribute(attrName) == name) {
116             foundInputElements = true;
117             if (!duplicateNumber)
118                 return element;
119             --duplicateNumber;
120         }
121     }
122
123     if (!foundInputElements) {
124         for (unsigned i = 0; i < form->m_imageElements.size(); ++i) {
125             HTMLImageElement* element = form->m_imageElements[i];
126             if (element->getAttribute(attrName) == name) {
127                 if (!duplicateNumber)
128                     return element;
129                 --duplicateNumber;
130             }
131         }
132     }
133
134     return 0;
135 }
136
137 Node* HTMLFormCollection::nextItem() const
138 {
139     return item(info()->position + 1);
140 }
141
142 Node* HTMLFormCollection::namedItem(const AtomicString& name) const
143 {
144     // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/nameditem.asp
145     // This method first searches for an object with a matching id
146     // attribute. If a match is not found, the method then searches for an
147     // object with a matching name attribute, but only on those elements
148     // that are allowed a name attribute.
149     resetCollectionInfo();
150     info()->current = getNamedItem(idAttr, name);
151     if (info()->current)
152         return info()->current;
153     info()->current = getNamedItem(nameAttr, name);
154     return info()->current;
155 }
156
157 void HTMLFormCollection::updateNameCache() const
158 {
159     if (info()->hasNameCache)
160         return;
161
162     HashSet<AtomicStringImpl*> foundInputElements;
163
164     HTMLFormElement* f = static_cast<HTMLFormElement*>(base());
165
166     for (unsigned i = 0; i < f->m_associatedElements.size(); ++i) {
167         FormAssociatedElement* associatedElement = f->m_associatedElements[i];
168         if (associatedElement->isEnumeratable()) {
169             HTMLElement* element = toHTMLElement(associatedElement);
170             const AtomicString& idAttrVal = element->getIdAttribute();
171             const AtomicString& nameAttrVal = element->getAttribute(nameAttr);
172             if (!idAttrVal.isEmpty()) {
173                 // add to id cache
174                 Vector<Element*>* idVector = info()->idCache.get(idAttrVal.impl());
175                 if (!idVector) {
176                     idVector = new Vector<Element*>;
177                     info()->idCache.add(idAttrVal.impl(), idVector);
178                 }
179                 idVector->append(element);
180                 foundInputElements.add(idAttrVal.impl());
181             }
182             if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal) {
183                 // add to name cache
184                 Vector<Element*>* nameVector = info()->nameCache.get(nameAttrVal.impl());
185                 if (!nameVector) {
186                     nameVector = new Vector<Element*>;
187                     info()->nameCache.add(nameAttrVal.impl(), nameVector);
188                 }
189                 nameVector->append(element);
190                 foundInputElements.add(nameAttrVal.impl());
191             }
192         }
193     }
194
195     for (unsigned i = 0; i < f->m_imageElements.size(); ++i) {
196         HTMLImageElement* element = f->m_imageElements[i];
197         const AtomicString& idAttrVal = element->getIdAttribute();
198         const AtomicString& nameAttrVal = element->getAttribute(nameAttr);
199         if (!idAttrVal.isEmpty() && !foundInputElements.contains(idAttrVal.impl())) {
200             // add to id cache
201             Vector<Element*>* idVector = info()->idCache.get(idAttrVal.impl());
202             if (!idVector) {
203                 idVector = new Vector<Element*>;
204                 info()->idCache.add(idAttrVal.impl(), idVector);
205             }
206             idVector->append(element);
207         }
208         if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && !foundInputElements.contains(nameAttrVal.impl())) {
209             // add to name cache
210             Vector<Element*>* nameVector = info()->nameCache.get(nameAttrVal.impl());
211             if (!nameVector) {
212                 nameVector = new Vector<Element*>;
213                 info()->nameCache.add(nameAttrVal.impl(), nameVector);
214             }
215             nameVector->append(element);
216         }
217     }
218
219     info()->hasNameCache = true;
220 }
221
222 }