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 } | |
|
kochi
2016/03/02 13:26:56
nit: you can remove {}.
yuzuchan
2016/03/03 04:12:42
Done.
| |
| 18 return nullptr; | |
| 19 } | |
| 20 | |
| 21 Element* AssignedElementTraversal::assignedAncestor(const Element& current) | |
| 22 { | |
| 23 // assignedAncestor returns an ancestor element of current which is directly assigned to a slot. | |
| 24 Element* element = const_cast<Element*>(¤t); | |
| 25 for (; element; element = element->parentElement()) { | |
| 26 if (element->assignedSlot()) | |
| 27 break; | |
| 28 } | |
| 29 return element; | |
| 30 } | |
| 31 | |
| 32 Element* AssignedElementTraversal::next(const Element& current) | |
| 33 { | |
| 34 // current.assignedSlot returns a slot only when current is assigned explici tly | |
| 35 // if current is directly assigned to a slot, return a descendant of current , which is indirectly assigned to the same slot as current. | |
| 36 HTMLSlotElement* slot = current.assignedSlot(); | |
| 37 WillBeHeapVector<RefPtrWillBeMember<Node>> assignedNodes; | |
| 38 if (slot) { | |
| 39 if (Element* next = ElementTraversal::next(current, ¤t)) | |
| 40 return next; | |
| 41 // if not, jump to another tree of assigned elements. | |
| 42 assignedNodes = slot->getAssignedNodes(); | |
| 43 } else { | |
| 44 // if current is indirectly assigned to a slot, find a directly-assigned ancestor. | |
| 45 Element* assignedAncestor = AssignedElementTraversal::assignedAncestor(c urrent); | |
| 46 if (Element* next = ElementTraversal::next(current, assignedAncestor)) | |
| 47 return next; | |
| 48 assignedNodes = AssignedElementTraversal::slot(*assignedAncestor)->getAs signedNodes(); | |
|
kochi
2016/03/02 13:26:56
nit: I feel like on this line it should be
slo
yuzuchan
2016/03/03 04:12:42
Done.
| |
| 49 } | |
| 50 size_t currentIndex = assignedNodes.find(current); | |
| 51 if (currentIndex != kNotFound && currentIndex < assignedNodes.size() - 1) | |
| 52 return toElement(assignedNodes[currentIndex + 1]); | |
| 53 return nullptr; | |
| 54 } | |
| 55 | |
| 56 Element* AssignedElementTraversal::previous(const Element& current) | |
| 57 { | |
| 58 Element* assignedAncestor = AssignedElementTraversal::assignedAncestor(curre nt); | |
| 59 // NodeTraversal within assignedAncestor | |
| 60 if (Element* previous = ElementTraversal::previous(current, assignedAncestor )) | |
| 61 return previous; | |
| 62 // if null, jump to previous assigned node's descendant | |
| 63 const WillBeHeapVector<RefPtrWillBeMember<Node>> assignedNodes = AssignedEle mentTraversal::slot(*assignedAncestor)->getAssignedNodes(); | |
| 64 size_t currentIndex = assignedNodes.reverseFind(current); | |
| 65 if (currentIndex != kNotFound && currentIndex > 0) { | |
| 66 Node* assignedPrevious = assignedNodes[currentIndex - 1]; | |
| 67 if (Node* lastChild = assignedPrevious->lastChild()) | |
| 68 return toElement(lastChild); | |
| 69 // if the previous assigned node has no children, return the assigned no de itself | |
| 70 return toElement(assignedPrevious); | |
| 71 } | |
| 72 return nullptr; | |
| 73 } | |
| 74 | |
| 75 bool AssignedElementTraversal::isInAssignedScope(const Element& current) | |
| 76 { | |
| 77 return AssignedElementTraversal::assignedAncestor(current); | |
|
kochi
2016/03/02 13:26:56
aha, you don't need !! in front of AssingedElement
yuzuchan
2016/03/03 04:12:42
Acknowledged.
| |
| 78 } | |
| 79 | |
| 80 } // namespace blink | |
| OLD | NEW |