blob: 128c1e417e2cb77f6af24450253ddf7be7fa2f2d [file] [log] [blame]
Ben Murdoche69819b2013-07-17 14:56:49 +01001/*
2 * Copyright (C) 2012 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
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
13 * distribution.
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.
17 *
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.
29 */
30
31#include "config.h"
32#include "bindings/v8/CustomElementWrapper.h"
33
34#include "V8HTMLElement.h"
35#include "V8HTMLElementWrapperFactory.h"
36#include "V8SVGElement.h"
37#include "V8SVGElementWrapperFactory.h"
38#include "bindings/v8/DOMDataStore.h"
Ben Murdochfff88842013-07-30 15:20:09 +010039#include "bindings/v8/DOMWrapperWorld.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010040#include "bindings/v8/V8PerContextData.h"
Torne (Richard Coles)1e202182013-10-18 15:46:42 +010041#include "core/dom/custom/CustomElement.h"
Ben Murdoche69819b2013-07-17 14:56:49 +010042#include "core/html/HTMLElement.h"
43#include "core/html/HTMLUnknownElement.h"
44#include "core/svg/SVGElement.h"
45
46namespace WebCore {
47
48template<typename ElementType>
49v8::Handle<v8::Object> createDirectWrapper(ElementType*, v8::Handle<v8::Object> creationContext, v8::Isolate*);
50
51template<>
52v8::Handle<v8::Object> createDirectWrapper<HTMLElement>(HTMLElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
53{
54 return createV8HTMLDirectWrapper(element, creationContext, isolate);
55}
56
57template<>
58v8::Handle<v8::Object> createDirectWrapper<SVGElement>(SVGElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
59{
60 return createV8SVGDirectWrapper(element, creationContext, isolate);
61}
62
63template<typename ElementType>
64v8::Handle<v8::Object> createFallbackWrapper(ElementType*, v8::Handle<v8::Object> creationContext, v8::Isolate*);
65
66template<>
67v8::Handle<v8::Object> createFallbackWrapper<HTMLElement>(HTMLElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
68{
69 return createV8HTMLFallbackWrapper(toHTMLUnknownElement(element), creationContext, isolate);
70}
71
72template<>
73v8::Handle<v8::Object> createFallbackWrapper<SVGElement>(SVGElement* element, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
74{
75 return createV8SVGFallbackWrapper(element, creationContext, isolate);
76}
77
78template<typename ElementType>
79v8::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*))
80{
Torne (Richard Coles)e1f1df52013-08-23 16:39:30 +010081 if (CustomElement::isValidName(element->localName()))
Ben Murdoche69819b2013-07-17 14:56:49 +010082 return createDirectWrapper(element, creationContext, isolate);
83 if (createSpecificWrapper)
84 return createSpecificWrapper(element, creationContext, isolate);
85 return createFallbackWrapper(element, creationContext, isolate);
86}
87
88template<typename ElementType, typename WrapperType>
89v8::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*))
90{
91 ASSERT(DOMDataStore::getWrapper<V8Element>(element.get(), isolate).IsEmpty());
92
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();
97
Ben Murdochfff88842013-07-30 15:20:09 +010098 if (!element->isUpgradedCustomElement() || DOMWrapperWorld::isolatedWorld(context))
Ben Murdoche69819b2013-07-17 14:56:49 +010099 return createUpgradeCandidateWrapper(element.get(), creationContext, isolate, createSpecificWrapper);
100
101 V8PerContextData* perContextData = V8PerContextData::from(context);
102 if (!perContextData)
103 return v8::Handle<v8::Object>();
104
Ben Murdoch02772c62013-07-26 10:21:05 +0100105 CustomElementBinding* binding = perContextData->customElementBinding(CustomElement::definitionFor(element.get()));
Ben Murdoche69819b2013-07-17 14:56:49 +0100106 v8::Handle<v8::Object> wrapper = V8DOMWrapper::createWrapper(creationContext, binding->wrapperType(), element.get(), isolate);
107 if (wrapper.IsEmpty())
108 return v8::Handle<v8::Object>();
109
110 wrapper->SetPrototype(binding->prototype());
111
112 V8DOMWrapper::associateObjectWithWrapper<WrapperType>(element, binding->wrapperType(), wrapper, isolate, WrapperConfiguration::Dependent);
113 return wrapper;
114}
115
116template
117class CustomElementWrapper<HTMLElement, V8HTMLElement>;
118
119template
120class CustomElementWrapper<SVGElement, V8SVGElement>;
121
122} // namespace WebCore