-
Notifications
You must be signed in to change notification settings - Fork 366
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the ability to create stanzas via tagged template literal
- Loading branch information
Showing
5 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import Strophe from './core.js'; | ||
|
||
const PARSE_ERROR_NS = 'http://www.w3.org/1999/xhtml'; | ||
|
||
/** | ||
* @param { string } string | ||
* @param { boolean } [throwErrorIfInvalidNS] | ||
* @return { Element } | ||
*/ | ||
export function toStanza (string, throwErrorIfInvalidNS) { | ||
const doc = Strophe.xmlHtmlNode(string); | ||
|
||
if (doc.getElementsByTagNameNS(PARSE_ERROR_NS, 'parsererror').length) { | ||
throw new Error(`Parser Error: ${string}`); | ||
} | ||
|
||
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}`; | ||
if (throwErrorIfInvalidNS) { | ||
throw new Error(err_msg); | ||
} else { | ||
Strophe.log(Strophe.LogLevel.ERROR, err_msg); | ||
} | ||
} | ||
return node; | ||
} | ||
|
||
/** | ||
* A Stanza represents a XML element used in XMPP (commonly referred to as | ||
* stanzas). | ||
*/ | ||
class Stanza { | ||
|
||
/** | ||
* @param { string[] } strings | ||
* @param { any[] } values | ||
*/ | ||
constructor (strings, values) { | ||
this.strings = strings; | ||
this.values = values; | ||
} | ||
|
||
/** | ||
* @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].toString() : ''; | ||
return acc + str + value; | ||
}, ''); | ||
return this.string; | ||
} | ||
|
||
/** | ||
* @return { Element } | ||
*/ | ||
tree () { | ||
this.node = this.node ?? toStanza(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>` | ||
* | ||
* @param { string[] } strings | ||
* @param { ...any } values | ||
*/ | ||
export function stx (strings, ...values) { | ||
return new Stanza(strings, values); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
/** | ||
* @param { string } string | ||
* @param { boolean } [throwErrorIfInvalidNS] | ||
* @return { Element } | ||
*/ | ||
export function toStanza(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>` | ||
* | ||
* @param { string[] } strings | ||
* @param { ...any } values | ||
*/ | ||
export function stx(strings: string[], ...values: any[]): Stanza; | ||
/** | ||
* A Stanza represents a XML element used in XMPP (commonly referred to as | ||
* stanzas). | ||
*/ | ||
declare class Stanza { | ||
/** | ||
* @param { string[] } strings | ||
* @param { any[] } values | ||
*/ | ||
constructor(strings: string[], values: any[]); | ||
strings: string[]; | ||
values: any[]; | ||
/** | ||
* @return { string } | ||
*/ | ||
toString(): string; | ||
string: any; | ||
/** | ||
* @return { Element } | ||
*/ | ||
tree(): Element; | ||
node: any; | ||
} | ||
export {}; | ||
//# sourceMappingURL=stanza.d.ts.map |