source: webkit/trunk/JavaScriptCore/API/minidom.js@ 15484

Last change on this file since 15484 was 15484, checked in by ggaren, 19 years ago

Reviewed by Maciej.


  • Removed the exception parameter from the initialize callback and, by extension, JSObjectMake. We have never had a need for exceptions when iniitializing, so the parameter seemed likely to "get in the way."


Also, an exception in JavaScript is thrown in response to input --
"invalid URL", "index not a number", etc., so it's the job of the
constructor function, not the initialize method, to throw.


If initialize *really* wants to throw, it can communicate the throw to
the constructor through the constructed object's private data (e.g., set
it to NULL, signaling to the consntructor that initialization failed.)


  • Added JSObjectMakeWithData, which enables a constructor to set private data on an object *before* it has been initialized. That way, the initialize methods can properly operate on the data.
  • API/JSNode.c: Moved ref into the initialize method, for better encapsulation, now that it's possible.
  • API/JSNodeList.c: ditto
  • API/minidom.c: (main): Do more aggressive garbage collection to test ref/deref and initialize/finalize.
  • API/minidom.js: store childNodes in a temporary so it doesn't get re-created like a thousand times. This makes debugging ref/deref easier
File size: 7.7 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// For browser-based testing
28if (typeof window != 'undefined') {
29 /*
30 The methods and constructors below approximate what that the native host should provide
31
32 print(string);
33
34 Node
35 |-- TextNode
36 |-- Element
37 |-- RootElement
38 |-- HeroElement
39 |-- VillainElement
40 |-- NameElement
41 |-- WeaponElement
42 |-- Document
43 */
44
45 function print(string, indentLevel)
46 {
47 document.getElementById('pre').appendChild(document.createTextNode(string));
48 }
49
50 Node = function()
51 {
52 this.__defineGetter__("childNodes", function() {
53 if (!this._childNodes)
54 this._childNodes = new Array();
55 return this._childNodes;
56 });
57 this.__defineGetter__("firstChild", function () {
58 return this.childNodes[0];
59 });
60 }
61
62 Node.prototype.nodeType = "Node";
63
64 Node.prototype.appendChild = function(child) {
65 this.childNodes.push(child);
66 }
67
68 Node.prototype.serialize = function(numSpaces) {
69 function printSpaces(n)
70 {
71 for (var i = 0; i < n; i++) // >
72 print(" ");
73 }
74
75 printSpaces(numSpaces);
76 print('<' + this.nodeType + '>' + '\n');
77
78 var childNodesLength = this.childNodes.length;
79 for (var i = 0; i < childNodesLength; i++) //>
80 this.childNodes[i].serialize(numSpaces + 4);
81
82 printSpaces(numSpaces);
83 print('</' + this.nodeType + '>\n');
84 }
85
86 TextNode = function(text)
87 {
88 this.text = text;
89 }
90
91 TextNode.prototype = new Node();
92
93 TextNode.prototype.nodeType = "Text";
94
95 TextNode.prototype.serialize = function(numSpaces) {
96 for (var i = 0; i < numSpaces; i++) // >
97 print(" ");
98 print(this.text + '\n');
99 }
100
101 Element = function()
102 {
103 }
104
105 Element.prototype = new Node();
106
107 Element.prototype.nodeType = "Element";
108
109 RootElement = function()
110 {
111 }
112
113 RootElement.prototype = new Element();
114
115 RootElement.prototype.nodeType = "Root";
116
117 HeroElement = function()
118 {
119 }
120
121 HeroElement.prototype = new Element();
122
123 HeroElement.prototype.nodeType = "Hero";
124
125 VillainElement = function()
126 {
127 }
128
129 VillainElement.prototype = new Element();
130
131 VillainElement.prototype.nodeType = "Villain";
132
133 NameElement = function()
134 {
135 }
136
137 NameElement.prototype = new Element();
138
139 NameElement.prototype.nodeType = "Name";
140
141 WeaponElement = function()
142 {
143 }
144
145 WeaponElement.prototype = new Element();
146
147 WeaponElement.prototype.nodeType = "Weapon";
148
149 Document = function()
150 {
151 }
152
153 Document.prototype = new Node();
154
155 Document.prototype.serialize = function() {
156 this.firstChild.serialize(0);
157 }
158
159 Document.prototype.createElement = function(elementType) {
160 return eval('new ' + elementType + 'Element()');
161 }
162
163 Document.prototype.createTextNode = function(text) {
164 return new TextNode(text);
165 }
166}
167
168function shouldBe(a, b)
169{
170 var evalA;
171 try {
172 evalA = eval(a);
173 } catch(e) {
174 evalA = e;
175 }
176
177 if (evalA == b || isNaN(evalA) && typeof evalA == 'number' && isNaN(b) && typeof b == 'number')
178 print("PASS: " + a + " should be " + b + " and is.", "green");
179 else
180 print("__FAIL__: " + a + " should be " + b + " but instead is " + evalA + ".", "red");
181}
182
183function test()
184{
185 print("Node is " + Node);
186 for (var p in Node)
187 print(p + ": " + Node[p]);
188
189 node = new Node();
190 print("node is " + node);
191 for (var p in node)
192 print(p + ": " + node[p]);
193
194 child1 = new Node();
195 child2 = new Node();
196 child3 = new Node();
197
198 node.appendChild(child1);
199 node.appendChild(child2);
200
201 var childNodes = node.childNodes;
202
203 for (var i = 0; i < childNodes.length + 1; i++) {
204 print("item " + i + ": " + childNodes.item(i));
205 }
206
207 for (var i = 0; i < childNodes.length + 1; i++) {
208 print(i + ": " + childNodes[i]);
209 }
210
211 node.removeChild(child1);
212 node.replaceChild(child3, child2);
213
214 for (var i = 0; i < childNodes.length + 1; i++) {
215 print("item " + i + ": " + childNodes.item(i));
216 }
217
218 for (var i = 0; i < childNodes.length + 1; i++) {
219 print(i + ": " + childNodes[i]);
220 }
221
222 try {
223 node.appendChild(null);
224 } catch(e) {
225 print("caught: " + e);
226 }
227
228 try {
229 var o = new Object();
230 o.appendChild = node.appendChild;
231 o.appendChild(node);
232 } catch(e) {
233 print("caught: " + e);
234 }
235
236 try {
237 node.appendChild();
238 } catch(e) {
239 print("caught: " + e);
240 }
241
242 oldNodeType = node.nodeType;
243 node.nodeType = 1;
244 shouldBe("node.nodeType", oldNodeType);
245
246 shouldBe("node instanceof Node", true);
247 shouldBe("new Object() instanceof Node", false);
248
249 print(Node);
250
251 /*
252 var element, name, weapon;
253
254 var document = new Document();
255 document.appendChild(document.createElement('Root'));
256
257 // Tank Girl
258 element = document.createElement('Hero');
259
260 name = document.createElement('Name');
261 name.appendChild(document.createTextNode('Tank Girl'));
262 element.appendChild(name);
263
264 weapon = document.createElement('Weapon');
265 weapon.appendChild(document.createTextNode('Tank'));
266 element.appendChild(weapon);
267
268 weapon = document.createElement('Weapon');
269 weapon.appendChild(document.createTextNode('Attitude'));
270 element.appendChild(weapon);
271
272 weapon = document.createElement('Weapon');
273 weapon.appendChild(document.createTextNode('Army of genetically engineered super-kangaroos'));
274 element.appendChild(weapon);
275
276 document.firstChild.appendChild(element);
277
278
279 // Skeletor
280 element = document.createElement('Villain');
281
282 name = document.createElement('Name');
283 name.appendChild(document.createTextNode('Skeletor'));
284 element.appendChild(name);
285
286 weapon = document.createElement('Weapon');
287 weapon.appendChild(document.createTextNode('Havok Staff'));
288 element.appendChild(weapon);
289
290 weapon = document.createElement('Weapon');
291 weapon.appendChild(document.createTextNode('Motley crew of henchmen'));
292 element.appendChild(weapon);
293
294 document.firstChild.appendChild(element);
295
296 // Serialize
297 document.serialize();
298 */
299}
300
301test();
Note: See TracBrowser for help on using the repository browser.