HTMLFormCollection.cpp [plain text]
#include "config.h"
#include "HTMLFormCollection.h"
#include "HTMLGenericFormElement.h"
#include "HTMLFormElement.h"
#include "HTMLImageElement.h"
#include "HTMLNames.h"
namespace WebCore {
using namespace HTMLNames;
HTMLFormCollection::HTMLFormCollection(Node* _base)
: HTMLCollection(_base, HTMLCollection::Type(0))
{
HTMLFormElement *formBase = static_cast<HTMLFormElement*>(m_base.get());
if (!formBase->collectionInfo)
formBase->collectionInfo = new CollectionInfo();
info = formBase->collectionInfo;
}
HTMLFormCollection::~HTMLFormCollection()
{
}
unsigned HTMLFormCollection::calcLength() const
{
return static_cast<HTMLFormElement*>(m_base.get())->length();
}
Node *HTMLFormCollection::item(unsigned index) const
{
resetCollectionInfo();
if (info->current && info->position == index)
return info->current;
if (info->haslength && info->length <= index)
return 0;
if (!info->current || info->position > index) {
info->current = 0;
info->position = 0;
info->elementsArrayPosition = 0;
}
Vector<HTMLGenericFormElement*>& l = static_cast<HTMLFormElement*>(m_base.get())->formElements;
unsigned currentIndex = info->position;
for (unsigned i = info->elementsArrayPosition; i < l.size(); i++) {
if (l[i]->isEnumeratable() ) {
if (index == currentIndex) {
info->position = index;
info->current = l[i];
info->elementsArrayPosition = i;
return l[i];
}
currentIndex++;
}
}
return 0;
}
Node* HTMLFormCollection::getNamedItem(Node*, const QualifiedName& attrName, const String& name, bool caseSensitive) const
{
info->position = 0;
return getNamedFormItem(attrName, name, 0, caseSensitive);
}
Node* HTMLFormCollection::getNamedFormItem(const QualifiedName& attrName, const String& name, int duplicateNumber, bool caseSensitive) const
{
if (m_base->isElementNode()) {
HTMLElement* baseElement = static_cast<HTMLElement*>(m_base.get());
bool foundInputElements = false;
if (baseElement->hasLocalName(formTag)) {
HTMLFormElement* f = static_cast<HTMLFormElement*>(baseElement);
for (unsigned i = 0; i < f->formElements.size(); ++i) {
HTMLGenericFormElement* e = f->formElements[i];
if (e->isEnumeratable()) {
bool found;
if (caseSensitive)
found = e->getAttribute(attrName) == name;
else
found = e->getAttribute(attrName).domString().lower() == name.lower();
if (found) {
foundInputElements = true;
if (!duplicateNumber)
return e;
--duplicateNumber;
}
}
}
}
if (!foundInputElements) {
HTMLFormElement* f = static_cast<HTMLFormElement*>(baseElement);
for(unsigned i = 0; i < f->imgElements.size(); ++i)
{
HTMLImageElement* e = f->imgElements[i];
bool found;
if (caseSensitive)
found = e->getAttribute(attrName) == name;
else
found = e->getAttribute(attrName).domString().lower() == name.lower();
if (found) {
if (!duplicateNumber)
return e;
--duplicateNumber;
}
}
}
}
return 0;
}
Node * HTMLFormCollection::firstItem() const
{
return item(0);
}
Node * HTMLFormCollection::nextItem() const
{
return item(info->position + 1);
}
Node * HTMLFormCollection::nextNamedItemInternal(const String &name) const
{
Node *retval = getNamedFormItem( idsDone ? nameAttr : idAttr, name, ++info->position, true );
if (retval)
return retval;
if (idsDone) return 0;
idsDone = true;
return getNamedItem(m_base->firstChild(), nameAttr, name, true);
}
Node *HTMLFormCollection::namedItem( const String &name, bool caseSensitive ) const
{
resetCollectionInfo();
idsDone = false;
info->current = getNamedItem(m_base->firstChild(), idAttr, name, true);
if(info->current)
return info->current;
idsDone = true;
info->current = getNamedItem(m_base->firstChild(), nameAttr, name, true);
return info->current;
}
Node *HTMLFormCollection::nextNamedItem( const String &name ) const
{
Node *impl = nextNamedItemInternal( name );
if (!idsDone) return impl;
bool ok = false;
while (impl && !ok)
{
if(impl->isElementNode())
{
HTMLElement *e = static_cast<HTMLElement *>(impl);
ok = (e->getAttribute(idAttr) != name);
if (!ok)
impl = nextNamedItemInternal( name );
} else ok = true;
}
return impl;
}
void HTMLFormCollection::updateNameCache() const
{
if (info->hasNameCache)
return;
HashSet<AtomicStringImpl*> foundInputElements;
if (!m_base->hasTagName(formTag)) {
info->hasNameCache = true;
return;
}
HTMLElement* baseElement = static_cast<HTMLElement*>(m_base.get());
HTMLFormElement* f = static_cast<HTMLFormElement*>(baseElement);
for (unsigned i = 0; i < f->formElements.size(); ++i) {
HTMLGenericFormElement* e = f->formElements[i];
if (e->isEnumeratable()) {
const AtomicString& idAttrVal = e->getAttribute(idAttr);
const AtomicString& nameAttrVal = e->getAttribute(nameAttr);
if (!idAttrVal.isEmpty()) {
Vector<Node*>* idVector = info->idCache.get(idAttrVal.impl());
if (!idVector) {
idVector = new Vector<Node*>;
info->idCache.add(idAttrVal.impl(), idVector);
}
idVector->append(e);
foundInputElements.add(idAttrVal.impl());
}
if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal) {
Vector<Node*>* nameVector = info->nameCache.get(nameAttrVal.impl());
if (!nameVector) {
nameVector = new Vector<Node*>;
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(idAttr);
const AtomicString& nameAttrVal = e->getAttribute(nameAttr);
if (!idAttrVal.isEmpty() && !foundInputElements.contains(idAttrVal.impl())) {
Vector<Node*>* idVector = info->idCache.get(idAttrVal.impl());
if (!idVector) {
idVector = new Vector<Node*>;
info->idCache.add(idAttrVal.impl(), idVector);
}
idVector->append(e);
}
if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && !foundInputElements.contains(nameAttrVal.impl())) {
Vector<Node*>* nameVector = info->nameCache.get(nameAttrVal.impl());
if (!nameVector) {
nameVector = new Vector<Node*>;
info->nameCache.add(nameAttrVal.impl(), nameVector);
}
nameVector->append(e);
}
}
info->hasNameCache = true;
}
}