2 * Copyright (C) 2015 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "HTMLSlotElement.h"
29 #if ENABLE(SHADOW_DOM) || ENABLE(DETAILS_ELEMENT)
31 #include "ElementChildIterator.h"
32 #include "HTMLNames.h"
33 #include "ShadowRoot.h"
37 using namespace HTMLNames;
39 Ref<HTMLSlotElement> HTMLSlotElement::create(const QualifiedName& tagName, Document& document)
41 return adoptRef(*new HTMLSlotElement(tagName, document));
44 HTMLSlotElement::HTMLSlotElement(const QualifiedName& tagName, Document& document)
45 : HTMLElement(tagName, document)
47 ASSERT(hasTagName(slotTag));
50 HTMLSlotElement::InsertionNotificationRequest HTMLSlotElement::insertedInto(ContainerNode& insertionPoint)
52 auto insertionResult = HTMLElement::insertedInto(insertionPoint);
53 ASSERT_UNUSED(insertionResult, insertionResult == InsertionDone);
55 // This function could be called when this element's shadow root's host or its ancestor is inserted.
56 // This element is new to the shadow tree (and its tree scope) only if the parent into which this element
57 // or its ancestor is inserted belongs to the same tree scope as this element's.
58 if (insertionPoint.isInShadowTree() && isInShadowTree() && &insertionPoint.treeScope() == &treeScope()) {
59 if (auto shadowRoot = containingShadowRoot())
60 shadowRoot->addSlotElementByName(fastGetAttribute(nameAttr), *this);
66 void HTMLSlotElement::removedFrom(ContainerNode& insertionPoint)
68 // ContainerNode::removeBetween always sets the removed chid's tree scope to Document's but InShadowRoot flag is unset in Node::removedFrom.
69 // So if InShadowRoot flag is set but this element's tree scope is Document's, this element has just been removed from a shadow root.
70 if (insertionPoint.isInShadowTree() && isInShadowTree() && &treeScope() == &document()) {
71 auto* oldShadowRoot = insertionPoint.containingShadowRoot();
72 ASSERT(oldShadowRoot);
73 oldShadowRoot->removeSlotElementByName(fastGetAttribute(nameAttr), *this);
76 HTMLElement::removedFrom(insertionPoint);
79 void HTMLSlotElement::attributeChanged(const QualifiedName& name, const AtomicString& oldValue, const AtomicString& newValue, AttributeModificationReason reason)
81 HTMLElement::attributeChanged(name, oldValue, newValue, reason);
83 if (isInShadowTree() && name == nameAttr) {
84 if (auto* shadowRoot = containingShadowRoot()) {
85 shadowRoot->removeSlotElementByName(oldValue, *this);
86 shadowRoot->addSlotElementByName(newValue, *this);
91 const Vector<Node*>* HTMLSlotElement::assignedNodes() const
93 auto* shadowRoot = containingShadowRoot();
97 return shadowRoot->assignedNodesForSlot(*this);