OLD | NEW |
(Empty) | |
| 1 /* |
| 2 * Copyright (C) 2015 Google Inc. All rights reserved. |
| 3 * |
| 4 * Redistribution and use in source and binary forms, with or without |
| 5 * modification, are permitted provided that the following conditions are |
| 6 * met: |
| 7 * |
| 8 * * Redistributions of source code must retain the above copyright |
| 9 * notice, this list of conditions and the following disclaimer. |
| 10 * * Redistributions in binary form must reproduce the above |
| 11 * copyright notice, this list of conditions and the following disclaimer |
| 12 * in the documentation and/or other materials provided with the |
| 13 * distribution. |
| 14 * * Neither the name of Google Inc. nor the names of its |
| 15 * contributors may be used to endorse or promote products derived from |
| 16 * this software without specific prior written permission. |
| 17 * |
| 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 */ |
| 30 |
| 31 #include "config.h" |
| 32 |
| 33 #include "core/dom/shadow/SlotAssignment.h" |
| 34 |
| 35 #include "core/HTMLNames.h" |
| 36 #include "core/dom/ElementTraversal.h" |
| 37 #include "core/dom/NodeTraversal.h" |
| 38 #include "core/dom/shadow/InsertionPoint.h" |
| 39 #include "core/dom/shadow/ShadowRoot.h" |
| 40 #include "core/html/HTMLSlotElement.h" |
| 41 |
| 42 namespace blink { |
| 43 |
| 44 HTMLSlotElement* SlotAssignment::assignedSlotFor(const Node& node) const |
| 45 { |
| 46 return m_assignment.get(const_cast<Node*>(&node)); |
| 47 } |
| 48 |
| 49 static void detachNotAssignedNode(Node& node) |
| 50 { |
| 51 if (node.layoutObject()) |
| 52 node.lazyReattachIfAttached(); |
| 53 } |
| 54 |
| 55 void SlotAssignment::resolveAssignment(const ShadowRoot& shadowRoot) |
| 56 { |
| 57 m_assignment.clear(); |
| 58 |
| 59 using Name2Slot = HashMap<AtomicString, HTMLSlotElement*>; |
| 60 Name2Slot name2slot; |
| 61 HTMLSlotElement* defaultSlot = nullptr; |
| 62 |
| 63 // TODO(hayato): Cache slots elements so that we do not have to travese the
shadow tree. See ShadowRoot::descendantInsertionPoints() |
| 64 for (HTMLSlotElement& slot : Traversal<HTMLSlotElement>::descendantsOf(shado
wRoot)) { |
| 65 slot.clearDistribution(); |
| 66 |
| 67 AtomicString name = slot.fastGetAttribute(HTMLNames::nameAttr); |
| 68 if ((name.isEmpty() || name.isNull()) && !defaultSlot) |
| 69 defaultSlot = &slot; |
| 70 else |
| 71 name2slot.add(name, &slot); |
| 72 } |
| 73 |
| 74 for (Node& child : NodeTraversal::childrenOf(*shadowRoot.host())) { |
| 75 if (child.isElementNode()) { |
| 76 if (isActiveInsertionPoint(child)) { |
| 77 // TODO(hayato): Support re-distribution across v0 and v1 shadow
trees |
| 78 detachNotAssignedNode(child); |
| 79 continue; |
| 80 } |
| 81 AtomicString slotName = toElement(child).fastGetAttribute(HTMLNames:
:slotAttr); |
| 82 if (slotName.isNull() || slotName.isEmpty()) { |
| 83 if (defaultSlot) |
| 84 assign(child, *defaultSlot); |
| 85 else |
| 86 detachNotAssignedNode(child); |
| 87 } else { |
| 88 HTMLSlotElement* slot = name2slot.get(slotName); |
| 89 if (slot) |
| 90 assign(child, *slot); |
| 91 else |
| 92 detachNotAssignedNode(child); |
| 93 } |
| 94 } else if (defaultSlot) { |
| 95 assign(child, *defaultSlot); |
| 96 } else { |
| 97 detachNotAssignedNode(child); |
| 98 } |
| 99 } |
| 100 } |
| 101 |
| 102 void SlotAssignment::assign(Node& hostChild, HTMLSlotElement& slot) |
| 103 { |
| 104 m_assignment.add(&hostChild, &slot); |
| 105 slot.appendAssignedNode(hostChild); |
| 106 if (isHTMLSlotElement(hostChild)) |
| 107 slot.appendDistributedNodes(toHTMLSlotElement(hostChild).getDistributedN
odes()); |
| 108 else |
| 109 slot.appendDistributedNode(hostChild); |
| 110 } |
| 111 |
| 112 DEFINE_TRACE(SlotAssignment) |
| 113 { |
| 114 #if ENABLE(OILPAN) |
| 115 visitor->trace(m_assignment); |
| 116 #endif |
| 117 } |
| 118 |
| 119 } // namespace blink |
OLD | NEW |