Descriptions: Improved empty field handling, reduces whitespace

For #5724
This commit is contained in:
Dan Brown
2026-02-20 14:22:54 +00:00
parent 8e99fc6783
commit 229a99ba24
3 changed files with 27 additions and 1 deletions

View File

@@ -50,6 +50,11 @@ class EntityHtmlDescription
return $html;
}
$isEmpty = empty(trim(strip_tags($html)));
if ($isEmpty) {
return '<p></p>';
}
return HtmlContentFilter::removeActiveContentFromHtmlString($html);
}

View File

@@ -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

View File

@@ -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);
}
}
}