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

Commit ca1e45b

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 #569
1 parent 084b83f commit ca1e45b

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

src/jqLite.js

+19-2
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,16 @@ 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+
return name.replace(/\-(\w)/g, function(all, letter, offset){
97+
return (offset == 0 && letter == 'w') ? 'w' : letter.toUpperCase();
98+
});
99+
}
100+
91101
/////////////////////////////////////////////
92102
function jqLiteWrap(element) {
93103
if (isString(element) && element.charAt(0) != '<') {
@@ -247,20 +257,27 @@ forEach({
247257
hasClass: JQLiteHasClass,
248258

249259
css: function(element, name, value) {
260+
name = camelCase(name);
261+
250262
if (isDefined(value)) {
251263
element.style[name] = value;
252264
} else {
253265
var val;
254266

255-
if (msie <=8) {
267+
if (msie <= 8) {
256268
// this is some IE specific weirdness that jQuery 1.6.4 does not sure why
257269
val = element.currentStyle && element.currentStyle[name];
258270
if (val === '') val = 'auto';
259271
}
260272

261273
val = val || element.style[name];
262274

263-
return (val === '') ? undefined : val;
275+
if (msie <= 8) {
276+
// jquery weirdness :-/
277+
val = (val === '') ? undefined : val;
278+
}
279+
280+
return val;
264281
}
265282
},
266283

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)