Skip to content

Commit 00852f1

Browse files
diptopolDiptopol Dam
authored and
Diptopol Dam
committed
Fixed the trimmed text calculation for new line
Previously, for stopInputAtMaximum option trimmed text calculation does not consider twoCharCarriageReturn value. As a result, if text contains new lines, user will able to insert extra characters after reaching the max value.
1 parent dc35ab7 commit 00852f1

File tree

2 files changed

+19
-8
lines changed

2 files changed

+19
-8
lines changed

textcounter.js

+18-7
Original file line numberDiff line numberDiff line change
@@ -106,16 +106,20 @@
106106
}
107107
}
108108
else { // character type
109+
var maxLimit = (base.options.twoCharCarriageReturn) ?
110+
base.options.max - base.twoCharCarriageReturnCount($text)
111+
: base.options.max;
112+
109113
if (base.options.countSpaces) { // if spaces should be counted
110-
trimmedString = $text.substring(0, base.options.max);
114+
trimmedString = $text.substring(0, maxLimit);
111115
}
112116
else {
113117
var charArray = $text.split(''),
114118
totalCharacters = charArray.length,
115119
charCount = 0,
116120
i = 0;
117121

118-
while (charCount < base.options.max && i < totalCharacters) {
122+
while (charCount < maxLimit && i < totalCharacters) {
119123
if (charArray[i] !== ' ') charCount++;
120124
trimmedString += charArray[i++];
121125
}
@@ -162,11 +166,7 @@
162166

163167
// count carriage returns/newlines as 2 characters
164168
if (base.options.twoCharCarriageReturn) {
165-
var carriageReturns = text.match(/(\r\n|\n|\r)/g);
166-
167-
if (carriageReturns !== null) {
168-
carriageReturnsCount = carriageReturns.length;
169-
}
169+
carriageReturnsCount = base.twoCharCarriageReturnCount(text);
170170
}
171171

172172
if (base.options.countSpaces) { // if need to count spaces
@@ -194,6 +194,17 @@
194194
return textCount;
195195
};
196196

197+
base.twoCharCarriageReturnCount = function(text) {
198+
var carriageReturns = text.match(/(\r\n|\n|\r)/g),
199+
carriageReturnsCount = 0;
200+
201+
if (carriageReturns !== null) {
202+
carriageReturnsCount = carriageReturns.length;
203+
}
204+
205+
return carriageReturnsCount;
206+
};
207+
197208
base.setCount = function(count) {
198209
base.$text_counter.text(count);
199210
};

textcounter.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)