Skip to content

Commit

Permalink
fixes #41: escape special chars
Browse files Browse the repository at this point in the history
  • Loading branch information
lou committed Dec 27, 2012
1 parent 0403da8 commit 82e89a6
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 17 deletions.
46 changes: 34 additions & 12 deletions js/jquery.multi-select.js
Original file line number Original file line Diff line number Diff line change
@@ -1,5 +1,5 @@
/* /*
* MultiSelect v0.9.2 * MultiSelect v0.9.3
* Copyright (c) 2012 Louis Cuny * Copyright (c) 2012 Louis Cuny
* *
* This program is free software. It comes without any warranty, to * This program is free software. It comes without any warranty, to
Expand Down Expand Up @@ -104,9 +104,19 @@
var selectableLi = $('<li '+attributes+'><span>'+$(this).text()+'</span></li>'), var selectableLi = $('<li '+attributes+'><span>'+$(this).text()+'</span></li>'),
selectedLi = selectableLi.clone(); selectedLi = selectableLi.clone();


var value = $(this).val(); var value = $(this).val(),
selectableLi.addClass('ms-elem-selectable').attr('id', value+'-selectable'); msId = that.sanitize(value);
selectedLi.addClass('ms-elem-selection').attr('id', value+'-selection').hide();
selectableLi
.data('ms-value', value)
.addClass('ms-elem-selectable')
.attr('id', msId+'-selectable');

selectedLi
.data('ms-value', value)
.addClass('ms-elem-selection')
.attr('id', msId+'-selection')
.hide();


that.$selectionUl.find('.ms-optgroup-label').hide(); that.$selectionUl.find('.ms-optgroup-label').hide();


Expand Down Expand Up @@ -147,17 +157,17 @@


if(that.options.dblClick) { if(that.options.dblClick) {
that.$selectableUl.on('dblclick', '.ms-elem-selectable', function(){ that.$selectableUl.on('dblclick', '.ms-elem-selectable', function(){
that.select($(this).attr('id').replace(/-selectable/, '')); that.select($(this).data('ms-value'));
}); });
that.$selectionUl.on('dblclick', '.ms-elem-selection', function(){ that.$selectionUl.on('dblclick', '.ms-elem-selection', function(){
that.deselect($(this).attr('id').replace(/-selection/, '')); that.deselect($(this).data('ms-value'));
}); });
} else { } else {
that.$selectableUl.on('click', '.ms-elem-selectable', function(){ that.$selectableUl.on('click', '.ms-elem-selectable', function(){
that.select($(this).attr('id').replace(/-selectable/, '')); that.select($(this).data('ms-value'));
}); });
that.$selectionUl.on('click', '.ms-elem-selection', function(){ that.$selectionUl.on('click', '.ms-elem-selection', function(){
that.deselect($(this).attr('id').replace(/-selection/, '')); that.deselect($(this).data('ms-value'));
}); });
} }


Expand Down Expand Up @@ -273,17 +283,20 @@
that.options.afterInit.call(this, this.$container); that.options.afterInit.call(this, this.$container);
} }
}, },

'refresh' : function() { 'refresh' : function() {
$("#ms-"+this.$element.attr("id")).remove(); $("#ms-"+this.$element.attr("id")).remove();
this.init(this.options); this.init(this.options);
}, },

'select' : function(value, method){ 'select' : function(value, method){
if (typeof value == 'string') if (typeof value == 'string')
value = [value] value = [value]
var that = this, var that = this,
ms = this.$element, ms = this.$element,
selectables = this.$selectableUl.find('#' + value.join('-selectable, #')+'-selectable').filter(':not(.'+that.options.disabledClass+')'), msIds = $.map(value, function(val, index){ return(that.sanitize(val)) }),
selections = this.$selectionUl.find('#' + value.join('-selection, #') + '-selection'), selectables = this.$selectableUl.find('#' + msIds.join('-selectable, #')+'-selectable').filter(':not(.'+that.options.disabledClass+')'),
selections = this.$selectionUl.find('#' + msIds.join('-selection, #') + '-selection'),
options = ms.find('option').filter(function(index){ return($.inArray(this.value, value) > -1) }); options = ms.find('option').filter(function(index){ return($.inArray(this.value, value) > -1) });


if (selectables.length > 0){ if (selectables.length > 0){
Expand Down Expand Up @@ -318,13 +331,15 @@
} }
} }
}, },

'deselect' : function(value){ 'deselect' : function(value){
if (typeof value == 'string') if (typeof value == 'string')
value = [value] value = [value]
var that = this, var that = this,
ms = this.$element, ms = this.$element,
selectables = this.$selectableUl.find('#' + value.join('-selectable, #')+'-selectable'), msIds = $.map(value, function(val, index){ return(that.sanitize(val)) }),
selections = this.$selectionUl.find('#' + value.join('-selection, #')+'-selection').filter('.ms-selected'), selectables = this.$selectableUl.find('#' + msIds.join('-selectable, #')+'-selectable'),
selections = this.$selectionUl.find('#' + msIds.join('-selection, #')+'-selection').filter('.ms-selected'),
options = ms.find('option').filter(function(index){ return($.inArray(this.value, value) > -1) }); options = ms.find('option').filter(function(index){ return($.inArray(this.value, value) > -1) });


if (selections.length > 0){ if (selections.length > 0){
Expand Down Expand Up @@ -357,6 +372,7 @@
} }
} }
}, },

'select_all' : function(){ 'select_all' : function(){
var ms = this.$element; var ms = this.$element;


Expand All @@ -369,6 +385,7 @@
this.$selectableUl.focusout(); this.$selectableUl.focusout();
ms.trigger('change'); ms.trigger('change');
}, },

'deselect_all' : function(){ 'deselect_all' : function(){
var ms = this.$element; var ms = this.$element;


Expand All @@ -381,13 +398,18 @@
this.$selectionUl.focusout(); this.$selectionUl.focusout();
ms.trigger('change'); ms.trigger('change');
}, },

isDomNode: function (attr){ isDomNode: function (attr){
return ( return (
attr && attr &&
typeof attr === "object" && typeof attr === "object" &&
typeof attr.nodeType === "number" && typeof attr.nodeType === "number" &&
typeof attr.nodeName === "string" typeof attr.nodeName === "string"
); );
},

sanitize: function(value){
return(value.replace(/[^A-Za-z0-9]*/gi, '_'));
} }
} }


Expand Down
6 changes: 5 additions & 1 deletion test/spec/SpecHelper.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ beforeEach(function() {


afterEach(function () { afterEach(function () {
$("#multi-select, #multi-select-optgroup, .ms-container").remove(); $("#multi-select, #multi-select-optgroup, .ms-container").remove();
}); });

sanitize = function(value){
return(value.replace(/[^A-Za-z0-9]*/gi, '_'));
}
8 changes: 4 additions & 4 deletions test/spec/multiSelectSpec.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ describe("multiSelect", function() {


it ('should select the pre-selected options', function(){ it ('should select the pre-selected options', function(){
$.each(selectedValues, function(index, value){ $.each(selectedValues, function(index, value){
expect($('.ms-selectable ul.ms-list li#'+value+'-selectable')).toBe('.ms-selected'); expect($('.ms-selectable ul.ms-list li#'+sanitize(value)+'-selectable')).toBe('.ms-selected');
}); });
expect($('.ms-selectable ul.ms-list li.ms-selected').length).toEqual(2); expect($('.ms-selectable ul.ms-list li.ms-selected').length).toEqual(2);
}); });
Expand Down Expand Up @@ -104,7 +104,7 @@ describe("multiSelect", function() {
optgroupLabels = optgroupMsContainer.find('.ms-selectable .ms-optgroup-label'); optgroupLabels = optgroupMsContainer.find('.ms-selectable .ms-optgroup-label');
}); });


it ('should do select all nested options when clicking on optgroup', function(){ it ('should select all nested options when clicking on optgroup', function(){
var clickedOptGroupLabel = optgroupLabels.first(); var clickedOptGroupLabel = optgroupLabels.first();
clickedOptGroupLabel.trigger('click'); clickedOptGroupLabel.trigger('click');
expect(optgroupSelect.val().length).toBe(10); expect(optgroupSelect.val().length).toBe(10);
Expand Down Expand Up @@ -147,7 +147,7 @@ describe("multiSelect", function() {
beforeEach(function() { beforeEach(function() {
$('#multi-select').multiSelect(); $('#multi-select').multiSelect();
clickedItem = $('.ms-selectable ul.ms-list li').first(); clickedItem = $('.ms-selectable ul.ms-list li').first();
value = clickedItem.attr('id').replace('-selectable', ''); value = clickedItem.data('ms-value');
spyOnEvent(select, 'change'); spyOnEvent(select, 'change');
spyOnEvent(select, 'focus'); spyOnEvent(select, 'focus');
clickedItem.trigger('click'); clickedItem.trigger('click');
Expand All @@ -166,7 +166,7 @@ describe("multiSelect", function() {
}); });


it('should show the associated selected item', function(){ it('should show the associated selected item', function(){
expect($('#'+value+'-selection')).toBe(':visible'); expect($('#'+sanitize(value)+'-selection')).toBe(':visible');
}); });


it('should trigger the original select change event', function(){ it('should trigger the original select change event', function(){
Expand Down

0 comments on commit 82e89a6

Please sign in to comment.