Upstream version 9.37.197.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / custom / V8NodeCustom.cpp
1 /*
2  * Copyright (C) 2007-2009 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/core/v8/V8Node.h"
33
34 #include "bindings/core/v8/V8Attr.h"
35 #include "bindings/core/v8/V8CDATASection.h"
36 #include "bindings/core/v8/V8Comment.h"
37 #include "bindings/core/v8/V8Document.h"
38 #include "bindings/core/v8/V8DocumentFragment.h"
39 #include "bindings/core/v8/V8DocumentType.h"
40 #include "bindings/core/v8/V8Element.h"
41 #include "bindings/core/v8/V8HTMLElement.h"
42 #include "bindings/core/v8/V8Notation.h"
43 #include "bindings/core/v8/V8ProcessingInstruction.h"
44 #include "bindings/core/v8/V8SVGElement.h"
45 #include "bindings/core/v8/V8ShadowRoot.h"
46 #include "bindings/core/v8/V8Text.h"
47 #include "bindings/v8/ExceptionState.h"
48 #include "bindings/v8/V8AbstractEventListener.h"
49 #include "bindings/v8/V8Binding.h"
50 #include "bindings/v8/V8EventListener.h"
51 #include "core/dom/Document.h"
52 #include "core/dom/custom/CustomElementCallbackDispatcher.h"
53 #include "core/events/EventListener.h"
54 #include "core/dom/shadow/ShadowRoot.h"
55 #include "wtf/RefPtr.h"
56
57 namespace WebCore {
58
59 // These functions are custom to prevent a wrapper lookup of the return value which is always
60 // part of the arguments.
61 void V8Node::insertBeforeMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
62 {
63     v8::Handle<v8::Object> holder = info.Holder();
64     Node* impl = V8Node::toNative(holder);
65
66     CustomElementCallbackDispatcher::CallbackDeliveryScope deliveryScope;
67
68     ExceptionState exceptionState(ExceptionState::ExecutionContext, "insertBefore", "Node", info.Holder(), info.GetIsolate());
69     Node* newChild = V8Node::toNativeWithTypeCheck(info.GetIsolate(), info[0]);
70     Node* refChild = V8Node::toNativeWithTypeCheck(info.GetIsolate(), info[1]);
71     impl->insertBefore(newChild, refChild, exceptionState);
72     if (exceptionState.throwIfNeeded())
73         return;
74     v8SetReturnValue(info, info[0]);
75 }
76
77 void V8Node::replaceChildMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
78 {
79     v8::Handle<v8::Object> holder = info.Holder();
80     Node* impl = V8Node::toNative(holder);
81
82     CustomElementCallbackDispatcher::CallbackDeliveryScope deliveryScope;
83
84     ExceptionState exceptionState(ExceptionState::ExecutionContext, "replaceChild", "Node", info.Holder(), info.GetIsolate());
85     Node* newChild = V8Node::toNativeWithTypeCheck(info.GetIsolate(), info[0]);
86     Node* oldChild = V8Node::toNativeWithTypeCheck(info.GetIsolate(), info[1]);
87     impl->replaceChild(newChild, oldChild, exceptionState);
88     if (exceptionState.throwIfNeeded())
89         return;
90     v8SetReturnValue(info, info[1]);
91 }
92
93 void V8Node::removeChildMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
94 {
95     v8::Handle<v8::Object> holder = info.Holder();
96     Node* impl = V8Node::toNative(holder);
97
98     CustomElementCallbackDispatcher::CallbackDeliveryScope deliveryScope;
99
100     ExceptionState exceptionState(ExceptionState::ExecutionContext, "removeChild", "Node", info.Holder(), info.GetIsolate());
101     Node* oldChild = V8Node::toNativeWithTypeCheck(info.GetIsolate(), info[0]);
102     impl->removeChild(oldChild, exceptionState);
103     if (exceptionState.throwIfNeeded())
104         return;
105     v8SetReturnValue(info, info[0]);
106 }
107
108 void V8Node::appendChildMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
109 {
110     v8::Handle<v8::Object> holder = info.Holder();
111     Node* impl = V8Node::toNative(holder);
112
113     CustomElementCallbackDispatcher::CallbackDeliveryScope deliveryScope;
114
115     ExceptionState exceptionState(ExceptionState::ExecutionContext, "appendChild", "Node", info.Holder(), info.GetIsolate());
116     Node* newChild = V8Node::toNativeWithTypeCheck(info.GetIsolate(), info[0]);
117     impl->appendChild(newChild, exceptionState);
118     if (exceptionState.throwIfNeeded())
119         return;
120     v8SetReturnValue(info, info[0]);
121 }
122
123 v8::Handle<v8::Object> wrap(Node* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
124 {
125     ASSERT(impl);
126     switch (impl->nodeType()) {
127     case Node::ELEMENT_NODE:
128         // For performance reasons, this is inlined from V8Element::wrap and must remain in sync.
129         if (impl->isHTMLElement())
130             return wrap(toHTMLElement(impl), creationContext, isolate);
131         if (impl->isSVGElement())
132             return wrap(toSVGElement(impl), creationContext, isolate);
133         return V8Element::createWrapper(toElement(impl), creationContext, isolate);
134     case Node::ATTRIBUTE_NODE:
135         return wrap(toAttr(impl), creationContext, isolate);
136     case Node::TEXT_NODE:
137         return wrap(toText(impl), creationContext, isolate);
138     case Node::CDATA_SECTION_NODE:
139         return wrap(toCDATASection(impl), creationContext, isolate);
140     case Node::PROCESSING_INSTRUCTION_NODE:
141         return wrap(toProcessingInstruction(impl), creationContext, isolate);
142     case Node::COMMENT_NODE:
143         return wrap(toComment(impl), creationContext, isolate);
144     case Node::DOCUMENT_NODE:
145         return wrap(toDocument(impl), creationContext, isolate);
146     case Node::DOCUMENT_TYPE_NODE:
147         return wrap(toDocumentType(impl), creationContext, isolate);
148     case Node::DOCUMENT_FRAGMENT_NODE:
149         if (impl->isShadowRoot())
150             return wrap(toShadowRoot(impl), creationContext, isolate);
151         return wrap(toDocumentFragment(impl), creationContext, isolate);
152     }
153     ASSERT_NOT_REACHED();
154     return V8Node::createWrapper(impl, creationContext, isolate);
155 }
156 } // namespace WebCore