Skip to content

Commit

Permalink
Fix HTML in site messages.
Browse files Browse the repository at this point in the history
If we detect that a site message contains HTML then don't use the
html_para filter to avoid adding extra <br> elements which mess up
spacing.
  • Loading branch information
chrismytton authored and dracos committed Feb 12, 2025
1 parent 556b7f1 commit e9952da
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 5 deletions.
15 changes: 15 additions & 0 deletions perllib/FixMyStreet/Template.pm
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ sub html_paragraph_email_factory : FilterFactory('html_para_email') {
}
}

=head2 html_paragraph_conditional
Calls html_paragraph, but only if the text doesn't include
a <p>, <ol>, or <ul>.
=cut

sub html_paragraph_conditional : Filter('html_para_conditional') {
my $text = shift;

Check warning on line 167 in perllib/FixMyStreet/Template.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/Template.pm#L167

Added line #L167 was not covered by tests
if ($text !~ /<(p|ol|ul)[^>]*>/) {
$text = html_paragraph($text);

Check warning on line 169 in perllib/FixMyStreet/Template.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/Template.pm#L169

Added line #L169 was not covered by tests
}
return $text;

Check warning on line 171 in perllib/FixMyStreet/Template.pm

View check run for this annotation

Codecov / codecov/patch

perllib/FixMyStreet/Template.pm#L171

Added line #L171 was not covered by tests
}

sub sanitize {
my ($text, $admin) = @_;

Expand Down
22 changes: 22 additions & 0 deletions t/app/controller/admin/sitemessage.t
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,28 @@ FixMyStreet::override_config {
$mech->get_ok('/');
$mech->content_contains('This is an OOH message');
};

subtest 'HTML vs non-HTML site messages' => sub {
# Test plain text message gets wrapped in paragraphs
$mech->get_ok('/admin/sitemessage');
my ($csrf) = $mech->content =~ /name="token" value="([^"]*)"/;
$mech->post_ok('/admin/sitemessage', {
site_message => "First line\n\nSecond line",
token => $csrf,
});
$mech->get_ok('/');
$mech->content_contains("<p>\nFirst line\n</p>\n\n<p>\nSecond line</p>");

# Test HTML message is left as-is
$mech->get_ok('/admin/sitemessage');
($csrf) = $mech->content =~ /name="token" value="([^"]*)"/;
$mech->post_ok('/admin/sitemessage', {
site_message => "<p>Test <strong>HTML</strong> message</p>\n\n<ul>\n<li>Item 1</li>\n<li>Item 2</li>\n</ul>",
token => $csrf,
});
$mech->get_ok('/');
$mech->content_contains("<p>Test <strong>HTML</strong> message</p>\r\n\r\n<ul>\r\n<li>Item 1</li>\r\n<li>Item 2</li>\r\n</ul>");
};
};

done_testing;
2 changes: 1 addition & 1 deletion templates/web/base/front/site-message.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[% site_message = c.cobrand.site_message %]
[% IF site_message && !rendered_site_message %]
<div class="site-message">
[% site_message | html_para %]
[% site_message | html_para_conditional %]
</div>
[% END %]
[% rendered_site_message = 1 %]
2 changes: 1 addition & 1 deletion templates/web/base/report/new/form_after_heading.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[% site_message = c.cobrand.site_message('reporting') %]
[% IF site_message %]
<div class="site-message__reporting">
[% site_message | safe | html_para %]
[% site_message | html_para_conditional %]
</div>
[% END %]

2 changes: 1 addition & 1 deletion templates/web/base/waste/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[% site_message = c.cobrand.site_message('waste') %]
[% IF site_message %]
<div class="site-message">
[% site_message | html_para %]
[% site_message | html_para_conditional %]
</div>
[% END %]
[% IF site_message_upcoming_downtime %]
Expand Down
2 changes: 1 addition & 1 deletion templates/web/bathnes/around/_postcode_form_post.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[% site_message = c.cobrand.site_message %]
[% IF site_message %]
<div class="front-main-other-issues-wrapper">
[% site_message | html_para %]
[% site_message | html_para_conditional %]
</div>
[% END %]
2 changes: 1 addition & 1 deletion templates/web/peterborough/waste/header.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@
[% site_message = c.cobrand.site_message('waste') %]
[% IF site_message %]
<div class="site-message">
[% site_message | html_para %]
[% site_message | html_para_conditional %]
</div>
[% END %]

0 comments on commit e9952da

Please sign in to comment.