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::createElementByRunningConstructor( | |
158 Document& document, const QualifiedName& tagName, | |
159 ExceptionState& exceptionState) | |
160 { | |
161 // Create an element | |
162 // https://dom.spec.whatwg.org/#concept-create-element | |
163 // 6. If definition is non-null | |
164 // 6.1. If the synchronous custom elements flag is set: | |
165 // 6.1.2. Set result to Construct(C). Rethrow any exceptions. | |
166 Element* element; | |
167 { | |
168 v8::TryCatch tryCatch(m_scriptState->isolate()); | |
169 element = runConstructor(); | |
170 if (tryCatch.HasCaught()) { | |
171 exceptionState.rethrowV8Exception(tryCatch.Exception()); | |
172 return nullptr; | |
173 } | |
174 } | |
175 | |
176 // 6.1.3. through 6.1.9. | |
177 errorForConstructorResult(element, document, tagName, exceptionState); | |
dominicc (has gone to gerrit)
2016/06/10 06:13:28
Could we call this checkConstructorResult or somet
kojii
2016/06/10 19:01:45
Done.
| |
178 if (exceptionState.hadException()) | |
179 return nullptr; | |
180 | |
181 DCHECK_EQ(element->getCustomElementState(), CustomElementState::Custom); | |
182 return toHTMLElement(element); | |
183 } | |
184 | |
185 HTMLElement* ScriptCustomElementDefinition::createElementByRunningConstructor( | |
186 Document& document, const QualifiedName& tagName) | |
187 { | |
188 // When invoked from "create an element for a token": | |
189 // https://html.spec.whatwg.org/multipage/syntax.html#create-an-element-for- the-token | |
190 // 7. If this step throws an exception, then report the exception, and | |
191 // let element be instead a new element that implements HTMLUnknownElement, | |
192 // with no attributes, namespace set to given namespace, namespace prefix | |
193 // set to null, custom element state "undefined", and node document set to | |
194 // document. | |
195 v8::Isolate* isolate = m_scriptState->isolate(); | |
dominicc (has gone to gerrit)
2016/06/10 06:13:28
When the parser calls this there will not be a han
kojii
2016/06/10 19:01:44
Done.
| |
196 v8::TryCatch tryCatch(isolate); | |
197 tryCatch.SetVerbose(true); | |
198 ExceptionState exceptionState(ExceptionState::ConstructionContext, "CustomEl ement", constructor(), isolate); | |
199 | |
200 HTMLElement* element = createElementByRunningConstructor(document, tagName, exceptionState); | |
201 if (!exceptionState.throwIfNeeded() && element && !tryCatch.HasCaught()) | |
dominicc (has gone to gerrit)
2016/06/10 06:13:28
exceptionState.throwIfNeeded() will set tryCatch.H
kojii
2016/06/10 19:01:44
Done.
| |
202 return element; | |
203 | |
204 element = HTMLUnknownElement::create(tagName, document); | |
205 element->setCustomElementState(CustomElementState::Undefined); | |
206 return element; | |
dominicc (has gone to gerrit)
2016/06/10 06:13:28
Maybe reversing the condition and putting HTMLUnkn
kojii
2016/06/10 19:01:45
Done.
| |
207 } | |
208 | |
155 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades | 209 // https://html.spec.whatwg.org/multipage/scripting.html#upgrades |
156 bool ScriptCustomElementDefinition::runConstructor(Element* element) | 210 bool ScriptCustomElementDefinition::runConstructor(Element* element) |
157 { | 211 { |
158 if (!m_scriptState->contextIsValid()) | 212 if (!m_scriptState->contextIsValid()) |
159 return false; | 213 return false; |
160 ScriptState::Scope scope(m_scriptState.get()); | |
161 v8::Isolate* isolate = m_scriptState->isolate(); | 214 v8::Isolate* isolate = m_scriptState->isolate(); |
162 | 215 |
163 // Step 5 says to rethrow the exception; but there is no one to | 216 // Step 5 says to rethrow the exception; but there is no one to |
164 // catch it. The side effect is to report the error. | 217 // catch it. The side effect is to report the error. |
165 v8::TryCatch tryCatch(isolate); | 218 v8::TryCatch tryCatch(isolate); |
166 tryCatch.SetVerbose(true); | 219 tryCatch.SetVerbose(true); |
167 | 220 |
168 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); | 221 Element* result = runConstructor(); |
169 v8::Local<v8::Value> result; | 222 if (!result) |
170 if (!v8Call(V8ScriptRunner::callAsConstructor( | |
171 isolate, | |
172 constructor(), | |
173 executionContext, | |
174 0, | |
175 nullptr), | |
176 result)) | |
177 return false; | 223 return false; |
178 | 224 |
179 if (V8Element::toImplWithTypeCheck(isolate, result) != element) { | 225 if (result != element) { |
180 V8ThrowException::throwException( | 226 V8ThrowException::throwException( |
181 V8ThrowException::createDOMException( | 227 V8ThrowException::createDOMException( |
182 m_scriptState->isolate(), | 228 m_scriptState->isolate(), |
183 InvalidStateError, | 229 InvalidStateError, |
184 "custom element constructors must call super() first and must " | 230 "custom element constructors must call super() first and must " |
185 "not return a different object", | 231 "not return a different object", |
186 constructor()), | 232 constructor()), |
187 m_scriptState->isolate()); | 233 m_scriptState->isolate()); |
188 return false; | 234 return false; |
189 } | 235 } |
190 | 236 |
191 return true; | 237 return true; |
192 } | 238 } |
193 | 239 |
240 Element* ScriptCustomElementDefinition::runConstructor() | |
241 { | |
242 if (!m_scriptState->contextIsValid()) | |
dominicc (has gone to gerrit)
2016/06/10 06:13:28
I'm not sure this is right; all the calls are from
kojii
2016/06/10 19:01:45
Removed and the one above revived. Started to unde
| |
243 return nullptr; | |
244 ScriptState::Scope scope(m_scriptState.get()); | |
245 v8::Isolate* isolate = m_scriptState->isolate(); | |
246 ExecutionContext* executionContext = m_scriptState->getExecutionContext(); | |
247 v8::Local<v8::Value> result; | |
248 if (!v8Call(V8ScriptRunner::callAsConstructor( | |
249 isolate, | |
250 constructor(), | |
251 executionContext, | |
252 0, | |
253 nullptr), | |
254 result)) { | |
255 return nullptr; | |
256 } | |
257 return V8Element::toImplWithTypeCheck(isolate, result); | |
258 } | |
259 | |
194 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const | 260 v8::Local<v8::Object> ScriptCustomElementDefinition::constructor() const |
195 { | 261 { |
196 DCHECK(!m_constructor.isEmpty()); | 262 DCHECK(!m_constructor.isEmpty()); |
197 return m_constructor.newLocal(m_scriptState->isolate()); | 263 return m_constructor.newLocal(m_scriptState->isolate()); |
198 } | 264 } |
199 | 265 |
200 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const | 266 v8::Local<v8::Object> ScriptCustomElementDefinition::prototype() const |
201 { | 267 { |
202 DCHECK(!m_prototype.isEmpty()); | 268 DCHECK(!m_prototype.isEmpty()); |
203 return m_prototype.newLocal(m_scriptState->isolate()); | 269 return m_prototype.newLocal(m_scriptState->isolate()); |
204 } | 270 } |
205 | 271 |
206 // CustomElementDefinition | 272 // CustomElementDefinition |
207 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() | 273 ScriptValue ScriptCustomElementDefinition::getConstructorForScript() |
208 { | 274 { |
209 return ScriptValue(m_scriptState.get(), constructor()); | 275 return ScriptValue(m_scriptState.get(), constructor()); |
210 } | 276 } |
211 | 277 |
212 } // namespace blink | 278 } // namespace blink |
OLD | NEW |