Imported Upstream version 1.0.0
[platform/upstream/js.git] / js / src / jsxml.h
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2  *
3  * ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is SpiderMonkey E4X code, released August, 2004.
17  *
18  * The Initial Developer of the Original Code is
19  * Netscape Communications Corporation.
20  * Portions created by the Initial Developer are Copyright (C) 1998
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  *
25  * Alternatively, the contents of this file may be used under the terms of
26  * either of the GNU General Public License Version 2 or later (the "GPL"),
27  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28  * in which case the provisions of the GPL or the LGPL are applicable instead
29  * of those above. If you wish to allow use of your version of this file only
30  * under the terms of either the GPL or the LGPL, and not to allow others to
31  * use your version of this file under the terms of the MPL, indicate your
32  * decision by deleting the provisions above and replace them with the notice
33  * and other provisions required by the GPL or the LGPL. If you do not delete
34  * the provisions above, a recipient may use your version of this file under
35  * the terms of any one of the MPL, the GPL or the LGPL.
36  *
37  * ***** END LICENSE BLOCK ***** */
38
39 #ifndef jsxml_h___
40 #define jsxml_h___
41
42 #include "jspubtd.h"
43 #include "jsobj.h"
44 #include "jscell.h"
45
46 extern const char js_AnyName_str[];
47 extern const char js_AttributeName_str[];
48 extern const char js_isXMLName_str[];
49 extern const char js_XMLList_str[];
50
51 extern const char js_amp_entity_str[];
52 extern const char js_gt_entity_str[];
53 extern const char js_lt_entity_str[];
54 extern const char js_quot_entity_str[];
55
56 typedef JSBool
57 (* JSIdentityOp)(const void *a, const void *b);
58
59 struct JSXMLArray {
60     uint32              length;
61     uint32              capacity;
62     void                **vector;
63     JSXMLArrayCursor    *cursors;
64
65     void init() {
66         length = capacity = 0;
67         vector = NULL;
68         cursors = NULL;
69     }
70
71     void finish(JSContext *cx);
72
73     bool setCapacity(JSContext *cx, uint32 capacity);
74     void trim();
75 };
76
77 struct JSXMLArrayCursor
78 {
79     JSXMLArray       *array;
80     uint32           index;
81     JSXMLArrayCursor *next;
82     JSXMLArrayCursor **prevp;
83     void             *root;
84
85     JSXMLArrayCursor(JSXMLArray *array)
86       : array(array), index(0), next(array->cursors), prevp(&array->cursors),
87         root(NULL)
88     {
89         if (next)
90             next->prevp = &next;
91         array->cursors = this;
92     }
93
94     ~JSXMLArrayCursor() { disconnect(); }
95
96     void disconnect() {
97         if (!array)
98             return;
99         if (next)
100             next->prevp = prevp;
101         *prevp = next;
102         array = NULL;
103     }
104
105     void *getNext() {
106         if (!array || index >= array->length)
107             return NULL;
108         return root = array->vector[index++];
109     }
110
111     void *getCurrent() {
112         if (!array || index >= array->length)
113             return NULL;
114         return root = array->vector[index];
115     }
116
117     void trace(JSTracer *trc);
118 };
119
120 #define JSXML_PRESET_CAPACITY   JS_BIT(31)
121 #define JSXML_CAPACITY_MASK     JS_BITMASK(31)
122 #define JSXML_CAPACITY(array)   ((array)->capacity & JSXML_CAPACITY_MASK)
123
124 /*
125  * NB: don't reorder this enum without changing all array initializers that
126  * depend on it in jsxml.c.
127  */
128 typedef enum JSXMLClass {
129     JSXML_CLASS_LIST,
130     JSXML_CLASS_ELEMENT,
131     JSXML_CLASS_ATTRIBUTE,
132     JSXML_CLASS_PROCESSING_INSTRUCTION,
133     JSXML_CLASS_TEXT,
134     JSXML_CLASS_COMMENT,
135     JSXML_CLASS_LIMIT
136 } JSXMLClass;
137
138 #define JSXML_CLASS_HAS_KIDS(class_)    ((class_) < JSXML_CLASS_ATTRIBUTE)
139 #define JSXML_CLASS_HAS_VALUE(class_)   ((class_) >= JSXML_CLASS_ATTRIBUTE)
140 #define JSXML_CLASS_HAS_NAME(class_)                                          \
141     ((uintN)((class_) - JSXML_CLASS_ELEMENT) <=                               \
142      (uintN)(JSXML_CLASS_PROCESSING_INSTRUCTION - JSXML_CLASS_ELEMENT))
143
144 #ifdef DEBUG_notme
145 #include "jsclist.h"
146 #endif
147
148 typedef struct JSXMLListVar {
149     JSXMLArray          kids;           /* NB: must come first */
150     JSXML               *target;
151     JSObject            *targetprop;
152 } JSXMLListVar;
153
154 typedef struct JSXMLElemVar {
155     JSXMLArray          kids;           /* NB: must come first */
156     JSXMLArray          namespaces;
157     JSXMLArray          attrs;
158 } JSXMLElemVar;
159
160 /* union member shorthands */
161 #define xml_kids        u.list.kids
162 #define xml_target      u.list.target
163 #define xml_targetprop  u.list.targetprop
164 #define xml_namespaces  u.elem.namespaces
165 #define xml_attrs       u.elem.attrs
166 #define xml_value       u.value
167
168 /* xml_class-testing macros */
169 #define JSXML_HAS_KIDS(xml)     JSXML_CLASS_HAS_KIDS((xml)->xml_class)
170 #define JSXML_HAS_VALUE(xml)    JSXML_CLASS_HAS_VALUE((xml)->xml_class)
171 #define JSXML_HAS_NAME(xml)     JSXML_CLASS_HAS_NAME((xml)->xml_class)
172 #define JSXML_LENGTH(xml)       (JSXML_CLASS_HAS_KIDS((xml)->xml_class)       \
173                                  ? (xml)->xml_kids.length                     \
174                                  : 0)
175
176 struct JSXML : js::gc::Cell {
177 #ifdef DEBUG_notme
178     JSCList             links;
179     uint32              serial;
180 #endif
181     JSObject            *object;
182     void                *domnode;       /* DOM node if mapped info item */
183     JSXML               *parent;
184     JSObject            *name;
185     uint32              xml_class;      /* discriminates u, below */
186     uint32              xml_flags;      /* flags, see below */
187     union {
188         JSXMLListVar    list;
189         JSXMLElemVar    elem;
190         JSString        *value;
191     } u;
192     
193     void finalize(JSContext *cx) {
194         if (JSXML_HAS_KIDS(this)) {
195             xml_kids.finish(cx);
196             if (xml_class == JSXML_CLASS_ELEMENT) {
197                 xml_namespaces.finish(cx);
198                 xml_attrs.finish(cx);
199             }
200         }
201 #ifdef DEBUG_notme
202         JS_REMOVE_LINK(&links);
203 #endif
204     }
205 };
206
207 /* xml_flags values */
208 #define XMLF_WHITESPACE_TEXT    0x1
209
210 extern JSXML *
211 js_NewXML(JSContext *cx, JSXMLClass xml_class);
212
213 extern void
214 js_TraceXML(JSTracer *trc, JSXML *xml);
215
216 extern JSObject *
217 js_NewXMLObject(JSContext *cx, JSXMLClass xml_class);
218
219 extern JSObject *
220 js_GetXMLObject(JSContext *cx, JSXML *xml);
221
222 extern JS_FRIEND_DATA(js::Class) js_XMLClass;
223 extern JS_FRIEND_DATA(js::Class) js_NamespaceClass;
224 extern JS_FRIEND_DATA(js::Class) js_QNameClass;
225 extern JS_FRIEND_DATA(js::Class) js_AttributeNameClass;
226 extern JS_FRIEND_DATA(js::Class) js_AnyNameClass;
227 extern js::Class                 js_XMLFilterClass;
228
229 /*
230  * Methods to test whether an object or a value is of type "xml" (per typeof).
231  */
232 inline bool
233 JSObject::isXML() const
234 {
235     return getClass() == &js_XMLClass;
236 }
237
238 inline bool
239 JSObject::isXMLId() const
240 {
241     js::Class *clasp = getClass();
242     return clasp == &js_QNameClass ||
243            clasp == &js_AttributeNameClass ||
244            clasp == &js_AnyNameClass;
245 }
246
247 #define VALUE_IS_XML(v)      (!JSVAL_IS_PRIMITIVE(v) && JSVAL_TO_OBJECT(v)->isXML())
248
249 inline bool
250 JSObject::isNamespace() const
251 {
252     return getClass() == &js_NamespaceClass;
253 }
254
255 inline bool
256 JSObject::isQName() const
257 {
258     js::Class* clasp = getClass();
259     return clasp == &js_QNameClass ||
260            clasp == &js_AttributeNameClass ||
261            clasp == &js_AnyNameClass;
262 }
263
264 static inline bool
265 IsXML(const js::Value &v)
266 {
267     return v.isObject() && v.toObject().isXML();
268 }
269
270 extern JSObject *
271 js_InitNamespaceClass(JSContext *cx, JSObject *obj);
272
273 extern JSObject *
274 js_InitQNameClass(JSContext *cx, JSObject *obj);
275
276 extern JSObject *
277 js_InitXMLClass(JSContext *cx, JSObject *obj);
278
279 extern JSObject *
280 js_InitXMLClasses(JSContext *cx, JSObject *obj);
281
282 extern JSBool
283 js_GetFunctionNamespace(JSContext *cx, js::Value *vp);
284
285 /*
286  * If obj is QName corresponding to function::name, set *funidp to name's id,
287  * otherwise set *funidp to void.
288  */
289 JSBool
290 js_IsFunctionQName(JSContext *cx, JSObject *obj, jsid *funidp);
291
292 extern JSBool
293 js_GetDefaultXMLNamespace(JSContext *cx, jsval *vp);
294
295 extern JSBool
296 js_SetDefaultXMLNamespace(JSContext *cx, const js::Value &v);
297
298 /*
299  * Return true if v is a XML QName object, or if it converts to a string that
300  * contains a valid XML qualified name (one containing no :), false otherwise.
301  * NB: This function is an infallible predicate, it hides exceptions.
302  */
303 extern JSBool
304 js_IsXMLName(JSContext *cx, jsval v);
305
306 extern JSBool
307 js_ToAttributeName(JSContext *cx, js::Value *vp);
308
309 extern JSFlatString *
310 js_EscapeAttributeValue(JSContext *cx, JSString *str, JSBool quote);
311
312 extern JSString *
313 js_AddAttributePart(JSContext *cx, JSBool isName, JSString *str,
314                     JSString *str2);
315
316 extern JSFlatString *
317 js_EscapeElementValue(JSContext *cx, JSString *str);
318
319 extern JSString *
320 js_ValueToXMLString(JSContext *cx, const js::Value &v);
321
322 extern JSObject *
323 js_ConstructXMLQNameObject(JSContext *cx, const js::Value & nsval,
324                            const js::Value & lnval);
325
326 extern JSBool
327 js_GetAnyName(JSContext *cx, jsid *idp);
328
329 /*
330  * Note: nameval must be either QName, AttributeName, or AnyName.
331  */
332 extern JSBool
333 js_FindXMLProperty(JSContext *cx, const js::Value &nameval, JSObject **objp, jsid *idp);
334
335 extern JSBool
336 js_GetXMLMethod(JSContext *cx, JSObject *obj, jsid id, js::Value *vp);
337
338 extern JSBool
339 js_GetXMLDescendants(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
340
341 extern JSBool
342 js_DeleteXMLListElements(JSContext *cx, JSObject *listobj);
343
344 extern JSBool
345 js_StepXMLListFilter(JSContext *cx, JSBool initialized);
346
347 extern JSObject *
348 js_ValueToXMLObject(JSContext *cx, const js::Value &v);
349
350 extern JSObject *
351 js_ValueToXMLListObject(JSContext *cx, const js::Value &v);
352
353 extern JSObject *
354 js_NewXMLSpecialObject(JSContext *cx, JSXMLClass xml_class, JSString *name,
355                        JSString *value);
356
357 extern JSString *
358 js_MakeXMLCDATAString(JSContext *cx, JSString *str);
359
360 extern JSString *
361 js_MakeXMLCommentString(JSContext *cx, JSString *str);
362
363 extern JSString *
364 js_MakeXMLPIString(JSContext *cx, JSString *name, JSString *str);
365
366 /* The caller must ensure that either v1 or v2 is an object. */
367 extern JSBool
368 js_TestXMLEquality(JSContext *cx, const js::Value &v1, const js::Value &v2,
369                    JSBool *bp);
370
371 extern JSBool
372 js_ConcatenateXML(JSContext *cx, JSObject *obj1, JSObject *obj2, js::Value *vp);
373
374 #endif /* jsxml_h___ */