Skip to content

Commit

Permalink
Avoid inserting commas when nesting a list of stx templates
Browse files Browse the repository at this point in the history
Also add tests for `stx`.
  • Loading branch information
jcbrand committed Dec 8, 2024
1 parent 780967d commit 1f322fa
Show file tree
Hide file tree
Showing 8 changed files with 285 additions and 19 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
# Strophe.js Change Log

## Version 3.0.2 - (Unreleased)

- Avoid inserting commas when nesting lists of `stx` templates.
- Remove deprecated `abab` package
- Make sure `ConnectionOptions` type is exportable
- fix: invert default and types exports

## Version 3.0.1 - (2024-08-15)

* Bugfix: `Package path . is not exported from package`
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,9 @@ Since version 1.3.0, support for IE < 11 has been dropped.
### React Native

Since version 1.6.0 the [WebCrypto](https://developer.mozilla.org/en-US/docs/Web/API/Web_Crypto_API)
API (included by default in Browsers and NodeJS) is used for crypto primitives such as hashing and signatures.
API (included by default in Browsers and NodeJS) is used for crypto primitives
such as hashing and signatures.

Unfortunately this API is not available in React Native, and integrators will
need to look for a 3rd party implementations of this API if they want to use
Strophe there.
Expand Down
7 changes: 6 additions & 1 deletion karma.conf.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,12 @@ module.exports = function (config) {
frameworks: ['qunit'],

// list of files / patterns to load in the browser
files: ['node_modules/sinon/pkg/sinon.js', 'dist/strophe.umd.js', 'tests/tests.js'],
files: [
'node_modules/sinon/pkg/sinon.js',
'dist/strophe.umd.js',
'tests/tests.js',
'tests/stx.js'
],

// list of files to exclude
exclude: [],
Expand Down
13 changes: 9 additions & 4 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import Websocket from './websocket.js';
import WorkerWebsocket from './worker-websocket.js';
import log from './log.js';
import { ElementType, ErrorCondition, LOG_LEVELS, NS, Status, XHTML } from './constants.js';
import { stx, toStanza, Stanza } from './stanza.js';
import { stx, toStanzaElement, Stanza } from './stanza.js';

/**
* A container for all Strophe library functions.
Expand Down Expand Up @@ -115,6 +115,7 @@ const Strophe = {
SASLExternal,
SASLXOAuth2,

Stanza,
Builder,
ElementType,
ErrorCondition,
Expand All @@ -140,7 +141,7 @@ const Strophe = {
* @return {string} - The serialized element tree as a String.
*/
serialize(elem) {
return Builder.serialize(elem)
return Builder.serialize(elem);
},

/**
Expand Down Expand Up @@ -184,6 +185,10 @@ globalThis.$iq = $iq;
globalThis.$msg = $msg;
globalThis.$pres = $pres;
globalThis.Strophe = Strophe;
globalThis.toStanza = toStanza;
globalThis.toStanzaElement = toStanzaElement;
globalThis.stx = stx;

export { Builder, $build, $iq, $msg, $pres, Strophe, Stanza, stx, toStanza, Request };
globalThis.toStanza = toStanzaElement; // Deprecated

export { Builder, $build, $iq, $msg, $pres, Strophe, Stanza, stx, toStanzaElement, Request };
export { toStanzaElement as toStanza }
30 changes: 23 additions & 7 deletions src/stanza.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ const PARSE_ERROR_NS = 'http://www.w3.org/1999/xhtml';
/**
* @param {string} string
* @param {boolean} [throwErrorIfInvalidNS]
* @return {Element}
* @returns {Element}
*/
export function toStanza(string, throwErrorIfInvalidNS) {
export function toStanzaElement(string, throwErrorIfInvalidNS) {
const doc = xmlHtmlNode(string);

if (doc.getElementsByTagNameNS(PARSE_ERROR_NS, 'parsererror').length) {
Expand Down Expand Up @@ -52,8 +52,8 @@ export class Stanza {
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;
const value = this.values.length > idx ? this.values[idx] : '';
return acc + str + (Array.isArray(value) ? value.join('') : value.toString());
}, '');
return this.string;
}
Expand All @@ -62,17 +62,33 @@ export class Stanza {
* @return {Element}
*/
tree() {
this.node = this.node ?? toStanza(this.toString(), true);
this.node = this.node ?? toStanzaElement(this.toString(), true);
return this.node;
}
}

/**
* Tagged template literal function which generates {@link Stanza } objects
* @example stx`<presence type="${type}" xmlns="jabber:client"><show>${show}</show></presence>`
* Tagged template literal function which generates {@link Stanza} objects
*
* @example
* const pres = stx`<presence type="${type}" xmlns="jabber:client"><show>${show}</show></presence>`
*
* connection.send(msg);
*
* @example
* const msg = stx`<message
* from='[email protected]'
* id='hgn27af1'
* to='[email protected]'
* type='chat'>
* <body>Hello world</body>
* </message>`;
*
* connection.send(msg);
*
* @param {string[]} strings
* @param {...any} values
* @returns {Stanza}
*/
export function stx(strings, ...values) {
return new Stanza(strings, values);
Expand Down
5 changes: 3 additions & 2 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ export const Strophe: {
SASLOAuthBearer: typeof SASLOAuthBearer;
SASLExternal: typeof SASLExternal;
SASLXOAuth2: typeof SASLXOAuth2;
Stanza: typeof Stanza;
Builder: typeof Builder;
ElementType: {
NORMAL: number;
Expand Down Expand Up @@ -200,7 +201,7 @@ export const Strophe: {
};
import { Stanza } from './stanza.js';
import { stx } from './stanza.js';
import { toStanza } from './stanza.js';
import { toStanzaElement } from './stanza.js';
import Request from './request.js';
import * as shims from './shims.js';
import Bosh from './bosh.js';
Expand All @@ -221,5 +222,5 @@ import SASLMechanism from './sasl.js';
import { Status } from './constants.js';
import TimedHandler from './timed-handler.js';
import * as utils from './utils.js';
export { Builder, $build, $iq, $msg, $pres, Stanza, stx, toStanza, Request };
export { Builder, $build, $iq, $msg, $pres, Stanza, stx, toStanzaElement, Request, toStanzaElement as toStanza };
//# sourceMappingURL=index.d.ts.map
24 changes: 20 additions & 4 deletions src/types/stanza.d.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,31 @@
/**
* @param {string} string
* @param {boolean} [throwErrorIfInvalidNS]
* @return {Element}
* @returns {Element}
*/
export function toStanza(string: string, throwErrorIfInvalidNS?: boolean): Element;
export function toStanzaElement(string: string, throwErrorIfInvalidNS?: boolean): Element;
/**
* Tagged template literal function which generates {@link Stanza } objects
* @example stx`<presence type="${type}" xmlns="jabber:client"><show>${show}</show></presence>`
* Tagged template literal function which generates {@link Stanza} objects
*
* @example
* const pres = stx`<presence type="${type}" xmlns="jabber:client"><show>${show}</show></presence>`
*
* connection.send(msg);
*
* @example
* const msg = stx`<message
* from='[email protected]'
* id='hgn27af1'
* to='[email protected]'
* type='chat'>
* <body>Hello world</body>
* </message>`;
*
* connection.send(msg);
*
* @param {string[]} strings
* @param {...any} values
* @returns {Stanza}
*/
export function stx(strings: string[], ...values: any[]): Stanza;
/**
Expand Down
Loading

0 comments on commit 1f322fa

Please sign in to comment.