diff --git a/core/modules/simpletest/src/AssertContentTrait.php b/core/modules/simpletest/src/AssertContentTrait.php
index a425f4f954c66bcfc7b3433e68bd6401256a10c4..b0efbe5cc4a56d163e49834fc0319394367b594b 100644
--- a/core/modules/simpletest/src/AssertContentTrait.php
+++ b/core/modules/simpletest/src/AssertContentTrait.php
@@ -76,7 +76,10 @@ protected function setRawContent($content) {
    */
   protected function getTextContent() {
     if (!isset($this->plainTextContent)) {
-      $this->plainTextContent = Xss::filter($this->getRawContent(), array());
+      $raw_content = $this->getRawContent();
+      // Strip everything between the HEAD tags.
+      $raw_content = preg_replace('@<head>(.+?)</head>@si', '', $raw_content);
+      $this->plainTextContent = Xss::filter($raw_content, array());
     }
     return $this->plainTextContent;
   }
diff --git a/core/modules/simpletest/tests/src/Unit/AssertContentTraitTest.php b/core/modules/simpletest/tests/src/Unit/AssertContentTraitTest.php
new file mode 100644
index 0000000000000000000000000000000000000000..505a3b91ff42c781aff664175abbcfb91eab0184
--- /dev/null
+++ b/core/modules/simpletest/tests/src/Unit/AssertContentTraitTest.php
@@ -0,0 +1,54 @@
+<?php
+
+/**
+ * @file
+ * Contains \Drupal\Tests\simpletest\Unit\AssertContentTraitTest.
+ */
+
+namespace Drupal\Tests\simpletest\Unit;
+
+use Drupal\simpletest\AssertContentTrait;
+use Drupal\Tests\UnitTestCase;
+
+/**
+ * @coversDefaultClass \Drupal\simpletest\AssertContentTrait
+ * @group simpletest
+ */
+class AssertContentTraitTest extends UnitTestCase {
+
+  /**
+   * @covers ::getTextContent
+   */
+  public function testGetTextContent() {
+    $test = new TestClass();
+    $raw_content = <<<EOT
+
+<Head>
+<style>
+@import url("foo.css");
+</style>
+</head>
+<body>
+bar
+</body>
+EOT;
+    $test->_setRawContent($raw_content);
+    $this->assertNotContains('foo', $test->_getTextContent());
+    $this->assertNotContains('<body>', $test->_getTextContent());
+    $this->assertContains('bar', $test->_getTextContent());
+  }
+
+}
+
+class TestClass {
+  use AssertContentTrait;
+
+  public function _setRawContent($content) {
+    $this->setRawContent($content);
+  }
+
+  public function _getTextContent() {
+    return $this->getTextContent();
+  }
+
+}