Zen Cart Logo
Forums / General Questions / [Not a ZC bug] Truncated product description may fail HTML validation

[Not a ZC bug] Truncated product description may fail HTML validation

Locked

Views: 1,370

Results 1 to 9 of 9
This thread is locked. New replies are disabled.
21 Jul 2010, 9:45 AM
#1
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,873
Plugin Contributions:
7

[Not a ZC bug] Truncated product description may fail HTML validation

I would not really class this as bug but in the interests of 100% validation...

If the product description starts with a <p> tag and the truncated text loses the closing </p> it will fail validation.

I added an extra function into function zen_trunc_string to check for unbalanced p tags and add one on the end if necessary which seems to be working ok.

(Why do I have a p tag in the product description anyway? - I use <p class... to style the first paragraph differently,)

Steve

21 Jul 2010, 10:09 AM
#2
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Re: [Not a ZC bug] Truncated product description may fail HTML validation

Care to share the extra code you added?

21 Jul 2010, 1:31 PM
#3
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,873
Plugin Contributions:
7

Re: [Not a ZC bug] Truncated product description may fail HTML validation

WHAT! My hacks in public!
Well, in the interests of someone doing it better...

In/functions/functions_general.php:
in function zen_trunc_string (around line 1036),
I edited it to call a new function in
/extra_functions/my_new_functions.php just before returning the truncated string:

 function zen_trunc_string($str = "", $len = 150, $more = 'true') {
  //echo '$str = '.$str.'  $len = '.$len.'  $more = '.$more.'<br />';//check
    if ($str == "") return $str;
    if (is_array($str)) return $str;
    $str = trim($str);//strips whitespace from either end
    // if it's less than the size given, then return it
    if (strlen($str) <= $len) return $str;
    // else get that size of text
    $str = substr($str, 0, $len);//strips string to required length
    // backtrack to the end of a word
    if ($str != "") {
      // check to see if there are any spaces left
      if (!substr_count($str , " ")) {
        if ($more == 'true') $str .= "...";
	[B]$str = check_for_p($str);//check for unclosed paragraph tags[/B]
        return $str;
      }
      // backtrack
      while(strlen($str) && ($str[strlen($str)-1] != " ")) {
        $str = substr($str, 0, -1);
      }
      $str = substr($str, 0, -1);
	  
      if ($more == 'true') $str .= "...";
	  if ($more != 'true' and $more != 'false') $str .= $more;
	[B]$str = check_for_p($str);//steve check for unclosed paragraph tags[/B]
    }
    return $str;
  }

and the new function:

function check_for_p ($str) {
	  $needle = '<p';
      $opened_p = substr_count($str,$needle);
	  $needle = '</p>';
      $closed_p = substr_count($str,$needle);
	  if ($opened_p != $closed_p) {$str = $str.'</p>';} 
	  return $str;
}
21 Jul 2010, 3:11 PM
#4
ajeh avatar

ajeh

Oba-san

Join Date:
Sep 2003
Location:
Ohio
Posts:
62,757
Plugin Contributions:
1

Re: [Not a ZC bug] Truncated product description may fail HTML validation

Where are you seeing that if using:

<p>long description here</p>

that this isn't getting stripped on the Listings?

What version Zen Cart?

What happens if you switch to the Classic Template?

So far, I am not reproducing this ... :unsure:

21 Jul 2010, 3:57 PM
#5
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Re: [Not a ZC bug] Truncated product description may fail HTML validation

What you see is > <p>long descriptionwhich flags as invalid code, significant because Google appears to now take this into account and in extreme examples can even break the structure of a page.

torvista, that's a move in the right direction but a bit too specific. It will only deal with the problem of a missing paragraph closing tag. But other HTML elements that might be used in product descriptions and can get broken include DIVs, ULs, OLs, LIs and all the table elements. And even if its possible numberically to work out what's missing, they need to be closed in the right order.

I suspect that a more robust route approach (at least in PHP 5) would be to use DOMDocument::loadHTML or DOMDocument::loadXML, to create a complete structure based on the truncated string, but there may be some gotchas lurking in there.

21 Jul 2010, 5:02 PM
#6
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Re: [Not a ZC bug] Truncated product description may fail HTML validation

Hmmm. Torvista. Where are you actually seeing this manifest and what in what version of Zen Cart?

21 Jul 2010, 5:19 PM
#7
torvista avatar

torvista

Totally Zenned

Join Date:
Aug 2007
Location:
Gijón, Asturias, Spain
Posts:
2,873
Plugin Contributions:
7

Re: [Not a ZC bug] Truncated product description may fail HTML validation

:cry:Stand down...false alarm. I should have posted this in the Bug Reports-for-my-shop section.
I did not have a vanilla install available to check this at the time...and thought I understood what I was looking at.

Zen Cart 139d already deals with this issue by stripping html from the truncated text in the listings so there are no problems with unclosed tags.

I use the Boilerplate Text mod and Single Listing Template and other mods in my product listings which has given rise to this fault, not sure yet exactly which mod code or my implementation is at fault but thats not relevant to this thread. Added those some time ago when I knew nothing as opposed to almost nothing.

Sorry for the time wasted,
regards
Steve

21 Jul 2010, 5:20 PM
#8
kuroi avatar

kuroi

Totally Zenned

Join Date:
Apr 2006
Location:
London, UK
Posts:
10,475
Plugin Contributions:
11

Re: [Not a ZC bug] Truncated product description may fail HTML validation

LOL. I had a quick chat with Ajeh and we'd come to much the same conclusion. Glad you got in there first!

21 Jul 2010, 6:25 PM
#9
gjh42 avatar

gjh42

Black Belt

Join Date:
Jul 2005
Location:
Upstate NY
Posts:
21,876
Plugin Contributions:
8

Re: [Not a ZC bug] Truncated product description may fail HTML validation

That is a warning I generally give when discussing how to have HTML in the product listing - you need to make sure you either don't truncate the descriptions at all, or keep HTML tag sets inside or outside the truncation point, not crossing it.