svelte/src/compiler/parse/state/mustache.ts

405 lines
9.6 KiB

6 years ago
import read_context from '../read/context';
import read_expression from '../read/expression';
import { closing_tag_omitted } from '../utils/html';
import { whitespace } from '../../utils/patterns';
6 years ago
import { trim_start, trim_end } from '../../utils/trim';
import { to_string } from '../utils/node';
import { Parser } from '../index';
import { TemplateNode } from '../../interfaces';
8 years ago
function trim_whitespace(block: TemplateNode, trim_before: boolean, trim_after: boolean) {
if (!block.children || block.children.length === 0) return; // AwaitBlock
7 years ago
6 years ago
const first_child = block.children[0];
const last_child = block.children[block.children.length - 1];
6 years ago
if (first_child.type === 'Text' && trim_before) {
first_child.data = trim_start(first_child.data);
if (!first_child.data) block.children.shift();
}
6 years ago
if (last_child.type === 'Text' && trim_after) {
last_child.data = trim_end(last_child.data);
if (!last_child.data) block.children.pop();
}
if (block.else) {
6 years ago
trim_whitespace(block.else, trim_before, trim_after);
}
6 years ago
if (first_child.elseif) {
trim_whitespace(first_child, trim_before, trim_after);
}
}
export default function mustache(parser: Parser) {
8 years ago
const start = parser.index;
7 years ago
parser.index += 1;
8 years ago
6 years ago
parser.allow_whitespace();
8 years ago
// {/if}, {/each}, {/await} or {/key}
if (parser.eat('/')) {
let block = parser.current();
8 years ago
let expected;
if (closing_tag_omitted(block.name)) {
block.end = start;
parser.stack.pop();
block = parser.current();
}
7 years ago
if (block.type === 'ElseBlock' || block.type === 'PendingBlock' || block.type === 'ThenBlock' || block.type === 'CatchBlock') {
block.end = start;
parser.stack.pop();
block = parser.current();
expected = 'await';
}
if (block.type === 'IfBlock') {
8 years ago
expected = 'if';
} else if (block.type === 'EachBlock') {
8 years ago
expected = 'each';
} else if (block.type === 'AwaitBlock') {
expected = 'await';
} else if (block.type === 'KeyBlock') {
expected = 'key';
8 years ago
} else {
parser.error({
code: 'unexpected-block-close',
message: 'Unexpected block closing tag'
});
8 years ago
}
parser.eat(expected, true);
6 years ago
parser.allow_whitespace();
7 years ago
parser.eat('}', true);
8 years ago
while (block.elseif) {
8 years ago
block.end = parser.index;
parser.stack.pop();
block = parser.current();
if (block.else) {
block.else.end = start;
}
8 years ago
}
// strip leading/trailing whitespace as necessary
6 years ago
const char_before = parser.template[block.start - 1];
const char_after = parser.template[parser.index];
const trim_before = !char_before || whitespace.test(char_before);
const trim_after = !char_after || whitespace.test(char_after);
6 years ago
trim_whitespace(block, trim_before, trim_after);
block.end = parser.index;
8 years ago
parser.stack.pop();
} else if (parser.eat(':else')) {
if (parser.eat('if')) {
parser.error({
code: 'invalid-elseif',
message: "'elseif' should be 'else if'"
});
}
8 years ago
6 years ago
parser.allow_whitespace();
8 years ago
// :else if
if (parser.eat('if')) {
const block = parser.current();
if (block.type !== 'IfBlock') {
parser.error({
code: 'invalid-elseif-placement',
message: parser.stack.some(block => block.type === 'IfBlock')
? `Expected to close ${to_string(block)} before seeing {:else if ...} block`
: 'Cannot have an {:else if ...} block outside an {#if ...} block'
});
}
8 years ago
6 years ago
parser.require_whitespace();
8 years ago
6 years ago
const expression = read_expression(parser);
6 years ago
parser.allow_whitespace();
parser.eat('}', true);
block.else = {
start: parser.index,
end: null,
type: 'ElseBlock',
children: [
{
start: parser.index,
end: null,
type: 'IfBlock',
elseif: true,
expression,
children: []
}
]
};
parser.stack.push(block.else.children[0]);
}
// :else
else {
const block = parser.current();
if (block.type !== 'IfBlock' && block.type !== 'EachBlock') {
parser.error({
code: 'invalid-else-placement',
message: parser.stack.some(block => block.type === 'IfBlock' || block.type === 'EachBlock')
? `Expected to close ${to_string(block)} before seeing {:else} block`
: 'Cannot have an {:else} block outside an {#if ...} or {#each ...} block'
});
}
6 years ago
parser.allow_whitespace();
parser.eat('}', true);
block.else = {
start: parser.index,
end: null,
type: 'ElseBlock',
children: []
};
parser.stack.push(block.else);
}
} else if (parser.match(':then') || parser.match(':catch')) {
const block = parser.current();
6 years ago
const is_then = parser.eat(':then') || !parser.eat(':catch');
6 years ago
if (is_then) {
if (block.type !== 'PendingBlock') {
parser.error({
code: 'invalid-then-placement',
message: parser.stack.some(block => block.type === 'PendingBlock')
? `Expected to close ${to_string(block)} before seeing {:then} block`
: 'Cannot have an {:then} block outside an {#await ...} block'
});
}
} else {
if (block.type !== 'ThenBlock' && block.type !== 'PendingBlock') {
parser.error({
code: 'invalid-catch-placement',
message: parser.stack.some(block => block.type === 'ThenBlock' || block.type === 'PendingBlock')
? `Expected to close ${to_string(block)} before seeing {:catch} block`
: 'Cannot have an {:catch} block outside an {#await ...} block'
});
}
}
block.end = start;
parser.stack.pop();
const await_block = parser.current();
if (!parser.eat('}')) {
parser.require_whitespace();
await_block[is_then ? 'value': 'error'] = read_context(parser);
parser.allow_whitespace();
parser.eat('}', true);
}
const new_block: TemplateNode = {
start,
end: null,
6 years ago
type: is_then ? 'ThenBlock': 'CatchBlock',
children: [],
skip: false
};
6 years ago
await_block[is_then ? 'then' : 'catch'] = new_block;
parser.stack.push(new_block);
} else if (parser.eat('#')) {
// {#if foo}, {#each foo} or {#await foo}
8 years ago
let type;
if (parser.eat('if')) {
8 years ago
type = 'IfBlock';
} else if (parser.eat('each')) {
8 years ago
type = 'EachBlock';
} else if (parser.eat('await')) {
type = 'AwaitBlock';
} else if (parser.eat('key')) {
type = 'KeyBlock';
8 years ago
} else {
parser.error({
code: 'expected-block-type',
message: 'Expected if, each, await or key'
});
8 years ago
}
6 years ago
parser.require_whitespace();
8 years ago
6 years ago
const expression = read_expression(parser);
8 years ago
const block: TemplateNode = type === 'AwaitBlock' ?
7 years ago
{
start,
end: null,
type,
expression,
value: null,
error: null,
pending: {
start: null,
7 years ago
end: null,
type: 'PendingBlock',
children: [],
skip: true
7 years ago
},
then: {
start: null,
end: null,
type: 'ThenBlock',
children: [],
skip: true
7 years ago
},
catch: {
start: null,
end: null,
type: 'CatchBlock',
children: [],
skip: true
}
7 years ago
} :
{
start,
end: null,
type,
expression,
children: []
7 years ago
};
8 years ago
6 years ago
parser.allow_whitespace();
// {#each} blocks must declare a context {#each list as item}
if (type === 'EachBlock') {
parser.eat('as', true);
6 years ago
parser.require_whitespace();
6 years ago
block.context = read_context(parser);
6 years ago
parser.allow_whitespace();
if (parser.eat(',')) {
6 years ago
parser.allow_whitespace();
block.index = parser.read_identifier();
if (!block.index) parser.error({
code: 'expected-name',
message: 'Expected name'
});
6 years ago
parser.allow_whitespace();
}
if (parser.eat('(')) {
6 years ago
parser.allow_whitespace();
6 years ago
block.key = read_expression(parser);
parser.allow_whitespace();
parser.eat(')', true);
6 years ago
parser.allow_whitespace();
}
}
const await_block_shorthand = type === 'AwaitBlock' && parser.eat('then');
6 years ago
if (await_block_shorthand) {
parser.require_whitespace();
block.value = read_context(parser);
6 years ago
parser.allow_whitespace();
}
const await_block_catch_shorthand = !await_block_shorthand && type === 'AwaitBlock' && parser.eat('catch');
if (await_block_catch_shorthand) {
parser.require_whitespace();
block.error = read_context(parser);
parser.allow_whitespace();
}
7 years ago
parser.eat('}', true);
parser.current().children.push(block);
parser.stack.push(block);
7 years ago
if (type === 'AwaitBlock') {
let child_block;
if (await_block_shorthand) {
block.then.skip = false;
child_block = block.then;
} else if (await_block_catch_shorthand) {
block.catch.skip = false;
child_block = block.catch;
} else {
block.pending.skip = false;
child_block = block.pending;
}
6 years ago
child_block.start = parser.index;
parser.stack.push(child_block);
7 years ago
}
7 years ago
} else if (parser.eat('@html')) {
// {@html content} tag
6 years ago
parser.require_whitespace();
6 years ago
const expression = read_expression(parser);
6 years ago
parser.allow_whitespace();
7 years ago
parser.eat('}', true);
parser.current().children.push({
start,
end: parser.index,
type: 'RawMustacheTag',
expression
});
7 years ago
} else if (parser.eat('@debug')) {
let identifiers;
// Implies {@debug} which indicates "debug all"
if (parser.read(/\s*}/)) {
identifiers = [];
} else {
6 years ago
const expression = read_expression(parser);
identifiers = expression.type === 'SequenceExpression'
? expression.expressions
: [expression];
identifiers.forEach(node => {
if (node.type !== 'Identifier') {
parser.error({
code: 'invalid-debug-args',
message: '{@debug ...} arguments must be identifiers, not arbitrary expressions'
}, node.start);
}
});
6 years ago
parser.allow_whitespace();
parser.eat('}', true);
}
7 years ago
parser.current().children.push({
start,
end: parser.index,
type: 'DebugTag',
identifiers
7 years ago
});
} else {
6 years ago
const expression = read_expression(parser);
8 years ago
6 years ago
parser.allow_whitespace();
7 years ago
parser.eat('}', true);
8 years ago
parser.current().children.push({
start,
end: parser.index,
type: 'MustacheTag',
expression
8 years ago
});
}
}