2 * Copyright (C) 2007, 2009, 2010 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
30 #include "CachedImage.h"
31 #include "CachedScript.h"
32 #include "CDATASection.h"
35 #include "DocumentFragment.h"
36 #include "DocumentType.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"
50 #include "JSCDATASection.h"
51 #include "JSComment.h"
52 #include "JSDOMBinding.h"
53 #include "JSDocument.h"
54 #include "JSDocumentFragment.h"
55 #include "JSDocumentType.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"
66 #include "ProcessingInstruction.h"
67 #include "RegisteredEventListener.h"
68 #include "ShadowRoot.h"
69 #include "StyleSheet.h"
70 #include "StyledElement.h"
72 #include <wtf/PassRefPtr.h>
73 #include <wtf/RefPtr.h>
76 #include "JSSVGElementWrapperFactory.h"
77 #include "SVGElement.h"
84 using namespace HTMLNames;
86 static inline bool isObservable(JSNode* jsNode, Node* node)
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())
95 if (jsNode->hasCustomProperties())
98 // A node's JS wrapper is responsible for marking its JS event listeners.
99 if (node->hasEventListeners())
105 static inline bool isReachableFromDOM(JSNode* jsNode, Node* node, SlotVisitor& visitor)
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())
118 else if (node->hasTagName(audioTag)) {
119 if (!static_cast<HTMLAudioElement*>(node)->paused())
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())
130 return isObservable(jsNode, node) && visitor.containsOpaqueRoot(root(node));
133 bool JSNodeOwner::isReachableFromOpaqueRoots(JSC::Handle<JSC::Unknown> handle, void*, SlotVisitor& visitor)
135 JSNode* jsNode = static_cast<JSNode*>(handle.get().asCell());
136 return isReachableFromDOM(jsNode, jsNode->impl(), visitor);
139 void JSNodeOwner::finalize(JSC::Handle<JSC::Unknown> handle, void* context)
141 JSNode* jsNode = static_cast<JSNode*>(handle.get().asCell());
142 DOMWrapperWorld* world = static_cast<DOMWrapperWorld*>(context);
143 uncacheWrapper(world, jsNode->impl(), jsNode);
146 JSValue JSNode::insertBefore(ExecState* exec)
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);
153 return exec->argument(0);
157 JSValue JSNode::replaceChild(ExecState* exec)
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);
164 return exec->argument(1);
168 JSValue JSNode::removeChild(ExecState* exec)
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);
175 return exec->argument(0);
179 JSValue JSNode::appendChild(ExecState* exec)
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);
186 return exec->argument(0);
190 ScopeChainNode* JSNode::pushEventHandlerScope(ExecState*, ScopeChainNode* node) const
195 void JSNode::visitChildren(SlotVisitor& visitor)
197 ASSERT_GC_OBJECT_INHERITS(this, &s_info);
198 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
199 ASSERT(structure()->typeInfo().overridesVisitChildren());
200 Base::visitChildren(visitor);
202 Node* node = m_impl.get();
203 node->visitJSEventListeners(visitor);
205 visitor.addOpaqueRoot(root(node));
208 static ALWAYS_INLINE JSValue createWrapperInline(ExecState* exec, JSDOMGlobalObject* globalObject, Node* node)
211 ASSERT(!getCachedWrapper(currentWorld(exec), node));
213 JSDOMWrapper* wrapper;
214 switch (node->nodeType()) {
215 case Node::ELEMENT_NODE:
216 if (node->isHTMLElement())
217 wrapper = createJSHTMLWrapper(exec, globalObject, toHTMLElement(node));
219 else if (node->isSVGElement())
220 wrapper = createJSSVGWrapper(exec, globalObject, static_cast<SVGElement*>(node));
223 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Element, node);
225 case Node::ATTRIBUTE_NODE:
226 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Attr, node);
228 case Node::TEXT_NODE:
229 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Text, node);
231 case Node::CDATA_SECTION_NODE:
232 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, CDATASection, node);
234 case Node::ENTITY_NODE:
235 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Entity, node);
237 case Node::PROCESSING_INSTRUCTION_NODE:
238 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, ProcessingInstruction, node);
240 case Node::COMMENT_NODE:
241 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Comment, node);
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);
249 case Node::NOTATION_NODE:
250 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Notation, node);
252 case Node::DOCUMENT_FRAGMENT_NODE:
253 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, DocumentFragment, node);
255 case Node::SHADOW_ROOT_NODE:
256 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Node, node);
258 case Node::ENTITY_REFERENCE_NODE:
259 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, EntityReference, node);
262 wrapper = CREATE_DOM_WRAPPER(exec, globalObject, Node, node);
268 JSValue createWrapper(ExecState* exec, JSDOMGlobalObject* globalObject, Node* node)
270 return createWrapperInline(exec, globalObject, node);
273 JSValue toJSNewlyCreated(ExecState* exec, JSDOMGlobalObject* globalObject, Node* node)
278 return createWrapperInline(exec, globalObject, node);
281 } // namespace WebCore