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 "core/dom/Node.h"
33
34#include "V8Attr.h"
35#include "V8CDATASection.h"
36#include "V8Comment.h"
37#include "V8Document.h"
38#include "V8DocumentFragment.h"
39#include "V8DocumentType.h"
40#include "V8Element.h"
41#include "V8Entity.h"
42#include "V8HTMLElement.h"
43#include "V8Node.h"
44#include "V8Notation.h"
45#include "V8ProcessingInstruction.h"
46#include "V8SVGElement.h"
47#include "V8ShadowRoot.h"
48#include "V8Text.h"
49#include "bindings/v8/ExceptionState.h"
50#include "bindings/v8/V8AbstractEventListener.h"
51#include "bindings/v8/V8Binding.h"
52#include "bindings/v8/V8EventListener.h"
53#include "core/dom/CustomElementCallbackDispatcher.h"
54#include "core/dom/Document.h"
55#include "core/dom/EventListener.h"
56#include "core/dom/shadow/ShadowRoot.h"
57#include "wtf/RefPtr.h"
58
59namespace WebCore {
60
61// This function is customized to take advantage of the optional 4th argument: AttachBehavior
62void V8Node::insertBeforeMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args)
63{
64    v8::Handle<v8::Object> holder = args.Holder();
65    Node* imp = V8Node::toNative(holder);
66
67    CustomElementCallbackDispatcher::CallbackDeliveryScope deliveryScope;
68
69    ExceptionState es(args.GetIsolate());
70    Node* newChild = V8Node::HasInstance(args[0], args.GetIsolate(), worldType(args.GetIsolate())) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
71    Node* refChild = V8Node::HasInstance(args[1], args.GetIsolate(), worldType(args.GetIsolate())) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[1])) : 0;
72    imp->insertBefore(newChild, refChild, es, AttachLazily);
73    if (es.throwIfNeeded())
74        return;
75    v8SetReturnValue(args, args[0]);
76}
77
78// This function is customized to take advantage of the optional 4th argument: AttachBehavior
79void V8Node::replaceChildMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args)
80{
81    v8::Handle<v8::Object> holder = args.Holder();
82    Node* imp = V8Node::toNative(holder);
83
84    CustomElementCallbackDispatcher::CallbackDeliveryScope deliveryScope;
85
86    ExceptionState es(args.GetIsolate());
87    Node* newChild = V8Node::HasInstance(args[0], args.GetIsolate(), worldType(args.GetIsolate())) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
88    Node* oldChild = V8Node::HasInstance(args[1], args.GetIsolate(), worldType(args.GetIsolate())) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[1])) : 0;
89    imp->replaceChild(newChild, oldChild, es, AttachLazily);
90    if (es.throwIfNeeded())
91        return;
92    v8SetReturnValue(args, args[1]);
93}
94
95void V8Node::removeChildMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args)
96{
97    v8::Handle<v8::Object> holder = args.Holder();
98    Node* imp = V8Node::toNative(holder);
99
100    CustomElementCallbackDispatcher::CallbackDeliveryScope deliveryScope;
101
102    ExceptionState es(args.GetIsolate());
103    Node* oldChild = V8Node::HasInstance(args[0], args.GetIsolate(), worldType(args.GetIsolate())) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
104    imp->removeChild(oldChild, es);
105    if (es.throwIfNeeded())
106        return;
107    v8SetReturnValue(args, args[0]);
108}
109
110// This function is customized to take advantage of the optional 4th argument: AttachBehavior
111void V8Node::appendChildMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args)
112{
113    v8::Handle<v8::Object> holder = args.Holder();
114    Node* imp = V8Node::toNative(holder);
115
116    CustomElementCallbackDispatcher::CallbackDeliveryScope deliveryScope;
117
118    ExceptionState es(args.GetIsolate());
119    Node* newChild = V8Node::HasInstance(args[0], args.GetIsolate(), worldType(args.GetIsolate())) ? V8Node::toNative(v8::Handle<v8::Object>::Cast(args[0])) : 0;
120    imp->appendChild(newChild, es, AttachLazily);
121    if (es.throwIfNeeded())
122        return;
123    v8SetReturnValue(args, args[0]);
124}
125
126v8::Handle<v8::Object> wrap(Node* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate)
127{
128    ASSERT(impl);
129    switch (impl->nodeType()) {
130    case Node::ELEMENT_NODE:
131        // For performance reasons, this is inlined from V8Element::wrap and must remain in sync.
132        if (impl->isHTMLElement())
133            return wrap(toHTMLElement(impl), creationContext, isolate);
134        if (impl->isSVGElement())
135            return wrap(toSVGElement(impl), creationContext, isolate);
136        return V8Element::createWrapper(toElement(impl), creationContext, isolate);
137    case Node::ATTRIBUTE_NODE:
138        return wrap(toAttr(impl), creationContext, isolate);
139    case Node::TEXT_NODE:
140        return wrap(toText(impl), creationContext, isolate);
141    case Node::CDATA_SECTION_NODE:
142        return wrap(static_cast<CDATASection*>(impl), creationContext, isolate);
143    case Node::ENTITY_NODE:
144        return wrap(static_cast<Entity*>(impl), creationContext, isolate);
145    case Node::PROCESSING_INSTRUCTION_NODE:
146        return wrap(static_cast<ProcessingInstruction*>(impl), creationContext, isolate);
147    case Node::COMMENT_NODE:
148        return wrap(static_cast<Comment*>(impl), creationContext, isolate);
149    case Node::DOCUMENT_NODE:
150        return wrap(toDocument(impl), creationContext, isolate);
151    case Node::DOCUMENT_TYPE_NODE:
152        return wrap(static_cast<DocumentType*>(impl), creationContext, isolate);
153    case Node::DOCUMENT_FRAGMENT_NODE:
154        if (impl->isShadowRoot())
155            return wrap(toShadowRoot(impl), creationContext, isolate);
156        return wrap(static_cast<DocumentFragment*>(impl), creationContext, isolate);
157    case Node::NOTATION_NODE:
158        return wrap(static_cast<Notation*>(impl), creationContext, isolate);
159    default:
160        break; // ENTITY_REFERENCE_NODE or XPATH_NAMESPACE_NODE
161    }
162    return V8Node::createWrapper(impl, creationContext, isolate);
163}
164} // namespace WebCore
165