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 "bindings/core/v8/ScriptCustomElementDefinition.h" | 5 #include "bindings/core/v8/ScriptCustomElementDefinition.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptState.h" | 7 #include "bindings/core/v8/ScriptState.h" |
8 #include "bindings/core/v8/V8Binding.h" | 8 #include "bindings/core/v8/V8Binding.h" |
9 #include "bindings/core/v8/V8BindingMacros.h" | 9 #include "bindings/core/v8/V8BindingMacros.h" |
10 #include "bindings/core/v8/V8CustomElementsRegistry.h" | 10 #include "bindings/core/v8/V8CustomElementsRegistry.h" |
11 #include "bindings/core/v8/V8Element.h" | 11 #include "bindings/core/v8/V8Element.h" |
12 #include "bindings/core/v8/V8HiddenValue.h" | 12 #include "bindings/core/v8/V8HiddenValue.h" |
13 #include "bindings/core/v8/V8ScriptRunner.h" | 13 #include "bindings/core/v8/V8ScriptRunner.h" |
14 #include "bindings/core/v8/V8ThrowException.h" | 14 #include "bindings/core/v8/V8ThrowException.h" |
15 #include "core/dom/ExceptionCode.h" | 15 #include "core/dom/ExceptionCode.h" |
| 16 #include "core/html/HTMLElement.h" |
| 17 #include "core/html/HTMLUnknownElement.h" |
16 #include "v8.h" | 18 #include "v8.h" |
17 #include "wtf/Allocator.h" | 19 #include "wtf/Allocator.h" |
18 | 20 |
19 namespace blink { | 21 namespace blink { |
20 | 22 |
21 // Retrieves the custom elements constructor -> name map, creating it | 23 // Retrieves the custom elements constructor -> name map, creating it |
22 // if necessary. The same map is used to keep prototypes alive. | 24 // if necessary. The same map is used to keep prototypes alive. |
23 static v8::Local<v8::Map> ensureCustomElementsRegistryMap( | 25 static v8::Local<v8::Map> ensureCustomElementsRegistryMap( |
24 ScriptState* scriptState, | 26 ScriptState* scriptState, |
25 CustomElementsRegistry* registry) | 27 CustomElementsRegistry* registry) |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
145 const v8::Local<v8::Object>& disconnectedCallback, | 147 const v8::Local<v8::Object>& disconnectedCallback, |
146 const v8::Local<v8::Object>& attributeChangedCallback, | 148 const v8::Local<v8::Object>& attributeChangedCallback, |
147 const HashSet<AtomicString>& observedAttributes) | 149 const HashSet<AtomicString>& observedAttributes) |
148 : CustomElementDefinition(descriptor) | 150 : CustomElementDefinition(descriptor) |
149 , m_scriptState(scriptState) | 151 , m_scriptState(scriptState) |
150 , m_constructor(scriptState->isolate(), constructor) | 152 , m_constructor(scriptState->isolate(), constructor) |
151 , m_observedAttributes(observedAttributes) | 153 , m_observedAttributes(observedAttributes) |
152 { | 154 { |
153 } | 155 } |
154 | 156 |
| 157 HTMLElement* ScriptCustomElementDefinition::createElementSync( |
| 158 Document& document, const QualifiedName& tagName, |
| 159 ExceptionState& exceptionState) |
| 160 { |
| 161 DCHECK(ScriptState::current(m_scriptState->isolate()) == m_scriptState); |
| 162 |
| 163 // Create an element |
| 164 // https://dom.spec.whatwg.org/#concept-create-element |
| 165 // 6. If definition is non-null |
| 166 // 6.1. If the synchronous custom elements flag is set: |
| 167 // 6.1.2. Set result to Construct(C). Rethrow any exceptions. |
| 168 Element* element = nullptr; |
| 169 { |
| 170 v8::TryCatch tryCatch(m_scriptState->isolate()); |
| 171 element = runConstructor(); |
| 172 if (tryCatch.HasCaught()) { |
| 173 exceptionState.rethrowV8Exception(tryCatch.Exception()); |
| 174 return nullptr; |
| 175 } |
| 176 } |
| 177 |
| 178 // 6.1.3. through 6.1.9. |
| 179 checkConstructorResult(element, document, tagName, exceptionState); |
| 180 if (exceptionState.hadException()) |
| 181 return nullptr; |
| 182 |
| 183 DCHECK_EQ(element->getCustomElementState(), CustomElementState::Custom); |
| 184 return toHTMLElement(element); |
| 185 } |
| 186 |
| 187 HTMLElement* ScriptCustomElementDefinition::createElementSync( |
| 188 Document& document, const QualifiedName& tagName) |
| 189 { |
| 190 ScriptState::Scope scope(m_scriptState.get()); |
| 191 v8::Isolate* isolate = m_scriptState->isolate(); |
| 192 |
| 193 // When invoked from "create an element for a token": |
| 194 // https://html.spec.whatwg.org/multipage/syntax.html#create-an-element-for-
the-token |
| 195 |
| 196 ExceptionState exceptionState(ExceptionState::ConstructionContext, |
| 197 "CustomElement", constructor(), isolate); |
| 198 HTMLElement* element = createElementSync(document, tagName, exceptionState); |
| 199 |
| 200 if (exceptionState.hadException() || !element) { |
| 201 // 7. If this step throws an exception, then report the exception, ... |
| 202 { |
| 203 v8::TryCatch tryCatch(isolate); |
| 204 tryCatch.SetVerbose(true); |
| 205 exceptionState.throwIfNeeded(); |
| 206 } |
| 207 |
| 208 // ...and let element be instead a new element that implements |
| 209 // HTMLUnknownElement, with no attributes, namespace set to given |
| 210 // namespace, namespace prefix set to null, custom element state |
| 211 // "undefined", and node document set to document. |
| 212 element = HTMLUnknownElement::create(tagName, document); |
| 213 element->setCustomElementState(CustomElementState::Undefined); |
| 214 } |
| 215 return element; |
| 216 } |
| 217 |
155 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades | 218 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades |
156 bool ScriptCustomElementDefinition::runConstructor(Element* element) | 219 bool ScriptCustomElementDefinition::runConstructor(Element* element) |
157 { | 220 { |
158 if (!m_scriptState->contextIsValid()) | 221 if (!m_scriptState->contextIsValid()) |
159 return false; | 222 return false; |
160 ScriptState::Scope scope(m_scriptState.get()); | 223 ScriptState::Scope scope(m_scriptState.get()); |
161 v8::Isolate* isolate = m_scriptState->isolate(); | 224 v8::Isolate* isolate = m_scriptState->isolate(); |
162 | 225 |
163 // Step 5 says to rethrow the exception; but there is no one to | 226 // Step 5 says to rethrow the exception; but there is no one to |
164 // catch it. The side effect is to report the error. | 227 // catch it. The side effect is to report the error. |
165 v8::TryCatch tryCatch(isolate); | 228 v8::TryCatch tryCatch(isolate); |
166 tryCatch.SetVerbose(true); | 229 tryCatch.SetVerbose(true); |
167 | 230 |
168 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); | 231 Element* result = runConstructor(); |
169 v8::Local<v8::Value> result; | 232 if (!result) |
170 if (!v8Call(V8ScriptRunner::callAsConstructor( | |
171 isolate, | |
172 constructor(), | |
173 executionContext, | |
174 0, | |
175 nullptr), | |
176 result)) | |
177 return false; | 233 return false; |
178 | 234 |
179 if (V8Element::toImplWithTypeCheck(isolate, result) != element) { | 235 if (result != element) { |
180 V8ThrowException::throwException( | 236 V8ThrowException::throwException( |
181 V8ThrowException::createDOMException( | 237 V8ThrowException::createDOMException( |
182 m_scriptState->isolate(), | 238 m_scriptState->isolate(), |
183 InvalidStateError, | 239 InvalidStateError, |
184 "custom element constructors must call super() first and must " | 240 "custom element constructors must call super() first and must " |
185 "not return a different object", | 241 "not return a different object", |
186 constructor()), | 242 constructor()), |
187 m_scriptState->isolate()); | 243 m_scriptState->isolate()); |
188 return false; | 244 return false; |
189 } | 245 } |
190 | 246 |
191 return true; | 247 return true; |
192 } | 248 } |
193 | 249 |
| 250 Element* ScriptCustomElementDefinition::runConstructor() |
| 251 { |
| 252 v8::Isolate* isolate = m_scriptState->isolate(); |
| 253 DCHECK(ScriptState::current(isolate) == m_scriptState); |
| 254 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); |
| 255 v8::Local<v8::Value> result; |
| 256 if (!v8Call(V8ScriptRunner::callAsConstructor( |
| 257 isolate, |
| 258 constructor(), |
| 259 executionContext, |
| 260 0, |
| 261 nullptr), |
| 262 result)) { |
| 263 return nullptr; |
| 264 } |
| 265 return V8Element::toImplWithTypeCheck(isolate, result); |
| 266 } |
| 267 |
194 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const | 268 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const |
195 { | 269 { |
196 DCHECK(!m_constructor.isEmpty()); | 270 DCHECK(!m_constructor.isEmpty()); |
197 return m_constructor.newLocal(m_scriptState->isolate()); | 271 return m_constructor.newLocal(m_scriptState->isolate()); |
198 } | 272 } |
199 | 273 |
200 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const | 274 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const |
201 { | 275 { |
202 DCHECK(!m_prototype.isEmpty()); | 276 DCHECK(!m_prototype.isEmpty()); |
203 return m_prototype.newLocal(m_scriptState->isolate()); | 277 return m_prototype.newLocal(m_scriptState->isolate()); |
204 } | 278 } |
205 | 279 |
206 // CustomElementDefinition | 280 // CustomElementDefinition |
207 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() | 281 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() |
208 { | 282 { |
209 return ScriptValue(m_scriptState.get(), constructor()); | 283 return ScriptValue(m_scriptState.get(), constructor()); |
210 } | 284 } |
211 | 285 |
212 } // namespace blink | 286 } // namespace blink |
OLD | NEW |