Skip to content

Preserve whitespace inside nodes if necessary #676

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 5 commits into from
Jun 26, 2017
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
83 changes: 50 additions & 33 deletions src/generators/dom/preprocess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ const preprocessors = {
generator: DomGenerator,
block: Block,
state: State,
node: Node
node: Node,
stripWhitespace: boolean
) => {
const dependencies = block.findDependencies(node.expression);
block.addDependencies(dependencies);
Expand All @@ -48,7 +49,8 @@ const preprocessors = {
generator: DomGenerator,
block: Block,
state: State,
node: Node
node: Node,
stripWhitespace: boolean
) => {
const dependencies = block.findDependencies(node.expression);
block.addDependencies(dependencies);
Expand All @@ -59,7 +61,7 @@ const preprocessors = {
node._state = getChildState(state, { basename, name });
},

Text: (generator: DomGenerator, block: Block, state: State, node: Node) => {
Text: (generator: DomGenerator, block: Block, state: State, node: Node, stripWhitespace: boolean) => {
node._state = getChildState(state);

if (!/\S/.test(node.data)) {
Expand All @@ -75,7 +77,9 @@ const preprocessors = {
generator: DomGenerator,
block: Block,
state: State,
node: Node
node: Node,
stripWhitespace: boolean,
nextSibling: Node
) => {
const blocks: Block[] = [];
let dynamic = false;
Expand All @@ -93,7 +97,7 @@ const preprocessors = {
node._state = getChildState(state);

blocks.push(node._block);
preprocessChildren(generator, node._block, node._state, node);
preprocessChildren(generator, node._block, node._state, node, stripWhitespace, node);

if (node._block.dependencies.size > 0) {
dynamic = true;
Expand All @@ -117,7 +121,9 @@ const preprocessors = {
generator,
node.else._block,
node.else._state,
node.else
node.else,
stripWhitespace,
nextSibling
);

if (node.else._block.dependencies.size > 0) {
Expand All @@ -142,7 +148,9 @@ const preprocessors = {
generator: DomGenerator,
block: Block,
state: State,
node: Node
node: Node,
stripWhitespace: boolean,
nextSibling: Node
) => {
const dependencies = block.findDependencies(node.expression);
block.addDependencies(dependencies);
Expand Down Expand Up @@ -189,7 +197,7 @@ const preprocessors = {
});

generator.blocks.push(node._block);
preprocessChildren(generator, node._block, node._state, node);
preprocessChildren(generator, node._block, node._state, node, stripWhitespace, nextSibling);
block.addDependencies(node._block.dependencies);
node._block.hasUpdateMethod = node._block.dependencies.size > 0;

Expand All @@ -205,7 +213,9 @@ const preprocessors = {
generator,
node.else._block,
node.else._state,
node.else
node.else,
stripWhitespace,
nextSibling
);
node.else._block.hasUpdateMethod = node.else._block.dependencies.size > 0;
}
Expand All @@ -215,7 +225,9 @@ const preprocessors = {
generator: DomGenerator,
block: Block,
state: State,
node: Node
node: Node,
stripWhitespace: boolean,
nextSibling: Node
) => {
node.attributes.forEach((attribute: Node) => {
if (attribute.type === 'Attribute' && attribute.value !== true) {
Expand Down Expand Up @@ -305,11 +317,12 @@ const preprocessors = {
});

generator.blocks.push(node._block);
preprocessChildren(generator, node._block, node._state, node);
preprocessChildren(generator, node._block, node._state, node, stripWhitespace, nextSibling);
block.addDependencies(node._block.dependencies);
node._block.hasUpdateMethod = node._block.dependencies.size > 0;
} else {
preprocessChildren(generator, block, node._state, node);
if (node.name === 'pre' || node.name === 'textarea') stripWhitespace = false;
preprocessChildren(generator, block, node._state, node, stripWhitespace, nextSibling);
}
}
},
Expand All @@ -320,7 +333,8 @@ function preprocessChildren(
block: Block,
state: State,
node: Node,
isTopLevel: boolean = false
stripWhitespace: boolean,
nextSibling: Node
) {
// glue text nodes together
const cleaned: Node[] = [];
Expand All @@ -333,32 +347,22 @@ function preprocessChildren(
lastChild.data += child.data;
lastChild.end = child.end;
} else {
cleaned.push(child);
if (child.type === 'Text' && stripWhitespace && cleaned.length === 0) {
child.data = trimStart(child.data);
if (child.data) cleaned.push(child);
} else {
cleaned.push(child);
}
}

lastChild = child;
});

if (isTopLevel) {
// trim leading and trailing whitespace from the top level
const firstChild = cleaned[0];
if (firstChild && firstChild.type === 'Text') {
firstChild.data = trimStart(firstChild.data);
if (!firstChild.data) cleaned.shift();
}

const lastChild = cleaned[cleaned.length - 1];
if (lastChild && lastChild.type === 'Text') {
lastChild.data = trimEnd(lastChild.data);
if (!lastChild.data) cleaned.pop();
}
}

lastChild = null;

cleaned.forEach((child: Node) => {
const preprocess = preprocessors[child.type];
if (preprocess) preprocess(generator, block, state, child);
cleaned.forEach((child: Node, i: number) => {
const preprocessor = preprocessors[child.type];
if (preprocessor) preprocessor(generator, block, state, child, stripWhitespace, cleaned[i + 1] || nextSibling);

if (lastChild) {
lastChild.next = child;
Expand All @@ -368,6 +372,19 @@ function preprocessChildren(
lastChild = child;
});

// We want to remove trailing whitespace inside an element/component/block,
// *unless* there is no whitespace between this node and its next sibling
if (lastChild && lastChild.type === 'Text') {
if (stripWhitespace && (!nextSibling || (nextSibling.type === 'Text' && /^\s/.test(nextSibling.data)))) {
lastChild.data = trimEnd(lastChild.data);
if (!lastChild.data) {
cleaned.pop();
lastChild = cleaned[cleaned.length - 1];
lastChild.next = null;
}
}
}

if (lastChild) {
lastChild.needsAnchor = !state.parentNode;
}
Expand Down Expand Up @@ -404,7 +421,7 @@ export default function preprocess(
};

generator.blocks.push(block);
preprocessChildren(generator, block, state, node, true);
preprocessChildren(generator, block, state, node, true, null);
block.hasUpdateMethod = block.dependencies.size > 0;

return { block, state };
Expand Down
2 changes: 1 addition & 1 deletion src/generators/server-side-rendering/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default function ssr(
}
`}

return \`${generator.renderCode}\`;
return \`${generator.renderCode}\`.trim();
};

${name}.renderCss = function () {
Expand Down
40 changes: 9 additions & 31 deletions src/parse/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,39 +76,17 @@ export class Parser {
this.error('Unexpected end of input');
}

// trim unnecessary whitespace
while (this.html.children.length) {
const firstChild = this.html.children[0];
this.html.start = firstChild.start;

if (firstChild.type !== 'Text') break;

const length = firstChild.data.length;
firstChild.data = trimStart(firstChild.data);

if (firstChild.data === '') {
this.html.children.shift();
} else {
this.html.start += length - firstChild.data.length;
break;
}
}

while (this.html.children.length) {
const lastChild = this.html.children[this.html.children.length - 1];
this.html.end = lastChild.end;

if (lastChild.type !== 'Text') break;
if (this.html.children.length) {
let start = this.html.children[0] && this.html.children[0].start;
while (/\s/.test(template[start])) start += 1;

const length = lastChild.data.length;
lastChild.data = trimEnd(lastChild.data);
let end = this.html.children[this.html.children.length - 1] && this.html.children[this.html.children.length - 1].end;
while (/\s/.test(template[end - 1])) end -= 1;

if (lastChild.data === '') {
this.html.children.pop();
} else {
this.html.end -= length - lastChild.data.length;
break;
}
this.html.start = start;
this.html.end = end;
} else {
this.html.start = this.html.end = null;
}
}

Expand Down
22 changes: 0 additions & 22 deletions src/parse/state/tag.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,23 +62,6 @@ const disallowedContents = new Map([
['th', new Set(['td', 'th', 'tr'])],
]);

function stripWhitespace(element) {
if (element.children.length) {
const firstChild = element.children[0];
const lastChild = element.children[element.children.length - 1];

if (firstChild.type === 'Text') {
firstChild.data = trimStart(firstChild.data);
if (!firstChild.data) element.children.shift();
}

if (lastChild.type === 'Text') {
lastChild.data = trimEnd(lastChild.data);
if (!lastChild.data) element.children.pop();
}
}
}

export default function tag(parser: Parser) {
const start = parser.index++;

Expand Down Expand Up @@ -147,9 +130,6 @@ export default function tag(parser: Parser) {
parent = parser.current();
}

// strip leading/trailing whitespace as necessary
stripWhitespace(parent);

parent.end = parser.index;
parser.stack.pop();

Expand All @@ -158,8 +138,6 @@ export default function tag(parser: Parser) {
// can this be a child of the parent element, or does it implicitly
// close it, like `<li>one<li>two`?
if (disallowedContents.get(parent.name).has(name)) {
stripWhitespace(parent);

parent.end = start;
parser.stack.pop();
}
Expand Down
6 changes: 2 additions & 4 deletions test/js/samples/use-elements-as-anchors/expected-bundle.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ var proto = {
};

function create_main_fragment ( state, component ) {
var div, text, p, text_1, text_2, text_3, text_4, p_1, text_5, text_6, text_7, text_8, if_block_4_anchor;
var div, text, p, text_1, text_2, text_3, text_4, p_1, text_5, text_6, text_8, if_block_4_anchor;

var if_block = (state.a) && create_if_block( state, component );

Expand Down Expand Up @@ -164,7 +164,6 @@ function create_main_fragment ( state, component ) {
text_5 = createText( "so can this" );
text_6 = createText( "\n\n\t" );
if ( if_block_3 ) if_block_3.create();
text_7 = createText( "\n\n\t" );
text_8 = createText( "\n\n" );
if ( if_block_4 ) if_block_4.create();
if_block_4_anchor = createComment();
Expand All @@ -185,7 +184,6 @@ function create_main_fragment ( state, component ) {
appendNode( text_5, p_1 );
appendNode( text_6, div );
if ( if_block_3 ) if_block_3.mount( div, null );
appendNode( text_7, div );
insertNode( text_8, target, anchor );
if ( if_block_4 ) if_block_4.mount( target, anchor );
insertNode( if_block_4_anchor, target, anchor );
Expand Down Expand Up @@ -232,7 +230,7 @@ function create_main_fragment ( state, component ) {
if ( !if_block_3 ) {
if_block_3 = create_if_block_3( state, component );
if_block_3.create();
if_block_3.mount( div, text_7 );
if_block_3.mount( div, null );
}
} else if ( if_block_3 ) {
if_block_3.unmount();
Expand Down
6 changes: 2 additions & 4 deletions test/js/samples/use-elements-as-anchors/expected.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { appendNode, assign, createComment, createElement, createText, detachNode, dispatchObservers, insertNode, noop, proto } from "svelte/shared.js";

function create_main_fragment ( state, component ) {
var div, text, p, text_1, text_2, text_3, text_4, p_1, text_5, text_6, text_7, text_8, if_block_4_anchor;
var div, text, p, text_1, text_2, text_3, text_4, p_1, text_5, text_6, text_8, if_block_4_anchor;

var if_block = (state.a) && create_if_block( state, component );

Expand Down Expand Up @@ -29,7 +29,6 @@ function create_main_fragment ( state, component ) {
text_5 = createText( "so can this" );
text_6 = createText( "\n\n\t" );
if ( if_block_3 ) if_block_3.create();
text_7 = createText( "\n\n\t" );
text_8 = createText( "\n\n" );
if ( if_block_4 ) if_block_4.create();
if_block_4_anchor = createComment();
Expand All @@ -50,7 +49,6 @@ function create_main_fragment ( state, component ) {
appendNode( text_5, p_1 );
appendNode( text_6, div );
if ( if_block_3 ) if_block_3.mount( div, null );
appendNode( text_7, div );
insertNode( text_8, target, anchor );
if ( if_block_4 ) if_block_4.mount( target, anchor );
insertNode( if_block_4_anchor, target, anchor );
Expand Down Expand Up @@ -97,7 +95,7 @@ function create_main_fragment ( state, component ) {
if ( !if_block_3 ) {
if_block_3 = create_if_block_3( state, component );
if_block_3.create();
if_block_3.mount( div, text_7 );
if_block_3.mount( div, null );
}
} else if ( if_block_3 ) {
if_block_3.unmount();
Expand Down
5 changes: 3 additions & 2 deletions test/parser/samples/attribute-dynamic-boolean/output.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"hash": 3179574701,
"html": {
"start": 0,
"end": 45,
Expand All @@ -21,9 +22,9 @@
"end": 32,
"type": "MustacheTag",
"expression": {
"type": "Identifier",
"start": 22,
"end": 30,
"type": "Identifier",
"name": "readonly"
}
}
Expand All @@ -36,4 +37,4 @@
},
"css": null,
"js": null
}
}
2 changes: 1 addition & 1 deletion test/parser/samples/attribute-dynamic-reserved/output.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"hash": 3305933215,
"hash": 2788845841,
"html": {
"start": 0,
"end": 29,
Expand Down
Loading