|
46 | 46 | eventTriggered = e.originalEvent === undefined ? false : true;
|
47 | 47 |
|
48 | 48 | if (!$.isEmptyObject($text)) {
|
49 |
| - if (base.options.type == "word") { // word count |
50 |
| - textCount = $text.trim().replace(/\s+/gi, ' ').split(' ').length; |
51 |
| - } |
52 |
| - else { // character count |
53 |
| - // count carriage returns/newlines as 2 characters |
54 |
| - if (base.options.twoCharCarriageReturn) { |
55 |
| - var carriageReturns = $text.match(/(\r\n|\n|\r)/g), |
56 |
| - carriageReturnsCount = 0; |
57 |
| - |
58 |
| - if (carriageReturns !== null) { |
59 |
| - carriageReturnsCount = carriageReturns.length; |
60 |
| - } |
61 |
| - } |
62 |
| - |
63 |
| - if (base.options.countSpaces) { // if need to count spaces |
64 |
| - textCount = $text.replace(/[^\S\n|\r|\r\n]/g, ' ').length; |
65 |
| - } |
66 |
| - else { |
67 |
| - textCount = $text.replace(/\s/g, '').length; |
68 |
| - } |
69 |
| - |
70 |
| - // count extended characters (e.g. Chinese) |
71 |
| - if (base.options.countExtendedCharacters) { |
72 |
| - var extended = $text.match(/[^\x00-\xff]/gi); |
73 |
| - |
74 |
| - if (extended == null) { |
75 |
| - textCount = $text.length; |
76 |
| - } else { |
77 |
| - textCount = $text.length + extended.length; |
78 |
| - } |
79 |
| - } |
80 |
| - |
81 |
| - if (base.options.twoCharCarriageReturn) { |
82 |
| - textCount += carriageReturnsCount; |
83 |
| - } |
84 |
| - } |
| 49 | + textCount = base.textCount($text); |
85 | 50 | }
|
86 | 51 |
|
87 | 52 | // if max is auto retrieve value
|
|
159 | 124 |
|
160 | 125 | $this.val(trimmedString.trim());
|
161 | 126 |
|
162 |
| - textTotalCount = base.options.countDown ? 0 : base.options.max; |
| 127 | + textCount = base.textCount($this.val()); |
| 128 | + textTotalCount = base.options.countDown ? base.options.max - textCount : textCount; |
163 | 129 | base.setCount(textTotalCount);
|
164 | 130 | } else {
|
165 | 131 | base.setErrors('max');
|
|
173 | 139 | }
|
174 | 140 | };
|
175 | 141 |
|
| 142 | + base.textCount = function(text) { |
| 143 | + var textCount = 0; |
| 144 | + |
| 145 | + if (base.options.type == "word") { // word count |
| 146 | + textCount = base.wordCount(text); |
| 147 | + } |
| 148 | + else { // character count |
| 149 | + textCount = base.characterCount(text); |
| 150 | + } |
| 151 | + |
| 152 | + return textCount; |
| 153 | + }; |
| 154 | + |
| 155 | + base.wordCount = function(text) { |
| 156 | + return text.trim().replace(/\s+/gi, ' ').split(' ').length; |
| 157 | + }; |
| 158 | + |
| 159 | + base.characterCount = function(text) { |
| 160 | + var textCount = 0, |
| 161 | + carriageReturnsCount = 0; |
| 162 | + |
| 163 | + // count carriage returns/newlines as 2 characters |
| 164 | + if (base.options.twoCharCarriageReturn) { |
| 165 | + var carriageReturns = text.match(/(\r\n|\n|\r)/g); |
| 166 | + |
| 167 | + if (carriageReturns !== null) { |
| 168 | + carriageReturnsCount = carriageReturns.length; |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + if (base.options.countSpaces) { // if need to count spaces |
| 173 | + textCount = text.replace(/[^\S\n|\r|\r\n]/g, ' ').length; |
| 174 | + } |
| 175 | + else { |
| 176 | + textCount = text.replace(/\s/g, '').length; |
| 177 | + } |
| 178 | + |
| 179 | + // count extended characters (e.g. Chinese) |
| 180 | + if (base.options.countExtendedCharacters) { |
| 181 | + var extended = text.match(/[^\x00-\xff]/gi); |
| 182 | + |
| 183 | + if (extended == null) { |
| 184 | + textCount = text.length; |
| 185 | + } else { |
| 186 | + textCount = text.length + extended.length; |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + if (base.options.twoCharCarriageReturn) { |
| 191 | + textCount += carriageReturnsCount; |
| 192 | + } |
| 193 | + |
| 194 | + return textCount; |
| 195 | + }; |
| 196 | + |
176 | 197 | base.setCount = function(count) {
|
177 | 198 | base.$text_counter.text(count);
|
178 | 199 | };
|
|
0 commit comments