blob: 7d06308fc9bfbf01c569cf0c6e000931e618bac4 [file] [log] [blame]
darin@apple.com6b166602007-12-03 04:57:59 +00001/*
darinb9481ed2006-03-20 02:57:59 +00002 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
kling@webkit.orgf7ac4f22012-01-01 04:34:01 +00004 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All rights reserved.
darinb9481ed2006-03-20 02:57:59 +00005 *
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
ddkilzerc8eccec2007-09-26 02:29:57 +000018 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
19 * Boston, MA 02110-1301, USA.
darinb9481ed2006-03-20 02:57:59 +000020 *
21 */
darin36d11362006-04-11 16:30:21 +000022
darinb9481ed2006-03-20 02:57:59 +000023#include "config.h"
24#include "HTMLCollection.h"
darin36d11362006-04-11 16:30:21 +000025
darinb9481ed2006-03-20 02:57:59 +000026#include "HTMLDocument.h"
27#include "HTMLElement.h"
darin98fa8b82006-03-20 08:03:57 +000028#include "HTMLNames.h"
beidson1564c5e2007-05-10 08:55:44 +000029#include "HTMLObjectElement.h"
eric@webkit.org40ddb7c2009-08-18 07:16:12 +000030#include "HTMLOptionElement.h"
weinig0d789942007-06-09 06:00:26 +000031#include "NodeList.h"
darinb9481ed2006-03-20 02:57:59 +000032
anderscae4be0952007-07-19 20:04:46 +000033#include <utility>
34
darinb9481ed2006-03-20 02:57:59 +000035namespace WebCore {
36
37using namespace HTMLNames;
38
kling@webkit.orga3a65e32012-01-01 21:05:42 +000039HTMLCollection::HTMLCollection(Node* base, CollectionType type)
40 : m_includeChildren(shouldIncludeChildren(type))
andreas.kling@nokia.comc84be3e2011-11-09 18:14:09 +000041 , m_type(type)
kling@webkit.orgf5704662011-12-30 20:15:16 +000042 , m_base(base)
darin@apple.com6b166602007-12-03 04:57:59 +000043{
kling@webkit.orgfe420042012-01-07 09:35:21 +000044 ASSERT(m_base);
kling@webkit.org97404102012-01-02 08:42:50 +000045 m_cache.clear();
kling@webkit.org3e527e92011-12-16 23:11:11 +000046}
47
kling@webkit.orgfccd2062011-12-18 03:41:45 +000048bool HTMLCollection::shouldIncludeChildren(CollectionType type)
49{
50 switch (type) {
51 case DocAll:
52 case DocAnchors:
53 case DocApplets:
54 case DocEmbeds:
55 case DocForms:
56 case DocImages:
57 case DocLinks:
58 case DocObjects:
59 case DocScripts:
60 case DocumentNamedItems:
61 case MapAreas:
62 case OtherCollection:
63 case SelectOptions:
64 case DataListOptions:
65 case WindowNamedItems:
66#if ENABLE(MICRODATA)
67 case ItemProperties:
68#endif
69 return true;
70 case NodeChildren:
71 case TRCells:
72 case TSectionRows:
73 case TableTBodies:
74 return false;
75 }
76 ASSERT_NOT_REACHED();
77 return false;
78}
79
kling@webkit.orgfe420042012-01-07 09:35:21 +000080PassOwnPtr<HTMLCollection> HTMLCollection::create(Node* base, CollectionType type)
kling@webkit.org3e527e92011-12-16 23:11:11 +000081{
kling@webkit.orgfe420042012-01-07 09:35:21 +000082 return adoptPtr(new HTMLCollection(base, type));
darin@apple.com642f5002008-06-07 22:51:37 +000083}
84
darinb9481ed2006-03-20 02:57:59 +000085HTMLCollection::~HTMLCollection()
86{
darinb9481ed2006-03-20 02:57:59 +000087}
88
kling@webkit.org048cb3d2012-01-02 00:37:59 +000089void HTMLCollection::invalidateCacheIfNeeded() const
darinb9481ed2006-03-20 02:57:59 +000090{
eae@chromium.orga01dd602011-03-11 01:46:48 +000091 uint64_t docversion = static_cast<HTMLDocument*>(m_base->document())->domTreeVersion();
darinb9481ed2006-03-20 02:57:59 +000092
kling@webkit.org048cb3d2012-01-02 00:37:59 +000093 if (m_cache.version == docversion)
darinb9481ed2006-03-20 02:57:59 +000094 return;
darinb9481ed2006-03-20 02:57:59 +000095
kling@webkit.org97404102012-01-02 08:42:50 +000096 m_cache.clear();
kling@webkit.org048cb3d2012-01-02 00:37:59 +000097 m_cache.version = docversion;
darinb9481ed2006-03-20 02:57:59 +000098}
99
kling@webkit.orgfccd2062011-12-18 03:41:45 +0000100inline bool HTMLCollection::isAcceptableElement(Element* element) const
101{
102 switch (m_type) {
103 case DocImages:
104 return element->hasLocalName(imgTag);
105 case DocScripts:
106 return element->hasLocalName(scriptTag);
107 case DocForms:
108 return element->hasLocalName(formTag);
109 case TableTBodies:
110 return element->hasLocalName(tbodyTag);
111 case TRCells:
112 return element->hasLocalName(tdTag) || element->hasLocalName(thTag);
113 case TSectionRows:
114 return element->hasLocalName(trTag);
115 case SelectOptions:
116 return element->hasLocalName(optionTag);
117 case DataListOptions:
118 if (element->hasLocalName(optionTag)) {
119 HTMLOptionElement* option = static_cast<HTMLOptionElement*>(element);
120 if (!option->disabled() && !option->value().isEmpty())
121 return true;
122 }
123 return false;
124 case MapAreas:
125 return element->hasLocalName(areaTag);
126 case DocApplets:
127 return element->hasLocalName(appletTag) || (element->hasLocalName(objectTag) && static_cast<HTMLObjectElement*>(element)->containsJavaApplet());
128 case DocEmbeds:
129 return element->hasLocalName(embedTag);
130 case DocObjects:
131 return element->hasLocalName(objectTag);
132 case DocLinks:
133 return (element->hasLocalName(aTag) || element->hasLocalName(areaTag)) && element->fastHasAttribute(hrefAttr);
134 case DocAnchors:
135 return element->hasLocalName(aTag) && element->fastHasAttribute(nameAttr);
136 case DocAll:
137 case NodeChildren:
138 return true;
139#if ENABLE(MICRODATA)
140 case ItemProperties:
141 return element->isHTMLElement() && element->fastHasAttribute(itempropAttr);
142#endif
143 case DocumentNamedItems:
144 case OtherCollection:
145 case WindowNamedItems:
146 ASSERT_NOT_REACHED();
147 }
148 return false;
149}
150
darin@apple.com6b166602007-12-03 04:57:59 +0000151static Node* nextNodeOrSibling(Node* base, Node* node, bool includeChildren)
152{
153 return includeChildren ? node->traverseNextNode(base) : node->traverseNextSibling(base);
154}
155
156Element* HTMLCollection::itemAfter(Element* previous) const
157{
darin@apple.com6b166602007-12-03 04:57:59 +0000158 Node* current;
159 if (!previous)
160 current = m_base->firstChild();
darinb9481ed2006-03-20 02:57:59 +0000161 else
kling@webkit.orgfccd2062011-12-18 03:41:45 +0000162 current = nextNodeOrSibling(m_base, previous, m_includeChildren);
darinb9481ed2006-03-20 02:57:59 +0000163
kling@webkit.orgfccd2062011-12-18 03:41:45 +0000164 for (; current; current = nextNodeOrSibling(m_base, current, m_includeChildren)) {
darin@apple.com6b166602007-12-03 04:57:59 +0000165 if (!current->isElementNode())
166 continue;
kling@webkit.orgfccd2062011-12-18 03:41:45 +0000167 Element* element = static_cast<Element*>(current);
168 if (isAcceptableElement(element))
169 return element;
darinb9481ed2006-03-20 02:57:59 +0000170 }
darin@apple.com6b166602007-12-03 04:57:59 +0000171
darinb9481ed2006-03-20 02:57:59 +0000172 return 0;
173}
174
darinb9481ed2006-03-20 02:57:59 +0000175unsigned HTMLCollection::calcLength() const
176{
177 unsigned len = 0;
darin@apple.com6b166602007-12-03 04:57:59 +0000178 for (Element* current = itemAfter(0); current; current = itemAfter(current))
179 ++len;
darinb9481ed2006-03-20 02:57:59 +0000180 return len;
181}
182
183// since the collections are to be "live", we have to do the
184// calculation every time if anything has changed
185unsigned HTMLCollection::length() const
186{
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000187 invalidateCacheIfNeeded();
188 if (!m_cache.hasLength) {
189 m_cache.length = calcLength();
190 m_cache.hasLength = true;
darinb9481ed2006-03-20 02:57:59 +0000191 }
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000192 return m_cache.length;
darinb9481ed2006-03-20 02:57:59 +0000193}
194
darin@apple.com6b166602007-12-03 04:57:59 +0000195Node* HTMLCollection::item(unsigned index) const
darinb9481ed2006-03-20 02:57:59 +0000196{
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000197 invalidateCacheIfNeeded();
198 if (m_cache.current && m_cache.position == index)
199 return m_cache.current;
200 if (m_cache.hasLength && m_cache.length <= index)
darinb9481ed2006-03-20 02:57:59 +0000201 return 0;
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000202 if (!m_cache.current || m_cache.position > index) {
203 m_cache.current = itemAfter(0);
204 m_cache.position = 0;
205 if (!m_cache.current)
darinb9481ed2006-03-20 02:57:59 +0000206 return 0;
207 }
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000208 Element* e = m_cache.current;
209 for (unsigned pos = m_cache.position; e && pos < index; pos++)
darin@apple.com6b166602007-12-03 04:57:59 +0000210 e = itemAfter(e);
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000211 m_cache.current = e;
212 m_cache.position = index;
213 return m_cache.current;
darinb9481ed2006-03-20 02:57:59 +0000214}
215
darin@apple.com6b166602007-12-03 04:57:59 +0000216Node* HTMLCollection::firstItem() const
darinb9481ed2006-03-20 02:57:59 +0000217{
218 return item(0);
219}
220
darin@apple.com6b166602007-12-03 04:57:59 +0000221Node* HTMLCollection::nextItem() const
darinb9481ed2006-03-20 02:57:59 +0000222{
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000223 invalidateCacheIfNeeded();
kling@webkit.orgf16278a2012-01-01 05:54:09 +0000224
darinb9481ed2006-03-20 02:57:59 +0000225 // Look for the 'second' item. The first one is currentItem, already given back.
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000226 Element* retval = itemAfter(m_cache.current);
227 m_cache.current = retval;
228 m_cache.position++;
darinb9481ed2006-03-20 02:57:59 +0000229 return retval;
230}
231
darin@apple.com2b0054e2011-12-06 17:30:00 +0000232static inline bool nameShouldBeVisibleInDocumentAll(HTMLElement* element)
233{
234 // The document.all collection returns only certain types of elements by name,
235 // although it returns any type of element by id.
236 return element->hasLocalName(appletTag)
237 || element->hasLocalName(embedTag)
238 || element->hasLocalName(formTag)
239 || element->hasLocalName(imgTag)
240 || element->hasLocalName(inputTag)
241 || element->hasLocalName(objectTag)
242 || element->hasLocalName(selectTag);
243}
244
darin@apple.com72b7bb52009-01-05 17:32:03 +0000245bool HTMLCollection::checkForNameMatch(Element* element, bool checkName, const AtomicString& name) const
darinb9481ed2006-03-20 02:57:59 +0000246{
darin@apple.com6b166602007-12-03 04:57:59 +0000247 if (!element->isHTMLElement())
darinb9481ed2006-03-20 02:57:59 +0000248 return false;
249
yael.aharon@nokia.com3be82c12011-02-09 23:13:27 +0000250 HTMLElement* e = toHTMLElement(element);
darin@apple.com72b7bb52009-01-05 17:32:03 +0000251 if (!checkName)
darin@apple.comf5247d12010-06-13 17:29:10 +0000252 return e->getIdAttribute() == name;
darinb9481ed2006-03-20 02:57:59 +0000253
darin@apple.com2b0054e2011-12-06 17:30:00 +0000254 if (m_type == DocAll && !nameShouldBeVisibleInDocumentAll(e))
darin@apple.com72b7bb52009-01-05 17:32:03 +0000255 return false;
darinb9481ed2006-03-20 02:57:59 +0000256
kling@webkit.org11744b12012-02-11 14:50:28 +0000257 return e->getNameAttribute() == name && e->getIdAttribute() != name;
darinb9481ed2006-03-20 02:57:59 +0000258}
259
darin@apple.com72b7bb52009-01-05 17:32:03 +0000260Node* HTMLCollection::namedItem(const AtomicString& name) const
darinb9481ed2006-03-20 02:57:59 +0000261{
262 // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/nameditem.asp
263 // This method first searches for an object with a matching id
264 // attribute. If a match is not found, the method then searches for an
265 // object with a matching name attribute, but only on those elements
266 // that are allowed a name attribute.
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000267 invalidateCacheIfNeeded();
darinb9481ed2006-03-20 02:57:59 +0000268
darin@apple.com6b166602007-12-03 04:57:59 +0000269 for (Element* e = itemAfter(0); e; e = itemAfter(e)) {
andreas.kling@nokia.comf1b44fe2011-11-13 14:53:48 +0000270 if (checkForNameMatch(e, /* checkName */ false, name)) {
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000271 m_cache.current = e;
darin@apple.com6b166602007-12-03 04:57:59 +0000272 return e;
273 }
darinb9481ed2006-03-20 02:57:59 +0000274 }
darinb9481ed2006-03-20 02:57:59 +0000275
darin@apple.com6b166602007-12-03 04:57:59 +0000276 for (Element* e = itemAfter(0); e; e = itemAfter(e)) {
andreas.kling@nokia.comf1b44fe2011-11-13 14:53:48 +0000277 if (checkForNameMatch(e, /* checkName */ true, name)) {
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000278 m_cache.current = e;
darin@apple.com6b166602007-12-03 04:57:59 +0000279 return e;
280 }
darinb9481ed2006-03-20 02:57:59 +0000281 }
282
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000283 m_cache.current = 0;
darin@apple.com6b166602007-12-03 04:57:59 +0000284 return 0;
darinb9481ed2006-03-20 02:57:59 +0000285}
286
287void HTMLCollection::updateNameCache() const
288{
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000289 if (m_cache.hasNameCache)
darinb9481ed2006-03-20 02:57:59 +0000290 return;
kling@webkit.orgf16278a2012-01-01 05:54:09 +0000291
darin@apple.com6b166602007-12-03 04:57:59 +0000292 for (Element* element = itemAfter(0); element; element = itemAfter(element)) {
293 if (!element->isHTMLElement())
darinb9481ed2006-03-20 02:57:59 +0000294 continue;
yael.aharon@nokia.com3be82c12011-02-09 23:13:27 +0000295 HTMLElement* e = toHTMLElement(element);
darin@apple.comf5247d12010-06-13 17:29:10 +0000296 const AtomicString& idAttrVal = e->getIdAttribute();
kling@webkit.org11744b12012-02-11 14:50:28 +0000297 const AtomicString& nameAttrVal = e->getNameAttribute();
darin@apple.com2b0054e2011-12-06 17:30:00 +0000298 if (!idAttrVal.isEmpty())
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000299 append(m_cache.idCache, idAttrVal, e);
darin@apple.com2b0054e2011-12-06 17:30:00 +0000300 if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && (m_type != DocAll || nameShouldBeVisibleInDocumentAll(e)))
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000301 append(m_cache.nameCache, nameAttrVal, e);
darinb9481ed2006-03-20 02:57:59 +0000302 }
303
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000304 m_cache.hasNameCache = true;
darinb9481ed2006-03-20 02:57:59 +0000305}
306
kling@webkit.org5c2d0a22011-12-18 22:21:05 +0000307bool HTMLCollection::hasNamedItem(const AtomicString& name) const
308{
309 if (name.isEmpty())
310 return false;
311
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000312 invalidateCacheIfNeeded();
kling@webkit.org5c2d0a22011-12-18 22:21:05 +0000313 updateNameCache();
kling@webkit.org5c2d0a22011-12-18 22:21:05 +0000314
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000315 if (Vector<Element*>* idCache = m_cache.idCache.get(name.impl())) {
kling@webkit.org5c2d0a22011-12-18 22:21:05 +0000316 if (!idCache->isEmpty())
317 return true;
318 }
319
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000320 if (Vector<Element*>* nameCache = m_cache.nameCache.get(name.impl())) {
kling@webkit.org5c2d0a22011-12-18 22:21:05 +0000321 if (!nameCache->isEmpty())
322 return true;
323 }
324
325 return false;
326}
327
darin@apple.com6b166602007-12-03 04:57:59 +0000328void HTMLCollection::namedItems(const AtomicString& name, Vector<RefPtr<Node> >& result) const
darinb9481ed2006-03-20 02:57:59 +0000329{
andersca02b2f192006-11-16 21:22:48 +0000330 ASSERT(result.isEmpty());
darinb9481ed2006-03-20 02:57:59 +0000331 if (name.isEmpty())
andersca02b2f192006-11-16 21:22:48 +0000332 return;
darinb9481ed2006-03-20 02:57:59 +0000333
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000334 invalidateCacheIfNeeded();
darinb9481ed2006-03-20 02:57:59 +0000335 updateNameCache();
ap@apple.com5b3b14b2010-01-27 23:41:50 +0000336
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000337 Vector<Element*>* idResults = m_cache.idCache.get(name.impl());
338 Vector<Element*>* nameResults = m_cache.nameCache.get(name.impl());
339
darinb9481ed2006-03-20 02:57:59 +0000340 for (unsigned i = 0; idResults && i < idResults->size(); ++i)
andersca02b2f192006-11-16 21:22:48 +0000341 result.append(idResults->at(i));
darinb9481ed2006-03-20 02:57:59 +0000342
343 for (unsigned i = 0; nameResults && i < nameResults->size(); ++i)
andersca02b2f192006-11-16 21:22:48 +0000344 result.append(nameResults->at(i));
darinb9481ed2006-03-20 02:57:59 +0000345}
346
weinig0d789942007-06-09 06:00:26 +0000347PassRefPtr<NodeList> HTMLCollection::tags(const String& name)
348{
darin@apple.com6b166602007-12-03 04:57:59 +0000349 return m_base->getElementsByTagName(name);
darinb9481ed2006-03-20 02:57:59 +0000350}
weinig0d789942007-06-09 06:00:26 +0000351
kling@webkit.org048cb3d2012-01-02 00:37:59 +0000352void HTMLCollection::append(NodeCacheMap& map, const AtomicString& key, Element* element)
353{
354 OwnPtr<Vector<Element*> >& vector = map.add(key.impl(), nullptr).first->second;
355 if (!vector)
356 vector = adoptPtr(new Vector<Element*>);
357 vector->append(element);
358}
359
weinig0d789942007-06-09 06:00:26 +0000360} // namespace WebCore