/** * @license * Copyright Google Inc. All Rights Reserved. * * Use of this source code is governed by an MIT-style license that can be * found in the LICENSE file at https://angular.io/license */ import {getHtmlTagDefinition} from '../../src/ml_parser/html_tags'; import {InterpolationConfig} from '../../src/ml_parser/interpolation_config'; import * as lex from '../../src/ml_parser/lexer'; import {ParseLocation, ParseSourceFile, ParseSourceSpan} from '../../src/parse_util'; export function main() { describe('HtmlLexer', () => { describe('line/column numbers', () => { it('should work without newlines', () => { expect(tokenizeAndHumanizeLineColumn('a')).toEqual([ [lex.TokenType.TAG_OPEN_START, '0:0'], [lex.TokenType.TAG_OPEN_END, '0:2'], [lex.TokenType.TEXT, '0:3'], [lex.TokenType.TAG_CLOSE, '0:4'], [lex.TokenType.EOF, '0:8'], ]); }); it('should work with one newline', () => { expect(tokenizeAndHumanizeLineColumn('\na')).toEqual([ [lex.TokenType.TAG_OPEN_START, '0:0'], [lex.TokenType.TAG_OPEN_END, '0:2'], [lex.TokenType.TEXT, '0:3'], [lex.TokenType.TAG_CLOSE, '1:1'], [lex.TokenType.EOF, '1:5'], ]); }); it('should work with multiple newlines', () => { expect(tokenizeAndHumanizeLineColumn('\na')).toEqual([ [lex.TokenType.TAG_OPEN_START, '0:0'], [lex.TokenType.TAG_OPEN_END, '1:0'], [lex.TokenType.TEXT, '1:1'], [lex.TokenType.TAG_CLOSE, '2:1'], [lex.TokenType.EOF, '2:5'], ]); }); it('should work with CR and LF', () => { expect(tokenizeAndHumanizeLineColumn('\r\na\r')).toEqual([ [lex.TokenType.TAG_OPEN_START, '0:0'], [lex.TokenType.TAG_OPEN_END, '1:0'], [lex.TokenType.TEXT, '1:1'], [lex.TokenType.TAG_CLOSE, '2:1'], [lex.TokenType.EOF, '2:5'], ]); }); }); describe('comments', () => { it('should parse comments', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.COMMENT_START], [lex.TokenType.RAW_TEXT, 't\ne\ns\nt'], [lex.TokenType.COMMENT_END], [lex.TokenType.EOF], ]); }); it('should store the locations', () => { expect(tokenizeAndHumanizeSourceSpans('')).toEqual([ [lex.TokenType.COMMENT_START, ''], [lex.TokenType.EOF, ''], ]); }); it('should report { expect(tokenizeAndHumanizeErrors(' { expect(tokenizeAndHumanizeErrors('')).toEqual([ [lex.TokenType.COMMENT_START, ''], [lex.TokenType.EOF, ''], ]); }); it('should accept comments finishing by too many dashes (odd number)', () => { expect(tokenizeAndHumanizeSourceSpans('')).toEqual([ [lex.TokenType.COMMENT_START, ''], [lex.TokenType.EOF, ''], ]); }); }); describe('doctype', () => { it('should parse doctypes', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.DOC_TYPE, 'doctype html'], [lex.TokenType.EOF], ]); }); it('should store the locations', () => { expect(tokenizeAndHumanizeSourceSpans('')).toEqual([ [lex.TokenType.DOC_TYPE, ''], [lex.TokenType.EOF, ''], ]); }); it('should report missing end doctype', () => { expect(tokenizeAndHumanizeErrors(' { it('should parse CDATA', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.CDATA_START], [lex.TokenType.RAW_TEXT, 't\ne\ns\nt'], [lex.TokenType.CDATA_END], [lex.TokenType.EOF], ]); }); it('should store the locations', () => { expect(tokenizeAndHumanizeSourceSpans('')).toEqual([ [lex.TokenType.CDATA_START, ''], [lex.TokenType.EOF, ''], ]); }); it('should report { expect(tokenizeAndHumanizeErrors(' { expect(tokenizeAndHumanizeErrors(' { it('should parse open tags without prefix', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 'test'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should parse namespace prefix', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, 'ns1', 'test'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should parse void tags', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 'test'], [lex.TokenType.TAG_OPEN_END_VOID], [lex.TokenType.EOF], ]); }); it('should allow whitespace after the tag name', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 'test'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should store the locations', () => { expect(tokenizeAndHumanizeSourceSpans('')).toEqual([ [lex.TokenType.TAG_OPEN_START, ''], [lex.TokenType.EOF, ''], ]); }); }); describe('attributes', () => { it('should parse attributes without prefix', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 't'], [lex.TokenType.ATTR_NAME, null, 'a'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should parse attributes with interpolation', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 't'], [lex.TokenType.ATTR_NAME, null, 'a'], [lex.TokenType.ATTR_VALUE, '{{v}}'], [lex.TokenType.ATTR_NAME, null, 'b'], [lex.TokenType.ATTR_VALUE, 's{{m}}e'], [lex.TokenType.ATTR_NAME, null, 'c'], [lex.TokenType.ATTR_VALUE, 's{{m//c}}e'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should parse attributes with prefix', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 't'], [lex.TokenType.ATTR_NAME, 'ns1', 'a'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should parse attributes whose prefix is not valid', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 't'], [lex.TokenType.ATTR_NAME, null, '(ns1:a)'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should parse attributes with single quote value', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 't'], [lex.TokenType.ATTR_NAME, null, 'a'], [lex.TokenType.ATTR_VALUE, 'b'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should parse attributes with double quote value', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 't'], [lex.TokenType.ATTR_NAME, null, 'a'], [lex.TokenType.ATTR_VALUE, 'b'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should parse attributes with unquoted value', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 't'], [lex.TokenType.ATTR_NAME, null, 'a'], [lex.TokenType.ATTR_VALUE, 'b'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should allow whitespace', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 't'], [lex.TokenType.ATTR_NAME, null, 'a'], [lex.TokenType.ATTR_VALUE, 'b'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should parse attributes with entities in values', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 't'], [lex.TokenType.ATTR_NAME, null, 'a'], [lex.TokenType.ATTR_VALUE, 'AA'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should not decode entities without trailing ";"', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 't'], [lex.TokenType.ATTR_NAME, null, 'a'], [lex.TokenType.ATTR_VALUE, '&'], [lex.TokenType.ATTR_NAME, null, 'b'], [lex.TokenType.ATTR_VALUE, 'c&&d'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should parse attributes with "&" in values', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 't'], [lex.TokenType.ATTR_NAME, null, 'a'], [lex.TokenType.ATTR_VALUE, 'b && c &'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should parse values with CR and LF', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 't'], [lex.TokenType.ATTR_NAME, null, 'a'], [lex.TokenType.ATTR_VALUE, 't\ne\ns\nt'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EOF], ]); }); it('should store the locations', () => { expect(tokenizeAndHumanizeSourceSpans('')).toEqual([ [lex.TokenType.TAG_OPEN_START, ''], [lex.TokenType.EOF, ''], ]); }); }); describe('closing tags', () => { it('should parse closing tags without prefix', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_CLOSE, null, 'test'], [lex.TokenType.EOF], ]); }); it('should parse closing tags with prefix', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_CLOSE, 'ns1', 'test'], [lex.TokenType.EOF], ]); }); it('should allow whitespace', () => { expect(tokenizeAndHumanizeParts('')).toEqual([ [lex.TokenType.TAG_CLOSE, null, 'test'], [lex.TokenType.EOF], ]); }); it('should store the locations', () => { expect(tokenizeAndHumanizeSourceSpans('')).toEqual([ [lex.TokenType.TAG_CLOSE, ''], [lex.TokenType.EOF, ''], ]); }); it('should report missing name after { expect(tokenizeAndHumanizeErrors('', () => { expect(tokenizeAndHumanizeErrors(' { it('should parse named entities', () => { expect(tokenizeAndHumanizeParts('a&b')).toEqual([ [lex.TokenType.TEXT, 'a&b'], [lex.TokenType.EOF], ]); }); it('should parse hexadecimal entities', () => { expect(tokenizeAndHumanizeParts('AA')).toEqual([ [lex.TokenType.TEXT, 'AA'], [lex.TokenType.EOF], ]); }); it('should parse decimal entities', () => { expect(tokenizeAndHumanizeParts('A')).toEqual([ [lex.TokenType.TEXT, 'A'], [lex.TokenType.EOF], ]); }); it('should store the locations', () => { expect(tokenizeAndHumanizeSourceSpans('a&b')).toEqual([ [lex.TokenType.TEXT, 'a&b'], [lex.TokenType.EOF, ''], ]); }); it('should report malformed/unknown entities', () => { expect(tokenizeAndHumanizeErrors('&tbo;')).toEqual([[ lex.TokenType.TEXT, 'Unknown entity "tbo" - use the "&#;" or "&#x;" syntax', '0:0' ]]); expect(tokenizeAndHumanizeErrors('&#asdf;')).toEqual([ [lex.TokenType.TEXT, 'Unexpected character "s"', '0:3'] ]); expect(tokenizeAndHumanizeErrors(' sdf;')).toEqual([ [lex.TokenType.TEXT, 'Unexpected character "s"', '0:4'] ]); expect(tokenizeAndHumanizeErrors('઼')).toEqual([ [lex.TokenType.TEXT, 'Unexpected character "EOF"', '0:6'] ]); }); }); describe('regular text', () => { it('should parse text', () => { expect(tokenizeAndHumanizeParts('a')).toEqual([ [lex.TokenType.TEXT, 'a'], [lex.TokenType.EOF], ]); }); it('should parse interpolation', () => { expect(tokenizeAndHumanizeParts('{{ a }}b{{ c // comment }}')).toEqual([ [lex.TokenType.TEXT, '{{ a }}b{{ c // comment }}'], [lex.TokenType.EOF], ]); }); it('should parse interpolation with custom markers', () => { expect(tokenizeAndHumanizeParts('{% a %}', null !, {start: '{%', end: '%}'})).toEqual([ [lex.TokenType.TEXT, '{% a %}'], [lex.TokenType.EOF], ]); }); it('should handle CR & LF', () => { expect(tokenizeAndHumanizeParts('t\ne\rs\r\nt')).toEqual([ [lex.TokenType.TEXT, 't\ne\ns\nt'], [lex.TokenType.EOF], ]); }); it('should parse entities', () => { expect(tokenizeAndHumanizeParts('a&b')).toEqual([ [lex.TokenType.TEXT, 'a&b'], [lex.TokenType.EOF], ]); }); it('should parse text starting with "&"', () => { expect(tokenizeAndHumanizeParts('a && b &')).toEqual([ [lex.TokenType.TEXT, 'a && b &'], [lex.TokenType.EOF], ]); }); it('should store the locations', () => { expect(tokenizeAndHumanizeSourceSpans('a')).toEqual([ [lex.TokenType.TEXT, 'a'], [lex.TokenType.EOF, ''], ]); }); it('should allow "<" in text nodes', () => { expect(tokenizeAndHumanizeParts('{{ a < b ? c : d }}')).toEqual([ [lex.TokenType.TEXT, '{{ a < b ? c : d }}'], [lex.TokenType.EOF], ]); expect(tokenizeAndHumanizeSourceSpans('

a')).toEqual([ [lex.TokenType.TAG_OPEN_START, ''], [lex.TokenType.TEXT, 'a'], [lex.TokenType.EOF, ''], ]); expect(tokenizeAndHumanizeParts('< a>')).toEqual([ [lex.TokenType.TEXT, '< a>'], [lex.TokenType.EOF], ]); }); it('should parse valid start tag in interpolation', () => { expect(tokenizeAndHumanizeParts('{{ a d }}')).toEqual([ [lex.TokenType.TEXT, '{{ a '], [lex.TokenType.TAG_OPEN_START, null, 'b'], [lex.TokenType.ATTR_NAME, null, '&&'], [lex.TokenType.ATTR_NAME, null, 'c'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.TEXT, ' d }}'], [lex.TokenType.EOF], ]); }); it('should be able to escape {', () => { expect(tokenizeAndHumanizeParts('{{ "{" }}')).toEqual([ [lex.TokenType.TEXT, '{{ "{" }}'], [lex.TokenType.EOF], ]); }); it('should be able to escape {{', () => { expect(tokenizeAndHumanizeParts('{{ "{{" }}')).toEqual([ [lex.TokenType.TEXT, '{{ "{{" }}'], [lex.TokenType.EOF], ]); }); it('should treat expansion form as text when they are not parsed', () => { expect(tokenizeAndHumanizeParts('{a, b, =4 {c}}', false)).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 'span'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.TEXT, '{a, b, =4 {c}}'], [lex.TokenType.TAG_CLOSE, null, 'span'], [lex.TokenType.EOF], ]); }); }); describe('raw text', () => { it('should parse text', () => { expect(tokenizeAndHumanizeParts(``)).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 'script'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.RAW_TEXT, 't\ne\ns\nt'], [lex.TokenType.TAG_CLOSE, null, 'script'], [lex.TokenType.EOF], ]); }); it('should not detect entities', () => { expect(tokenizeAndHumanizeParts(``)).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 'script'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.RAW_TEXT, '&'], [lex.TokenType.TAG_CLOSE, null, 'script'], [lex.TokenType.EOF], ]); }); it('should ignore other opening tags', () => { expect(tokenizeAndHumanizeParts(``)).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 'script'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.RAW_TEXT, 'a

'], [lex.TokenType.TAG_CLOSE, null, 'script'], [lex.TokenType.EOF], ]); }); it('should ignore other closing tags', () => { expect(tokenizeAndHumanizeParts(``)).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 'script'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.RAW_TEXT, 'a'], [lex.TokenType.TAG_CLOSE, null, 'script'], [lex.TokenType.EOF], ]); }); it('should store the locations', () => { expect(tokenizeAndHumanizeSourceSpans(``)).toEqual([ [lex.TokenType.TAG_OPEN_START, ''], [lex.TokenType.RAW_TEXT, 'a'], [lex.TokenType.TAG_CLOSE, ''], [lex.TokenType.EOF, ''], ]); }); }); describe('escapable raw text', () => { it('should parse text', () => { expect(tokenizeAndHumanizeParts(`t\ne\rs\r\nt`)).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 'title'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.ESCAPABLE_RAW_TEXT, 't\ne\ns\nt'], [lex.TokenType.TAG_CLOSE, null, 'title'], [lex.TokenType.EOF], ]); }); it('should detect entities', () => { expect(tokenizeAndHumanizeParts(`&`)).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 'title'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.ESCAPABLE_RAW_TEXT, '&'], [lex.TokenType.TAG_CLOSE, null, 'title'], [lex.TokenType.EOF], ]); }); it('should ignore other opening tags', () => { expect(tokenizeAndHumanizeParts(`a<div>`)).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 'title'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.ESCAPABLE_RAW_TEXT, 'a
'], [lex.TokenType.TAG_CLOSE, null, 'title'], [lex.TokenType.EOF], ]); }); it('should ignore other closing tags', () => { expect(tokenizeAndHumanizeParts(`a</test>`)).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 'title'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.ESCAPABLE_RAW_TEXT, 'a'], [lex.TokenType.TAG_CLOSE, null, 'title'], [lex.TokenType.EOF], ]); }); it('should store the locations', () => { expect(tokenizeAndHumanizeSourceSpans(`a`)).toEqual([ [lex.TokenType.TAG_OPEN_START, ''], [lex.TokenType.ESCAPABLE_RAW_TEXT, 'a'], [lex.TokenType.TAG_CLOSE, ''], [lex.TokenType.EOF, ''], ]); }); }); describe('expansion forms', () => { it('should parse an expansion form', () => { expect(tokenizeAndHumanizeParts('{one.two, three, =4 {four} =5 {five} foo {bar} }', true)) .toEqual([ [lex.TokenType.EXPANSION_FORM_START], [lex.TokenType.RAW_TEXT, 'one.two'], [lex.TokenType.RAW_TEXT, 'three'], [lex.TokenType.EXPANSION_CASE_VALUE, '=4'], [lex.TokenType.EXPANSION_CASE_EXP_START], [lex.TokenType.TEXT, 'four'], [lex.TokenType.EXPANSION_CASE_EXP_END], [lex.TokenType.EXPANSION_CASE_VALUE, '=5'], [lex.TokenType.EXPANSION_CASE_EXP_START], [lex.TokenType.TEXT, 'five'], [lex.TokenType.EXPANSION_CASE_EXP_END], [lex.TokenType.EXPANSION_CASE_VALUE, 'foo'], [lex.TokenType.EXPANSION_CASE_EXP_START], [lex.TokenType.TEXT, 'bar'], [lex.TokenType.EXPANSION_CASE_EXP_END], [lex.TokenType.EXPANSION_FORM_END], [lex.TokenType.EOF], ]); }); it('should parse an expansion form with text elements surrounding it', () => { expect(tokenizeAndHumanizeParts('before{one.two, three, =4 {four}}after', true)).toEqual([ [lex.TokenType.TEXT, 'before'], [lex.TokenType.EXPANSION_FORM_START], [lex.TokenType.RAW_TEXT, 'one.two'], [lex.TokenType.RAW_TEXT, 'three'], [lex.TokenType.EXPANSION_CASE_VALUE, '=4'], [lex.TokenType.EXPANSION_CASE_EXP_START], [lex.TokenType.TEXT, 'four'], [lex.TokenType.EXPANSION_CASE_EXP_END], [lex.TokenType.EXPANSION_FORM_END], [lex.TokenType.TEXT, 'after'], [lex.TokenType.EOF], ]); }); it('should parse an expansion form as a tag single child', () => { expect(tokenizeAndHumanizeParts('
{a, b, =4 {c}}
', true)).toEqual([ [lex.TokenType.TAG_OPEN_START, null, 'div'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.TAG_OPEN_START, null, 'span'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.EXPANSION_FORM_START], [lex.TokenType.RAW_TEXT, 'a'], [lex.TokenType.RAW_TEXT, 'b'], [lex.TokenType.EXPANSION_CASE_VALUE, '=4'], [lex.TokenType.EXPANSION_CASE_EXP_START], [lex.TokenType.TEXT, 'c'], [lex.TokenType.EXPANSION_CASE_EXP_END], [lex.TokenType.EXPANSION_FORM_END], [lex.TokenType.TAG_CLOSE, null, 'span'], [lex.TokenType.TAG_CLOSE, null, 'div'], [lex.TokenType.EOF], ]); }); it('should parse an expansion forms with elements in it', () => { expect(tokenizeAndHumanizeParts('{one.two, three, =4 {four a}}', true)).toEqual([ [lex.TokenType.EXPANSION_FORM_START], [lex.TokenType.RAW_TEXT, 'one.two'], [lex.TokenType.RAW_TEXT, 'three'], [lex.TokenType.EXPANSION_CASE_VALUE, '=4'], [lex.TokenType.EXPANSION_CASE_EXP_START], [lex.TokenType.TEXT, 'four '], [lex.TokenType.TAG_OPEN_START, null, 'b'], [lex.TokenType.TAG_OPEN_END], [lex.TokenType.TEXT, 'a'], [lex.TokenType.TAG_CLOSE, null, 'b'], [lex.TokenType.EXPANSION_CASE_EXP_END], [lex.TokenType.EXPANSION_FORM_END], [lex.TokenType.EOF], ]); }); it('should parse an expansion forms containing an interpolation', () => { expect(tokenizeAndHumanizeParts('{one.two, three, =4 {four {{a}}}}', true)).toEqual([ [lex.TokenType.EXPANSION_FORM_START], [lex.TokenType.RAW_TEXT, 'one.two'], [lex.TokenType.RAW_TEXT, 'three'], [lex.TokenType.EXPANSION_CASE_VALUE, '=4'], [lex.TokenType.EXPANSION_CASE_EXP_START], [lex.TokenType.TEXT, 'four {{a}}'], [lex.TokenType.EXPANSION_CASE_EXP_END], [lex.TokenType.EXPANSION_FORM_END], [lex.TokenType.EOF], ]); }); it('should parse nested expansion forms', () => { expect(tokenizeAndHumanizeParts(`{one.two, three, =4 { {xx, yy, =x {one}} }}`, true)) .toEqual([ [lex.TokenType.EXPANSION_FORM_START], [lex.TokenType.RAW_TEXT, 'one.two'], [lex.TokenType.RAW_TEXT, 'three'], [lex.TokenType.EXPANSION_CASE_VALUE, '=4'], [lex.TokenType.EXPANSION_CASE_EXP_START], [lex.TokenType.EXPANSION_FORM_START], [lex.TokenType.RAW_TEXT, 'xx'], [lex.TokenType.RAW_TEXT, 'yy'], [lex.TokenType.EXPANSION_CASE_VALUE, '=x'], [lex.TokenType.EXPANSION_CASE_EXP_START], [lex.TokenType.TEXT, 'one'], [lex.TokenType.EXPANSION_CASE_EXP_END], [lex.TokenType.EXPANSION_FORM_END], [lex.TokenType.TEXT, ' '], [lex.TokenType.EXPANSION_CASE_EXP_END], [lex.TokenType.EXPANSION_FORM_END], [lex.TokenType.EOF], ]); }); }); describe('errors', () => { it('should report unescaped "{" on error', () => { expect(tokenizeAndHumanizeErrors(`

before { after

`, true)).toEqual([[ lex.TokenType.RAW_TEXT, `Unexpected character "EOF" (Do you have an unescaped "{" in your template? Use "{{ '{' }}") to escape it.)`, '0:21', ]]); }); it('should include 2 lines of context in message', () => { const src = '111\n222\n333\nE\n444\n555\n666\n'; const file = new ParseSourceFile(src, 'file://'); const location = new ParseLocation(file, 12, 123, 456); const span = new ParseSourceSpan(location, location); const error = new lex.TokenError('**ERROR**', null !, span); expect(error.toString()) .toEqual(`**ERROR** ("\n222\n333\n[ERROR ->]E\n444\n555\n"): file://@123:456`); }); }); describe('unicode characters', () => { it('should support unicode characters', () => { expect(tokenizeAndHumanizeSourceSpans(`

İ

`)).toEqual([ [lex.TokenType.TAG_OPEN_START, ''], [lex.TokenType.TEXT, 'İ'], [lex.TokenType.TAG_CLOSE, '

'], [lex.TokenType.EOF, ''], ]); }); }); }); } function tokenizeWithoutErrors( input: string, tokenizeExpansionForms: boolean = false, interpolationConfig?: InterpolationConfig): lex.Token[] { const tokenizeResult = lex.tokenize( input, 'someUrl', getHtmlTagDefinition, tokenizeExpansionForms, interpolationConfig); if (tokenizeResult.errors.length > 0) { const errorString = tokenizeResult.errors.join('\n'); throw new Error(`Unexpected parse errors:\n${errorString}`); } return tokenizeResult.tokens; } function tokenizeAndHumanizeParts( input: string, tokenizeExpansionForms: boolean = false, interpolationConfig?: InterpolationConfig): any[] { return tokenizeWithoutErrors(input, tokenizeExpansionForms, interpolationConfig) .map(token => [token.type].concat(token.parts)); } function tokenizeAndHumanizeSourceSpans(input: string): any[] { return tokenizeWithoutErrors(input).map(token => [token.type, token.sourceSpan.toString()]); } function humanizeLineColumn(location: ParseLocation): string { return `${location.line}:${location.col}`; } function tokenizeAndHumanizeLineColumn(input: string): any[] { return tokenizeWithoutErrors(input).map( token => [token.type, humanizeLineColumn(token.sourceSpan.start)]); } function tokenizeAndHumanizeErrors(input: string, tokenizeExpansionForms: boolean = false): any[] { return lex.tokenize(input, 'someUrl', getHtmlTagDefinition, tokenizeExpansionForms) .errors.map(e => [e.tokenType, e.msg, humanizeLineColumn(e.span.start)]); }