JaquelineBrandao / yii

Automatically exported from code.google.com/p/yii
0 stars 0 forks source link

Javascrit confirmation box with new line characters impossible #285

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Hello.

I've been trying to get a javascript confirmation box with new lines in it
but couldn't get there. After doing some research I found out that there is
only one possible solution for the final jscript code:

- The \n character must be used with single quotes in the confirm() command
e.g. confirm('First line\n second line\n third line');

In CHtml.php, version 1.04, line 1627 it reads:
$confirm='confirm(\''.CJavaScript::quote($htmlOptions['confirm']).'\')';

The CJavaScript::quote() function there, escapes the special characters and
as a result we get: 
confirm('First line\\n second line\\n third line');

This doesn't create a new line but displays the message: "First line\n
second line\n third line" in a single row. 

Actually there is no way to get the code: confirm('First line\n second
line\n third line'); because the '\n' gets escaped and 'n' of course does not.

Do you think that CJavaScript::quote() function should be removed and leave
the responsibility to the programmer to properly escape his string?

Thanks,

A Yii fan.

Original issue reported on code.google.com by g.chatzidamianos@gmail.com on 1 May 2009 at 7:50

GoogleCodeExporter commented 9 years ago

After some research I figured out that the problem was caused by 
CJavaScript::quote
function where the string substitutions take place:
return
strtr($js,array("\t"=>'\t',"\n"=>'\n',"\r"=>'\r','"'=>'\"','\''=>'\\\'','\\'=>'\
\\\'));

More specifically, I removed the: '\\'=>'\\\\' part and doesn't escape the \
character any more.

Original comment by g.chatzidamianos@gmail.com on 1 May 2009 at 2:07

GoogleCodeExporter commented 9 years ago
In your original message, you can use: "something \n else", instead of 
'something \n
else'. This will give you the expected result when using CJavaScript::quote().

Original comment by qiang.xue on 5 May 2009 at 7:39

GoogleCodeExporter commented 9 years ago
When the confirm message is asigned to a variable before using it in the confim
message (i.e. it was saved in a database), then the \n is not escaped 
correctly.  How
can this be done correctly.

Original comment by photobo...@gmail.com on 10 May 2010 at 9:05