OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2012 Google Inc. All rights reserved. | 2 * Copyright (C) 2012 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 20 matching lines...) Expand all Loading... |
31 #include "config.h" | 31 #include "config.h" |
32 | 32 |
33 #include "bindings/v8/CustomElementHelpers.h" | 33 #include "bindings/v8/CustomElementHelpers.h" |
34 | 34 |
35 #include "HTMLNames.h" | 35 #include "HTMLNames.h" |
36 #include "V8CustomElementConstructor.h" | 36 #include "V8CustomElementConstructor.h" |
37 #include "V8HTMLElementWrapperFactory.h" | 37 #include "V8HTMLElementWrapperFactory.h" |
38 #include "bindings/v8/DOMWrapperWorld.h" | 38 #include "bindings/v8/DOMWrapperWorld.h" |
39 #include "bindings/v8/ScriptController.h" | 39 #include "bindings/v8/ScriptController.h" |
40 #include "core/dom/CustomElementRegistry.h" | 40 #include "core/dom/CustomElementRegistry.h" |
| 41 #include "core/dom/Node.h" |
| 42 #include "core/html/HTMLElement.h" |
| 43 #include "core/html/HTMLUnknownElement.h" |
41 | 44 |
42 #if ENABLE(SVG) | 45 #if ENABLE(SVG) |
43 #include "V8SVGElementWrapperFactory.h" | 46 #include "V8SVGElementWrapperFactory.h" |
44 #include "SVGNames.h" | 47 #include "SVGNames.h" |
| 48 #include "core/svg/SVGElement.h" |
45 #endif | 49 #endif |
46 | 50 |
47 namespace WebCore { | 51 namespace WebCore { |
48 | 52 |
49 v8::Handle<v8::Object> CustomElementHelpers::createWrapper(PassRefPtr<Element> i
mpl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) | 53 v8::Handle<v8::Object> CustomElementHelpers::createWrapper(PassRefPtr<Element> i
mpl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate, const CreateW
rapperFunction& createTypeExtensionUpgradeCandidateWrapper) |
50 { | 54 { |
51 ASSERT(impl); | 55 ASSERT(impl); |
52 | 56 |
53 // The constructor and registered lifecycle callbacks should be visible only
from main world. | 57 // The constructor and registered lifecycle callbacks should be visible only
from main world. |
54 // FIXME: This shouldn't be needed once each custom element has its own Func
tionTemplate | 58 // FIXME: This shouldn't be needed once each custom element has its own Func
tionTemplate |
55 // https://bugs.webkit.org/show_bug.cgi?id=108138 | 59 // https://bugs.webkit.org/show_bug.cgi?id=108138 |
56 if (!CustomElementHelpers::isFeatureAllowed(creationContext->CreationContext
())) { | 60 if (!CustomElementHelpers::isFeatureAllowed(creationContext->CreationContext
())) { |
57 v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationCon
text, &V8HTMLElement::info, impl.get(), isolate); | 61 v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationCon
text, &V8HTMLElement::info, impl.get(), isolate); |
58 if (!wrapper.IsEmpty()) | 62 if (!wrapper.IsEmpty()) |
59 V8DOMWrapper::associateObjectWithWrapper(impl, &V8HTMLElement::info,
wrapper, isolate, WrapperConfiguration::Dependent); | 63 V8DOMWrapper::associateObjectWithWrapper(impl, &V8HTMLElement::info,
wrapper, isolate, WrapperConfiguration::Dependent); |
60 return wrapper; | 64 return wrapper; |
61 } | 65 } |
62 | 66 |
63 CustomElementRegistry* registry = impl->document()->registry(); | 67 CustomElementRegistry* registry = impl->document()->registry(); |
64 RefPtr<CustomElementDefinition> definition = registry->findFor(impl.get()); | 68 RefPtr<CustomElementDefinition> definition = registry->findFor(impl.get()); |
65 if (!definition) { | 69 if (!definition) |
66 // FIXME: When can this happen? | 70 return createUpgradeCandidateWrapper(impl, creationContext, isolate, cre
ateTypeExtensionUpgradeCandidateWrapper); |
67 return v8::Handle<v8::Object>(); | 71 |
68 } | |
69 v8::Handle<v8::Object> prototype = v8::Handle<v8::Object>::Cast(definition->
prototype().v8Value()); | 72 v8::Handle<v8::Object> prototype = v8::Handle<v8::Object>::Cast(definition->
prototype().v8Value()); |
70 | 73 |
71 WrapperTypeInfo* typeInfo = CustomElementHelpers::findWrapperType(prototype)
; | 74 WrapperTypeInfo* typeInfo = CustomElementHelpers::findWrapperType(prototype)
; |
72 if (!typeInfo) { | 75 if (!typeInfo) { |
73 // FIXME: When can this happen? | 76 // FIXME: When can this happen? |
74 return v8::Handle<v8::Object>(); | 77 return v8::Handle<v8::Object>(); |
75 } | 78 } |
76 | 79 |
77 v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationContext
, typeInfo, impl.get(), isolate); | 80 v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationContext
, typeInfo, impl.get(), isolate); |
78 if (wrapper.IsEmpty()) | 81 if (wrapper.IsEmpty()) |
79 return v8::Handle<v8::Object>(); | 82 return v8::Handle<v8::Object>(); |
80 | 83 |
81 wrapper->SetPrototype(prototype); | 84 wrapper->SetPrototype(prototype); |
82 V8DOMWrapper::associateObjectWithWrapper(impl, typeInfo, wrapper, isolate, W
rapperConfiguration::Dependent); | 85 V8DOMWrapper::associateObjectWithWrapper(impl, typeInfo, wrapper, isolate, W
rapperConfiguration::Dependent); |
83 return wrapper; | 86 return wrapper; |
84 } | 87 } |
85 | 88 |
| 89 v8::Handle<v8::Object> CustomElementHelpers::CreateWrapperFunction::invoke(Eleme
nt* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) const |
| 90 { |
| 91 if (element->isHTMLElement()) { |
| 92 if (m_html) |
| 93 return m_html(toHTMLElement(element), creationContext, isolate); |
| 94 return createV8HTMLFallbackWrapper(toHTMLUnknownElement(toHTMLElement(el
ement)), creationContext, isolate); |
| 95 } |
| 96 #if ENABLE(SVG) |
| 97 else if (element->isSVGElement()) { |
| 98 if (m_svg) |
| 99 return m_svg(toSVGElement(element), creationContext, isolate); |
| 100 return createV8SVGFallbackWrapper(toSVGElement(element), creationContext
, isolate); |
| 101 } |
| 102 #endif |
| 103 ASSERT(0); |
| 104 return v8::Handle<v8::Object>(); |
| 105 } |
| 106 |
| 107 v8::Handle<v8::Object> CustomElementHelpers::createUpgradeCandidateWrapper(PassR
efPtr<Element> element, v8::Handle<v8::Object> creationContext, v8::Isolate* iso
late, const CreateWrapperFunction& createTypeExtensionUpgradeCandidateWrapper) |
| 108 { |
| 109 if (CustomElementRegistry::isCustomTagName(element->localName())) { |
| 110 if (element->isHTMLElement()) |
| 111 return createV8HTMLDirectWrapper(toHTMLElement(element.get()), creat
ionContext, isolate); |
| 112 #if ENABLE(SVG) |
| 113 else if (element->isSVGElement()) |
| 114 return createV8SVGDirectWrapper(toSVGElement(element.get()), creatio
nContext, isolate); |
| 115 #endif |
| 116 else { |
| 117 ASSERT(0); |
| 118 return v8::Handle<v8::Object>(); |
| 119 } |
| 120 } else { |
| 121 // It's a type extension |
| 122 return createTypeExtensionUpgradeCandidateWrapper.invoke(element.get(),
creationContext, isolate); |
| 123 } |
| 124 } |
| 125 |
86 bool CustomElementHelpers::initializeConstructorWrapper(CustomElementConstructor
* constructor, const ScriptValue& prototype, ScriptState* state) | 126 bool CustomElementHelpers::initializeConstructorWrapper(CustomElementConstructor
* constructor, const ScriptValue& prototype, ScriptState* state) |
87 { | 127 { |
88 ASSERT(isFeatureAllowed(state)); | 128 ASSERT(isFeatureAllowed(state)); |
89 ASSERT(!prototype.v8Value().IsEmpty() && prototype.v8Value()->IsObject()); | 129 ASSERT(!prototype.v8Value().IsEmpty() && prototype.v8Value()->IsObject()); |
90 v8::Handle<v8::Value> wrapperValue = toV8(constructor, state->context()->Glo
bal(), state->context()->GetIsolate()); | 130 v8::Handle<v8::Value> wrapperValue = toV8(constructor, state->context()->Glo
bal(), state->context()->GetIsolate()); |
91 if (wrapperValue.IsEmpty() || !wrapperValue->IsObject()) | 131 if (wrapperValue.IsEmpty() || !wrapperValue->IsObject()) |
92 return false; | 132 return false; |
93 v8::Handle<v8::Function> wrapper = v8::Handle<v8::Function>::Cast(wrapperVal
ue); | 133 v8::Handle<v8::Function> wrapper = v8::Handle<v8::Function>::Cast(wrapperVal
ue); |
94 // - Object::ForceSet() nor Object::SetAccessor Doesn't work against the "pr
ototype" property of function objects. | 134 // - Object::ForceSet() nor Object::SetAccessor Doesn't work against the "pr
ototype" property of function objects. |
95 // - Set()-ing here is safe because | 135 // - Set()-ing here is safe because |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 return 0; | 235 return 0; |
196 if (const QualifiedName* htmlName = findHTMLTagNameOfV8Type(type)) | 236 if (const QualifiedName* htmlName = findHTMLTagNameOfV8Type(type)) |
197 return htmlName; | 237 return htmlName; |
198 #if ENABLE(SVG) | 238 #if ENABLE(SVG) |
199 if (const QualifiedName* svgName = findSVGTagNameOfV8Type(type)) | 239 if (const QualifiedName* svgName = findSVGTagNameOfV8Type(type)) |
200 return svgName; | 240 return svgName; |
201 #endif | 241 #endif |
202 return 0; | 242 return 0; |
203 } | 243 } |
204 | 244 |
| 245 bool CustomElementHelpers::isCustomElement(Element* element) |
| 246 { |
| 247 // FIXME: This dynamically consults the "is" attribute; instead a |
| 248 // bit should be marked on elements that are Custom Elements |
| 249 return CustomElementRegistry::isCustomTagName(element->localName()) || Custo
mElementRegistry::isCustomTagName(element->getAttribute(HTMLNames::isAttr)); |
| 250 } |
| 251 |
205 void CustomElementHelpers::invokeReadyCallbackIfNeeded(Element* element, v8::Han
dle<v8::Context> context) | 252 void CustomElementHelpers::invokeReadyCallbackIfNeeded(Element* element, v8::Han
dle<v8::Context> context) |
206 { | 253 { |
207 v8::Handle<v8::Value> wrapperValue = toV8(element, context->Global(), contex
t->GetIsolate()); | 254 v8::Handle<v8::Value> wrapperValue = toV8(element, context->Global(), contex
t->GetIsolate()); |
208 if (wrapperValue.IsEmpty() || !wrapperValue->IsObject()) | 255 if (wrapperValue.IsEmpty() || !wrapperValue->IsObject()) |
209 return; | 256 return; |
210 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(wrapperValue); | 257 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(wrapperValue); |
211 v8::Handle<v8::Value> prototypeValue = wrapper->GetPrototype(); | 258 v8::Handle<v8::Value> prototypeValue = wrapper->GetPrototype(); |
212 if (prototypeValue.IsEmpty() || !prototypeValue->IsObject()) | 259 if (prototypeValue.IsEmpty() || !prototypeValue->IsObject()) |
213 return; | 260 return; |
214 v8::Handle<v8::Object> prototype = v8::Handle<v8::Object>::Cast(prototypeVal
ue); | 261 v8::Handle<v8::Object> prototype = v8::Handle<v8::Object>::Cast(prototypeVal
ue); |
(...skipping 19 matching lines...) Expand all Loading... |
234 return; | 281 return; |
235 v8::Context::Scope scope(context); | 282 v8::Context::Scope scope(context); |
236 | 283 |
237 for (size_t i = 0; i < invocations.size(); ++i) { | 284 for (size_t i = 0; i < invocations.size(); ++i) { |
238 ASSERT(executionContext == invocations[i].element()->document()); | 285 ASSERT(executionContext == invocations[i].element()->document()); |
239 invokeReadyCallbackIfNeeded(invocations[i].element(), context); | 286 invokeReadyCallbackIfNeeded(invocations[i].element(), context); |
240 } | 287 } |
241 } | 288 } |
242 | 289 |
243 } // namespace WebCore | 290 } // namespace WebCore |
OLD | NEW |