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

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

Issue 229213002: Make HTMLCollection / NodeList backward traversal consistent with forward one (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Created 6 years, 8 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
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 258 matching lines...) Expand 10 before | Expand all | Expand 10 after
269 template <> inline bool isMatchingElement(const ClassCollection& collection, con st Element& element) 269 template <> inline bool isMatchingElement(const ClassCollection& collection, con st Element& element)
270 { 270 {
271 return collection.elementMatches(element); 271 return collection.elementMatches(element);
272 } 272 }
273 273
274 template <> inline bool isMatchingElement(const HTMLTagCollection& collection, c onst Element& element) 274 template <> inline bool isMatchingElement(const HTMLTagCollection& collection, c onst Element& element)
275 { 275 {
276 return collection.elementMatches(element); 276 return collection.elementMatches(element);
277 } 277 }
278 278
279 Element* HTMLCollection::itemBefore(const Element* previous) const
280 {
281 return LiveNodeListBase::itemBefore(*this, previous);
282 }
283
284 Element* HTMLCollection::virtualItemAfter(Element*) const 279 Element* HTMLCollection::virtualItemAfter(Element*) const
285 { 280 {
286 ASSERT_NOT_REACHED(); 281 ASSERT_NOT_REACHED();
287 return 0; 282 return 0;
288 } 283 }
289 284
290 static inline bool nameShouldBeVisibleInDocumentAll(const HTMLElement& element) 285 static inline bool nameShouldBeVisibleInDocumentAll(const HTMLElement& element)
291 { 286 {
292 // http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-in terfaces.html#dom-htmlallcollection-nameditem: 287 // http://www.whatwg.org/specs/web-apps/current-work/multipage/common-dom-in terfaces.html#dom-htmlallcollection-nameditem:
293 // The document.all collection returns only certain types of elements by nam e, 288 // The document.all collection returns only certain types of elements by nam e,
(...skipping 13 matching lines...) Expand all
307 } 302 }
308 303
309 inline Element* firstMatchingChildElement(const HTMLCollection& nodeList) 304 inline Element* firstMatchingChildElement(const HTMLCollection& nodeList)
310 { 305 {
311 Element* element = ElementTraversal::firstChild(nodeList.rootNode()); 306 Element* element = ElementTraversal::firstChild(nodeList.rootNode());
312 while (element && !isMatchingElement(nodeList, *element)) 307 while (element && !isMatchingElement(nodeList, *element))
313 element = ElementTraversal::nextSibling(*element); 308 element = ElementTraversal::nextSibling(*element);
314 return element; 309 return element;
315 } 310 }
316 311
312 inline Element* lastMatchingChildElement(const HTMLCollection& nodeList)
313 {
314 Element* element = ElementTraversal::lastChild(nodeList.rootNode());
315 while (element && !isMatchingElement(nodeList, *element))
316 element = ElementTraversal::previousSibling(*element);
317 return element;
318 }
319
317 inline Element* nextMatchingChildElement(const HTMLCollection& nodeList, Element & current) 320 inline Element* nextMatchingChildElement(const HTMLCollection& nodeList, Element & current)
318 { 321 {
319 Element* next = &current; 322 Element* next = &current;
320 do { 323 do {
321 next = ElementTraversal::nextSibling(*next); 324 next = ElementTraversal::nextSibling(*next);
322 } while (next && !isMatchingElement(nodeList, *next)); 325 } while (next && !isMatchingElement(nodeList, *next));
323 return next; 326 return next;
324 } 327 }
325 328
329 inline Element* previousMatchingChildElement(const HTMLCollection& nodeList, Ele ment& current)
330 {
331 Element* previous = &current;
332 do {
333 previous = ElementTraversal::previousSibling(*previous);
334 } while (previous && !isMatchingElement(nodeList, *previous));
335 return previous;
336 }
337
326 Element* HTMLCollection::traverseToFirstElement() const 338 Element* HTMLCollection::traverseToFirstElement() const
327 { 339 {
328 switch (type()) { 340 switch (type()) {
329 case HTMLTagCollectionType: 341 case HTMLTagCollectionType:
330 return firstMatchingElement(static_cast<const HTMLTagCollection&>(*this) ); 342 return firstMatchingElement(static_cast<const HTMLTagCollection&>(*this) );
331 case ClassCollectionType: 343 case ClassCollectionType:
332 return firstMatchingElement(static_cast<const ClassCollection&>(*this)); 344 return firstMatchingElement(static_cast<const ClassCollection&>(*this));
333 default: 345 default:
334 if (overridesItemAfter()) 346 if (overridesItemAfter())
335 return virtualItemAfter(0); 347 return virtualItemAfter(0);
336 if (shouldOnlyIncludeDirectChildren()) 348 if (shouldOnlyIncludeDirectChildren())
337 return firstMatchingChildElement(*this); 349 return firstMatchingChildElement(*this);
338 return firstMatchingElement(*this); 350 return firstMatchingElement(*this);
339 } 351 }
340 } 352 }
341 353
354 Element* HTMLCollection::traverseToLastElement() const
355 {
356 ASSERT(canTraverseBackward());
357 if (shouldOnlyIncludeDirectChildren())
358 return lastMatchingChildElement(*this);
359 return lastMatchingElement(*this);
360 }
361
342 inline Element* HTMLCollection::traverseNextElement(Element& previous) const 362 inline Element* HTMLCollection::traverseNextElement(Element& previous) const
343 { 363 {
344 if (overridesItemAfter()) 364 if (overridesItemAfter())
345 return virtualItemAfter(&previous); 365 return virtualItemAfter(&previous);
346 if (shouldOnlyIncludeDirectChildren()) 366 if (shouldOnlyIncludeDirectChildren())
347 return nextMatchingChildElement(*this, previous); 367 return nextMatchingChildElement(*this, previous);
348 return nextMatchingElement(*this, previous); 368 return nextMatchingElement(*this, previous);
349 } 369 }
350 370
351 Element* HTMLCollection::traverseForwardToOffset(unsigned offset, Element& curre ntElement, unsigned& currentOffset) const 371 Element* HTMLCollection::traverseForwardToOffset(unsigned offset, Element& curre ntElement, unsigned& currentOffset) const
(...skipping 18 matching lines...) Expand all
370 while ((next = nextMatchingChildElement(*this, *next))) { 390 while ((next = nextMatchingChildElement(*this, *next))) {
371 if (++currentOffset == offset) 391 if (++currentOffset == offset)
372 return next; 392 return next;
373 } 393 }
374 return 0; 394 return 0;
375 } 395 }
376 return traverseMatchingElementsForwardToOffset(*this, offset, currentEle ment, currentOffset); 396 return traverseMatchingElementsForwardToOffset(*this, offset, currentEle ment, currentOffset);
377 } 397 }
378 } 398 }
379 399
400 Element* HTMLCollection::traverseBackwardToOffset(unsigned offset, Element& curr entElement, unsigned& currentOffset) const
401 {
402 ASSERT(currentOffset > offset);
403 ASSERT(canTraverseBackward());
404 if (shouldOnlyIncludeDirectChildren()) {
Inactive 2014/04/09 23:08:58 Now we do this check once and then traverse to the
405 Element* previous = &currentElement;
406 while ((previous = previousMatchingChildElement(*this, *previous))) {
407 if (--currentOffset == offset)
408 return previous;
409 }
410 return 0;
411 }
412 return traverseMatchingElementsBackwardToOffset(*this, offset, currentElemen t, currentOffset);
413 }
414
380 Element* HTMLCollection::namedItem(const AtomicString& name) const 415 Element* HTMLCollection::namedItem(const AtomicString& name) const
381 { 416 {
382 // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/namedit em.asp 417 // http://msdn.microsoft.com/workshop/author/dhtml/reference/methods/namedit em.asp
383 // This method first searches for an object with a matching id 418 // This method first searches for an object with a matching id
384 // attribute. If a match is not found, the method then searches for an 419 // attribute. If a match is not found, the method then searches for an
385 // object with a matching name attribute, but only on those elements 420 // object with a matching name attribute, but only on those elements
386 // that are allowed a name attribute. 421 // that are allowed a name attribute.
387 updateIdNameCache(); 422 updateIdNameCache();
388 423
389 const NamedItemCache& cache = namedItemCache(); 424 const NamedItemCache& cache = namedItemCache();
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after
474 509
475 for (unsigned i = 0; nameResults && i < nameResults->size(); ++i) 510 for (unsigned i = 0; nameResults && i < nameResults->size(); ++i)
476 result.append(nameResults->at(i)); 511 result.append(nameResults->at(i));
477 } 512 }
478 513
479 HTMLCollection::NamedItemCache::NamedItemCache() 514 HTMLCollection::NamedItemCache::NamedItemCache()
480 { 515 {
481 } 516 }
482 517
483 } // namespace WebCore 518 } // namespace WebCore
OLDNEW
« Source/core/html/CollectionIndexCache.h ('K') | « Source/core/html/HTMLCollection.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698