Skip to content

Commit

Permalink
Merge pull request #49 from Swatinem/elseif-whitespace
Browse files Browse the repository at this point in the history
also trim whitespace from elseif/else blocks
  • Loading branch information
Rich-Harris committed Nov 30, 2016
2 parents 0040b7c + 8518065 commit 6860507
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 39 deletions.
45 changes: 30 additions & 15 deletions compiler/parse/state/mustache.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,29 @@ import { trimStart, trimEnd } from '../utils/trim.js';

const validIdentifier = /[a-zA-Z_$][a-zA-Z0-9_$]*/;

function trimWhitespace ( block, trimBefore, trimAfter ) {
const firstChild = block.children[0];
const lastChild = block.children[ block.children.length - 1 ];

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

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

if ( block.else ) {
trimWhitespace( block.else, trimBefore, trimAfter );
}

if ( firstChild.elseif ) {
trimWhitespace( firstChild, trimBefore, trimAfter );
}
}

export default function mustache ( parser ) {
const start = parser.index;
parser.index += 2;
Expand All @@ -16,7 +39,7 @@ export default function mustache ( parser ) {
let expected;

if ( block.type === 'ElseBlock' ) {
// TODO need to strip whitespace from else and elseif blocks
block.end = start;
parser.stack.pop();
block = parser.current();
}
Expand All @@ -37,29 +60,21 @@ export default function mustache ( parser ) {
block.end = parser.index;
parser.stack.pop();
block = parser.current();
}

if ( block.else ) {
block.else.end = start;
if ( block.else ) {
block.else.end = start;
}
}

// strip leading/trailing whitespace as necessary
if ( !block.children.length ) parser.error( `Empty block`, block.start );
const firstChild = block.children[0];
const lastChild = block.children[ block.children.length - 1 ];

const charBefore = parser.template[ block.start - 1 ];
const charAfter = parser.template[ parser.index ];
const trimBefore = !charBefore || whitespace.test( charBefore );
const trimAfter = !charAfter || whitespace.test( charAfter );

if ( firstChild.type === 'Text' && !charBefore || whitespace.test( charBefore ) ) {
firstChild.data = trimStart( firstChild.data );
if ( !firstChild.data ) block.children.shift();
}

if ( lastChild.type === 'Text' && !charAfter || whitespace.test( charAfter ) ) {
lastChild.data = trimEnd( lastChild.data );
if ( !lastChild.data ) block.children.pop();
}
trimWhitespace( block, trimBefore, trimAfter );

block.end = parser.index;
parser.stack.pop();
Expand Down
12 changes: 0 additions & 12 deletions test/parser/if-block-else/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,6 @@
"end": 49,
"type": "ElseBlock",
"children": [
{
"start": 32,
"end": 34,
"type": "Text",
"data": "\n\t"
},
{
"start": 34,
"end": 48,
Expand All @@ -56,12 +50,6 @@
"data": "not foo"
}
]
},
{
"start": 48,
"end": 49,
"type": "Text",
"data": "\n"
}
]
}
Expand Down
12 changes: 0 additions & 12 deletions test/parser/if-block-elseif/output.json
Original file line number Diff line number Diff line change
Expand Up @@ -74,12 +74,6 @@
}
},
"children": [
{
"start": 60,
"end": 62,
"type": "Text",
"data": "\n\t"
},
{
"start": 62,
"end": 85,
Expand All @@ -94,12 +88,6 @@
"data": "x is less than 5"
}
]
},
{
"start": 85,
"end": 86,
"type": "Text",
"data": "\n"
}
]
}
Expand Down

0 comments on commit 6860507

Please sign in to comment.