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( |
kojii
2016/06/03 07:50:42
nit: this needs to be HTMLElementFactory::createHT
| |
59 definition->descriptor().localName(), | |
60 AtomicString(), | |
61 exceptionState); | |
62 if (exceptionState.throwIfNeeded()) | |
63 return; | |
64 } else if ((element = definition->constructionStack().last())) { | |
Sébastien Marchand
2016/06/03 13:34:33
This is breaking the Windows PGO builder (and it'l
| |
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(); | |
60 return; | 75 return; |
76 } | |
61 const WrapperTypeInfo* wrapperType = element->wrapperTypeInfo(); | 77 const WrapperTypeInfo* wrapperType = element->wrapperTypeInfo(); |
62 v8::Local<v8::Object> wrapper = V8DOMWrapper::associateObjectWithWrapper( | 78 v8::Local<v8::Object> wrapper = V8DOMWrapper::associateObjectWithWrapper( |
63 isolate, | 79 isolate, |
64 element, | 80 element, |
65 wrapperType, | 81 wrapperType, |
66 info.This()); | 82 info.This()); |
83 // If the element had a wrapper, we now update and return that | |
84 // instead. | |
85 v8SetReturnValue(info, wrapper); | |
67 | 86 |
68 if (!v8CallBoolean(wrapper->SetPrototype( | 87 v8CallOrCrash(wrapper->SetPrototype( |
69 scriptState->context(), | 88 scriptState->context(), |
70 def->prototype()))) { | 89 definition->prototype())); |
71 return; | 90 |
72 } | 91 // TODO(dominicc): These elements should be 'undefined', not |
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); | |
73 } | 99 } |
74 | 100 |
75 } // namespace blink | 101 } // namespace blink |
OLD | NEW |