diff --git a/core/modules/editor/editor.admin.inc b/core/modules/editor/editor.admin.inc
index 83bc2cf..67ebedf 100644
--- a/core/modules/editor/editor.admin.inc
+++ b/core/modules/editor/editor.admin.inc
@@ -92,8 +92,8 @@ function editor_image_upload_settings_form(Editor $editor) {
   $form['max_dimensions'] = array(
     '#type' => 'item',
     '#title' => t('Maximum dimensions'),
-    '#field_prefix' => '<div class="container-inline clearfix">',
-    '#field_suffix' => '</div>',
+    // '#field_prefix' => '<div class="container-inline clearfix">',
+    // '#field_suffix' => '</div>',
     '#description' => t('Images larger than these dimensions will be scaled down.'),
     '#states' => $show_if_image_uploads_enabled,
   );
diff --git a/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php b/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php
index 9ec3b67..829626a 100644
--- a/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php
+++ b/core/modules/image/src/Plugin/Field/FieldType/ImageItem.php
@@ -210,8 +210,8 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
       '#title' => t('Maximum image resolution'),
       '#element_validate' => array(array(get_class($this), 'validateResolution')),
       '#weight' => 4.1,
-      '#field_prefix' => '<div class="container-inline">',
-      '#field_suffix' => '</div>',
+      // '#field_prefix' => '<div class="container-inline">',
+      // '#field_suffix' => '</div>',
       '#description' => t('The maximum allowed image size expressed as WIDTH×HEIGHT (e.g. 640×480). Leave blank for no restriction. If a larger image is uploaded, it will be resized to reflect the given width and height. Resizing images on upload will cause the loss of <a href=":url">EXIF data</a> in the image.', array(':url' => 'http://en.wikipedia.org/wiki/Exchangeable_image_file_format')),
     );
     $element['max_resolution']['x'] = array(
@@ -237,8 +237,8 @@ public function fieldSettingsForm(array $form, FormStateInterface $form_state) {
       '#title' => t('Minimum image resolution'),
       '#element_validate' => array(array(get_class($this), 'validateResolution')),
       '#weight' => 4.2,
-      '#field_prefix' => '<div class="container-inline">',
-      '#field_suffix' => '</div>',
+      // '#field_prefix' => '<div class="container-inline">',
+      // '#field_suffix' => '</div>',
       '#description' => t('The minimum allowed image size expressed as WIDTH×HEIGHT (e.g. 640×480). Leave blank for no restriction. If a smaller image is uploaded, it will be rejected.'),
     );
     $element['min_resolution']['x'] = array(
diff --git a/core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php b/core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php
index cb8f584..5adca1a 100644
--- a/core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php
+++ b/core/modules/rdf/src/Tests/Field/FieldRdfaTestBase.php
@@ -7,6 +7,7 @@
 namespace Drupal\rdf\Tests\Field;
 
 use Drupal\field\Tests\FieldUnitTestBase;
+use Masterminds\HTML5;
 
 abstract class FieldRdfaTestBase extends FieldUnitTestBase {
 
@@ -150,8 +151,7 @@ protected function getAbsoluteUri($entity) {
    *   An array containing simplexml objects.
    */
   protected function parseContent($content) {
-    $htmlDom = new \DOMDocument();
-    @$htmlDom->loadHTML('<?xml encoding="UTF-8">' . $content);
+    $htmlDom = (new HTML5(['disable_html_ns' => TRUE]))->loadHTML($content);
     $elements = simplexml_import_dom($htmlDom);
 
     return $elements;
diff --git a/core/modules/simpletest/src/AssertContentTrait.php b/core/modules/simpletest/src/AssertContentTrait.php
index 00898be..a7e0d90 100644
--- a/core/modules/simpletest/src/AssertContentTrait.php
+++ b/core/modules/simpletest/src/AssertContentTrait.php
@@ -12,6 +12,7 @@
 use Drupal\Component\Utility\SafeMarkup;
 use Drupal\Component\Utility\Xss;
 use Drupal\Core\Render\RenderContext;
+use Masterminds\HTML5;
 use Symfony\Component\CssSelector\CssSelector;
 
 /**
@@ -127,10 +128,8 @@ protected function setDrupalSettings($settings) {
    */
   protected function parse() {
     if (!isset($this->elements)) {
-      // DOM can load HTML soup. But, HTML soup can throw warnings, suppress
-      // them.
-      $html_dom = new \DOMDocument();
-      @$html_dom->loadHTML('<?xml encoding="UTF-8">' . $this->getRawContent());
+      $html5 = new HTML5(['disable_html_ns' => TRUE]);
+      $html_dom = $html5->loadHTML($this->getRawContent());
       if ($html_dom) {
         $this->pass(SafeMarkup::format('Valid HTML found on "@path"', array('@path' => $this->getUrl())), 'Browser');
         // It's much easier to work with simplexml than DOM, luckily enough
diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php
index 667f705..0682f23 100644
--- a/core/modules/simpletest/src/WebTestBase.php
+++ b/core/modules/simpletest/src/WebTestBase.php
@@ -7,15 +7,14 @@
 
 namespace Drupal\simpletest;
 
-use Drupal\block\Entity\Block;
 use Drupal\Component\FileCache\FileCacheFactory;
 use Drupal\Component\Serialization\Json;
 use Drupal\Component\Serialization\Yaml;
 use Drupal\Component\Utility\Html;
 use Drupal\Component\Utility\NestedArray;
+use Drupal\Component\Utility\SafeMarkup;
 use Drupal\Component\Utility\UrlHelper;
 use Drupal\Core\Cache\Cache;
-use Drupal\Component\Utility\SafeMarkup;
 use Drupal\Core\Database\Database;
 use Drupal\Core\DrupalKernel;
 use Drupal\Core\Entity\EntityInterface;
@@ -29,6 +28,8 @@
 use Drupal\Core\Site\Settings;
 use Drupal\Core\StreamWrapper\PublicStream;
 use Drupal\Core\Url;
+use Drupal\block\Entity\Block;
+use Masterminds\HTML5;
 use Symfony\Component\DependencyInjection\ContainerInterface;
 use Symfony\Component\HttpFoundation\Request;
 use Zend\Diactoros\Uri;
@@ -1843,10 +1844,8 @@ protected function drupalProcessAjaxResponse($content, array $ajax_response, arr
     $ajax_settings += array(
       'method' => 'replaceWith',
     );
-    // DOM can load HTML soup. But, HTML soup can throw warnings, suppress
-    // them.
-    $dom = new \DOMDocument();
-    @$dom->loadHTML($content);
+    $html5 = new HTML5(['disable_html_ns' => TRUE]);
+    $dom = $html5->loadHTML($content);
     // XPath allows for finding wrapper nodes better than DOM does.
     $xpath = new \DOMXPath($dom);
     foreach ($ajax_response as $command) {
@@ -1873,14 +1872,12 @@ protected function drupalProcessAjaxResponse($content, array $ajax_response, arr
             $wrapperNode = $xpath->query('//' . $command['selector'])->item(0);
           }
           if ($wrapperNode) {
+            $command['data'] = strlen(trim($command['data'])) ? trim($command['data']) : $command['data'];
             // ajax.js adds an enclosing DIV to work around a Safari bug.
-            $newDom = new \DOMDocument();
-            // DOM can load HTML soup. But, HTML soup can throw warnings,
-            // suppress them.
-            @$newDom->loadHTML('<div>' . $command['data'] . '</div>');
+            $newDom = $html5->loadHTML('<div>' . $command['data'] . '</div>');
             // Suppress warnings thrown when duplicate HTML IDs are encountered.
             // This probably means we are replacing an element with the same ID.
-            $newNode = @$dom->importNode($newDom->documentElement->firstChild->firstChild, TRUE);
+            $newNode = $dom->importNode($newDom->documentElement->firstChild->firstChild, TRUE);
             $method = isset($command['method']) ? $command['method'] : $ajax_settings['method'];
             // The "method" is a jQuery DOM manipulation function. Emulate
             // each one using PHP's DOMNode API.
@@ -1936,7 +1933,7 @@ protected function drupalProcessAjaxResponse($content, array $ajax_response, arr
           break;
       }
     }
-    $content = $dom->saveHTML();
+    $content = $html5->saveHTML($dom);
     $this->setRawContent($content);
     $this->setDrupalSettings($drupal_settings);
   }
@@ -2083,12 +2080,13 @@ protected function cronRun() {
    */
   protected function checkForMetaRefresh() {
     if (strpos($this->getRawContent(), '<meta ') && $this->parse() && (!isset($this->maximumMetaRefreshCount) || $this->metaRefreshCount < $this->maximumMetaRefreshCount)) {
-      $refresh = $this->xpath('//meta[@http-equiv="Refresh"]');
-      if (!empty($refresh)) {
         // Parse the content attribute of the meta tag for the format:
         // "[delay]: URL=[page_to_redirect_to]".
-        if (preg_match('/\d+;\s*URL=(?<url>.*)/i', $refresh[0]['content'], $match)) {
-          $this->metaRefreshCount++;
+      preg_match('|<meta\s*http-equiv\s*=\s*"Refresh"\s*content="(.*)"\s*/>|', $this->getRawContent(), $matches);
+      if ($matches) {
+        // Parse the content attribute of the meta tag for the format:
+        // "[delay]: URL=[page_to_redirect_to]".
+        if (preg_match('/\d+;\s*URL=(?<url>.*)/i', $matches[1], $match)) {
           return $this->drupalGet($this->getAbsoluteUrl(Html::decodeEntities($match['url'])));
         }
       }
diff --git a/core/modules/system/src/Tests/Theme/FunctionsTest.php b/core/modules/system/src/Tests/Theme/FunctionsTest.php
index 5e31422..d4ec3ec 100644
--- a/core/modules/system/src/Tests/Theme/FunctionsTest.php
+++ b/core/modules/system/src/Tests/Theme/FunctionsTest.php
@@ -13,6 +13,7 @@
 use Drupal\Core\Session\UserSession;
 use Drupal\Core\Url;
 use Drupal\simpletest\WebTestBase;
+use Masterminds\HTML5;
 
 /**
  * Tests for common theme functions.
@@ -350,8 +351,7 @@ function testDrupalPreRenderLinks() {
     // it.
     $render_array = $base_array;
     $html = \Drupal::service('renderer')->renderRoot($render_array);
-    $dom = new \DOMDocument();
-    $dom->loadHTML($html);
+    $dom = (new HTML5())->loadHTML($html);
     $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered HTML.');
     $list_elements = $dom->getElementsByTagName('li');
     $this->assertEqual($list_elements->length, 3, 'Three "li" tags found in the rendered HTML.');
@@ -368,16 +368,14 @@ function testDrupalPreRenderLinks() {
     $child_html = \Drupal::service('renderer')->renderRoot($render_array['first_child']);
     $parent_html = \Drupal::service('renderer')->renderRoot($render_array);
     // First check the child HTML.
-    $dom = new \DOMDocument();
-    $dom->loadHTML($child_html);
+    $dom = (new HTML5())->loadHTML($child_html);
     $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered child HTML.');
     $list_elements = $dom->getElementsByTagName('li');
     $this->assertEqual($list_elements->length, 2, 'Two "li" tags found in the rendered child HTML.');
     $this->assertEqual($list_elements->item(0)->nodeValue, 'Parent link copy', 'First expected link found.');
     $this->assertEqual($list_elements->item(1)->nodeValue, 'First child link', 'Second expected link found.');
     // Then check the parent HTML.
-    $dom = new \DOMDocument();
-    $dom->loadHTML($parent_html);
+    $dom = (new HTML5())->loadHTML($parent_html);
     $this->assertEqual($dom->getElementsByTagName('ul')->length, 1, 'One "ul" tag found in the rendered parent HTML.');
     $list_elements = $dom->getElementsByTagName('li');
     $this->assertEqual($list_elements->length, 2, 'Two "li" tags found in the rendered parent HTML.');
diff --git a/core/modules/views/src/Tests/Handler/FieldWebTest.php b/core/modules/views/src/Tests/Handler/FieldWebTest.php
index 2d43a10..96ff373 100644
--- a/core/modules/views/src/Tests/Handler/FieldWebTest.php
+++ b/core/modules/views/src/Tests/Handler/FieldWebTest.php
@@ -14,6 +14,7 @@
 use Drupal\Core\Url;
 use Drupal\system\Tests\Cache\AssertPageCacheContextsAndTagsTrait;
 use Drupal\views\Views;
+use Masterminds\HTML5;
 
 /**
  * Tests fields from within a UI.
@@ -155,9 +156,8 @@ protected function assertNotSubString($haystack, $needle, $message = '', $group
    *   An array containing simplexml objects.
    */
   protected function parseContent($content) {
-    $htmlDom = new \DOMDocument();
-    @$htmlDom->loadHTML('<?xml encoding="UTF-8">' . $content);
-    $elements = simplexml_import_dom($htmlDom);
+    $htmlDom = (new HTML5(['disable_html_ns' => TRUE]))->loadHTML((string) $content);
+    $elements = @simplexml_import_dom($htmlDom);
 
     return $elements;
   }
diff --git a/core/modules/views/src/Tests/Plugin/DisplayFeedTest.php b/core/modules/views/src/Tests/Plugin/DisplayFeedTest.php
index 8032af3..bb4ae40 100644
--- a/core/modules/views/src/Tests/Plugin/DisplayFeedTest.php
+++ b/core/modules/views/src/Tests/Plugin/DisplayFeedTest.php
@@ -7,6 +7,7 @@
 
 namespace Drupal\views\Tests\Plugin;
 
+use Drupal\Component\Utility\SafeMarkup;
 use Drupal\views\Views;
 
 /**
@@ -61,7 +62,7 @@ public function testFeedOutput() {
     $this->config('system.site')->set('name', $site_name)->save();
 
     $this->drupalGet('test-feed-display.xml');
-    $result = $this->xpath('//title');
+    $result = $this->xpathXml('//title');
     $this->assertEqual($result[0], $site_name, 'The site title is used for the feed title.');
     $this->assertEqual($result[1], $node_title, 'Node title with HTML entities displays correctly.');
     // Verify HTML is properly escaped in the description field.
@@ -73,7 +74,7 @@ public function testFeedOutput() {
     $view->save();
 
     $this->drupalGet('test-feed-display.xml');
-    $result = $this->xpath('//title');
+    $result = $this->xpathXml('//title');
     $this->assertEqual($result[0], 'test_display_feed', 'The display title is used for the feed title.');
 
     // Add a block display and attach the feed.
@@ -115,7 +116,7 @@ public function testFeedFieldOutput() {
     ));
 
     $this->drupalGet('test-feed-display-fields.xml');
-    $result = $this->xpath('//title/a');
+    $result = $this->xpathXml('//title/a');
     $this->assertEqual($result[0], $node_title, 'Node title with HTML entities displays correctly.');
     // Verify HTML is properly escaped in the description field.
     $this->assertRaw('&lt;p&gt;A paragraph&lt;/p&gt;');
@@ -136,7 +137,7 @@ public function testDisabledFeed() {
 
     // Check that the rss header is output on the page display.
     $this->drupalGet('/test-attached-disabled');
-    $feed_header = $this->xpath('//link[@rel="alternate"]');
+    $feed_header = $this->xpathXml('//link[@rel="alternate"]');
     $this->assertEqual($feed_header[0]['type'], 'application/rss+xml', 'The feed link has the type application/rss+xml.');
     $this->assertTrue(strpos($feed_header[0]['href'], 'test-attached-disabled.xml'), 'Page display contains the correct feed URL.');
 
@@ -146,7 +147,7 @@ public function testDisabledFeed() {
 
     // Ensure there is no link rel present on the page.
     $this->drupalGet('/test-attached-disabled');
-    $result = $this->xpath('//link[@rel="alternate"]');
+    $result = $this->xpathXml('//link[@rel="alternate"]');
     $this->assertTrue(empty($result), 'Page display does not contain a feed header.');
 
     // Ensure the feed attachment returns 'Not found'.
@@ -154,4 +155,34 @@ public function testDisabledFeed() {
     $this->assertResponse(404);
   }
 
+  protected function parseXml() {
+    if (!isset($this->elements)) {
+      $dom = new \DOMDocument();
+      $success = $dom->loadXML($this->getRawContent());
+      if ($success) {
+        $this->pass(SafeMarkup::format('Valid XML found on "@path"', ['@path' => $this->getUrl()]), 'Browser');
+        // It's much easier to work with simplexml than DOM, luckily enough
+        // we can just simply import our DOM tree.
+        $this->elements = simplexml_import_dom($dom);
+      }
+    }
+    if ($this->elements === FALSE) {
+      $this->fail('Parsed page successfully.', 'Browser');
+    }
+
+    return $this->elements;
+  }
+
+  protected function xpathXml($xpath, array $arguments = []) {
+    if ($this->parseXml()) {
+      $xpath = $this->buildXPathQuery($xpath, $arguments);
+      $result = $this->elements->xpath($xpath);
+      // Some combinations of PHP / libxml versions return an empty array
+      // instead of the documented FALSE. Forcefully convert any falsish values
+      // to an empty array to allow foreach(...) constructions.
+      return $result ?: [];
+    }
+    return FALSE;
+  }
+
 }
diff --git a/core/modules/views/src/Tests/Plugin/StyleTest.php b/core/modules/views/src/Tests/Plugin/StyleTest.php
index c44eda6..ae20f63 100644
--- a/core/modules/views/src/Tests/Plugin/StyleTest.php
+++ b/core/modules/views/src/Tests/Plugin/StyleTest.php
@@ -7,12 +7,13 @@
 
 namespace Drupal\views\Tests\Plugin;
 
-use Drupal\views\Views;
-use Drupal\views\Tests\ViewTestBase;
-use Drupal\views_test_data\Plugin\views\row\RowTest;
 use Drupal\views\Plugin\views\row\Fields;
 use Drupal\views\ResultRow;
+use Drupal\views\Tests\ViewTestBase;
+use Drupal\views\Views;
+use Drupal\views_test_data\Plugin\views\row\RowTest;
 use Drupal\views_test_data\Plugin\views\style\StyleTest as StyleTestPlugin;
+use Masterminds\HTML5;
 
 /**
  * Tests general style functionality.
@@ -292,8 +293,7 @@ function testCustomRowClasses() {
    * Stores a view output in the elements.
    */
   protected function storeViewPreview($output) {
-    $htmlDom = new \DOMDocument();
-    @$htmlDom->loadHTML($output);
+    $htmlDom = (new HTML5(['disable_html_ns' => TRUE]))->loadHTML($output);
     if ($htmlDom) {
       // It's much easier to work with simplexml than DOM, luckily enough
       // we can just simply import our DOM tree.
diff --git a/core/modules/views/src/Tests/Plugin/StyleTestBase.php b/core/modules/views/src/Tests/Plugin/StyleTestBase.php
index 508fc12..e477797 100644
--- a/core/modules/views/src/Tests/Plugin/StyleTestBase.php
+++ b/core/modules/views/src/Tests/Plugin/StyleTestBase.php
@@ -26,7 +26,7 @@
    * Stores a view output in the elements.
    */
   function storeViewPreview($output) {
-    $html5 = new HTML5();
+    $html5 = new HTML5(['disable_html_ns' => TRUE]);
     $htmlDom = $html5->loadHTML('<html><body>' . $output . '</body></html>');
     if ($htmlDom) {
       // It's much easier to work with simplexml than DOM, luckily enough
diff --git a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
index 69941a9..50f8dbe 100644
--- a/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
+++ b/core/tests/Drupal/Tests/Core/Template/AttributeTest.php
@@ -7,13 +7,14 @@
 
 namespace Drupal\Tests\Core\Template;
 
+use Drupal\Component\Render\MarkupInterface;
 use Drupal\Component\Utility\Html;
 use Drupal\Core\Render\Markup;
 use Drupal\Core\Template\Attribute;
 use Drupal\Core\Template\AttributeArray;
 use Drupal\Core\Template\AttributeString;
 use Drupal\Tests\UnitTestCase;
-use Drupal\Component\Render\MarkupInterface;
+use Masterminds\HTML5;
 
 /**
  * @coversDefaultClass \Drupal\Core\Template\Attribute
@@ -440,8 +441,7 @@ protected function assertNoID($id, $html) {
    *   The number of results that are found.
    */
   protected function getXPathResultCount($query, $html) {
-    $document = new \DOMDocument;
-    $document->loadHTML($html);
+    $document = (new HTML5(['disable_html_ns' => TRUE]))->loadHTML($html);
     $xpath = new \DOMXPath($document);
 
     return $xpath->query($query)->length;
diff --git a/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php b/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
index e2ffb80..7c176fa 100644
--- a/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
+++ b/core/tests/Drupal/Tests/Core/Utility/LinkGeneratorTest.php
@@ -8,6 +8,7 @@
 namespace Drupal\Tests\Core\Utility {
 
 use Drupal\Component\Render\MarkupInterface;
+use Drupal\Core\DependencyInjection\ContainerBuilder;
 use Drupal\Core\GeneratedUrl;
 use Drupal\Core\Language\Language;
 use Drupal\Core\Link;
@@ -15,7 +16,7 @@
 use Drupal\Core\Url;
 use Drupal\Core\Utility\LinkGenerator;
 use Drupal\Tests\UnitTestCase;
-use Drupal\Core\DependencyInjection\ContainerBuilder;
+use Masterminds\HTML5;
 
 /**
  * @coversDefaultClass \Drupal\Core\Utility\LinkGenerator
@@ -524,8 +525,7 @@ public static function assertLink(array $properties, MarkupInterface $html, $cou
     }
 
     // Execute the query.
-    $document = new \DOMDocument;
-    $document->loadHTML($html);
+    $document = (new HTML5(['disable_html_ns' => TRUE]))->loadHTML($html);
     $xpath = new \DOMXPath($document);
 
     self::assertEquals($count, $xpath->query($query)->length);
@@ -543,8 +543,7 @@ public static function assertLink(array $properties, MarkupInterface $html, $cou
    *   The number of results that are found.
    */
   protected function assertNoXPathResults($query, $html) {
-    $document = new \DOMDocument;
-    $document->loadHTML($html);
+    $document = (new HTML5(['disable_html_ns' => TRUE]))->loadHTML($html);
     $xpath = new \DOMXPath($document);
 
     self::assertFalse((bool) $xpath->query($query)->length);
