VirtueMart Forum

VirtueMart 2 + 3 + 4 => Virtuemart Development and bug reports => Topic started by: Pisu on October 26, 2011, 11:32:31 AM

Title: [FIXED]Javascript error on add to cart if translation contains quote
Post by: Pisu on October 26, 2011, 11:32:31 AM
In the file it-IT.com_virtuemart.ini, of the Italian translation, we have this line:

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

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.
Title: Re: Javascript error on add to cart if translation contains quote
Post by: Studio 42 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
Title: Re: Javascript error on add to cart if translation contains quote
Post by: Pisu 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.
Title: Re: Javascript error on add to cart if translation contains quote
Post by: Milbo on October 26, 2011, 19:08:06 PM
Heyhooo Pisu,

wb

Quote from: Pisu 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?
You just need two times the same content for both strings.

Quote from: Pisu on October 26, 2011, 18:24:23 PM
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.
Title: Re: Javascript error on add to cart if translation contains quote
Post by: Pisu on October 26, 2011, 22:40:32 PM
Function jPrice() in /administrator/components/com_virtuemart/helpers/config.php; this is current code:


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


$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?
Title: Re: Javascript error on add to cart if translation contains quote
Post by: Studio 42 on October 26, 2011, 23:29:43 PM
I added addslashes now, this must prevent error(work on my test)
Title: Re: Javascript error on add to cart if translation contains quote
Post by: Pisu on October 27, 2011, 15:48:44 PM
Quote from: Electrocity on October 26, 2011, 23:29:43 PM
I added addslashes now, this must prevent error(work on my test)

Thank you, addslashes is a good alternative.