diff --git a/core/modules/ckeditor/js/plugins/drupalimage/plugin.es6.js b/core/modules/ckeditor/js/plugins/drupalimage/plugin.es6.js
index fda04cbff4..170a03dac7 100644
--- a/core/modules/ckeditor/js/plugins/drupalimage/plugin.es6.js
+++ b/core/modules/ckeditor/js/plugins/drupalimage/plugin.es6.js
@@ -324,6 +324,21 @@
           return;
         }
 
+        widget.on('data', (event) => {
+          // If a link has been set, move the caret from the previous location
+          // to select the image.
+          if (this.data.link) {
+            this.focus();
+            // Adding a link through setData('link') may affect the unlink
+            // button, so refresh the button state to enable/disable it.
+            if (editor.plugins.drupallink) {
+              editor
+                .getCommand('drupalunlink')
+                .refresh(editor, editor.elementPath(this.element));
+            }
+          }
+        });
+
         widget.on('edit', (event) => {
           // Cancel edit event to break image2's dialog binding
           // (and also to prevent automatic insertion before opening dialog).
diff --git a/core/modules/ckeditor/js/plugins/drupalimage/plugin.js b/core/modules/ckeditor/js/plugins/drupalimage/plugin.js
index 34b0cc0519..3b94826112 100644
--- a/core/modules/ckeditor/js/plugins/drupalimage/plugin.js
+++ b/core/modules/ckeditor/js/plugins/drupalimage/plugin.js
@@ -50,6 +50,8 @@
     icons: 'drupalimage',
     hidpi: true,
     beforeInit: function beforeInit(editor) {
+      var _this = this;
+
       editor.on('widgetDefinition', function (event) {
         var widgetDefinition = event.data;
 
@@ -193,6 +195,15 @@
           return;
         }
 
+        widget.on('data', function (event) {
+          if (_this.data.link) {
+            _this.focus();
+
+            if (editor.plugins.drupallink) {
+              editor.getCommand('drupalunlink').refresh(editor, editor.elementPath(_this.element));
+            }
+          }
+        });
         widget.on('edit', function (event) {
           event.cancel();
           editor.execCommand('editdrupalimage', {
diff --git a/core/modules/ckeditor/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php b/core/modules/ckeditor/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php
index 90d60e40eb..76188ee9ba 100644
--- a/core/modules/ckeditor/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php
+++ b/core/modules/ckeditor/tests/src/FunctionalJavascript/CKEditorIntegrationTest.php
@@ -234,4 +234,31 @@ public function testOffCanvasStyles() {
     $this->assertNotEquals($old_keys, $new_keys, 'Clearing caches changed the off-canvas style cache key.');
   }
 
+  /**
+   * Tests if the CKEditor Inserts empty tags into DOM.
+   */
+  public function testDrupalEmptyInsert() {
+    $assert_session = $this->assertSession();
+
+    // Navigate to the page
+    $this->drupalGet('node/add/page');
+    $this->waitForEditor();
+
+    // Issue occurs on several clicks of the source button, when this happens
+    // the ckeditor text area increases.
+    // So to check if the error has been fixed we toggle the source button and
+    // compare the first height to the last height.
+    $this->pressEditorButton('source');
+    $this->assertNotEmpty($assert_session->waitForElement('css', '.cke_contents > textarea'));
+    $first_height = $this->getSession()->evaluateScript("document.getElementById('cke_edit-body-0-value').clientHeight");
+    $this->pressEditorButton('source');
+    $this->assertNotEmpty($assert_session->waitForElement('css', '.cke_contents > span'));
+    $this->pressEditorButton('source');
+    $this->assertNotEmpty($assert_session->waitForElement('css', '.cke_contents > textarea'));
+    $last_height = $this->getSession()->evaluateScript("document.getElementById('cke_edit-body-0-value').clientHeight");
+
+    // If the height is different then empty tag has been entered into the DOM.
+    $this->assertEquals($first_height, $last_height);
+  }
+
 }
