Author Topic: [FIXED]Javascript error on add to cart if translation contains quote  (Read 5050 times)

Pisu

  • Contributing Developer
  • Jr. Member
  • *
  • Posts: 451
  • ..still looking for..
    • Blog di Stefano Bagnatica
In the file it-IT.com_virtuemart.ini, of the Italian translation, we have this line:

Code: [Select]
COM_VIRTUEMART_MINICART_ERROR="c' è stato un errore nell' aggiornamento del tuo carrello."

But with this code, the "add to cart" button does not work. The minicart popup doesn't work and the cart remains empty.
I see the Javascript error "missing ; before statement".

So I have fixed with this line (single backslashes are not enough):

Code: [Select]
COM_VIRTUEMART_MINICART_ERROR="c\\' è stato un errore nell\\' aggiornamento del tuo carrello."

I think that this should not be a translator problem, VirtueMart should escape the string when using inside Javascript.
Pisu - Team VMItalia (Supporto Italiano VirtueMart)
www.stefanobagnatica.it | www.vmitalia.net

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4522
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: Javascript error on add to cart if translation contains quote
« Reply #1 on: October 26, 2011, 14:57:16 pm »
HI pisu,
You are right and we want change next the naming convention , that translator know, it's javascript
eg. COM_VIRTUEMART_MINICART_ERROR to COM_VIRTUEMART_MINICART_JS_ERROR
But in all case all special char have to be controlled each time they are used because in "standard strings" this can break the HTML.
if you put <b> but not ending </b> then bold is set for all HTML.
some browser correct it and you don't see it in last Firefox for eg. but in some case this can change all your layout.
Tks,

Patrick

Pisu

  • Contributing Developer
  • Jr. Member
  • *
  • Posts: 451
  • ..still looking for..
    • Blog di Stefano Bagnatica
Re: Javascript error on add to cart if translation contains quote
« Reply #2 on: October 26, 2011, 18:24:23 pm »
mmm I don't like very much the "naming convention" because if you want, later, do use this string for both Javascript and HTML message?

I think that if using in HTML you should use the raw string, and if using in Javascript you should do escape.

For HTML tag it's a different argument, because in a pure translation environment you should not have the abilty to insert format tags in language string, format is template-related and not language-related.

So for me would be OK to use htmlentities() function before printing a string in a HTML output.
Pisu - Team VMItalia (Supporto Italiano VirtueMart)
www.stefanobagnatica.it | www.vmitalia.net

Milbo

  • Virtuemart Projectleader
  • Administrator
  • Super Hero
  • *
  • Posts: 10144
  • VM3.9 Eagle Owl
    • VM3 Extensions
  • VirtueMart Version: VirtueMart 3 on joomla 3
Re: Javascript error on add to cart if translation contains quote
« Reply #3 on: October 26, 2011, 19:08:06 pm »
Heyhooo Pisu,

wb

mmm I don't like very much the "naming convention" because if you want, later, do use this string for both Javascript and HTML message?
You just need two times the same content for both strings.

I think that if using in HTML you should use the raw string, and if using in Javascript you should do escape.
is it so easy? You may show us the solution and sent to us by skype (write pn) or as attachment here.
Should I fix your bug, please support the VirtueMart project and become a member
______________________________________
Extensions approved by the core team: http://extensions.virtuemart.net/

Pisu

  • Contributing Developer
  • Jr. Member
  • *
  • Posts: 451
  • ..still looking for..
    • Blog di Stefano Bagnatica
Re: Javascript error on add to cart if translation contains quote
« Reply #4 on: October 26, 2011, 22:40:32 pm »
Function jPrice() in /administrator/components/com_virtuemart/helpers/config.php; this is current code:

Code: [Select]
$closeimage = JURI::root(true) .'/components/com_virtuemart/assets/images/facebox/closelabel.png';
$jsVars  = "siteurl = '". JURI::base( ) ."' ;\n" ;
$jsVars .= "vmCartText = '". JText::_('COM_VIRTUEMART_MINICART_ADDED_JS') ."' ;\n" ;
$jsVars .= "vmCartError = '". JText::_('COM_VIRTUEMART_MINICART_ERROR_JS') ."' ;\n" ;
$jsVars .= "loadingImage = '".JURI::root(true) ."/components/com_virtuemart/assets/images/facebox/loading.gif'  ;\n" ;
$jsVars .= "closeImage = '".$closeimage."' ; \n";
$jsVars .= "faceboxHtml = \"<div id='facebox' style='display:none;'><div class='popup'><div class='content'></div> <a href='#' class='close'><img src='".$closeimage."' title='close' class='close_image' /></a></div></div>\" ;\n";
$document = JFactory::getDocument();
$document->addScriptDeclaration($jsVars);

A solution could be escaping with json_encode, so you don't have to mind having quotes and double quotes:

Code: [Select]
$closeimage = JURI::root(true) .'/components/com_virtuemart/assets/images/facebox/closelabel.png';
$jsVars  = "siteurl = '". JURI::base( ) ."' ;\n" ;
$jsVars .= "vmCartText = ". json_encode(JText::_('COM_VIRTUEMART_MINICART_ADDED')) ." ;\n" ;
$jsVars .= "vmCartError = ". json_encode(JText::_('COM_VIRTUEMART_MINICART_ERROR')) ." ;\n" ;
$jsVars .= "loadingImage = '".JURI::root(true) ."/components/com_virtuemart/assets/images/facebox/loading.gif'  ;\n" ;
$jsVars .= "closeImage = '".$closeimage."' ; \n";
$jsVars .= "faceboxHtml = \"<div id='facebox' style='display:none;'><div class='popup'><div class='content'></div> <a href='#' class='close'><img src='".$closeimage."' title='close' class='close_image' /></a></div></div>\" ;\n";
$document = JFactory::getDocument();
$document->addScriptDeclaration($jsVars);

Isn't easy?
Pisu - Team VMItalia (Supporto Italiano VirtueMart)
www.stefanobagnatica.it | www.vmitalia.net

Studio 42

  • Contributing Developer
  • Sr. Member
  • *
  • Posts: 4522
  • Joomla & Virtuemart developper
    • Studio 42 - Virtuemart & Joomla extentions
  • VirtueMart Version: 2.6 & 3
Re: Javascript error on add to cart if translation contains quote
« Reply #5 on: October 26, 2011, 23:29:43 pm »
I added addslashes now, this must prevent error(work on my test)

Pisu

  • Contributing Developer
  • Jr. Member
  • *
  • Posts: 451
  • ..still looking for..
    • Blog di Stefano Bagnatica
Re: Javascript error on add to cart if translation contains quote
« Reply #6 on: October 27, 2011, 15:48:44 pm »
I added addslashes now, this must prevent error(work on my test)

Thank you, addslashes is a good alternative.
Pisu - Team VMItalia (Supporto Italiano VirtueMart)
www.stefanobagnatica.it | www.vmitalia.net