diff --git a/core/modules/filter/src/Plugin/Filter/FilterCaption.php b/core/modules/filter/src/Plugin/Filter/FilterCaption.php
index 76a1125..627379e 100644
--- a/core/modules/filter/src/Plugin/Filter/FilterCaption.php
+++ b/core/modules/filter/src/Plugin/Filter/FilterCaption.php
@@ -72,16 +72,18 @@ public function process($text, $langcode) {
         $altered_html = drupal_render($filter_caption);
 
         // Load the altered HTML into a new DOMDocument and retrieve the element.
-        $updated_node = Html::load($altered_html)->getElementsByTagName('body')
+        $updated_nodes = Html::load($altered_html)->getElementsByTagName('body')
           ->item(0)
-          ->childNodes
-          ->item(0);
+          ->childNodes;
 
-        // Import the updated node from the new DOMDocument into the original
-        // one, importing also the child nodes of the updated node.
-        $updated_node = $dom->importNode($updated_node, TRUE);
-        // Finally, replace the original node with the new node.
-        $node->parentNode->replaceChild($updated_node, $node);
+        foreach ($updated_nodes as $updated_node) {
+          // Import the updated node from the new DOMDocument into the original
+          // one, importing also the child nodes of the updated node.
+          $updated_node = $dom->importNode($updated_node, TRUE);
+          $node->parentNode->insertBefore($updated_node, $node);
+        }
+        // Finally, remove the original data-caption node.
+        $node->parentNode->removeChild($node);
       }
 
       $result->setProcessedText(Html::serialize($dom))
diff --git a/core/modules/filter/src/Tests/FilterTwigDebugTest.php b/core/modules/filter/src/Tests/FilterTwigDebugTest.php
new file mode 100644
index 0000000..83302f1
--- /dev/null
+++ b/core/modules/filter/src/Tests/FilterTwigDebugTest.php
@@ -0,0 +1,114 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\filter\Tests\FilterTwigDebugTest.
+ */
+
+namespace Drupal\filter\Tests;
+
+use Drupal\Core\Render\RenderContext;
+use Drupal\simpletest\WebTestBase;
+use Drupal\filter\FilterPluginCollection;
+
+/**
+ * Tests Filter module filters with twig debugging on.
+ *
+ * @group filter
+ */
+class FilterTwigDebugTest extends WebTestBase {
+  /**
+   * Modules to enable.
+   *
+   * @var array
+   */
+  public static $modules = array('system', 'filter');
+
+  /**
+   * @var \Drupal\filter\Plugin\FilterInterface[]
+   */
+  protected $filters;
+
+  /**
+   * Ensures twig debugging is on in the current container.
+   */
+  protected function debugOn() {
+    // Enable debug, rebuild the service container, and clear all caches.
+    $parameters = $this->container->getParameter('twig.config');
+    if (! $parameters['debug']) {
+      $parameters['debug'] = TRUE;
+      $this->setContainerParameter('twig.config', $parameters);
+      $this->rebuildContainer();
+      $this->resetAll();
+    }
+  }
+
+  /**
+   * Ensures twig debugging is off in the current container.
+   */
+  protected function debugOff() {
+    // Disable debug, rebuild the service container, and clear all caches.
+    $parameters = $this->container->getParameter('twig.config');
+    if ($parameters['debug']) {
+      $parameters['debug'] = FALSE;
+      $this->setContainerParameter('twig.config', $parameters);
+      $this->rebuildContainer();
+      $this->resetAll();
+    }
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function setUp() {
+    parent::setUp();
+
+    $this->debugOn();
+
+    $manager = $this->container->get('plugin.manager.filter');
+    $bag = new FilterPluginCollection($manager, array());
+    $this->filters = $bag->getAll();
+  }
+
+  /**
+   * {@inheritdoc}
+   */
+  protected function tearDown() {
+    $this->debugOff();
+  }
+
+  /**
+   * Tests the caption filter with twig debugging on.
+   */
+  function testCaptionFilter() {
+    /** @var \Drupal\Core\Render\RendererInterface $renderer */
+    $renderer = \Drupal::service('renderer');
+    $filter = $this->filters['filter_caption'];
+
+    $test = function($input) use ($filter, $renderer) {
+      return $renderer->executeInRenderContext(new RenderContext(), function () use ($input, $filter) {
+        return $filter->process($input, 'und');
+      });
+    };
+
+    $attached_library = array(
+      'library' => array(
+        'filter/caption',
+      ),
+    );
+
+    // No data-caption attribute.
+    $input = '<img src="llama.jpg" />';
+    $expected = $input;
+    $this->assertIdentical($expected, $test($input)->getProcessedText());
+
+    // Data-caption attribute.
+    $input = '<img src="llama.jpg" data-caption="Loquacious llama!" />';
+    $expected = '<img src="llama.jpg" /><figcaption>Loquacious llama!</figcaption>';
+    $output = $test($input);
+    $output = $output->getProcessedText();
+    $this->assertTrue(strpos($output, $expected) !== false, "\"$output\" contains \"$expected\"");
+    $this->assertTrue(strpos($output, '<!-- THEME HOOK: \'filter_caption\' -->') !== false, 'filter_caption theme hook debug comment is present.');
+  }
+
+}
