angular-cn/modules/@angular/compiler/src/ml_parser/icu_ast_expander.ts

124 lines
4.1 KiB
TypeScript
Raw Normal View History

/**
* @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
*/
2016-07-21 14:41:25 -04:00
import {ParseError, ParseSourceSpan} from '../parse_util';
2016-07-21 16:56:58 -04:00
import * as html from './ast';
// http://cldr.unicode.org/index/cldr-spec/plural-rules
const PLURAL_CASES: string[] = ['zero', 'one', 'two', 'few', 'many', 'other'];
/**
* Expands special forms into elements.
*
* For example,
*
* ```
* { messages.length, plural,
* =0 {zero}
* =1 {one}
* other {more than one}
* }
* ```
*
* will be expanded into
*
* ```
* <ng-container [ngPlural]="messages.length">
* <template ngPluralCase="=0">zero</ng-container>
* <template ngPluralCase="=1">one</ng-container>
* <template ngPluralCase="other">more than one</ng-container>
* </ng-container>
* ```
*/
2016-07-21 16:56:58 -04:00
export function expandNodes(nodes: html.Node[]): ExpansionResult {
const expander = new _Expander();
2016-07-21 16:56:58 -04:00
return new ExpansionResult(html.visitAll(expander, nodes), expander.isExpanded, expander.errors);
}
export class ExpansionResult {
2016-07-21 16:56:58 -04:00
constructor(public nodes: html.Node[], public expanded: boolean, public errors: ParseError[]) {}
}
export class ExpansionError extends ParseError {
constructor(span: ParseSourceSpan, errorMsg: string) { super(span, errorMsg); }
}
/**
* Expand expansion forms (plural, select) to directives
*
* @internal
*/
2016-07-21 16:56:58 -04:00
class _Expander implements html.Visitor {
isExpanded: boolean = false;
errors: ParseError[] = [];
2016-07-21 16:56:58 -04:00
visitElement(element: html.Element, context: any): any {
return new html.Element(
element.name, element.attrs, html.visitAll(this, element.children), element.sourceSpan,
element.startSourceSpan, element.endSourceSpan);
}
2016-07-21 16:56:58 -04:00
visitAttribute(attribute: html.Attribute, context: any): any { return attribute; }
2016-07-21 16:56:58 -04:00
visitText(text: html.Text, context: any): any { return text; }
2016-07-21 16:56:58 -04:00
visitComment(comment: html.Comment, context: any): any { return comment; }
2016-07-21 16:56:58 -04:00
visitExpansion(icu: html.Expansion, context: any): any {
this.isExpanded = true;
2016-07-21 16:56:58 -04:00
return icu.type == 'plural' ? _expandPluralForm(icu, this.errors) :
_expandDefaultForm(icu, this.errors);
}
2016-07-21 16:56:58 -04:00
visitExpansionCase(icuCase: html.ExpansionCase, context: any): any {
throw new Error('Should not be reached');
}
}
2016-07-21 16:56:58 -04:00
function _expandPluralForm(ast: html.Expansion, errors: ParseError[]): html.Element {
const children = ast.cases.map(c => {
if (PLURAL_CASES.indexOf(c.value) == -1 && !c.value.match(/^=\d+$/)) {
errors.push(new ExpansionError(
c.valueSourceSpan,
`Plural cases should be "=<number>" or one of ${PLURAL_CASES.join(", ")}`));
}
const expansionResult = expandNodes(c.expression);
errors.push(...expansionResult.errors);
2016-07-21 16:56:58 -04:00
return new html.Element(
`template`, [new html.Attribute('ngPluralCase', `${c.value}`, c.valueSourceSpan)],
expansionResult.nodes, c.sourceSpan, c.sourceSpan, c.sourceSpan);
});
2016-07-21 16:56:58 -04:00
const switchAttr = new html.Attribute('[ngPlural]', ast.switchValue, ast.switchValueSourceSpan);
return new html.Element(
'ng-container', [switchAttr], children, ast.sourceSpan, ast.sourceSpan, ast.sourceSpan);
}
2016-07-21 16:56:58 -04:00
function _expandDefaultForm(ast: html.Expansion, errors: ParseError[]): html.Element {
let children = ast.cases.map(c => {
const expansionResult = expandNodes(c.expression);
errors.push(...expansionResult.errors);
if (c.value === 'other') {
// other is the default case when no values match
return new html.Element(
`template`, [new html.Attribute('ngSwitchDefault', '', c.valueSourceSpan)],
expansionResult.nodes, c.sourceSpan, c.sourceSpan, c.sourceSpan);
}
2016-07-21 16:56:58 -04:00
return new html.Element(
`template`, [new html.Attribute('ngSwitchCase', `${c.value}`, c.valueSourceSpan)],
expansionResult.nodes, c.sourceSpan, c.sourceSpan, c.sourceSpan);
});
2016-07-21 16:56:58 -04:00
const switchAttr = new html.Attribute('[ngSwitch]', ast.switchValue, ast.switchValueSourceSpan);
return new html.Element(
'ng-container', [switchAttr], children, ast.sourceSpan, ast.sourceSpan, ast.sourceSpan);
}