diff --git a/sites/all/modules/maxlength/maxlength.inc b/sites/all/modules/maxlength/maxlength.inc index 5ac96a2..a96c63d 100644 --- a/sites/all/modules/maxlength/maxlength.inc +++ b/sites/all/modules/maxlength/maxlength.inc @@ -149,3 +149,14 @@ function _maxlength_get_values($field = 'body', $type = '') { } } +function _maxlength_strip_html($raw) { + if (module_exists('wysiwyg_maxlength')) { + // 1. remove linebreaks (artificially replaced by the character 'X') + // 2. remove html tags + // 3. replace multiple spaces by only one + // 4. consider HTML entities (é for example) as one character (artificially replaced by the character 'X') + // 5. remove white spaces at the beginning and end of the text. + return trim(preg_replace('/&\w+;/', 'X',preg_replace('/<[^>]*>/', '', str_replace("\r\n",'X',$raw)))); + } + return $raw; +} diff --git a/sites/all/modules/maxlength/maxlength.module b/sites/all/modules/maxlength/maxlength.module index 50ae0ce..784d186 100644 --- a/sites/all/modules/maxlength/maxlength.module +++ b/sites/all/modules/maxlength/maxlength.module @@ -81,8 +81,9 @@ function maxlength_nodeapi(&$node, $op, $a3 = NULL, $a4 = NULL) { $text = str_replace("\r\n", '#', $text); $text = str_replace("\n", '#', $text); $text = str_replace("\r", '#', $text); + $text = _maxlength_strip_html($text); if (drupal_strlen($text) > $limit) { - form_set_error($field, t('The !field field has exceeded its maximum number of characters (!limit).', array('!limit' => $limit, '!field' => $field))); + form_set_error($field, t('The !field field has exceeded its maximum number of characters (!length instead of !limit).', array('!length' => drupal_strlen($text), '!limit' => $limit, '!field' => $field))); } } } @@ -140,15 +141,16 @@ function _maxlength_format_element($element) { $path = drupal_get_path('module', 'maxlength'); drupal_add_js($path .'/maxlength.js'); - $remaining = $values['limit'] - drupal_strlen($value); - + $raw_length = drupal_strlen($value); + $text_length = drupal_strlen(_maxlength_strip_html($value)); + $remaining = $values['limit'] - $text_length; + if ($remaining < 0) { drupal_set_message( t('%body_field_label truncated to %limit characters!', array( '%body_field_label' => $element['#title'], - '%limit' => $values['limit']) - ), + '%limit' => $values['limit'])), 'error' ); @@ -159,13 +161,16 @@ function _maxlength_format_element($element) { if (in_array($element['#id'], $processed) === FALSE) { $js_settings = array( 'maxlength' => array( - 'edit-'. $id => $values['limit'], + 'edit-'. $id => $values['limit'] + ($raw_length - $text_length), + ), + 'maxlength_original' => array( + 'edit-'. $id => $values['limit'], ), ); drupal_add_js($js_settings, 'setting'); } $element['#suffix'] = '