Skip to content

Commit

Permalink
(finally) fixing the onchange bug that bothered us for 2 years. fixes #…
Browse files Browse the repository at this point in the history
…44 #47 #66 #86 #91

also, closing #13, as we have now a good(not perfect) test suite. fixes #13

to celebrate, I'm launching maskMoney 3.0.0 :-)
  • Loading branch information
Diego Plentz committed Dec 31, 2013
1 parent 5b752a0 commit 507ddb7
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 59 deletions.
62 changes: 29 additions & 33 deletions dist/jquery.maskMoney.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
/*
* jquery-maskMoney - v2.9.6
* jquery-maskMoney - v3.0.0
* jQuery plugin to mask data entry in the input text in the form of money (currency)
* https://github.com/plentz/jquery-maskmoney
*
* Made by Diego Plentz
* Under MIT License (https://raw.github.com/plentz/jquery-maskmoney/master/LICENSE)
*/
(function ($) {
"use strict";
if (!$.browser) {

This comment has been minimized.

Copy link
@FagnerMartinsBrack

FagnerMartinsBrack Dec 31, 2013

Contributor

This property is removed from jQuery in 1.9, as from the docs: http://api.jquery.com/jquery.browser/

Are you sure this is working? Can't you use feature detection instead?

This comment has been minimized.

Copy link
@FagnerMartinsBrack

FagnerMartinsBrack Dec 31, 2013

Contributor

As it stands, this code path is theorycally going to be always falsy from now on.

This comment has been minimized.

Copy link
@plentz

plentz Dec 31, 2013

Owner

I don't know how to do it with feature detection, since it is some specific browser behavior(like safari's del, etc).

Anyway, I know it's working because I did this dirty trick https://github.com/plentz/jquery-maskmoney/blob/master/src/jquery.maskMoney.js#L3-L9

This comment has been minimized.

Copy link
@FagnerMartinsBrack

FagnerMartinsBrack Dec 31, 2013

Contributor

Looks like the partial diff confused me, I did not had the full context and assumed it was using the built-in jQuery.browser instead. If I had more attention though I could have seen the code below which is recreating the object anyway.

Unfortunatelly I can't tell you how to feature test this without spending some time on it, but it is always worth keep in mind that jQuery.browser has all sorts of problems.

Congratz for the 3.0.0 by the way.

This comment has been minimized.

Copy link
@plentz

plentz Dec 31, 2013

Owner

@FagnerMartinsBrack I know. It's really just for backwards compatibility. If the user is using jquery < 1.9, we use the default $.browser, if not, we define our own.

$.browser = {};
$.browser.mozilla = /mozilla/.test(navigator.userAgent.toLowerCase()) && !/webkit/.test(navigator.userAgent.toLowerCase());
Expand Down Expand Up @@ -44,9 +45,9 @@
var value = ($(this).val() || "0"),
isNegative = value.indexOf("-") !== -1,
decimalPart;
// get the last position of the array that is a number(probably there's a better way to do that)
// get the last position of the array that is a number(coercion makes "" to be evaluated as false)
$(value.split(/\D/).reverse()).each(function (index, element) {
if(element.match(/\d/)) {
if(element) {
decimalPart = element;
return false;
}
Expand Down Expand Up @@ -74,21 +75,20 @@

return this.each(function () {
var $input = $(this),
dirty = false;
onFocusValue;

// data-* api
settings = $.extend(settings, $input.data());

function markAsDirty() {
dirty = true;
}

function clearDirt() {
dirty = false;
}

function getInputSelection() {
var start = 0, end = 0, normalizedValue, range, textInputRange, len, endRange, el = $input.get(0);
var el = $input.get(0),
start = 0,
end = 0,
normalizedValue,
range,
textInputRange,
len,
endRange;

if (typeof el.selectionStart === "number" && typeof el.selectionEnd === "number") {
start = el.selectionStart;
Expand Down Expand Up @@ -155,7 +155,6 @@
range.select();
}
});
return this;
}

function setSymbol(value) {
Expand Down Expand Up @@ -233,29 +232,24 @@
keyPressedChar,
selection,
startPos,
endPos;
//nevalue = $inputeded to handle an IE "special" event
endPos,
value;
//added to handle an IE "special" event
if (key === undefined) {
return false;
}
// any key except the numbers 0-9
if (key < 48 || key > 57) {
// -(minus) key
if (key === 45) {
markAsDirty();
$input.val(changeSign());
return false;
// +(plus) key
} else if (key === 43) {
markAsDirty();
$input.val($input.val().replace("-", ""));
return false;
// enter key or tab key
} else if (key === 13 || key === 9) {
if (dirty) {
clearDirt();
$input.change();
}
return true;
} else if ($.browser.mozilla && (key === 37 || key === 39) && e.charCode === 0) {
// needed for left arrow key or right arrow key with firefox
Expand All @@ -277,7 +271,6 @@
value = $input.val();
$input.val(value.substring(0, startPos) + keyPressedChar + value.substring(endPos, value.length));
maskAndPosition(startPos + 1);
markAsDirty();
return false;
}
}
Expand Down Expand Up @@ -324,23 +317,21 @@
$input.val(value.substring(0, startPos) + value.substring(endPos, value.length));

maskAndPosition(startPos);
markAsDirty();
return false;
} else if (key === 9) { // tab key
if (dirty) {
$input.change();
clearDirt();
}
return true;
} else { // any other key
return true;
}
}

function focusEvent() {
onFocusValue = $input.val();
mask();
if (this.createTextRange) {
var textRange = this.createTextRange();
var input = $input.get(0),
textRange;
if (input.createTextRange) {
textRange = input.createTextRange();
textRange.collapse(false); // set the cursor at the end of the input
textRange.select();
}
Expand Down Expand Up @@ -370,12 +361,17 @@
$input.val(newValue);
}
}
if ($input.val() !== onFocusValue) {
$input.change();
}
}

function clickEvent() {
if (this.setSelectionRange) {
var length = $input.val().length;
this.setSelectionRange(length, length);
var input = $input.get(0),
length;
if (input.setSelectionRange) {
length = $input.val().length;
input.setSelectionRange(length, length);
} else {
$input.val($input.val());
}
Expand Down
4 changes: 2 additions & 2 deletions dist/jquery.maskMoney.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion maskMoney.jquery.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "maskMoney",
"version": "2.9.6",
"version": "3.0.0",
"title": "jQuery maskMoney",
"author": {
"name": "Diego Plentz",
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "jquery-maskMoney",
"description": "jQuery plugin to mask data entry in the input text in the form of money (currency)",
"version": "2.9.6",
"version": "3.0.0",
"homepage": "https://github.com/plentz/jquery-maskmoney",
"repository": {
"type": "git",
Expand Down
Loading

0 comments on commit 507ddb7

Please sign in to comment.