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/V8HTMLElement.h" | 5 #include "bindings/core/v8/V8HTMLElement.h" |
6 | 6 |
7 #include "bindings/core/v8/DOMWrapperWorld.h" | 7 #include "bindings/core/v8/DOMWrapperWorld.h" |
8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
9 #include "bindings/core/v8/ScriptCustomElementDefinition.h" | 9 #include "bindings/core/v8/ScriptCustomElementDefinition.h" |
10 #include "bindings/core/v8/V8Binding.h" | 10 #include "bindings/core/v8/V8Binding.h" |
11 #include "bindings/core/v8/V8BindingMacros.h" | 11 #include "bindings/core/v8/V8BindingMacros.h" |
12 #include "bindings/core/v8/V8DOMWrapper.h" | 12 #include "bindings/core/v8/V8DOMWrapper.h" |
13 #include "bindings/core/v8/V8ThrowException.h" | 13 #include "bindings/core/v8/V8ThrowException.h" |
14 #include "core/dom/Document.h" | 14 #include "core/dom/Document.h" |
| 15 #include "core/dom/ExceptionCode.h" |
15 #include "core/dom/custom/CustomElementsRegistry.h" | 16 #include "core/dom/custom/CustomElementsRegistry.h" |
16 #include "core/frame/LocalDOMWindow.h" | 17 #include "core/frame/LocalDOMWindow.h" |
17 #include "platform/RuntimeEnabledFeatures.h" | 18 #include "platform/RuntimeEnabledFeatures.h" |
18 | 19 |
19 namespace blink { | 20 namespace blink { |
20 | 21 |
21 void V8HTMLElement::constructorCustom( | 22 void V8HTMLElement::constructorCustom( |
22 const v8::FunctionCallbackInfo<v8::Value>& info) | 23 const v8::FunctionCallbackInfo<v8::Value>& info) |
23 { | 24 { |
24 DCHECK(info.IsConstructCall()); | 25 DCHECK(info.IsConstructCall()); |
25 | 26 |
26 v8::Isolate* isolate = info.GetIsolate(); | 27 v8::Isolate* isolate = info.GetIsolate(); |
27 ScriptState* scriptState = ScriptState::current(isolate); | 28 ScriptState* scriptState = ScriptState::current(isolate); |
28 | 29 |
29 if (!RuntimeEnabledFeatures::customElementsV1Enabled() | 30 if (!RuntimeEnabledFeatures::customElementsV1Enabled() |
30 || !scriptState->world().isMainWorld()) { | 31 || !scriptState->world().isMainWorld()) { |
31 V8ThrowException::throwTypeError(info.GetIsolate(), "Illegal constructor
"); | 32 V8ThrowException::throwTypeError( |
| 33 info.GetIsolate(), |
| 34 "Illegal constructor"); |
32 return; | 35 return; |
33 } | 36 } |
34 | 37 |
35 LocalDOMWindow* window = scriptState->domWindow(); | 38 LocalDOMWindow* window = scriptState->domWindow(); |
36 ScriptCustomElementDefinition* def = | 39 ScriptCustomElementDefinition* definition = |
37 ScriptCustomElementDefinition::forConstructor( | 40 ScriptCustomElementDefinition::forConstructor( |
38 scriptState, | 41 scriptState, |
39 window->customElements(), | 42 window->customElements(), |
40 info.NewTarget()); | 43 info.NewTarget()); |
41 if (!def) { | 44 if (!definition) { |
42 V8ThrowException::throwTypeError(isolate, "Illegal constructor"); | 45 V8ThrowException::throwTypeError(isolate, "Illegal constructor"); |
43 return; | 46 return; |
44 } | 47 } |
45 | 48 |
46 // TODO(dominicc): Implement cases where the definition's | |
47 // construction stack is not empty when parser-creation is | |
48 // implemented. | |
49 | |
50 ExceptionState exceptionState( | 49 ExceptionState exceptionState( |
51 ExceptionState::ConstructionContext, | 50 ExceptionState::ConstructionContext, |
52 "HTMLElement", | 51 "HTMLElement", |
53 info.Holder(), | 52 info.Holder(), |
54 isolate); | 53 isolate); |
55 Element* element = window->document()->createElement( | 54 |
56 def->descriptor().localName(), | 55 Element* element; |
57 AtomicString(), | 56 if (definition->constructionStack().isEmpty()) { |
58 exceptionState); | 57 // This is an element being created with 'new' from script |
59 if (exceptionState.throwIfNeeded()) | 58 element = window->document()->createElement( |
60 return; | 59 definition->descriptor().localName(), |
| 60 AtomicString(), |
| 61 exceptionState); |
| 62 if (exceptionState.throwIfNeeded()) |
| 63 return; |
| 64 } else { |
| 65 element = definition->constructionStack().last(); |
| 66 if (element) { |
| 67 // This is an element being upgraded that has called super |
| 68 definition->constructionStack().last().clear(); |
| 69 } else { |
| 70 // During upgrade an element has invoked the same constructor |
| 71 // before calling 'super' and that invocation has poached the |
| 72 // element. |
| 73 exceptionState.throwDOMException( |
| 74 InvalidStateError, |
| 75 "this instance is already constructed"); |
| 76 exceptionState.throwIfNeeded(); |
| 77 return; |
| 78 } |
| 79 } |
61 const WrapperTypeInfo* wrapperType = element->wrapperTypeInfo(); | 80 const WrapperTypeInfo* wrapperType = element->wrapperTypeInfo(); |
62 v8::Local<v8::Object> wrapper = V8DOMWrapper::associateObjectWithWrapper( | 81 v8::Local<v8::Object> wrapper = V8DOMWrapper::associateObjectWithWrapper( |
63 isolate, | 82 isolate, |
64 element, | 83 element, |
65 wrapperType, | 84 wrapperType, |
66 info.This()); | 85 info.This()); |
| 86 // If the element had a wrapper, we now update and return that |
| 87 // instead. |
| 88 v8SetReturnValue(info, wrapper); |
67 | 89 |
68 if (!v8CallBoolean(wrapper->SetPrototype( | 90 v8CallOrCrash(wrapper->SetPrototype( |
69 scriptState->context(), | 91 scriptState->context(), |
70 def->prototype()))) { | 92 definition->prototype())); |
71 return; | 93 |
72 } | 94 // TODO(dominicc): These elements should be 'undefined', not |
| 95 // 'uncustomized', on creation. Investigate why some elements are |
| 96 // running around uncustomized. |
| 97 if (element->getCustomElementState() == CustomElementState::Uncustomized) |
| 98 element->setCustomElementState(CustomElementState::Undefined); |
| 99 // TODO(dominicc): Move this to the exactly correct place when |
| 100 // https://github.com/whatwg/html/issues/1297 is closed. |
| 101 element->setCustomElementState(CustomElementState::Custom); |
73 } | 102 } |
74 | 103 |
75 } // namespace blink | 104 } // namespace blink |
OLD | NEW |