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 ***** */
45 extern const char js_AnyName_str
[];
46 extern const char js_AttributeName_str
[];
47 extern const char js_isXMLName_str
[];
48 extern const char js_XMLList_str
[];
50 extern const char js_amp_entity_str
[];
51 extern const char js_gt_entity_str
[];
52 extern const char js_lt_entity_str
[];
53 extern const char js_quot_entity_str
[];
56 (* JSIdentityOp
)(const void *a
, const void *b
);
62 JSXMLArrayCursor
*cursors
;
65 length
= capacity
= 0;
70 void finish(JSContext
*cx
);
72 bool setCapacity(JSContext
*cx
, uint32 capacity
);
76 struct JSXMLArrayCursor
80 JSXMLArrayCursor
*next
;
81 JSXMLArrayCursor
**prevp
;
84 JSXMLArrayCursor(JSXMLArray
*array
)
85 : array(array
), index(0), next(array
->cursors
), prevp(&array
->cursors
),
90 array
->cursors
= this;
93 ~JSXMLArrayCursor() { disconnect(); }
105 if (!array
|| index
>= array
->length
)
107 return root
= array
->vector
[index
++];
111 if (!array
|| index
>= array
->length
)
113 return root
= array
->vector
[index
];
116 void trace(JSTracer
*trc
) {
120 for (JSXMLArrayCursor
*cursor
= this; cursor
; cursor
= cursor
->next
)
121 js::MarkGCThing(trc
, cursor
->root
, "cursor_root", index
++);
125 #define JSXML_PRESET_CAPACITY JS_BIT(31)
126 #define JSXML_CAPACITY_MASK JS_BITMASK(31)
127 #define JSXML_CAPACITY(array) ((array)->capacity & JSXML_CAPACITY_MASK)
130 * NB: don't reorder this enum without changing all array initializers that
131 * depend on it in jsxml.c.
133 typedef enum JSXMLClass
{
136 JSXML_CLASS_ATTRIBUTE
,
137 JSXML_CLASS_PROCESSING_INSTRUCTION
,
143 #define JSXML_CLASS_HAS_KIDS(class_) ((class_) < JSXML_CLASS_ATTRIBUTE)
144 #define JSXML_CLASS_HAS_VALUE(class_) ((class_) >= JSXML_CLASS_ATTRIBUTE)
145 #define JSXML_CLASS_HAS_NAME(class_) \
146 ((uintN)((class_) - JSXML_CLASS_ELEMENT) <= \
147 (uintN)(JSXML_CLASS_PROCESSING_INSTRUCTION - JSXML_CLASS_ELEMENT))
153 typedef struct JSXMLListVar
{
154 JSXMLArray kids
; /* NB: must come first */
156 JSObject
*targetprop
;
159 typedef struct JSXMLElemVar
{
160 JSXMLArray kids
; /* NB: must come first */
161 JSXMLArray namespaces
;
171 void *domnode
; /* DOM node if mapped info item */
174 uint32 xml_class
; /* discriminates u, below */
175 uint32 xml_flags
; /* flags, see below */
183 JS_STATIC_ASSERT(sizeof(JSXML
) % JS_GCTHING_ALIGN
== 0);
185 /* union member shorthands */
186 #define xml_kids u.list.kids
187 #define xml_target u.list.target
188 #define xml_targetprop u.list.targetprop
189 #define xml_namespaces u.elem.namespaces
190 #define xml_attrs u.elem.attrs
191 #define xml_value u.value
193 /* xml_flags values */
194 #define XMLF_WHITESPACE_TEXT 0x1
196 /* xml_class-testing macros */
197 #define JSXML_HAS_KIDS(xml) JSXML_CLASS_HAS_KIDS((xml)->xml_class)
198 #define JSXML_HAS_VALUE(xml) JSXML_CLASS_HAS_VALUE((xml)->xml_class)
199 #define JSXML_HAS_NAME(xml) JSXML_CLASS_HAS_NAME((xml)->xml_class)
200 #define JSXML_LENGTH(xml) (JSXML_CLASS_HAS_KIDS((xml)->xml_class) \
201 ? (xml)->xml_kids.length \
205 js_NewXML(JSContext
*cx
, JSXMLClass xml_class
);
208 js_TraceXML(JSTracer
*trc
, JSXML
*xml
);
211 js_FinalizeXML(JSContext
*cx
, JSXML
*xml
);
214 js_NewXMLObject(JSContext
*cx
, JSXMLClass xml_class
);
217 js_GetXMLObject(JSContext
*cx
, JSXML
*xml
);
219 extern JS_FRIEND_DATA(js::Class
) js_XMLClass
;
220 extern JS_FRIEND_DATA(js::Class
) js_NamespaceClass
;
221 extern JS_FRIEND_DATA(js::Class
) js_QNameClass
;
222 extern JS_FRIEND_DATA(js::Class
) js_AttributeNameClass
;
223 extern JS_FRIEND_DATA(js::Class
) js_AnyNameClass
;
224 extern js::Class js_XMLFilterClass
;
227 * Methods to test whether an object or a value is of type "xml" (per typeof).
230 JSObject::isXML() const
232 return getClass() == &js_XMLClass
;
236 JSObject::isXMLId() const
238 js::Class
*clasp
= getClass();
239 return clasp
== &js_QNameClass
||
240 clasp
== &js_AttributeNameClass
||
241 clasp
== &js_AnyNameClass
;
244 #define VALUE_IS_XML(v) (!JSVAL_IS_PRIMITIVE(v) && JSVAL_TO_OBJECT(v)->isXML())
247 JSObject::isNamespace() const
249 return getClass() == &js_NamespaceClass
;
253 JSObject::isQName() const
255 js::Class
* clasp
= getClass();
256 return clasp
== &js_QNameClass
||
257 clasp
== &js_AttributeNameClass
||
258 clasp
== &js_AnyNameClass
;
262 IsXML(const js::Value
&v
)
264 return v
.isObject() && v
.toObject().isXML();
268 js_InitNamespaceClass(JSContext
*cx
, JSObject
*obj
);
271 js_InitQNameClass(JSContext
*cx
, JSObject
*obj
);
274 js_InitAttributeNameClass(JSContext
*cx
, JSObject
*obj
);
277 js_InitAnyNameClass(JSContext
*cx
, JSObject
*obj
);
280 js_InitXMLClass(JSContext
*cx
, JSObject
*obj
);
283 js_InitXMLClasses(JSContext
*cx
, JSObject
*obj
);
286 js_GetFunctionNamespace(JSContext
*cx
, js::Value
*vp
);
289 * If obj is QName corresponding to function::name, set *funidp to name's id,
290 * otherwise set *funidp to void.
293 js_IsFunctionQName(JSContext
*cx
, JSObject
*obj
, jsid
*funidp
);
296 js_GetDefaultXMLNamespace(JSContext
*cx
, jsval
*vp
);
299 js_SetDefaultXMLNamespace(JSContext
*cx
, const js::Value
&v
);
302 * Return true if v is a XML QName object, or if it converts to a string that
303 * contains a valid XML qualified name (one containing no :), false otherwise.
304 * NB: This function is an infallible predicate, it hides exceptions.
307 js_IsXMLName(JSContext
*cx
, jsval v
);
310 js_ToAttributeName(JSContext
*cx
, js::Value
*vp
);
313 js_EscapeAttributeValue(JSContext
*cx
, JSString
*str
, JSBool quote
);
316 js_AddAttributePart(JSContext
*cx
, JSBool isName
, JSString
*str
,
320 js_EscapeElementValue(JSContext
*cx
, JSString
*str
);
323 js_ValueToXMLString(JSContext
*cx
, const js::Value
&v
);
326 js_ConstructXMLQNameObject(JSContext
*cx
, const js::Value
& nsval
,
327 const js::Value
& lnval
);
330 js_GetAnyName(JSContext
*cx
, jsid
*idp
);
333 * Note: nameval must be either QName, AttributeName, or AnyName.
336 js_FindXMLProperty(JSContext
*cx
, const js::Value
&nameval
, JSObject
**objp
, jsid
*idp
);
339 js_GetXMLMethod(JSContext
*cx
, JSObject
*obj
, jsid id
, js::Value
*vp
);
342 js_GetXMLDescendants(JSContext
*cx
, JSObject
*obj
, jsval id
, jsval
*vp
);
345 js_DeleteXMLListElements(JSContext
*cx
, JSObject
*listobj
);
348 js_StepXMLListFilter(JSContext
*cx
, JSBool initialized
);
351 js_ValueToXMLObject(JSContext
*cx
, const js::Value
&v
);
354 js_ValueToXMLListObject(JSContext
*cx
, const js::Value
&v
);
357 js_NewXMLSpecialObject(JSContext
*cx
, JSXMLClass xml_class
, JSString
*name
,
361 js_MakeXMLCDATAString(JSContext
*cx
, JSString
*str
);
364 js_MakeXMLCommentString(JSContext
*cx
, JSString
*str
);
367 js_MakeXMLPIString(JSContext
*cx
, JSString
*name
, JSString
*str
);
369 /* The caller must ensure that either v1 or v2 is an object. */
371 js_TestXMLEquality(JSContext
*cx
, const js::Value
&v1
, const js::Value
&v2
,
375 js_ConcatenateXML(JSContext
*cx
, JSObject
*obj1
, JSObject
*obj2
, js::Value
*vp
);
377 #endif /* jsxml_h___ */