From 80204518a25bb98beb0790f06496f19ec66d61ca Mon Sep 17 00:00:00 2001 From: Dan Brown Date: Thu, 19 Feb 2026 23:25:00 +0000 Subject: [PATCH] Page Content: Better handling for empty content filtering For #6028 --- app/Util/HtmlDocument.php | 8 +++++++- tests/Entity/PageEditorTest.php | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/app/Util/HtmlDocument.php b/app/Util/HtmlDocument.php index 7517955b9..c1ec2cf41 100644 --- a/app/Util/HtmlDocument.php +++ b/app/Util/HtmlDocument.php @@ -103,7 +103,13 @@ class HtmlDocument */ public function getBody(): DOMNode { - return $this->document->getElementsByTagName('body')[0]; + $bodies = $this->document->getElementsByTagName('body'); + + if ($bodies->length === 0) { + return new DOMElement('body', ''); + } + + return $bodies[0]; } /** diff --git a/tests/Entity/PageEditorTest.php b/tests/Entity/PageEditorTest.php index 4cd3c1671..67283f704 100644 --- a/tests/Entity/PageEditorTest.php +++ b/tests/Entity/PageEditorTest.php @@ -282,4 +282,23 @@ class PageEditorTest extends TestCase $resp->assertOk(); $resp->assertDontSee('hellotherethisisaturtlemonster', false); } + + public function test_editor_html_filtered_does_not_cause_error_if_empty() + { + $emptyExamples = ['', '

', '

 

', ' ', "\n"]; + $editor = $this->users->editor(); + $page = $this->entities->page(); + $page->updated_by = $editor->id; + + foreach ($emptyExamples as $emptyExample) { + $page->html = $emptyExample; + $page->save(); + + $resp = $this->asAdmin()->get($page->getUrl('edit')); + $resp->assertOk(); + + $resp = $this->asAdmin()->get("/ajax/page/{$page->id}"); + $resp->assertOk(); + } + } }