Skip to content
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

also trim whitespace from elseif/else blocks #49

Merged
merged 1 commit into from
Nov 30, 2016
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
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