initial import
[vuplus_webkit] / Source / WebCore / bindings / js / JSNodeCustom.cpp
1 /*
2  * Copyright (C) 2007, 2009, 2010 Apple 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
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
24  */
25
26 #include "config.h"
27 #include "JSNode.h"
28
29 #include "Attr.h"
30 #include "CachedImage.h"
31 #include "CachedScript.h"
32 #include "CDATASection.h"
33 #include "Comment.h"
34 #include "Document.h"
35 #include "DocumentFragment.h"
36 #include "DocumentType.h"
37 #include "Entity.h"
38 #include "EntityReference.h"
39 #include "ExceptionCode.h"
40 #include "HTMLAudioElement.h"
41 #include "HTMLCanvasElement.h"
42 #include "HTMLElement.h"
43 #include "HTMLFrameElementBase.h"
44 #include "HTMLImageElement.h"
45 #include "HTMLLinkElement.h"
46 #include "HTMLNames.h"
47 #include "HTMLScriptElement.h"
48 #include "HTMLStyleElement.h"
49 #include "JSAttr.h"
50 #include "JSCDATASection.h"
51 #include "JSComment.h"
52 #include "JSDOMBinding.h"
53 #include "JSDocument.h"
54 #include "JSDocumentFragment.h"
55 #include "JSDocumentType.h"
56 #include "JSEntity.h"
57 #include "JSEntityReference.h"
58 #include "JSEventListener.h"
59 #include "JSHTMLElement.h"
60 #include "JSHTMLElementWrapperFactory.h"
61 #include "JSNotation.h"
62 #include "JSProcessingInstruction.h"
63 #include "JSText.h"
64 #include "Node.h"
65 #include "Notation.h"
66 #include "ProcessingInstruction.h"
67 #include "RegisteredEventListener.h"
68 #include "ShadowRoot.h"
69 #include "StyleSheet.h"
70 #include "StyledElement.h"
71 #include "Text.h"
72 #include <wtf/PassRefPtr.h>
73 #include <wtf/RefPtr.h>
74
75 #if ENABLE(SVG)
76 #include "JSSVGElementWrapperFactory.h"
77 #include "SVGElement.h"
78 #endif
79
80 using namespace JSC;
81
82 namespace WebCore {
83
84 using namespace HTMLNames;
85
86 static inline bool isObservable(JSNode* jsNode, Node* node)
87 {
88     // The DOM doesn't know how to keep a tree of nodes alive without the root
89     // being explicitly referenced. So, we artificially treat the root of
90     // every tree as observable.
91     // FIXME: Resolve this lifetime issue in the DOM, and remove this.
92     if (!node->parentNode())
93         return true;
94
95     if (jsNode->hasCustomProperties())
96         return true;
97
98     // A node's JS wrapper is responsible for marking its JS event listeners.
99     if (node->hasEventListeners())
100         return true;
101
102     return false;
103 }
104
105 static inline bool isReachableFromDOM(JSNode* jsNode, Node* node, SlotVisitor& visitor)
106 {
107     if (!node->inDocument()) {
108         // If a wrapper is the last reference to an image element
109         // that is loading but not in the document, the wrapper is observable
110         // because it is the only thing keeping the image element alive, and if
111         // the element is destroyed, its load event will not fire.
112         // FIXME: The DOM should manage this issue without the help of JavaScript wrappers.
113         if (node->hasTagName(imgTag)) {
114             if (static_cast<HTMLImageElement*>(node)->hasPendingActivity())
115                 return true;
116         }
117     #if ENABLE(VIDEO)
118         else if (node->hasTagName(audioTag)) {
119             if (!static_cast<HTMLAudioElement*>(node)->paused())
120                 return true;
121         }
122     #endif
123
124         // If a node is firing event listeners, its wrapper is observable because
125         // its wrapper is responsible for marking those event listeners.
126         if (node->isFiringEventListeners())
127             return true;
128     }
129
130     return isObservable(jsNode, node) && visitor.containsOpaqueRoot(root(node));
131 }
132
133 bool JSNodeOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, SlotVisitor& visitor)
134 {
135     JSNode* jsNode = static_cast<JSNode*>(handle.get().asCell());
136     return isReachableFromDOM(jsNode, jsNode->impl(), visitor);
137 }
138
139 void JSNodeOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)
140 {
141     JSNode* jsNode = static_cast<JSNode*>(handle.get().asCell());
142     DOMWrapperWorld* world = static_cast<DOMWrapperWorld*>(context);
143     uncacheWrapper(world, jsNode->impl(), jsNode);
144 }
145
146 JSValue JSNode::insertBefore(ExecState* exec)
147 {
148     Node* imp = static_cast<Node*>(impl());
149     ExceptionCode ec = 0;
150     bool ok = imp->insertBefore(toNode(exec->argument(0)), toNode(exec->argument(1)), ec, true);
151     setDOMException(exec, ec);
152     if (ok)
153         return exec->argument(0);
154     return jsNull();
155 }
156
157 JSValue JSNode::replaceChild(ExecState* exec)
158 {
159     Node* imp = static_cast<Node*>(impl());
160     ExceptionCode ec = 0;
161     bool ok = imp->replaceChild(toNode(exec->argument(0)), toNode(exec->argument(1)), ec, true);
162     setDOMException(exec, ec);
163     if (ok)
164         return exec->argument(1);
165     return jsNull();
166 }
167
168 JSValue JSNode::removeChild(ExecState* exec)
169 {
170     Node* imp = static_cast<Node*>(impl());
171     ExceptionCode ec = 0;
172     bool ok = imp->removeChild(toNode(exec->argument(0)), ec);
173     setDOMException(exec, ec);
174     if (ok)
175         return exec->argument(0);
176     return jsNull();
177 }
178
179 JSValue JSNode::appendChild(ExecState* exec)
180 {
181     Node* imp = static_cast<Node*>(impl());
182     ExceptionCode ec = 0;
183     bool ok = imp->appendChild(toNode(exec->argument(0)), ec, true);
184     setDOMException(exec, ec);
185     if (ok)
186         return exec->argument(0);
187     return jsNull();
188 }
189
190 ScopeChainNode* JSNode::pushEventHandlerScope(ExecState*, ScopeChainNode* node) const
191 {
192     return node;
193 }
194
195 void JSNode::visitChildren(SlotVisitor& visitor)
196 {
197     ASSERT_GC_OBJECT_INHERITS(this, &s_info);
198     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
199     ASSERT(structure()->typeInfo().overridesVisitChildren());
200     Base::visitChildren(visitor);
201
202     Node* node = m_impl.get();
203     node->visitJSEventListeners(visitor);
204
205     visitor.addOpaqueRoot(root(node));
206 }
207
208 static ALWAYS_INLINE JSValue createWrapperInline(ExecState* exec, JSDOMGlobalObject* globalObject, Node* node)
209 {
210     ASSERT(node);
211     ASSERT(!getCachedWrapper(currentWorld(exec), node));
212     
213     JSDOMWrapper* wrapper;    
214     switch (node->nodeType()) {
215         case Node::ELEMENT_NODE:
216             if (node->isHTMLElement())
217                 wrapper = createJSHTMLWrapper(exec, globalObject, toHTMLElement(node));
218 #if ENABLE(SVG)
219             else if (node->isSVGElement())
220                 wrapper = createJSSVGWrapper(exec, globalObject, static_cast<SVGElement*>(node));
221 #endif
222             else
223                 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Element, node);
224             break;
225         case Node::ATTRIBUTE_NODE:
226             wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Attr, node);
227             break;
228         case Node::TEXT_NODE:
229             wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Text, node);
230             break;
231         case Node::CDATA_SECTION_NODE:
232             wrapper = CREATE_DOM_WRAPPER(exec, globalObject, CDATASection, node);
233             break;
234         case Node::ENTITY_NODE:
235             wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Entity, node);
236             break;
237         case Node::PROCESSING_INSTRUCTION_NODE:
238             wrapper = CREATE_DOM_WRAPPER(exec, globalObject, ProcessingInstruction, node);
239             break;
240         case Node::COMMENT_NODE:
241             wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Comment, node);
242             break;
243         case Node::DOCUMENT_NODE:
244             // we don't want to cache the document itself in the per-document dictionary
245             return toJS(exec, globalObject, static_cast<Document*>(node));
246         case Node::DOCUMENT_TYPE_NODE:
247             wrapper = CREATE_DOM_WRAPPER(exec, globalObject, DocumentType, node);
248             break;
249         case Node::NOTATION_NODE:
250             wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Notation, node);
251             break;
252         case Node::DOCUMENT_FRAGMENT_NODE:
253             wrapper = CREATE_DOM_WRAPPER(exec, globalObject, DocumentFragment, node);
254             break;
255         case Node::SHADOW_ROOT_NODE:
256             wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Node, node);
257             break;
258         case Node::ENTITY_REFERENCE_NODE:
259             wrapper = CREATE_DOM_WRAPPER(exec, globalObject, EntityReference, node);
260             break;
261         default:
262             wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Node, node);
263     }
264
265     return wrapper;    
266 }
267
268 JSValue createWrapper(ExecState* exec, JSDOMGlobalObject* globalObject, Node* node)
269 {
270     return createWrapperInline(exec, globalObject, node);
271 }
272     
273 JSValue toJSNewlyCreated(ExecState* exec, JSDOMGlobalObject* globalObject, Node* node)
274 {
275     if (!node)
276         return jsNull();
277     
278     return createWrapperInline(exec, globalObject, node);
279 }
280
281 } // namespace WebCore