| 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/V8CustomElementRegistry.h" | 10 #include "bindings/core/v8/V8CustomElementRegistry.h" |
| 11 #include "bindings/core/v8/V8Element.h" | 11 #include "bindings/core/v8/V8Element.h" |
| 12 #include "bindings/core/v8/V8ErrorHandler.h" | 12 #include "bindings/core/v8/V8ErrorHandler.h" |
| 13 #include "bindings/core/v8/V8HiddenValue.h" | 13 #include "bindings/core/v8/V8HiddenValue.h" |
| 14 #include "bindings/core/v8/V8ScriptRunner.h" | 14 #include "bindings/core/v8/V8ScriptRunner.h" |
| 15 #include "bindings/core/v8/V8ThrowException.h" | 15 #include "bindings/core/v8/V8ThrowException.h" |
| 16 #include "core/dom/ExceptionCode.h" | 16 #include "core/dom/ExceptionCode.h" |
| 17 #include "core/dom/custom/CustomElement.h" | 17 #include "core/dom/custom/CustomElement.h" |
| 18 #include "core/events/ErrorEvent.h" | 18 #include "core/events/ErrorEvent.h" |
| 19 #include "core/html/HTMLElement.h" | 19 #include "core/html/HTMLElement.h" |
| 20 #include "core/html/imports/HTMLImportsController.h" |
| 20 #include "v8.h" | 21 #include "v8.h" |
| 21 #include "wtf/Allocator.h" | 22 #include "wtf/Allocator.h" |
| 22 | 23 |
| 23 namespace blink { | 24 namespace blink { |
| 24 | 25 |
| 25 // Retrieves the custom elements constructor -> name map, creating it | 26 // Retrieves the custom elements constructor -> name map, creating it |
| 26 // if necessary. | 27 // if necessary. |
| 27 static v8::Local<v8::Map> ensureCustomElementRegistryMap( | 28 static v8::Local<v8::Map> ensureCustomElementRegistryMap( |
| 28 ScriptState* scriptState, | 29 ScriptState* scriptState, |
| 29 CustomElementRegistry* registry) { | 30 CustomElementRegistry* registry) { |
| (...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 178 return CustomElement::createFailedElement(document, tagName); | 179 return CustomElement::createFailedElement(document, tagName); |
| 179 ScriptState::Scope scope(m_scriptState.get()); | 180 ScriptState::Scope scope(m_scriptState.get()); |
| 180 v8::Isolate* isolate = m_scriptState->isolate(); | 181 v8::Isolate* isolate = m_scriptState->isolate(); |
| 181 | 182 |
| 182 ExceptionState exceptionState(isolate, ExceptionState::ConstructionContext, | 183 ExceptionState exceptionState(isolate, ExceptionState::ConstructionContext, |
| 183 "CustomElement"); | 184 "CustomElement"); |
| 184 | 185 |
| 185 // Create an element with the synchronous custom elements flag set. | 186 // Create an element with the synchronous custom elements flag set. |
| 186 // https://dom.spec.whatwg.org/#concept-create-element | 187 // https://dom.spec.whatwg.org/#concept-create-element |
| 187 | 188 |
| 188 // Create an element and push to the construction stack. | 189 // TODO(dominicc): Implement step 5 which constructs customized |
| 189 // V8HTMLElement::constructorCustom() can only refer to | 190 // built-in elements. |
| 190 // window.document(), but it is different from the document here | 191 |
| 191 // when it is an import document. This is not exactly what the | 192 Element* element = nullptr; |
| 192 // spec defines, but the non-imports behavior matches to the spec. | |
| 193 Element* element = createElementForConstructor(document); | |
| 194 { | 193 { |
| 195 ConstructionStackScope constructionStackScope(this, element); | |
| 196 v8::TryCatch tryCatch(m_scriptState->isolate()); | 194 v8::TryCatch tryCatch(m_scriptState->isolate()); |
| 197 element = runConstructor(); | 195 |
| 196 bool isImportDocument = document.importsController() && |
| 197 document.importsController()->master() != document; |
| 198 if (isImportDocument) { |
| 199 // V8HTMLElement::constructorCustom() can only refer to |
| 200 // window.document() which is not the import document. Create |
| 201 // elements in import documents ahead of time so they end up in |
| 202 // the right document. This subtly violates recursive |
| 203 // construction semantics, but only in import documents. |
| 204 element = createElementForConstructor(document); |
| 205 DCHECK(!tryCatch.HasCaught()); |
| 206 |
| 207 ConstructionStackScope constructionStackScope(this, element); |
| 208 element = callConstructor(); |
| 209 } else { |
| 210 element = callConstructor(); |
| 211 } |
| 212 |
| 198 if (tryCatch.HasCaught()) { | 213 if (tryCatch.HasCaught()) { |
| 199 exceptionState.rethrowV8Exception(tryCatch.Exception()); | 214 exceptionState.rethrowV8Exception(tryCatch.Exception()); |
| 200 return handleCreateElementSyncException(document, tagName, isolate, | 215 return handleCreateElementSyncException(document, tagName, isolate, |
| 201 exceptionState); | 216 exceptionState); |
| 202 } | 217 } |
| 203 } | 218 } |
| 204 | 219 |
| 205 // 6.1.3. through 6.1.9. | 220 // 6.1.3. through 6.1.9. |
| 206 checkConstructorResult(element, document, tagName, exceptionState); | 221 checkConstructorResult(element, document, tagName, exceptionState); |
| 207 if (exceptionState.hadException()) { | 222 if (exceptionState.hadException()) { |
| 208 return handleCreateElementSyncException(document, tagName, isolate, | 223 return handleCreateElementSyncException(document, tagName, isolate, |
| 209 exceptionState); | 224 exceptionState); |
| 210 } | 225 } |
| 211 DCHECK_EQ(element->getCustomElementState(), CustomElementState::Custom); | 226 DCHECK_EQ(element->getCustomElementState(), CustomElementState::Custom); |
| 212 return toHTMLElement(element); | 227 return toHTMLElement(element); |
| 213 } | 228 } |
| 214 | 229 |
| 215 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades | 230 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades |
| 216 bool ScriptCustomElementDefinition::runConstructor(Element* element) { | 231 bool ScriptCustomElementDefinition::runConstructor(Element* element) { |
| 217 if (!m_scriptState->contextIsValid()) | 232 if (!m_scriptState->contextIsValid()) |
| 218 return false; | 233 return false; |
| 219 ScriptState::Scope scope(m_scriptState.get()); | 234 ScriptState::Scope scope(m_scriptState.get()); |
| 220 v8::Isolate* isolate = m_scriptState->isolate(); | 235 v8::Isolate* isolate = m_scriptState->isolate(); |
| 221 | 236 |
| 222 // Step 5 says to rethrow the exception; but there is no one to | 237 // Step 5 says to rethrow the exception; but there is no one to |
| 223 // catch it. The side effect is to report the error. | 238 // catch it. The side effect is to report the error. |
| 224 v8::TryCatch tryCatch(isolate); | 239 v8::TryCatch tryCatch(isolate); |
| 225 tryCatch.SetVerbose(true); | 240 tryCatch.SetVerbose(true); |
| 226 | 241 |
| 227 Element* result = runConstructor(); | 242 Element* result = callConstructor(); |
| 228 | 243 |
| 229 // To report exception thrown from runConstructor() | 244 // To report exception thrown from callConstructor() |
| 230 if (tryCatch.HasCaught()) | 245 if (tryCatch.HasCaught()) |
| 231 return false; | 246 return false; |
| 232 | 247 |
| 233 // To report InvalidStateError Exception, when the constructor returns some | 248 // To report InvalidStateError Exception, when the constructor returns some |
| 234 // different object | 249 // different object |
| 235 if (result != element) { | 250 if (result != element) { |
| 236 const String& message = | 251 const String& message = |
| 237 "custom element constructors must call super() first and must " | 252 "custom element constructors must call super() first and must " |
| 238 "not return a different object"; | 253 "not return a different object"; |
| 239 v8::Local<v8::Value> exception = V8ThrowException::createDOMException( | 254 v8::Local<v8::Value> exception = V8ThrowException::createDOMException( |
| 240 m_scriptState->isolate(), InvalidStateError, message); | 255 m_scriptState->isolate(), InvalidStateError, message); |
| 241 dispatchErrorEvent(isolate, exception, constructor()); | 256 dispatchErrorEvent(isolate, exception, constructor()); |
| 242 return false; | 257 return false; |
| 243 } | 258 } |
| 244 | 259 |
| 245 return true; | 260 return true; |
| 246 } | 261 } |
| 247 | 262 |
| 248 Element* ScriptCustomElementDefinition::runConstructor() { | 263 Element* ScriptCustomElementDefinition::callConstructor() { |
| 249 v8::Isolate* isolate = m_scriptState->isolate(); | 264 v8::Isolate* isolate = m_scriptState->isolate(); |
| 250 DCHECK(ScriptState::current(isolate) == m_scriptState); | 265 DCHECK(ScriptState::current(isolate) == m_scriptState); |
| 251 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); | 266 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); |
| 252 v8::Local<v8::Value> result; | 267 v8::Local<v8::Value> result; |
| 253 if (!v8Call(V8ScriptRunner::callAsConstructor(isolate, constructor(), | 268 if (!v8Call(V8ScriptRunner::callAsConstructor(isolate, constructor(), |
| 254 executionContext, 0, nullptr), | 269 executionContext, 0, nullptr), |
| 255 result)) { | 270 result)) { |
| 256 return nullptr; | 271 return nullptr; |
| 257 } | 272 } |
| 258 return V8Element::toImplWithTypeCheck(isolate, result); | 273 return V8Element::toImplWithTypeCheck(isolate, result); |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 343 v8::Local<v8::Value> argv[] = { | 358 v8::Local<v8::Value> argv[] = { |
| 344 v8String(isolate, name.localName()), v8StringOrNull(isolate, oldValue), | 359 v8String(isolate, name.localName()), v8StringOrNull(isolate, oldValue), |
| 345 v8StringOrNull(isolate, newValue), | 360 v8StringOrNull(isolate, newValue), |
| 346 v8StringOrNull(isolate, name.namespaceURI()), | 361 v8StringOrNull(isolate, name.namespaceURI()), |
| 347 }; | 362 }; |
| 348 runCallback(m_attributeChangedCallback.newLocal(isolate), element, | 363 runCallback(m_attributeChangedCallback.newLocal(isolate), element, |
| 349 WTF_ARRAY_LENGTH(argv), argv); | 364 WTF_ARRAY_LENGTH(argv), argv); |
| 350 } | 365 } |
| 351 | 366 |
| 352 } // namespace blink | 367 } // namespace blink |
| OLD | NEW |