diff -rup a/includes/media.filter.inc b/includes/media.filter.inc
--- a/includes/media.filter.inc	2013-10-26 01:31:00.000000000 +0200
+++ b/includes/media.filter.inc	2014-08-20 15:43:22.616588503 +0200
@@ -437,24 +437,19 @@ function media_filter_field_parser($tag_
  * Builds a map of the media tags in an element that are being rendered to their
  * rendered HTML. The map is stored in JS, so we can transform them when the
  * editor is being displayed.
+ *
+ * @see _media_generate_tagMap()
+ *
+ * @return array
  */
-function media_pre_render_text_format($element) {
-  // filter_process_format() copies properties to the expanded 'value' child
-  // element.
-  if (!isset($element['format'])) {
-    return $element;
+function media_pre_render_text_format(array $element) {
+  if (isset($element['format'])) {
+    // The "drupal_add_js" function is merge settings and we get an correct array.
+    drupal_add_js(array(
+      'tagmap' => _media_generate_tagMap($element['value']['#value']),
+    ), 'setting');
   }
 
-  $field = &$element['value'];
-  $settings = array(
-    'field' => $field['#id'],
-  );
-
-  $tagmap = _media_generate_tagMap($field['#value']);
-
-  if (isset($tagmap)) {
-    drupal_add_js(array('tagmap' => $tagmap), 'setting');
-  }
   return $element;
 }
 
@@ -470,35 +465,38 @@ function media_pre_render_text_format($e
  * @return array
  *   An associative array with tag code as key and html markup as the value.
  *
- * @see media_process_form()
  * @see media_token_to_markup()
+ *
+ * @return array
  */
 function _media_generate_tagMap($text) {
-  // Making $tagmap static as this function is called many times and
-  // adds duplicate markup for each tag code in Drupal.settings JS,
-  // so in media_process_form it adds something like tagCode:<markup>,
-  // <markup> and when we replace in attach see two duplicate images
-  // for one tagCode. Making static would make function remember value
-  // between function calls. Since media_process_form is multiple times
-  // with same form, this function is also called multiple times.
-  static $tagmap = array();
-  preg_match_all("/\[\[.*?\]\]/s", $text, $matches, PREG_SET_ORDER);
-  foreach ($matches as $match) {
-    // We see if tagContent is already in $tagMap, if not we add it
-    // to $tagmap.  If we return an empty array, we break embeddings of the same
-    // media multiple times.
-    if (empty($tagmap[$match[0]])) {
-      // @TODO: Total HACK, but better than nothing.
-      // We should find a better way of cleaning this up.
-      if ($markup_for_media = media_token_to_markup($match, TRUE)) {
-        $tagmap[$match[0]] = $markup_for_media;
-      }
-      else {
-        $missing = file_create_url(drupal_get_path('module', 'media') . '/images/icons/default/image-x-generic.png');
-        $tagmap[$match[0]] = '<div><img src="' . $missing . '" width="100px" height="100px"/></div>';
+  /**
+   * @var string
+   *   This variable was defined as static because the functions
+   *   "drupal_get_path" and "file_create_url" placed into loop body
+   *   and, without "static" keyword, could be called multiple times.
+   */
+  static $missing = '';
+
+  preg_match_all(MEDIA_TOKEN_REGEX, $text, $matches);
+  $tagmap = array();
+
+  foreach ($matches[0] as $match) {
+    $markup = media_token_to_markup(array($match), TRUE);
+
+    if ($markup) {
+      $tagmap[$match] = $markup;
+    }
+    else {
+      if (!$missing) {
+        $missing = drupal_get_path('module', 'media');
+        $missing = file_create_url("$missing/images/icons/default/image-x-generic.png");
       }
+
+      $tagmap[$match] = '<div><img src="' . $missing . '" width="100" height="100"/></div>';
     }
   }
+
   return $tagmap;
 }
 
