Skip to content

Commit d4226f2

Browse files
committed
fix(jqLite): css should convert dash-separated properties to camelCase
this fix is needed for Firefox or other browsers that strictly follow dom/css spec which states that element.style should make properties available in camelCased form. Closes angular#569
1 parent 084b83f commit d4226f2

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed

src/jqLite.js

+33-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,30 @@ function getStyle(element) {
8888
return current;
8989
}
9090

91+
92+
/**
93+
* Converts dash-separated names to camelCase. Useful for dealing with css properties.
94+
*/
95+
function camelCase(name) {
96+
var parts = name.split('-');
97+
98+
name = parts.shift();
99+
100+
if (name == '' && parts.length) {
101+
// browser specific property starting with "-"
102+
name = parts.shift();
103+
104+
if (name == 'moz') name = 'Moz';
105+
}
106+
107+
forEach(parts, function(part) {
108+
name += part.charAt(0).toUpperCase();
109+
name += part.substr(1);
110+
});
111+
112+
return name;
113+
}
114+
91115
/////////////////////////////////////////////
92116
function jqLiteWrap(element) {
93117
if (isString(element) && element.charAt(0) != '<') {
@@ -247,20 +271,27 @@ forEach({
247271
hasClass: JQLiteHasClass,
248272

249273
css: function(element, name, value) {
274+
name = camelCase(name);
275+
250276
if (isDefined(value)) {
251277
element.style[name] = value;
252278
} else {
253279
var val;
254280

255-
if (msie <=8) {
281+
if (msie <= 8) {
256282
// this is some IE specific weirdness that jQuery 1.6.4 does not sure why
257283
val = element.currentStyle && element.currentStyle[name];
258284
if (val === '') val = 'auto';
259285
}
260286

261287
val = val || element.style[name];
262288

263-
return (val === '') ? undefined : val;
289+
if (msie <= 8) {
290+
// jquery weirdness :-/
291+
val = (val === '') ? undefined : val;
292+
}
293+
294+
return val;
264295
}
265296
},
266297

test/jqLiteSpec.js

+42
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,25 @@ describe('jqLite', function(){
366366
expect(jqLite(a).css('padding')).toBe('2px');
367367
expect(jqLite(a).css('border')).toBeFalsy();
368368
});
369+
370+
371+
it('should correctly handle dash-separated and camelCased properties', function() {
372+
var jqA = jqLite(a);
373+
374+
expect(jqA.css('z-index')).toBeOneOf('', 'auto');
375+
expect(jqA.css('zIndex')).toBeOneOf('', 'auto');
376+
377+
378+
jqA.css({'zIndex':5});
379+
380+
expect(jqA.css('z-index')).toBeOneOf('5', 5);
381+
expect(jqA.css('zIndex')).toBeOneOf('5', 5);
382+
383+
jqA.css({'z-index':7});
384+
385+
expect(jqA.css('z-index')).toBeOneOf('7', 7);
386+
expect(jqA.css('zIndex')).toBeOneOf('7', 7);
387+
});
369388
});
370389

371390

@@ -747,4 +766,27 @@ describe('jqLite', function(){
747766
expect(element.find('span').eq(20).length).toBe(0);
748767
});
749768
});
769+
770+
771+
describe('camelCase', function() {
772+
773+
it('should leave non-dashed strings alone', function() {
774+
expect(camelCase('foo')).toBe('foo');
775+
expect(camelCase('')).toBe('');
776+
expect(camelCase('fooBar')).toBe('fooBar');
777+
});
778+
779+
780+
it('should covert dash-separated strings to camelCase', function() {
781+
expect(camelCase('foo-bar')).toBe('fooBar');
782+
expect(camelCase('foo-bar-baz')).toBe('fooBarBaz');
783+
});
784+
785+
786+
it('should covert browser specific css properties', function() {
787+
expect(camelCase('-moz-foo-bar')).toBe('MozFooBar');
788+
expect(camelCase('-webkit-foo-bar')).toBe('webkitFooBar');
789+
expect(camelCase('-webkit-foo-bar')).toBe('webkitFooBar');
790+
})
791+
});
750792
});

0 commit comments

Comments
 (0)