Skip to content

Commit

Permalink
Fix radioBinding using value with quote causes exception in jQuery 2.x
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonychu committed Aug 5, 2015
1 parent c70a488 commit 4d87244
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Gruntfile.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
options: {
specs: ['tests/utilsBehaviors.js', 'tests/stringTemplateEngineBehaviors.js', 'tests/bindings/*.js'],
vendor: [
'http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js',
'http://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js',
'http://netdna.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.min.js',
'http://cdnjs.cloudflare.com/ajax/libs/knockout/3.0.0/knockout-min.js'
],
Expand Down
3 changes: 2 additions & 1 deletion src/bindings/radioBinding.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ ko.bindingHandlers.radio = {
},

update: function (element, valueAccessor) {
var $radioButton = $(element).find('input[value="' + ko.unwrap(valueAccessor()) + '"]'),
var value = ko.unwrap(valueAccessor()) || '',
$radioButton = $(element).find('input[value="' + value.replace(/"/g, '\\"') + '"]'),
$radioButtonWrapper;

if ($radioButton.length) {
Expand Down
5 changes: 4 additions & 1 deletion tests/bindings/radioBindingBehaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
this.prepareTestElement('<div class="btn-group form-group" data-toggle="buttons" data-bind="radio: value">'
+ '<label class="btn btn-primary"><input type="radio" name="options" value="A" />A</label>'
+ '<label class="btn btn-primary"><input type="radio" name="options" value="B" />B</label>'
+ '<label class="btn btn-primary"><input type="radio" name="options" value="X&quot;" />X&quot;</label>'
+ '</div>');

it('Should throw exception for non-observable value', function() {
Expand Down Expand Up @@ -39,6 +40,8 @@
expect(this.testElement.find('.active input:checked')).toHaveValue('B');
vm.value('A');
expect(this.testElement.find('.active input:checked')).toHaveValue('A');
vm.value('X"');
expect(this.testElement.find('.active input:checked')).toHaveValue('X"');
});

it('Should change value according to clicked button', function () {
Expand Down Expand Up @@ -80,7 +83,7 @@

ko.applyBindings(vm, this.testElement[0]);

this.testElement.children().eq(2).click();
this.testElement.children().eq(3).click();
jasmine.clock().tick(1);
expect(vm.value()).toEqual('C');

Expand Down

0 comments on commit 4d87244

Please sign in to comment.