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.
5 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Library General Public License for more details.
17 * You should have received a copy of the GNU Library General Public License
18 * along with this library; see the file COPYING.LIB. If not, write to
19 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
20 * Boston, MA 02110-1301, USA.
25 #include "core/html/HTMLFormControlsCollection.h"
27 #include "HTMLNames.h"
28 #include "core/frame/UseCounter.h"
29 #include "core/html/HTMLFieldSetElement.h"
30 #include "core/html/HTMLFormElement.h"
31 #include "core/html/HTMLImageElement.h"
32 #include "wtf/HashSet.h"
36 using namespace HTMLNames;
38 // Since the collections are to be "live", we have to do the
39 // calculation every time if anything has changed.
41 HTMLFormControlsCollection::HTMLFormControlsCollection(ContainerNode& ownerNode)
42 : HTMLCollection(ownerNode, FormControls, OverridesItemAfter)
44 , m_cachedElementOffsetInArray(0)
46 ASSERT(isHTMLFormElement(ownerNode) || isHTMLFieldSetElement(ownerNode));
47 ScriptWrappable::init(this);
50 PassRefPtr<HTMLFormControlsCollection> HTMLFormControlsCollection::create(ContainerNode& ownerNode, CollectionType)
52 return adoptRef(new HTMLFormControlsCollection(ownerNode));
55 HTMLFormControlsCollection::~HTMLFormControlsCollection()
59 const FormAssociatedElement::List& HTMLFormControlsCollection::formControlElements() const
61 ASSERT(isHTMLFormElement(ownerNode()) || isHTMLFieldSetElement(ownerNode()));
62 if (isHTMLFormElement(ownerNode()))
63 return toHTMLFormElement(ownerNode()).associatedElements();
64 return toHTMLFieldSetElement(ownerNode()).associatedElements();
67 const Vector<HTMLImageElement*>& HTMLFormControlsCollection::formImageElements() const
69 return toHTMLFormElement(ownerNode()).imageElements();
72 static unsigned findFormAssociatedElement(const FormAssociatedElement::List& associatedElements, Element* element)
75 for (; i < associatedElements.size(); ++i) {
76 FormAssociatedElement* associatedElement = associatedElements[i];
77 if (associatedElement->isEnumeratable() && toHTMLElement(associatedElement) == element)
83 Element* HTMLFormControlsCollection::virtualItemAfter(Element* previous) const
85 const FormAssociatedElement::List& associatedElements = formControlElements();
89 else if (m_cachedElement == previous)
90 offset = m_cachedElementOffsetInArray + 1;
92 offset = findFormAssociatedElement(associatedElements, previous) + 1;
94 for (unsigned i = offset; i < associatedElements.size(); ++i) {
95 FormAssociatedElement* associatedElement = associatedElements[i];
96 if (associatedElement->isEnumeratable()) {
97 m_cachedElement = toHTMLElement(associatedElement);
98 m_cachedElementOffsetInArray = i;
99 return m_cachedElement;
105 void HTMLFormControlsCollection::invalidateCache(Document* oldDocument) const
107 HTMLCollection::invalidateCache(oldDocument);
109 m_cachedElementOffsetInArray = 0;
112 static HTMLElement* firstNamedItem(const FormAssociatedElement::List& elementsArray,
113 const Vector<HTMLImageElement*>* imageElementsArray, const QualifiedName& attrName, const String& name)
115 ASSERT(attrName == idAttr || attrName == nameAttr);
117 for (unsigned i = 0; i < elementsArray.size(); ++i) {
118 HTMLElement* element = toHTMLElement(elementsArray[i]);
119 if (elementsArray[i]->isEnumeratable() && element->fastGetAttribute(attrName) == name)
123 if (!imageElementsArray)
126 for (unsigned i = 0; i < imageElementsArray->size(); ++i) {
127 HTMLImageElement* element = (*imageElementsArray)[i];
128 if (element->fastGetAttribute(attrName) == name) {
129 UseCounter::count(element->document(), UseCounter::FormNameAccessForImageElement);
137 Element* HTMLFormControlsCollection::namedItem(const AtomicString& name) const
139 // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/nameditem.asp
140 // This method first searches for an object with a matching id
141 // attribute. If a match is not found, the method then searches for an
142 // object with a matching name attribute, but only on those elements
143 // that are allowed a name attribute.
144 const Vector<HTMLImageElement*>* imagesElements = isHTMLFieldSetElement(ownerNode()) ? 0 : &formImageElements();
145 if (HTMLElement* item = firstNamedItem(formControlElements(), imagesElements, idAttr, name))
148 return firstNamedItem(formControlElements(), imagesElements, nameAttr, name);
151 void HTMLFormControlsCollection::updateIdNameCache() const
153 if (hasValidIdNameCache())
156 OwnPtr<NamedItemCache> cache = NamedItemCache::create();
157 HashSet<StringImpl*> foundInputElements;
159 const FormAssociatedElement::List& elementsArray = formControlElements();
161 for (unsigned i = 0; i < elementsArray.size(); ++i) {
162 FormAssociatedElement* associatedElement = elementsArray[i];
163 if (associatedElement->isEnumeratable()) {
164 HTMLElement* element = toHTMLElement(associatedElement);
165 const AtomicString& idAttrVal = element->getIdAttribute();
166 const AtomicString& nameAttrVal = element->getNameAttribute();
167 if (!idAttrVal.isEmpty()) {
168 cache->addElementWithId(idAttrVal, element);
169 foundInputElements.add(idAttrVal.impl());
171 if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal) {
172 cache->addElementWithName(nameAttrVal, element);
173 foundInputElements.add(nameAttrVal.impl());
178 if (isHTMLFormElement(ownerNode())) {
179 const Vector<HTMLImageElement*>& imageElementsArray = formImageElements();
180 for (unsigned i = 0; i < imageElementsArray.size(); ++i) {
181 HTMLImageElement* element = imageElementsArray[i];
182 const AtomicString& idAttrVal = element->getIdAttribute();
183 const AtomicString& nameAttrVal = element->getNameAttribute();
184 if (!idAttrVal.isEmpty() && !foundInputElements.contains(idAttrVal.impl()))
185 cache->addElementWithId(idAttrVal, element);
186 if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && !foundInputElements.contains(nameAttrVal.impl()))
187 cache->addElementWithName(nameAttrVal, element);
191 // Set the named item cache last as traversing the tree may cause cache invalidation.
192 setNamedItemCache(cache.release());
195 void HTMLFormControlsCollection::namedGetter(const AtomicString& name, bool& radioNodeListEnabled, RefPtr<RadioNodeList>& radioNodeList, bool& elementEnabled, RefPtr<Element>& element)
197 Vector<RefPtr<Element> > namedItems;
198 this->namedItems(name, namedItems);
200 if (namedItems.isEmpty())
203 if (namedItems.size() == 1) {
204 elementEnabled = true;
205 element = namedItems.first();
209 radioNodeListEnabled = true;
210 radioNodeList = ownerNode().radioNodeList(name);
213 void HTMLFormControlsCollection::supportedPropertyNames(Vector<String>& names)
215 // http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-interfaces.html#htmlformcontrolscollection-0:
216 // The supported property names consist of the non-empty values of all the id and name attributes
217 // of all the elements represented by the collection, in tree order, ignoring later duplicates,
218 // with the id of an element preceding its name if it contributes both, they differ from each
219 // other, and neither is the duplicate of an earlier entry.
220 HashSet<AtomicString> existingNames;
221 unsigned length = this->length();
222 for (unsigned i = 0; i < length; ++i) {
223 Element* element = item(i);
225 const AtomicString& idAttribute = element->getIdAttribute();
226 if (!idAttribute.isEmpty()) {
227 HashSet<AtomicString>::AddResult addResult = existingNames.add(idAttribute);
228 if (addResult.isNewEntry)
229 names.append(idAttribute);
231 const AtomicString& nameAttribute = element->getNameAttribute();
232 if (!nameAttribute.isEmpty()) {
233 HashSet<AtomicString>::AddResult addResult = existingNames.add(nameAttribute);
234 if (addResult.isNewEntry)
235 names.append(nameAttribute);