diff --git a/js/plugins/drupalmedia/plugin.es6.js b/js/plugins/drupalmedia/plugin.es6.js
index 7a0c5a4..2ca4de5 100644
--- a/js/plugins/drupalmedia/plugin.es6.js
+++ b/js/plugins/drupalmedia/plugin.es6.js
@@ -482,6 +482,23 @@
                 'label',
                 jqXhr.getResponseHeader('Drupal-Media-Label'),
               );
+              const next = this.element.getParent().getNext();
+              if (
+                next &&
+                typeof next.getName === 'function' &&
+                next.getName() === 'p'
+              ) {
+                const child = next.getFirst();
+                if (
+                  child &&
+                  typeof child.getName === 'function' &&
+                  child.getName() === 'a'
+                ) {
+                  if (child.getText() === ' ') {
+                    next.remove();
+                  }
+                }
+              }
               callback(this);
             },
             error: () => {
diff --git a/js/plugins/drupalmedia/plugin.js b/js/plugins/drupalmedia/plugin.js
index 6269316..da488e3 100644
--- a/js/plugins/drupalmedia/plugin.js
+++ b/js/plugins/drupalmedia/plugin.js
@@ -318,7 +318,15 @@
               _this3.element.setHtml(previewHtml);
 
               _this3.setData('label', jqXhr.getResponseHeader('Drupal-Media-Label'));
-
+              var next = _this3.element.getParent().getNext();
+              if (next && typeof next.getName === 'function' && next.getName() === 'p') {
+                var child = next.getFirst();
+                if (child && typeof child.getName === 'function' && child.getName() === 'a') {
+                  if (child.getText() === ' ') {
+                    next.remove();
+                  }
+                }
+              }
               callback(_this3);
             },
             error: function error() {
diff --git a/tests/src/FunctionalJavascript/MediaTest.php b/tests/src/FunctionalJavascript/MediaTest.php
index e68edeb..7f2a718 100644
--- a/tests/src/FunctionalJavascript/MediaTest.php
+++ b/tests/src/FunctionalJavascript/MediaTest.php
@@ -1032,7 +1032,44 @@ class MediaTest extends WebDriverTestBase {
       ],
     ];
   }
+  /**
+  * Ensure that CKEditor does not add empty <p> tags after linking media.
+  */
+  public function testNoEmptyInsertAfterLinkedMedia() {
+    $assert_session = $this->assertSession();
+
+    // Navigate to the page.
+    $this->drupalGet($this->host->toUrl('edit-form'));
+    $this->waitForEditor();
+    $this->assignNameToCkeditorIframe();
+    $this->getSession()->switchToIFrame('ckeditor');
+
+    // Select the CKEditor <drupal-media> widget and add a link.
+    $drupalmedia = $assert_session->waitForElementVisible('css', 'drupal-media');
+    $this->assertNotEmpty($drupalmedia);
+    $drupalmedia->click();
+  
+    $this->pressEditorButton('drupallink');
+    $assert_session->waitForId('drupal-modal');
+
+    // Enter a link in the link dialog and save.
+    $field = $assert_session->waitForElementVisible('xpath', '//input[@name="attributes[href]"]');
+    $this->assertNotEmpty($field);
+    $field->setValue('https://www.drupal.org');
+    $assert_session->elementExists('css', 'button.form-submit')->press();
+    $this->getSession()->switchToIFrame('ckeditor');
+    $this->assertNotEmpty($assert_session->waitForElementVisible('css', 'drupal-media a'));
+
+    // No <p> tag should be present.
+    $assert_session->assertNoElementAfterWait('css', 'p');
 
+    // Ensure that no extra <p> tag is added after clicking the "source" button.
+    $this->pressEditorButton('source');
+    $this->leaveSourceMode();
+    $this->assertNotEmpty($assert_session->waitForElementVisible('css', 'drupal-media a'));
+    $assert_session->assertNoElementAfterWait('css', 'p');
+  }
+  
   /**
    * Tests preview route access.
    *
