From 7d37585e3cb15212f70a41127b01cdac0f1eba79 Mon Sep 17 00:00:00 2001 From: catch Date: Sun, 27 Sep 2015 09:55:26 +0100 Subject: [PATCH] Issue #2511306 by ElusiveMind, JeroenT, leolando.tan: Error: Call to a member function getElementsByTagName() in filter.module --- core/lib/Drupal/Component/Utility/Html.php | 18 ++++++++++-------- .../Tests/Component/Utility/HtmlTest.php | 15 ++++++++++++++- 2 files changed, 24 insertions(+), 9 deletions(-) diff --git a/core/lib/Drupal/Component/Utility/Html.php b/core/lib/Drupal/Component/Utility/Html.php index 76e05414413..69adbebe796 100644 --- a/core/lib/Drupal/Component/Utility/Html.php +++ b/core/lib/Drupal/Component/Utility/Html.php @@ -292,14 +292,16 @@ EOD; $body_node = $document->getElementsByTagName('body')->item(0); $html = ''; - foreach ($body_node->getElementsByTagName('script') as $node) { - static::escapeCdataElement($node); - } - foreach ($body_node->getElementsByTagName('style') as $node) { - static::escapeCdataElement($node, '/*', '*/'); - } - foreach ($body_node->childNodes as $node) { - $html .= $document->saveXML($node); + if ($body_node !== NULL) { + foreach ($body_node->getElementsByTagName('script') as $node) { + static::escapeCdataElement($node); + } + foreach ($body_node->getElementsByTagName('style') as $node) { + static::escapeCdataElement($node, '/*', '*/'); + } + foreach ($body_node->childNodes as $node) { + $html .= $document->saveXML($node); + } } return $html; } diff --git a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php index bca26232988..42984c425aa 100644 --- a/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php +++ b/core/tests/Drupal/Tests/Component/Utility/HtmlTest.php @@ -312,4 +312,17 @@ class HtmlTest extends UnitTestCase { $this->assertSame('<em>répété</em>', $escaped); } -} + /** + * Tests Html::serialize(). + * + * Resolves an issue by where an empty DOMDocument object sent to serialization would + * cause errors in getElementsByTagName() in the serialization function. + * + * @covers ::serialize + */ + public function testSerialize() { + $document = new \DOMDocument(); + $result = Html::serialize($document); + $this->assertSame('', $result); + } +} \ No newline at end of file