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 "HTMLFormControlsCollection.h"
25
26#include "HTMLFieldSetElement.h"
27#include "HTMLFormElement.h"
28#include "HTMLImageElement.h"
29#include "HTMLNames.h"
30
31namespace WebCore {
32
33using 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
38HTMLFormControlsCollection::HTMLFormControlsCollection(ContainerNode& ownerNode)
39    : HTMLCollection(ownerNode, FormControls, CustomForwardOnlyTraversal)
40    , m_cachedElement(nullptr)
41    , m_cachedElementOffsetInArray(0)
42{
43    ASSERT(isHTMLFormElement(ownerNode) || isHTMLFieldSetElement(ownerNode));
44}
45
46PassRef<HTMLFormControlsCollection> HTMLFormControlsCollection::create(ContainerNode& ownerNode, CollectionType)
47{
48    return adoptRef(*new HTMLFormControlsCollection(ownerNode));
49}
50
51HTMLFormControlsCollection::~HTMLFormControlsCollection()
52{
53}
54
55const Vector<FormAssociatedElement*>& HTMLFormControlsCollection::formControlElements() const
56{
57    ASSERT(isHTMLFormElement(ownerNode()) || ownerNode().hasTagName(fieldsetTag));
58    if (isHTMLFormElement(ownerNode()))
59        return toHTMLFormElement(ownerNode()).associatedElements();
60    return toHTMLFieldSetElement(ownerNode()).associatedElements();
61}
62
63const Vector<HTMLImageElement*>& HTMLFormControlsCollection::formImageElements() const
64{
65    ASSERT(isHTMLFormElement(ownerNode()));
66    return toHTMLFormElement(ownerNode()).imageElements();
67}
68
69static unsigned findFormAssociatedElement(const Vector<FormAssociatedElement*>& elements, const Element& element)
70{
71    for (unsigned i = 0; i < elements.size(); ++i) {
72        auto& associatedElement = *elements[i];
73        if (associatedElement.isEnumeratable() && &associatedElement.asHTMLElement() == &element)
74            return i;
75    }
76    return elements.size();
77}
78
79Element* HTMLFormControlsCollection::customElementAfter(Element* current) const
80{
81    const Vector<FormAssociatedElement*>& elements = formControlElements();
82    unsigned start;
83    if (!current)
84        start = 0;
85    else if (m_cachedElement == current)
86        start = m_cachedElementOffsetInArray + 1;
87    else
88        start = findFormAssociatedElement(elements, *current) + 1;
89
90    for (unsigned i = start; i < elements.size(); ++i) {
91        FormAssociatedElement& element = *elements[i];
92        if (element.isEnumeratable()) {
93            m_cachedElement = &element.asHTMLElement();
94            m_cachedElementOffsetInArray = i;
95            return &element.asHTMLElement();
96        }
97    }
98    return nullptr;
99}
100
101static HTMLElement* firstNamedItem(const Vector<FormAssociatedElement*>& elementsArray,
102    const Vector<HTMLImageElement*>* imageElementsArray, const QualifiedName& attrName, const String& name)
103{
104    ASSERT(attrName == idAttr || attrName == nameAttr);
105
106    for (unsigned i = 0; i < elementsArray.size(); ++i) {
107        HTMLElement& element = elementsArray[i]->asHTMLElement();
108        if (elementsArray[i]->isEnumeratable() && element.fastGetAttribute(attrName) == name)
109            return &element;
110    }
111
112    if (!imageElementsArray)
113        return 0;
114
115    for (unsigned i = 0; i < imageElementsArray->size(); ++i) {
116        HTMLImageElement& element = *(*imageElementsArray)[i];
117        if (element.fastGetAttribute(attrName) == name)
118            return &element;
119    }
120
121    return nullptr;
122}
123
124Node* HTMLFormControlsCollection::namedItem(const AtomicString& name) const
125{
126    // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/nameditem.asp
127    // This method first searches for an object with a matching id
128    // attribute. If a match is not found, the method then searches for an
129    // object with a matching name attribute, but only on those elements
130    // that are allowed a name attribute.
131    const Vector<HTMLImageElement*>* imagesElements = ownerNode().hasTagName(fieldsetTag) ? nullptr : &formImageElements();
132    if (HTMLElement* item = firstNamedItem(formControlElements(), imagesElements, idAttr, name))
133        return item;
134
135    return firstNamedItem(formControlElements(), imagesElements, nameAttr, name);
136}
137
138void HTMLFormControlsCollection::updateNamedElementCache() const
139{
140    if (hasNamedElementCache())
141        return;
142
143    auto cache = std::make_unique<CollectionNamedElementCache>();
144    HashSet<AtomicStringImpl*> foundInputElements;
145    const Vector<FormAssociatedElement*>& elementsArray = formControlElements();
146
147    for (unsigned i = 0; i < elementsArray.size(); ++i) {
148        FormAssociatedElement& associatedElement = *elementsArray[i];
149        if (associatedElement.isEnumeratable()) {
150            HTMLElement& element = associatedElement.asHTMLElement();
151            const AtomicString& idAttrVal = element.getIdAttribute();
152            const AtomicString& nameAttrVal = element.getNameAttribute();
153            if (!idAttrVal.isEmpty()) {
154                cache->appendIdCache(idAttrVal, &element);
155                foundInputElements.add(idAttrVal.impl());
156            }
157            if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal) {
158                cache->appendNameCache(nameAttrVal, &element);
159                foundInputElements.add(nameAttrVal.impl());
160            }
161        }
162    }
163
164    if (isHTMLFormElement(ownerNode())) {
165        const Vector<HTMLImageElement*>& imageElementsArray = formImageElements();
166        for (unsigned i = 0; i < imageElementsArray.size(); ++i) {
167            HTMLImageElement& element = *imageElementsArray[i];
168            const AtomicString& idAttrVal = element.getIdAttribute();
169            const AtomicString& nameAttrVal = element.getNameAttribute();
170            if (!idAttrVal.isEmpty() && !foundInputElements.contains(idAttrVal.impl()))
171                cache->appendIdCache(idAttrVal, &element);
172            if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && !foundInputElements.contains(nameAttrVal.impl()))
173                cache->appendNameCache(nameAttrVal, &element);
174        }
175    }
176
177    cache->didPopulate();
178    setNameItemCache(WTF::move(cache));
179}
180
181void HTMLFormControlsCollection::invalidateCache(Document& document) const
182{
183    HTMLCollection::invalidateCache(document);
184    m_cachedElement = nullptr;
185    m_cachedElementOffsetInArray = 0;
186}
187
188}
189