diff --git a/js/maxlength.js b/js/maxlength.js index af816cb..0631955 100644 --- a/js/maxlength.js +++ b/js/maxlength.js @@ -26,6 +26,9 @@ if ($(this).hasClass('maxlength_js_truncate_html')) { options['truncateHtml'] = true; } + if ($(this).hasClass('maxlength_js_exclude_spaces')) { + options['excludeSpaces'] = true; + } $(this).charCount(options); }); }, @@ -59,10 +62,10 @@ if (count == undefined) { if (options.truncateHtml) { - count = ml.strip_tags(obj.val()).length; + count = ml.strip_spaces(options, ml.strip_tags(obj.val())).length; } else { - count = ml.twochar_lineending(obj.val()).length; + count = ml.twochar_lineending(ml.strip_spaces(options, obj.val())).length; } } @@ -84,22 +87,22 @@ 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; + count = ml.strip_spaces(options, 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; + count = ml.strip_spaces(options, 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; + count = ml.strip_spaces(options, 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; + count = ml.twochar_lineending(ml.strip_spaces(options, obj.val())).length; } } } @@ -252,7 +255,8 @@ counterText: Drupal.t('Content limited to @limit characters, remaining: @remaining'), action: 'attach', enforce: false, - truncateHtml: false + truncateHtml: false, + excludeSpaces: false }; var options = $.extend(defaults, options); @@ -400,4 +404,9 @@ e.editor.setData(data); } + // Remove spaces from input string + ml.strip_spaces = function(options, str) { + return (options.excludeSpaces) ? str.replace(/\s/g, "") : str; + }; + })(jQuery); diff --git a/maxlength.module b/maxlength.module index cc7c7af..a437442 100644 --- a/maxlength.module +++ b/maxlength.module @@ -33,6 +33,9 @@ function maxlength_pre_render($element) { $element['#attributes']['maxlength'] = $element['#maxlength']; } $element['#attributes']['class'][] = 'maxlength'; + if(isset($element['#maxlength_js_exclude_spaces']) && $element['#maxlength_js_exclude_spaces']) { + $element['#attributes']['class'][] = 'maxlength_js_exclude_spaces'; + } $element['#attached']['js'][] = drupal_get_path('module', 'maxlength') . '/js/maxlength.js'; } return $element; @@ -98,6 +101,15 @@ function maxlength_form_field_ui_field_edit_form_alter(&$form, &$form_state, $fo ); } + if (isset($form['instance']['widget']['settings']['maxlength_js'])) { + $form['instance']['widget']['settings']['maxlength_js_exclude_spaces'] = array( + '#type' => 'checkbox', + '#title' => 'Exclude spaces from count', + '#description' => t('Check this option if you want to ignore spaces when counting the characters.'), + '#default_value' => isset($form['#instance']['widget']['settings']['maxlength_js_exclude_spaces']) ? $form['#instance']['widget']['settings']['maxlength_js_exclude_spaces'] : NULL, + ); + } + $form['instance']['widget']['settings']['maxlength_js_label'] = array( '#type' => 'textarea', '#rows' => 2, @@ -151,6 +163,9 @@ function _maxlength_children(&$element, $ms_elements) { if (isset($settings['maxlength_js_truncate_html']) && $settings['maxlength_js_truncate_html']) { $element[$child]['#maxlength_js_truncate_html'] = TRUE; } + if (isset($settings['maxlength_js_exclude_spaces']) && $settings['maxlength_js_exclude_spaces']) { + $element[$child]['#maxlength_js_exclude_spaces'] = TRUE; + } } if (isset($element[$child]['summary']) && isset($settings['maxlength_js_summary']) @@ -187,6 +202,10 @@ function maxlength_process_element($element, &$form_state) { $element['#attributes']['class'][] = 'maxlength_js_truncate_html'; unset($element['#maxlength']); } + + if (isset($element['#maxlength_js_enforce']) && $element['#maxlength_js_enforce']) { + $element['#attributes']['class'][] = 'maxlength_js_enforce'; + } return $element; }