diff --git a/js/wysiwyg-media.js b/js/wysiwyg-media.js
index cdf5ab2..2c23a98 100644
--- a/js/wysiwyg-media.js
+++ b/js/wysiwyg-media.js
@@ -9,6 +9,38 @@
 Drupal.media = Drupal.media || {};
 
 /**
+ * Prevent media markup from being left behind in the WYSIWYG during editing.
+ *
+ * When an embedded document is placed in the WYSIWYG, and later selected and
+ * either (1) deleted, or (2) overwritten with other text, it is easy for the
+ * visible markup (i.e., the file icon and filename) to completely disappear
+ * but the underlying, invisible media token to be left behind in the HTML. If
+ * this happens, then after the content is saved the embedded document will
+ * reappear despite having already been deleted from the user's perspective.
+ *
+ * This function fixes the issue by forcing the full media element (including
+ * the invisible token) to be selected once enough of it is.
+ *
+ * @todo: Make this work for editors other than CKEditor.
+ */
+if (typeof CKEDITOR !== 'undefined') {
+  CKEDITOR.on('instanceReady', function (event) {
+    event.editor.on('selectionChange', function (event) {
+      // If something more than just the filename link was selected, change the
+      // selection to encompass the entire media element.
+      var $selected_element = $(event.data.selection.getStartElement().$);
+      if (!$selected_element.is('a')) {
+        var $full_element = $selected_element.closest('.media-element');
+        if ($full_element.length) {
+          var full_element = new CKEDITOR.dom.element($full_element.get(0));
+          event.data.selection.selectElement(full_element);
+        }
+      }
+    });
+  });
+}
+
+/**
  * Register the plugin with WYSIWYG.
  */
 Drupal.wysiwyg.plugins.media = {
