1/*
2 * Copyright (C) 2015, 2016 Apple 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
6 * are met:
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.
12 *
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.
24 */
25
26#include "config.h"
27#include "CustomElementRegistry.h"
28
29#include "CustomElementReactionQueue.h"
30#include "DOMWindow.h"
31#include "Document.h"
32#include "Element.h"
33#include "ElementTraversal.h"
34#include "JSCustomElementInterface.h"
35#include "MathMLNames.h"
36#include "QualifiedName.h"
37#include "ShadowRoot.h"
38#include "TypedElementDescendantIterator.h"
39#include <JavaScriptCore/JSCJSValueInlines.h>
40#include <wtf/text/AtomString.h>
41
42namespace WebCore {
43
44Ref<CustomElementRegistry> CustomElementRegistry::create(DOMWindow& window, ScriptExecutionContext* scriptExecutionContext)
45{
46 return adoptRef(*new CustomElementRegistry(window, scriptExecutionContext));
47}
48
49CustomElementRegistry::CustomElementRegistry(DOMWindow& window, ScriptExecutionContext* scriptExecutionContext)
50 : ContextDestructionObserver(scriptExecutionContext)
51 , m_window(window)
52{
53}
54
55CustomElementRegistry::~CustomElementRegistry() = default;
56
57// https://dom.spec.whatwg.org/#concept-shadow-including-tree-order
58static void enqueueUpgradeInShadowIncludingTreeOrder(ContainerNode& node, JSCustomElementInterface& elementInterface)
59{
60 for (Element* element = ElementTraversal::firstWithin(node); element; element = ElementTraversal::next(*element)) {
61 if (element->isCustomElementUpgradeCandidate() && element->tagQName() == elementInterface.name())
62 element->enqueueToUpgrade(elementInterface);
63 if (auto* shadowRoot = element->shadowRoot()) {
64 if (shadowRoot->mode() != ShadowRootMode::UserAgent)
65 enqueueUpgradeInShadowIncludingTreeOrder(*shadowRoot, elementInterface);
66 }
67 }
68}
69
70void CustomElementRegistry::addElementDefinition(Ref<JSCustomElementInterface>&& elementInterface)
71{
72 AtomString localName = elementInterface->name().localName();
73 ASSERT(!m_nameMap.contains(localName));
74 m_constructorMap.add(elementInterface->constructor(), elementInterface.ptr());
75 m_nameMap.add(localName, elementInterface.copyRef());
76
77 if (auto* document = m_window.document())
78 enqueueUpgradeInShadowIncludingTreeOrder(*document, elementInterface.get());
79
80 if (auto promise = m_promiseMap.take(localName))
81 promise.value()->resolve();
82}
83
84JSCustomElementInterface* CustomElementRegistry::findInterface(const Element& element) const
85{
86 return findInterface(element.tagQName());
87}
88
89JSCustomElementInterface* CustomElementRegistry::findInterface(const QualifiedName& name) const
90{
91 if (name.namespaceURI() != HTMLNames::xhtmlNamespaceURI)
92 return nullptr;
93 return m_nameMap.get(name.localName());
94}
95
96JSCustomElementInterface* CustomElementRegistry::findInterface(const AtomString& name) const
97{
98 return m_nameMap.get(name);
99}
100
101JSCustomElementInterface* CustomElementRegistry::findInterface(const JSC::JSObject* constructor) const
102{
103 return m_constructorMap.get(constructor);
104}
105
106bool CustomElementRegistry::containsConstructor(const JSC::JSObject* constructor) const
107{
108 return m_constructorMap.contains(constructor);
109}
110
111JSC::JSValue CustomElementRegistry::get(const AtomString& name)
112{
113 if (auto* elementInterface = m_nameMap.get(name))
114 return elementInterface->constructor();
115 return JSC::jsUndefined();
116}
117
118static void upgradeElementsInShadowIncludingDescendants(ContainerNode& root)
119{
120 for (auto& element : descendantsOfType<Element>(root)) {
121 if (element.isCustomElementUpgradeCandidate())
122 CustomElementReactionQueue::enqueueElementUpgradeIfDefined(element);
123 if (auto* shadowRoot = element.shadowRoot())
124 upgradeElementsInShadowIncludingDescendants(*shadowRoot);
125 }
126}
127
128void CustomElementRegistry::upgrade(Node& root)
129{
130 if (!is<ContainerNode>(root))
131 return;
132
133 if (is<Element>(root) && downcast<Element>(root).isCustomElementUpgradeCandidate())
134 CustomElementReactionQueue::enqueueElementUpgradeIfDefined(downcast<Element>(root));
135
136 upgradeElementsInShadowIncludingDescendants(downcast<ContainerNode>(root));
137}
138
139}
140