1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
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/
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
16 * The Original Code is SpiderMonkey E4X code, released August, 2004.
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.
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.
37 * ***** END LICENSE BLOCK ***** */
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[];
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[];
57 (* JSIdentityOp)(const void *a, const void *b);
63 JSXMLArrayCursor *cursors;
66 length = capacity = 0;
71 void finish(JSContext *cx);
73 bool setCapacity(JSContext *cx, uint32 capacity);
77 struct JSXMLArrayCursor
81 JSXMLArrayCursor *next;
82 JSXMLArrayCursor **prevp;
85 JSXMLArrayCursor(JSXMLArray *array)
86 : array(array), index(0), next(array->cursors), prevp(&array->cursors),
91 array->cursors = this;
94 ~JSXMLArrayCursor() { disconnect(); }
106 if (!array || index >= array->length)
108 return root = array->vector[index++];
112 if (!array || index >= array->length)
114 return root = array->vector[index];
117 void trace(JSTracer *trc);
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)
125 * NB: don't reorder this enum without changing all array initializers that
126 * depend on it in jsxml.c.
128 typedef enum JSXMLClass {
131 JSXML_CLASS_ATTRIBUTE,
132 JSXML_CLASS_PROCESSING_INSTRUCTION,
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))
148 typedef struct JSXMLListVar {
149 JSXMLArray kids; /* NB: must come first */
151 JSObject *targetprop;
154 typedef struct JSXMLElemVar {
155 JSXMLArray kids; /* NB: must come first */
156 JSXMLArray namespaces;
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
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 \
176 struct JSXML : js::gc::Cell {
182 void *domnode; /* DOM node if mapped info item */
185 uint32 xml_class; /* discriminates u, below */
186 uint32 xml_flags; /* flags, see below */
193 void finalize(JSContext *cx) {
194 if (JSXML_HAS_KIDS(this)) {
196 if (xml_class == JSXML_CLASS_ELEMENT) {
197 xml_namespaces.finish(cx);
198 xml_attrs.finish(cx);
202 JS_REMOVE_LINK(&links);
207 /* xml_flags values */
208 #define XMLF_WHITESPACE_TEXT 0x1
211 js_NewXML(JSContext *cx, JSXMLClass xml_class);
214 js_TraceXML(JSTracer *trc, JSXML *xml);
217 js_NewXMLObject(JSContext *cx, JSXMLClass xml_class);
220 js_GetXMLObject(JSContext *cx, JSXML *xml);
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;
230 * Methods to test whether an object or a value is of type "xml" (per typeof).
233 JSObject::isXML() const
235 return getClass() == &js_XMLClass;
239 JSObject::isXMLId() const
241 js::Class *clasp = getClass();
242 return clasp == &js_QNameClass ||
243 clasp == &js_AttributeNameClass ||
244 clasp == &js_AnyNameClass;
247 #define VALUE_IS_XML(v) (!JSVAL_IS_PRIMITIVE(v) && JSVAL_TO_OBJECT(v)->isXML())
250 JSObject::isNamespace() const
252 return getClass() == &js_NamespaceClass;
256 JSObject::isQName() const
258 js::Class* clasp = getClass();
259 return clasp == &js_QNameClass ||
260 clasp == &js_AttributeNameClass ||
261 clasp == &js_AnyNameClass;
265 IsXML(const js::Value &v)
267 return v.isObject() && v.toObject().isXML();
271 js_InitNamespaceClass(JSContext *cx, JSObject *obj);
274 js_InitQNameClass(JSContext *cx, JSObject *obj);
277 js_InitXMLClass(JSContext *cx, JSObject *obj);
280 js_InitXMLClasses(JSContext *cx, JSObject *obj);
283 js_GetFunctionNamespace(JSContext *cx, js::Value *vp);
286 * If obj is QName corresponding to function::name, set *funidp to name's id,
287 * otherwise set *funidp to void.
290 js_IsFunctionQName(JSContext *cx, JSObject *obj, jsid *funidp);
293 js_GetDefaultXMLNamespace(JSContext *cx, jsval *vp);
296 js_SetDefaultXMLNamespace(JSContext *cx, const js::Value &v);
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.
304 js_IsXMLName(JSContext *cx, jsval v);
307 js_ToAttributeName(JSContext *cx, js::Value *vp);
309 extern JSFlatString *
310 js_EscapeAttributeValue(JSContext *cx, JSString *str, JSBool quote);
313 js_AddAttributePart(JSContext *cx, JSBool isName, JSString *str,
316 extern JSFlatString *
317 js_EscapeElementValue(JSContext *cx, JSString *str);
320 js_ValueToXMLString(JSContext *cx, const js::Value &v);
323 js_ConstructXMLQNameObject(JSContext *cx, const js::Value & nsval,
324 const js::Value & lnval);
327 js_GetAnyName(JSContext *cx, jsid *idp);
330 * Note: nameval must be either QName, AttributeName, or AnyName.
333 js_FindXMLProperty(JSContext *cx, const js::Value &nameval, JSObject **objp, jsid *idp);
336 js_GetXMLMethod(JSContext *cx, JSObject *obj, jsid id, js::Value *vp);
339 js_GetXMLDescendants(JSContext *cx, JSObject *obj, jsval id, jsval *vp);
342 js_DeleteXMLListElements(JSContext *cx, JSObject *listobj);
345 js_StepXMLListFilter(JSContext *cx, JSBool initialized);
348 js_ValueToXMLObject(JSContext *cx, const js::Value &v);
351 js_ValueToXMLListObject(JSContext *cx, const js::Value &v);
354 js_NewXMLSpecialObject(JSContext *cx, JSXMLClass xml_class, JSString *name,
358 js_MakeXMLCDATAString(JSContext *cx, JSString *str);
361 js_MakeXMLCommentString(JSContext *cx, JSString *str);
364 js_MakeXMLPIString(JSContext *cx, JSString *name, JSString *str);
366 /* The caller must ensure that either v1 or v2 is an object. */
368 js_TestXMLEquality(JSContext *cx, const js::Value &v1, const js::Value &v2,
372 js_ConcatenateXML(JSContext *cx, JSObject *obj1, JSObject *obj2, js::Value *vp);
374 #endif /* jsxml_h___ */