Skip to content

Commit

Permalink
Avoid division by zero
Browse files Browse the repository at this point in the history
In some cases where diff between max and min can be zero (e.g. when used with
mutable collections where items are deleted), the library will attempt a
division by zero, resulting in this.percentages to contain NaN or Infinity
values.

This adds a check, defaulting to [0, 0, 0].
  • Loading branch information
Vince Martinez committed May 6, 2014
1 parent 7a7193d commit 82605d8
Showing 1 changed file with 28 additions and 16 deletions.
44 changes: 28 additions & 16 deletions js/bootstrap-slider.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
* ========================================================= */

(function( $ ) {

var ErrorMsgs = {
Expand Down Expand Up @@ -176,11 +176,16 @@
}
}
this.diff = this.max - this.min;
this.percentage = [
(this.value[0]-this.min)*100/this.diff,
(this.value[1]-this.min)*100/this.diff,
this.step*100/this.diff
];

if (this.diff > 0) {
this.percentage = [
(this.value[0] - this.min) * 100 / this.diff,
(this.value[1] - this.min) * 100 / this.diff,
this.step * 100 / this.diff
];
} else {
this.percentage = [0, 0, 0];
}

this.offset = this.picker.offset();
this.size = this.picker[0][this.sizePos];
Expand Down Expand Up @@ -235,7 +240,7 @@
});
}

this.enabled = options.enabled &&
this.enabled = options.enabled &&
(this.element.data('slider-enabled') === undefined || this.element.data('slider-enabled') === true);
if(this.enabled) {
this.enable();
Expand All @@ -249,7 +254,7 @@

over: false,
inDrag: false,

showTooltip: function(){
if (this.tooltip_split === false ){
this.tooltip.addClass('in');
Expand All @@ -260,7 +265,7 @@

this.over = true;
},

hideTooltip: function(){
if (this.inDrag === false && this.alwaysShowTooltip !== true) {
this.tooltip.removeClass('in');
Expand Down Expand Up @@ -381,7 +386,7 @@
triggerFocusOnHandle: function(handleIdx) {
if(handleIdx === 0) {
this.handle1.focus();
}
}
if(handleIdx === 1) {
this.handle2.focus();
}
Expand Down Expand Up @@ -445,7 +450,7 @@
if (this.touchCapable && ev.type === 'touchmove') {
ev = ev.originalEvent;
}

var percentage = this.getPercentage(ev);
this.adjustPercentageForRangeSliders(percentage);
this.percentage[this.dragged] = this.reversed ? 100 - percentage : percentage;
Expand Down Expand Up @@ -565,11 +570,18 @@
}
}
this.diff = this.max - this.min;
this.percentage = [
(this.value[0]-this.min)*100/this.diff,
(this.value[1]-this.min)*100/this.diff,
this.step*100/this.diff
];


if (this.diff > 0) {
this.percentage = [
(this.value[0] - this.min) * 100 / this.diff,
(this.value[1] - this.min) * 100 / this.diff,
this.step * 100 / this.diff
];
} else {
this.percentage = [0, 0, 0];
}

this.layout();

this.element
Expand Down

0 comments on commit 82605d8

Please sign in to comment.