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
37#include "V8AbstractEventListener.h"
38#include "V8Attr.h"
39#include "V8Binding.h"
40#include "V8CDATASection.h"
41#include "V8Comment.h"
42#include "V8CustomEventListener.h"
43#include "V8Document.h"
44#include "V8DocumentFragment.h"
45#include "V8DocumentType.h"
46#include "V8Element.h"
47#include "V8Entity.h"
48#include "V8EntityReference.h"
49#include "V8Node.h"
50#include "V8Notation.h"
51#include "V8ProcessingInstruction.h"
52#include "V8Proxy.h"
53#include "V8Text.h"
54
55#include <wtf/RefPtr.h>
56
57namespace WebCore {
58
59v8::Handle<v8::Value> V8Node::addEventListenerCallback(const v8::Arguments& args)
60{
61    INC_STATS("DOM.Node.addEventListener()");
62    Node* node = V8Node::toNative(args.Holder());
63
64    RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(node, args[1], false, ListenerFindOrCreate);
65    if (listener) {
66        String type = toWebCoreString(args[0]);
67        bool useCapture = args[2]->BooleanValue();
68        node->addEventListener(type, listener, useCapture);
69        createHiddenDependency(args.Holder(), args[1], cacheIndex);
70    }
71    return v8::Undefined();
72}
73
74v8::Handle<v8::Value> V8Node::removeEventListenerCallback(const v8::Arguments& args)
75{
76    INC_STATS("DOM.Node.removeEventListener()");
77    Node* node = V8Node::toNative(args.Holder());
78
79    // It is possbile that the owner document of the node is detached
80    // from the frame.
81    // See issue http://b/878909
82    RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(node, args[1], false, ListenerFindOnly);
83    if (listener) {
84        AtomicString type = v8ValueToAtomicWebCoreString(args[0]);
85        bool useCapture = args[2]->BooleanValue();
86        node->removeEventListener(type, listener.get(), useCapture);
87        removeHiddenDependency(args.Holder(), args[1], cacheIndex);
88    }
89
90    return v8::Undefined();
91}
92
93// This function is customized to take advantage of the optional 4th argument: shouldLazyAttach
94v8::Handle<v8::Value> V8Node::insertBeforeCallback(const v8::Arguments& args)
95{
96    INC_STATS("DOM.Node.insertBefore");
97    v8::Handle<v8::Object> holder = args.Holder();
98    Node* imp = V8Node::toNative(holder);
99    ExceptionCode ec = 0;
100    Node* newChild = V8Node::HasInstance(args[0]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
101    Node* refChild = V8Node::HasInstance(args[1]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[1])) : 0;
102    bool success = imp->insertBefore(newChild, refChild, ec, true);
103    if (ec) {
104        V8Proxy::setDOMException(ec);
105        return v8::Handle<v8::Value>();
106    }
107    if (success)
108        return args[0];
109    return v8::Null();
110}
111
112// This function is customized to take advantage of the optional 4th argument: shouldLazyAttach
113v8::Handle<v8::Value> V8Node::replaceChildCallback(const v8::Arguments& args)
114{
115    INC_STATS("DOM.Node.replaceChild");
116    v8::Handle<v8::Object> holder = args.Holder();
117    Node* imp = V8Node::toNative(holder);
118    ExceptionCode ec = 0;
119    Node* newChild = V8Node::HasInstance(args[0]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
120    Node* oldChild = V8Node::HasInstance(args[1]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[1])) : 0;
121    bool success = imp->replaceChild(newChild, oldChild, ec, true);
122    if (ec) {
123        V8Proxy::setDOMException(ec);
124        return v8::Handle<v8::Value>();
125    }
126    if (success)
127        return args[1];
128    return v8::Null();
129}
130
131v8::Handle<v8::Value> V8Node::removeChildCallback(const v8::Arguments& args)
132{
133    INC_STATS("DOM.Node.removeChild");
134    v8::Handle<v8::Object> holder = args.Holder();
135    Node* imp = V8Node::toNative(holder);
136    ExceptionCode ec = 0;
137    Node* oldChild = V8Node::HasInstance(args[0]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
138    bool success = imp->removeChild(oldChild, ec);
139    if (ec) {
140        V8Proxy::setDOMException(ec);
141        return v8::Handle<v8::Value>();
142    }
143    if (success)
144        return args[0];
145    return v8::Null();
146}
147
148// This function is customized to take advantage of the optional 4th argument: shouldLazyAttach
149v8::Handle<v8::Value> V8Node::appendChildCallback(const v8::Arguments& args)
150{
151    INC_STATS("DOM.Node.appendChild");
152    v8::Handle<v8::Object> holder = args.Holder();
153    Node* imp = V8Node::toNative(holder);
154    ExceptionCode ec = 0;
155    Node* newChild = V8Node::HasInstance(args[0]) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
156    bool success = imp->appendChild(newChild, ec, true );
157    if (ec) {
158        V8Proxy::setDOMException(ec);
159        return v8::Handle<v8::Value>();
160    }
161    if (success)
162        return args[0];
163    return v8::Null();
164}
165
166v8::Handle<v8::Value> toV8(Node* impl, bool forceNewObject)
167{
168    if (!impl)
169        return v8::Null();
170
171    if (!forceNewObject) {
172        v8::Handle<v8::Value> wrapper = V8DOMWrapper::getWrapper(impl);
173        if (!wrapper.IsEmpty())
174            return wrapper;
175    }
176    switch (impl->nodeType()) {
177    case Node::ELEMENT_NODE:
178        return toV8(static_cast<Element*>(impl), forceNewObject);
179    case Node::ATTRIBUTE_NODE:
180        return toV8(static_cast<Attr*>(impl), forceNewObject);
181    case Node::TEXT_NODE:
182        return toV8(static_cast<Text*>(impl), forceNewObject);
183    case Node::CDATA_SECTION_NODE:
184        return toV8(static_cast<CDATASection*>(impl), forceNewObject);
185    case Node::ENTITY_REFERENCE_NODE:
186        return toV8(static_cast<EntityReference*>(impl), forceNewObject);
187    case Node::ENTITY_NODE:
188        return toV8(static_cast<Entity*>(impl), forceNewObject);
189    case Node::PROCESSING_INSTRUCTION_NODE:
190        return toV8(static_cast<ProcessingInstruction*>(impl), forceNewObject);
191    case Node::COMMENT_NODE:
192        return toV8(static_cast<Comment*>(impl), forceNewObject);
193    case Node::DOCUMENT_NODE:
194        return toV8(static_cast<Document*>(impl), forceNewObject);
195    case Node::DOCUMENT_TYPE_NODE:
196        return toV8(static_cast<DocumentType*>(impl), forceNewObject);
197    case Node::DOCUMENT_FRAGMENT_NODE:
198        return toV8(static_cast<DocumentFragment*>(impl), forceNewObject);
199    case Node::NOTATION_NODE:
200        return toV8(static_cast<Notation*>(impl), forceNewObject);
201    default: break; // XPATH_NAMESPACE_NODE
202    }
203    return V8Node::wrap(impl, forceNewObject);
204}
205} // namespace WebCore
206