Commit b55fc012 authored by Dries's avatar Dries
Browse files

- Patch #1346166 by Dave Reid: Improve performance of token_replace() if there...

- Patch #1346166 by Dave Reid: Improve performance of token_replace() if there are no tokens to replace.
parent 68fc0fad
...@@ -77,8 +77,13 @@ ...@@ -77,8 +77,13 @@
* Text with tokens replaced. * Text with tokens replaced.
*/ */
function token_replace($text, array $data = array(), array $options = array()) { function token_replace($text, array $data = array(), array $options = array()) {
$text_tokens = token_scan($text);
if (empty($text_tokens)) {
return $text;
}
$replacements = array(); $replacements = array();
foreach (token_scan($text) as $type => $tokens) { foreach ($text_tokens as $type => $tokens) {
$replacements += token_generate($type, $tokens, $data, $options); $replacements += token_generate($type, $tokens, $data, $options);
if (!empty($options['clear'])) { if (!empty($options['clear'])) {
$replacements += array_fill_keys($tokens, ''); $replacements += array_fill_keys($tokens, '');
......
...@@ -1852,6 +1852,9 @@ class TokenReplaceTestCase extends DrupalWebTestCase { ...@@ -1852,6 +1852,9 @@ class TokenReplaceTestCase extends DrupalWebTestCase {
$generated = token_generate('node', $raw_tokens, array('node' => $node), array('sanitize' => FALSE)); $generated = token_generate('node', $raw_tokens, array('node' => $node), array('sanitize' => FALSE));
$this->assertEqual($generated['[node:title]'], $node->title, t('Unsanitized token generated properly.')); $this->assertEqual($generated['[node:title]'], $node->title, t('Unsanitized token generated properly.'));
// Test token replacement when the string contains no tokens.
$this->assertEqual(token_replace('No tokens here.'), 'No tokens here.');
} }
/** /**
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment