Skip to content

Commit

Permalink
Merge pull request #2584 from SergioCrisostomo/checkbox-newElement-va…
Browse files Browse the repository at this point in the history
…lue-fix

normalize values on newElement for radio and checkbox types - Fix #2538
  • Loading branch information
ibolmo committed Jul 3, 2014
2 parents cec16d6 + bcf7c90 commit b830707
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 10 deletions.
23 changes: 13 additions & 10 deletions Source/Element/Element.js
Expand Up @@ -217,17 +217,20 @@ var escapeQuotes = function(html){
Document.implement({

newElement: function(tag, props){
if (props && props.checked != null) props.defaultChecked = props.checked;
/*<ltIE8>*/// Fix for readonly name and type properties in IE < 8
if (createElementAcceptsHTML && props){
tag = '<' + tag;
if (props.name) tag += ' name="' + escapeQuotes(props.name) + '"';
if (props.type) tag += ' type="' + escapeQuotes(props.type) + '"';
tag += '>';
delete props.name;
delete props.type;
if (props){
if (props.checked != null) props.defaultChecked = props.checked;
if ((props.type == 'checkbox' || props.type == 'radio') && props.value == null) props.value = 'on';
/*<ltIE8>*/// Fix for readonly name and type properties in IE < 8
if (createElementAcceptsHTML){
tag = '<' + tag;
if (props.name) tag += ' name="' + escapeQuotes(props.name) + '"';
if (props.type) tag += ' type="' + escapeQuotes(props.type) + '"';
tag += '>';
delete props.name;
delete props.type;
}
/*</ltIE8>*/
}
/*</ltIE8>*/
return this.id(this.createElement(tag)).set(props);
}

Expand Down
33 changes: 33 additions & 0 deletions Specs/Element/Element.js
Expand Up @@ -2586,3 +2586,36 @@ describe('Element', function(){
});

});
describe('normalize value for new Element type == checkbox || radio', function(){

it('value of new created checkbox should be "on" if none specified', function() {
var input = new Element('input', {
type: 'checkbox'
});
input.set('checked', true);
expect(input.get('value')).toEqual('on');
});
it('value of new created checkbox should be the specified in constructor', function() {
var input = new Element('input', {
type: 'checkbox',
value: 'someValue'
});
input.set('checked', true);
expect(input.get('value')).toEqual('someValue');
});
it('value of new created radio button should be "on" if none specified', function() {
var input = new Element('input', {
type: 'radio'
});
input.set('checked', true);
expect(input.get('value')).toEqual('on');
});
it('value of new created radio should be the specified in constructor', function() {
var input = new Element('input', {
type: 'radio',
value: 'someValue'
});
input.set('checked', true);
expect(input.get('value')).toEqual('someValue');
});
});

0 comments on commit b830707

Please sign in to comment.