You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
monaco-editor/test/handlebars.test.ts

291 lines
9.6 KiB
TypeScript

9 years ago
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
'use strict';
8 years ago
import { testTokenization } from './testRunner';
import { htmlTokenTypes } from '../src/handlebars';
9 years ago
const HTML_DELIM_START = htmlTokenTypes.DELIM_START;
const HTML_DELIM_END = htmlTokenTypes.DELIM_END;
const DELIM_ASSIGN = 'delimiter';
const HTML_ATTRIB_NAME = 'attribute.name';
const HTML_ATTRIB_VALUE = 'attribute.value';
function getTag(name: string) {
return htmlTokenTypes.getTag(name);
}
const handlebarsTokenTypes = {
EMBED: 'delimiter.handlebars',
EMBED_UNESCAPED: 'delimiter.handlebars',
9 years ago
KEYWORD: 'keyword.helper.handlebars',
VARIABLE: 'variable.parameter.handlebars',
}
testTokenization(['handlebars', 'css'], [
// Just HTML
[{
line: '<h1>handlebars!</h1>',
tokens: [
8 years ago
{ startIndex: 0, type: HTML_DELIM_START },
{ startIndex: 1, type: getTag('h1') },
{ startIndex: 3, type: HTML_DELIM_END },
{ startIndex: 4, type: '' },
{ startIndex: 15, type: HTML_DELIM_START },
{ startIndex: 17, type: getTag('h1') },
{ startIndex: 19, type: HTML_DELIM_END }
9 years ago
]
}],
// Expressions
[{
line: '<h1>{{ title }}</h1>',
tokens: [
8 years ago
{ startIndex: 0, type: HTML_DELIM_START },
{ startIndex: 1, type: getTag('h1') },
{ startIndex: 3, type: HTML_DELIM_END },
{ startIndex: 4, type: handlebarsTokenTypes.EMBED },
{ startIndex: 6, type: '' },
{ startIndex: 7, type: handlebarsTokenTypes.VARIABLE },
{ startIndex: 12, type: '' },
{ startIndex: 13, type: handlebarsTokenTypes.EMBED },
{ startIndex: 15, type: HTML_DELIM_START },
{ startIndex: 17, type: getTag('h1') },
{ startIndex: 19, type: HTML_DELIM_END }
9 years ago
]
}],
// Expressions Sans Whitespace
[{
line: '<h1>{{title}}</h1>',
tokens: [
8 years ago
{ startIndex: 0, type: HTML_DELIM_START },
{ startIndex: 1, type: getTag('h1') },
{ startIndex: 3, type: HTML_DELIM_END },
{ startIndex: 4, type: handlebarsTokenTypes.EMBED },
{ startIndex: 6, type: handlebarsTokenTypes.VARIABLE },
{ startIndex: 11, type: handlebarsTokenTypes.EMBED },
{ startIndex: 13, type: HTML_DELIM_START },
{ startIndex: 15, type: getTag('h1') },
{ startIndex: 17, type: HTML_DELIM_END }
9 years ago
]
}],
// Unescaped Expressions
[{
line: '<h1>{{{ title }}}</h1>',
tokens: [
8 years ago
{ startIndex: 0, type: HTML_DELIM_START },
{ startIndex: 1, type: getTag('h1') },
{ startIndex: 3, type: HTML_DELIM_END },
{ startIndex: 4, type: handlebarsTokenTypes.EMBED_UNESCAPED },
{ startIndex: 7, type: '' },
{ startIndex: 8, type: handlebarsTokenTypes.VARIABLE },
{ startIndex: 13, type: '' },
{ startIndex: 14, type: handlebarsTokenTypes.EMBED_UNESCAPED },
{ startIndex: 17, type: HTML_DELIM_START },
{ startIndex: 19, type: getTag('h1') },
{ startIndex: 21, type: HTML_DELIM_END }
9 years ago
]
}],
// Blocks
[{
line: '<ul>{{#each items}}<li>{{item}}</li>{{/each}}</ul>',
tokens: [
8 years ago
{ startIndex: 0, type: HTML_DELIM_START },
{ startIndex: 1, type: getTag('ul') },
{ startIndex: 3, type: HTML_DELIM_END },
{ startIndex: 4, type: handlebarsTokenTypes.EMBED },
{ startIndex: 6, type: handlebarsTokenTypes.KEYWORD },
{ startIndex: 11, type: '' },
{ startIndex: 12, type: handlebarsTokenTypes.VARIABLE },
{ startIndex: 17, type: handlebarsTokenTypes.EMBED },
{ startIndex: 19, type: HTML_DELIM_START },
{ startIndex: 20, type: getTag('li') },
{ startIndex: 22, type: HTML_DELIM_END },
{ startIndex: 23, type: handlebarsTokenTypes.EMBED },
{ startIndex: 25, type: handlebarsTokenTypes.VARIABLE },
{ startIndex: 29, type: handlebarsTokenTypes.EMBED },
{ startIndex: 31, type: HTML_DELIM_START },
{ startIndex: 33, type: getTag('li') },
{ startIndex: 35, type: HTML_DELIM_END },
{ startIndex: 36, type: handlebarsTokenTypes.EMBED },
{ startIndex: 38, type: handlebarsTokenTypes.KEYWORD },
{ startIndex: 43, type: handlebarsTokenTypes.EMBED },
{ startIndex: 45, type: HTML_DELIM_START },
{ startIndex: 47, type: getTag('ul') },
{ startIndex: 49, type: HTML_DELIM_END }
9 years ago
]
}],
// Multiline
[{
line: '<div>',
tokens: [
8 years ago
{ startIndex: 0, type: HTML_DELIM_START },
{ startIndex: 1, type: getTag('div') },
{ startIndex: 4, type: HTML_DELIM_END }
9 years ago
]
}, {
line: '{{#if foo}}',
tokens: [
8 years ago
{ startIndex: 0, type: handlebarsTokenTypes.EMBED },
{ startIndex: 2, type: handlebarsTokenTypes.KEYWORD },
{ startIndex: 5, type: '' },
{ startIndex: 6, type: handlebarsTokenTypes.VARIABLE },
{ startIndex: 9, type: handlebarsTokenTypes.EMBED }
9 years ago
]
}, {
line: '<span>{{bar}}</span>',
tokens: [
8 years ago
{ startIndex: 0, type: HTML_DELIM_START },
{ startIndex: 1, type: getTag('span') },
{ startIndex: 5, type: HTML_DELIM_END },
{ startIndex: 6, type: handlebarsTokenTypes.EMBED },
{ startIndex: 8, type: handlebarsTokenTypes.VARIABLE },
{ startIndex: 11, type: handlebarsTokenTypes.EMBED },
{ startIndex: 13, type: HTML_DELIM_START },
{ startIndex: 15, type: getTag('span') },
{ startIndex: 19, type: HTML_DELIM_END }
9 years ago
]
}, {
line: '{{/if}}',
tokens: [
8 years ago
{ startIndex: 0, type: handlebarsTokenTypes.EMBED },
{ startIndex: 2, type: handlebarsTokenTypes.KEYWORD },
{ startIndex: 5, type: handlebarsTokenTypes.EMBED }
9 years ago
]
}],
// Div end
[{
line: '</div>',
tokens: [
8 years ago
{ startIndex: 0, type: HTML_DELIM_START },
{ startIndex: 2, type: getTag('div') },
{ startIndex: 5, type: HTML_DELIM_END }
9 years ago
]
}],
// HTML Expressions
[{
line: '<script type="text/x-handlebars-template"><h1>{{ title }}</h1></script>',
tokens: [
8 years ago
{ startIndex: 0, type: HTML_DELIM_START },
{ startIndex: 1, type: getTag('script') },
{ startIndex: 7, type: '' },
{ startIndex: 8, type: HTML_ATTRIB_NAME },
{ startIndex: 12, type: DELIM_ASSIGN },
{ startIndex: 13, type: HTML_ATTRIB_VALUE },
{ startIndex: 41, type: HTML_DELIM_END },
{ startIndex: 42, type: HTML_DELIM_START },
{ startIndex: 43, type: getTag('h1') },
{ startIndex: 45, type: HTML_DELIM_END },
{ startIndex: 46, type: handlebarsTokenTypes.EMBED },
{ startIndex: 48, type: '' },
{ startIndex: 49, type: handlebarsTokenTypes.VARIABLE },
{ startIndex: 54, type: '' },
{ startIndex: 55, type: handlebarsTokenTypes.EMBED },
{ startIndex: 57, type: HTML_DELIM_START },
{ startIndex: 59, type: getTag('h1') },
{ startIndex: 61, type: HTML_DELIM_END },
{ startIndex: 62, type: HTML_DELIM_START },
{ startIndex: 64, type: getTag('script') },
{ startIndex: 70, type: HTML_DELIM_END }
9 years ago
]
}],
// Multi-line HTML Expressions
[{
line: '<script type="text/x-handlebars-template">',
tokens: [
8 years ago
{ startIndex: 0, type: HTML_DELIM_START },
{ startIndex: 1, type: getTag('script') },
{ startIndex: 7, type: '' },
{ startIndex: 8, type: HTML_ATTRIB_NAME },
{ startIndex: 12, type: DELIM_ASSIGN },
{ startIndex: 13, type: HTML_ATTRIB_VALUE },
{ startIndex: 41, type: HTML_DELIM_END }
9 years ago
]
}, {
line: '<h1>{{ title }}</h1>',
tokens: [
8 years ago
{ startIndex: 0, type: HTML_DELIM_START },
{ startIndex: 1, type: getTag('h1') },
{ startIndex: 3, type: HTML_DELIM_END },
{ startIndex: 4, type: handlebarsTokenTypes.EMBED },
{ startIndex: 6, type: '' },
{ startIndex: 7, type: handlebarsTokenTypes.VARIABLE },
{ startIndex: 12, type: '' },
{ startIndex: 13, type: handlebarsTokenTypes.EMBED },
{ startIndex: 15, type: HTML_DELIM_START },
{ startIndex: 17, type: getTag('h1') },
{ startIndex: 19, type: HTML_DELIM_END }
9 years ago
]
}, {
line: '</script>',
tokens: [
8 years ago
{ startIndex: 0, type: HTML_DELIM_START },
{ startIndex: 2, type: getTag('script') },
{ startIndex: 8, type: HTML_DELIM_END }
9 years ago
]
}],
// HTML Nested Modes
[{
line: '{{foo}}<script></script>{{bar}}',
tokens: [
8 years ago
{ startIndex: 0, type: handlebarsTokenTypes.EMBED },
{ startIndex: 2, type: handlebarsTokenTypes.VARIABLE },
{ startIndex: 5, type: handlebarsTokenTypes.EMBED },
{ startIndex: 7, type: HTML_DELIM_START },
{ startIndex: 8, type: getTag('script') },
{ startIndex: 14, type: HTML_DELIM_END },
// { startIndex:15, type: HTML_DELIM_START },
8 years ago
{ startIndex: 17, type: getTag('script') },
{ startIndex: 23, type: HTML_DELIM_END },
{ startIndex: 24, type: handlebarsTokenTypes.EMBED },
{ startIndex: 26, type: handlebarsTokenTypes.VARIABLE },
{ startIndex: 29, type: handlebarsTokenTypes.EMBED }
9 years ago
]
}],
// else keyword
[{
line: '{{else}}',
tokens: [
8 years ago
{ startIndex: 0, type: handlebarsTokenTypes.EMBED },
{ startIndex: 2, type: handlebarsTokenTypes.KEYWORD },
{ startIndex: 6, type: handlebarsTokenTypes.EMBED }
9 years ago
]
}],
// else keyword #2
[{
line: '{{elseFoo}}',
tokens: [
8 years ago
{ startIndex: 0, type: handlebarsTokenTypes.EMBED },
{ startIndex: 2, type: handlebarsTokenTypes.VARIABLE },
{ startIndex: 9, type: handlebarsTokenTypes.EMBED }
9 years ago
]
}],
// Token inside attribute
[{
line: '<a href="/posts/{{permalink}}">',
tokens: [
8 years ago
{ startIndex: 0, type: HTML_DELIM_START },
{ startIndex: 1, type: getTag('a') },
{ startIndex: 2, type: '' },
{ startIndex: 3, type: HTML_ATTRIB_NAME },
{ startIndex: 7, type: DELIM_ASSIGN },
{ startIndex: 8, type: HTML_ATTRIB_VALUE },
{ startIndex: 30, type: HTML_DELIM_END }
9 years ago
]
}]
]);