Permalink
Browse files

Merge pull request #2584 from SergioCrisostomo/checkbox-newElement-va…

…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 b8307071fc950623c4f7804ade716f168152dc75
Showing with 46 additions and 10 deletions.
  1. +13 −10 Source/Element/Element.js
  2. +33 −0 Specs/Element/Element.js
View
@@ -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);
}
View
@@ -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.