diff --git a/packages/html-parser/src/html-parser.test.ts b/packages/html-parser/src/html-parser.test.ts
index d159375b..c780ff79 100644
--- a/packages/html-parser/src/html-parser.test.ts
+++ b/packages/html-parser/src/html-parser.test.ts
@@ -341,6 +341,14 @@ describe('HtmlMessageEntityParser', () => {
)
})
+ it('should keep whitespaces in raw text', () => {
+ const dot = { text: ' ∙ ' }
+ const lf = { text: '\n' }
+ test(htm`this is${dot}some text${lf}xd`, [], 'this is ∙ some text\nxd')
+
+ test(htm`hewwo ${htm`
`} world`, [], 'hewwo \nworld')
+ })
+
it('should not ignore newlines and indentation in pre', () => {
test(
htm`
this is some text\n\nwith newlines`, diff --git a/packages/html-parser/src/index.ts b/packages/html-parser/src/index.ts index 705dfeec..a6c7752c 100644 --- a/packages/html-parser/src/index.ts +++ b/packages/html-parser/src/index.ts @@ -27,10 +27,10 @@ function parse( let plainText = '' let pendingText = '' - function processPendingText(tagEnd = false) { + function processPendingText(tagEnd = false, keepWhitespace = false) { if (!pendingText.length) return - if (!stacks.pre?.length) { + if (!stacks.pre?.length && !keepWhitespace) { pendingText = pendingText.replace(/[^\S\u00A0]+/gs, ' ') if (tagEnd) pendingText = pendingText.trimEnd() @@ -237,6 +237,8 @@ function parse( entities.push({ ...ent, offset: ent.offset + baseOffset }) } } + + processPendingText(false, true) } })