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/CustomElement.h" | 5 #include "core/dom/custom/CustomElement.h" |
6 | 6 |
7 #include "core/dom/Document.h" | 7 #include "core/dom/Document.h" |
8 #include "core/dom/QualifiedName.h" | 8 #include "core/dom/QualifiedName.h" |
9 #include "core/dom/custom/CustomElementDefinition.h" | 9 #include "core/dom/custom/CustomElementDefinition.h" |
10 #include "core/dom/custom/CustomElementsRegistry.h" | 10 #include "core/dom/custom/CustomElementsRegistry.h" |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 return RuntimeEnabledFeatures::customElementsV1Enabled() | 77 return RuntimeEnabledFeatures::customElementsV1Enabled() |
78 && document.frame() && isValidName(localName); | 78 && document.frame() && isValidName(localName); |
79 } | 79 } |
80 | 80 |
81 bool CustomElement::shouldCreateCustomElement(Document& document, const Qualifie
dName& tagName) | 81 bool CustomElement::shouldCreateCustomElement(Document& document, const Qualifie
dName& tagName) |
82 { | 82 { |
83 return shouldCreateCustomElement(document, tagName.localName()) | 83 return shouldCreateCustomElement(document, tagName.localName()) |
84 && tagName.namespaceURI() == HTMLNames::xhtmlNamespaceURI; | 84 && tagName.namespaceURI() == HTMLNames::xhtmlNamespaceURI; |
85 } | 85 } |
86 | 86 |
87 HTMLElement* CustomElement::createCustomElement(Document& document, const Atomic
String& localName, CreateElementFlags flags) | 87 static CustomElementDefinition* definitionForName(const Document& document, cons
t QualifiedName& name) |
88 { | 88 { |
89 return createCustomElement(document, | 89 if (CustomElementsRegistry* registry = CustomElement::registry(document)) |
90 QualifiedName(nullAtom, localName, HTMLNames::xhtmlNamespaceURI), | 90 return registry->definitionForName(name.localName()); |
91 flags); | 91 return nullptr; |
92 } | 92 } |
93 | 93 |
94 HTMLElement* CustomElement::createCustomElement(Document& document, const Qualif
iedName& tagName, CreateElementFlags flags) | 94 HTMLElement* CustomElement::createCustomElementSync(Document& document, const At
omicString& localName, ExceptionState& exceptionState) |
95 { | 95 { |
96 DCHECK(shouldCreateCustomElement(document, tagName)); | 96 return createCustomElementSync(document, |
| 97 QualifiedName(nullAtom, localName, HTMLNames::xhtmlNamespaceURI), |
| 98 exceptionState); |
| 99 } |
| 100 |
| 101 HTMLElement* CustomElement::createCustomElementSync(Document& document, const Qu
alifiedName& tagName, ExceptionState& exceptionState) |
| 102 { |
| 103 CHECK(shouldCreateCustomElement(document, tagName)); |
97 | 104 |
98 // To create an element: | 105 // To create an element: |
99 // https://dom.spec.whatwg.org/#concept-create-element | 106 // https://dom.spec.whatwg.org/#concept-create-element |
100 // 6. If definition is non-null, then: | 107 // 6. If definition is non-null, then: |
| 108 // 6.1. If the synchronous custom elements flag is set: |
| 109 if (CustomElementDefinition* definition = definitionForName(document, tagNam
e)) |
| 110 return definition->createElementSync(document, tagName, exceptionState); |
| 111 |
| 112 return createUndefinedElement(document, tagName); |
| 113 } |
| 114 |
| 115 HTMLElement* CustomElement::createCustomElementSync(Document& document, const Qu
alifiedName& tagName) |
| 116 { |
| 117 CHECK(shouldCreateCustomElement(document, tagName)); |
| 118 |
| 119 // When invoked from "create an element for a token": |
| 120 // https://html.spec.whatwg.org/multipage/syntax.html#create-an-element-for-
the-token |
| 121 // 7. If this step throws an exception, then report the exception, |
| 122 // and let element be instead a new element that implements |
| 123 // HTMLUnknownElement. |
| 124 if (CustomElementDefinition* definition = definitionForName(document, tagNam
e)) |
| 125 return definition->createElementSync(document, tagName); |
| 126 |
| 127 return createUndefinedElement(document, tagName); |
| 128 } |
| 129 |
| 130 HTMLElement* CustomElement::createCustomElementAsync(Document& document, const Q
ualifiedName& tagName) |
| 131 { |
| 132 CHECK(shouldCreateCustomElement(document, tagName)); |
| 133 |
| 134 // To create an element: |
| 135 // https://dom.spec.whatwg.org/#concept-create-element |
| 136 // 6. If definition is non-null, then: |
| 137 // 6.2. If the synchronous custom elements flag is not set: |
| 138 if (CustomElementDefinition* definition = definitionForName(document, tagNam
e)) |
| 139 return definition->createElementAsync(document, tagName); |
| 140 |
| 141 return createUndefinedElement(document, tagName); |
| 142 } |
| 143 |
| 144 HTMLElement* CustomElement::createUndefinedElement(Document& document, const Qua
lifiedName& tagName) |
| 145 { |
| 146 DCHECK(shouldCreateCustomElement(document, tagName)); |
| 147 |
101 HTMLElement* element; | 148 HTMLElement* element; |
102 if (CustomElementsRegistry* registry = CustomElement::registry(document)) { | |
103 if (CustomElementDefinition* definition = registry->definitionForName(ta
gName.localName())) { | |
104 // 6.2. If the synchronous custom elements flag is not set: | |
105 if (flags & AsynchronousCustomElements) | |
106 return createCustomElementAsync(document, *definition, tagName); | |
107 | |
108 // TODO(kojii): Synchronous mode implementation coming after async. | |
109 } | |
110 } | |
111 | |
112 if (V0CustomElement::isValidName(tagName.localName()) && document.registrati
onContext()) { | 149 if (V0CustomElement::isValidName(tagName.localName()) && document.registrati
onContext()) { |
113 Element* v0element = document.registrationContext()->createCustomTagElem
ent(document, tagName); | 150 Element* v0element = document.registrationContext()->createCustomTagElem
ent(document, tagName); |
114 SECURITY_DCHECK(v0element->isHTMLElement()); | 151 SECURITY_DCHECK(v0element->isHTMLElement()); |
115 element = toHTMLElement(v0element); | 152 element = toHTMLElement(v0element); |
116 } else { | 153 } else { |
117 element = HTMLElement::create(tagName, document); | 154 element = HTMLElement::create(tagName, document); |
118 } | 155 } |
119 | 156 |
120 element->setCustomElementState(CustomElementState::Undefined); | 157 element->setCustomElementState(CustomElementState::Undefined); |
121 | 158 |
122 return element; | 159 return element; |
123 } | 160 } |
124 | 161 |
125 HTMLElement* CustomElement::createCustomElementAsync(Document& document, | |
126 CustomElementDefinition& definition, const QualifiedName& tagName) | |
127 { | |
128 // https://dom.spec.whatwg.org/#concept-create-element | |
129 // 6. If definition is non-null, then: | |
130 // 6.2. If the synchronous custom elements flag is not set: | |
131 // 6.2.1. Set result to a new element that implements the HTMLElement | |
132 // interface, with no attributes, namespace set to the HTML namespace, | |
133 // namespace prefix set to prefix, local name set to localName, custom | |
134 // element state set to "undefined", and node document set to document. | |
135 HTMLElement* element = HTMLElement::create(tagName, document); | |
136 element->setCustomElementState(CustomElementState::Undefined); | |
137 // 6.2.2. Enqueue a custom element upgrade reaction given result and | |
138 // definition. | |
139 definition.enqueueUpgradeReaction(element); | |
140 return element; | |
141 } | |
142 | |
143 void CustomElement::enqueueConnectedCallback(Element* element) | 162 void CustomElement::enqueueConnectedCallback(Element* element) |
144 { | 163 { |
145 DCHECK_EQ(element->getCustomElementState(), CustomElementState::Custom); | 164 DCHECK_EQ(element->getCustomElementState(), CustomElementState::Custom); |
146 CustomElementDefinition* definition = definitionForElement(*element); | 165 CustomElementDefinition* definition = definitionForElement(*element); |
147 if (definition->hasConnectedCallback()) | 166 if (definition->hasConnectedCallback()) |
148 definition->enqueueConnectedCallback(element); | 167 definition->enqueueConnectedCallback(element); |
149 } | 168 } |
150 | 169 |
151 void CustomElement::enqueueDisconnectedCallback(Element* element) | 170 void CustomElement::enqueueDisconnectedCallback(Element* element) |
152 { | 171 { |
153 DCHECK_EQ(element->getCustomElementState(), CustomElementState::Custom); | 172 DCHECK_EQ(element->getCustomElementState(), CustomElementState::Custom); |
154 CustomElementDefinition* definition = definitionForElement(*element); | 173 CustomElementDefinition* definition = definitionForElement(*element); |
155 if (definition->hasDisconnectedCallback()) | 174 if (definition->hasDisconnectedCallback()) |
156 definition->enqueueDisconnectedCallback(element); | 175 definition->enqueueDisconnectedCallback(element); |
157 } | 176 } |
158 | 177 |
159 | 178 |
160 void CustomElement::enqueueAttributeChangedCallback(Element* element, | 179 void CustomElement::enqueueAttributeChangedCallback(Element* element, |
161 const QualifiedName& name, | 180 const QualifiedName& name, |
162 const AtomicString& oldValue, const AtomicString& newValue) | 181 const AtomicString& oldValue, const AtomicString& newValue) |
163 { | 182 { |
164 DCHECK_EQ(element->getCustomElementState(), CustomElementState::Custom); | 183 DCHECK_EQ(element->getCustomElementState(), CustomElementState::Custom); |
165 CustomElementDefinition* definition = definitionForElement(*element); | 184 CustomElementDefinition* definition = definitionForElement(*element); |
166 if (definition->hasAttributeChangedCallback(name)) | 185 if (definition->hasAttributeChangedCallback(name)) |
167 definition->enqueueAttributeChangedCallback(element, name, oldValue, new
Value); | 186 definition->enqueueAttributeChangedCallback(element, name, oldValue, new
Value); |
168 } | 187 } |
169 | 188 |
170 } // namespace blink | 189 } // namespace blink |
OLD | NEW |