Strophe.js
    Preparing search index...

    Class 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.

    // 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>

    Hierarchy (View Summary)

    Index
    • The attributes should be passed in object notation.

      Parameters

      • name: string

        The name of the root element.

      • Optionalattrs: StanzaAttrs

        The attributes for the root element in object notation.

      Returns Builder

      const b = new Builder('message', {to: 'you', from: 'me'});
      
      const b = new Builder('messsage', {'xml:lang': 'en'});
      
    • 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.

      Parameters

      • moreattrs: Record<string, string | number | null>

        The attributes to add/modify in object notation. If an attribute is set to null or undefined, it will be removed.

      Returns Builder

      The Strophe.Builder object.

    • 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.

      Parameters

      • name: string

        The name of the child.

      • Optionalattrs: string | Record<string, string>

        The attributes of the child in object notation.

      • Optionaltext: string

        The text to add to the child.

      Returns Builder

      The Strophe.Builder object.

    • 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.

      Parameters

      • elem: Element | Builder

        A DOM element.

      Returns Builder

      The Strophe.Builder object.

    • Replace current element contents with the HTML passed in.

      This does not make the child the new current element

      Parameters

      • html: string

        The html to insert as contents of current element.

      Returns Builder

      The Strophe.Builder object.

    • 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 Builder

      The Strophe.Builder object.

    • Add a child text element.

      This does not make the child the new current element since there are no children of text elements.

      Parameters

      • text: string

        The text data to append to the current element.

      Returns Builder

      The Strophe.Builder object.

    • 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 string

      The serialized DOM tree in a String.

    • 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 Element

      The DOM tree as a element object.

    • Make the current parent element the new current element. This function is often used after c() to traverse back up the tree.

      Returns Builder

      The Strophe.Builder object.

      // For example, to add two children to the same element
      builder.c('child1', {}).up().c('child2', {});
    • Creates a new Builder object from an XML string.

      Parameters

      • str: string

      Returns Builder

      const stanza = Builder.fromString('<presence from="juliet@example.com/chamber"></presence>');
      
    • Render a DOM element and all descendants to a String.

      Parameters

      • elem: Element | Builder

        A DOM element.

      Returns string

      The serialized element tree as a String.