2011-05-03 Andrey Kosyakov <caseq@chromium.org>
[WebKit-https.git] / Source / WebCore / 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 "Node.h"
33
34 #include "Document.h"
35 #include "EventListener.h"
36 #include "ShadowRoot.h"
37
38 #include "V8AbstractEventListener.h"
39 #include "V8Attr.h"
40 #include "V8Binding.h"
41 #include "V8BindingState.h"
42 #include "V8CDATASection.h"
43 #include "V8Comment.h"
44 #include "V8Document.h"
45 #include "V8DocumentFragment.h"
46 #include "V8DocumentType.h"
47 #include "V8Element.h"
48 #include "V8Entity.h"
49 #include "V8EntityReference.h"
50 #include "V8EventListener.h"
51 #include "V8Node.h"
52 #include "V8Notation.h"
53 #include "V8ProcessingInstruction.h"
54 #include "V8Proxy.h"
55 #include "V8Text.h"
56
57 #include <wtf/RefPtr.h>
58
59 namespace WebCore {
60
61 // This function is customized to take advantage of the optional 4th argument: shouldLazyAttach
62 v8::Handle<v8::Value> V8Node::insertBeforeCallback(const v8::Arguments& args)
63 {
64     INC_STATS("DOM.Node.insertBefore");
65     v8::Handle<v8::Object> holder = args.Holder();
66     Node* imp = V8Node::toNative(holder);
67     ExceptionCode ec = 0;
68     Node* newChild = V8Node::HasInstance(args[0]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
69     Node* refChild = V8Node::HasInstance(args[1]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[1])) : 0;
70     bool success = imp->insertBefore(newChild, refChild, ec, true);
71     if (ec) {
72         V8Proxy::setDOMException(ec);
73         return v8::Handle<v8::Value>();
74     }
75     if (success)
76         return args[0];
77     return v8::Null();
78 }
79
80 // This function is customized to take advantage of the optional 4th argument: shouldLazyAttach
81 v8::Handle<v8::Value> V8Node::replaceChildCallback(const v8::Arguments& args)
82 {
83     INC_STATS("DOM.Node.replaceChild");
84     v8::Handle<v8::Object> holder = args.Holder();
85     Node* imp = V8Node::toNative(holder);
86     ExceptionCode ec = 0;
87     Node* newChild = V8Node::HasInstance(args[0]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
88     Node* oldChild = V8Node::HasInstance(args[1]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[1])) : 0;
89     bool success = imp->replaceChild(newChild, oldChild, ec, true);
90     if (ec) {
91         V8Proxy::setDOMException(ec);
92         return v8::Handle<v8::Value>();
93     }
94     if (success)
95         return args[1];
96     return v8::Null();
97 }
98
99 v8::Handle<v8::Value> V8Node::removeChildCallback(const v8::Arguments& args)
100 {
101     INC_STATS("DOM.Node.removeChild");
102     v8::Handle<v8::Object> holder = args.Holder();
103     Node* imp = V8Node::toNative(holder);
104     ExceptionCode ec = 0;
105     Node* oldChild = V8Node::HasInstance(args[0]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
106     bool success = imp->removeChild(oldChild, ec);
107     if (ec) {
108         V8Proxy::setDOMException(ec);
109         return v8::Handle<v8::Value>();
110     }
111     if (success)
112         return args[0];
113     return v8::Null();
114 }
115
116 // This function is customized to take advantage of the optional 4th argument: shouldLazyAttach
117 v8::Handle<v8::Value> V8Node::appendChildCallback(const v8::Arguments& args)
118 {
119     INC_STATS("DOM.Node.appendChild");
120     v8::Handle<v8::Object> holder = args.Holder();
121     Node* imp = V8Node::toNative(holder);
122     ExceptionCode ec = 0;
123     Node* newChild = V8Node::HasInstance(args[0]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
124     bool success = imp->appendChild(newChild, ec, true );
125     if (ec) {
126         V8Proxy::setDOMException(ec);
127         return v8::Handle<v8::Value>();
128     }
129     if (success)
130         return args[0];
131     return v8::Null();
132 }
133
134 v8::Handle<v8::Value> toV8Slow(Node* impl, bool forceNewObject)
135 {
136     if (!impl)
137         return v8::Null();
138
139     if (!forceNewObject) {
140         v8::Handle<v8::Value> wrapper = V8DOMWrapper::getWrapper(impl);
141         if (!wrapper.IsEmpty())
142             return wrapper;
143     }
144     switch (impl->nodeType()) {
145     case Node::ELEMENT_NODE:
146         return toV8(static_cast<Element*>(impl), forceNewObject);
147     case Node::ATTRIBUTE_NODE:
148         return toV8(static_cast<Attr*>(impl), forceNewObject);
149     case Node::TEXT_NODE:
150         return toV8(static_cast<Text*>(impl), forceNewObject);
151     case Node::CDATA_SECTION_NODE:
152         return toV8(static_cast<CDATASection*>(impl), forceNewObject);
153     case Node::ENTITY_REFERENCE_NODE:
154         return toV8(static_cast<EntityReference*>(impl), forceNewObject);
155     case Node::ENTITY_NODE:
156         return toV8(static_cast<Entity*>(impl), forceNewObject);
157     case Node::PROCESSING_INSTRUCTION_NODE:
158         return toV8(static_cast<ProcessingInstruction*>(impl), forceNewObject);
159     case Node::COMMENT_NODE:
160         return toV8(static_cast<Comment*>(impl), forceNewObject);
161     case Node::DOCUMENT_NODE:
162         return toV8(static_cast<Document*>(impl), forceNewObject);
163     case Node::DOCUMENT_TYPE_NODE:
164         return toV8(static_cast<DocumentType*>(impl), forceNewObject);
165     case Node::DOCUMENT_FRAGMENT_NODE:
166         return toV8(static_cast<DocumentFragment*>(impl), forceNewObject);
167     case Node::NOTATION_NODE:
168         return toV8(static_cast<Notation*>(impl), forceNewObject);
169     case Node::SHADOW_ROOT_NODE: // There's no IDL class for ShadowRoot, fall-through to default and use Node instead.
170     default: break; // XPATH_NAMESPACE_NODE
171     }
172     return V8Node::wrap(impl, forceNewObject);
173 }
174 } // namespace WebCore