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