]>
Commit | Line | Data |
---|---|---|
b37bf2e1 A |
1 | /* |
2 | * Copyright (C) 2006 Apple Computer, 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 "JSNode.h" | |
27 | #include "JSNodeList.h" | |
28 | #include "JSObjectRef.h" | |
29 | #include "JSStringRef.h" | |
30 | #include "JSValueRef.h" | |
31 | #include "Node.h" | |
32 | #include "NodeList.h" | |
b37bf2e1 A |
33 | #include <wtf/Assertions.h> |
34 | ||
35 | static JSValueRef JSNode_appendChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
36 | { | |
37 | UNUSED_PARAM(function); | |
38 | ||
9dae56ea | 39 | /* Example of throwing a type error for invalid values */ |
b37bf2e1 A |
40 | if (!JSValueIsObjectOfClass(context, thisObject, JSNode_class(context))) { |
41 | JSStringRef message = JSStringCreateWithUTF8CString("TypeError: appendChild can only be called on nodes"); | |
42 | *exception = JSValueMakeString(context, message); | |
43 | JSStringRelease(message); | |
44 | } else if (argumentCount < 1 || !JSValueIsObjectOfClass(context, arguments[0], JSNode_class(context))) { | |
45 | JSStringRef message = JSStringCreateWithUTF8CString("TypeError: first argument to appendChild must be a node"); | |
46 | *exception = JSValueMakeString(context, message); | |
47 | JSStringRelease(message); | |
48 | } else { | |
49 | Node* node = JSObjectGetPrivate(thisObject); | |
50 | Node* child = JSObjectGetPrivate(JSValueToObject(context, arguments[0], NULL)); | |
51 | ||
52 | Node_appendChild(node, child); | |
53 | } | |
54 | ||
55 | return JSValueMakeUndefined(context); | |
56 | } | |
57 | ||
58 | static JSValueRef JSNode_removeChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
59 | { | |
60 | UNUSED_PARAM(function); | |
9dae56ea A |
61 | |
62 | /* Example of ignoring invalid values */ | |
b37bf2e1 A |
63 | if (argumentCount > 0) { |
64 | if (JSValueIsObjectOfClass(context, thisObject, JSNode_class(context))) { | |
65 | if (JSValueIsObjectOfClass(context, arguments[0], JSNode_class(context))) { | |
66 | Node* node = JSObjectGetPrivate(thisObject); | |
67 | Node* child = JSObjectGetPrivate(JSValueToObject(context, arguments[0], exception)); | |
68 | ||
69 | Node_removeChild(node, child); | |
70 | } | |
71 | } | |
72 | } | |
73 | ||
74 | return JSValueMakeUndefined(context); | |
75 | } | |
76 | ||
77 | static JSValueRef JSNode_replaceChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
78 | { | |
79 | UNUSED_PARAM(function); | |
80 | ||
81 | if (argumentCount > 1) { | |
82 | if (JSValueIsObjectOfClass(context, thisObject, JSNode_class(context))) { | |
83 | if (JSValueIsObjectOfClass(context, arguments[0], JSNode_class(context))) { | |
84 | if (JSValueIsObjectOfClass(context, arguments[1], JSNode_class(context))) { | |
85 | Node* node = JSObjectGetPrivate(thisObject); | |
86 | Node* newChild = JSObjectGetPrivate(JSValueToObject(context, arguments[0], exception)); | |
87 | Node* oldChild = JSObjectGetPrivate(JSValueToObject(context, arguments[1], exception)); | |
88 | ||
89 | Node_replaceChild(node, newChild, oldChild); | |
90 | } | |
91 | } | |
92 | } | |
93 | } | |
94 | ||
95 | return JSValueMakeUndefined(context); | |
96 | } | |
97 | ||
98 | static JSStaticFunction JSNode_staticFunctions[] = { | |
99 | { "appendChild", JSNode_appendChild, kJSPropertyAttributeDontDelete }, | |
100 | { "removeChild", JSNode_removeChild, kJSPropertyAttributeDontDelete }, | |
101 | { "replaceChild", JSNode_replaceChild, kJSPropertyAttributeDontDelete }, | |
102 | { 0, 0, 0 } | |
103 | }; | |
104 | ||
105 | static JSValueRef JSNode_getNodeType(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) | |
106 | { | |
107 | UNUSED_PARAM(propertyName); | |
108 | UNUSED_PARAM(exception); | |
109 | ||
110 | Node* node = JSObjectGetPrivate(object); | |
111 | if (node) { | |
112 | JSStringRef nodeType = JSStringCreateWithUTF8CString(node->nodeType); | |
113 | JSValueRef value = JSValueMakeString(context, nodeType); | |
114 | JSStringRelease(nodeType); | |
115 | return value; | |
116 | } | |
117 | ||
118 | return NULL; | |
119 | } | |
120 | ||
121 | static JSValueRef JSNode_getChildNodes(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception) | |
122 | { | |
123 | UNUSED_PARAM(propertyName); | |
124 | UNUSED_PARAM(exception); | |
125 | ||
126 | Node* node = JSObjectGetPrivate(thisObject); | |
127 | ASSERT(node); | |
128 | return JSNodeList_new(context, NodeList_new(node)); | |
129 | } | |
130 | ||
131 | static JSValueRef JSNode_getFirstChild(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception) | |
132 | { | |
133 | UNUSED_PARAM(object); | |
134 | UNUSED_PARAM(propertyName); | |
135 | UNUSED_PARAM(exception); | |
136 | ||
137 | return JSValueMakeUndefined(context); | |
138 | } | |
139 | ||
140 | static JSStaticValue JSNode_staticValues[] = { | |
141 | { "nodeType", JSNode_getNodeType, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, | |
142 | { "childNodes", JSNode_getChildNodes, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, | |
143 | { "firstChild", JSNode_getFirstChild, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly }, | |
144 | { 0, 0, 0, 0 } | |
145 | }; | |
146 | ||
147 | static void JSNode_initialize(JSContextRef context, JSObjectRef object) | |
148 | { | |
149 | UNUSED_PARAM(context); | |
150 | ||
151 | Node* node = JSObjectGetPrivate(object); | |
152 | ASSERT(node); | |
153 | ||
154 | Node_ref(node); | |
155 | } | |
156 | ||
157 | static void JSNode_finalize(JSObjectRef object) | |
158 | { | |
159 | Node* node = JSObjectGetPrivate(object); | |
160 | ASSERT(node); | |
161 | ||
162 | Node_deref(node); | |
163 | } | |
164 | ||
165 | JSClassRef JSNode_class(JSContextRef context) | |
166 | { | |
167 | UNUSED_PARAM(context); | |
168 | ||
169 | static JSClassRef jsClass; | |
170 | if (!jsClass) { | |
171 | JSClassDefinition definition = kJSClassDefinitionEmpty; | |
172 | definition.staticValues = JSNode_staticValues; | |
173 | definition.staticFunctions = JSNode_staticFunctions; | |
174 | definition.initialize = JSNode_initialize; | |
175 | definition.finalize = JSNode_finalize; | |
176 | ||
177 | jsClass = JSClassCreate(&definition); | |
178 | } | |
179 | return jsClass; | |
180 | } | |
181 | ||
182 | JSObjectRef JSNode_new(JSContextRef context, Node* node) | |
183 | { | |
184 | return JSObjectMake(context, JSNode_class(context), node); | |
185 | } | |
186 | ||
187 | JSObjectRef JSNode_construct(JSContextRef context, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception) | |
188 | { | |
189 | UNUSED_PARAM(object); | |
190 | UNUSED_PARAM(argumentCount); | |
191 | UNUSED_PARAM(arguments); | |
192 | UNUSED_PARAM(exception); | |
193 | ||
194 | return JSNode_new(context, Node_new()); | |
195 | } |