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 "Node.h"
|
---|
30 | #include "NodeList.h"
|
---|
31 | #include "UnusedParam.h"
|
---|
32 |
|
---|
33 | static JSClassRef JSNode_class(JSContextRef context);
|
---|
34 |
|
---|
35 | static JSValueRef JSNodePrototype_appendChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
|
---|
36 | {
|
---|
37 | UNUSED_PARAM(context);
|
---|
38 | UNUSED_PARAM(function);
|
---|
39 |
|
---|
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);
|
---|
49 | } else {
|
---|
50 | Node* node = JSObjectGetPrivate(thisObject);
|
---|
51 | Node* child = JSObjectGetPrivate(JSValueToObject(context, arguments[0], NULL));
|
---|
52 |
|
---|
53 | Node_appendChild(node, child);
|
---|
54 | }
|
---|
55 |
|
---|
56 | return JSValueMakeUndefined(context);
|
---|
57 | }
|
---|
58 |
|
---|
59 | static JSValueRef JSNodePrototype_removeChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
|
---|
60 | {
|
---|
61 | UNUSED_PARAM(context);
|
---|
62 | UNUSED_PARAM(function);
|
---|
63 |
|
---|
64 | // Example of ignoring invalid values
|
---|
65 | if (argumentCount > 0) {
|
---|
66 | if (JSValueIsObjectOfClass(context, thisObject, JSNode_class(context))) {
|
---|
67 | if (JSValueIsObjectOfClass(context, arguments[0], JSNode_class(context))) {
|
---|
68 | Node* node = JSObjectGetPrivate(thisObject);
|
---|
69 | Node* child = JSObjectGetPrivate(JSValueToObject(context, arguments[0], NULL));
|
---|
70 |
|
---|
71 | Node_removeChild(node, child);
|
---|
72 | }
|
---|
73 | }
|
---|
74 | }
|
---|
75 |
|
---|
76 | return JSValueMakeUndefined(context);
|
---|
77 | }
|
---|
78 |
|
---|
79 | static JSValueRef JSNodePrototype_replaceChild(JSContextRef context, JSObjectRef function, JSObjectRef thisObject, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
|
---|
80 | {
|
---|
81 | UNUSED_PARAM(context);
|
---|
82 | UNUSED_PARAM(function);
|
---|
83 |
|
---|
84 | if (argumentCount > 1) {
|
---|
85 | if (JSValueIsObjectOfClass(context, thisObject, JSNode_class(context))) {
|
---|
86 | if (JSValueIsObjectOfClass(context, arguments[0], JSNode_class(context))) {
|
---|
87 | if (JSValueIsObjectOfClass(context, arguments[1], JSNode_class(context))) {
|
---|
88 | Node* node = JSObjectGetPrivate(thisObject);
|
---|
89 | Node* newChild = JSObjectGetPrivate(JSValueToObject(context, arguments[0], NULL));
|
---|
90 | Node* oldChild = JSObjectGetPrivate(JSValueToObject(context, arguments[1], NULL));
|
---|
91 |
|
---|
92 | Node_replaceChild(node, newChild, oldChild);
|
---|
93 | }
|
---|
94 | }
|
---|
95 | }
|
---|
96 | }
|
---|
97 |
|
---|
98 | return JSValueMakeUndefined(context);
|
---|
99 | }
|
---|
100 |
|
---|
101 | static JSStaticFunction JSNodePrototype_staticFunctions[] = {
|
---|
102 | { "appendChild", JSNodePrototype_appendChild, kJSPropertyAttributeDontDelete },
|
---|
103 | { "removeChild", JSNodePrototype_removeChild, kJSPropertyAttributeDontDelete },
|
---|
104 | { "replaceChild", JSNodePrototype_replaceChild, kJSPropertyAttributeDontDelete },
|
---|
105 | { 0, 0, 0 }
|
---|
106 | };
|
---|
107 |
|
---|
108 | static JSClassRef JSNodePrototype_class(JSContextRef context)
|
---|
109 | {
|
---|
110 | static JSClassRef jsClass;
|
---|
111 | if (!jsClass) {
|
---|
112 | JSClassDefinition definition = kJSClassDefinitionNull;
|
---|
113 | definition.staticFunctions = JSNodePrototype_staticFunctions;
|
---|
114 | jsClass = JSClassCreate(&definition);
|
---|
115 | }
|
---|
116 | return jsClass;
|
---|
117 | }
|
---|
118 |
|
---|
119 | static JSValueRef JSNode_getNodeType(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
|
---|
120 | {
|
---|
121 | UNUSED_PARAM(context);
|
---|
122 | UNUSED_PARAM(propertyName);
|
---|
123 |
|
---|
124 | Node* node = JSObjectGetPrivate(object);
|
---|
125 | if (node) {
|
---|
126 | JSStringRef nodeType = JSStringCreateWithUTF8CString(node->nodeType);
|
---|
127 | JSValueRef value = JSValueMakeString(context, nodeType);
|
---|
128 | JSStringRelease(nodeType);
|
---|
129 | return value;
|
---|
130 | }
|
---|
131 |
|
---|
132 | return NULL;
|
---|
133 | }
|
---|
134 |
|
---|
135 | static JSValueRef JSNode_getChildNodes(JSContextRef context, JSObjectRef thisObject, JSStringRef propertyName, JSValueRef* exception)
|
---|
136 | {
|
---|
137 | UNUSED_PARAM(propertyName);
|
---|
138 | Node* node = JSObjectGetPrivate(thisObject);
|
---|
139 | assert(node);
|
---|
140 | return JSNodeList_new(context, NodeList_new(node));
|
---|
141 | }
|
---|
142 |
|
---|
143 | static JSValueRef JSNode_getFirstChild(JSContextRef context, JSObjectRef object, JSStringRef propertyName, JSValueRef* exception)
|
---|
144 | {
|
---|
145 | UNUSED_PARAM(context);
|
---|
146 | UNUSED_PARAM(propertyName);
|
---|
147 | UNUSED_PARAM(object);
|
---|
148 |
|
---|
149 | return JSValueMakeUndefined(context);
|
---|
150 | }
|
---|
151 |
|
---|
152 | static JSStaticValue JSNode_staticValues[] = {
|
---|
153 | { "nodeType", JSNode_getNodeType, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
|
---|
154 | { "childNodes", JSNode_getChildNodes, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
|
---|
155 | { "firstChild", JSNode_getFirstChild, NULL, kJSPropertyAttributeDontDelete | kJSPropertyAttributeReadOnly },
|
---|
156 | { 0, 0, 0, 0 }
|
---|
157 | };
|
---|
158 |
|
---|
159 | static void JSNode_finalize(JSObjectRef object)
|
---|
160 | {
|
---|
161 | Node* node = JSObjectGetPrivate(object);
|
---|
162 | assert(node);
|
---|
163 |
|
---|
164 | Node_deref(node);
|
---|
165 | }
|
---|
166 |
|
---|
167 | static JSClassRef JSNode_class(JSContextRef context)
|
---|
168 | {
|
---|
169 | static JSClassRef jsClass;
|
---|
170 | if (!jsClass) {
|
---|
171 | JSClassDefinition definition = kJSClassDefinitionNull;
|
---|
172 | definition.staticValues = JSNode_staticValues;
|
---|
173 | definition.finalize = JSNode_finalize;
|
---|
174 |
|
---|
175 | jsClass = JSClassCreate(&definition);
|
---|
176 | }
|
---|
177 | return jsClass;
|
---|
178 | }
|
---|
179 |
|
---|
180 | JSObjectRef JSNode_prototype(JSContextRef context)
|
---|
181 | {
|
---|
182 | static JSObjectRef prototype;
|
---|
183 | if (!prototype) {
|
---|
184 | prototype = JSObjectMake(context, JSNodePrototype_class(context), NULL);
|
---|
185 | JSValueProtect(context, prototype);
|
---|
186 | }
|
---|
187 | return prototype;
|
---|
188 | }
|
---|
189 |
|
---|
190 | JSObjectRef JSNode_new(JSContextRef context, Node* node)
|
---|
191 | {
|
---|
192 | Node_ref(node);
|
---|
193 |
|
---|
194 | JSObjectRef jsNode = JSObjectMake(context, JSNode_class(context), JSNode_prototype(context));
|
---|
195 | JSObjectSetPrivate(jsNode, node);
|
---|
196 | return jsNode;
|
---|
197 | }
|
---|
198 |
|
---|
199 | JSObjectRef JSNode_construct(JSContextRef context, JSObjectRef object, size_t argumentCount, const JSValueRef arguments[], JSValueRef* exception)
|
---|
200 | {
|
---|
201 | UNUSED_PARAM(object);
|
---|
202 | UNUSED_PARAM(argumentCount);
|
---|
203 | UNUSED_PARAM(arguments);
|
---|
204 |
|
---|
205 | return JSNode_new(context, Node_new());
|
---|
206 | }
|
---|