Refactor content blocks to ignore lines

We can simplify our previous standalone determination logic by merging content blocks again.

Fixes #854
This commit is contained in:
kpdecker
2014-08-25 13:27:28 -05:00
parent 980c38cebf
commit dc9a2ed7eb
4 changed files with 54 additions and 106 deletions
+31 -57
View File
@@ -93,7 +93,7 @@ export function prepareProgram(statements, isRoot) {
if (omitLeft(statements, i)) {
// If we are on a standalone node, save the indent info for partials
if (current.type === 'partial') {
current.indent = statements[i-1].original;
current.indent = (/([ \t]+$)/).exec(statements[i-1].original) ? RegExp.$1 : '';
}
}
}
@@ -114,39 +114,36 @@ export function prepareProgram(statements, isRoot) {
return statements;
}
function isPrevWhitespace(statements, i, isRoot, disallowIndent) {
function isPrevWhitespace(statements, i, isRoot) {
if (i === undefined) {
i = statements.length;
}
// Nodes that end with newlines are considered whitespace (but are special
// cased for strip operations)
var prev = statements[i-1];
if (prev && /\n$/.test(prev.string)) {
return true;
var prev = statements[i-1],
sibling = statements[i-2];
if (!prev) {
return isRoot;
}
return checkWhitespace(isRoot, prev, statements[i-2]);
if (prev.type === 'content') {
return (sibling || !isRoot ? (/\r?\n\s*?$/) : (/(^|\r?\n)\s*?$/)).test(prev.original);
}
}
function isNextWhitespace(statements, i, isRoot) {
if (i === undefined) {
i = -1;
}
return checkWhitespace(isRoot, statements[i+1], statements[i+2]);
}
function checkWhitespace(isRoot, next1, next2) {
if (!next1) {
var next = statements[i+1],
sibling = statements[i+2];
if (!next) {
return isRoot;
} else if (next1.type === 'content') {
// Check if the previous node is empty or whitespace only
if (/^[\s]*$/.test(next1.string)) {
if (next2) {
return next2.type === 'content' || /\n$/.test(next1.string);
} else {
return isRoot || (next1.string.indexOf('\n') >= 0);
}
}
}
if (next.type === 'content') {
return (sibling || !isRoot ? (/^\s*?\r?\n/) : (/^\s*?(\r?\n|$)/)).test(next.original);
}
}
@@ -158,22 +155,14 @@ function checkWhitespace(isRoot, next1, next2) {
// If mulitple is truthy then all whitespace will be stripped out until non-whitespace
// content is met.
function omitRight(statements, i, multiple) {
i = i == null ? 0 : i + 1;
var current = statements[i];
while (current) {
if (current.type !== 'content') {
return;
}
current.string = current.string.replace(/^[\s]+/, '');
if (multiple && !current.string) {
current = statements[++i];
} else {
return;
}
var current = statements[i == null ? 0 : i + 1];
if (!current || current.type !== 'content' || (!multiple && current.rightStripped)) {
return;
}
var original = current.string;
current.string = current.string.replace(multiple ? (/^\s+/) : (/^[ \t]*\r?\n?/), '');
current.rightStripped = current.string !== original;
}
// Marks the node to the left of the position as omitted.
@@ -184,29 +173,14 @@ function omitRight(statements, i, multiple) {
// If mulitple is truthy then all whitespace will be stripped out until non-whitespace
// content is met.
function omitLeft(statements, i, multiple) {
if (i == null) {
i = statements.length;
var current = statements[i == null ? statements.length - 1 : i - 1];
if (!current || current.type !== 'content' || (!multiple && current.leftStripped)) {
return;
}
var current = statements[--i],
prev = statements[i-1];
while (current) {
if (current.type !== 'content') {
return;
}
// We omit the last node if it's whitespace only and not preceeded by a non-content node.
if (multiple || (/^[\s]*$/.test(current.string) && (!prev || prev.type === 'content'))) {
current.string = current.string.replace(/[\s]+$/, '');
if (multiple && !current.string) {
current = statements[--i];
} else {
return true;
}
} else {
return;
}
}
// We omit the last node if it's whitespace only and not preceeded by a non-content node.
var original = current.string;
current.string = current.string.replace(multiple ? (/\s+$/) : (/[ \t]+$/), '');
current.leftStripped = current.string !== original;
return current.leftStripped;
}