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, 2008 Apple Inc. All rights reserved.
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.
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.
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.
24 #include "HTMLCollection.h"
26 #include "HTMLDocument.h"
27 #include "HTMLElement.h"
28 #include "HTMLNames.h"
29 #include "HTMLObjectElement.h"
30 #include "HTMLOptionElement.h"
37 using namespace HTMLNames;
39 HTMLCollection::HTMLCollection(PassRefPtr<Node> base, CollectionType type)
43 , m_info(m_base->isDocumentNode() ? static_cast<Document*>(m_base.get())->collectionInfo(type) : 0)
48 HTMLCollection::HTMLCollection(PassRefPtr<Node> base, CollectionType type, CollectionCache* info)
57 PassRefPtr<HTMLCollection> HTMLCollection::create(PassRefPtr<Node> base, CollectionType type)
59 return adoptRef(new HTMLCollection(base, type));
62 HTMLCollection::~HTMLCollection()
68 void HTMLCollection::resetCollectionInfo() const
70 uint64_t docversion = static_cast<HTMLDocument*>(m_base->document())->domTreeVersion();
73 m_info = new CollectionCache;
75 m_info->version = docversion;
79 if (m_info->version != docversion) {
81 m_info->version = docversion;
85 static Node* nextNodeOrSibling(Node* base, Node* node, bool includeChildren)
87 return includeChildren ? node->traverseNextNode(base) : node->traverseNextSibling(base);
90 Element* HTMLCollection::itemAfter(Element* previous) const
104 case DocumentNamedItems:
106 case OtherCollection:
108 case DataListOptions:
109 case WindowNamedItems:
121 current = m_base->firstChild();
123 current = nextNodeOrSibling(m_base.get(), previous, deep);
125 for (; current; current = nextNodeOrSibling(m_base.get(), current, deep)) {
126 if (!current->isElementNode())
128 Element* e = static_cast<Element*>(current);
131 if (e->hasLocalName(imgTag))
135 if (e->hasLocalName(scriptTag))
139 if (e->hasLocalName(formTag))
143 if (e->hasLocalName(tbodyTag))
147 if (e->hasLocalName(tdTag) || e->hasLocalName(thTag))
151 if (e->hasLocalName(trTag))
155 if (e->hasLocalName(optionTag))
158 case DataListOptions:
159 if (e->hasLocalName(optionTag)) {
160 HTMLOptionElement* option = static_cast<HTMLOptionElement*>(e);
161 if (!option->disabled() && !option->value().isEmpty())
166 if (e->hasLocalName(areaTag))
169 case DocApplets: // all <applet> elements and <object> elements that contain Java Applets
170 if (e->hasLocalName(appletTag))
172 if (e->hasLocalName(objectTag) && static_cast<HTMLObjectElement*>(e)->containsJavaApplet())
176 if (e->hasLocalName(embedTag))
180 if (e->hasLocalName(objectTag))
183 case DocLinks: // all <a> and <area> elements with a value for href
184 if ((e->hasLocalName(aTag) || e->hasLocalName(areaTag)) && e->fastHasAttribute(hrefAttr))
187 case DocAnchors: // all <a> elements with a value for name
188 if (e->hasLocalName(aTag) && e->fastHasAttribute(nameAttr))
194 case DocumentNamedItems:
195 case OtherCollection:
196 case WindowNamedItems:
197 ASSERT_NOT_REACHED();
205 unsigned HTMLCollection::calcLength() const
208 for (Element* current = itemAfter(0); current; current = itemAfter(current))
213 // since the collections are to be "live", we have to do the
214 // calculation every time if anything has changed
215 unsigned HTMLCollection::length() const
217 resetCollectionInfo();
218 if (!m_info->hasLength) {
219 m_info->length = calcLength();
220 m_info->hasLength = true;
222 return m_info->length;
225 Node* HTMLCollection::item(unsigned index) const
227 resetCollectionInfo();
228 if (m_info->current && m_info->position == index)
229 return m_info->current;
230 if (m_info->hasLength && m_info->length <= index)
232 if (!m_info->current || m_info->position > index) {
233 m_info->current = itemAfter(0);
234 m_info->position = 0;
235 if (!m_info->current)
238 Element* e = m_info->current;
239 for (unsigned pos = m_info->position; e && pos < index; pos++)
242 m_info->position = index;
243 return m_info->current;
246 Node* HTMLCollection::firstItem() const
251 Node* HTMLCollection::nextItem() const
253 resetCollectionInfo();
255 // Look for the 'second' item. The first one is currentItem, already given back.
256 Element* retval = itemAfter(m_info->current);
257 m_info->current = retval;
262 bool HTMLCollection::checkForNameMatch(Element* element, bool checkName, const AtomicString& name) const
264 if (!element->isHTMLElement())
267 HTMLElement* e = toHTMLElement(element);
269 return e->getIdAttribute() == name;
271 // document.all returns only images, forms, applets, objects and embeds
272 // by name (though everything by id)
273 if (m_type == DocAll &&
274 !(e->hasLocalName(imgTag) || e->hasLocalName(formTag) ||
275 e->hasLocalName(appletTag) || e->hasLocalName(objectTag) ||
276 e->hasLocalName(embedTag) || e->hasLocalName(inputTag) ||
277 e->hasLocalName(selectTag)))
280 return e->getAttribute(nameAttr) == name && e->getIdAttribute() != name;
283 Node* HTMLCollection::namedItem(const AtomicString& name) const
285 // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/nameditem.asp
286 // This method first searches for an object with a matching id
287 // attribute. If a match is not found, the method then searches for an
288 // object with a matching name attribute, but only on those elements
289 // that are allowed a name attribute.
290 resetCollectionInfo();
293 for (Element* e = itemAfter(0); e; e = itemAfter(e)) {
294 if (checkForNameMatch(e, m_idsDone, name)) {
302 for (Element* e = itemAfter(0); e; e = itemAfter(e)) {
303 if (checkForNameMatch(e, m_idsDone, name)) {
313 void HTMLCollection::updateNameCache() const
315 if (m_info->hasNameCache)
318 for (Element* element = itemAfter(0); element; element = itemAfter(element)) {
319 if (!element->isHTMLElement())
321 HTMLElement* e = toHTMLElement(element);
322 const AtomicString& idAttrVal = e->getIdAttribute();
323 const AtomicString& nameAttrVal = e->getAttribute(nameAttr);
324 if (!idAttrVal.isEmpty()) {
326 Vector<Element*>* idVector = m_info->idCache.get(idAttrVal.impl());
328 idVector = new Vector<Element*>;
329 m_info->idCache.add(idAttrVal.impl(), idVector);
333 if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal
334 && (m_type != DocAll ||
335 (e->hasLocalName(imgTag) || e->hasLocalName(formTag) ||
336 e->hasLocalName(appletTag) || e->hasLocalName(objectTag) ||
337 e->hasLocalName(embedTag) || e->hasLocalName(inputTag) ||
338 e->hasLocalName(selectTag)))) {
340 Vector<Element*>* nameVector = m_info->nameCache.get(nameAttrVal.impl());
342 nameVector = new Vector<Element*>;
343 m_info->nameCache.add(nameAttrVal.impl(), nameVector);
345 nameVector->append(e);
349 m_info->hasNameCache = true;
352 void HTMLCollection::namedItems(const AtomicString& name, Vector<RefPtr<Node> >& result) const
354 ASSERT(result.isEmpty());
359 resetCollectionInfo();
361 m_info->checkConsistency();
363 Vector<Element*>* idResults = m_info->idCache.get(name.impl());
364 Vector<Element*>* nameResults = m_info->nameCache.get(name.impl());
366 for (unsigned i = 0; idResults && i < idResults->size(); ++i)
367 result.append(idResults->at(i));
369 for (unsigned i = 0; nameResults && i < nameResults->size(); ++i)
370 result.append(nameResults->at(i));
374 Node* HTMLCollection::nextNamedItem(const AtomicString& name) const
376 resetCollectionInfo();
377 m_info->checkConsistency();
379 for (Element* e = itemAfter(m_info->current); e; e = itemAfter(e)) {
380 if (checkForNameMatch(e, m_idsDone, name)) {
392 for (Element* e = itemAfter(m_info->current); e; e = itemAfter(e)) {
393 if (checkForNameMatch(e, m_idsDone, name)) {
402 PassRefPtr<NodeList> HTMLCollection::tags(const String& name)
404 return m_base->getElementsByTagName(name);
407 } // namespace WebCore