Skip to content

Commit

Permalink
Strip unnecessary whitespace from Stanza elements
Browse files Browse the repository at this point in the history
  • Loading branch information
jcbrand committed Dec 29, 2024
1 parent 715a036 commit 9b643a4
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Strophe.js Change Log

## Version 3.1.1 - (Unreleased)

- Strip unnecessary whitespace from `Stanza` elements
- Bugfix. Handle `null` and `undefined` values inside `stx` tagged template literals.

## Version 3.1.0 - (2024-12-16)

- **Security Fix**: Escape values passed to the `stx` tagged template literal
Expand Down
4 changes: 2 additions & 2 deletions src/stanza.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Builder from './builder.js';
import log from './log.js';
import { getFirstElementChild, getParserError, xmlHtmlNode, xmlescape } from './utils.js';
import { getFirstElementChild, getParserError, stripWhitespace, xmlHtmlNode, xmlescape } from './utils.js';

/**
* A Stanza represents a XML element used in XMPP (commonly referred to as stanzas).
Expand Down Expand Up @@ -61,7 +61,7 @@ export class Stanza extends Builder {
throw new Error(`Parser Error: ${parserError}`);
}

const node = getFirstElementChild(doc);
const node = stripWhitespace(getFirstElementChild(doc));
if (
['message', 'iq', 'presence'].includes(node.nodeName.toLowerCase()) &&
node.namespaceURI !== 'jabber:client' &&
Expand Down
1 change: 1 addition & 0 deletions src/types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ export const Strophe: {
}): void;
xmlGenerator(): Document;
xmlTextNode(text: string): Text;
stripWhitespace(stanza: Element): Element;
xmlHtmlNode(text: string): XMLDocument;
getParserError(doc: XMLDocument): string | null;
getFirstElementChild(el: XMLDocument): Element;
Expand Down
5 changes: 5 additions & 0 deletions src/types/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ export function xmlGenerator(): Document;
* @return {Text} - A new XML DOM text node.
*/
export function xmlTextNode(text: string): Text;
/**
* @param {Element} stanza
* @return {Element}
*/
export function stripWhitespace(stanza: Element): Element;
/**
* Creates an XML DOM node.
* @param {string} text - The contents of the XML element.
Expand Down
25 changes: 25 additions & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,31 @@ export function xmlTextNode(text) {
return xmlGenerator().createTextNode(text);
}

/**
* @param {Element} stanza
* @return {Element}
*/
export function stripWhitespace(stanza) {
const childNodes = Array.from(stanza.childNodes);
if (childNodes.length === 1 && childNodes[0].nodeType === ElementType.TEXT) {
// If the element has only one child and it's a text node, we assume
// it's significant and don't remove it, even if it's only whitespace.
return stanza;
}
childNodes.forEach((node) => {
if (node.nodeName.toLowerCase() === 'body') {
// We don't remove anything inside <body> elements
return;
}
if (node.nodeType === ElementType.TEXT && !(/\S/).test(node.nodeValue)) {
stanza.removeChild(node);
} else if (node.nodeType === ElementType.NORMAL) {
stripWhitespace(/** @type {Element} */ (node));
}
});
return stanza;
}

/**
* Creates an XML DOM node.
* @param {string} text - The contents of the XML element.
Expand Down

0 comments on commit 9b643a4

Please sign in to comment.