Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

Commit beecf2e

Browse files
Wesley Chowesleycho
Wesley Cho
authored andcommitted
feat($compile): Add support for ng-attr with camelcased attributes
- Modify $compile to handle camelcased attributes only with ng-attr to support SVG fully - Change to svg to make clear it works - Ensure the elements and attributes used are valid SVG elements & attributes - Modify in attempt to use attribute IE is happy with - Add specific SVG check for IE - Fix incorrect attribute value used Modification to re-run tests - Break up line for jshint - Fix codestyle - Replace with prefix regexp - Added more test cases to catch prefix regexp
1 parent e9b9421 commit beecf2e

File tree

2 files changed

+40
-3
lines changed

2 files changed

+40
-3
lines changed

src/ng/compile.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -1405,7 +1405,10 @@ function $CompileProvider($provide, $$sanitizeUriProvider) {
14051405
// support ngAttr attribute binding
14061406
ngAttrName = directiveNormalize(name);
14071407
if (isNgAttr = NG_ATTR_BINDING.test(ngAttrName)) {
1408-
name = snake_case(ngAttrName.substr(6), '-');
1408+
name = name.replace(PREFIX_REGEXP, '')
1409+
.substr(8).replace(/_(.)/g, function(match, letter) {
1410+
return letter.toUpperCase();
1411+
});
14091412
}
14101413

14111414
var directiveNName = ngAttrName.replace(/(Start|End)$/, '');

test/ng/compileSpec.js

+36-2
Original file line numberDiff line numberDiff line change
@@ -6542,16 +6542,21 @@ describe('$compile', function() {
65426542
expect(element.attr('href')).toBe('test/test');
65436543
}));
65446544

6545-
it('should work if they are prefixed with x- or data-', inject(function($compile, $rootScope) {
6545+
it('should work if they are prefixed with x- or data- and different prefixes', inject(function($compile, $rootScope) {
65466546
$rootScope.name = "Misko";
6547-
element = $compile('<span data-ng-attr-test2="{{name}}" x-ng-attr-test3="{{name}}" data-ng:attr-test4="{{name}}"></span>')($rootScope);
6547+
element = $compile('<span data-ng-attr-test2="{{name}}" x-ng-attr-test3="{{name}}" data-ng:attr-test4="{{name}}" ' +
6548+
'x_ng-attr-test5="{{name}}" data:ng-attr-test6="{{name}}"></span>')($rootScope);
65486549
expect(element.attr('test2')).toBeUndefined();
65496550
expect(element.attr('test3')).toBeUndefined();
65506551
expect(element.attr('test4')).toBeUndefined();
6552+
expect(element.attr('test5')).toBeUndefined();
6553+
expect(element.attr('test6')).toBeUndefined();
65516554
$rootScope.$digest();
65526555
expect(element.attr('test2')).toBe('Misko');
65536556
expect(element.attr('test3')).toBe('Misko');
65546557
expect(element.attr('test4')).toBe('Misko');
6558+
expect(element.attr('test5')).toBe('Misko');
6559+
expect(element.attr('test6')).toBe('Misko');
65556560
}));
65566561

65576562
describe('when an attribute has a dash-separated name', function() {
@@ -6619,6 +6624,35 @@ describe('$compile', function() {
66196624
});
66206625

66216626

6627+
describe('when an attribute has an underscore-separated name', function() {
6628+
6629+
it('should work with different prefixes', inject(function($compile, $rootScope) {
6630+
$rootScope.dimensions = "0 0 0 0";
6631+
element = $compile('<svg ng:attr:view_box="{{dimensions}}"></svg>')($rootScope);
6632+
expect(element.attr('viewBox')).toBeUndefined();
6633+
$rootScope.$digest();
6634+
expect(element.attr('viewBox')).toBe('0 0 0 0');
6635+
}));
6636+
6637+
it('should work if they are prefixed with x- or data-', inject(function($compile, $rootScope) {
6638+
$rootScope.dimensions = "0 0 0 0";
6639+
$rootScope.number = 0.42;
6640+
$rootScope.scale = 1;
6641+
element = $compile('<svg data-ng-attr-view_box="{{dimensions}}">' +
6642+
'<filter x-ng-attr-filter_units="{{number}}">' +
6643+
'<feDiffuseLighting data-ng:attr_surface_scale="{{scale}}">' +
6644+
'</feDiffuseLighting>' +
6645+
'<feSpecularLighting x-ng:attr_surface_scale="{{scale}}">' +
6646+
'</feSpecularLighting></filter></svg>')($rootScope);
6647+
expect(element.attr('viewBox')).toBeUndefined();
6648+
$rootScope.$digest();
6649+
expect(element.attr('viewBox')).toBe('0 0 0 0');
6650+
expect(element.find('filter').attr('filterUnits')).toBe('0.42');
6651+
expect(element.find('feDiffuseLighting').attr('surfaceScale')).toBe('1');
6652+
expect(element.find('feSpecularLighting').attr('surfaceScale')).toBe('1');
6653+
}));
6654+
});
6655+
66226656
describe('multi-element directive', function() {
66236657
it('should group on link function', inject(function($compile, $rootScope) {
66246658
$rootScope.show = false;

0 commit comments

Comments
 (0)