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, 2011, 2012 Apple Inc. All rights reserved.
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.
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.
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.
24 #include "HTMLFormCollection.h"
26 #include "HTMLFieldSetElement.h"
27 #include "HTMLFormControlElement.h"
28 #include "HTMLFormElement.h"
29 #include "HTMLImageElement.h"
30 #include "HTMLNames.h"
34 using namespace HTMLNames;
36 // Since the collections are to be "live", we have to do the
37 // calculation every time if anything has changed.
39 HTMLFormCollection::HTMLFormCollection(Element* base)
40 : HTMLCollection(base, FormControls)
42 ASSERT(base->hasTagName(formTag) || base->hasTagName(fieldsetTag));
45 PassOwnPtr<HTMLFormCollection> HTMLFormCollection::create(Element* base)
47 return adoptPtr(new HTMLFormCollection(base));
50 HTMLFormCollection::~HTMLFormCollection()
54 const Vector<FormAssociatedElement*>& HTMLFormCollection::formControlElements() const
57 ASSERT(base()->hasTagName(formTag) || base()->hasTagName(fieldsetTag));
58 if (base()->hasTagName(formTag))
59 return static_cast<HTMLFormElement*>(base())->associatedElements();
60 return static_cast<HTMLFieldSetElement*>(base())->associatedElements();
63 const Vector<HTMLImageElement*>& HTMLFormCollection::formImageElements() const
66 ASSERT(base()->hasTagName(formTag));
67 return static_cast<HTMLFormElement*>(base())->imageElements();
70 unsigned HTMLFormCollection::numberOfFormControlElements() const
73 ASSERT(base()->hasTagName(formTag) || base()->hasTagName(fieldsetTag));
74 if (base()->hasTagName(formTag))
75 return static_cast<HTMLFormElement*>(base())->length();
76 return static_cast<HTMLFieldSetElement*>(base())->length();
79 unsigned HTMLFormCollection::calcLength() const
81 return numberOfFormControlElements();
84 Node* HTMLFormCollection::item(unsigned index) const
86 invalidateCacheIfNeeded();
88 if (isItemCacheValid() && cachedItemOffset() == index)
91 if (isLengthCacheValid() && cachedLength() <= index)
94 if (!isItemCacheValid() || cachedItemOffset() > index)
95 setItemCache(0, 0, 0);
97 const Vector<FormAssociatedElement*>& elementsArray = formControlElements();
98 unsigned currentIndex = cachedItemOffset();
100 for (unsigned i = cachedElementsArrayOffset(); i < elementsArray.size(); i++) {
101 if (elementsArray[i]->isEnumeratable()) {
102 HTMLElement* element = toHTMLElement(elementsArray[i]);
103 if (index == currentIndex) {
104 setItemCache(element, index, i);
115 static HTMLElement* firstNamedItem(const Vector<FormAssociatedElement*>& elementsArray,
116 const Vector<HTMLImageElement*>* imageElementsArray, const QualifiedName& attrName, const String& name)
118 ASSERT(attrName == idAttr || attrName == nameAttr);
120 for (unsigned i = 0; i < elementsArray.size(); ++i) {
121 HTMLElement* element = toHTMLElement(elementsArray[i]);
122 if (elementsArray[i]->isEnumeratable() && element->fastGetAttribute(attrName) == name)
126 if (!imageElementsArray)
129 for (unsigned i = 0; i < imageElementsArray->size(); ++i) {
130 HTMLImageElement* element = (*imageElementsArray)[i];
131 if (element->fastGetAttribute(attrName) == name)
138 Node* HTMLFormCollection::namedItem(const AtomicString& name) const
140 // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/nameditem.asp
141 // This method first searches for an object with a matching id
142 // attribute. If a match is not found, the method then searches for an
143 // object with a matching name attribute, but only on those elements
144 // that are allowed a name attribute.
145 invalidateCacheIfNeeded();
146 const Vector<HTMLImageElement*>* imagesElements = base()->hasTagName(fieldsetTag) ? 0 : &formImageElements();
147 if (HTMLElement* item = firstNamedItem(formControlElements(), imagesElements, idAttr, name))
150 return firstNamedItem(formControlElements(), imagesElements, nameAttr, name);
153 void HTMLFormCollection::updateNameCache() const
158 HashSet<AtomicStringImpl*> foundInputElements;
160 const Vector<FormAssociatedElement*>& elementsArray = formControlElements();
162 for (unsigned i = 0; i < elementsArray.size(); ++i) {
163 FormAssociatedElement* associatedElement = elementsArray[i];
164 if (associatedElement->isEnumeratable()) {
165 HTMLElement* element = toHTMLElement(associatedElement);
166 const AtomicString& idAttrVal = element->getIdAttribute();
167 const AtomicString& nameAttrVal = element->getNameAttribute();
168 if (!idAttrVal.isEmpty()) {
169 appendIdCache(idAttrVal, element);
170 foundInputElements.add(idAttrVal.impl());
172 if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal) {
173 appendNameCache(nameAttrVal, element);
174 foundInputElements.add(nameAttrVal.impl());
179 if (base()->hasTagName(formTag)) {
180 const Vector<HTMLImageElement*>& imageElementsArray = formImageElements();
181 for (unsigned i = 0; i < imageElementsArray.size(); ++i) {
182 HTMLImageElement* element = imageElementsArray[i];
183 const AtomicString& idAttrVal = element->getIdAttribute();
184 const AtomicString& nameAttrVal = element->getNameAttribute();
185 if (!idAttrVal.isEmpty() && !foundInputElements.contains(idAttrVal.impl()))
186 appendIdCache(idAttrVal, element);
187 if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && !foundInputElements.contains(nameAttrVal.impl()))
188 appendNameCache(nameAttrVal, element);