fix(html): keep whitespaces in raw text

This commit is contained in:
alina 🌸 2024-04-05 18:14:11 +03:00
parent d3ceed84d5
commit 869d5987c7
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
2 changed files with 12 additions and 2 deletions

View file

@ -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`<br>`} world`, [], 'hewwo \nworld')
})
it('should not ignore newlines and indentation in pre', () => {
test(
htm`<pre>this is some text\n\nwith newlines</pre>`,

View file

@ -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)
}
})