-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmaxlength.js
417 lines (390 loc) · 14.7 KB
/
maxlength.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
(function($) {
// Make evrything local
var ml = ml || {};
ml.options = ml.options || {};
Backdrop.behaviors.maxlength = {
attach: function(context, settings) {
if (typeof(CKEDITOR) != 'undefined') {
ml.ckeditor();
}
$('.maxlength', context).once('maxlength', function() {
var options = {};
options['counterText'] = $(this).attr('maxlength_js_label');
if ($(this).hasClass('maxlength_js_enforce')) {
options['enforce'] = true;
}
if ($(this).hasClass('maxlength_js_truncate_html')) {
options['truncateHtml'] = true;
}
$(this).charCount(options);
});
},
detach: function(context, settings) {
$('.maxlength', context).removeOnce('maxlength', function() {
$(this).charCount({
action: 'detach'
});
});
}
};
/**
* Code below is based on:
* Character Count Plugin - jQuery plugin
* Dynamic character count for text areas and input fields
* written by Alen Grakalic
* http://cssglobe.com/post/7161/jquery-plugin-simplest-twitterlike-dynamic-character-count-for-textareas
*
* @param obj
* a jQuery object for input elements
* @param options
* an array of options.
* @param count
* In case obj.val() wouldn't return the text to count, this should
* be passed with the number of characters.
*/
ml.calculate = function(obj, options, count, wysiwyg, getter, setter) {
var counter = $('#' + obj.attr('id') + '-' + options.css);
var limit = parseInt(obj.attr('maxlength'));
if (count == undefined) {
if (options.truncateHtml) {
count = ml.strip_tags(obj.val()).length;
}
else {
count = ml.twochar_lineending(obj.val()).length;
}
}
var available = limit - count;
if (available <= options.warning) {
counter.addClass(options.cssWarning);
}
else {
counter.removeClass(options.cssWarning);
}
if (available < 0) {
counter.addClass(options.cssExceeded);
// Trim text.
if (options.enforce) {
if (wysiwyg != undefined) {
if (typeof ml[getter] == 'function' && typeof ml[setter] == 'function') {
if (options.truncateHtml) {
var new_html = ml.truncate_html(ml[getter](wysiwyg), limit)
ml[setter](wysiwyg, new_html);
count = ml.strip_tags(new_html).length;
} else {
var new_html = ml[getter](wysiwyg).substr(0, limit);
ml[setter](wysiwyg, new_html);
count = new_html.length;
}
}
} else {
if (options.truncateHtml) {
obj.val(ml.truncate_html(obj.val(), limit));
// Re calculate text length
count = ml.strip_tags(obj.val()).length;
} else {
obj.val(obj.val().substr(0, limit));
// Re calculate text length
count = ml.twochar_lineending(obj.val()).length;
}
}
}
available = limit - count;
}
else {
counter.removeClass(options.cssExceeded);
}
counter.html(options.counterText.replace('@limit', limit).replace('@remaining', available).replace('@count', count));
};
/**
* Replaces line ending with to chars, because PHP-calculation counts with two chars
* as two characters.
*
* @see http://www.sitepoint.com/blogs/2004/02/16/line-endings-in-javascript/
*/
ml.twochar_lineending = function(str) {
return str.replace(/(\r\n|\r|\n)/g, "\r\n");
};
ml.strip_tags = function(input, allowed) {
// making the lineendings with two chars
input = ml.twochar_lineending(input);
// We do want that the space characters to count as 1, not 6...
input = input.replace(' ', ' ');
//input = input.split(' ').join('');
// Strips HTML and PHP tags from a string
allowed = (((allowed || "") + "")
.toLowerCase()
.match(/<[a-z][a-z0-9]*>/g) || [])
.join(''); // making sure the allowed arg is a string containing only tags in lowercase (<a><b><c>)
var tags = /<\/?([a-z][a-z0-9]*)\b[^>]*>/gi,
commentsAndPhpTags = /<!--[\s\S]*?-->|<\?(?:php)?[\s\S]*?\?>/gi;
return input.replace(commentsAndPhpTags, '').replace(tags, function($0, $1){
return allowed.indexOf('<' + $1.toLowerCase() + '>') > -1 ? $0 : '';
});
};
/**
* Cuts a html text up to limit characters. Still experimental.
*/
ml.truncate_html = function(text, limit) {
// The html result after cut.
var result_html = '';
// The text result, that will actually used when counting characters.
var result_text = '';
// A stack that will keep the tags that are open at a given time.
var tags_open = new Array();
// making the lineendings with two chars
text = ml.twochar_lineending(text);
// We do want that the space characters to count as 1, not 6...
text = text.replace(' ', ' ');
while (result_text.length < limit && text.length > 0) {
switch (text.charAt(0)) {
case '<': {
if (text.charAt(1) != '/') {
var tag_name = '';
var tag_name_completed = false;
while (text.charAt(0) != '>' && text.length > 0) {
var first_char = text.charAt(0).toString();
// Until the tag is closed, we do not append anything
// to the visible text, only to the html.
result_html += first_char;
// Also, check if we have a valid tag name.
if (!tag_name_completed && first_char == ' ') {
// We have the tag name, so push it into the stack.
tag_name_completed = true;
tags_open.push(tag_name);
}
// Check if we are still in the tag name.
if (!tag_name_completed && first_char != '<') {
tag_name += first_char;
}
//If we have the combination "/>" it means that the tag
//is closed, so remove it from the open tags stack.
if (first_char == '/' && text.length > 1 && text.charAt(1) == '>') {
tags_open.pop();
}
//Done with this char, remove it from the original text.
text = text.substring(1);
}
if (!tag_name_completed) {
// In this case we have a tag like "<strong>some text</strong> so
// we did not have any attributes in the tag, but still, the tag
// has to be marked as open.
tags_open.push(tag_name);
}
//We are here, then the tag is closed, so just remove the
//remaining ">" character.
if (text.length > 0) {
result_html += text.charAt(0).toString();
}
} else {
// In this case, we have an ending tag.
// The name of the ending tag should match the last open tag,
// otherwise, something is wrong with th html text.
var tag_name = '';
while (text.charAt(0) != '>' && text.length > 0) {
var first_char = text.charAt(0).toString();
if (first_char != '<' && first_char != '/') {
tag_name += first_char;
}
result_html += first_char;
text = text.substring(1);
}
if (text.length > 0) {
result_html = result_html + text.charAt(0).toString();
}
// Pop the last element from the tags stack and compare it with
// the tag name.
var expected_tag_name = tags_open.pop();
if (expected_tag_name != tag_name) {
//Should throw an exception, but for the moment just alert.
alert('Expected end tag: ' + expected_tag_name + '; Found end tag: '+tag_name);
}
}
break;
}
default: {
// In this case, we have a character that should also count for the
// limit, so append it to both, the html and text result.
var first_char = text.charAt(0).toString();
result_html += first_char;
result_text += first_char;
break;
}
}
// Remove the first character, it did its job.
text = text.substring(1);
}
// Restore the open tags that were not closed. This happens when the text
// got truncated in the middle of one or more html tags.
var tag = '';
while (tag = tags_open.pop()) {
result_html += '</' + tag + ">";
}
return result_html;
}
$.fn.charCount = function(options) {
// default configuration properties
var defaults = {
warning: 10,
css: 'counter',
counterElement: 'div',
cssWarning: 'messages warning',
cssExceeded: 'error',
counterText: Backdrop.t('Content limited to @limit characters, remaining: <strong>@remaining</strong>'),
action: 'attach',
enforce: false,
truncateHtml: false
};
var options = $.extend(defaults, options);
ml.options[$(this).attr('id')] = options;
if (options.action == 'detach') {
$(this).removeClass('maxlength-processed');
$('#' + $(this).attr('id') + '-' + options.css).remove();
delete ml.options[$(this).attr('id')];
return 'removed';
}
var sanitizedId = ($(this).attr('id') + '-' + options.css).replace(/[^0-9a-z-_]/gi, '');
var counterElement = $('<' + options.counterElement + ' id="' + sanitizedId + '" class="' + options.css + '"></' + options.counterElement + '>');
if ($(this).next('div.grippie').length) {
$(this).next('div.grippie').after(counterElement);
} else {
$(this).after(counterElement);
}
ml.calculate($(this), options);
$(this).keyup(function() {
ml.calculate($(this), options);
});
$(this).change(function() {
ml.calculate($(this), options);
});
};
/**
* Integrate with WYSIWYG
* Detect changes on editors and invoke ml.calculate()
*/
ml.tinymce = function() {
// We only run it once
var onlyOnce = false;
if (!onlyOnce) {
onlyOnce = true;
tinyMCE.onAddEditor.add(function(mgr,ed) {
// the editor is on a maxlength field
var editor = $('#' + ed.editorId + '.maxlength');
if (editor.length == 1) {
if (editor.hasClass('maxlength_js_enforce')) {
ml.options[ed.editorId].enforce = true;
} else {
ml.options[ed.editorId].enforce = false;
}
// Check if we should strip the tags when counting.
if (editor.hasClass('maxlength_js_truncate_html')) {
ml.options[ed.editorId].truncateHtml = true;
} else {
ml.options[ed.editorId].truncateHtml = false;
}
ed.onChange.add(function(ed, l) {
ml.tinymceChange(ed);
});
ed.onKeyUp.add(function(ed, e) {
ml.tinymceChange(ed);
});
ed.onPaste.add(function(ed, e) {
setTimeout(function(){ml.tinymceChange(ed)}, 500);
});
}
});
}
}
// Invoke ml.calculate() for editor
ml.tinymceChange = function(ed) {
// CLone to avoid changing defaults
var options = $.extend({}, ml.options[ed.editorId]);
/* The following lines take the text from the wysiwyg and
strip out some html and special characters so an accurate character
count can be taken. */
var bodyContent = ml.tinymceGetData(ed);
bodyContent = bodyContent.replace(/>/g, ' ');
bodyContent = bodyContent.replace(/</g, ' ');
bodyContent = bodyContent.replace(/&/g, ' ');
bodyContent = bodyContent.replace(/<\/?[^>]+(>|$)/g, "");
bodyContent = bodyContent.replace(/\<p>/g, "");
bodyContent = bodyContent.replace(/\<\/p>/g, "");
bodyContent = bodyContent.replace(/[\n\r]/g, '');
bodyContent = bodyContent.replace(/&/g, '&');
bodyContent = bodyContent.replace(/ /g, ' ');
bodyContent = bodyContent.replace(/\<span>/g, ' ');
bodyContent = bodyContent.replace(/\<\/span>/g, ' ');
bodyContent = bodyContent.replace(/\<br>/g, ' ');
bodyContent = bodyContent.replace(/\<BR>/g, ' ');
bodyLength = bodyContent.length;
if (options.truncateHtml){
ml.calculate($(ed.getElement()), options, bodyLength, 'tinymceGetData', 'tinymceSetData');
}
else {
ml.calculate($(ed.getElement()), options, bodyLength, ed, 'tinymceGetData', 'tinymceSetData');
}
};
// Gets the data from the tinyMCE. Not tested yet.
ml.tinymceGetData = function(e) {
return e.getContent();
}
// Sets the data into a tinyMCE. Not tested yet.
ml.tinymceSetData = function(e, data) {
e.setContent(data);
}
/**
* Integrate with ckEditor
* Detect changes on editors and invoke ml.calculate()
*/
ml.ckeditor = function() {
// We only run it once
var onlyOnce = false;
if (!onlyOnce) {
onlyOnce = true;
CKEDITOR.on('instanceReady', function(e) {
var editor = $('#' + e.editor.name + '.maxlength');
if (editor.length == 1) {
if (editor.hasClass('maxlength_js_enforce')) {
ml.options[e.editor.element.getId()].enforce = true;
} else {
ml.options[e.editor.element.getId()].enforce = false;
}
// Check if we should strip the tags when counting.
if (editor.hasClass('maxlength_js_truncate_html')) {
ml.options[e.editor.element.getId()].truncateHtml = true;
} else {
ml.options[e.editor.element.getId()].truncateHtml = false;
}
// Add the events on the editor.
e.editor.on('key', function(e) {
setTimeout(function(){ml.ckeditorChange(e)}, 100);
});
e.editor.on('paste', function(e) {
setTimeout(function(){ml.ckeditorChange(e)}, 500);
});
e.editor.on('elementsPathUpdate', function(e) {
setTimeout(function(){ml.ckeditorChange(e)}, 100);
});
}
});
}
}
// Invoke ml.calculate() for editor
ml.ckeditorChange = function(e) {
// Clone to avoid changing defaults
var options = $.extend({}, ml.options[e.editor.element.getId()]);
if (options.truncateHtml){
ml.calculate($('#' + e.editor.element.getId()), options, ml.strip_tags(ml.ckeditorGetData(e)).length, e, 'ckeditorGetData', 'ckeditorSetData');
}
else {
ml.calculate($('#' + e.editor.element.getId()), options, ml.twochar_lineending(ml.ckeditorGetData(e)).length, e, 'ckeditorGetData', 'ckeditorSetData');
}
};
// Gets the data from the ckeditor.
ml.ckeditorGetData = function(e) {
return e.editor.getData();
}
// Sets the data into a ckeditor.
ml.ckeditorSetData = function(e, data) {
e.editor.setData(data);
}
})(jQuery);