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..5a7bcca 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) {
@@ -1874,10 +1873,7 @@ protected function drupalProcessAjaxResponse($content, array $ajax_response, arr
           }
           if ($wrapperNode) {
             // 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>' . trim($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);
@@ -1936,7 +1932,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 +2079,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..7f13a48 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,8 +156,7 @@ 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);
+    $htmlDom = (new HTML5(['disable_html_ns' => TRUE]))->loadHTML($content);
     $elements = simplexml_import_dom($htmlDom);
 
     return $elements;
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);
