Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cd65fc2

Browse files
fix(compiler): detect and strip data- prefix from bindings
Fixes angular#2687 Closes angular#2719
1 parent e69af1a commit cd65fc2

File tree

2 files changed

+55
-1
lines changed

2 files changed

+55
-1
lines changed

modules/angular2/src/render/dom/compiler/property_binding_parser.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {isPresent, RegExpWrapper} from 'angular2/src/facade/lang';
1+
import {isPresent, RegExpWrapper, StringWrapper} from 'angular2/src/facade/lang';
22
import {MapWrapper} from 'angular2/src/facade/collection';
33

44
import {Parser} from 'angular2/change_detection';
@@ -30,6 +30,9 @@ export class PropertyBindingParser implements CompileStep {
3030
var newAttrs = new Map();
3131

3232
MapWrapper.forEach(attrs, (attrValue, attrName) => {
33+
34+
attrName = this._normalizeAttributeName(attrName);
35+
3336
var bindParts = RegExpWrapper.firstMatch(BIND_NAME_REGEXP, attrName);
3437
if (isPresent(bindParts)) {
3538
if (isPresent(bindParts[1])) { // match: bind-prop
@@ -69,6 +72,11 @@ export class PropertyBindingParser implements CompileStep {
6972
MapWrapper.forEach(newAttrs, (attrValue, attrName) => { attrs.set(attrName, attrValue); });
7073
}
7174

75+
_normalizeAttributeName(attrName: string): string {
76+
return StringWrapper.startsWith(attrName, 'data-') ? StringWrapper.substring(attrName, 5) :
77+
attrName;
78+
}
79+
7280
_bindVariable(identifier, value, current: CompileElement, newAttrs: Map<any, any>) {
7381
current.bindElement().bindVariable(dashCaseToCamelCase(identifier), value);
7482
newAttrs.set(identifier, value);

modules/angular2/test/render/dom/compiler/property_binding_parser_spec.ts

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ export function main() {
3434
expect(results[0].propertyBindings.get('a').source).toEqual('b');
3535
});
3636

37+
it('should detect [] syntax with data- prefix', () => {
38+
var results = process(el('<div data-[a]="b"></div>'));
39+
expect(results[0].propertyBindings.get('a').source).toEqual('b');
40+
});
41+
3742
it('should detect [] syntax only if an attribute name starts and ends with []', () => {
3843
expect(process(el('<div z[a]="b"></div>'))[0]).toBe(null);
3944
expect(process(el('<div [a]v="b"></div>'))[0]).toBe(null);
@@ -44,6 +49,11 @@ export function main() {
4449
expect(results[0].propertyBindings.get('a').source).toEqual('b');
4550
});
4651

52+
it('should detect bind- syntax with data- prefix', () => {
53+
var results = process(el('<div data-bind-a="b"></div>'));
54+
expect(results[0].propertyBindings.get('a').source).toEqual('b');
55+
});
56+
4757
it('should detect bind- syntax only if an attribute name starts with bind',
4858
() => { expect(process(el('<div _bind-a="b"></div>'))[0]).toEqual(null); });
4959

@@ -52,6 +62,11 @@ export function main() {
5262
expect(results[0].propertyBindings.get('a').source).toEqual('{{b}}');
5363
});
5464

65+
it('should detect interpolation syntax with data- prefix', () => {
66+
var results = process(el('<div data-a="{{b}}"></div>'));
67+
expect(results[0].propertyBindings.get('a').source).toEqual('{{b}}');
68+
});
69+
5570
it('should store property setters as camel case', () => {
5671
var element = el('<div bind-some-prop="1">');
5772
var results = process(element);
@@ -63,6 +78,11 @@ export function main() {
6378
expect(results[0].variableBindings.get('b')).toEqual('a');
6479
});
6580

81+
it('should detect var- syntax with data- prefix', () => {
82+
var results = process(el('<template data-var-a="b"></template>'));
83+
expect(results[0].variableBindings.get('b')).toEqual('a');
84+
});
85+
6686
it('should store variable binding for a template element on the nestedProtoView', () => {
6787
var results = process(el('<template var-george="washington"></p>'), true);
6888
expect(results[0].variableBindings).toEqual(EMPTY_MAP);
@@ -113,6 +133,13 @@ export function main() {
113133
expect(eventBinding.fullName).toEqual('click[]');
114134
});
115135

136+
it('should detect () syntax with data- prefix', () => {
137+
var results = process(el('<div data-(click)="b()"></div>'));
138+
var eventBinding = results[0].eventBindings[0];
139+
expect(eventBinding.source.source).toEqual('b()');
140+
expect(eventBinding.fullName).toEqual('click');
141+
});
142+
116143
it('should detect () syntax only if an attribute name starts and ends with ()', () => {
117144
expect(process(el('<div z(a)="b()"></div>'))[0]).toEqual(null);
118145
expect(process(el('<div (a)v="b()"></div>'))[0]).toEqual(null);
@@ -132,6 +159,13 @@ export function main() {
132159
expect(eventBinding.fullName).toEqual('click');
133160
});
134161

162+
it('should detect on- syntax with data- prefix', () => {
163+
var results = process(el('<div data-on-click="b()"></div>'));
164+
var eventBinding = results[0].eventBindings[0];
165+
expect(eventBinding.source.source).toEqual('b()');
166+
expect(eventBinding.fullName).toEqual('click');
167+
});
168+
135169
it('should parse event handlers using on- syntax as actions', () => {
136170
var results = process(el('<div on-click="foo=bar"></div>'));
137171
var eventBinding = results[0].eventBindings[0];
@@ -157,11 +191,23 @@ export function main() {
157191
expect(results[0].eventBindings[0].source.source).toEqual('b=$event');
158192
});
159193

194+
it('should detect [()] syntax with data- prefix', () => {
195+
var results = process(el('<div data-[(a)]="b"></div>'));
196+
expect(results[0].propertyBindings.get('a').source).toEqual('b');
197+
expect(results[0].eventBindings[0].source.source).toEqual('b=$event');
198+
});
199+
160200
it('should detect bindon- syntax', () => {
161201
var results = process(el('<div bindon-a="b"></div>'));
162202
expect(results[0].propertyBindings.get('a').source).toEqual('b');
163203
expect(results[0].eventBindings[0].source.source).toEqual('b=$event');
164204
});
205+
206+
it('should detect bindon- syntax with data- prefix', () => {
207+
var results = process(el('<div data-bindon-a="b"></div>'));
208+
expect(results[0].propertyBindings.get('a').source).toEqual('b');
209+
expect(results[0].eventBindings[0].source.source).toEqual('b=$event');
210+
});
165211
});
166212
}
167213

0 commit comments

Comments
 (0)