Closed GoogleCodeExporter closed 8 years ago
Quick notice: hex2bin doesn't still exists in php 5.3, so i've not been able to
test your example; i tried using unpack("H*", ..) and hexdec(decbin(..)), but
they didn't work (an alert was correctly displayed).
Original comment by ctrlal...@gmail.com
on 29 Jan 2012 at 12:40
Are you using utf-8 encoding and content-type header for your pages?
Original comment by google...@pcforum.hu
on 29 Jan 2012 at 6:02
Testcase attached. Reproduces using original trunk sources flawlessly. I'm
using PHP 5.3.6/VC9, but that shouldn't matter.
Original comment by google...@pcforum.hu
on 29 Jan 2012 at 6:19
Attachments:
I tested a bit the code involved and the first idea i had was to completely
replace prado's TJavascript::encode() with json_encode.
This creates some easy-to-resolve issues (avoid encoding "javascript:" strings,
workaround INF(inite), fix controls like TDatePicker), but there's also a
bigger issue that needs to be solved.
Json_[en|de]code is only able to work on UTF8, while the actual TJavascript
methods takes different approaches:
- encode() and quoteString() seems to have been charset-agnostic at first, but
then quoteUTF8() has been added, caisung them to become UTF8-only;
- jsonEncode(), as you noticed, tries to convert from the
Application->Globalization->Charset to UTF8 before encoding;
- jsonDecode() doesn's validate its input, and will fail if it's not UTF8;
- the same jsonEncode() and jsonDecode() behaviours appears in the TJSON php
implementation.
Assuming that TJavaScript::encode() was written first as a
low-computational-cost alternative to a full php json implementation (php's
json extension was not available at the time of prado 3.0.0), and that nowadays
json_encode is surely quickier than any php-based alternative, the idea of just
trashing TJavascript::encode() can be meaningful.
This will make prado use UTF-8 only for javascript and ajax. I'd like to note
that our globalization stuff is already UTF8-only. As an example, setting in
application.xml
<module id="globalization" class="TGlobalization" Culture="zh" Charset="GB-2312" />
will make TDatePicker month and week days appear as mojibake.
So my idea would be to:
1. use json_encode() instead of encode();
2. fix the involved code;
3. deprecate TJSON to avoid people from using it;
4a. explicitly state in the documentation that everything related to Javascript
uses UTF8, or
4b. add proper _toUTF8 and _fromUTF8 routines to both php and javascript to be
able to convert from Application->Globalization->Charset to UTF8 and viceversa.
Sorry for derailing your ticket.
Original comment by ctrlal...@gmail.com
on 29 Jan 2012 at 8:23
Sorry, i answered without seeing your comment #2 and #3. Yes i'm using UTF8,
and here's a screenshot of the result:
Original comment by ctrlal...@gmail.com
on 29 Jan 2012 at 8:31
Attachments:
Side note: TPage actually tries to decode the postData in
processCallbackRequest:
// Decode Callback postData from UTF-8 to current Charset
if (($g=$this->getApplication()->getGlobalization(false))!==null &&
strtoupper($enc=$g->getCharset())!='UTF-8')
foreach ($this->_postData as $k=>$v)
$this->_postData[$k]=iconv('UTF-8',$enc.'//IGNORE',$v);
Original comment by ctrlal...@gmail.com
on 5 Feb 2012 at 5:35
I managed to reproduce the problem using your example code under osx.
Original comment by ctrlal...@gmail.com
on 9 Feb 2012 at 3:34
Your patch has been committed in r3103. Some parts have been changed:
- re-added mising JSMin() method;
- unified error control code for jsonEncode and jsonDecode, and added
conditional checks for the use of json_last_error() that requires php5.3;
- changed quoteString() behaviour to actually add quotes around the string,
avoiding the need to strip them off after json_encode()
Original comment by ctrlal...@gmail.com
on 9 Feb 2012 at 4:46
Patch needs corrections in TJavaScript::checkJsonError():
1. "switch (json_last_error())" should be "switch ($err = json_last_error())"
(otherwise there will be no $err for inclusion in the exception text)
2. also the text in the Exception next to the switch statement should be
changed from "JSON encode error" to "JSON encode/decode error" as it is called
both after encode and decode, and telling the developer that there was a "JSON
encode error" whereas the error might have occoured (and is most likely to
occour anyway) during a JSON-decode might be misleading, and make the developer
to look for problems at the wrong places. even better solution would be to pass
a string describing whether it was an encode/decode operation to the check
function, and include that one in the exception text as well, so it would tell
exactly whether the error was a decoding or encoding error.
Original comment by google...@pcforum.hu
on 10 Feb 2012 at 10:34
Good point on 1, i just missed it.
About point 2, i just removed the "encode" word from the error message, since
the stack strace already tells the developer the exact place where the
exception gets triggered:
#0 prado/framework/Web/Javascripts/TJavaScript.php(216):
TJavaScript::checkJsonError()
#1 prado/framework/Web/Javascripts/TJavaScript.php(86):
TJavaScript::jsonEncode('??)???9?4?O ?', 13)
#2 testcase/protected/Pages/Home.php(10): TJavaScript::quoteString('??)???9?4?O
?')
Original comment by ctrlal...@gmail.com
on 11 Feb 2012 at 8:40
Original issue reported on code.google.com by
google...@pcforum.hu
on 28 Jan 2012 at 7:04Attachments: