1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
|
// Copyright 2016 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "core/dom/shadow/SlotScopedTraversal.h"
#include "core/dom/Element.h"
#include "core/dom/ElementTraversal.h"
#include "core/html/HTMLSlotElement.h"
namespace blink {
namespace {
Element* nextSkippingChildrenOfShadowHost(const Element& start,
const Element& scope) {
DCHECK(scope.assignedSlot());
if (!start.authorShadowRoot()) {
if (Element* first = ElementTraversal::firstChild(start))
return first;
}
for (const Element* current = &start; current != scope;
current = current->parentElement()) {
if (Element* nextSibling = ElementTraversal::nextSibling(*current))
return nextSibling;
}
return nullptr;
}
Element* lastWithinOrSelfSkippingChildrenOfShadowHost(const Element& scope) {
Element* current = const_cast<Element*>(&scope);
while (!current->authorShadowRoot()) {
Element* lastChild = ElementTraversal::lastChild(*current);
if (!lastChild)
break;
current = lastChild;
}
return current;
}
Element* previousSkippingChildrenOfShadowHost(const Element& start,
const Element& scope) {
DCHECK(scope.assignedSlot());
DCHECK_NE(start, &scope);
if (Element* previousSibling = ElementTraversal::previousSibling(start))
return lastWithinOrSelfSkippingChildrenOfShadowHost(*previousSibling);
return start.parentElement();
}
} // namespace
HTMLSlotElement* SlotScopedTraversal::findScopeOwnerSlot(
const Element& current) {
if (Element* nearestInclusiveAncestorAssignedToSlot =
SlotScopedTraversal::nearestInclusiveAncestorAssignedToSlot(current))
return nearestInclusiveAncestorAssignedToSlot->assignedSlot();
return nullptr;
}
Element* SlotScopedTraversal::nearestInclusiveAncestorAssignedToSlot(
const Element& current) {
Element* element = const_cast<Element*>(¤t);
for (; element; element = element->parentElement()) {
if (element->assignedSlot())
break;
}
return element;
}
Element* SlotScopedTraversal::next(const Element& current) {
Element* nearestInclusiveAncestorAssignedToSlot =
SlotScopedTraversal::nearestInclusiveAncestorAssignedToSlot(current);
DCHECK(nearestInclusiveAncestorAssignedToSlot);
// Search within children of an element which is assigned to a slot.
if (Element* next = nextSkippingChildrenOfShadowHost(
current, *nearestInclusiveAncestorAssignedToSlot))
return next;
// Seek to the next element assigned to the same slot.
HTMLSlotElement* slot =
nearestInclusiveAncestorAssignedToSlot->assignedSlot();
DCHECK(slot);
const HeapVector<Member<Node>>& assignedNodes = slot->assignedNodes();
size_t currentIndex =
assignedNodes.find(*nearestInclusiveAncestorAssignedToSlot);
DCHECK_NE(currentIndex, kNotFound);
for (++currentIndex; currentIndex < assignedNodes.size(); ++currentIndex) {
if (assignedNodes[currentIndex]->isElementNode())
return toElement(assignedNodes[currentIndex]);
}
return nullptr;
}
Element* SlotScopedTraversal::previous(const Element& current) {
Element* nearestInclusiveAncestorAssignedToSlot =
SlotScopedTraversal::nearestInclusiveAncestorAssignedToSlot(current);
DCHECK(nearestInclusiveAncestorAssignedToSlot);
if (current != nearestInclusiveAncestorAssignedToSlot) {
// Search within children of an element which is assigned to a slot.
Element* previous = previousSkippingChildrenOfShadowHost(
current, *nearestInclusiveAncestorAssignedToSlot);
DCHECK(previous);
return previous;
}
// Seek to the previous element assigned to the same slot.
const HeapVector<Member<Node>>& assignedNodes =
nearestInclusiveAncestorAssignedToSlot->assignedSlot()->assignedNodes();
size_t currentIndex =
assignedNodes.reverseFind(*nearestInclusiveAncestorAssignedToSlot);
DCHECK_NE(currentIndex, kNotFound);
for (; currentIndex > 0; --currentIndex) {
const Member<Node> assignedNode = assignedNodes[currentIndex - 1];
if (!assignedNode->isElementNode())
continue;
return lastWithinOrSelfSkippingChildrenOfShadowHost(
*toElement(assignedNode));
}
return nullptr;
}
Element* SlotScopedTraversal::firstAssignedToSlot(HTMLSlotElement& slot) {
const HeapVector<Member<Node>>& assignedNodes = slot.assignedNodes();
for (auto assignedNode : assignedNodes) {
if (assignedNode->isElementNode())
return toElement(assignedNode);
}
return nullptr;
}
Element* SlotScopedTraversal::lastAssignedToSlot(HTMLSlotElement& slot) {
const HeapVector<Member<Node>>& assignedNodes = slot.assignedNodes();
for (auto assignedNode = assignedNodes.rbegin();
assignedNode != assignedNodes.rend(); ++assignedNode) {
if (!(*assignedNode)->isElementNode())
continue;
return lastWithinOrSelfSkippingChildrenOfShadowHost(
*toElement(*assignedNode));
}
return nullptr;
}
bool SlotScopedTraversal::isSlotScoped(const Element& current) {
return SlotScopedTraversal::nearestInclusiveAncestorAssignedToSlot(current);
}
} // namespace blink
|