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