Trims only the first leading space in a SSE field value (#3588) (#3589)

This commit is contained in:
Anton Lorani
2026-01-20 22:06:19 +01:00
committed by GitHub
parent d3faddb0e0
commit 54bf849fc9
2 changed files with 21 additions and 1 deletions

View File

@@ -723,7 +723,9 @@ var htmx = (() => {
if (colonIndex <= 0) continue;
let field = line.slice(0, colonIndex);
let value = line.slice(colonIndex + 1).trimStart();
let value = line.slice(colonIndex + 1)
if (value[0] === ' ') value = value.slice(1);
if (field === 'data') {
message.data += (message.data ? '\n' : '') + value;

View File

@@ -458,4 +458,22 @@ describe('Server-Sent Events', function() {
stream.close();
});
it('only trims single space after colon', async function() {
const stream = mockStreamResponse('/sse-whitespace');
createProcessedHTML('<button id="ws-btn" hx-get="/sse-whitespace" hx-swap="innerHTML">Whitespace</button>');
find('#ws-btn').click();
await htmx.timeout(1);
stream.send(' \ttext-with-two-leading-spaces');
await waitForEvent('htmx:after:sse:message');
assertTextContentIs('#ws-btn', ' \ttext-with-two-leading-spaces');
stream.send('NoTrim');
await waitForEvent('htmx:after:sse:message');
assertTextContentIs('#ws-btn', 'NoTrim');
stream.close();
});
});