--- invisimail.module.orig	2008-08-13 11:47:50.000000000 -0700
+++ invisimail.module	2008-08-13 12:15:44.000000000 -0700
@@ -87,13 +87,17 @@
 }
 
 function invisimail($string, $format) {
-  $pattern = "!(<p>|<li>|<br />|[ \n\r\t\(])([A-Za-z0-9._-]+@[A-Za-z0-9._-]+\.[A-Za-z]{2,4})([.,]?)(?=(</p>|</li>|<br />|[ \n\r\t\)]))!i";
-  
   // the callback needs to know what filter we're using
   // however there's no way to hand off that variable
   // so we'll set a global variable
   $GLOBALS['invisimail_format'] = $format;
 
+  // First fix addresses that are already linked
+  $pattern = "!<a([^>]+?)href=[\"']mailto:([^\"']+?)[\"'](.*?)>(.*?)</a>!is";
+  $string = preg_replace_callback($pattern, 'invisimail_callback2', $string);
+
+  $pattern = "!(^|<p>|<td>|<li>|<br />|[ \n\r\t\(])([A-Za-z0-9._-]+@[A-Za-z0-9._-]+\.[A-Za-z]{2,4})([.,]?)(?=(</p>|</td>|</li>|<br />|[ \n\r\t\)]|$))!i";
+
   return preg_replace_callback($pattern, 'invisimail_callback', $string);
  }
 
@@ -105,6 +109,20 @@
   return $matches[1] . invisimail_ascii_encode($matches[2], $js, $link) . $matches[3];
 }
 
+function invisimail_callback2($matches) {
+  $format = $GLOBALS['invisimail_format'];
+  $js = variable_get('invisimail_js_'.$format, FALSE);
+  $link = variable_get('invisimail_link_'.$format, FALSE);
+  $attribs = $matches[1] . $matches[3];
+  $text = null;
+  // If the text is not the email address we don't want it encoded
+  if ($matches[2] != trim($matches[4])) {
+    $text = $matches[4];
+  }
+
+  return invisimail_ascii_encode($matches[2], $js, $link, $text, $attribs);
+}
+
 /**
  * This function does the actual encoding. 
  *
@@ -116,14 +134,12 @@
  *   Optional: A boolean which set whether the result includes a mailto link.
  * @param $text
  *   Optional: The text to be used for the link.
+ * @param $attribs
+ *   Optional: Any additional attributes for the anchor tag
  * @return
  *   An ascii encoded email address. 
  */
-function invisimail_ascii_encode($string, $js = FALSE, $link = FALSE, $text = NULL) {
-  if ($text == NULL) {
-    $text = $string;
-  }
-  
+function invisimail_ascii_encode($string, $js = FALSE, $link = FALSE, $text = NULL, $attribs = '') {
   if ($js) {
     $output = "<script type='text/javascript'><!--
     document.write('";
@@ -141,11 +157,15 @@
   }
   
   if ($link && !$js) {
+    // If text for link is empty, use encoded email address
+    $linkText = $text == null ? $encode : $text;
     // ascii in this next line is "mailto:"
-    $output .= "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;$encode\">$text</a>";
+    $output .= "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;$encode\"$attribs>$linkText</a>";
   }
   elseif ($link && $js) {
-    $output .= "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;'+'$encode'+'\">'+'$text'+'</a>";    
+    $attribs = str_replace("'", "\\'", $attribs);
+    $linkText = $text == null ? $encode : str_replace("'", "\\'", $text);
+    $output .= "<a href=\"&#109;&#97;&#105;&#108;&#116;&#111;&#58;'+'$encode'+'\"$attribs>'+'$linkText'+'</a>";    
   }
   else {
     $output .= $encode;
