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 WrapperTypeInfo* info; |
| 110 |
| 111 if (CustomElementRegistry::isCustomTagName(element->localName())) { |
| 112 if (element->isHTMLElement()) |
| 113 return createV8HTMLDirectWrapper(toHTMLElement(element.get()), creat
ionContext, isolate); |
| 114 #if ENABLE(SVG) |
| 115 else if (element->isSVGElement()) |
| 116 return createV8SVGDirectWrapper(toSVGElement(element.get()), creatio
nContext, isolate); |
| 117 #endif |
| 118 else { |
| 119 ASSERT(0); |
| 120 return v8::Handle<v8::Object>(); |
| 121 } |
| 122 } else { |
| 123 // It's a type extension |
| 124 return createTypeExtensionUpgradeCandidateWrapper.invoke(element.get(),
creationContext, isolate); |
| 125 } |
| 126 } |
| 127 |
86 bool CustomElementHelpers::initializeConstructorWrapper(CustomElementConstructor
* constructor, const ScriptValue& prototype, ScriptState* state) | 128 bool CustomElementHelpers::initializeConstructorWrapper(CustomElementConstructor
* constructor, const ScriptValue& prototype, ScriptState* state) |
87 { | 129 { |
88 ASSERT(isFeatureAllowed(state)); | 130 ASSERT(isFeatureAllowed(state)); |
89 ASSERT(!prototype.v8Value().IsEmpty() && prototype.v8Value()->IsObject()); | 131 ASSERT(!prototype.v8Value().IsEmpty() && prototype.v8Value()->IsObject()); |
90 v8::Handle<v8::Value> wrapperValue = toV8(constructor, state->context()->Glo
bal(), state->context()->GetIsolate()); | 132 v8::Handle<v8::Value> wrapperValue = toV8(constructor, state->context()->Glo
bal(), state->context()->GetIsolate()); |
91 if (wrapperValue.IsEmpty() || !wrapperValue->IsObject()) | 133 if (wrapperValue.IsEmpty() || !wrapperValue->IsObject()) |
92 return false; | 134 return false; |
93 v8::Handle<v8::Function> wrapper = v8::Handle<v8::Function>::Cast(wrapperVal
ue); | 135 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. | 136 // - Object::ForceSet() nor Object::SetAccessor Doesn't work against the "pr
ototype" property of function objects. |
95 // - Set()-ing here is safe because | 137 // - Set()-ing here is safe because |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
195 return 0; | 237 return 0; |
196 if (const QualifiedName* htmlName = findHTMLTagNameOfV8Type(type)) | 238 if (const QualifiedName* htmlName = findHTMLTagNameOfV8Type(type)) |
197 return htmlName; | 239 return htmlName; |
198 #if ENABLE(SVG) | 240 #if ENABLE(SVG) |
199 if (const QualifiedName* svgName = findSVGTagNameOfV8Type(type)) | 241 if (const QualifiedName* svgName = findSVGTagNameOfV8Type(type)) |
200 return svgName; | 242 return svgName; |
201 #endif | 243 #endif |
202 return 0; | 244 return 0; |
203 } | 245 } |
204 | 246 |
| 247 bool CustomElementHelpers::isCustomElement(Element* element) |
| 248 { |
| 249 // FIXME: This dynamically consults the "is" attribute; instead a |
| 250 // bit should be marked on elements that are Custom Elements |
| 251 return CustomElementRegistry::isCustomTagName(element->localName()) || Custo
mElementRegistry::isCustomTagName(element->getAttribute(HTMLNames::isAttr)); |
| 252 } |
| 253 |
205 void CustomElementHelpers::invokeReadyCallbackIfNeeded(Element* element, v8::Han
dle<v8::Context> context) | 254 void CustomElementHelpers::invokeReadyCallbackIfNeeded(Element* element, v8::Han
dle<v8::Context> context) |
206 { | 255 { |
207 v8::Handle<v8::Value> wrapperValue = toV8(element, context->Global(), contex
t->GetIsolate()); | 256 v8::Handle<v8::Value> wrapperValue = toV8(element, context->Global(), contex
t->GetIsolate()); |
208 if (wrapperValue.IsEmpty() || !wrapperValue->IsObject()) | 257 if (wrapperValue.IsEmpty() || !wrapperValue->IsObject()) |
209 return; | 258 return; |
210 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(wrapperValue); | 259 v8::Handle<v8::Object> wrapper = v8::Handle<v8::Object>::Cast(wrapperValue); |
211 v8::Handle<v8::Value> prototypeValue = wrapper->GetPrototype(); | 260 v8::Handle<v8::Value> prototypeValue = wrapper->GetPrototype(); |
212 if (prototypeValue.IsEmpty() || !prototypeValue->IsObject()) | 261 if (prototypeValue.IsEmpty() || !prototypeValue->IsObject()) |
213 return; | 262 return; |
214 v8::Handle<v8::Object> prototype = v8::Handle<v8::Object>::Cast(prototypeVal
ue); | 263 v8::Handle<v8::Object> prototype = v8::Handle<v8::Object>::Cast(prototypeVal
ue); |
(...skipping 19 matching lines...) Expand all Loading... |
234 return; | 283 return; |
235 v8::Context::Scope scope(context); | 284 v8::Context::Scope scope(context); |
236 | 285 |
237 for (size_t i = 0; i < invocations.size(); ++i) { | 286 for (size_t i = 0; i < invocations.size(); ++i) { |
238 ASSERT(executionContext == invocations[i].element()->document()); | 287 ASSERT(executionContext == invocations[i].element()->document()); |
239 invokeReadyCallbackIfNeeded(invocations[i].element(), context); | 288 invokeReadyCallbackIfNeeded(invocations[i].element(), context); |
240 } | 289 } |
241 } | 290 } |
242 | 291 |
243 } // namespace WebCore | 292 } // namespace WebCore |
OLD | NEW |