only remove whitespace at end of range if safe to do so

pull/676/head
Rich Harris 8 years ago
parent c4ad36023c
commit 9488bb9dbf

@ -78,7 +78,8 @@ const preprocessors = {
block: Block, block: Block,
state: State, state: State,
node: Node, node: Node,
stripWhitespace: boolean stripWhitespace: boolean,
nextSibling: Node
) => { ) => {
const blocks: Block[] = []; const blocks: Block[] = [];
let dynamic = false; let dynamic = false;
@ -96,7 +97,7 @@ const preprocessors = {
node._state = getChildState(state); node._state = getChildState(state);
blocks.push(node._block); blocks.push(node._block);
preprocessChildren(generator, node._block, node._state, node, stripWhitespace); preprocessChildren(generator, node._block, node._state, node, stripWhitespace, node);
if (node._block.dependencies.size > 0) { if (node._block.dependencies.size > 0) {
dynamic = true; dynamic = true;
@ -121,7 +122,8 @@ const preprocessors = {
node.else._block, node.else._block,
node.else._state, node.else._state,
node.else, node.else,
stripWhitespace stripWhitespace,
nextSibling
); );
if (node.else._block.dependencies.size > 0) { if (node.else._block.dependencies.size > 0) {
@ -147,7 +149,8 @@ const preprocessors = {
block: Block, block: Block,
state: State, state: State,
node: Node, node: Node,
stripWhitespace: boolean stripWhitespace: boolean,
nextSibling: Node
) => { ) => {
const dependencies = block.findDependencies(node.expression); const dependencies = block.findDependencies(node.expression);
block.addDependencies(dependencies); block.addDependencies(dependencies);
@ -194,7 +197,7 @@ const preprocessors = {
}); });
generator.blocks.push(node._block); generator.blocks.push(node._block);
preprocessChildren(generator, node._block, node._state, node, stripWhitespace); preprocessChildren(generator, node._block, node._state, node, stripWhitespace, nextSibling);
block.addDependencies(node._block.dependencies); block.addDependencies(node._block.dependencies);
node._block.hasUpdateMethod = node._block.dependencies.size > 0; node._block.hasUpdateMethod = node._block.dependencies.size > 0;
@ -211,7 +214,8 @@ const preprocessors = {
node.else._block, node.else._block,
node.else._state, node.else._state,
node.else, node.else,
stripWhitespace stripWhitespace,
nextSibling
); );
node.else._block.hasUpdateMethod = node.else._block.dependencies.size > 0; node.else._block.hasUpdateMethod = node.else._block.dependencies.size > 0;
} }
@ -222,7 +226,8 @@ const preprocessors = {
block: Block, block: Block,
state: State, state: State,
node: Node, node: Node,
stripWhitespace: boolean stripWhitespace: boolean,
nextSibling: Node
) => { ) => {
node.attributes.forEach((attribute: Node) => { node.attributes.forEach((attribute: Node) => {
if (attribute.type === 'Attribute' && attribute.value !== true) { if (attribute.type === 'Attribute' && attribute.value !== true) {
@ -312,12 +317,12 @@ const preprocessors = {
}); });
generator.blocks.push(node._block); generator.blocks.push(node._block);
preprocessChildren(generator, node._block, node._state, node, stripWhitespace); preprocessChildren(generator, node._block, node._state, node, stripWhitespace, nextSibling);
block.addDependencies(node._block.dependencies); block.addDependencies(node._block.dependencies);
node._block.hasUpdateMethod = node._block.dependencies.size > 0; node._block.hasUpdateMethod = node._block.dependencies.size > 0;
} else { } else {
if (node.name === 'pre' || node.name === 'textarea') stripWhitespace = false; if (node.name === 'pre' || node.name === 'textarea') stripWhitespace = false;
preprocessChildren(generator, block, node._state, node, stripWhitespace); preprocessChildren(generator, block, node._state, node, stripWhitespace, nextSibling);
} }
} }
}, },
@ -328,7 +333,8 @@ function preprocessChildren(
block: Block, block: Block,
state: State, state: State,
node: Node, node: Node,
stripWhitespace: boolean stripWhitespace: boolean,
nextSibling: Node
) { ) {
// glue text nodes together // glue text nodes together
const cleaned: Node[] = []; const cleaned: Node[] = [];
@ -354,9 +360,9 @@ function preprocessChildren(
lastChild = null; lastChild = null;
cleaned.forEach((child: Node) => { cleaned.forEach((child: Node, i: number) => {
const preprocessor = preprocessors[child.type]; const preprocessor = preprocessors[child.type];
if (preprocessor) preprocessor(generator, block, state, child, stripWhitespace); if (preprocessor) preprocessor(generator, block, state, child, stripWhitespace, cleaned[i + 1] || nextSibling);
if (lastChild) { if (lastChild) {
lastChild.next = child; lastChild.next = child;
@ -366,8 +372,10 @@ function preprocessChildren(
lastChild = child; lastChild = child;
}); });
if (lastChild) { // We want to remove trailing whitespace inside an element/component/block,
if (stripWhitespace && lastChild.type === 'Text') { // *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); lastChild.data = trimEnd(lastChild.data);
if (!lastChild.data) { if (!lastChild.data) {
cleaned.pop(); cleaned.pop();
@ -413,7 +421,7 @@ export default function preprocess(
}; };
generator.blocks.push(block); generator.blocks.push(block);
preprocessChildren(generator, block, state, node, true); preprocessChildren(generator, block, state, node, true, null);
block.hasUpdateMethod = block.dependencies.size > 0; block.hasUpdateMethod = block.dependencies.size > 0;
return { block, state }; return { block, state };

Loading…
Cancel
Save