f9232df334532ba127d3922150030e7d40d24c75
[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 "V8HTMLElement.h"
52 #include "V8Node.h"
53 #include "V8Notation.h"
54 #include "V8ProcessingInstruction.h"
55 #include "V8Proxy.h"
56 #include "V8Text.h"
57
58 #if ENABLE(SVG)
59 #include "V8SVGElement.h"
60 #endif
61
62 #include <wtf/RefPtr.h>
63
64 namespace WebCore {
65
66 // This function is customized to take advantage of the optional 4th argument: shouldLazyAttach
67 v8::Handle<v8::Value> V8Node::insertBeforeCallback(const v8::Arguments& args)
68 {
69     INC_STATS("DOM.Node.insertBefore");
70     v8::Handle<v8::Object> holder = args.Holder();
71     Node* imp = V8Node::toNative(holder);
72     ExceptionCode ec = 0;
73     Node* newChild = V8Node::HasInstance(args[0]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
74     Node* refChild = V8Node::HasInstance(args[1]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[1])) : 0;
75     bool success = imp->insertBefore(newChild, refChild, ec, true);
76     if (ec) {
77         V8Proxy::setDOMException(ec);
78         return v8::Handle<v8::Value>();
79     }
80     if (success)
81         return args[0];
82     return v8::Null();
83 }
84
85 // This function is customized to take advantage of the optional 4th argument: shouldLazyAttach
86 v8::Handle<v8::Value> V8Node::replaceChildCallback(const v8::Arguments& args)
87 {
88     INC_STATS("DOM.Node.replaceChild");
89     v8::Handle<v8::Object> holder = args.Holder();
90     Node* imp = V8Node::toNative(holder);
91     ExceptionCode ec = 0;
92     Node* newChild = V8Node::HasInstance(args[0]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
93     Node* oldChild = V8Node::HasInstance(args[1]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[1])) : 0;
94     bool success = imp->replaceChild(newChild, oldChild, ec, true);
95     if (ec) {
96         V8Proxy::setDOMException(ec);
97         return v8::Handle<v8::Value>();
98     }
99     if (success)
100         return args[1];
101     return v8::Null();
102 }
103
104 v8::Handle<v8::Value> V8Node::removeChildCallback(const v8::Arguments& args)
105 {
106     INC_STATS("DOM.Node.removeChild");
107     v8::Handle<v8::Object> holder = args.Holder();
108     Node* imp = V8Node::toNative(holder);
109     ExceptionCode ec = 0;
110     Node* oldChild = V8Node::HasInstance(args[0]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
111     bool success = imp->removeChild(oldChild, ec);
112     if (ec) {
113         V8Proxy::setDOMException(ec);
114         return v8::Handle<v8::Value>();
115     }
116     if (success)
117         return args[0];
118     return v8::Null();
119 }
120
121 // This function is customized to take advantage of the optional 4th argument: shouldLazyAttach
122 v8::Handle<v8::Value> V8Node::appendChildCallback(const v8::Arguments& args)
123 {
124     INC_STATS("DOM.Node.appendChild");
125     v8::Handle<v8::Object> holder = args.Holder();
126     Node* imp = V8Node::toNative(holder);
127     ExceptionCode ec = 0;
128     Node* newChild = V8Node::HasInstance(args[0]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
129     bool success = imp->appendChild(newChild, ec, true );
130     if (ec) {
131         V8Proxy::setDOMException(ec);
132         return v8::Handle<v8::Value>();
133     }
134     if (success)
135         return args[0];
136     return v8::Null();
137 }
138
139 v8::Handle<v8::Value> toV8Slow(Node* impl, v8::Isolate* isolate, bool forceNewObject)
140 {
141     if (!impl)
142         return v8::Null();
143
144     if (!forceNewObject) {
145         v8::Handle<v8::Value> wrapper = V8DOMWrapper::getCachedWrapper(impl);
146         if (!wrapper.IsEmpty())
147             return wrapper;
148     }
149     switch (impl->nodeType()) {
150     case Node::ELEMENT_NODE:
151         if (impl->isHTMLElement())
152             return toV8(toHTMLElement(impl), isolate, forceNewObject);
153 #if ENABLE(SVG)
154         if (impl->isSVGElement())
155             return toV8(static_cast<SVGElement*>(impl), isolate, forceNewObject);
156 #endif
157         return V8Element::wrap(static_cast<Element*>(impl), isolate, forceNewObject);
158     case Node::ATTRIBUTE_NODE:
159         return toV8(static_cast<Attr*>(impl), isolate, forceNewObject);
160     case Node::TEXT_NODE:
161         return toV8(toText(impl), isolate, forceNewObject);
162     case Node::CDATA_SECTION_NODE:
163         return toV8(static_cast<CDATASection*>(impl), isolate, forceNewObject);
164     case Node::ENTITY_REFERENCE_NODE:
165         return toV8(static_cast<EntityReference*>(impl), isolate, forceNewObject);
166     case Node::ENTITY_NODE:
167         return toV8(static_cast<Entity*>(impl), isolate, forceNewObject);
168     case Node::PROCESSING_INSTRUCTION_NODE:
169         return toV8(static_cast<ProcessingInstruction*>(impl), isolate, forceNewObject);
170     case Node::COMMENT_NODE:
171         return toV8(static_cast<Comment*>(impl), isolate, forceNewObject);
172     case Node::DOCUMENT_NODE:
173         return toV8(static_cast<Document*>(impl), isolate, forceNewObject);
174     case Node::DOCUMENT_TYPE_NODE:
175         return toV8(static_cast<DocumentType*>(impl), isolate, forceNewObject);
176     case Node::DOCUMENT_FRAGMENT_NODE:
177         return toV8(static_cast<DocumentFragment*>(impl), isolate, forceNewObject);
178     case Node::NOTATION_NODE:
179         return toV8(static_cast<Notation*>(impl), isolate, forceNewObject);
180     default: break; // XPATH_NAMESPACE_NODE
181     }
182     return V8Node::wrap(impl, isolate, forceNewObject);
183 }
184 } // namespace WebCore