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/Attr.h" |
7 #include "core/dom/custom/CEReactionsScope.h" | 8 #include "core/dom/custom/CEReactionsScope.h" |
8 #include "core/dom/custom/CustomElement.h" | 9 #include "core/dom/custom/CustomElement.h" |
9 #include "core/dom/custom/CustomElementAttributeChangedCallbackReaction.h" | 10 #include "core/dom/custom/CustomElementAttributeChangedCallbackReaction.h" |
10 #include "core/dom/custom/CustomElementConnectedCallbackReaction.h" | 11 #include "core/dom/custom/CustomElementConnectedCallbackReaction.h" |
11 #include "core/dom/custom/CustomElementDisconnectedCallbackReaction.h" | 12 #include "core/dom/custom/CustomElementDisconnectedCallbackReaction.h" |
12 #include "core/dom/custom/CustomElementUpgradeReaction.h" | 13 #include "core/dom/custom/CustomElementUpgradeReaction.h" |
13 | 14 |
14 namespace blink { | 15 namespace blink { |
15 | 16 |
16 CustomElementDefinition::CustomElementDefinition( | 17 CustomElementDefinition::CustomElementDefinition( |
17 const CustomElementDescriptor& descriptor) | 18 const CustomElementDescriptor& descriptor) |
18 : m_descriptor(descriptor) | 19 : m_descriptor(descriptor) |
19 { | 20 { |
20 } | 21 } |
21 | 22 |
22 CustomElementDefinition::~CustomElementDefinition() | 23 CustomElementDefinition::~CustomElementDefinition() |
23 { | 24 { |
24 } | 25 } |
25 | 26 |
26 DEFINE_TRACE(CustomElementDefinition) | 27 DEFINE_TRACE(CustomElementDefinition) |
27 { | 28 { |
28 visitor->trace(m_constructionStack); | 29 visitor->trace(m_constructionStack); |
29 } | 30 } |
30 | 31 |
31 // https://html.spec.whatwg.org/multipage/scripting.html#concept-upgrade-an-elem
ent | 32 // https://html.spec.whatwg.org/multipage/scripting.html#concept-upgrade-an-elem
ent |
32 void CustomElementDefinition::upgrade(Element* element) | 33 void CustomElementDefinition::upgrade(Element* element) |
33 { | 34 { |
34 // TODO(kojii): This should be reversed by exposing observedAttributes from | 35 if (!m_observedAttributes.isEmpty()) |
35 // ScriptCustomElementDefinition, because Element::attributes() requires | 36 enqueueAttributeChangedCallbackForAllAttributes(element); |
36 // attribute synchronizations, and generally elements have more attributes | |
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 | 37 |
43 if (element->inShadowIncludingDocument() && hasConnectedCallback()) | 38 if (element->inShadowIncludingDocument() && hasConnectedCallback()) |
44 enqueueConnectedCallback(element); | 39 enqueueConnectedCallback(element); |
45 | 40 |
46 m_constructionStack.append(element); | 41 m_constructionStack.append(element); |
47 size_t depth = m_constructionStack.size(); | 42 size_t depth = m_constructionStack.size(); |
48 | 43 |
49 bool succeeded = runConstructor(element); | 44 bool succeeded = runConstructor(element); |
50 | 45 |
51 // Pop the construction stack. | 46 // Pop the construction stack. |
52 if (m_constructionStack.last().get()) | 47 if (m_constructionStack.last().get()) |
53 DCHECK_EQ(m_constructionStack.last(), element); | 48 DCHECK_EQ(m_constructionStack.last(), element); |
54 DCHECK_EQ(m_constructionStack.size(), depth); // It's a *stack*. | 49 DCHECK_EQ(m_constructionStack.size(), depth); // It's a *stack*. |
55 m_constructionStack.removeLast(); | 50 m_constructionStack.removeLast(); |
56 | 51 |
57 if (!succeeded) | 52 if (!succeeded) |
58 return; | 53 return; |
59 | 54 |
60 CHECK(element->getCustomElementState() == CustomElementState::Custom); | 55 CHECK(element->getCustomElementState() == CustomElementState::Custom); |
61 } | 56 } |
62 | 57 |
| 58 bool CustomElementDefinition::hasAttributeChangedCallback( |
| 59 const QualifiedName& name) |
| 60 { |
| 61 return m_observedAttributes.contains(name.localName()); |
| 62 } |
| 63 |
63 static void enqueueReaction(Element* element, CustomElementReaction* reaction) | 64 static void enqueueReaction(Element* element, CustomElementReaction* reaction) |
64 { | 65 { |
65 // CEReactionsScope must be created by [CEReactions] in IDL, | 66 // CEReactionsScope must be created by [CEReactions] in IDL, |
66 // or callers must setup explicitly if it does not go through bindings. | 67 // or callers must setup explicitly if it does not go through bindings. |
67 DCHECK(CEReactionsScope::current()); | 68 DCHECK(CEReactionsScope::current()); |
68 CEReactionsScope::current()->enqueue(element, reaction); | 69 CEReactionsScope::current()->enqueue(element, reaction); |
69 } | 70 } |
70 | 71 |
71 void CustomElementDefinition::enqueueUpgradeReaction(Element* element) | 72 void CustomElementDefinition::enqueueUpgradeReaction(Element* element) |
72 { | 73 { |
(...skipping 10 matching lines...) Expand all Loading... |
83 enqueueReaction(element, new CustomElementDisconnectedCallbackReaction(this)
); | 84 enqueueReaction(element, new CustomElementDisconnectedCallbackReaction(this)
); |
84 } | 85 } |
85 | 86 |
86 void CustomElementDefinition::enqueueAttributeChangedCallback(Element* element, | 87 void CustomElementDefinition::enqueueAttributeChangedCallback(Element* element, |
87 const QualifiedName& name, | 88 const QualifiedName& name, |
88 const AtomicString& oldValue, const AtomicString& newValue) | 89 const AtomicString& oldValue, const AtomicString& newValue) |
89 { | 90 { |
90 enqueueReaction(element, new CustomElementAttributeChangedCallbackReaction(t
his, name, oldValue, newValue)); | 91 enqueueReaction(element, new CustomElementAttributeChangedCallbackReaction(t
his, name, oldValue, newValue)); |
91 } | 92 } |
92 | 93 |
| 94 void CustomElementDefinition::enqueueAttributeChangedCallbackForAllAttributes( |
| 95 Element* element) |
| 96 { |
| 97 // Avoid synchronizing all attributes unless it is needed, while enqueing |
| 98 // callbacks "in order" as defined in the spec. |
| 99 // https://html.spec.whatwg.org/multipage/scripting.html#concept-upgrade-an-
element |
| 100 for (const AtomicString& name : m_observedAttributes) |
| 101 element->synchronizeAttribute(name); |
| 102 for (const auto& attribute : element->attributesWithoutUpdate()) { |
| 103 if (hasAttributeChangedCallback(attribute.name())) { |
| 104 enqueueAttributeChangedCallback(element, attribute.name(), |
| 105 nullAtom, attribute.value()); |
| 106 } |
| 107 } |
| 108 } |
| 109 |
93 } // namespace blink | 110 } // namespace blink |
OLD | NEW |