1 /*
2 * Copyright (C) 2007, 2009 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 "CDATASection.h"
31 #include "Comment.h"
32 #include "Document.h"
33 #include "DocumentFragment.h"
34 #include "DocumentType.h"
35 #include "Entity.h"
36 #include "EntityReference.h"
37 #include "HTMLElement.h"
38 #include "JSAttr.h"
39 #include "JSCDATASection.h"
40 #include "JSComment.h"
41 #include "JSDocument.h"
42 #include "JSDocumentFragment.h"
43 #include "JSDocumentType.h"
44 #include "JSEntity.h"
45 #include "JSEntityReference.h"
46 #include "JSEventListener.h"
47 #include "JSHTMLElement.h"
48 #include "JSHTMLElementWrapperFactory.h"
49 #include "JSNotation.h"
50 #include "JSProcessingInstruction.h"
51 #include "JSText.h"
52 #include "Node.h"
53 #include "Notation.h"
54 #include "ProcessingInstruction.h"
55 #include "RegisteredEventListener.h"
56 #include "Text.h"
57 #include <wtf/PassRefPtr.h>
58 #include <wtf/RefPtr.h>
59
60 #if ENABLE(SVG)
61 #include "JSSVGElementWrapperFactory.h"
62 #include "SVGElement.h"
63 #endif
64
65 using namespace JSC;
66
67 namespace WebCore {
68
69 typedef int ExpectionCode;
70
insertBefore(ExecState * exec,const ArgList & args)71 JSValue JSNode::insertBefore(ExecState* exec, const ArgList& args)
72 {
73 ExceptionCode ec = 0;
74 bool ok = impl()->insertBefore(toNode(args.at(0)), toNode(args.at(1)), ec, true);
75 setDOMException(exec, ec);
76 if (ok)
77 return args.at(0);
78 return jsNull();
79 }
80
replaceChild(ExecState * exec,const ArgList & args)81 JSValue JSNode::replaceChild(ExecState* exec, const ArgList& args)
82 {
83 ExceptionCode ec = 0;
84 bool ok = impl()->replaceChild(toNode(args.at(0)), toNode(args.at(1)), ec, true);
85 setDOMException(exec, ec);
86 if (ok)
87 return args.at(1);
88 return jsNull();
89 }
90
removeChild(ExecState * exec,const ArgList & args)91 JSValue JSNode::removeChild(ExecState* exec, const ArgList& args)
92 {
93 ExceptionCode ec = 0;
94 bool ok = impl()->removeChild(toNode(args.at(0)), ec);
95 setDOMException(exec, ec);
96 if (ok)
97 return args.at(0);
98 return jsNull();
99 }
100
appendChild(ExecState * exec,const ArgList & args)101 JSValue JSNode::appendChild(ExecState* exec, const ArgList& args)
102 {
103 ExceptionCode ec = 0;
104 bool ok = impl()->appendChild(toNode(args.at(0)), ec, true);
105 setDOMException(exec, ec);
106 if (ok)
107 return args.at(0);
108 return jsNull();
109 }
110
addEventListener(ExecState * exec,const ArgList & args)111 JSValue JSNode::addEventListener(ExecState* exec, const ArgList& args)
112 {
113 JSDOMGlobalObject* globalObject = toJSDOMGlobalObject(impl()->document());
114 if (!globalObject)
115 return jsUndefined();
116
117 if (RefPtr<JSEventListener> listener = globalObject->findOrCreateJSEventListener(args.at(1)))
118 impl()->addEventListener(args.at(0).toString(exec), listener.release(), args.at(2).toBoolean(exec));
119
120 return jsUndefined();
121 }
122
removeEventListener(ExecState * exec,const ArgList & args)123 JSValue JSNode::removeEventListener(ExecState* exec, const ArgList& args)
124 {
125 JSDOMGlobalObject* globalObject = toJSDOMGlobalObject(impl()->document());
126 if (!globalObject)
127 return jsUndefined();
128
129 if (JSEventListener* listener = globalObject->findJSEventListener(args.at(1)))
130 impl()->removeEventListener(args.at(0).toString(exec), listener, args.at(2).toBoolean(exec));
131
132 return jsUndefined();
133 }
134
pushEventHandlerScope(ExecState *,ScopeChain &) const135 void JSNode::pushEventHandlerScope(ExecState*, ScopeChain&) const
136 {
137 }
138
markChildren(MarkStack & markStack)139 void JSNode::markChildren(MarkStack& markStack)
140 {
141 Node* node = m_impl.get();
142
143 Base::markChildren(markStack);
144 markEventListeners(markStack, node->eventListeners());
145
146 // Nodes in the document are kept alive by JSDocument::mark, so, if we're in
147 // the document, we need to mark the document, but we don't need to explicitly
148 // mark any other nodes.
149 if (node->inDocument()) {
150 if (Document* doc = node->ownerDocument()) {
151 if (DOMObject* docWrapper = getCachedDOMObjectWrapper(*Heap::heap(this)->globalData(), doc))
152 markStack.append(docWrapper);
153 }
154 return;
155 }
156
157 // This is a node outside the document, so find the root of the tree it is in,
158 // and start marking from there.
159 Node* root = node;
160 for (Node* current = m_impl.get(); current; current = current->parentNode())
161 root = current;
162
163 // Nodes in a subtree are marked by the tree's root, so, if the root is already
164 // marking the tree, we don't need to explicitly mark any other nodes.
165 if (root->inSubtreeMark())
166 return;
167
168 // Mark the whole tree subtree.
169 root->setInSubtreeMark(true);
170 for (Node* nodeToMark = root; nodeToMark; nodeToMark = nodeToMark->traverseNextNode()) {
171 JSNode* wrapper = getCachedDOMNodeWrapper(m_impl->document(), nodeToMark);
172 if (wrapper)
173 markStack.append(wrapper);
174 }
175 root->setInSubtreeMark(false);
176 }
177
createWrapper(ExecState * exec,JSDOMGlobalObject * globalObject,Node * node)178 static ALWAYS_INLINE JSValue createWrapper(ExecState* exec, JSDOMGlobalObject* globalObject, Node* node)
179 {
180 ASSERT(node);
181 ASSERT(!getCachedDOMNodeWrapper(node->document(), node));
182
183 JSNode* wrapper;
184 switch (node->nodeType()) {
185 case Node::ELEMENT_NODE:
186 if (node->isHTMLElement())
187 wrapper = createJSHTMLWrapper(exec, globalObject, static_cast<HTMLElement*>(node));
188 #if ENABLE(SVG)
189 else if (node->isSVGElement())
190 wrapper = createJSSVGWrapper(exec, globalObject, static_cast<SVGElement*>(node));
191 #endif
192 else
193 wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, Element, node);
194 break;
195 case Node::ATTRIBUTE_NODE:
196 wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, Attr, node);
197 break;
198 case Node::TEXT_NODE:
199 wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, Text, node);
200 break;
201 case Node::CDATA_SECTION_NODE:
202 wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, CDATASection, node);
203 break;
204 case Node::ENTITY_NODE:
205 wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, Entity, node);
206 break;
207 case Node::PROCESSING_INSTRUCTION_NODE:
208 wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, ProcessingInstruction, node);
209 break;
210 case Node::COMMENT_NODE:
211 wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, Comment, node);
212 break;
213 case Node::DOCUMENT_NODE:
214 // we don't want to cache the document itself in the per-document dictionary
215 return toJS(exec, globalObject, static_cast<Document*>(node));
216 case Node::DOCUMENT_TYPE_NODE:
217 wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, DocumentType, node);
218 break;
219 case Node::NOTATION_NODE:
220 wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, Notation, node);
221 break;
222 case Node::DOCUMENT_FRAGMENT_NODE:
223 wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, DocumentFragment, node);
224 break;
225 case Node::ENTITY_REFERENCE_NODE:
226 wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, EntityReference, node);
227 break;
228 default:
229 wrapper = CREATE_DOM_NODE_WRAPPER(exec, globalObject, Node, node);
230 }
231
232 return wrapper;
233 }
234
toJSNewlyCreated(ExecState * exec,JSDOMGlobalObject * globalObject,Node * node)235 JSValue toJSNewlyCreated(ExecState* exec, JSDOMGlobalObject* globalObject, Node* node)
236 {
237 if (!node)
238 return jsNull();
239
240 return createWrapper(exec, globalObject, node);
241 }
242
toJS(ExecState * exec,JSDOMGlobalObject * globalObject,Node * node)243 JSValue toJS(ExecState* exec, JSDOMGlobalObject* globalObject, Node* node)
244 {
245 if (!node)
246 return jsNull();
247
248 JSNode* wrapper = getCachedDOMNodeWrapper(node->document(), node);
249 if (wrapper)
250 return wrapper;
251
252 return createWrapper(exec, globalObject, node);
253 }
254
255 } // namespace WebCore
256