Skip to content

Commit

Permalink
Let the Stanza class extend the Builder class.
Browse files Browse the repository at this point in the history
This enables better compatibility between the two and lets us use
Builder methods on Stanza classes.
  • Loading branch information
jcbrand committed Dec 11, 2024
1 parent 7765223 commit c2813e3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 21 deletions.
42 changes: 38 additions & 4 deletions src/builder.js
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,15 @@ class Builder {
* @property {string} [StanzaAttrs.xmlns]
*/

/** @type {Element} */
#nodeTree;
/** @type {Element} */
#node;
/** @type {string} */
#name;
/** @type {StanzaAttrs} */
#attrs;

/**
* The attributes should be passed in object notation.
* @param {string} name - The name of the root element.
Expand All @@ -89,10 +98,35 @@ class Builder {
attrs = { xmlns: NS.CLIENT };
}
}
// Holds the tree being built.
this.nodeTree = xmlElement(name, attrs);
// Points to the current operation node.
this.node = this.nodeTree;

this.#name = name;
this.#attrs = attrs;
}

buildTree() {
return xmlElement(this.#name, this.#attrs);
}

/** @return {Element} */
get nodeTree() {
if (!this.#nodeTree) {
// Holds the tree being built.
this.#nodeTree = this.buildTree();
}
return this.#nodeTree;
}

/** @return {Element} */
get node() {
if (!this.#node) {
this.#node = this.tree();
}
return this.#node;
}

/** @param {Element} el */
set node(el) {
this.#node = el;
}

/**
Expand Down
40 changes: 23 additions & 17 deletions src/stanza.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Builder from './builder.js';
import log from './log.js';
import { getFirstElementChild, getParserError, xmlHtmlNode } from './utils.js';

Expand Down Expand Up @@ -32,36 +33,41 @@ export function toStanzaElement(string, throwErrorIfInvalidNS) {
/**
* A Stanza represents a XML element used in XMPP (commonly referred to as stanzas).
*/
export class Stanza {
export class Stanza extends Builder {

/** @type {string} */
#string;
/** @type {Array<string>} */
#strings;
/** @type {Array<string|Stanza>} */
#values;

/**
* @param {string[]} strings
* @param {any[]} values
*/
constructor(strings, values) {
this.strings = strings;
this.values = values;
super('stanza');
this.#strings = strings;
this.#values = values;
}

buildTree() {
return toStanzaElement(this.toString(), true);
}

/**
* @return {string}
*/
toString() {
this.string =
this.string ||
this.strings.reduce((acc, str) => {
const idx = this.strings.indexOf(str);
const value = this.values.length > idx ? this.values[idx] : '';
this.#string =
this.#string ||
this.#strings.reduce((acc, str) => {
const idx = this.#strings.indexOf(str);
const value = this.#values.length > idx ? this.#values[idx] : '';
return acc + str + (Array.isArray(value) ? value.join('') : value.toString());
}, '');
return this.string;
}

/**
* @return {Element}
*/
tree() {
this.node = this.node ?? toStanzaElement(this.toString(), true);
return this.node;
return this.#string;
}
}

Expand Down

0 comments on commit c2813e3

Please sign in to comment.