diff --git a/src/compiler/compile/nodes/Text.ts b/src/compiler/compile/nodes/Text.ts index a3ba4b9e85..a4514f56f2 100644 --- a/src/compiler/compile/nodes/Text.ts +++ b/src/compiler/compile/nodes/Text.ts @@ -12,7 +12,7 @@ export default class Text extends Node { super(component, parent, scope, info); this.data = info.data; - if (!component.component_options.preserveWhitespace) { + if (!component.component_options.preserveWhitespace && !/[\S\u00A0]/.test(info.data)) { let node = parent; while (node) { if (node.type === 'Element' && node.name === 'pre') { @@ -21,7 +21,7 @@ export default class Text extends Node { node = node.parent; } - + this.use_space = true; } } } diff --git a/src/compiler/compile/render-dom/wrappers/Text.ts b/src/compiler/compile/render-dom/wrappers/Text.ts index c83fd38f2c..ceacae29ba 100644 --- a/src/compiler/compile/render-dom/wrappers/Text.ts +++ b/src/compiler/compile/render-dom/wrappers/Text.ts @@ -54,7 +54,7 @@ export default class TextWrapper extends Wrapper { block.add_element( this.var, - `@text(${stringify(this.data)})`, + this.node.use_space ? `@space()` : `@text(${stringify(this.data)})`, parent_nodes && `@claim_text(${parent_nodes}, ${stringify(this.data)})`, parent_node ); diff --git a/src/runtime/internal/dom.ts b/src/runtime/internal/dom.ts index 05e18e2bcd..f65d07117c 100644 --- a/src/runtime/internal/dom.ts +++ b/src/runtime/internal/dom.ts @@ -62,6 +62,9 @@ export function text(data: string) { return document.createTextNode(data); } +export function space() { + return text(' '); +} export function empty() { return text('');