UNPKG

strophe.js

Version:

Strophe.js is an XMPP library for JavaScript

181 lines 6.78 kB
export type StanzaAttrs = Record<string, string | number>; /** * Create a {@link Strophe.Builder} * This is an alias for `new Strophe.Builder(name, attrs)`. * @param name - The root element name. * @param attrs - The attributes for the root element in object notation. * @returns A new Strophe.Builder object. */ export declare function $build(name: string, attrs?: StanzaAttrs): Builder; /** * Create a {@link Strophe.Builder} with a `<message/>` element as the root. * @param attrs - The <message/> element attributes in object notation. * @returns A new Strophe.Builder object. */ export declare function $msg(attrs?: Record<string, string>): Builder; /** * Create a {@link Strophe.Builder} with an `<iq/>` element as the root. * @param attrs - The <iq/> element attributes in object notation. * @returns A new Strophe.Builder object. */ export declare function $iq(attrs?: Record<string, string>): Builder; /** * Create a {@link Strophe.Builder} with a `<presence/>` element as the root. * @param attrs - The <presence/> element attributes in object notation. * @returns A new Strophe.Builder object. */ export declare function $pres(attrs?: Record<string, string>): Builder; /** * This class provides an interface similar to JQuery but for building * DOM elements easily and rapidly. All the functions except for `toString()` * and tree() return the object, so calls can be chained. * * The corresponding DOM manipulations to get a similar fragment would be * a lot more tedious and probably involve several helper variables. * * Since adding children makes new operations operate on the child, up() * is provided to traverse up the tree. To add two children, do * > builder.c('child1', ...).up().c('child2', ...) * * The next operation on the Builder will be relative to the second child. * * @example * // Here's an example using the $iq() builder helper. * $iq({to: 'you', from: 'me', type: 'get', id: '1'}) * .c('query', {xmlns: 'strophe:example'}) * .c('example') * .toString() * * // The above generates this XML fragment * // <iq to='you' from='me' type='get' id='1'> * // <query xmlns='strophe:example'> * // <example/> * // </query> * // </iq> */ declare class Builder { #private; /** * The attributes should be passed in object notation. * @param name - The name of the root element. * @param attrs - The attributes for the root element in object notation. * @example const b = new Builder('message', {to: 'you', from: 'me'}); * @example const b = new Builder('messsage', {'xml:lang': 'en'}); */ constructor(name: string, attrs?: StanzaAttrs); /** * Creates a new Builder object from an XML string. * @param str * @returns * @example const stanza = Builder.fromString('<presence from="juliet@example.com/chamber"></presence>'); */ static fromString(str: string): Builder; buildTree(): Element | null; get nodeTree(): Element; get node(): Element; set node(el: Element); /** * Render a DOM element and all descendants to a String. * @param elem - A DOM element. * @returns The serialized element tree as a String. */ static serialize(elem: Element | Builder | null): string | null; /** * Return the DOM tree. * * This function returns the current DOM tree as an element object. This * is suitable for passing to functions like Strophe.Connection.send(). * * @returns The DOM tree as a element object. */ tree(): Element; /** * Serialize the DOM tree to a String. * * This function returns a string serialization of the current DOM * tree. It is often used internally to pass data to a * Strophe.Request object. * * @returns The serialized DOM tree in a String. */ toString(): string; /** * Make the current parent element the new current element. * This function is often used after c() to traverse back up the tree. * * @example * // For example, to add two children to the same element * builder.c('child1', {}).up().c('child2', {}); * * @returns The Strophe.Builder object. */ up(): Builder; /** * Make the root element the new current element. * * When at a deeply nested element in the tree, this function can be used * to jump back to the root of the tree, instead of having to repeatedly * call up(). * * @returns The Strophe.Builder object. */ root(): Builder; /** * Add or modify attributes of the current element. * * The attributes should be passed in object notation. * This function does not move the current element pointer. * @param moreattrs - The attributes to add/modify in object notation. * If an attribute is set to `null` or `undefined`, it will be removed. * @returns The Strophe.Builder object. */ attrs(moreattrs: Record<string, string | number | null>): Builder; /** * Add a child to the current element and make it the new current * element. * * This function moves the current element pointer to the child, * unless text is provided. If you need to add another child, it * is necessary to use up() to go back to the parent in the tree. * * @param name - The name of the child. * @param attrs - The attributes of the child in object notation. * @param text - The text to add to the child. * * @returns The Strophe.Builder object. */ c(name: string, attrs?: Record<string, string> | string, text?: string): Builder; /** * Add a child to the current element and make it the new current * element. * * This function is the same as c() except that instead of using a * name and an attributes object to create the child it uses an * existing DOM element object. * * @param elem - A DOM element. * @returns The Strophe.Builder object. */ cnode(elem: Element | Builder): Builder; /** * Add a child text element. * * This *does not* make the child the new current element since there * are no children of text elements. * * @param text - The text data to append to the current element. * @returns The Strophe.Builder object. */ t(text: string): Builder; /** * Replace current element contents with the HTML passed in. * * This *does not* make the child the new current element * * @param html - The html to insert as contents of current element. * @returns The Strophe.Builder object. */ h(html: string): Builder; } export default Builder; //# sourceMappingURL=builder.d.ts.map