diff -u b/core/misc/ajax.js b/core/misc/ajax.js
--- b/core/misc/ajax.js
+++ b/core/misc/ajax.js
@@ -1015,6 +1015,10 @@
      *   The XMLHttpRequest status.
      */
     insert: function (ajax, response, status) {
+      if (!response.data) {
+        return;
+      }
+
       // Get information from the response. If it is not there, default to
       // our presets.
       var $wrapper = response.selector ? $(response.selector) : $(ajax.wrapper);
@@ -1031,21 +1035,26 @@
       // context object that includes top-level text nodes. Therefore text nodes
       // will be wrapped with a <span> element. This also gives themers the
       // possibility to style the response.
-
-      var $responseDataWrapped = $('<div></div>').html(response.data);
-      var $new_content = $('<div></div>');
+      var $new_content;
 
       // If only NODE_ELEMENT or COMMENT_NODE elements are returned skip wrap processing.
-      var responseDataWrappedContents = $.parseHTML(response.data, true);
+      var responseDataWrappedContents = $.parseHTML(response.data.trim(), true);
       var isAllElementNode = responseDataWrappedContents.every(function (element) {
         return element.nodeType === Node.ELEMENT_NODE || element.nodeType === Node.COMMENT_NODE;
       });
 
       if (!isAllElementNode) {
-        $new_content = $responseDataWrapped;
+        // If response.data contains TEXT_ELEMENT type only or target element
+        // is not a block level element, response.data will be wrapped with SPAN.
+        if (responseDataWrappedContents.length === 1 || ($wrapper.css('display') !== 'block')) {
+          $new_content = $('<span class="ajax-inline"></span>').html(response.data);
+        }
+        else {
+          $new_content = $('<div></div>').html(response.data);
+        }
       }
       else {
-        $new_content = $responseDataWrapped.contents();
+        $new_content = $(response.data);
       }
 
       // If removing content from the wrapper, detach behaviors first.
diff -u b/core/modules/system/tests/modules/ajax_test/js/insert-ajax.js b/core/modules/system/tests/modules/ajax_test/js/insert-ajax.js
--- b/core/modules/system/tests/modules/ajax_test/js/insert-ajax.js
+++ b/core/modules/system/tests/modules/ajax_test/js/insert-ajax.js
@@ -21,6 +21,19 @@
         myAjaxObject.execute();
       });
 
+      $('.ajax-insert-inline').once('ajax-insert').on('click', function (event) {
+        event.preventDefault();
+        var ajaxSettings = {
+          url: event.currentTarget.getAttribute('href'),
+          wrapper: 'ajax-target-inline',
+          base: false,
+          element: false,
+          method: event.currentTarget.getAttribute('data-method')
+        };
+        var myAjaxObject = Drupal.ajax(ajaxSettings);
+        myAjaxObject.execute();
+      });
+
       $(context).addClass('processed');
     }
   };
diff -u b/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php b/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php
--- b/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php
+++ b/core/modules/system/tests/modules/ajax_test/src/Controller/AjaxTestController.php
@@ -74,7 +74,7 @@
 
     $build['links'] = [
       'ajax_target' => [
-        '#markup' => '<div id="ajax-target">Target</div>',
+        '#markup' => '<div id="ajax-target">Target</div><div><strong>Inline Test:</strong></div><span>TEXT</span> <span id="ajax-target-inline"></span> <span>TEXT</span>',
       ],
       'links' => [
         '#theme' => 'links',
@@ -83,11 +83,12 @@
     ];
     foreach ($methods as $method) {
       foreach (array_keys($this->getRenderTypes()) as $type) {
+        $class = $type == 'inline' ? 'ajax-insert-inline' : 'ajax-insert';
         $build['links']['links']['#links']["$method-$type"] = [
           'title' => "Link $method $type",
           'url' => Url::fromRoute('ajax_test.ajax_render_types', ['type' => $type]),
           'attributes' => [
-            'class' => ['ajax-insert'],
+            'class' => [$class],
             'data-method' => $method,
           ],
         ];
@@ -284,11 +285,13 @@
    */
   protected function getRenderTypes() {
     return [
-      'pre-wrapped' => '<div class="pre-wrapped">pre-wrapped</div>',
-      'pre-wrapped-leading-whitespace' => ' <div class="pre-wrapped-leading-whitespace">pre-wrapped-leading-whitespace</div>',
+      'pre-wrapped' => '<div class="pre-wrapped">pre-wrapped<script> var test;</script></div>',
+      'pre-wrapped-whitespace' => ' <div class="pre-wrapped-whitespace">pre-wrapped-whitespace</div>' . "\r\n",
       'not-wrapped' => 'not-wrapped',
       'comment-not-wrapped' => '<!-- COMMENT --><div class="comment-not-wrapped">comment-not-wrapped</div>',
       'mixed' => ' foo <!-- COMMENT -->  foo bar<div class="a class"><p>some string</p></div> additional not wrapped strings, <!-- ANOTHER COMMENT --> <p>final string</p>',
+      'inline' => 'inline <div>BLOCK LEVEL<script> var test;</script></div>',
+      'empty' => '',
     ];
   }
 
diff -u b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php
--- b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php
+++ b/core/tests/Drupal/FunctionalJavascriptTests/Ajax/AjaxTest.php
@@ -111,20 +111,20 @@
       // correctly.
       [
         'render_type' => 'pre-wrapped',
-        'expected' => '<div class="pre-wrapped processed">pre-wrapped</div>',
+        'expected' => '<div class="pre-wrapped processed">pre-wrapped<script> var test;</script></div>',
       ],
       // Test that no additional empty leading div is added when the return
       // value had a leading space and all top-level node elements (context) are
       // processed correctly.
       [
-        'render_type' => 'pre-wrapped-leading-whitespace',
-        'expected' => '<div class="pre-wrapped-leading-whitespace processed">pre-wrapped-leading-whitespace</div>',
+        'render_type' => 'pre-wrapped-whitespace',
+        'expected' => '<div class="pre-wrapped-whitespace processed">pre-wrapped-leading-whitespace</div>',
       ],
       // Test that not wrapped response data (text node) is inserted wrapped and
       // all top-level node elements (context) are processed correctly.
       [
         'render_type' => 'not-wrapped',
-        'expected' => '<span class="processed">not-wrapped</span>',
+        'expected' => '<span class="ajax-inline processed">not-wrapped</span>',
       ],
       // Test that top-level comments (which are not lead by text nodes) are
       // inserted without wrapper.
@@ -137,9 +137,18 @@
       [
         'method' => 'html',
         'render_type' => 'mixed',
-        'expected' => '<span class="processed"> foo <!-- COMMENT -->  foo bar</span><div class="a class processed"><p>some string</p></div><span class="processed"> additional not wrapped strings, <!-- ANOTHER COMMENT --> </span><p class="processed">final string</p>',
+        'expected' => '<div class="processed"> foo <!-- COMMENT -->  foo bar<div class="a class"><p>some string</p></div> additional not wrapped strings, <!-- ANOTHER COMMENT --> <p>final string</p></div>',
+      ],
+      // Test that inline response data.
+      [
+        'render_type' => 'inline',
+        'expected' => '<span class="ajax-inline processed">inline <div>BLOCK LEVEL<script> var test;</script></div></span>',
+      ],
+      // Test that empty response data.
+      [
+        'render_type' => 'empty',
+        'expected' => '',
       ],
-
     ];
 
     foreach (['html', 'replaceWith'] as $method) {
