Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
|
kochi
2016/03/01 12:55:40
2016?
yuzuchan
2016/03/02 09:02:14
Done.
| |
| 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/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 } | |
| 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. | |
|
kochi
2016/03/01 12:55:40
Maybe isChildOfV1ShadowHost() is enough for this p
yuzuchan
2016/03/02 09:02:14
isChildOfV1ShadowHost() returns true even if the n
kochi
2016/03/02 13:26:56
Acknowledged.
| |
| 24 Element* element = const_cast<Element*>(¤t); | |
|
kochi
2016/03/01 12:55:40
Is const_cast<> really necessary?
yuzuchan
2016/03/02 09:02:14
Yes, because I update element in the for loop belo
kochi
2016/03/02 13:26:56
That is not the reason.
You can write
const Eleme
yuzuchan
2016/03/03 04:12:42
Acknowledged.
| |
| 25 while (!element->assignedSlot()) { | |
| 26 if (element->parentElement()) { | |
| 27 element = element->parentElement(); | |
| 28 } else { | |
| 29 return nullptr; | |
| 30 } | |
| 31 } | |
| 32 return element; | |
|
kochi
2016/03/01 12:55:40
This can be written as:
for (Element* element = c
yuzuchan
2016/03/02 09:02:14
I kept Element* element = const_cast<Element*>(&cu
kochi
2016/03/02 13:26:56
I see your point... but
Element* element = const_
yuzuchan
2016/03/03 04:12:42
Acknowledged.
| |
| 33 } | |
| 34 | |
| 35 Element* AssignedElementTraversal::next(const Element& current) | |
| 36 { | |
| 37 // current.assignedSlot returns a slot only when current is assigned explici tly | |
| 38 // if current is directly assigned to a slot, return a descendant of current , which is indirectly assigned to the same slot as current. | |
| 39 if (HTMLSlotElement* slot = current.assignedSlot()) { | |
| 40 if (Element* next = ElementTraversal::next(current, ¤t)) | |
| 41 return next; | |
| 42 // if not, jump to another tree of assigned elements. | |
| 43 const WillBeHeapVector<RefPtrWillBeMember<Node>> assignedNodes = slot->g etAssignedNodes(); | |
| 44 for (size_t i = 0; i < assignedNodes.size()-1; ++i) { | |
|
kochi
2016/03/01 12:55:40
Vector<> has find() method, though you should take
yuzuchan
2016/03/02 09:02:14
Done.
| |
| 45 if (current == toElement(assignedNodes[i].get())) | |
|
kochi
2016/03/01 12:55:40
nit: is toElement() necessary here?
(because this
yuzuchan
2016/03/02 09:02:14
Yes exactly.
| |
| 46 return toElement(assignedNodes[i+1].get()); | |
|
kochi
2016/03/01 12:55:40
nit: space around '+'.
yuzuchan
2016/03/02 09:02:14
Done.
| |
| 47 } | |
| 48 return nullptr; | |
| 49 } | |
| 50 // if current is indirectly assigned to a slot, find a directly-assigned anc estor. | |
| 51 Element* assignedAncestor = AssignedElementTraversal::assignedAncestor(curre nt); | |
| 52 if (Element* next = ElementTraversal::next(current, assignedAncestor)) | |
| 53 return next; | |
| 54 const WillBeHeapVector<RefPtrWillBeMember<Node>> assignedNodes = AssignedEle mentTraversal::slot(*assignedAncestor)->getAssignedNodes(); | |
| 55 for (size_t i = 0; i < assignedNodes.size()-1; ++i) { | |
|
kochi
2016/03/01 12:55:40
This is almost same as line 44-.
Probably you can
yuzuchan
2016/03/02 09:02:14
I refactored this part of code so that find method
| |
| 56 if (assignedAncestor == toElement(assignedNodes[i].get())) | |
| 57 return toElement(assignedNodes[i+1].get()); | |
| 58 } | |
| 59 return nullptr; | |
| 60 } | |
| 61 | |
| 62 Element* AssignedElementTraversal::previous(const Element& current) | |
| 63 { | |
| 64 Element* assignedAncestor = AssignedElementTraversal::assignedAncestor(curre nt); | |
| 65 // NodeTraversal within assignedAncestor | |
| 66 if (Element* previous = ElementTraversal::previous(current, assignedAncestor )) { | |
| 67 return previous; | |
| 68 } | |
|
kochi
2016/03/01 12:55:40
nit: if the body is one-liner, omit {}.
yuzuchan
2016/03/02 09:02:14
Done.
| |
| 69 // if null, jump to previous assigned node's descendant | |
| 70 const WillBeHeapVector<RefPtrWillBeMember<Node>> assignedNodes = AssignedEle mentTraversal::slot(*assignedAncestor)->getAssignedNodes(); | |
| 71 for (size_t i = assignedNodes.size()-1; i > 0; --i) { | |
|
kochi
2016/03/01 12:55:40
Vector<> also has reverseFind()
yuzuchan
2016/03/02 09:02:14
Done.
| |
| 72 if (assignedAncestor == toElement(assignedNodes[i].get())) { | |
| 73 if (Node* lastChild = assignedNodes[i-1]->lastChild()) | |
| 74 return toElement(lastChild); | |
| 75 // if the previous assigned node has no children, return the assigne d node itself | |
| 76 return toElement(assignedNodes[i-1].get()); | |
| 77 } | |
| 78 } | |
| 79 return nullptr; | |
| 80 } | |
| 81 | |
| 82 bool AssignedElementTraversal::isInAssignedScope(const Element& current) | |
| 83 { | |
| 84 if (AssignedElementTraversal::assignedAncestor(current)) { | |
| 85 return true; | |
| 86 } | |
| 87 return false; | |
|
kochi
2016/03/01 12:55:40
You can write this as
return !!AssignedElementTrav
yuzuchan
2016/03/02 09:02:14
Done.
| |
| 88 } | |
| 89 | |
| 90 } // namespace blink | |
| OLD | NEW |