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

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

Issue 154183002: Update HTMLCollection.namedItem() to use the id/name cache (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 6 years, 10 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
« no previous file with comments | « Source/core/html/HTMLCollection.h ('k') | no next file » | 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 * 5 *
6 * This library is free software; you can redistribute it and/or 6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Library General Public 7 * modify it under the terms of the GNU Library General Public
8 * License as published by the Free Software Foundation; either 8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version. 9 * version 2 of the License, or (at your option) any later version.
10 * 10 *
(...skipping 396 matching lines...) Expand 10 before | Expand all | Expand 10 after
407 // although it returns any type of element by id. 407 // although it returns any type of element by id.
408 return element.hasLocalName(appletTag) 408 return element.hasLocalName(appletTag)
409 || element.hasLocalName(embedTag) 409 || element.hasLocalName(embedTag)
410 || element.hasLocalName(formTag) 410 || element.hasLocalName(formTag)
411 || element.hasLocalName(imgTag) 411 || element.hasLocalName(imgTag)
412 || element.hasLocalName(inputTag) 412 || element.hasLocalName(inputTag)
413 || element.hasLocalName(objectTag) 413 || element.hasLocalName(objectTag)
414 || element.hasLocalName(selectTag); 414 || element.hasLocalName(selectTag);
415 } 415 }
416 416
417 bool HTMLCollection::checkForNameMatch(const Element& element, bool checkName, c onst AtomicString& name) const
418 {
419 if (!element.isHTMLElement())
420 return false;
421
422 const HTMLElement& e = toHTMLElement(element);
423 if (!checkName)
424 return e.getIdAttribute() == name;
425
426 if (type() == DocAll && !nameShouldBeVisibleInDocumentAll(e))
427 return false;
428
429 return e.getNameAttribute() == name && e.getIdAttribute() != name;
430 }
431
432 inline Element* firstMatchingChildElement(const HTMLCollection& nodeList, const ContainerNode& root) 417 inline Element* firstMatchingChildElement(const HTMLCollection& nodeList, const ContainerNode& root)
433 { 418 {
434 Element* element = ElementTraversal::firstWithin(root); 419 Element* element = ElementTraversal::firstWithin(root);
435 while (element && !isMatchingElement(nodeList, *element)) 420 while (element && !isMatchingElement(nodeList, *element))
436 element = ElementTraversal::nextSkippingChildren(*element, &root); 421 element = ElementTraversal::nextSkippingChildren(*element, &root);
437 return element; 422 return element;
438 } 423 }
439 424
440 inline Element* nextMatchingChildElement(const HTMLCollection& nodeList, Element & current, const ContainerNode& root) 425 inline Element* nextMatchingChildElement(const HTMLCollection& nodeList, Element & current, const ContainerNode& root)
441 { 426 {
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
486 return traverseMatchingElementsForwardToOffset(*this, offset, currentElement , currentOffset, root); 471 return traverseMatchingElementsForwardToOffset(*this, offset, currentElement , currentOffset, root);
487 } 472 }
488 473
489 Element* HTMLCollection::namedItem(const AtomicString& name) const 474 Element* HTMLCollection::namedItem(const AtomicString& name) const
490 { 475 {
491 // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/namedit em.asp 476 // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/namedit em.asp
492 // This method first searches for an object with a matching id 477 // This method first searches for an object with a matching id
493 // attribute. If a match is not found, the method then searches for an 478 // attribute. If a match is not found, the method then searches for an
494 // object with a matching name attribute, but only on those elements 479 // object with a matching name attribute, but only on those elements
495 // that are allowed a name attribute. 480 // that are allowed a name attribute.
481 updateNameCache();
496 482
497 ContainerNode& root = rootNode(); 483 Vector<Element*>* idResults = idCache(name);
498 unsigned i = 0; 484 if (idResults && !idResults->isEmpty())
499 for (Element* element = traverseToFirstElement(root); element; element = tra verseNextElement(*element, root)) { 485 return idResults->first();
500 if (checkForNameMatch(*element, /* checkName */ false, name)) {
501 m_collectionIndexCache.setCachedNode(element, i);
502 return element;
503 }
504 i++;
505 }
506 486
507 i = 0; 487 Vector<Element*>* nameResults = nameCache(name);
508 for (Element* element = traverseToFirstElement(root); element; element = tra verseNextElement(*element, root)) { 488 if (nameResults && !nameResults->isEmpty())
509 if (checkForNameMatch(*element, /* checkName */ true, name)) { 489 return nameResults->first();
510 m_collectionIndexCache.setCachedNode(element, i);
511 return element;
512 }
513 i++;
514 }
515 490
516 return 0; 491 return 0;
517 } 492 }
518 493
519 void HTMLCollection::updateNameCache() const 494 void HTMLCollection::updateNameCache() const
520 { 495 {
521 if (hasNameCache()) 496 if (hasNameCache())
522 return; 497 return;
523 498
524 ContainerNode& root = rootNode(); 499 ContainerNode& root = rootNode();
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 531
557 void HTMLCollection::append(NodeCacheMap& map, const AtomicString& key, Element* element) 532 void HTMLCollection::append(NodeCacheMap& map, const AtomicString& key, Element* element)
558 { 533 {
559 OwnPtr<Vector<Element*> >& vector = map.add(key.impl(), nullptr).iterator->v alue; 534 OwnPtr<Vector<Element*> >& vector = map.add(key.impl(), nullptr).iterator->v alue;
560 if (!vector) 535 if (!vector)
561 vector = adoptPtr(new Vector<Element*>); 536 vector = adoptPtr(new Vector<Element*>);
562 vector->append(element); 537 vector->append(element);
563 } 538 }
564 539
565 } // namespace WebCore 540 } // namespace WebCore
OLDNEW
« no previous file with comments | « Source/core/html/HTMLCollection.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698