2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "bindings/v8/CustomElementWrapper.h"
34 #include "V8HTMLElement.h"
35 #include "V8HTMLElementWrapperFactory.h"
36 #include "V8SVGElement.h"
37 #include "V8SVGElementWrapperFactory.h"
38 #include "bindings/v8/DOMDataStore.h"
39 #include "bindings/v8/DOMWrapperWorld.h"
40 #include "bindings/v8/V8PerContextData.h"
41 #include "core/dom/custom/CustomElement.h"
42 #include "core/html/HTMLElement.h"
43 #include "core/html/HTMLUnknownElement.h"
44 #include "core/svg/SVGElement.h"
48 template<typename ElementType>
49 v8::Handle<v8::Object> createDirectWrapper(ElementType*, v8::Handle<v8::Object> creationContext, v8::Isolate*);
52 v8::Handle<v8::Object> createDirectWrapper<HTMLElement>(HTMLElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
54 return createV8HTMLDirectWrapper(element, creationContext, isolate);
58 v8::Handle<v8::Object> createDirectWrapper<SVGElement>(SVGElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
60 return createV8SVGDirectWrapper(element, creationContext, isolate);
63 template<typename ElementType>
64 v8::Handle<v8::Object> createFallbackWrapper(ElementType*, v8::Handle<v8::Object> creationContext, v8::Isolate*);
67 v8::Handle<v8::Object> createFallbackWrapper<HTMLElement>(HTMLElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
69 return createV8HTMLFallbackWrapper(toHTMLUnknownElement(element), creationContext, isolate);
73 v8::Handle<v8::Object> createFallbackWrapper<SVGElement>(SVGElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
75 return createV8SVGFallbackWrapper(element, creationContext, isolate);
78 template<typename ElementType>
79 v8::Handle<v8::Object> createUpgradeCandidateWrapper(ElementType* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate, v8::Handle<v8::Object> (*createSpecificWrapper)(ElementType* element, v8::Handle<v8::Object> creationContext, v8::Isolate*))
81 if (CustomElement::isValidName(element->localName()))
82 return createDirectWrapper(element, creationContext, isolate);
83 if (createSpecificWrapper)
84 return createSpecificWrapper(element, creationContext, isolate);
85 return createFallbackWrapper(element, creationContext, isolate);
88 template<typename ElementType, typename WrapperType>
89 v8::Handle<v8::Object> CustomElementWrapper<ElementType, WrapperType>::wrap(PassRefPtr<ElementType> element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate, v8::Handle<v8::Object> (*createSpecificWrapper)(ElementType* element, v8::Handle<v8::Object> creationContext, v8::Isolate*))
91 ASSERT(DOMDataStore::getWrapper<V8Element>(element.get(), isolate).IsEmpty());
93 // FIXME: creationContext.IsEmpty() should never happen. Remove
94 // this when callers (like InspectorController::inspect) are fixed
95 // to never pass an empty creation context.
96 v8::Handle<v8::Context> context = creationContext.IsEmpty() ? isolate->GetCurrentContext() : creationContext->CreationContext();
98 if (!element->isUpgradedCustomElement() || DOMWrapperWorld::isolatedWorld(context))
99 return createUpgradeCandidateWrapper(element.get(), creationContext, isolate, createSpecificWrapper);
101 V8PerContextData* perContextData = V8PerContextData::from(context);
103 return v8::Handle<v8::Object>();
105 CustomElementBinding* binding = perContextData->customElementBinding(CustomElement::definitionFor(element.get()));
106 v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationContext, binding->wrapperType(), element.get(), isolate);
107 if (wrapper.IsEmpty())
108 return v8::Handle<v8::Object>();
110 wrapper->SetPrototype(binding->prototype());
112 V8DOMWrapper::associateObjectWithWrapper<WrapperType>(element, binding->wrapperType(), wrapper, isolate, WrapperConfiguration::Dependent);
117 class CustomElementWrapper<HTMLElement, V8HTMLElement>;
120 class CustomElementWrapper<SVGElement, V8SVGElement>;
122 } // namespace WebCore