Element* HTMLFormCollection::getNamedFormItem(const QualifiedName& attrName, const String& name, int duplicateNumber) const
{
    HTMLFormElement* form = static_cast<HTMLFormElement*>(base());

    bool foundInputElements = false;
    for (unsigned i = 0; i < form->formElements.size(); ++i) {
        HTMLFormControlElement* e = form->formElements[i];
        const QualifiedName& attributeName = (attrName == idAttr) ? e->idAttributeName() : attrName;
        if (e->isEnumeratable() && e->getAttribute(attributeName) == name) {
            foundInputElements = true;
            if (!duplicateNumber)
                return e;
            --duplicateNumber;
        }
    }

    if (!foundInputElements) {
        for (unsigned i = 0; i < form->imgElements.size(); ++i) {
            HTMLImageElement* e = form->imgElements[i];
            const QualifiedName& attributeName = (attrName == idAttr) ? e->idAttributeName() : attrName;
            if (e->getAttribute(attributeName) == name) {
                if (!duplicateNumber)
                    return e;
                --duplicateNumber;
            }
        }
    }

    return 0;
}
void HTMLFormCollection::updateNameCache() const
{
    if (info()->hasNameCache)
        return;

    HashSet<AtomicStringImpl*> foundInputElements;

    HTMLFormElement* f = static_cast<HTMLFormElement*>(base());

    for (unsigned i = 0; i < f->formElements.size(); ++i) {
        HTMLFormControlElement* e = f->formElements[i];
        if (e->isEnumeratable()) {
            const AtomicString& idAttrVal = e->getAttribute(e->idAttributeName());
            const AtomicString& nameAttrVal = e->getAttribute(nameAttr);
            if (!idAttrVal.isEmpty()) {
                // add to id cache
                Vector<Element*>* idVector = info()->idCache.get(idAttrVal.impl());
                if (!idVector) {
                    idVector = new Vector<Element*>;
                    info()->idCache.add(idAttrVal.impl(), idVector);
                }
                idVector->append(e);
                foundInputElements.add(idAttrVal.impl());
            }
            if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal) {
                // add to name cache
                Vector<Element*>* nameVector = info()->nameCache.get(nameAttrVal.impl());
                if (!nameVector) {
                    nameVector = new Vector<Element*>;
                    info()->nameCache.add(nameAttrVal.impl(), nameVector);
                }
                nameVector->append(e);
                foundInputElements.add(nameAttrVal.impl());
            }
        }
    }

    for (unsigned i = 0; i < f->imgElements.size(); ++i) {
        HTMLImageElement* e = f->imgElements[i];
        const AtomicString& idAttrVal = e->getAttribute(e->idAttributeName());
        const AtomicString& nameAttrVal = e->getAttribute(nameAttr);
        if (!idAttrVal.isEmpty() && !foundInputElements.contains(idAttrVal.impl())) {
            // add to id cache
            Vector<Element*>* idVector = info()->idCache.get(idAttrVal.impl());
            if (!idVector) {
                idVector = new Vector<Element*>;
                info()->idCache.add(idAttrVal.impl(), idVector);
            }
            idVector->append(e);
        }
        if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && !foundInputElements.contains(nameAttrVal.impl())) {
            // add to name cache
            Vector<Element*>* nameVector = info()->nameCache.get(nameAttrVal.impl());
            if (!nameVector) {
                nameVector = new Vector<Element*>;
                info()->nameCache.add(nameAttrVal.impl(), nameVector);
            }
            nameVector->append(e);
        }
    }

    info()->hasNameCache = true;
}