source: webkit/trunk/JavaScriptCore/API/JSNode.c@ 29238

Last change on this file since 29238 was 29238, checked in by [email protected], 17 years ago

Reviewed by Darin.

Fix Mac build.

  • API/JSNode.c:
  • API/JSNode.h:
  • API/JSNodeList.c:
  • API/JSNodeList.h:
  • API/JavaScript.h:
  • API/JavaScriptCore.h:
  • API/minidom.c:
  • JavaScriptCore.xcodeproj/project.pbxproj:
File size: 7.5 KB
Line 
1// -*- mode: c++; c-basic-offset: 4 -*-
2/*
3 * Copyright (C) 2006 Apple Computer, Inc. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27#include "JSNode.h"
28#include "JSNodeList.h"
29#include "JSObjectRef.h"
30#include "JSStringRef.h"
31#include "JSValueRef.h"
32#include "Node.h"
33#include "NodeList.h"
34#include "UnusedParam.h"
35#include <wtf/Assertions.h>
36
37static JSValueRef JSNode_appendChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
38{
39 UNUSED_PARAM(context);
40 UNUSED_PARAM(function);
41
42 // Example of throwing a type error for invalid values
43 if (!JSValueIsObjectOfClass(context, thisObject, JSNode_class(context))) {
44 JSStringRef message = JSStringCreateWithUTF8CString("TypeError: appendChild can only be called on nodes");
45 *exception = JSValueMakeString(context, message);
46 JSStringRelease(message);
47 } else if (argumentCount < 1 || !JSValueIsObjectOfClass(context, arguments[0], JSNode_class(context))) {
48 JSStringRef message = JSStringCreateWithUTF8CString("TypeError: first argument to appendChild must be a node");
49 *exception = JSValueMakeString(context, message);
50 JSStringRelease(message);
51 } else {
52 Node* node = JSObjectGetPrivate(thisObject);
53 Node* child = JSObjectGetPrivate(JSValueToObject(context, arguments[0], NULL));
54
55 Node_appendChild(node, child);
56 }
57
58 return JSValueMakeUndefined(context);
59}
60
61static JSValueRef JSNode_removeChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
62{
63 UNUSED_PARAM(context);
64 UNUSED_PARAM(function);
65
66 // Example of ignoring invalid values
67 if (argumentCount > 0) {
68 if (JSValueIsObjectOfClass(context, thisObject, JSNode_class(context))) {
69 if (JSValueIsObjectOfClass(context, arguments[0], JSNode_class(context))) {
70 Node* node = JSObjectGetPrivate(thisObject);
71 Node* child = JSObjectGetPrivate(JSValueToObject(context, arguments[0], exception));
72
73 Node_removeChild(node, child);
74 }
75 }
76 }
77
78 return JSValueMakeUndefined(context);
79}
80
81static JSValueRef JSNode_replaceChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
82{
83 UNUSED_PARAM(context);
84 UNUSED_PARAM(function);
85
86 if (argumentCount > 1) {
87 if (JSValueIsObjectOfClass(context, thisObject, JSNode_class(context))) {
88 if (JSValueIsObjectOfClass(context, arguments[0], JSNode_class(context))) {
89 if (JSValueIsObjectOfClass(context, arguments[1], JSNode_class(context))) {
90 Node* node = JSObjectGetPrivate(thisObject);
91 Node* newChild = JSObjectGetPrivate(JSValueToObject(context, arguments[0], exception));
92 Node* oldChild = JSObjectGetPrivate(JSValueToObject(context, arguments[1], exception));
93
94 Node_replaceChild(node, newChild, oldChild);
95 }
96 }
97 }
98 }
99
100 return JSValueMakeUndefined(context);
101}
102
103static JSStaticFunction JSNode_staticFunctions[] = {
104 { "appendChild", JSNode_appendChild, kJSPropertyAttributeDontDelete },
105 { "removeChild", JSNode_removeChild, kJSPropertyAttributeDontDelete },
106 { "replaceChild", JSNode_replaceChild, kJSPropertyAttributeDontDelete },
107 { 0, 0, 0 }
108};
109
110static JSValueRef JSNode_getNodeType(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
111{
112 UNUSED_PARAM(context);
113 UNUSED_PARAM(propertyName);
114 UNUSED_PARAM(exception);
115
116 Node* node = JSObjectGetPrivate(object);
117 if (node) {
118 JSStringRef nodeType = JSStringCreateWithUTF8CString(node->nodeType);
119 JSValueRef value = JSValueMakeString(context, nodeType);
120 JSStringRelease(nodeType);
121 return value;
122 }
123
124 return NULL;
125}
126
127static JSValueRef JSNode_getChildNodes(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
128{
129 UNUSED_PARAM(propertyName);
130 UNUSED_PARAM(exception);
131
132 Node* node = JSObjectGetPrivate(thisObject);
133 ASSERT(node);
134 return JSNodeList_new(context, NodeList_new(node));
135}
136
137static JSValueRef JSNode_getFirstChild(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
138{
139 UNUSED_PARAM(context);
140 UNUSED_PARAM(propertyName);
141 UNUSED_PARAM(object);
142 UNUSED_PARAM(exception);
143
144 return JSValueMakeUndefined(context);
145}
146
147static JSStaticValue JSNode_staticValues[] = {
148 { "nodeType", JSNode_getNodeType, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
149 { "childNodes", JSNode_getChildNodes, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
150 { "firstChild", JSNode_getFirstChild, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
151 { 0, 0, 0, 0 }
152};
153
154static void JSNode_initialize(JSContextRef context, JSObjectRef object)
155{
156 UNUSED_PARAM(context);
157
158 Node* node = JSObjectGetPrivate(object);
159 ASSERT(node);
160
161 Node_ref(node);
162}
163
164static void JSNode_finalize(JSObjectRef object)
165{
166 Node* node = JSObjectGetPrivate(object);
167 ASSERT(node);
168
169 Node_deref(node);
170}
171
172JSClassRef JSNode_class(JSContextRef context)
173{
174 UNUSED_PARAM(context);
175
176 static JSClassRef jsClass;
177 if (!jsClass) {
178 JSClassDefinition definition = kJSClassDefinitionEmpty;
179 definition.staticValues = JSNode_staticValues;
180 definition.staticFunctions = JSNode_staticFunctions;
181 definition.initialize = JSNode_initialize;
182 definition.finalize = JSNode_finalize;
183
184 jsClass = JSClassCreate(&definition);
185 }
186 return jsClass;
187}
188
189JSObjectRef JSNode_new(JSContextRef context, Node* node)
190{
191 return JSObjectMake(context, JSNode_class(context), node);
192}
193
194JSObjectRef JSNode_construct(JSContextRef context, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
195{
196 UNUSED_PARAM(object);
197 UNUSED_PARAM(argumentCount);
198 UNUSED_PARAM(arguments);
199 UNUSED_PARAM(exception);
200
201 return JSNode_new(context, Node_new());
202}
Note: See TracBrowser for help on using the repository browser.