Skip to content

Commit 5d13e48

Browse files
author
laco0416
committed
feat(change_detection): make INTERPOLATE_REGEXP customizable
BREAKING CHANGES: `Parser` constructor required new parameter `config: CompilerConfig` as second argument.
1 parent 6da7967 commit 5d13e48

10 files changed

Lines changed: 43 additions & 18 deletions

File tree

‎modules/@angular/compiler/src/compiler.ts‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ function _createCompilerConfig() {
4141
*/
4242
export const COMPILER_PROVIDERS: Array<any | Type | {[k: string]: any} | any[]> =
4343
/*@ts2dart_const*/[
44+
/*@ts2dart_Provider*/ {provide: CompilerConfig, useFactory: _createCompilerConfig, deps: []},
4445
Lexer,
4546
Parser,
4647
HtmlParser,
@@ -50,7 +51,6 @@ export const COMPILER_PROVIDERS: Array<any | Type | {[k: string]: any} | any[]>
5051
DEFAULT_PACKAGE_URL_PROVIDER,
5152
StyleCompiler,
5253
ViewCompiler,
53-
/*@ts2dart_Provider*/ {provide: CompilerConfig, useFactory: _createCompilerConfig, deps: []},
5454
RuntimeCompiler,
5555
/*@ts2dart_Provider*/ {provide: ComponentResolver, useExisting: RuntimeCompiler},
5656
DomElementSchemaRegistry,

‎modules/@angular/compiler/src/config.ts‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,17 @@ import {CompileIdentifierMetadata} from './compile_metadata';
55

66
export class CompilerConfig {
77
public renderTypes: RenderTypes;
8+
public interpolateRegexp: RegExp;
89
constructor(public genDebugInfo: boolean, public logBindingUpdate: boolean,
9-
public useJit: boolean, renderTypes: RenderTypes = null) {
10+
public useJit: boolean, renderTypes: RenderTypes = null, interpolateRegexp: RegExp = null) {
1011
if (isBlank(renderTypes)) {
1112
renderTypes = new DefaultRenderTypes();
1213
}
1314
this.renderTypes = renderTypes;
15+
if (isBlank(interpolateRegexp)) {
16+
interpolateRegexp = DEFAULT_INTERPOLATE_REGEXP;
17+
}
18+
this.interpolateRegexp = interpolateRegexp;
1419
}
1520
}
1621

@@ -36,3 +41,8 @@ export class DefaultRenderTypes implements RenderTypes {
3641
renderNode = null;
3742
renderEvent = null;
3843
}
44+
45+
/**
46+
* A regexp pattern used to interpolate in default.
47+
*/
48+
export var DEFAULT_INTERPOLATE_REGEXP = /\{\{([\s\S]*?)\}\}/g;

‎modules/@angular/compiler/src/expression_parser/parser.ts‎

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {Injectable} from '@angular/core';
1+
import {Injectable, Inject} from '@angular/core';
22
import {isBlank, isPresent, StringWrapper} from '../../src/facade/lang';
33
import {BaseException} from '../../src/facade/exceptions';
44
import {ListWrapper} from '../../src/facade/collection';
@@ -46,11 +46,10 @@ import {
4646
AstVisitor,
4747
Quote
4848
} from './ast';
49+
import {CompilerConfig} from '../config';
4950

5051

5152
var _implicitReceiver = new ImplicitReceiver();
52-
// TODO(tbosch): Cannot make this const/final right now because of the transpiler...
53-
var INTERPOLATION_REGEXP = /\{\{([\s\S]*?)\}\}/g;
5453

5554
class ParseException extends BaseException {
5655
constructor(message: string, input: string, errLocation: string, ctxLocation?: any) {
@@ -69,7 +68,9 @@ export class TemplateBindingParseResult {
6968
@Injectable()
7069
export class Parser {
7170
constructor(/** @internal */
72-
public _lexer: Lexer) {}
71+
public _lexer: Lexer,
72+
/** @internal */
73+
public _config: CompilerConfig) {}
7374

7475
parseAction(input: string, location: any): ASTWithSource {
7576
this._checkNoInterpolation(input, location);
@@ -137,7 +138,7 @@ export class Parser {
137138
}
138139

139140
splitInterpolation(input: string, location: string): SplitInterpolation {
140-
var parts = StringWrapper.split(input, INTERPOLATION_REGEXP);
141+
var parts = StringWrapper.split(input, this._config.interpolateRegexp);
141142
if (parts.length <= 1) {
142143
return null;
143144
}
@@ -187,7 +188,7 @@ export class Parser {
187188
}
188189

189190
private _checkNoInterpolation(input: string, location: any): void {
190-
var parts = StringWrapper.split(input, INTERPOLATION_REGEXP);
191+
var parts = StringWrapper.split(input, this._config.interpolateRegexp);
191192
if (parts.length > 1) {
192193
throw new ParseException('Got interpolation ({{}}) where expression was expected', input,
193194
`at column ${this._findInterpolationErrorColumn(parts, 1)} in`,

‎modules/@angular/compiler/test/expression_parser/parser_spec.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,12 @@ import {ddescribe, describe, it, xit, iit, expect, beforeEach} from '@angular/co
22
import {isBlank, isPresent} from '../../src/facade/lang';
33
import {Parser} from '@angular/compiler/src/expression_parser/parser';
44
import {Unparser} from './unparser';
5+
import {CompilerConfig} from '@angular/compiler';
56
import {Lexer} from '@angular/compiler/src/expression_parser/lexer';
67
import {BindingPipe, LiteralPrimitive, AST} from '@angular/compiler/src/expression_parser/ast';
78

89
export function main() {
9-
function createParser() { return new Parser(new Lexer()); }
10+
function createParser() { return new Parser(new Lexer(), new CompilerConfig(true, true, true)); }
1011

1112
function parseAction(text, location = null): any {
1213
return createParser().parseAction(text, location);
@@ -453,6 +454,14 @@ export function main() {
453454
checkInterpolation(`{{ 'foo' +\n 'bar' +\r 'baz' }}`, `{{ "foo" + "bar" + "baz" }}`);
454455
});
455456

457+
it('should support custom interpolation regexp', () => {
458+
var customParser = new Parser(new Lexer(), new CompilerConfig(true, true, true, null, /<<([\s\S]*?)>>/g));
459+
var ast = (customParser.parseInterpolation('<< a >>', null) as any).ast;
460+
expect(ast.strings).toEqual(['', '']);
461+
expect(ast.expressions.length).toEqual(1);
462+
expect(ast.expressions[0].name).toEqual('a');
463+
});
464+
456465
describe("comments", () => {
457466
it('should ignore comments in interpolation expressions',
458467
() => { checkInterpolation('{{a //comment}}', '{{ a }}'); });

‎modules/@angular/compiler/test/i18n/i18n_html_parser_spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {describe, expect, it, iit, ddescribe} from "@angular/core/testing/testing_internal";
22
import {I18nHtmlParser} from "@angular/compiler/src/i18n/i18n_html_parser";
33
import {Message, id} from "@angular/compiler/src/i18n/message";
4+
import {CompilerConfig} from "@angular/compiler/src/config";
45
import {Parser} from "@angular/compiler/src/expression_parser/parser";
56
import {Lexer} from "@angular/compiler/src/expression_parser/lexer";
67
import {StringMapWrapper} from "../../src/facade/collection";
@@ -14,7 +15,7 @@ export function main() {
1415
describe('I18nHtmlParser', () => {
1516
function parse(template: string, messages: {[key: string]: string}, implicitTags: string[] = [],
1617
implicitAttrs: {[k: string]: string[]} = {}): HtmlParseTreeResult {
17-
var parser = new Parser(new Lexer());
18+
var parser = new Parser(new Lexer(), new CompilerConfig(true, true, true));
1819
let htmlParser = new HtmlParser();
1920

2021
let msgs = '';

‎modules/@angular/compiler/test/i18n/message_extractor_spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
} from '@angular/core/testing/testing_internal';
1212

1313
import {HtmlParser} from '@angular/compiler/src/html_parser';
14+
import {CompilerConfig} from '@angular/compiler/src/config';
1415
import {MessageExtractor, removeDuplicates} from '@angular/compiler/src/i18n/message_extractor';
1516
import {Message} from '@angular/compiler/src/i18n/message';
1617
import {Parser} from '@angular/compiler/src/expression_parser/parser';
@@ -22,7 +23,7 @@ export function main() {
2223

2324
beforeEach(() => {
2425
let htmlParser = new HtmlParser();
25-
var parser = new Parser(new Lexer());
26+
var parser = new Parser(new Lexer(), new CompilerConfig(true, true, true));
2627
extractor = new MessageExtractor(htmlParser, parser, ['i18n-tag'], {'i18n-el': ['trans']});
2728
});
2829

‎modules/@angular/compiler/test/offline_compiler_util.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ function _createOfflineCompiler(xhr: MockXHR, emitter: OutputEmitter): OfflineCo
5050
xhr.when(`${THIS_MODULE_PATH}/offline_compiler_compa.html`, 'Hello World {{user}}!');
5151
var htmlParser = new HtmlParser();
5252
var normalizer = new DirectiveNormalizer(xhr, urlResolver, htmlParser);
53+
var config = new CompilerConfig(true, true, true);
5354
return new OfflineCompiler(
54-
normalizer, new TemplateParser(new Parser(new Lexer()), new MockSchemaRegistry({}, {}),
55+
normalizer, new TemplateParser(new Parser(new Lexer(), config), new MockSchemaRegistry({}, {}),
5556
htmlParser, new Console(), []),
56-
new StyleCompiler(urlResolver), new ViewCompiler(new CompilerConfig(true, true, true)),
57+
new StyleCompiler(urlResolver), new ViewCompiler(config),
5758
emitter, xhr);
5859
}
5960

@@ -79,4 +80,4 @@ export class SimpleJsImportGenerator implements ImportGenerator {
7980
return importedUrlStr;
8081
}
8182
}
82-
}
83+
}

‎modules/@angular/compiler_cli/src/codegen.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -157,12 +157,13 @@ export class CodeGenerator {
157157
StaticAndDynamicReflectionCapabilities.install(staticReflector);
158158
const htmlParser = new HtmlParser();
159159
const normalizer = new DirectiveNormalizer(xhr, urlResolver, htmlParser);
160-
const parser = new Parser(new Lexer());
160+
const config = new compiler.CompilerConfig(true, true, true);
161+
const parser = new Parser(new Lexer(), config);
161162
const tmplParser = new TemplateParser(parser, new DomElementSchemaRegistry(), htmlParser,
162163
/*console*/ null, []);
163164
const offlineCompiler = new compiler.OfflineCompiler(
164165
normalizer, tmplParser, new StyleCompiler(urlResolver),
165-
new ViewCompiler(new compiler.CompilerConfig(true, true, true)),
166+
new ViewCompiler(config),
166167
new TypeScriptEmitter(reflectorHost), xhr);
167168
const resolver = new CompileMetadataResolver(
168169
new compiler.DirectiveResolver(staticReflector), new compiler.PipeResolver(staticReflector),

‎modules_dart/transform/lib/src/transform/common/ng_compiler.dart‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ OfflineCompiler createTemplateCompiler(AssetReader reader,
2424
var _urlResolver = createOfflineCompileUrlResolver();
2525

2626
// TODO(yjbanov): add router AST transformer when ready
27-
var parser = new ng.Parser(new ng.Lexer());
27+
var parser = new ng.Parser(new ng.Lexer(), compilerConfig);
2828
var _htmlParser = _createHtmlParser(translations, parser);
2929

3030
var templateParser = new TemplateParser(

‎tools/public_api_guard/public_api_spec.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1044,8 +1044,9 @@ const COMPILER = [
10441044
'CompileQueryMetadata.selectors:Array<CompileTokenMetadata>',
10451045
'CompileQueryMetadata.toJson():{[key:string]:any}',
10461046
'CompilerConfig',
1047-
'CompilerConfig.constructor(genDebugInfo:boolean, logBindingUpdate:boolean, useJit:boolean, renderTypes:RenderTypes)',
1047+
'CompilerConfig.constructor(genDebugInfo:boolean, logBindingUpdate:boolean, useJit:boolean, renderTypes:RenderTypes, interpolateRegexp:RegExp)',
10481048
'CompilerConfig.renderTypes:RenderTypes',
1049+
'CompilerConfig.interpolateRegexp:RegExp',
10491050
'CompileTemplateMetadata',
10501051
'CompileTemplateMetadata.animations:CompileAnimationEntryMetadata[]',
10511052
'CompileTemplateMetadata.constructor({encapsulation,template,templateUrl,styles,styleUrls,animations,ngContentSelectors}:{encapsulation?:ViewEncapsulation, template?:string, templateUrl?:string, styles?:string[], styleUrls?:string[], ngContentSelectors?:string[], animations?:CompileAnimationEntryMetadata[]})',

0 commit comments

Comments
 (0)