Index: includes/token.inc
===================================================================
RCS file: /cvs/drupal/drupal/includes/token.inc,v
retrieving revision 1.8
diff -u -p -r1.8 token.inc
--- includes/token.inc	25 Jan 2010 10:38:34 -0000	1.8
+++ includes/token.inc	27 Feb 2010 20:34:35 -0000
@@ -78,6 +78,9 @@ function token_replace($text, array $dat
   $replacements = array();
   foreach (token_scan($text) as $type => $tokens) {
     $replacements += token_generate($type, $tokens, $data, $options);
+    if (!empty($options['clear'])) {
+      $replacements += array_fill_keys($tokens, '');
+    }
   }
 
   // Optionally alter the list of replacement values.
Index: modules/system/system.test
===================================================================
RCS file: /cvs/drupal/drupal/modules/system/system.test,v
retrieving revision 1.112
diff -u -p -r1.112 system.test
--- modules/system/system.test	17 Feb 2010 22:44:52 -0000	1.112
+++ modules/system/system.test	27 Feb 2010 20:34:58 -0000
@@ -1408,26 +1408,30 @@ class TokenReplaceTestCase extends Drupa
     $source .= '[node:author:name]';   // Node author's name
     $source .= '[node:created:since]'; // Time since the node was created
     $source .= '[current-user:name]';  // Current user's name
-    $source .= '[user:name]';          // No user passed in, should be untouched
     $source .= '[date:short]';         // Short date format of REQUEST_TIME
-    $source .= '[bogus:token]';        // Nonexistent token, should be untouched
+    $source .= '[user:name]';          // No user passed in, should be untouched
+    $source .= '[bogus:token]';        // Non-existent token
 
     $target  = check_plain($node->title);
     $target .= check_plain($account->name);
     $target .= format_interval(REQUEST_TIME - $node->created, 2, $language->language);
     $target .= check_plain($user->name);
-    $target .= '[user:name]';
     $target .= format_date(REQUEST_TIME, 'short', '', NULL, $language->language);
-    $target .= '[bogus:token]';
 
+    // Test that the clear parameter cleans out non-existent tokens.
+    $result = token_replace($source, array('node' => $node), array('language' => $language, 'clear' => TRUE));
+    $result = $this->assertFalse(strcmp($target, $result), 'Valid tokens replaced while invalid tokens cleared out.');
+
+    // Test without using the clear parameter (non-existant token untouched).
+    $target .= '[user:name]';
+    $target .= '[bogus:token]';
     $result = token_replace($source, array('node' => $node), array('language' => $language));
+    $this->assertFalse(strcmp($target, $result), 'Valid tokens replaced while invalid tokens ignored.');
 
     // Check that the results of token_generate are sanitized properly. This does NOT
     // test the cleanliness of every token -- just that the $sanitize flag is being
     // passed properly through the call stack and being handled correctly by a 'known'
     // token, [node:title].
-    $this->assertFalse(strcmp($target, $result), t('Basic placeholder tokens replaced.'));
-
     $raw_tokens = array('title' => '[node:title]');
     $generated = token_generate('node', $raw_tokens, array('node' => $node));
     $this->assertFalse(strcmp($generated['[node:title]'], check_plain($node->title)), t('Token sanitized.'));
@@ -1625,4 +1629,4 @@ class ShutdownFunctionsTest extends Drup
     $this->assertText(t('First shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2)));
     $this->assertText(t('Second shutdown function, arg1 : @arg1, arg2: @arg2', array('@arg1' => $arg1, '@arg2' => $arg2)));
   }
-}
\ No newline at end of file
+}
