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

Commit

Permalink
fix(jqLite): attr for boolean attribute should lowercase value
Browse files Browse the repository at this point in the history
  • Loading branch information
IgorMinar committed Oct 13, 2011
1 parent 66fdb36 commit 8f46a3c
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
15 changes: 10 additions & 5 deletions src/jqLite.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,7 @@ var JQLitePrototype = JQLite.prototype = {
// value on get.
//////////////////////////////////////////
var BOOLEAN_ATTR = {};
forEach('multiple,selected,checked,disabled,readOnly,required'.split(','), function(value, key) {
forEach('multiple,selected,checked,disabled,readOnly,required'.split(','), function(value) {
BOOLEAN_ATTR[lowercase(value)] = value;
});

Expand Down Expand Up @@ -340,17 +340,22 @@ forEach({
},

attr: function(element, name, value){
if (BOOLEAN_ATTR[name]) {
var lowercasedName = lowercase(name);
if (BOOLEAN_ATTR[lowercasedName]) {
if (isDefined(value)) {
if (!!value) {
element[name] = true;
element.setAttribute(name, name);
element.setAttribute(name, lowercasedName);
} else {
element[name] = false;
element.removeAttribute(name);
element.removeAttribute(lowercasedName);
}
} else {
return (element[name] || element.getAttribute(name)) ? name : undefined;
return (element[name] ||
element.getAttribute(name) !== null &&
(msie < 9 ? element.getAttribute(name) !== '' : true))
? lowercasedName
: undefined;
}
} else if (isDefined(value)) {
element.setAttribute(name, value);
Expand Down
30 changes: 28 additions & 2 deletions test/jqLiteSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ describe('jqLite', function() {
this.addMatchers({
toJqEqual: function(expected) {
var msg = "Unequal length";
this.message = function() { return msg; };
this.message = function() {return msg;};

var value = this.actual && expected && this.actual.length == expected.length;
for (var i = 0; value && i < expected.length; i++) {
Expand Down Expand Up @@ -191,20 +191,46 @@ describe('jqLite', function() {
expect(jqLite(b).attr('prop')).toBeFalsy();
});

it('should read special attributes as strings', function() {
it('should read boolean attributes as strings', function() {
var select = jqLite('<select>');
expect(select.attr('multiple')).toBeUndefined();
expect(jqLite('<select multiple>').attr('multiple')).toBe('multiple');
expect(jqLite('<select multiple="">').attr('multiple')).toBe('multiple');
expect(jqLite('<select multiple="x">').attr('multiple')).toBe('multiple');
});

it('should add/remove boolean attributes', function() {
var select = jqLite('<select>');
select.attr('multiple', false);
expect(select.attr('multiple')).toBeUndefined();

select.attr('multiple', true);
expect(select.attr('multiple')).toBe('multiple');
});

it('should normalize the case of boolean attributes', function() {
var input = jqLite('<input readonly>');
expect(input.attr('readonly')).toBe('readonly');
expect(input.attr('readOnly')).toBe('readonly');
expect(input.attr('READONLY')).toBe('readonly');

input.attr('readonly', false);

// attr('readonly') fails in jQuery 1.6.4, so we have to bypass it
//expect(input.attr('readOnly')).toBeUndefined();
//expect(input.attr('readonly')).toBeUndefined();
if (msie < 9) {
expect(input[0].getAttribute('readonly')).toBe('');
} else {
expect(input[0].getAttribute('readonly')).toBe(null);
}
//expect('readOnly' in input[0].attributes).toBe(false);

input.attr('readOnly', 'READonly');
expect(input.attr('readonly')).toBe('readonly');
expect(input.attr('readOnly')).toBe('readonly');
});

it('should return undefined for non-existing attributes', function() {
var elm = jqLite('<div class="any">a</div>');
expect(elm.attr('non-existing')).toBeUndefined();
Expand Down

0 comments on commit 8f46a3c

Please sign in to comment.