Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "core/dom/shadow/AssignedElementTraversal.h" | |
| 6 | |
| 7 #include "core/dom/Element.h" | |
| 8 #include "core/dom/ElementTraversal.h" | |
| 9 #include "core/html/HTMLSlotElement.h" | |
| 10 | |
| 11 namespace blink { | |
| 12 | |
| 13 HTMLSlotElement* AssignedElementTraversal::slot(const Element& current) | |
| 14 { | |
| 15 if (Element* assignedAncestor = AssignedElementTraversal::assignedAncestor(c urrent)) | |
| 16 return assignedAncestor->assignedSlot(); | |
| 17 return nullptr; | |
| 18 } | |
| 19 | |
| 20 Element* AssignedElementTraversal::assignedAncestor(const Element& current) | |
| 21 { | |
| 22 // assignedAncestor returns an ancestor element of current which is directly assigned to a slot. | |
| 23 Element* element = const_cast<Element*>(¤t); | |
| 24 for (; element; element = element->parentElement()) { | |
| 25 if (element->assignedSlot()) | |
| 26 break; | |
| 27 } | |
| 28 return element; | |
| 29 } | |
| 30 | |
| 31 Element* AssignedElementTraversal::next(const Element& current) | |
| 32 { | |
| 33 // current.assignedSlot returns a slot only when current is assigned explici tly | |
| 34 // if current is directly assigned to a slot, return a descendant of current , which is indirectly assigned to the same slot as current. | |
| 35 HTMLSlotElement* slot = current.assignedSlot(); | |
| 36 if (slot) { | |
| 37 if (Element* next = ElementTraversal::next(current, ¤t)) | |
| 38 return next; | |
| 39 } else { | |
| 40 // if current is indirectly assigned to a slot, find a directly-assigned ancestor. | |
|
hayato
2016/03/03 05:42:21
A sentence in a comment should start with a capita
yuzuchan
2016/03/04 03:48:37
Done.
| |
| 41 Element* assignedAncestor = AssignedElementTraversal::assignedAncestor(c urrent); | |
|
hayato
2016/03/03 05:42:21
Could you assert ASSERT(assignedAncestor)?
yuzuchan
2016/03/04 03:48:37
Done.
| |
| 42 if (Element* next = ElementTraversal::next(current, assignedAncestor)) | |
| 43 return next; | |
| 44 slot = AssignedElementTraversal::slot(*assignedAncestor); | |
|
hayato
2016/03/03 05:42:21
"slot = assingedAncestor.assignedSlot()" might be
yuzuchan
2016/03/04 03:48:37
Done.
| |
| 45 } | |
| 46 WillBeHeapVector<RefPtrWillBeMember<Node>> assignedNodes = slot->getAssigned Nodes(); | |
| 47 size_t currentIndex = assignedNodes.find(current); | |
|
hayato
2016/03/03 05:42:21
Looks |assignedAncestor| should be used instead of
yuzuchan
2016/03/04 03:48:37
Done.
| |
| 48 if (currentIndex != kNotFound && currentIndex < assignedNodes.size() - 1) | |
|
hayato
2016/03/03 05:42:21
We can ASSERT(currentIndex != kNotFound). It looks
yuzuchan
2016/03/04 03:48:37
Done.
| |
| 49 return toElement(assignedNodes[currentIndex + 1]); | |
|
hayato
2016/03/03 05:42:21
assignedNodes can contain a non-Element Node, such
yuzuchan
2016/03/04 03:48:37
Done.
| |
| 50 return nullptr; | |
| 51 } | |
| 52 | |
| 53 Element* AssignedElementTraversal::previous(const Element& current) | |
| 54 { | |
| 55 Element* assignedAncestor = AssignedElementTraversal::assignedAncestor(curre nt); | |
|
hayato
2016/03/03 05:42:21
Can we assert ASSERT(assignedAncestor)?
yuzuchan
2016/03/04 03:48:37
Done.
| |
| 56 // NodeTraversal within assignedAncestor | |
| 57 if (Element* previous = ElementTraversal::previous(current, assignedAncestor )) | |
| 58 return previous; | |
| 59 // if null, jump to previous assigned node's descendant | |
| 60 const WillBeHeapVector<RefPtrWillBeMember<Node>> assignedNodes = AssignedEle mentTraversal::slot(*assignedAncestor)->getAssignedNodes(); | |
| 61 size_t currentIndex = assignedNodes.reverseFind(current); | |
|
hayato
2016/03/03 05:42:21
Looks we have to pass |assignedAncestor|, instead
yuzuchan
2016/03/04 03:48:37
Done.
| |
| 62 if (currentIndex != kNotFound && currentIndex > 0) { | |
|
hayato
2016/03/03 05:42:21
ASSERT(currentIndex != kNotFound)
yuzuchan
2016/03/04 03:48:37
Done.
| |
| 63 Node* assignedPrevious = assignedNodes[currentIndex - 1]; | |
| 64 if (Node* lastChild = assignedPrevious->lastChild()) | |
|
hayato
2016/03/03 05:42:22
Looks ElementTraversal::lastWithin(assignedPreviou
yuzuchan
2016/03/04 03:48:37
Done.
| |
| 65 return toElement(lastChild); | |
| 66 // if the previous assigned node has no children, return the assigned no de itself | |
|
hayato
2016/03/03 05:42:21
assignedPrevious can be non-element Node, such as
yuzuchan
2016/03/04 03:48:37
Done.
| |
| 67 return toElement(assignedPrevious); | |
| 68 } | |
| 69 return nullptr; | |
| 70 } | |
| 71 | |
| 72 bool AssignedElementTraversal::isInAssignedScope(const Element& current) | |
|
hayato
2016/03/03 05:42:21
Nit: How about renaming this to isInSubtreeAssigne
yuzuchan
2016/03/04 03:48:37
Renamed to isSlotScoped.
| |
| 73 { | |
| 74 return AssignedElementTraversal::assignedAncestor(current); | |
| 75 } | |
| 76 | |
| 77 } // namespace blink | |
| OLD | NEW |