Skip to content

collapse consecutive whitespace characters #2258

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions src/compile/Component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ type ComponentOptions = {
tag?: string;
immutable?: boolean;
accessors?: boolean;
preserveWhitespace?: boolean;
};

// We need to tell estree-walker that it should always
Expand Down Expand Up @@ -1195,7 +1196,8 @@ function process_component_options(component: Component, nodes) {
immutable: component.compile_options.immutable || false,
accessors: 'accessors' in component.compile_options
? component.compile_options.accessors
: !!component.compile_options.customElement
: !!component.compile_options.customElement,
preserveWhitespace: !!component.compile_options.preserveWhitespace
};

const node = nodes.find(node => node.name === 'svelte:options');
Expand Down Expand Up @@ -1271,6 +1273,7 @@ function process_component_options(component: Component, nodes) {

case 'accessors':
case 'immutable':
case 'preserveWhitespace':
const code = `invalid-${name}-value`;
const message = `${name} attribute must be true or false`
const value = get_value(attribute, code, message);
Expand All @@ -1291,7 +1294,7 @@ function process_component_options(component: Component, nodes) {
else {
component.error(attribute, {
code: `invalid-options-attribute`,
message: `<svelte:options> can only have static 'tag', 'namespace', 'accessors' and 'immutable' attributes`
message: `<svelte:options> can only have static 'tag', 'namespace', 'accessors', 'immutable' and 'preserveWhitespace' attributes`
});
}
});
Expand Down
3 changes: 2 additions & 1 deletion src/compile/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ const valid_options = [
'customElement',
'tag',
'css',
'preserveComments'
'preserveComments',
'preserveWhitespace'
];

function validate_options(options: CompileOptions, warnings: Warning[]) {
Expand Down
17 changes: 16 additions & 1 deletion src/compile/nodes/Text.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,26 @@
import Node from './shared/Node';
import Component from '../Component';
import TemplateScope from './shared/TemplateScope';

export default class Text extends Node {
type: 'Text';
data: string;
use_space = false;

constructor(component, parent, scope, info) {
constructor(component: Component, parent: Node, scope: TemplateScope, info: any) {
super(component, parent, scope, info);
this.data = info.data;

if (!component.component_options.preserveWhitespace && !/\S/.test(info.data)) {
let node = parent;
while (node) {
if (node.type === 'Element' && node.name === 'pre') {
return;
}
node = node.parent;
}

this.use_space = true;
}
}
}
2 changes: 1 addition & 1 deletion src/compile/render-dom/wrappers/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
);
Expand Down
3 changes: 2 additions & 1 deletion src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,8 @@ export interface CompileOptions {
tag?: string;
css?: boolean;

preserveComments?: boolean | false;
preserveComments?: boolean;
preserveWhitespace?: boolean;
}

export interface Visitor {
Expand Down
4 changes: 4 additions & 0 deletions src/internal/dom.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ export function text(data) {
return document.createTextNode(data);
}

export function space() {
return text(' ');
}

export function comment() {
return document.createComment('');
}
Expand Down
4 changes: 2 additions & 2 deletions test/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ function cleanChildren(node) {
node.removeChild(child);
}

child.data = child.data.replace(/\s{2,}/g, '\n');
child.data = child.data.replace(/\s+/g, '\n');

if (previous && previous.nodeType === 3) {
previous.data += child.data;
previous.data = previous.data.replace(/\s{2,}/g, '\n');
previous.data = previous.data.replace(/\s+/g, '\n');

node.removeChild(child);
child = previous;
Expand Down
3 changes: 2 additions & 1 deletion test/js/samples/debug-empty/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
noop,
safe_not_equal,
set_data,
space,
text
} from "svelte/internal";

Expand All @@ -24,7 +25,7 @@ function create_fragment(ctx) {
t0 = text("Hello ");
t1 = text(ctx.name);
t2 = text("!");
t3 = text("\n");
t3 = space();
debugger;
add_location(h1, file, 4, 0, 38);
},
Expand Down
5 changes: 3 additions & 2 deletions test/js/samples/debug-foo-bar-baz-things/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
noop,
safe_not_equal,
set_data,
space,
text
} from "svelte/internal";

Expand All @@ -30,7 +31,7 @@ function create_each_block(ctx) {
c: function create() {
span = element("span");
t0 = text(t0_value);
t1 = text("\n\t");
t1 = space();

{
const { foo, bar, baz, thing } = ctx;
Expand Down Expand Up @@ -84,7 +85,7 @@ function create_fragment(ctx) {
each_blocks[i].c();
}

t0 = text("\n\n");
t0 = space();
p = element("p");
t1 = text("foo: ");
t2 = text(ctx.foo);
Expand Down
5 changes: 3 additions & 2 deletions test/js/samples/debug-foo/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
noop,
safe_not_equal,
set_data,
space,
text
} from "svelte/internal";

Expand All @@ -30,7 +31,7 @@ function create_each_block(ctx) {
c: function create() {
span = element("span");
t0 = text(t0_value);
t1 = text("\n\t");
t1 = space();

{
const { foo } = ctx;
Expand Down Expand Up @@ -84,7 +85,7 @@ function create_fragment(ctx) {
each_blocks[i].c();
}

t0 = text("\n\n");
t0 = space();
p = element("p");
t1 = text("foo: ");
t2 = text(ctx.foo);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
noop,
safe_not_equal,
set_data,
space,
text
} from "svelte/internal";

Expand All @@ -22,7 +23,7 @@ function create_fragment(ctx) {
c: function create() {
p = element("p");
t0 = text(t0_value);
t1 = text("\n\t");
t1 = space();
t2 = text(ctx.bar);
add_location(p, file, 7, 0, 67);
},
Expand Down
4 changes: 2 additions & 2 deletions test/js/samples/do-use-dataset/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
insert,
noop,
safe_not_equal,
text
space
} from "svelte/internal";

function create_fragment(ctx) {
Expand All @@ -16,7 +16,7 @@ function create_fragment(ctx) {
return {
c() {
div0 = element("div");
t = text("\n");
t = space();
div1 = element("div");
div0.dataset.foo = "bar";
div1.dataset.foo = ctx.bar;
Expand Down
4 changes: 2 additions & 2 deletions test/js/samples/dont-use-dataset-in-legacy/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
insert,
noop,
safe_not_equal,
text
space
} from "svelte/internal";

function create_fragment(ctx) {
Expand All @@ -17,7 +17,7 @@ function create_fragment(ctx) {
return {
c() {
div0 = element("div");
t = text("\n");
t = space();
div1 = element("div");
attr(div0, "data-foo", "bar");
attr(div1, "data-foo", ctx.bar);
Expand Down
7 changes: 4 additions & 3 deletions test/js/samples/each-block-changed-check/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import {
noop,
safe_not_equal,
set_data,
space,
text
} from "svelte/internal";

Expand All @@ -30,13 +31,13 @@ function create_each_block(ctx) {
div = element("div");
strong = element("strong");
t0 = text(ctx.i);
t1 = text("\n\n\t\t");
t1 = space();
span = element("span");
t2 = text(t2_value);
t3 = text(" wrote ");
t4 = text(t4_value);
t5 = text(" ago:");
t6 = text("\n\n\t\t");
t6 = space();
raw_before = element('noscript');
span.className = "meta";
div.className = "comment";
Expand Down Expand Up @@ -97,7 +98,7 @@ function create_fragment(ctx) {
each_blocks[i].c();
}

t0 = text("\n\n");
t0 = space();
p = element("p");
t1 = text(ctx.foo);
},
Expand Down
8 changes: 4 additions & 4 deletions test/js/samples/event-modifiers/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import {
prevent_default,
run_all,
safe_not_equal,
stop_propagation,
text
space,
stop_propagation
} from "svelte/internal";

function create_fragment(ctx) {
Expand All @@ -23,10 +23,10 @@ function create_fragment(ctx) {
div = element("div");
button0 = element("button");
button0.textContent = "click me";
t1 = text("\n\t");
t1 = space();
button1 = element("button");
button1.textContent = "or me";
t3 = text("\n\t");
t3 = space();
button2 = element("button");
button2.textContent = "or me!";
dispose = [
Expand Down
4 changes: 2 additions & 2 deletions test/js/samples/inline-style-unoptimized/expected.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
insert,
noop,
safe_not_equal,
text
space
} from "svelte/internal";

function create_fragment(ctx) {
Expand All @@ -16,7 +16,7 @@ function create_fragment(ctx) {
return {
c() {
div0 = element("div");
t = text("\n");
t = space();
div1 = element("div");
div0.style.cssText = ctx.style;
div1.style.cssText = div1_style_value = "" + ctx.key + ": " + ctx.value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
noop,
safe_not_equal,
set_data,
space,
text
} from "svelte/internal";

Expand All @@ -20,7 +21,7 @@ function create_fragment(ctx) {
c() {
button = element("button");
button.textContent = "foo";
t1 = text("\n\n");
t1 = space();
p = element("p");
t2 = text("x: ");
t3 = text(ctx.x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
noop,
safe_not_equal,
set_data,
space,
text
} from "svelte/internal";

Expand All @@ -20,7 +21,7 @@ function create_fragment(ctx) {
c() {
button = element("button");
button.textContent = "foo";
t1 = text("\n\n");
t1 = space();
p = element("p");
t2 = text("number of things: ");
t3 = text(t3_value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
noop,
safe_not_equal,
set_data,
space,
text
} from "svelte/internal";

Expand All @@ -20,7 +21,7 @@ function create_fragment(ctx) {
c() {
button = element("button");
button.textContent = "foo";
t1 = text("\n\n");
t1 = space();
p = element("p");
t2 = text("x: ");
t3 = text(ctx.x);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
noop,
safe_not_equal,
set_data,
space,
text
} from "svelte/internal";

Expand All @@ -20,7 +21,7 @@ function create_fragment(ctx) {
c() {
button = element("button");
button.textContent = "foo";
t1 = text("\n\n");
t1 = space();
p = element("p");
t2 = text("number of things: ");
t3 = text(t3_value);
Expand Down
Loading