2 * This file is part of the DOM implementation for KDE.
4 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
5 * (C) 1999 Antti Koivisto (koivisto@kde.org)
6 * Copyright (C) 2003 Apple Computer, Inc.
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Library General Public License for more details.
18 * You should have received a copy of the GNU Library General Public License
19 * along with this library; see the file COPYING.LIB. If not, write to
20 * the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 * Boston, MA 02111-1307, USA.
24 // -------------------------------------------------------------------------
25 #include "html/html_miscimpl.h"
26 #include "html/html_formimpl.h"
27 #include "html/html_imageimpl.h"
28 #include "html/html_documentimpl.h"
30 #include "misc/htmlhashes.h"
31 #include "dom/dom_node.h"
37 HTMLBaseFontElementImpl::HTMLBaseFontElementImpl(DocumentPtr *doc)
38 : HTMLElementImpl(doc)
42 HTMLBaseFontElementImpl::~HTMLBaseFontElementImpl()
46 NodeImpl::Id HTMLBaseFontElementImpl::id() const
51 // -------------------------------------------------------------------------
53 HTMLCollectionImpl::HTMLCollectionImpl(NodeImpl *_base, int _type)
59 info = base->isDocumentNode() ? static_cast<HTMLDocumentImpl*>(base->getDocument())->collectionInfo(type) : 0;
62 HTMLCollectionImpl::~HTMLCollectionImpl()
67 HTMLCollectionImpl::CollectionInfo::CollectionInfo() :
70 idCache.setAutoDelete(true);
71 nameCache.setAutoDelete(true);
75 void HTMLCollectionImpl::CollectionInfo::reset()
81 elementsArrayPosition = 0;
87 void HTMLCollectionImpl::resetCollectionInfo() const
89 unsigned int docversion = static_cast<HTMLDocumentImpl*>(base->getDocument())->domTreeVersion();
92 info = new CollectionInfo;
93 info->version = docversion;
97 if (info->version != docversion) {
99 info->version = docversion;
104 NodeImpl *HTMLCollectionImpl::traverseNextItem(NodeImpl *current) const
106 current = current->traverseNextNode();
109 if(current->nodeType() == Node::ELEMENT_NODE) {
112 HTMLElementImpl *e = static_cast<HTMLElementImpl *>(current);
115 if(e->id() == ID_IMG)
119 if(e->id() == ID_FORM)
123 if(e->id() == ID_TBODY)
125 else if(e->id() == ID_TABLE)
129 if(e->id() == ID_TD || e->id() == ID_TH)
131 else if(e->id() == ID_TABLE)
138 else if(e->id() == ID_TABLE)
142 if(e->id() == ID_OPTION)
146 if(e->id() == ID_AREA)
149 case DOC_APPLETS: // all OBJECT and APPLET elements
150 if(e->id() == ID_OBJECT || e->id() == ID_APPLET)
153 case DOC_EMBEDS: // all EMBED elements
154 if(e->id() == ID_EMBED)
157 case DOC_LINKS: // all A _and_ AREA elements with a value for href
158 if(e->id() == ID_A || e->id() == ID_AREA)
159 if(!e->getAttribute(ATTR_HREF).isNull())
162 case DOC_ANCHORS: // all A elements with a value for name or an id attribute
164 if(!e->getAttribute(ATTR_NAME).isNull())
175 kdDebug( 6030 ) << "Error in HTMLCollection, wrong tagId!" << endl;
181 current = current->traverseNextNode(base);
185 current = current->traverseNextSibling(base);
191 unsigned long HTMLCollectionImpl::calcLength() const
193 unsigned long len = 0;
195 for (NodeImpl *current = traverseNextItem(base); current; current = traverseNextItem(current)) {
202 // since the collections are to be "live", we have to do the
203 // calculation every time if anything has changed
204 unsigned long HTMLCollectionImpl::length() const
206 resetCollectionInfo();
207 if (!info->haslength) {
208 info->length = calcLength();
209 info->haslength = true;
214 NodeImpl *HTMLCollectionImpl::item( unsigned long index ) const
216 resetCollectionInfo();
217 if (info->current && info->position == index) {
218 return info->current;
220 if (info->haslength && info->length <= index) {
223 if (!info->current || info->position > index) {
224 info->current = traverseNextItem(base);
229 NodeImpl *node = info->current;
230 for (unsigned pos = info->position; pos < index; pos++) {
231 node = traverseNextItem(node);
233 info->current = node;
234 info->position = index;
235 return info->current;
238 NodeImpl *HTMLCollectionImpl::firstItem() const
243 NodeImpl *HTMLCollectionImpl::nextItem() const
245 resetCollectionInfo();
247 // Look for the 'second' item. The first one is currentItem, already given back.
248 NodeImpl *retval = traverseNextItem(info->current);
249 info->current = retval;
254 bool HTMLCollectionImpl::checkForNameMatch(NodeImpl *node, bool checkName, const DOMString &name, bool caseSensitive) const
256 ElementImpl *e = static_cast<ElementImpl *>(node);
259 // document.all returns only images, forms, applets, objects and embeds
260 // by name (though everything by id)
261 if (type == DOC_ALL &&
262 !(e->id() == ID_IMG || e->id() == ID_FORM ||
263 e->id() == ID_APPLET || e->id() == ID_OBJECT ||
264 e->id() == ID_EMBED))
267 return e->getAttribute(ATTR_NAME) == name && e->getAttribute(ATTR_ID) != name;
269 return e->getAttribute(ATTR_ID) == name;
273 // document.all returns only images, forms, applets, objects and embeds
274 // by name (though everything by id)
275 if (type == DOC_ALL &&
276 !(e->id() == ID_IMG || e->id() == ID_FORM ||
277 e->id() == ID_APPLET || e->id() == ID_OBJECT ||
278 e->id() == ID_EMBED))
281 return e->getAttribute(ATTR_NAME).domString().lower() == name.lower() &&
282 e->getAttribute(ATTR_ID).domString().lower() != name.lower();
284 return e->getAttribute(ATTR_ID).domString().lower() == name.lower();
290 NodeImpl *HTMLCollectionImpl::namedItem( const DOMString &name, bool caseSensitive ) const
292 // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/nameditem.asp
293 // This method first searches for an object with a matching id
294 // attribute. If a match is not found, the method then searches for an
295 // object with a matching name attribute, but only on those elements
296 // that are allowed a name attribute.
297 resetCollectionInfo();
301 for (n = traverseNextItem(base); n; n = traverseNextItem(n)) {
302 if (checkForNameMatch(n, idsDone, name, caseSensitive)) {
309 return info->current;
312 for (n = traverseNextItem(base); n; n = traverseNextItem(n)) {
313 if (checkForNameMatch(n, idsDone, name, caseSensitive)) {
319 return info->current;
322 template<class T> static void appendToVector(QPtrVector<T> *vec, T *item)
324 unsigned size = vec->size();
325 unsigned count = vec->count();
327 vec->resize(size == 0 ? 8 : (int)(size * 1.5));
328 vec->insert(count, item);
331 void HTMLCollectionImpl::updateNameCache() const
333 if (info->hasNameCache)
336 for (NodeImpl *n = traverseNextItem(base); n; n = traverseNextItem(n)) {
337 ElementImpl *e = static_cast<ElementImpl *>(n);
338 QString idAttr = e->getAttribute(ATTR_ID).string();
339 QString nameAttr = e->getAttribute(ATTR_NAME).string();
340 if (!idAttr.isEmpty()) {
342 QPtrVector<NodeImpl> *idVector = info->idCache.find(idAttr);
344 idVector = new QPtrVector<NodeImpl>;
345 info->idCache.insert(idAttr, idVector);
347 appendToVector(idVector, n);
349 if (!nameAttr.isEmpty() && idAttr != nameAttr
350 && (type != DOC_ALL ||
351 (e->id() == ID_IMG || e->id() == ID_FORM ||
352 e->id() == ID_APPLET || e->id() == ID_OBJECT ||
353 e->id() == ID_EMBED))) {
355 QPtrVector<NodeImpl> *nameVector = info->idCache.find(nameAttr);
357 nameVector = new QPtrVector<NodeImpl>;
358 info->nameCache.insert(nameAttr, nameVector);
360 appendToVector(nameVector, n);
364 info->hasNameCache = true;
367 QValueList<Node> HTMLCollectionImpl::namedItems(const DOMString &name) const
369 QValueList<Node> result;
374 resetCollectionInfo();
377 QPtrVector<NodeImpl> *idResults = info->idCache.find(name.string());
378 QPtrVector<NodeImpl> *nameResults = info->nameCache.find(name.string());
380 for (unsigned i = 0; idResults && i < idResults->count(); ++i) {
381 result.append(idResults->at(i));
384 for (unsigned i = 0; nameResults && i < nameResults->count(); ++i) {
385 result.append(nameResults->at(i));
392 NodeImpl *HTMLCollectionImpl::nextNamedItem( const DOMString &name ) const
394 resetCollectionInfo();
396 for (NodeImpl *n = traverseNextItem(info->current ? info->current : base); n; n = traverseNextItem(n)) {
397 if (checkForNameMatch(n, idsDone, name, true)) {
409 for (NodeImpl *n = traverseNextItem(info->current ? info->current : base); n; n = traverseNextItem(n)) {
410 if (checkForNameMatch(n, idsDone, name, true)) {
419 // -----------------------------------------------------------------------------
421 HTMLFormCollectionImpl::HTMLFormCollectionImpl(NodeImpl* _base)
422 : HTMLCollectionImpl(_base, 0)
424 HTMLFormElementImpl *formBase = static_cast<HTMLFormElementImpl*>(base);
425 if (!formBase->collectionInfo) {
426 formBase->collectionInfo = new CollectionInfo();
428 info = formBase->collectionInfo;
431 HTMLFormCollectionImpl::~HTMLFormCollectionImpl()
435 unsigned long HTMLFormCollectionImpl::calcLength() const
437 QPtrVector<HTMLGenericFormElementImpl> &l = static_cast<HTMLFormElementImpl*>( base )->formElements;
440 for ( unsigned i = 0; i < l.count(); i++ )
441 if ( l.at( i )->isEnumeratable() )
447 NodeImpl *HTMLFormCollectionImpl::item(unsigned long index) const
449 resetCollectionInfo();
451 if (info->current && info->position == index) {
452 return info->current;
454 if (info->haslength && info->length <= index) {
457 if (!info->current || info->position > index) {
460 info->elementsArrayPosition = 0;
463 QPtrVector<HTMLGenericFormElementImpl> &l = static_cast<HTMLFormElementImpl*>( base )->formElements;
464 unsigned currentIndex = info->position;
466 for (unsigned i = info->elementsArrayPosition; i < l.count(); i++) {
467 if (l[i]->isEnumeratable() ) {
468 if (index == currentIndex) {
469 info->position = index;
470 info->current = l[i];
471 info->elementsArrayPosition = i;
482 NodeImpl* HTMLFormCollectionImpl::getNamedItem(NodeImpl*, int attr_id, const DOMString& name, bool caseSensitive) const
485 return getNamedFormItem( attr_id, name, 0, caseSensitive );
488 NodeImpl* HTMLFormCollectionImpl::getNamedFormItem(int attr_id, const DOMString& name, int duplicateNumber, bool caseSensitive) const
490 if(base->nodeType() == Node::ELEMENT_NODE)
492 HTMLElementImpl* baseElement = static_cast<HTMLElementImpl*>(base);
493 bool foundInputElements = false;
494 if(baseElement->id() == ID_FORM)
496 HTMLFormElementImpl* f = static_cast<HTMLFormElementImpl*>(baseElement);
497 for (unsigned i = 0; i < f->formElements.count(); ++i) {
498 HTMLGenericFormElementImpl* e = f->formElements[i];
499 if (e->isEnumeratable()) {
502 found = e->getAttribute(attr_id) == name;
504 found = e->getAttribute(attr_id).domString().lower() == name.lower();
506 foundInputElements = true;
507 if (!duplicateNumber)
515 if ( !foundInputElements )
517 HTMLFormElementImpl* f = static_cast<HTMLFormElementImpl*>(baseElement);
519 for(unsigned i = 0; i < f->imgElements.count(); ++i)
521 HTMLImageElementImpl* e = f->imgElements[i];
524 found = e->getAttribute(attr_id) == name;
526 found = e->getAttribute(attr_id).domString().lower() == name.lower();
528 if (!duplicateNumber)
538 NodeImpl * HTMLFormCollectionImpl::firstItem() const
543 NodeImpl * HTMLFormCollectionImpl::nextItem() const
545 return item(info->position + 1);
548 NodeImpl * HTMLFormCollectionImpl::nextNamedItemInternal( const DOMString &name ) const
550 NodeImpl *retval = getNamedFormItem( idsDone ? ATTR_NAME : ATTR_ID, name, ++info->position, true );
553 if ( idsDone ) // we're done
555 // After doing all ATTR_ID, do ATTR_NAME
557 return getNamedItem(base->firstChild(), ATTR_NAME, name, true);
560 NodeImpl *HTMLFormCollectionImpl::namedItem( const DOMString &name, bool caseSensitive ) const
562 // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/nameditem.asp
563 // This method first searches for an object with a matching id
564 // attribute. If a match is not found, the method then searches for an
565 // object with a matching name attribute, but only on those elements
566 // that are allowed a name attribute.
567 resetCollectionInfo();
569 info->current = getNamedItem(base->firstChild(), ATTR_ID, name, true);
571 return info->current;
573 info->current = getNamedItem(base->firstChild(), ATTR_NAME, name, true);
574 return info->current;
578 NodeImpl *HTMLFormCollectionImpl::nextNamedItem( const DOMString &name ) const
580 // nextNamedItemInternal can return an item that has both id=<name> and name=<name>
581 // Here, we have to filter out such cases.
582 NodeImpl *impl = nextNamedItemInternal( name );
583 if (!idsDone) // looking for id=<name> -> no filtering
585 // looking for name=<name> -> filter out if id=<name>
589 if(impl->nodeType() == Node::ELEMENT_NODE)
591 HTMLElementImpl *e = static_cast<HTMLElementImpl *>(impl);
592 ok = (e->getAttribute(ATTR_ID) != name);
594 impl = nextNamedItemInternal( name );
595 } else // can't happen
601 void HTMLFormCollectionImpl::updateNameCache() const
603 if (info->hasNameCache)
606 QDict<char> foundInputElements;
608 if (base->nodeType() != Node::ELEMENT_NODE ||static_cast<HTMLElementImpl*>(base)->id() != ID_FORM) {
609 info->hasNameCache = true;
613 HTMLElementImpl* baseElement = static_cast<HTMLElementImpl*>(base);
615 HTMLFormElementImpl* f = static_cast<HTMLFormElementImpl*>(baseElement);
616 for (unsigned i = 0; i < f->formElements.count(); ++i) {
617 HTMLGenericFormElementImpl* e = f->formElements[i];
618 if (e->isEnumeratable()) {
619 QString idAttr = e->getAttribute(ATTR_ID).string();
620 QString nameAttr = e->getAttribute(ATTR_NAME).string();
621 if (!idAttr.isEmpty()) {
623 QPtrVector<NodeImpl> *idVector = info->idCache.find(idAttr);
625 idVector = new QPtrVector<NodeImpl>;
626 info->idCache.insert(idAttr, idVector);
628 appendToVector(idVector, static_cast<NodeImpl *>(e));
629 foundInputElements.insert(idAttr, (char *)true);
631 if (!nameAttr.isEmpty() && idAttr != nameAttr) {
633 QPtrVector<NodeImpl> *nameVector = info->nameCache.find(nameAttr);
635 nameVector = new QPtrVector<NodeImpl>;
636 info->nameCache.insert(nameAttr, nameVector);
638 appendToVector(nameVector, static_cast<NodeImpl *>(e));
639 foundInputElements.insert(nameAttr, (char *)true);
644 for (unsigned i = 0; i < f->imgElements.count(); ++i) {
645 HTMLImageElementImpl* e = f->imgElements[i];
646 QString idAttr = e->getAttribute(ATTR_ID).string();
647 QString nameAttr = e->getAttribute(ATTR_NAME).string();
648 if (!idAttr.isEmpty() && !foundInputElements.find(idAttr)) {
650 QPtrVector<NodeImpl> *idVector = info->idCache.find(idAttr);
652 idVector = new QPtrVector<NodeImpl>;
653 info->idCache.insert(idAttr, idVector);
655 appendToVector(idVector, static_cast<NodeImpl *>(e));
657 if (!nameAttr.isEmpty() && idAttr != nameAttr && !foundInputElements.find(nameAttr)) {
659 QPtrVector<NodeImpl> *nameVector = info->idCache.find(nameAttr);
661 nameVector = new QPtrVector<NodeImpl>;
662 info->nameCache.insert(nameAttr, nameVector);
664 appendToVector(nameVector, static_cast<NodeImpl *>(e));
668 info->hasNameCache = true;