1 /* 2 * Copyright (C) 1999 Lars Knoll (knoll (at) kde.org) 3 * (C) 1999 Antti Koivisto (koivisto (at) kde.org) 4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. 5 * 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 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 19 * Boston, MA 02110-1301, USA. 20 * 21 */ 22 23 #include "config.h" 24 #include "HTMLCollection.h" 25 26 #include "HTMLDocument.h" 27 #include "HTMLElement.h" 28 #include "HTMLNames.h" 29 #include "HTMLObjectElement.h" 30 #include "HTMLOptionElement.h" 31 #include "NodeList.h" 32 33 #include <utility> 34 35 namespace WebCore { 36 37 using namespace HTMLNames; 38 39 HTMLCollection::HTMLCollection(PassRefPtr<Node> base, CollectionType type) 40 : m_idsDone(false) 41 , m_base(base) 42 , m_type(type) 43 , m_info(m_base->isDocumentNode() ? static_cast<Document*>(m_base.get())->collectionInfo(type) : 0) 44 , m_ownsInfo(false) 45 { 46 } 47 48 HTMLCollection::HTMLCollection(PassRefPtr<Node> base, CollectionType type, CollectionCache* info) 49 : m_idsDone(false) 50 , m_base(base) 51 , m_type(type) 52 , m_info(info) 53 , m_ownsInfo(false) 54 { 55 } 56 57 PassRefPtr<HTMLCollection> HTMLCollection::create(PassRefPtr<Node> base, CollectionType type) 58 { 59 return adoptRef(new HTMLCollection(base, type)); 60 } 61 62 HTMLCollection::~HTMLCollection() 63 { 64 if (m_ownsInfo) 65 delete m_info; 66 } 67 68 void HTMLCollection::resetCollectionInfo() const 69 { 70 unsigned docversion = static_cast<HTMLDocument*>(m_base->document())->domTreeVersion(); 71 72 if (!m_info) { 73 m_info = new CollectionCache; 74 m_ownsInfo = true; 75 m_info->version = docversion; 76 return; 77 } 78 79 if (m_info->version != docversion) { 80 m_info->reset(); 81 m_info->version = docversion; 82 } 83 } 84 85 static Node* nextNodeOrSibling(Node* base, Node* node, bool includeChildren) 86 { 87 return includeChildren ? node->traverseNextNode(base) : node->traverseNextSibling(base); 88 } 89 90 Element* HTMLCollection::itemAfter(Element* previous) const 91 { 92 bool deep = true; 93 94 switch (m_type) { 95 case DocAll: 96 case DocAnchors: 97 case DocApplets: 98 case DocEmbeds: 99 case DocForms: 100 case DocImages: 101 case DocLinks: 102 case DocObjects: 103 case DocScripts: 104 case DocumentNamedItems: 105 case MapAreas: 106 case OtherCollection: 107 case SelectOptions: 108 case DataListOptions: 109 case WindowNamedItems: 110 break; 111 case NodeChildren: 112 case TRCells: 113 case TSectionRows: 114 case TableTBodies: 115 deep = false; 116 break; 117 } 118 119 Node* current; 120 if (!previous) 121 current = m_base->firstChild(); 122 else 123 current = nextNodeOrSibling(m_base.get(), previous, deep); 124 125 for (; current; current = nextNodeOrSibling(m_base.get(), current, deep)) { 126 if (!current->isElementNode()) 127 continue; 128 Element* e = static_cast<Element*>(current); 129 switch (m_type) { 130 case DocImages: 131 if (e->hasLocalName(imgTag)) 132 return e; 133 break; 134 case DocScripts: 135 if (e->hasLocalName(scriptTag)) 136 return e; 137 break; 138 case DocForms: 139 if (e->hasLocalName(formTag)) 140 return e; 141 break; 142 case TableTBodies: 143 if (e->hasLocalName(tbodyTag)) 144 return e; 145 break; 146 case TRCells: 147 if (e->hasLocalName(tdTag) || e->hasLocalName(thTag)) 148 return e; 149 break; 150 case TSectionRows: 151 if (e->hasLocalName(trTag)) 152 return e; 153 break; 154 case SelectOptions: 155 if (e->hasLocalName(optionTag)) 156 return e; 157 break; 158 case DataListOptions: 159 if (e->hasLocalName(optionTag)) { 160 HTMLOptionElement* option = static_cast<HTMLOptionElement*>(e); 161 if (!option->disabled() && !option->value().isEmpty()) 162 return e; 163 } 164 break; 165 case MapAreas: 166 if (e->hasLocalName(areaTag)) 167 return e; 168 break; 169 case DocApplets: // all <applet> elements and <object> elements that contain Java Applets 170 if (e->hasLocalName(appletTag)) 171 return e; 172 if (e->hasLocalName(objectTag) && static_cast<HTMLObjectElement*>(e)->containsJavaApplet()) 173 return e; 174 break; 175 case DocEmbeds: 176 if (e->hasLocalName(embedTag)) 177 return e; 178 break; 179 case DocObjects: 180 if (e->hasLocalName(objectTag)) 181 return e; 182 break; 183 case DocLinks: // all <a> and <area> elements with a value for href 184 if ((e->hasLocalName(aTag) || e->hasLocalName(areaTag)) && (!e->getAttribute(hrefAttr).isNull())) 185 return e; 186 break; 187 case DocAnchors: // all <a> elements with a value for name 188 if (e->hasLocalName(aTag) && !e->getAttribute(nameAttr).isNull()) 189 return e; 190 break; 191 case DocAll: 192 case NodeChildren: 193 return e; 194 case DocumentNamedItems: 195 case OtherCollection: 196 case WindowNamedItems: 197 ASSERT_NOT_REACHED(); 198 break; 199 } 200 } 201 202 return 0; 203 } 204 205 unsigned HTMLCollection::calcLength() const 206 { 207 unsigned len = 0; 208 for (Element* current = itemAfter(0); current; current = itemAfter(current)) 209 ++len; 210 return len; 211 } 212 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 216 { 217 resetCollectionInfo(); 218 if (!m_info->hasLength) { 219 m_info->length = calcLength(); 220 m_info->hasLength = true; 221 } 222 return m_info->length; 223 } 224 225 Node* HTMLCollection::item(unsigned index) const 226 { 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) 231 return 0; 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) 236 return 0; 237 } 238 Element* e = m_info->current; 239 for (unsigned pos = m_info->position; e && pos < index; pos++) 240 e = itemAfter(e); 241 m_info->current = e; 242 m_info->position = index; 243 return m_info->current; 244 } 245 246 Node* HTMLCollection::firstItem() const 247 { 248 return item(0); 249 } 250 251 Node* HTMLCollection::nextItem() const 252 { 253 resetCollectionInfo(); 254 255 #ifdef ANDROID_FIX 256 // resetCollectionInfo() can set info->current to be 0. If this is the 257 // case, we need to go back to the firstItem. Otherwise traverseNextItem 258 // will crash. 259 if (!m_info->current) 260 return firstItem(); 261 #endif 262 263 // Look for the 'second' item. The first one is currentItem, already given back. 264 Element* retval = itemAfter(m_info->current); 265 m_info->current = retval; 266 m_info->position++; 267 return retval; 268 } 269 270 bool HTMLCollection::checkForNameMatch(Element* element, bool checkName, const AtomicString& name) const 271 { 272 if (!element->isHTMLElement()) 273 return false; 274 275 HTMLElement* e = static_cast<HTMLElement*>(element); 276 if (!checkName) 277 return e->getAttribute(e->idAttributeName()) == name; 278 279 // document.all returns only images, forms, applets, objects and embeds 280 // by name (though everything by id) 281 if (m_type == DocAll && 282 !(e->hasLocalName(imgTag) || e->hasLocalName(formTag) || 283 e->hasLocalName(appletTag) || e->hasLocalName(objectTag) || 284 e->hasLocalName(embedTag) || e->hasLocalName(inputTag) || 285 e->hasLocalName(selectTag))) 286 return false; 287 288 return e->getAttribute(nameAttr) == name && e->getAttribute(e->idAttributeName()) != name; 289 } 290 291 Node* HTMLCollection::namedItem(const AtomicString& name) const 292 { 293 // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/nameditem.asp 294 // This method first searches for an object with a matching id 295 // attribute. If a match is not found, the method then searches for an 296 // object with a matching name attribute, but only on those elements 297 // that are allowed a name attribute. 298 resetCollectionInfo(); 299 m_idsDone = false; 300 301 for (Element* e = itemAfter(0); e; e = itemAfter(e)) { 302 if (checkForNameMatch(e, m_idsDone, name)) { 303 m_info->current = e; 304 return e; 305 } 306 } 307 308 m_idsDone = true; 309 310 for (Element* e = itemAfter(0); e; e = itemAfter(e)) { 311 if (checkForNameMatch(e, m_idsDone, name)) { 312 m_info->current = e; 313 return e; 314 } 315 } 316 317 m_info->current = 0; 318 return 0; 319 } 320 321 void HTMLCollection::updateNameCache() const 322 { 323 if (m_info->hasNameCache) 324 return; 325 326 for (Element* element = itemAfter(0); element; element = itemAfter(element)) { 327 if (!element->isHTMLElement()) 328 continue; 329 HTMLElement* e = static_cast<HTMLElement*>(element); 330 const AtomicString& idAttrVal = e->getAttribute(e->idAttributeName()); 331 const AtomicString& nameAttrVal = e->getAttribute(nameAttr); 332 if (!idAttrVal.isEmpty()) { 333 // add to id cache 334 Vector<Element*>* idVector = m_info->idCache.get(idAttrVal.impl()); 335 if (!idVector) { 336 idVector = new Vector<Element*>; 337 m_info->idCache.add(idAttrVal.impl(), idVector); 338 } 339 idVector->append(e); 340 } 341 if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal 342 && (m_type != DocAll || 343 (e->hasLocalName(imgTag) || e->hasLocalName(formTag) || 344 e->hasLocalName(appletTag) || e->hasLocalName(objectTag) || 345 e->hasLocalName(embedTag) || e->hasLocalName(inputTag) || 346 e->hasLocalName(selectTag)))) { 347 // add to name cache 348 Vector<Element*>* nameVector = m_info->nameCache.get(nameAttrVal.impl()); 349 if (!nameVector) { 350 nameVector = new Vector<Element*>; 351 m_info->nameCache.add(nameAttrVal.impl(), nameVector); 352 } 353 nameVector->append(e); 354 } 355 } 356 357 m_info->hasNameCache = true; 358 } 359 360 void HTMLCollection::namedItems(const AtomicString& name, Vector<RefPtr<Node> >& result) const 361 { 362 ASSERT(result.isEmpty()); 363 364 if (name.isEmpty()) 365 return; 366 367 resetCollectionInfo(); 368 updateNameCache(); 369 m_info->checkConsistency(); 370 371 Vector<Element*>* idResults = m_info->idCache.get(name.impl()); 372 Vector<Element*>* nameResults = m_info->nameCache.get(name.impl()); 373 374 for (unsigned i = 0; idResults && i < idResults->size(); ++i) 375 result.append(idResults->at(i)); 376 377 for (unsigned i = 0; nameResults && i < nameResults->size(); ++i) 378 result.append(nameResults->at(i)); 379 } 380 381 382 Node* HTMLCollection::nextNamedItem(const AtomicString& name) const 383 { 384 resetCollectionInfo(); 385 m_info->checkConsistency(); 386 387 for (Element* e = itemAfter(m_info->current); e; e = itemAfter(e)) { 388 if (checkForNameMatch(e, m_idsDone, name)) { 389 m_info->current = e; 390 return e; 391 } 392 } 393 394 if (m_idsDone) { 395 m_info->current = 0; 396 return 0; 397 } 398 m_idsDone = true; 399 400 for (Element* e = itemAfter(m_info->current); e; e = itemAfter(e)) { 401 if (checkForNameMatch(e, m_idsDone, name)) { 402 m_info->current = e; 403 return e; 404 } 405 } 406 407 return 0; 408 } 409 410 PassRefPtr<NodeList> HTMLCollection::tags(const String& name) 411 { 412 return m_base->getElementsByTagName(name); 413 } 414 415 } // namespace WebCore 416