Skip to content

NOW MANAGED ON CODEBERG

Descriptions: Improved empty field handling, reduces whitespace

Dan Brown229a99ba24d7
diff --git a/app/Entities/Tools/EntityHtmlDescription.php b/app/Entities/Tools/EntityHtmlDescription.php
index b14deb257..795aaf4aa 100644
--- a/app/Entities/Tools/EntityHtmlDescription.php
+++ b/app/Entities/Tools/EntityHtmlDescription.php
@@ -50,6 +50,11 @@ class EntityHtmlDescription
return $html;
}
+ $isEmpty = empty(trim(strip_tags($html)));
+ if ($isEmpty) {
+ return '<p></p>';
+ }
+
return HtmlContentFilter::removeActiveContentFromHtmlString($html);
}
diff --git a/resources/views/form/description-html-input.blade.php b/resources/views/form/description-html-input.blade.php
index 983d2fb83..4b0a74df1 100644
--- a/resources/views/form/description-html-input.blade.php
+++ b/resources/views/form/description-html-input.blade.php
@@ -1,7 +1,7 @@
<textarea component="wysiwyg-input"
option:wysiwyg-input:text-direction="{{ $locale->htmlDirection() }}"
id="description_html" name="description_html" rows="5"
- @if($errors->has('description_html')) class="text-neg" @endif>@if(isset($model) || old('description_html')){{ old('description_html') ?? $model->descriptionInfo()->getHtml() }}@endif</textarea>
+ @if($errors->has('description_html')) class="text-neg" @endif>@if(isset($model) || old('description_html')){{ old('description_html') ?? $model->descriptionInfo()->getHtml() }}@else{{ '<p></p>' }}@endif</textarea>
@if($errors->has('description_html'))
<div class="text-neg text-small">{{ $errors->first('description_html') }}</div>
@endif
\ No newline at end of file
diff --git a/tests/Entity/BookTest.php b/tests/Entity/BookTest.php
index 5f0aabc38..6082c59de 100644
--- a/tests/Entity/BookTest.php
+++ b/tests/Entity/BookTest.php
@@ -278,4 +278,25 @@ class BookTest extends TestCase
$resp = $this->asEditor()->get($book->getUrl());
$resp->assertSee("<p>My great<br>\ndescription<br>\n<br>\nwith newlines</p>", false);
}
+
+ public function test_description_with_only_br_tags_results_in_empty_p_tag_used_on_show()
+ {
+ $descriptions = [
+ '<p><br></p>',
+ '<p><br><br><br><br></p>',
+ '<p><br><br><br></p><h1><br><br><br><br><br></h1>',
+ ];
+ $book = $this->entities->book();
+ $this->asEditor();
+
+ foreach ($descriptions as $descriptionTestCase) {
+ $book->description_html = $descriptionTestCase;
+ $book->save();
+
+ $resp = $this->get($book->getUrl());
+ $html = $this->withHtml($resp);
+ $descriptionHtml = $html->getInnerHtml('.book-content > div.text-muted:first-child');
+ $this->assertEquals('<p></p>', $descriptionHtml);
+ }
+ }
}