Skip to content

Commit

Permalink
prevent top-level text being discarded - fixes #3027
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jun 25, 2019
1 parent fc32147 commit 960b548
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 10 deletions.
3 changes: 0 additions & 3 deletions src/compiler/compile/nodes/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import { INode } from './interfaces';
export default class Text extends Node {
type: 'Text';
data: string;
use_space = false;

constructor(component: Component, parent: INode, scope: TemplateScope, info: any) {
super(component, parent, scope, info);
Expand All @@ -20,8 +19,6 @@ export default class Text extends Node {
}
node = node.parent;
}

this.use_space = true;
}
}
}
2 changes: 1 addition & 1 deletion src/compiler/compile/render_dom/wrappers/Element/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ export default class ElementWrapper extends Wrapper {

function to_html(wrapper: ElementWrapper | TextWrapper) {
if (wrapper.node.type === 'Text') {
if (wrapper.node.use_space) return ' ';
if ((wrapper as TextWrapper).use_space()) return ' ';

const parent = wrapper.node.parent as Element;

Expand Down
9 changes: 4 additions & 5 deletions src/compiler/compile/render_dom/wrappers/Fragment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import { INode } from '../../nodes/interfaces';
import Renderer from '../Renderer';
import Block from '../Block';
import { trim_start, trim_end } from '../../../utils/trim';
import TextWrapper from './Text';

const wrappers = {
AwaitBlock,
Expand Down Expand Up @@ -47,7 +48,7 @@ function trimmable_at(child: INode, next_sibling: Wrapper): boolean {
// The child and its sibling share a common nearest each block (not at an each block boundary)
// The next sibling's previous node is an each block
return (next_sibling.node.find_nearest(/EachBlock/) === child.find_nearest(/EachBlock/)) || next_sibling.node.prev.type === 'EachBlock';
}
}

export default class FragmentWrapper {
nodes: Wrapper[];
Expand Down Expand Up @@ -91,8 +92,7 @@ export default class FragmentWrapper {
// *unless* there is no whitespace between this node and its next sibling
if (this.nodes.length === 0) {
const should_trim = (
// @ts-ignore todo: probably error, should it be next_sibling.node.data?
next_sibling ? (next_sibling.node.type === 'Text' && /^\s/.test(next_sibling.data) && trimmable_at(child, next_sibling)) : !child.has_ancestor('EachBlock')
next_sibling ? (next_sibling.node.type === 'Text' && /^\s/.test(next_sibling.node.data) && trimmable_at(child, next_sibling)) : !child.has_ancestor('EachBlock')
);

if (should_trim) {
Expand All @@ -103,8 +103,7 @@ export default class FragmentWrapper {

// glue text nodes (which could e.g. be separated by comments) together
if (last_child && last_child.node.type === 'Text') {
// @ts-ignore todo: probably error, should it be last_child.node.data?
last_child.data = data + last_child.data;
(last_child as TextWrapper).data = data + (last_child as TextWrapper).data;
continue;
}

Expand Down
17 changes: 16 additions & 1 deletion src/compiler/compile/render_dom/wrappers/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,27 @@ export default class TextWrapper extends Wrapper {
this.var = this.skip ? null : 't';
}

use_space() {
if (this.renderer.component.component_options.preserveWhitespace) return false;
if (/[\S\u00A0]/.test(this.data)) return false;

let node = this.parent && this.parent.node;
while (node) {
if (node.type === 'Element' && node.name === 'pre') {
return false;
}
node = node.parent;
}

return true;
}

render(block: Block, parent_node: string, parent_nodes: string) {
if (this.skip) return;

block.add_element(
this.var,
this.node.use_space ? `@space()` : `@text(${stringify(this.data)})`,
this.use_space() ? `@space()` : `@text(${stringify(this.data)})`,
parent_nodes && `@claim_text(${parent_nodes}, ${stringify(this.data)})`,
parent_node
);
Expand Down
6 changes: 6 additions & 0 deletions test/runtime/samples/isolated-text/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export default {
html: `
before
<h1>after</h1>
`
};
5 changes: 5 additions & 0 deletions test/runtime/samples/isolated-text/main.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
before

<!---->

<h1>after</h1>

0 comments on commit 960b548

Please sign in to comment.