Skip to content

Commit 229a99b

Browse files
committed
Descriptions: Improved empty field handling, reduces whitespace
For #5724
1 parent 8e99fc6 commit 229a99b

File tree

3 files changed

+27
-1
lines changed

3 files changed

+27
-1
lines changed

app/Entities/Tools/EntityHtmlDescription.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public function getHtml(bool $raw = false): string
5050
return $html;
5151
}
5252

53+
$isEmpty = empty(trim(strip_tags($html)));
54+
if ($isEmpty) {
55+
return '<p></p>';
56+
}
57+
5358
return HtmlContentFilter::removeActiveContentFromHtmlString($html);
5459
}
5560

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<textarea component="wysiwyg-input"
22
option:wysiwyg-input:text-direction="{{ $locale->htmlDirection() }}"
33
id="description_html" name="description_html" rows="5"
4-
@if($errors->has('description_html')) class="text-neg" @endif>@if(isset($model) || old('description_html')){{ old('description_html') ?? $model->descriptionInfo()->getHtml() }}@endif</textarea>
4+
@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>
55
@if($errors->has('description_html'))
66
<div class="text-neg text-small">{{ $errors->first('description_html') }}</div>
77
@endif

tests/Entity/BookTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,4 +278,25 @@ public function test_show_view_displays_description_if_no_description_html_set()
278278
$resp = $this->asEditor()->get($book->getUrl());
279279
$resp->assertSee("<p>My great<br>\ndescription<br>\n<br>\nwith newlines</p>", false);
280280
}
281+
282+
public function test_description_with_only_br_tags_results_in_empty_p_tag_used_on_show()
283+
{
284+
$descriptions = [
285+
'<p><br></p>',
286+
'<p><br><br><br><br></p>',
287+
'<p><br><br><br></p><h1><br><br><br><br><br></h1>',
288+
];
289+
$book = $this->entities->book();
290+
$this->asEditor();
291+
292+
foreach ($descriptions as $descriptionTestCase) {
293+
$book->description_html = $descriptionTestCase;
294+
$book->save();
295+
296+
$resp = $this->get($book->getUrl());
297+
$html = $this->withHtml($resp);
298+
$descriptionHtml = $html->getInnerHtml('.book-content > div.text-muted:first-child');
299+
$this->assertEquals('<p></p>', $descriptionHtml);
300+
}
301+
}
281302
}

0 commit comments

Comments
 (0)