Skip to content

NOW MANAGED ON CODEBERG

Page Content: Better handling for empty content filtering

Dan Brown80204518a25b
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 = ['', '<p></p>', '<p>&nbsp;</p>', ' ', "\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();
+ }
+ }
}