OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "core/dom/custom/CustomElementDefinition.h" | 5 #include "core/dom/custom/CustomElementDefinition.h" |
6 | 6 |
| 7 #include "core/dom/custom/CEReactionsScope.h" |
| 8 #include "core/dom/custom/CustomElement.h" |
| 9 #include "core/dom/custom/CustomElementAttributeChangedCallbackReaction.h" |
| 10 #include "core/dom/custom/CustomElementConnectedCallbackReaction.h" |
| 11 #include "core/dom/custom/CustomElementDisconnectedCallbackReaction.h" |
| 12 #include "core/dom/custom/CustomElementUpgradeReaction.h" |
| 13 |
7 namespace blink { | 14 namespace blink { |
8 | 15 |
9 CustomElementDefinition::CustomElementDefinition( | 16 CustomElementDefinition::CustomElementDefinition( |
10 const CustomElementDescriptor& descriptor) | 17 const CustomElementDescriptor& descriptor) |
11 : m_descriptor(descriptor) | 18 : m_descriptor(descriptor) |
12 { | 19 { |
13 } | 20 } |
14 | 21 |
15 CustomElementDefinition::~CustomElementDefinition() | 22 CustomElementDefinition::~CustomElementDefinition() |
16 { | 23 { |
17 } | 24 } |
18 | 25 |
19 DEFINE_TRACE(CustomElementDefinition) | 26 DEFINE_TRACE(CustomElementDefinition) |
20 { | 27 { |
21 visitor->trace(m_constructionStack); | 28 visitor->trace(m_constructionStack); |
22 } | 29 } |
23 | 30 |
24 // https://html.spec.whatwg.org/multipage/scripting.html#concept-upgrade-an-elem
ent | 31 // https://html.spec.whatwg.org/multipage/scripting.html#concept-upgrade-an-elem
ent |
25 void CustomElementDefinition::upgrade(Element* element) | 32 void CustomElementDefinition::upgrade(Element* element) |
26 { | 33 { |
27 // TODO(dominicc): When the attributeChangedCallback is implemented, | 34 // TODO(kojii): This should be reversed by exposing observedAttributes from |
28 // enqueue reactions for attributes here. | 35 // ScriptCustomElementDefinition, because Element::attributes() requires |
29 // TODO(dominicc): When the connectedCallback is implemented, enqueue | 36 // attribute synchronizations, and generally elements have more attributes |
30 // reactions here, if applicable. | 37 // than custom elements observe. |
| 38 for (const auto& attribute : element->attributes()) { |
| 39 if (hasAttributeChangedCallback(attribute.name())) |
| 40 enqueueAttributeChangedCallback(element, attribute.name(), nullAtom,
attribute.value()); |
| 41 } |
| 42 |
| 43 if (element->inShadowIncludingDocument() && hasConnectedCallback()) |
| 44 enqueueConnectedCallback(element); |
31 | 45 |
32 m_constructionStack.append(element); | 46 m_constructionStack.append(element); |
33 size_t depth = m_constructionStack.size(); | 47 size_t depth = m_constructionStack.size(); |
34 | 48 |
35 bool succeeded = runConstructor(element); | 49 bool succeeded = runConstructor(element); |
36 | 50 |
37 // Pop the construction stack. | 51 // Pop the construction stack. |
38 if (m_constructionStack.last().get()) | 52 if (m_constructionStack.last().get()) |
39 DCHECK_EQ(m_constructionStack.last(), element); | 53 DCHECK_EQ(m_constructionStack.last(), element); |
40 DCHECK_EQ(m_constructionStack.size(), depth); // It's a *stack*. | 54 DCHECK_EQ(m_constructionStack.size(), depth); // It's a *stack*. |
41 m_constructionStack.removeLast(); | 55 m_constructionStack.removeLast(); |
42 | 56 |
43 if (!succeeded) | 57 if (!succeeded) |
44 return; | 58 return; |
45 | 59 |
46 CHECK(element->getCustomElementState() == CustomElementState::Custom); | 60 CHECK(element->getCustomElementState() == CustomElementState::Custom); |
47 } | 61 } |
48 | 62 |
| 63 static void enqueueReaction(Element* element, CustomElementReaction* reaction) |
| 64 { |
| 65 // CEReactionsScope must be created by [CEReactions] in IDL, |
| 66 // or callers must setup explicitly if it does not go through bindings. |
| 67 DCHECK(CEReactionsScope::current()); |
| 68 CEReactionsScope::current()->enqueue(element, reaction); |
| 69 } |
| 70 |
| 71 void CustomElementDefinition::enqueueUpgradeReaction(Element* element) |
| 72 { |
| 73 enqueueReaction(element, new CustomElementUpgradeReaction(this)); |
| 74 } |
| 75 |
| 76 void CustomElementDefinition::enqueueConnectedCallback(Element* element) |
| 77 { |
| 78 enqueueReaction(element, new CustomElementConnectedCallbackReaction(this)); |
| 79 } |
| 80 |
| 81 void CustomElementDefinition::enqueueDisconnectedCallback(Element* element) |
| 82 { |
| 83 enqueueReaction(element, new CustomElementDisconnectedCallbackReaction(this)
); |
| 84 } |
| 85 |
| 86 void CustomElementDefinition::enqueueAttributeChangedCallback(Element* element, |
| 87 const QualifiedName& name, |
| 88 const AtomicString& oldValue, const AtomicString& newValue) |
| 89 { |
| 90 enqueueReaction(element, new CustomElementAttributeChangedCallbackReaction(t
his, name, oldValue, newValue)); |
| 91 } |
| 92 |
49 } // namespace blink | 93 } // namespace blink |
OLD | NEW |