Index: sites/all/modules/fb/fb.process.inc
===================================================================
--- sites/all/modules/fb/fb.process.inc	(revision 3707)
+++ sites/all/modules/fb/fb.process.inc	(working copy)
@@ -38,6 +38,7 @@
   }
 
   $patterns = array();
+  $patterns_callback = array();
   $replacements = array();
   $base_path = base_path();
 
@@ -69,18 +70,20 @@
 
     // Add target=_top so that entire pages do not appear within an iframe.
     // TODO: make these pattern replacements more sophisticated, detect whether target is already set.
+    global $target;
     if (isset($options['add_target']) && ($target = $options['add_target'])) {
       // Add target=_top to all links
-      $patterns[] = "|<a ([^>]*)href=\"|";
-      $replacements[] = "<a $1 target=\"{$target}\" href=\"";
+      $patterns_callback[] = "|<a([^>]*)href=\"([^>]*)>|";
+//      $replacements_callback[] = "<a $1 target=\"{$target}\" href=\"$2";
       // Do not change local forms, but do change external ones
       $patterns[] = "|<form([^>]*)action=\"([^:\"]*):|";
       $replacements[] = "<form target=\"{$target}\" $1 action=\"$2:";
     }
     elseif (!isset($options['add_target'])) {
+      $target = '_top';
       // Add target=_top to only external links
-      $patterns[] = "|<a([^>]*)href=\"([^:\"]*):|";
-      $replacements[] = "<a target=\"_top\" $1 href=\"$2:";
+      $patterns_callback[] = "|<a([^>]*)href=\"([^:\"]*)(:[^>]*)>|";
+//      $replacements_callback[] = "<a target=\"_top\" $1 href=\"$2:";
       $patterns[] = "|<form([^>]*)action=\"([^:\"]*):|";
       $replacements[] = "<form target=\"_top\" $1 action=\"$2:";
     }
@@ -94,11 +97,21 @@
   }
   if (count($patterns)) {
     $count = 0;
-    $return = preg_replace($patterns, $replacements, $output, -1, $count);
+    $output = preg_replace($patterns, $replacements, $output, -1, $count);
     //print ("fb_process replaced $count.\n\n"); // debug
-    return $return;
   }
-  else
-    return $output;
+  if (count($patterns_callback)) {
+    $count = 0;
+    $output = preg_replace_callback($patterns_callback, 'fb_process_replace_target_callback', $output, -1, $count);
+    //print ("fb_process (callback) replaced $count.\n\n"); // debug
+  }
+  return $output;
 }
 
+function fb_process_replace_target_callback($findings) {
+  global $target;
+  if (strpos($findings[0], 'target') === FALSE) {
+    return "<a target='" . $target . "' " . $findings[1] .  ' href="' . $findings[2] . '>';
+  }
+  return $findings[0];
+} 
