Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(13)

Side by Side Diff: Source/core/html/HTMLCollection.cpp

Issue 215073003: Move HTMLCollection's id / name cache to a new NamedItemCache class (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « Source/core/html/HTMLCollection.h ('k') | Source/core/html/HTMLFormControlsCollection.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All r ights reserved. 4 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2011, 2012 Apple Inc. All r ights reserved.
5 * Copyright (C) 2014 Samsung Electronics. All rights reserved. 5 * Copyright (C) 2014 Samsung Electronics. All rights reserved.
6 * 6 *
7 * This library is free software; you can redistribute it and/or 7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Library General Public 8 * modify it under the terms of the GNU Library General Public
9 * License as published by the Free Software Foundation; either 9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version. 10 * version 2 of the License, or (at your option) any later version.
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after
155 break; 155 break;
156 } 156 }
157 ASSERT_NOT_REACHED(); 157 ASSERT_NOT_REACHED();
158 return DoNotInvalidateOnAttributeChanges; 158 return DoNotInvalidateOnAttributeChanges;
159 } 159 }
160 160
161 HTMLCollection::HTMLCollection(ContainerNode& ownerNode, CollectionType type, It emAfterOverrideType itemAfterOverrideType) 161 HTMLCollection::HTMLCollection(ContainerNode& ownerNode, CollectionType type, It emAfterOverrideType itemAfterOverrideType)
162 : LiveNodeListBase(ownerNode, rootTypeFromCollectionType(type), invalidation TypeExcludingIdAndNameAttributes(type), type) 162 : LiveNodeListBase(ownerNode, rootTypeFromCollectionType(type), invalidation TypeExcludingIdAndNameAttributes(type), type)
163 , m_overridesItemAfter(itemAfterOverrideType == OverridesItemAfter) 163 , m_overridesItemAfter(itemAfterOverrideType == OverridesItemAfter)
164 , m_shouldOnlyIncludeDirectChildren(shouldTypeOnlyIncludeDirectChildren(type )) 164 , m_shouldOnlyIncludeDirectChildren(shouldTypeOnlyIncludeDirectChildren(type ))
165 , m_hasValidIdNameCache(false)
166 { 165 {
167 ScriptWrappable::init(this); 166 ScriptWrappable::init(this);
168 } 167 }
169 168
170 PassRefPtr<HTMLCollection> HTMLCollection::create(ContainerNode& base, Collectio nType type) 169 PassRefPtr<HTMLCollection> HTMLCollection::create(ContainerNode& base, Collectio nType type)
171 { 170 {
172 return adoptRef(new HTMLCollection(base, type, DoesNotOverrideItemAfter)); 171 return adoptRef(new HTMLCollection(base, type, DoesNotOverrideItemAfter));
173 } 172 }
174 173
175 HTMLCollection::~HTMLCollection() 174 HTMLCollection::~HTMLCollection()
(...skipping 204 matching lines...) Expand 10 before | Expand all | Expand 10 after
380 379
381 Element* HTMLCollection::namedItem(const AtomicString& name) const 380 Element* HTMLCollection::namedItem(const AtomicString& name) const
382 { 381 {
383 // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/namedit em.asp 382 // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/namedit em.asp
384 // This method first searches for an object with a matching id 383 // This method first searches for an object with a matching id
385 // attribute. If a match is not found, the method then searches for an 384 // attribute. If a match is not found, the method then searches for an
386 // object with a matching name attribute, but only on those elements 385 // object with a matching name attribute, but only on those elements
387 // that are allowed a name attribute. 386 // that are allowed a name attribute.
388 updateIdNameCache(); 387 updateIdNameCache();
389 388
390 Vector<Element*>* idResults = idCache(name); 389 const NamedItemCache& cache = namedItemCache();
390 Vector<Element*>* idResults = cache.getElementsById(name);
391 if (idResults && !idResults->isEmpty()) 391 if (idResults && !idResults->isEmpty())
392 return idResults->first(); 392 return idResults->first();
393 393
394 Vector<Element*>* nameResults = nameCache(name); 394 Vector<Element*>* nameResults = cache.getElementsByName(name);
395 if (nameResults && !nameResults->isEmpty()) 395 if (nameResults && !nameResults->isEmpty())
396 return nameResults->first(); 396 return nameResults->first();
397 397
398 return 0; 398 return 0;
399 } 399 }
400 400
401 bool HTMLCollection::namedPropertyQuery(const AtomicString& name, ExceptionState &) 401 bool HTMLCollection::namedPropertyQuery(const AtomicString& name, ExceptionState &)
402 { 402 {
403 return namedItem(name); 403 return namedItem(name);
404 } 404 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
436 void HTMLCollection::namedPropertyEnumerator(Vector<String>& names, ExceptionSta te&) 436 void HTMLCollection::namedPropertyEnumerator(Vector<String>& names, ExceptionSta te&)
437 { 437 {
438 supportedPropertyNames(names); 438 supportedPropertyNames(names);
439 } 439 }
440 440
441 void HTMLCollection::updateIdNameCache() const 441 void HTMLCollection::updateIdNameCache() const
442 { 442 {
443 if (hasValidIdNameCache()) 443 if (hasValidIdNameCache())
444 return; 444 return;
445 445
446 NamedItemCache& cache = createNamedItemCache();
446 ContainerNode& root = rootNode(); 447 ContainerNode& root = rootNode();
447 for (Element* element = traverseToFirstElement(root); element; element = tra verseNextElement(*element, root)) { 448 for (Element* element = traverseToFirstElement(root); element; element = tra verseNextElement(*element, root)) {
448 const AtomicString& idAttrVal = element->getIdAttribute(); 449 const AtomicString& idAttrVal = element->getIdAttribute();
449 if (!idAttrVal.isEmpty()) 450 if (!idAttrVal.isEmpty())
450 appendIdCache(idAttrVal, element); 451 cache.addElementWithId(idAttrVal, element);
451 if (!element->isHTMLElement()) 452 if (!element->isHTMLElement())
452 continue; 453 continue;
453 const AtomicString& nameAttrVal = element->getNameAttribute(); 454 const AtomicString& nameAttrVal = element->getNameAttribute();
454 if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && (type() != Doc All || nameShouldBeVisibleInDocumentAll(toHTMLElement(*element)))) 455 if (!nameAttrVal.isEmpty() && idAttrVal != nameAttrVal && (type() != Doc All || nameShouldBeVisibleInDocumentAll(toHTMLElement(*element))))
455 appendNameCache(nameAttrVal, element); 456 cache.addElementWithName(nameAttrVal, element);
456 } 457 }
457
458 setHasValidIdNameCache();
459 } 458 }
460 459
461 void HTMLCollection::namedItems(const AtomicString& name, Vector<RefPtr<Element> >& result) const 460 void HTMLCollection::namedItems(const AtomicString& name, Vector<RefPtr<Element> >& result) const
462 { 461 {
463 ASSERT(result.isEmpty()); 462 ASSERT(result.isEmpty());
464 if (name.isEmpty()) 463 if (name.isEmpty())
465 return; 464 return;
466 465
467 updateIdNameCache(); 466 updateIdNameCache();
468 467
469 Vector<Element*>* idResults = idCache(name); 468 const NamedItemCache& cache = namedItemCache();
470 Vector<Element*>* nameResults = nameCache(name); 469 Vector<Element*>* idResults = cache.getElementsById(name);
470 Vector<Element*>* nameResults = cache.getElementsByName(name);
471 471
472 for (unsigned i = 0; idResults && i < idResults->size(); ++i) 472 for (unsigned i = 0; idResults && i < idResults->size(); ++i)
473 result.append(idResults->at(i)); 473 result.append(idResults->at(i));
474 474
475 for (unsigned i = 0; nameResults && i < nameResults->size(); ++i) 475 for (unsigned i = 0; nameResults && i < nameResults->size(); ++i)
476 result.append(nameResults->at(i)); 476 result.append(nameResults->at(i));
477 } 477 }
478 478
479 void HTMLCollection::append(NodeCacheMap& map, const AtomicString& key, Element* element)
480 {
481 OwnPtr<Vector<Element*> >& vector = map.add(key.impl(), nullptr).storedValue ->value;
482 if (!vector)
483 vector = adoptPtr(new Vector<Element*>);
484 vector->append(element);
485 }
486
487 } // namespace WebCore 479 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/html/HTMLCollection.h ('k') | Source/core/html/HTMLFormControlsCollection.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698