Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 6 additions & 8 deletions src/stanza.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import Builder from './builder.js';
import log from './log.js';
import { getFirstElementChild, getParserError, stripWhitespace, xmlHtmlNode, xmlescape } from './utils.js';

class UnsafeXML extends String {}

/**
* A Stanza represents a XML element used in XMPP (commonly referred to as stanzas).
*/
Expand Down Expand Up @@ -34,7 +36,7 @@ export class Stanza extends Builder {
* untrusted input.
*
* @param {string} string
* @returns {Builder}
* @returns {UnsafeXML}
* @example
* const status = '<status>I am busy!</status>';
* const pres = stx`
Expand All @@ -45,7 +47,7 @@ export class Stanza extends Builder {
* connection.send(pres);
*/
static unsafeXML(string) {
return Builder.fromString(string);
return new UnsafeXML(string);
}

/**
Expand Down Expand Up @@ -94,12 +96,8 @@ export class Stanza extends Builder {
acc +
str +
(Array.isArray(value)
? value
.map((v) =>
v instanceof Stanza || v instanceof Builder ? v : xmlescape(v.toString())
)
.join('')
: value instanceof Stanza || value instanceof Builder
? value.map((v) => (v instanceof Builder ? v : xmlescape(v.toString()))).join('')
: value instanceof UnsafeXML || value instanceof Builder
? value
: xmlescape((value ?? '').toString()))
);
Expand Down
7 changes: 5 additions & 2 deletions src/types/stanza.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export class Stanza extends Builder {
* untrusted input.
*
* @param {string} string
* @returns {Builder}
* @returns {UnsafeXML}
* @example
* const status = '<status>I am busy!</status>';
* const pres = stx`
Expand All @@ -44,7 +44,7 @@ export class Stanza extends Builder {
* </presence>`;
* connection.send(pres);
*/
static unsafeXML(string: string): Builder;
static unsafeXML(string: string): UnsafeXML;
/**
* Turns the passed-in string into an XML Element.
* @param {string} string
Expand All @@ -60,4 +60,7 @@ export class Stanza extends Builder {
#private;
}
import Builder from './builder.js';
declare class UnsafeXML extends String {
}
export {};
//# sourceMappingURL=stanza.d.ts.map