|
@@ -1,27 +1,63 @@
|
|
|
-const parser = new DOMParser();
|
|
|
-const parserErrorNS = parser.parseFromString('invalid', 'text/xml')
|
|
|
- .getElementsByTagName("parsererror")[0].namespaceURI;
|
|
|
+import log from '../log.js';
|
|
|
+import { Strophe } from 'strophe.js/src/strophe';
|
|
|
|
|
|
-export function toStanza (string) {
|
|
|
- const node = parser.parseFromString(string, "text/xml");
|
|
|
- if (node.getElementsByTagNameNS(parserErrorNS, 'parsererror').length) {
|
|
|
+const PARSE_ERROR_NS = 'http://www.w3.org/1999/xhtml';
|
|
|
+
|
|
|
+export function toStanza (string, throwErrorIfInvalidNS) {
|
|
|
+ const doc = Strophe.xmlHtmlNode(string);
|
|
|
+
|
|
|
+ if (doc.getElementsByTagNameNS(PARSE_ERROR_NS, 'parsererror').length) {
|
|
|
throw new Error(`Parser Error: ${string}`);
|
|
|
}
|
|
|
- return node.firstElementChild;
|
|
|
+
|
|
|
+ const node = doc.firstElementChild;
|
|
|
+
|
|
|
+ if (
|
|
|
+ ['message', 'iq', 'presence'].includes(node.nodeName.toLowerCase()) &&
|
|
|
+ node.namespaceURI !== 'jabber:client' &&
|
|
|
+ node.namespaceURI !== 'jabber:server'
|
|
|
+ ) {
|
|
|
+ const err_msg = `Invalid namespaceURI ${node.namespaceURI}`;
|
|
|
+ log.error(err_msg);
|
|
|
+ if (throwErrorIfInvalidNS) throw new Error(err_msg);
|
|
|
+ }
|
|
|
+ return node;
|
|
|
}
|
|
|
|
|
|
+/**
|
|
|
+ * A Stanza represents a XML element used in XMPP (commonly referred to as
|
|
|
+ * stanzas).
|
|
|
+ */
|
|
|
+class Stanza {
|
|
|
+
|
|
|
+ constructor (strings, values) {
|
|
|
+ this.strings = strings;
|
|
|
+ this.values = values;
|
|
|
+ }
|
|
|
+
|
|
|
+ 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].toString() : '';
|
|
|
+ return acc + str + value;
|
|
|
+ }, '');
|
|
|
+ return this.string;
|
|
|
+ }
|
|
|
+
|
|
|
+ tree () {
|
|
|
+ this.node = this.node ?? toStanza(this.toString(), true);
|
|
|
+ return this.node;
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
/**
|
|
|
- * Tagged template literal function which can be used to generate XML stanzas.
|
|
|
+ * Tagged template literal function which generates {@link Stanza } objects
|
|
|
+ *
|
|
|
* Similar to the `html` function, from Lit.
|
|
|
*
|
|
|
* @example stx`<presence type="${type}"><show>${show}</show></presence>`
|
|
|
*/
|
|
|
export function stx (strings, ...values) {
|
|
|
- return toStanza(
|
|
|
- strings.reduce((acc, str) => {
|
|
|
- const idx = strings.indexOf(str);
|
|
|
- return acc + str + (values.length > idx ? values[idx] : '')
|
|
|
- }, '')
|
|
|
- );
|
|
|
+ return new Stanza(strings, values);
|
|
|
}
|