News:

Wondering if this will always be free?  See why free is better.

Main Menu

Likes can generate empty ul tag (validation)

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

Previous topic - Next topic

Antechinus

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

Diego Andrés

This is not a bug, the code replaces the list with the HTML output of that template function.
Both are a list of 1 item, so it's never empty.
Edit: If it happens to be empty, I do not see any validation issues, still get valid HTML.

You cannot view this attachment.

SMF Tricks - Free & Premium Responsive Themes for SMF.

Diego Andrés

Update on this, found the relevant page for it:
https://html.spec.whatwg.org/multipage/grouping-content.html#the-ul-element

QuoteContent model:
    Zero or more li and script-supporting elements.

SMF Tricks - Free & Premium Responsive Themes for SMF.

Advertisement: