--- maxlength.module	2010-06-26 22:48:06.000000000 +0200
+++ maxlength.module	2011-01-15 14:22:34.000000000 +0100
@@ -183,15 +183,16 @@ function _maxlength_format_element(&$ele
     $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'
       );
 
@@ -201,12 +202,15 @@ function _maxlength_format_element(&$ele
 
     $js_settings = array(
       'maxlength' => array(
+        'edit-'. $id => $values['limit'] + ($raw_length - $text_length),
+      ),
+      'maxlength_original' => array(
         'edit-'. $id => $values['limit'],
       ),
     );
     drupal_add_js($js_settings, 'setting');
     $element['#suffix'] = '<div id="maxlength-'. $id .'"
-      class="maxlength-counter">'. t($values['text'], array('!limit' => $values['limit'], '!count' =>'<span class="maxlength-count">'. drupal_strlen($value).'</span>', '!remaining' => '<span class="maxlength-counter-remaining">'. $remaining .'</span>')) .'</div>';
+      class="maxlength-counter">'. t($values['text'], array('!limit' => $values['limit'], '!count' =>'<span class="maxlength-count">'. $text_length.'</span>', '!remaining' => '<span class="maxlength-counter-remaining">'. $remaining .'</span>')) .'</div>';
   }
 }
 
@@ -234,6 +238,18 @@ function _maxlength_get_values($field = 
   }
 }
 
+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 (&eacute; 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;
+}
+
 /**
  * Implementation of hook_nodeapi().
  *
@@ -254,8 +270,9 @@ function maxlength_nodeapi(&$node, $op, 
           $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)));
           }
           break;
         }
