Strophe = {
/** Constants: XMPP Namespace Constants
* Common namespace constants from the XMPP RFCs and XEPs.
*
* NS.HTTPBIND - HTTP BIND namespace from XEP 124.
* NS.BOSH - BOSH namespace from XEP 206.
* NS.CLIENT - Main XMPP client namespace.
* NS.AUTH - Legacy authentication namespace.
* NS.ROSTER - Roster operations namespace.
* NS.PROFILE - Profile namespace.
* NS.DISCO_INFO - Service discovery info namespace from XEP 30.
* NS.DISCO_ITEMS - Service discovery items namespace from XEP 30.
* NS.MUC - Multi-User Chat namespace from XEP 45.
* NS.SASL - XMPP SASL namespace from RFC 3920.
* NS.STREAM - XMPP Streams namespace from RFC 3920.
* NS.BIND - XMPP Binding namespace from RFC 3920.
* NS.SESSION - XMPP Session namespace from RFC 3920.
*/
NS: {
HTTPBIND: "http://jabber.org/protocol/httpbind",
BOSH: "urn:xmpp:xbosh",
CLIENT: "jabber:client",
AUTH: "jabber:iq:auth",
ROSTER: "jabber:iq:roster",
PROFILE: "jabber:iq:profile",
DISCO_INFO: "http://jabber.org/protocol/disco#info",
DISCO_ITEMS: "http://jabber.org/protocol/disco#items",
MUC: "http://jabber.org/protocol/muc",
SASL: "urn:ietf:params:xml:ns:xmpp-sasl",
STREAM: "http://etherx.jabber.org/streams",
BIND: "urn:ietf:params:xml:ns:xmpp-bind",
SESSION: "urn:ietf:params:xml:ns:xmpp-session",
VERSION: "jabber:iq:version"
},
/** Constants: Connection Status Constants
* Connection status constants for use by the connection handler
* callback.
*
* Status.ERROR - An error has occurred
* Status.CONNECTING - The connection is currently being made
* Status.CONNFAIL - The connection attempt failed
* Status.AUTHENTICATING - The connection is authenticating
* Status.AUTHFAIL - The authentication attempt failed
* Status.CONNECTED - The connection has succeeded
* Status.DISCONNECTED - The connection has been terminated
* Status.DISCONNECTING - The connection is currently being terminated
*/
Status: {
ERROR: 0,
CONNECTING: 1,
CONNFAIL: 2,
AUTHENTICATING: 3,
AUTHFAIL: 4,
CONNECTED: 5,
DISCONNECTED: 6,
DISCONNECTING: 7
},
/** Constants: Log Level Constants
* Logging level indicators.
*
* LogLevel.DEBUG - Debug output
* LogLevel.INFO - Informational output
* LogLevel.WARN - Warnings
* LogLevel.ERROR - Errors
* LogLevel.FATAL - Fatal errors
*/
LogLevel: {
DEBUG: 0,
INFO: 1,
WARN: 2,
ERROR: 3,
FATAL: 4
},
/** PrivateConstants: DOM Element Type Constants
* DOM element types.
*
* ElementType.NORMAL - Normal element.
* ElementType.TEXT - Text data element.
*/
ElementType: {
NORMAL: 1,
TEXT: 3
},
/** PrivateConstants: Timeout Values
* Timeout values for error states. These values are in seconds.
* These should not be changed unless you know exactly what you are
* doing.
*
* TIMEOUT - Time to wait for a request to return. This defaults to
* 70 seconds.
* SECONDARY_TIMEOUT - Time to wait for immediate request return. This
* defaults to 7 seconds.
*/
TIMEOUT: 70,
SECONDARY_TIMEOUT: 7,
/** Function: forEachChild
* Map a function over some or all child elements of a given element.
*
* This is a small convenience function for mapping a function over
* some or all of the children of an element. If elemName is null, all
* children will be passed to the function, otherwise only children
* whose tag names match elemName will be passed.
*
* Parameters:
* (XMLElement) elem - The element to operate on.
* (String) elemName - The child element tag name filter.
* (Function) func - The function to apply to each child. This
* function should take a single argument, a DOM element.
*/
forEachChild: function (elem, elemName, func)
{
var i, childNode;
for (i = 0; i < elem.childNodes.length; i++) {
childNode = elem.childNodes[i];
if (childNode.nodeType == Strophe.ElementType.NORMAL &&
(!elemName || this.isTagEqual(childNode, elemName))) {
func(childNode);
}
}
},
/** Function: isTagEqual
* Compare an element's tag name with a string.
*
* This function is case insensitive.
*
* Parameters:
* (XMLElement) el - A DOM element.
* (String) name - The element name.
*
* Returns:
* true if the element's tag name matches _el_, and false
* otherwise.
*/
isTagEqual: function (el, name)
{
return el.tagName.toLowerCase() == name.toLowerCase();
},
/** Function: xmlElement
* Create an XML DOM element.
*
* This function creates an XML DOM element correctly across all
* implementations. Specifically the Microsoft implementation of
* document.createElement makes DOM elements with 43+ default attributes
* unless elements are created with the ActiveX object Microsoft.XMLDOM.
*
* Most DOMs force element names to lowercase, so we use the
* _realname attribute on the created element to store the case
* sensitive name. This is required to generate proper XML for
* things like vCard avatars (XEP 153). This attribute is stripped
* out before being sent over the wire or serialized, but you may
* notice it during debugging.
*
* Parameters:
* (String) name - The name for the element.
* (Array) attrs - An optional array of key/value pairs to use as
* element attributes in the following format [['key1', 'value1'],
* ['key2', 'value2']]
* (String) text - The text child data for the element.
*
* Returns:
* A new XML DOM element.
*/
xmlElement: function (name)
{
// FIXME: this should also support attrs argument in object notation
if (!name) { return null; }
var node = null;
if (window.ActiveXObject) {
node = new ActiveXObject("Microsoft.XMLDOM").createElement(name);
} else {
node = document.createElement(name);
}
// use node._realname to store the case-sensitive version of the tag
// name, since some browsers will force tagnames to all lowercase.
// this is needed for the <vCard/> tag in XMPP specifically.
if (node.tagName != name)
node.setAttribute("_realname", name);
// FIXME: this should throw errors if args are the wrong type or
// there are more than two optional args
var a, i;
for (a = 1; a < arguments.length; a++) {
if (!arguments[a]) { continue; }
if (typeof(arguments[a]) == "string" ||
typeof(arguments[a]) == "number") {
node.appendChild(Strophe.xmlTextNode(arguments[a]));
} else if (typeof(arguments[a]) == "object" &&
typeof(arguments[a]['sort']) == "function") {
for (i = 0; i < arguments[a].length; i++) {
if (typeof(arguments[a][i]) == "object" &&
typeof(arguments[a][i]['sort']) == "function") {
node.setAttribute(arguments[a][i][0],
arguments[a][i][1]);
}
}
}
}
return node;
},
最新发布