• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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, 2011, 2012 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 "core/html/HTMLFormControlsCollection.h"
25 
26 #include "HTMLNames.h"
27 #include "core/html/HTMLFieldSetElement.h"
28 #include "core/html/HTMLFormElement.h"
29 #include "core/html/HTMLImageElement.h"
30 
31 namespace WebCore {
32 
33 using namespace HTMLNames;
34 
35 // Since the collections are to be "live", we have to do the
36 // calculation every time if anything has changed.
37 
HTMLFormControlsCollection(Node * ownerNode)38 HTMLFormControlsCollection::HTMLFormControlsCollection(Node* ownerNode)
39     : HTMLCollection(ownerNode, FormControls, OverridesItemAfter)
40 {
41     ASSERT(ownerNode->hasTagName(formTag) || ownerNode->hasTagName(fieldsetTag));
42     ScriptWrappable::init(this);
43 }
44 
create(Node * ownerNode,CollectionType)45 PassRefPtr<HTMLFormControlsCollection> HTMLFormControlsCollection::create(Node* ownerNode, CollectionType)
46 {
47     return adoptRef(new HTMLFormControlsCollection(ownerNode));
48 }
49 
~HTMLFormControlsCollection()50 HTMLFormControlsCollection::~HTMLFormControlsCollection()
51 {
52 }
53 
formControlElements() const54 const Vector<FormAssociatedElement*>& HTMLFormControlsCollection::formControlElements() const
55 {
56     ASSERT(ownerNode());
57     ASSERT(ownerNode()->hasTagName(formTag) || ownerNode()->hasTagName(fieldsetTag));
58     if (ownerNode()->hasTagName(formTag))
59         return toHTMLFormElement(ownerNode())->associatedElements();
60     return toHTMLFieldSetElement(ownerNode())->associatedElements();
61 }
62 
formImageElements() const63 const Vector<HTMLImageElement*>& HTMLFormControlsCollection::formImageElements() const
64 {
65     ASSERT(ownerNode());
66     return toHTMLFormElement(ownerNode())->imageElements();
67 }
68 
virtualItemAfter(unsigned & offset,Element * previousItem) const69 Element* HTMLFormControlsCollection::virtualItemAfter(unsigned& offset, Element* previousItem) const
70 {
71     const Vector<FormAssociatedElement*>& elementsArray = formControlElements();
72     if (previousItem)
73         offset++;
74     while (offset < elementsArray.size()) {
75         FormAssociatedElement* element = elementsArray[offset];
76         if (element->isEnumeratable())
77             return toHTMLElement(element);
78         offset++;
79     }
80     return 0;
81 }
82 
firstNamedItem(const Vector<FormAssociatedElement * > & elementsArray,const Vector<HTMLImageElement * > * imageElementsArray,const QualifiedName & attrName,const String & name)83 static HTMLElement* firstNamedItem(const Vector<FormAssociatedElement*>& elementsArray,
84     const Vector<HTMLImageElement*>* imageElementsArray, const QualifiedName& attrName, const String& name)
85 {
86     ASSERT(attrName == idAttr || attrName == nameAttr);
87 
88     for (unsigned i = 0; i < elementsArray.size(); ++i) {
89         HTMLElement* element = toHTMLElement(elementsArray[i]);
90         if (elementsArray[i]->isEnumeratable() && element->fastGetAttribute(attrName) == name)
91             return element;
92     }
93 
94     if (!imageElementsArray)
95         return 0;
96 
97     for (unsigned i = 0; i < imageElementsArray->size(); ++i) {
98         HTMLImageElement* element = (*imageElementsArray)[i];
99         if (element->fastGetAttribute(attrName) == name)
100             return element;
101     }
102 
103     return 0;
104 }
105 
namedItem(const AtomicString & name) const106 Node* HTMLFormControlsCollection::namedItem(const AtomicString& name) const
107 {
108     // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/nameditem.asp
109     // This method first searches for an object with a matching id
110     // attribute. If a match is not found, the method then searches for an
111     // object with a matching name attribute, but only on those elements
112     // that are allowed a name attribute.
113     const Vector<HTMLImageElement*>* imagesElements = ownerNode()->hasTagName(fieldsetTag) ? 0 : &formImageElements();
114     if (HTMLElement* item = firstNamedItem(formControlElements(), imagesElements, idAttr, name))
115         return item;
116 
117     return firstNamedItem(formControlElements(), imagesElements, nameAttr, name);
118 }
119 
updateNameCache() const120 void HTMLFormControlsCollection::updateNameCache() const
121 {
122     if (hasNameCache())
123         return;
124 
125     HashSet<StringImpl*> foundInputElements;
126 
127     const Vector<FormAssociatedElement*>& elementsArray = formControlElements();
128 
129     for (unsigned i = 0; i < elementsArray.size(); ++i) {
130         FormAssociatedElement* associatedElement = elementsArray[i];
131         if (associatedElement->isEnumeratable()) {
132             HTMLElement* element = toHTMLElement(associatedElement);
133             const AtomicString& idAttrVal = element->getIdAttribute();
134             const AtomicString& nameAttrVal = element->getNameAttribute();
135             if (!idAttrVal.isEmpty()) {
136                 appendIdCache(idAttrVal, element);
137                 foundInputElements.add(idAttrVal.impl());
138             }
139             if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal) {
140                 appendNameCache(nameAttrVal, element);
141                 foundInputElements.add(nameAttrVal.impl());
142             }
143         }
144     }
145 
146     if (ownerNode()->hasTagName(formTag)) {
147         const Vector<HTMLImageElement*>& imageElementsArray = formImageElements();
148         for (unsigned i = 0; i < imageElementsArray.size(); ++i) {
149             HTMLImageElement* element = imageElementsArray[i];
150             const AtomicString& idAttrVal = element->getIdAttribute();
151             const AtomicString& nameAttrVal = element->getNameAttribute();
152             if (!idAttrVal.isEmpty() && !foundInputElements.contains(idAttrVal.impl()))
153                 appendIdCache(idAttrVal, element);
154             if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && !foundInputElements.contains(nameAttrVal.impl()))
155                 appendNameCache(nameAttrVal, element);
156         }
157     }
158 
159     setHasNameCache();
160 }
161 
namedGetter(const AtomicString & name,bool & returnValue0Enabled,RefPtr<RadioNodeList> & returnValue0,bool & returnValue1Enabled,RefPtr<Node> & returnValue1)162 void HTMLFormControlsCollection::namedGetter(const AtomicString& name, bool& returnValue0Enabled, RefPtr<RadioNodeList>& returnValue0, bool& returnValue1Enabled, RefPtr<Node>& returnValue1)
163 {
164     Vector<RefPtr<Node> > namedItems;
165     this->namedItems(name, namedItems);
166 
167     if (!namedItems.size())
168         return;
169 
170     if (namedItems.size() == 1) {
171         returnValue1Enabled = true;
172         returnValue1 = namedItems.at(0);
173         return;
174     }
175 
176     returnValue0Enabled = true;
177     returnValue0 = this->ownerNode()->radioNodeList(name);
178 }
179 
180 }
181