2 * Copyright (C) 2006 Apple Computer, 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.
27 #include "JSNodeList.h"
28 #include "JSObjectRef.h"
29 #include "JSStringRef.h"
30 #include "JSValueRef.h"
33 #include "UnusedParam.h"
34 #include <wtf/Assertions.h>
36 static JSValueRef
JSNode_appendChild(JSContextRef context
, JSObjectRef function
, JSObjectRef thisObject
, size_t argumentCount
, const JSValueRef arguments
[], JSValueRef
* exception
)
38 UNUSED_PARAM(function
);
40 /* Example of throwing a type error for invalid values */
41 if (!JSValueIsObjectOfClass(context
, thisObject
, JSNode_class(context
))) {
42 JSStringRef message
= JSStringCreateWithUTF8CString("TypeError: appendChild can only be called on nodes");
43 *exception
= JSValueMakeString(context
, message
);
44 JSStringRelease(message
);
45 } else if (argumentCount
< 1 || !JSValueIsObjectOfClass(context
, arguments
[0], JSNode_class(context
))) {
46 JSStringRef message
= JSStringCreateWithUTF8CString("TypeError: first argument to appendChild must be a node");
47 *exception
= JSValueMakeString(context
, message
);
48 JSStringRelease(message
);
50 Node
* node
= JSObjectGetPrivate(thisObject
);
51 Node
* child
= JSObjectGetPrivate(JSValueToObject(context
, arguments
[0], NULL
));
53 Node_appendChild(node
, child
);
56 return JSValueMakeUndefined(context
);
59 static JSValueRef
JSNode_removeChild(JSContextRef context
, JSObjectRef function
, JSObjectRef thisObject
, size_t argumentCount
, const JSValueRef arguments
[], JSValueRef
* exception
)
61 UNUSED_PARAM(function
);
63 /* Example of ignoring invalid values */
64 if (argumentCount
> 0) {
65 if (JSValueIsObjectOfClass(context
, thisObject
, JSNode_class(context
))) {
66 if (JSValueIsObjectOfClass(context
, arguments
[0], JSNode_class(context
))) {
67 Node
* node
= JSObjectGetPrivate(thisObject
);
68 Node
* child
= JSObjectGetPrivate(JSValueToObject(context
, arguments
[0], exception
));
70 Node_removeChild(node
, child
);
75 return JSValueMakeUndefined(context
);
78 static JSValueRef
JSNode_replaceChild(JSContextRef context
, JSObjectRef function
, JSObjectRef thisObject
, size_t argumentCount
, const JSValueRef arguments
[], JSValueRef
* exception
)
80 UNUSED_PARAM(function
);
82 if (argumentCount
> 1) {
83 if (JSValueIsObjectOfClass(context
, thisObject
, JSNode_class(context
))) {
84 if (JSValueIsObjectOfClass(context
, arguments
[0], JSNode_class(context
))) {
85 if (JSValueIsObjectOfClass(context
, arguments
[1], JSNode_class(context
))) {
86 Node
* node
= JSObjectGetPrivate(thisObject
);
87 Node
* newChild
= JSObjectGetPrivate(JSValueToObject(context
, arguments
[0], exception
));
88 Node
* oldChild
= JSObjectGetPrivate(JSValueToObject(context
, arguments
[1], exception
));
90 Node_replaceChild(node
, newChild
, oldChild
);
96 return JSValueMakeUndefined(context
);
99 static JSStaticFunction JSNode_staticFunctions
[] = {
100 { "appendChild", JSNode_appendChild
, kJSPropertyAttributeDontDelete
},
101 { "removeChild", JSNode_removeChild
, kJSPropertyAttributeDontDelete
},
102 { "replaceChild", JSNode_replaceChild
, kJSPropertyAttributeDontDelete
},
106 static JSValueRef
JSNode_getNodeType(JSContextRef context
, JSObjectRef object
, JSStringRef propertyName
, JSValueRef
* exception
)
108 UNUSED_PARAM(propertyName
);
109 UNUSED_PARAM(exception
);
111 Node
* node
= JSObjectGetPrivate(object
);
113 JSStringRef nodeType
= JSStringCreateWithUTF8CString(node
->nodeType
);
114 JSValueRef value
= JSValueMakeString(context
, nodeType
);
115 JSStringRelease(nodeType
);
122 static JSValueRef
JSNode_getChildNodes(JSContextRef context
, JSObjectRef thisObject
, JSStringRef propertyName
, JSValueRef
* exception
)
124 UNUSED_PARAM(propertyName
);
125 UNUSED_PARAM(exception
);
127 Node
* node
= JSObjectGetPrivate(thisObject
);
129 return JSNodeList_new(context
, NodeList_new(node
));
132 static JSValueRef
JSNode_getFirstChild(JSContextRef context
, JSObjectRef object
, JSStringRef propertyName
, JSValueRef
* exception
)
134 UNUSED_PARAM(object
);
135 UNUSED_PARAM(propertyName
);
136 UNUSED_PARAM(exception
);
138 return JSValueMakeUndefined(context
);
141 static JSStaticValue JSNode_staticValues
[] = {
142 { "nodeType", JSNode_getNodeType
, NULL
, kJSPropertyAttributeDontDelete
| kJSPropertyAttributeReadOnly
},
143 { "childNodes", JSNode_getChildNodes
, NULL
, kJSPropertyAttributeDontDelete
| kJSPropertyAttributeReadOnly
},
144 { "firstChild", JSNode_getFirstChild
, NULL
, kJSPropertyAttributeDontDelete
| kJSPropertyAttributeReadOnly
},
148 static void JSNode_initialize(JSContextRef context
, JSObjectRef object
)
150 UNUSED_PARAM(context
);
152 Node
* node
= JSObjectGetPrivate(object
);
158 static void JSNode_finalize(JSObjectRef object
)
160 Node
* node
= JSObjectGetPrivate(object
);
166 JSClassRef
JSNode_class(JSContextRef context
)
168 UNUSED_PARAM(context
);
170 static JSClassRef jsClass
;
172 JSClassDefinition definition
= kJSClassDefinitionEmpty
;
173 definition
.staticValues
= JSNode_staticValues
;
174 definition
.staticFunctions
= JSNode_staticFunctions
;
175 definition
.initialize
= JSNode_initialize
;
176 definition
.finalize
= JSNode_finalize
;
178 jsClass
= JSClassCreate(&definition
);
183 JSObjectRef
JSNode_new(JSContextRef context
, Node
* node
)
185 return JSObjectMake(context
, JSNode_class(context
), node
);
188 JSObjectRef
JSNode_construct(JSContextRef context
, JSObjectRef object
, size_t argumentCount
, const JSValueRef arguments
[], JSValueRef
* exception
)
190 UNUSED_PARAM(object
);
191 UNUSED_PARAM(argumentCount
);
192 UNUSED_PARAM(arguments
);
193 UNUSED_PARAM(exception
);
195 return JSNode_new(context
, Node_new());