• Welcome to Simple Machines Community Forum. Please login or sign up.
October 17, 2021, 12:53:57 AM

News:

SMF 2.1 RC4 has been released! Try it out and help us test! :) Read more.


Likes can generate empty ul tag (validation)

Started by Antechinus, August 24, 2021, 10:32:56 PM

Previous topic - Next topic

Antechinus

August 24, 2021, 10:32:56 PM Last Edit: September 15, 2021, 01:18:00 PM by Diego Andrés
If likes are enabled, but there are no likes for a given post yet, the ul.floatleft tag will be generated but the child li's will not be. Result: an empty ul tag, which will break validation. One error for every post without any likes.

The same applies if likes are enabled and there are likes for a post, but a given member cannot like the post. It will still generate one error for every post these permissions apply to.

Code (Display.template.php) Select
echo '
<div class="under_message">';

// What about likes?
if (!empty($modSettings['enable_likes']))
{
echo '
<ul class="floatleft">';

if (!empty($message['likes']['can_like']))
{
echo '
<li class="smflikebutton" id="msg_', $message['id'], '_likes"', $ignoring ? ' style="display:none;"' : '', '>
<a href="', $scripturl, '?action=likes;ltype=msg;sa=like;like=', $message['id'], ';', $context['session_var'], '=', $context['session_id'], '" class="msg_like"><span class="main_icons ', $message['likes']['you'] ? 'unlike' : 'like', '"></span> ', $message['likes']['you'] ? $txt['unlike'] : $txt['like'], '</a>
</li>';
}

if (!empty($message['likes']['count']))
{
$context['some_likes'] = true;
$count = $message['likes']['count'];
$base = 'likes_';

if ($message['likes']['you'])
{
$base = 'you_' . $base;
$count--;
}

$base .= (isset($txt[$base . $count])) ? $count : 'n';

echo '
<li class="like_count smalltext">
', sprintf($txt[$base], $scripturl . '?action=likes;sa=view;ltype=msg;like=' . $message['id'] . ';' . $context['session_var'] . '=' . $context['session_id'], comma_format($count)), '
</li>';
}

echo '
</ul>';
}

// Show the quickbuttons, for various operations on posts.
template_quickbuttons($message['quickbuttons'], 'post');

echo '
</div><!-- .under_message -->

Antechinus

Should have put this in the OP: the easiest option here is to change the markup to a div, and the anchor, and one span.

It's not really a list of anything anyway, just a clickable icon and the text for the count. There's no real need to wrap the anchor in anything, AFAICT. With suitable CSS, this should be fine:

echo '
<div class="under_message">';

// What about likes?
if (!empty($modSettings['enable_likes']))
{
echo '
<div class="floatleft">';

if (!empty($message['likes']['can_like']))
{
echo '
<a href="', $scripturl, '?action=likes;ltype=msg;sa=like;like=', $message['id'], ';', $context['session_var'], '=', $context['session_id'], '" id="msg_', $message['id'], '_likes"', $ignoring ? ' style="display:none;"' : '', ' class="msg_like">
<span class="main_icons ', $message['likes']['you'] ? 'unlike' : 'like', '"></span>
', $message['likes']['you'] ? $txt['unlike'] : $txt['like'], '
</a>';
}

if (!empty($message['likes']['count']))
{
$context['some_likes'] = true;
$count = $message['likes']['count'];
$base = 'likes_';

if ($message['likes']['you'])
{
$base = 'you_' . $base;
$count--;
}

$base .= (isset($txt[$base . $count])) ? $count : 'n';

echo '
<span class="like_count smalltext">
', sprintf($txt[$base], $scripturl . '?action=likes;sa=view;ltype=msg;like=' . $message['id'] . ';' . $context['session_var'] . '=' . $context['session_id'], comma_format($count)), '
</span>';
}

echo '
</div>';
}

// Show the quickbuttons, for various operations on posts.
template_quickbuttons($message['quickbuttons'], 'post');

echo '
</div><!-- .under_message -->

Advertisement: