Home | History | Annotate | Download | only in html
      1 /*
      2  * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org)
      3  *           (C) 1999 Antti Koivisto (koivisto (at) kde.org)
      4  * Copyright (C) 2003, 2004, 2005, 2006, 2007 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->collectionInfo)
     42         form->collectionInfo = new CollectionCache;
     43     return form->collectionInfo;
     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<HTMLFormControlElement*>& l = static_cast<HTMLFormElement*>(base())->formElements;
     82     unsigned currentIndex = info()->position;
     83 
     84     for (unsigned i = info()->elementsArrayPosition; i < l.size(); i++) {
     85         if (l[i]->isEnumeratable() ) {
     86             if (index == currentIndex) {
     87                 info()->position = index;
     88                 info()->current = l[i];
     89                 info()->elementsArrayPosition = i;
     90                 return l[i];
     91             }
     92 
     93             currentIndex++;
     94         }
     95     }
     96 
     97     return 0;
     98 }
     99 
    100 Element* HTMLFormCollection::getNamedItem(const QualifiedName& attrName, const AtomicString& name) const
    101 {
    102     info()->position = 0;
    103     return getNamedFormItem(attrName, name, 0);
    104 }
    105 
    106 Element* HTMLFormCollection::getNamedFormItem(const QualifiedName& attrName, const String& name, int duplicateNumber) const
    107 {
    108     HTMLFormElement* form = static_cast<HTMLFormElement*>(base());
    109 
    110     bool foundInputElements = false;
    111     for (unsigned i = 0; i < form->formElements.size(); ++i) {
    112         HTMLFormControlElement* e = form->formElements[i];
    113         const QualifiedName& attributeName = (attrName == idAttr) ? e->idAttributeName() : attrName;
    114         if (e->isEnumeratable() && e->getAttribute(attributeName) == name) {
    115             foundInputElements = true;
    116             if (!duplicateNumber)
    117                 return e;
    118             --duplicateNumber;
    119         }
    120     }
    121 
    122     if (!foundInputElements) {
    123         for (unsigned i = 0; i < form->imgElements.size(); ++i) {
    124             HTMLImageElement* e = form->imgElements[i];
    125             const QualifiedName& attributeName = (attrName == idAttr) ? e->idAttributeName() : attrName;
    126             if (e->getAttribute(attributeName) == name) {
    127                 if (!duplicateNumber)
    128                     return e;
    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 Element* HTMLFormCollection::nextNamedItemInternal(const String &name) const
    143 {
    144     Element* retval = getNamedFormItem(m_idsDone ? nameAttr : idAttr, name, ++info()->position);
    145     if (retval)
    146         return retval;
    147     if (m_idsDone) // we're done
    148         return 0;
    149     // After doing id, do name
    150     m_idsDone = true;
    151     return getNamedItem(nameAttr, name);
    152 }
    153 
    154 Node* HTMLFormCollection::namedItem(const AtomicString& name) const
    155 {
    156     // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/nameditem.asp
    157     // This method first searches for an object with a matching id
    158     // attribute. If a match is not found, the method then searches for an
    159     // object with a matching name attribute, but only on those elements
    160     // that are allowed a name attribute.
    161     resetCollectionInfo();
    162     m_idsDone = false;
    163     info()->current = getNamedItem(idAttr, name);
    164     if (info()->current)
    165         return info()->current;
    166     m_idsDone = true;
    167     info()->current = getNamedItem(nameAttr, name);
    168     return info()->current;
    169 }
    170 
    171 Node* HTMLFormCollection::nextNamedItem(const AtomicString& name) const
    172 {
    173     // The nextNamedItemInternal function can return the same item twice if it has
    174     // both an id and name that are equal to the name parameter. So this function
    175     // checks if we are on the nameAttr half of the iteration and skips over any
    176     // that also have the same idAttributeName.
    177     Element* impl = nextNamedItemInternal(name);
    178     if (m_idsDone)
    179         while (impl && impl->getAttribute(impl->idAttributeName()) == name)
    180             impl = nextNamedItemInternal(name);
    181     return impl;
    182 }
    183 
    184 void HTMLFormCollection::updateNameCache() const
    185 {
    186     if (info()->hasNameCache)
    187         return;
    188 
    189     HashSet<AtomicStringImpl*> foundInputElements;
    190 
    191     HTMLFormElement* f = static_cast<HTMLFormElement*>(base());
    192 
    193     for (unsigned i = 0; i < f->formElements.size(); ++i) {
    194         HTMLFormControlElement* e = f->formElements[i];
    195         if (e->isEnumeratable()) {
    196             const AtomicString& idAttrVal = e->getAttribute(e->idAttributeName());
    197             const AtomicString& nameAttrVal = e->getAttribute(nameAttr);
    198             if (!idAttrVal.isEmpty()) {
    199                 // add to id cache
    200                 Vector<Element*>* idVector = info()->idCache.get(idAttrVal.impl());
    201                 if (!idVector) {
    202                     idVector = new Vector<Element*>;
    203                     info()->idCache.add(idAttrVal.impl(), idVector);
    204                 }
    205                 idVector->append(e);
    206                 foundInputElements.add(idAttrVal.impl());
    207             }
    208             if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal) {
    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(e);
    216                 foundInputElements.add(nameAttrVal.impl());
    217             }
    218         }
    219     }
    220 
    221     for (unsigned i = 0; i < f->imgElements.size(); ++i) {
    222         HTMLImageElement* e = f->imgElements[i];
    223         const AtomicString& idAttrVal = e->getAttribute(e->idAttributeName());
    224         const AtomicString& nameAttrVal = e->getAttribute(nameAttr);
    225         if (!idAttrVal.isEmpty() && !foundInputElements.contains(idAttrVal.impl())) {
    226             // add to id cache
    227             Vector<Element*>* idVector = info()->idCache.get(idAttrVal.impl());
    228             if (!idVector) {
    229                 idVector = new Vector<Element*>;
    230                 info()->idCache.add(idAttrVal.impl(), idVector);
    231             }
    232             idVector->append(e);
    233         }
    234         if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && !foundInputElements.contains(nameAttrVal.impl())) {
    235             // add to name cache
    236             Vector<Element*>* nameVector = info()->nameCache.get(nameAttrVal.impl());
    237             if (!nameVector) {
    238                 nameVector = new Vector<Element*>;
    239                 info()->nameCache.add(nameAttrVal.impl(), nameVector);
    240             }
    241             nameVector->append(e);
    242         }
    243     }
    244 
    245     info()->hasNameCache = true;
    246 }
    247 
    248 }
    249